ChienChung commited on
Commit
f27eda5
·
verified ·
1 Parent(s): 2e6217b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -452,21 +452,25 @@ weather_api_key = os.environ.get("WEATHER_API_KEY")
452
 
453
  def get_time_tool2(query: str) -> datetime:
454
  try:
455
- # Step 1: Get location from the query
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
  If not found, return "London".
459
 
460
  Query: "{query}"
461
  """
462
- location_response = llm_gpt4.invoke(location_prompt)
463
- location = location_response.content.strip() if isinstance(location_response, AIMessage) else str(location_response).strip()
 
 
 
 
464
 
465
- # Step 2: Get the timezone of the location
466
- tz_str = location_to_timezone(location)
467
- now = datetime.now(ZoneInfo(tz_str))
468
 
469
- # Step 3: Use GPT to determine the specific time
470
  time_query_prompt = f"""
471
  Given the current time in {location}, answer the following:
472
 
@@ -474,32 +478,28 @@ def get_time_tool2(query: str) -> datetime:
474
 
475
  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.
476
  """
477
- time_response = llm_gpt4.invoke(time_query_prompt)
 
 
 
 
 
478
 
479
- if isinstance(time_response, AIMessage):
480
- time_str = time_response.content.strip()
 
 
481
  else:
482
- time_str = str(time_response).strip()
483
-
484
- # Step 4: If the time string contains specific time formats, adjust accordingly
485
- try:
486
- # Check if time string contains AM/PM
487
- if "AM" in time_str or "PM" in time_str:
488
- target_time = datetime.strptime(time_str, "%I:%M %p")
489
- else:
490
- # Otherwise, parse the date and time
491
- target_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
492
 
493
- # Ensure it's in the correct timezone
494
- target_time = target_time.replace(tzinfo=ZoneInfo(tz_str))
495
 
496
- return target_time.strftime("%Y-%m-%d %H:%M:%S")
497
- except Exception as e:
498
- return f"Error in processing time: {e}"
499
 
500
  except Exception as e:
501
- return f"Error in retrieving location or time information: {e}"
502
-
503
  def weather_agent_tool(query: str) -> str:
504
  """Weather Agent: Return current, hourly, or historical weather info using WeatherAPI."""
505
  try:
 
452
 
453
  def get_time_tool2(query: str) -> datetime:
454
  try:
455
+ # Step 1: 獲取地點
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
  If not found, return "London".
459
 
460
  Query: "{query}"
461
  """
462
+ location_response = openai.Completion.create(
463
+ model="text-davinci-003",
464
+ prompt=location_prompt,
465
+ max_tokens=50
466
+ )
467
+ location = location_response.choices[0].text.strip()
468
 
469
+ # Step 2: 獲取當前時間(當地時間)
470
+ tz_str = location_to_timezone(location) # 從地點獲取時區
471
+ now = datetime.now(ZoneInfo(tz_str)) # 當地時間
472
 
473
+ # Step 3: GPT 推算具體時間
474
  time_query_prompt = f"""
475
  Given the current time in {location}, answer the following:
476
 
 
478
 
479
  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.
480
  """
481
+ time_response = openai.Completion.create(
482
+ model="text-davinci-003",
483
+ prompt=time_query_prompt,
484
+ max_tokens=50
485
+ )
486
+ time_str = time_response.choices[0].text.strip()
487
 
488
+ # Step 4: 解析並格式化時間
489
+ # 嘗試解析時間
490
+ if "AM" in time_str or "PM" in time_str:
491
+ target_time = datetime.strptime(time_str, "%I:%M %p")
492
  else:
493
+ target_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
 
 
 
 
 
 
 
 
 
494
 
495
+ # 添加時區
496
+ target_time = target_time.replace(tzinfo=ZoneInfo(tz_str))
497
 
498
+ return target_time.strftime("%Y-%m-%d %H:%M:%S")
 
 
499
 
500
  except Exception as e:
501
+ return f"Error in processing time: {e}"
502
+
503
  def weather_agent_tool(query: str) -> str:
504
  """Weather Agent: Return current, hourly, or historical weather info using WeatherAPI."""
505
  try: