ChienChung commited on
Commit
16579e9
·
verified ·
1 Parent(s): 8b5a74b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -19
app.py CHANGED
@@ -466,44 +466,57 @@ def get_time_tool2(query: str) -> datetime:
466
  tz_str = location_to_timezone(location)
467
  now = datetime.now(ZoneInfo(tz_str))
468
 
469
- # Step 3: FewShot + 格式限制 prompt 推出正確格式的時間
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
470
  time_query_prompt = f"""
471
  You are a time interpreter. Based on the user's query, calculate the **exact target time** they are referring to.
472
  Today is: {now.strftime('%A')}, {now.strftime('%Y-%m-%d %H:%M:%S')} (local time)
473
 
474
- Please return the result in **exact format**: `YYYY-MM-DD HH:MM:SS` (24-hour clock, no timezone info).
475
  Only return the time string — no explanation, no extra words.
476
 
477
  ### Examples:
478
-
479
- User Query: "two hours later"
480
- → 2025-04-02 17:30:00
481
-
482
- User Query: "tomorrow at 3pm"
483
- → 2025-04-03 15:00:00
484
-
485
- User Query: "昨天下午五點"
486
- → 2025-04-01 17:00:00
487
-
488
- User Query: "later this evening"
489
- → 2025-04-02 20:00:00
490
-
491
- User Query: "現在"
492
- → 2025-04-02 15:30:00
493
 
494
  ### Now process:
495
  User Query: "{query}"
496
 
497
  """
 
498
  time_response = llm_gpt4.invoke(time_query_prompt)
499
  time_str = time_response.content.strip() if isinstance(time_response, AIMessage) else str(time_response).strip()
500
 
501
- # Step 4: 嘗試解析時間
502
  try:
503
  target_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
504
  target_time = target_time.replace(tzinfo=ZoneInfo(tz_str))
505
  return target_time # ✅ 回傳 datetime,不是 str
506
- except Exception as e:
507
  return f"Failed to parse time string from LLM: '{time_str}'"
508
 
509
  except Exception as e:
 
466
  tz_str = location_to_timezone(location)
467
  now = datetime.now(ZoneInfo(tz_str))
468
 
469
+ # Step 3: 動態 few-shot prompt(每次更新 based on now)
470
+ examples = [
471
+ ("five hours later", now + timedelta(hours=5)),
472
+ ("later", now + timedelta(hours=2)),
473
+ ("soon", now + timedelta(minutes=30)),
474
+ ("shortly", now + timedelta(minutes=15)),
475
+ ("after a while", now + timedelta(hours=1)),
476
+ ("tomorrow at 3pm", now.replace(hour=15, minute=0, second=0) + timedelta(days=1)),
477
+ ("the day after tomorrow at 10am", now.replace(hour=10, minute=0, second=0) + timedelta(days=2)),
478
+ ("last Monday 9am", (now - timedelta(days=(now.weekday() + 7))).replace(hour=9, minute=0, second=0)),
479
+ ("next Monday", (now + timedelta(days=(7 - now.weekday()))).replace(hour=12, minute=0, second=0)),
480
+ ("last Friday", (now - timedelta(days=(now.weekday() - 4 + 7) % 7)).replace(hour=12, minute=0, second=0)),
481
+ ("next Friday", (now + timedelta(days=(4 - now.weekday() + 7) % 7)).replace(hour=12, minute=0, second=0)),
482
+ ("in 10 hours", now + timedelta(hours=10)),
483
+ ("this weekend", (now + timedelta(days=(5 - now.weekday()) % 7)).replace(hour=10, minute=0, second=0)),
484
+ ("next weekend", (now + timedelta(days=((5 - now.weekday()) % 7) + 7)).replace(hour=10, minute=0, second=0)),
485
+ ("下週一下午三點", (now + timedelta(days=(7 - now.weekday() + 0) % 7)).replace(hour=15, minute=0, second=0)),
486
+ ("昨天下午五點", (now - timedelta(days=1)).replace(hour=17, minute=0, second=0)),
487
+ ("昨天早上八點", (now - timedelta(days=1)).replace(hour=8, minute=0, second=0)),
488
+ ("later this evening", now.replace(hour=20, minute=0, second=0)),
489
+ ("現在", now),
490
+ ("last month", (now - timedelta(days=30)).replace(hour=12, minute=0, second=0)),
491
+ ("early tomorrow morning", now.replace(hour=6, minute=0, second=0) + timedelta(days=1)),
492
+ ]
493
+ examples_str = "\n".join([f'User Query: "{q}" → {dt.strftime("%Y-%m-%d %H:%M:%S")}' for q, dt in examples])
494
+
495
+ # Step 4: 构建完整 prompt
496
  time_query_prompt = f"""
497
  You are a time interpreter. Based on the user's query, calculate the **exact target time** they are referring to.
498
  Today is: {now.strftime('%A')}, {now.strftime('%Y-%m-%d %H:%M:%S')} (local time)
499
 
500
+ Please return the result in this **exact format**: `YYYY-MM-DD HH:MM:SS` (24-hour clock, no timezone info).
501
  Only return the time string — no explanation, no extra words.
502
 
503
  ### Examples:
504
+ {examples_str}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505
 
506
  ### Now process:
507
  User Query: "{query}"
508
 
509
  """
510
+
511
  time_response = llm_gpt4.invoke(time_query_prompt)
512
  time_str = time_response.content.strip() if isinstance(time_response, AIMessage) else str(time_response).strip()
513
 
514
+ # Step 5: 嘗試解析時間
515
  try:
516
  target_time = datetime.strptime(time_str, "%Y-%m-%d %H:%M:%S")
517
  target_time = target_time.replace(tzinfo=ZoneInfo(tz_str))
518
  return target_time # ✅ 回傳 datetime,不是 str
519
+ except Exception:
520
  return f"Failed to parse time string from LLM: '{time_str}'"
521
 
522
  except Exception as e: