Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -453,46 +453,53 @@ from zoneinfo import ZoneInfo
|
|
| 453 |
|
| 454 |
def get_time_tool2(query: str) -> datetime:
|
| 455 |
try:
|
|
|
|
| 456 |
location_prompt = f"""
|
| 457 |
-
You are a location extractor. Given a user's query about time or date, return the location mentioned in it.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
- "現在在紐約幾點?" → New York
|
| 463 |
-
- "今天幾號?" → London
|
| 464 |
-
- "What date is today?" → London
|
| 465 |
|
| 466 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
"""
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
|
|
|
| 471 |
else:
|
| 472 |
-
|
| 473 |
-
except Exception as e:
|
| 474 |
-
location = "London"
|
| 475 |
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
|
|
|
|
|
|
|
|
|
| 481 |
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
return target_time.replace(tzinfo=ZoneInfo(tz_str)) # Add timezone to the future time
|
| 492 |
-
else:
|
| 493 |
-
return now.replace(tzinfo=ZoneInfo(tz_str)) # Default return the current datetime object
|
| 494 |
-
else:
|
| 495 |
-
return now # Default return the current datetime object
|
| 496 |
|
| 497 |
def weather_agent_tool(query: str) -> str:
|
| 498 |
"""Weather Agent: Return current, hourly, or historical weather info using WeatherAPI."""
|
|
|
|
| 453 |
|
| 454 |
def get_time_tool2(query: str) -> datetime:
|
| 455 |
try:
|
| 456 |
+
# Step 1: Get location from the query
|
| 457 |
location_prompt = f"""
|
| 458 |
+
You are a location extractor. Given a user's query about time or date, return the location mentioned in it.
|
| 459 |
+
If not found, return "London".
|
| 460 |
+
|
| 461 |
+
Query: "{query}"
|
| 462 |
+
"""
|
| 463 |
+
location_response = llm_gpt4.invoke(location_prompt)
|
| 464 |
+
location = location_response.content.strip() if isinstance(location_response, AIMessage) else str(location_response).strip()
|
| 465 |
|
| 466 |
+
# Step 2: Get the timezone of the location
|
| 467 |
+
tz_str = location_to_timezone(location)
|
| 468 |
+
now = datetime.now(ZoneInfo(tz_str))
|
|
|
|
|
|
|
|
|
|
| 469 |
|
| 470 |
+
# Step 3: Use GPT to determine the specific time
|
| 471 |
+
time_query_prompt = f"""
|
| 472 |
+
Given the current time in {location}, answer the following:
|
| 473 |
+
|
| 474 |
+
Query: "{query}"
|
| 475 |
+
|
| 476 |
+
Provide the exact time the user is referring to. If the query is asking for a time in the future or past, calculate that time relative to now.
|
| 477 |
"""
|
| 478 |
+
time_response = llm_gpt4.invoke(time_query_prompt)
|
| 479 |
+
|
| 480 |
+
if isinstance(time_response, AIMessage):
|
| 481 |
+
time_str = time_response.content.strip()
|
| 482 |
else:
|
| 483 |
+
time_str = str(time_response).strip()
|
|
|
|
|
|
|
| 484 |
|
| 485 |
+
# Step 4: If the time string contains specific time formats, adjust accordingly
|
| 486 |
+
try:
|
| 487 |
+
if "AM" in time_str or "PM" in time_str:
|
| 488 |
+
# If it's a time format, assume it's the intended time
|
| 489 |
+
target_time = datetime.strptime(time_str, "%I:%M %p")
|
| 490 |
+
else:
|
| 491 |
+
# Otherwise, parse the date and time
|
| 492 |
+
target_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
|
| 493 |
|
| 494 |
+
# Ensure it's in the correct timezone
|
| 495 |
+
target_time = target_time.replace(tzinfo=ZoneInfo(tz_str))
|
| 496 |
+
|
| 497 |
+
return target_time.strftime("%Y-%m-%d %H:%M:%S")
|
| 498 |
+
except Exception as e:
|
| 499 |
+
return f"Error in processing time: {e}"
|
| 500 |
+
|
| 501 |
+
except Exception as e:
|
| 502 |
+
return f"Error in retrieving location or time information: {e}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
|
| 504 |
def weather_agent_tool(query: str) -> str:
|
| 505 |
"""Weather Agent: Return current, hourly, or historical weather info using WeatherAPI."""
|