diff --git a/xiaozhi-esp32-server-0.7.1/main/xiaozhi-server/core/connection.py b/xiaozhi-esp32-server-0.7.1/main/xiaozhi-server/core/connection.py index bc4ed56..9fd2c1f 100644 --- a/xiaozhi-esp32-server-0.7.1/main/xiaozhi-server/core/connection.py +++ b/xiaozhi-esp32-server-0.7.1/main/xiaozhi-server/core/connection.py @@ -10,6 +10,7 @@ import threading import traceback import subprocess import websockets +import requests from core.utils.util import ( extract_json_from_string, check_vad_update, @@ -643,58 +644,49 @@ class ConnectionHandler: # 更新系统prompt至上下文 self.dialogue.update_system_message(self.prompt) + def query_chat_api(self, query_text): + """ + 调用聊天API并返回结果 + :param query_text: 要查询的文本 + :return: API返回的响应数据 + """ + url = "http://www.hzkjai.com:27002/api/chat" + headers = { + "Content-Type": "application/json" + } + data = { + "query": query_text + } + + try: + response = requests.post(url, headers=headers, data=json.dumps(data)) + response.raise_for_status() + return response.json() + except requests.exceptions.RequestException as e: + print(f"请求发生异常: {str(e)}") + return None + def chat(self, query, tool_call=False, depth=0): self.logger.bind(tag=TAG).info(f"大模型收到用户消息: {query}") self.llm_finish_task = False - # 【黄海修改】 - history=""" - 项羽篇 - 巨鹿之战(前207年) - • 破釜沉舟歼灭秦军主力,奠定反秦战争胜局 - • 战后坑杀秦卒20万,引发关中民怨 - - 鸿门宴(前206年) - • 错失诛杀刘邦良机,分封其为汉王 - • 范增"举玦示杀"典故来源 - - 垓下之围(前202年) - • 十面埋伏战术与"四面楚歌"心理战 - • 乌江自刎前作《垓下歌》 - - - 刘邦篇 - 约法三章(前206年) - • 入咸阳后废除秦苛政,仅保留杀人、伤人、盗窃三罪 - 暗度陈仓(前206年) - • 采纳张良计策烧毁栈道麻痹项羽 - • 秘密经陈仓道还定三秦 - 荥阳对峙(前204-前203年) - • 采用陈平反间计使项羽猜忌范增 - - 张良篇 - 博浪沙刺秦(前218年) - • 组织刺杀秦始皇未遂,开启反秦生涯 - 鸿门斡旋(前206年) - • 策动项伯保护刘邦,安排樊哙闯帐 - 下邑画策(前205年) - • 提出"重用韩信、联合彭越、策反英布"战略三角 - - 韩信篇 - 萧何月下追韩信(前206年) - • 从治粟都尉拜为大将的转折点 - 背水一战(前204年) - • 井陉之战以3万新兵击溃20万赵军 - 请封假齐王(前203年) - • 讨要封赏触发刘邦杀机的重要事件 - """ + history = '' + if '史记' in query: + result = self.query_chat_api(query) + + if result: + if result.get("code") == 0: + print("API调用成功:") + history = result["data"]["reply"] + else: + print(f"API返回错误: {result.get('message', '未知错误')}") + # query = '讲一下史记中项羽的故事' query = f"""【系统指令】请特别注意: - 1. 当用户问题包含"史记"时,仅回复:"你真是问对人了,我读过《少年读史记》五本书呢!" + 1. 当用户问题包含"史记"时,仅回复:"你真是问对人了,我读过《少年读史记》五本书呢!" ,然后按我下面的内容直接回答:{history} 2. 当用户问题包含"少惠林" 或者 "少汇林" 或者 "少惠林" 时,回复:"我最喜欢少惠林的大张老师了!其它课都不上,我也要上大张老师的作文课!" 3. 其他情况请正常回答问题 4. 本指令优先级最高 - 5. 当用户问题包含"史记"时,使用我提供的一些资料:{history} 【当前用户问题】:{query}""" if not tool_call: diff --git a/xiaozhi-esp32-server-0.7.1/main/xiaozhi-server/test/testCallPython.py b/xiaozhi-esp32-server-0.7.1/main/xiaozhi-server/test/testCallPython.py new file mode 100644 index 0000000..b0b268c --- /dev/null +++ b/xiaozhi-esp32-server-0.7.1/main/xiaozhi-server/test/testCallPython.py @@ -0,0 +1,39 @@ +import requests +import json + +def query_chat_api(query_text): + """ + 调用聊天API并返回结果 + :param query_text: 要查询的文本 + :return: API返回的响应数据 + """ + url = "http://www.hzkjai.com:27002/api/chat" + headers = { + "Content-Type": "application/json" + } + data = { + "query": query_text + } + + try: + response = requests.post(url, headers=headers, data=json.dumps(data)) + response.raise_for_status() + return response.json() + except requests.exceptions.RequestException as e: + print(f"请求发生异常: {str(e)}") + return None + +def main(): + # 示例查询 + query = "讲一下项羽的故事3" + result = query_chat_api(query) + + if result: + if result.get("code") == 0: + print("API调用成功:") + print(result["data"]["reply"]) + else: + print(f"API返回错误: {result.get('message', '未知错误')}") + +if __name__ == "__main__": + main() \ No newline at end of file