Description
It seems that the current prompt implementation struggles to reliably handle relative time references, such as "yesterday," "last week," "next week," or "tomorrow." This functionality is often crucial for scoping document retrieval based on content where specific dates are mentioned (e.g., roadmaps), particularly when a user's question relies on their current local time.
Key Considerations and Suggestions
-
Dynamically Capturing User Time
To properly address queries with relative time, you can dynamically inject the user's current local time into the prompt. This can be done by capturing the time from the user's browser instead of relying solely on the backend's time zone, as this avoids potential discrepancies caused by server-side caching. -
Time Zone Handling
The time captured from the user's browser will inherently be in their local time zone. If needed, this can be converted to UTC or any other time zone for consistency in calculations or storage. -
Parsing Relative Time
When processing queries involving relative time expressions, parse the user's current time and calculate the corresponding date dynamically (e.g., "yesterday" is derived by subtracting one day from the captured time). -
Avoiding Cached Time Issues
Ensure that any caching mechanism doesn’t serve outdated time information. Each time-sensitive query should dynamically incorporate the updated current time.
Implementation Example
Frontend: Capturing and Sending User Time
Use JavaScript to capture the user's local time and send it to the backend for processing.
// Fetch current date and time in the user's timezone (local)
const userTime = new Date().toLocaleString(); // Local string representation of time
// Send the current time to your backend
fetch('/set-user-time', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ user_time: userTime })
});
Backend: Using User Time in the Prompt
Python can process the passed time and dynamically include it in the prompt template.
from datetime import datetime, timedelta
# Assuming `user_time` is received from the frontend
user_time = "2025-01-17 15:30:00" # Example time received from the front end
# Parse the user time
user_time_dt = datetime.strptime(user_time, '%Y-%m-%d %H:%M:%S')
# Calculate relative times
yesterday = user_time_dt - timedelta(days=1)
last_week = user_time_dt - timedelta(weeks=1)
# Include the dynamic times in your prompt
query_prompt_few_shots = [
{"role": "user", "content": "What happened yesterday?"},
{"role": "assistant", "content": f"The current time is {user_time_dt.strftime('%Y-%m-%d %H:%M:%S')}, and yesterday's date was {yesterday.strftime('%Y-%m-%d %H:%M:%S')}."},
]
Prompt Updates for Relative Time
When introducing relative time parsing, ensure that the prompt dynamically adapts to the user's current time. For example:
query_prompt_few_shots = [
{"role": "user", "content": "What is the date and time now?"},
{"role": "assistant", "content": f"The current date and time is {user_time_dt.strftime('%Y-%m-%d %H:%M:%S')}."},
{"role": "user", "content": "What events are planned for next week?"},
{"role": "assistant", "content": f"Based on the current date, next week's dates start from {(user_time_dt + timedelta(weeks=1)).strftime('%Y-%m-%d')}."},
]
By capturing and dynamically incorporating the user's local time, you ensure accurate and context-aware handling of relative time queries while avoiding backend caching pitfalls.