mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-02-20 17:59:56 +08:00
fix: 动态更新系统提示词中的运行时信息(时间戳)
问题: - system_prompt 在 Agent 初始化时固定,导致模型获取的时间信息过时 - 长时间运行的会话中,模型对时间判断不准确 解决方案: - 在 get_full_system_prompt() 中添加动态更新逻辑 - 每次获取系统提示词时,使用正则表达式替换运行时信息中的时间戳 - 保持其他运行时信息(模型、工作空间等)不变 测试: - 创建测试脚本验证时间动态更新功能 - 等待3秒后时间正确更新(22:19:45 -> 22:19:48)
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import json
|
||||
import time
|
||||
import threading
|
||||
import datetime
|
||||
import re
|
||||
|
||||
from common.log import logger
|
||||
from agent.protocol.models import LLMRequest, LLMModel
|
||||
@@ -105,8 +107,51 @@ class Agent:
|
||||
:return: Complete system prompt
|
||||
"""
|
||||
# Skills are now included in system_prompt by PromptBuilder
|
||||
# No need to append them here
|
||||
return self.system_prompt
|
||||
# Update runtime info (timestamp) dynamically before returning
|
||||
return self._update_runtime_info(self.system_prompt)
|
||||
|
||||
def _update_runtime_info(self, prompt: str) -> str:
|
||||
"""
|
||||
Update runtime information (timestamp) in the system prompt.
|
||||
|
||||
This ensures the model always has the current time, even if the
|
||||
agent was initialized hours ago.
|
||||
|
||||
:param prompt: Original system prompt
|
||||
:return: Updated system prompt with current time
|
||||
"""
|
||||
# Find the runtime info section
|
||||
runtime_section_pattern = r'(## 运行时信息\s*\n\s*\n)(当前时间: )([^\n]+)(\s+星期[一二三四五六日])(\s+\([^)]+\))?'
|
||||
|
||||
# Get current time info
|
||||
now = datetime.datetime.now()
|
||||
|
||||
# Get timezone
|
||||
try:
|
||||
offset = -time.timezone if not time.daylight else -time.altzone
|
||||
hours = offset // 3600
|
||||
minutes = (offset % 3600) // 60
|
||||
timezone_name = f"UTC{hours:+03d}:{minutes:02d}" if minutes else f"UTC{hours:+03d}"
|
||||
except Exception:
|
||||
timezone_name = "UTC"
|
||||
|
||||
# Chinese weekday mapping
|
||||
weekday_map = {
|
||||
'Monday': '星期一', 'Tuesday': '星期二', 'Wednesday': '星期三',
|
||||
'Thursday': '星期四', 'Friday': '星期五', 'Saturday': '星期六', 'Sunday': '星期日'
|
||||
}
|
||||
weekday_zh = weekday_map.get(now.strftime("%A"), now.strftime("%A"))
|
||||
|
||||
# Build new time string
|
||||
new_time = now.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# Replace the time in the prompt
|
||||
def replace_time(match):
|
||||
return f"{match.group(1)}{match.group(2)}{new_time} {weekday_zh} ({timezone_name})"
|
||||
|
||||
updated_prompt = re.sub(runtime_section_pattern, replace_time, prompt)
|
||||
|
||||
return updated_prompt
|
||||
|
||||
def refresh_skills(self):
|
||||
"""Refresh the loaded skills."""
|
||||
|
||||
Reference in New Issue
Block a user