diff --git a/main/xiaozhi-server/core/handle/intentHandler.py b/main/xiaozhi-server/core/handle/intentHandler.py index b1486ab9..23b2d0aa 100644 --- a/main/xiaozhi-server/core/handle/intentHandler.py +++ b/main/xiaozhi-server/core/handle/intentHandler.py @@ -98,16 +98,16 @@ async def process_intent_result(conn, intent_result, original_text): def process_context_result(): conn.dialogue.put(Message(role="user", content=original_text)) - # 使用现有的 prompt_manager 获取完整上下文 - from core.utils.prompt_manager import PromptManager - prompt_manager = PromptManager(conn.config, conn.logger) + from core.utils.current_time import get_current_time_info + + current_time, today_date, today_weekday, lunar_date = get_current_time_info() # 构建带上下文的基础提示 - context_prompt = prompt_manager.build_enhanced_prompt( - "当前时间:{{current_time}}\n今天日期:{{today_date}} ({{today_weekday}})\n今天农历:{{lunar_date}}\n用户所在城市:{{local_address}}", - conn.device_id, - getattr(conn, 'client_ip', None) - ) + context_prompt = f"""当前时间:{current_time} + 今天日期:{today_date} ({today_weekday}) + 今天农历:{lunar_date} + + 请根据以上信息回答用户的问题:{original_text}""" response = conn.intent.replyResult(context_prompt, original_text) speak_txt(conn, response) diff --git a/main/xiaozhi-server/core/utils/current_time.py b/main/xiaozhi-server/core/utils/current_time.py new file mode 100644 index 00000000..714dd50f --- /dev/null +++ b/main/xiaozhi-server/core/utils/current_time.py @@ -0,0 +1,68 @@ +""" +时间工具模块 +提供统一的时间获取功能 +""" + +import cnlunar +from datetime import datetime + +WEEKDAY_MAP = { + "Monday": "星期一", + "Tuesday": "星期二", + "Wednesday": "星期三", + "Thursday": "星期四", + "Friday": "星期五", + "Saturday": "星期六", + "Sunday": "星期日", +} + + +def get_current_time() -> str: + """ + 获取当前时间字符串 (格式: HH:MM) + """ + return datetime.now().strftime("%H:%M") + + +def get_current_date() -> str: + """ + 获取今天日期字符串 (格式: YYYY-MM-DD) + """ + return datetime.now().strftime("%Y-%m-%d") + + +def get_current_weekday() -> str: + """ + 获取今天星期几 + """ + now = datetime.now() + return WEEKDAY_MAP[now.strftime("%A")] + + +def get_current_lunar_date() -> str: + """ + 获取农历日期字符串 + """ + try: + now = datetime.now() + today_lunar = cnlunar.Lunar(now, godType="8char") + return "%s年%s%s" % ( + today_lunar.lunarYearCn, + today_lunar.lunarMonthCn[:-1], + today_lunar.lunarDayCn, + ) + except Exception: + return "农历获取失败" + + +def get_current_time_info() -> tuple: + """ + 获取当前时间信息 + 返回: (当前时间字符串, 今天日期, 今天星期, 农历日期) + """ + current_time = get_current_time() + today_date = get_current_date() + today_weekday = get_current_weekday() + lunar_date = get_current_lunar_date() + + return current_time, today_date, today_weekday, lunar_date diff --git a/main/xiaozhi-server/core/utils/prompt_manager.py b/main/xiaozhi-server/core/utils/prompt_manager.py index fca3a977..b6c4bef0 100644 --- a/main/xiaozhi-server/core/utils/prompt_manager.py +++ b/main/xiaozhi-server/core/utils/prompt_manager.py @@ -117,20 +117,13 @@ class PromptManager: def _get_current_time_info(self) -> tuple: """获取当前时间信息""" - from datetime import datetime + from .current_time import get_current_date, get_current_weekday, get_current_lunar_date + + today_date = get_current_date() + today_weekday = get_current_weekday() + lunar_date = get_current_lunar_date() + "\n" - now = datetime.now() - current_time = now.strftime("%Y年%m月%d日 %H:%M:%S") - today_date = now.strftime("%Y-%m-%d") - today_weekday = WEEKDAY_MAP[now.strftime("%A")] - today_lunar = cnlunar.Lunar(now, godType="8char") - lunar_date = "%s年%s%s\n" % ( - today_lunar.lunarYearCn, - today_lunar.lunarMonthCn[:-1], - today_lunar.lunarDayCn, - ) - - return current_time, today_date, today_weekday, lunar_date + return today_date, today_weekday, lunar_date def _get_location_info(self, client_ip: str) -> str: """获取位置信息""" @@ -199,7 +192,7 @@ class PromptManager: try: # 获取最新的时间信息(不缓存) - current_time, today_date, today_weekday, lunar_date = ( + today_date, today_weekday, lunar_date = ( self._get_current_time_info() ) @@ -224,7 +217,7 @@ class PromptManager: template = Template(self.base_prompt_template) enhanced_prompt = template.render( base_prompt=user_prompt, - current_time=current_time, + current_time="{{current_time}}", today_date=today_date, today_weekday=today_weekday, lunar_date=lunar_date,