ChienChung commited on
Commit
df014c9
·
verified ·
1 Parent(s): 708ef8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -33
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. If not found, return "London".
 
 
 
 
 
 
458
 
459
- Examples:
460
- - "What's the time in Tokyo now?" → Tokyo
461
- - "今天台北幾點?" → Taipei
462
- - "現在在紐約幾點?" → New York
463
- - "今天幾號?" → London
464
- - "What date is today?" → London
465
 
466
- Now process this query: "{query}"
 
 
 
 
 
 
467
  """
468
- location_response = llm_gpt4.invoke(location_prompt)
469
- if isinstance(location_response, AIMessage):
470
- location = location_response.content.strip()
 
471
  else:
472
- location = str(location_response).strip()
473
- except Exception as e:
474
- location = "London"
475
 
476
- location_key = location.lower()
477
- tz_str = location_to_timezone(location)
478
-
479
- # Get the correct local time based on the extracted location
480
- now = datetime.now(ZoneInfo(tz_str))
 
 
 
481
 
482
- # Handle AM/PM format in the query result
483
- q_lower = query.lower()
484
- if any(k in q_lower for k in ["date", "幾號", "today", "day"]):
485
- return now.date() # Return date object
486
- elif any(k in q_lower for k in ["time", "幾點", "現在"]):
487
- # If the query asks for time in the future (e.g., "2 hours later"), calculate and return future time
488
- if "hour" in query: # Detects if the query is asking for time in hours later
489
- hours_later = int(query.split(" ")[-2]) # Extracts the number of hours from query
490
- target_time = now + timedelta(hours=hours_later)
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."""