Can anyone offer instructions on how to have a chatbot get the current date to use in the role? Thank you for your help!
The underlying language models all have knowledge cutoff dates. For example, many OpenAI models have a cutoff date of October 2023.
In order to get up-to-date information, I recommend connecting the google search action.
If you want to get today’s date, there is a dedicated Action for exactly that. In the Act tab, go the community actions and then search for time. Connect it. It won’t work exactly the way you want it to (it must be explicitly triggered) but you can trigger it at the beginning of each conversation.
1 Like
Hi @LaurelRose,
If you want the correct time for any country in the world, you can create a custom action that connects to the timeapi service.
Code below
import requests
def world_time_api(city: str, country: str):
"""
Retrieve the correct time for the given location
Args:
city (string): User location
country (string): USer's Country
"""
# Insert your PYTHON code below. You can access environment variables using os.environ[].
# Currently, only the requests library is supported, but more libraries will be available soon.
# Use print statements or return values to display results to the user.
# If you save a png, pdf, csv, jpg, webp, gif, or html file in the root directory, it will be automatically displayed to the user.
# You do not have to call this function as the bot will automatically call and fill in the parameters.
# Format the timeZone parameter for the API call
time_zone = f"{country}/{city}"
# World Time API endpoint
url = f"https://timeapi.io/api/time/current/zone?timeZone={time_zone}"
try:
# Send the GET request to the World Time API
response = requests.get(url, timeout=10)
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
response_data = response.json()
# Extract relevant fields from the response
current_date = response_data.get("date", "Date not available")
current_time = response_data.get("time", "Time not available")
day_of_week = response_data.get("dayOfWeek", "Day of the week not available")
time_zone = response_data.get("timeZone", "Time zone not available")
# Formulate the response message
answer = (
f"The current time in {city}, {country} is {current_time} on {current_date}. "
f"It is {day_of_week} and the time zone is {time_zone}."
)
# Return the response to the user
print(answer)
return answer
else:
# Handle non-200 HTTP responses
error_message = f"Failed to retrieve the time. Status Code: {response.status_code}. Response: {response.text}"
print(error_message)
return error_message
except requests.exceptions.Timeout:
# Handle request timeout
timeout_message = "The request timed out. Please try again later."
print(timeout_message)
return timeout_message
except Exception as e:
# Handle general exceptions
error_message = f"An error occurred: {e}"
print(error_message)
return error_message
# Example Usage
city_name = "Melbourne"
country_name = "Australia"
world_time_api(city_name, country_name)