Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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:
|
| 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 =
|
| 463 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 464 |
|
| 465 |
-
# Step 2:
|
| 466 |
-
tz_str = location_to_timezone(location)
|
| 467 |
-
now = datetime.now(ZoneInfo(tz_str))
|
| 468 |
|
| 469 |
-
# Step 3:
|
| 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 =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 478 |
|
| 479 |
-
|
| 480 |
-
|
|
|
|
|
|
|
| 481 |
else:
|
| 482 |
-
|
| 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 |
-
|
| 494 |
-
|
| 495 |
|
| 496 |
-
|
| 497 |
-
except Exception as e:
|
| 498 |
-
return f"Error in processing time: {e}"
|
| 499 |
|
| 500 |
except Exception as e:
|
| 501 |
-
return f"Error in
|
| 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:
|