From 12bafdde8a17f7eb43a042d1837be6e18d09c67b Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Mon, 18 Aug 2025 15:58:31 +0800 Subject: [PATCH] 'commit' --- dsLightRag/Test/G2_TeachingStudent.py | 71 +++++++----------- dsLightRag/Util/GoApiUtil.py | 4 +- .../__pycache__/GoApiUtil.cpython-310.pyc | Bin 2478 -> 2478 bytes 3 files changed, 28 insertions(+), 47 deletions(-) diff --git a/dsLightRag/Test/G2_TeachingStudent.py b/dsLightRag/Test/G2_TeachingStudent.py index 739f1f3b..c819af6c 100644 --- a/dsLightRag/Test/G2_TeachingStudent.py +++ b/dsLightRag/Test/G2_TeachingStudent.py @@ -1,13 +1,14 @@ -from Config.GoApiConst import MODEL_GPT35, MODEL_GPT4 -from Util.GoApiUtil import ModelInteractor import sys +from Util import LlmUtil + + def initialize_chat_history(): """初始化对话历史,包含系统提示""" system_prompt = """ STRICT RULES Be an approachable-yet-dynamic teacher,who helps the user learn by guiding - them through their studies. +them through their studies. 1.Get to know the user.lf you don't know their goals or grade level,ask the user before diving in.(Keep this lightweight!)If they don't answer,aim for @@ -30,41 +31,18 @@ not alecture. Above all:DO NOT DO THE USER'S WORK FOR THEM. Don't answer homework questions - Help the user find the answer,by working with them collaboratively and building from what they already know. """ - return [{ - "role": "system", - "content": system_prompt - }] + return [{"role": "system", "content": system_prompt}] + -# 示例使用 if __name__ == "__main__": - # 创建模型交互器实例 - interactor = ModelInteractor() - - # 使用不同的模型 - model_name = MODEL_GPT4 - print(f"使用模型: {model_name}") - # 初始化对话历史 chat_history = initialize_chat_history() - - # 第一轮问题(可选) - first_question = "讲解一下勾股定理的证明。" - print(f"\n初始问题: {first_question}") - chat_history.append({ - "role": "user", - "content": first_question - }) - - # 发送第一轮请求 - print("\n教师助手:") - response_content = interactor.stream_request(model_name, chat_history) - chat_history.append({ - "role": "assistant", - "content": response_content - }) - + + # 欢迎消息 + print("教师助手已启动。输入 'exit' 或 '退出' 结束对话。") + print("你可以开始提问了,例如: '讲解一下勾股定理的证明'") + # 多轮对话循环 - print("\n多轮对话已启动。输入 'exit' 或 '退出' 结束对话。") while True: # 获取用户输入 user_input = input("\n你: ") @@ -75,17 +53,20 @@ if __name__ == "__main__": sys.exit(0) # 添加用户输入到对话历史 - chat_history.append({ - "role": "user", - "content": user_input - }) + chat_history.append({"role": "user", "content": user_input}) - # 发送请求 + # 发送请求(传递完整对话历史) print("\n教师助手:") - response_content = interactor.stream_request(model_name, chat_history) - - # 添加助手回复到对话历史 - chat_history.append({ - "role": "assistant", - "content": response_content - }) + try: + # 调用LlmUtil获取响应,传递对话历史 + response_content = LlmUtil.get_llm_response(user_input, chat_history) + + # 打印响应 + print(response_content) + + # 添加助手回复到对话历史 + chat_history.append({"role": "assistant", "content": response_content}) + except Exception as e: + print(f"发生错误: {str(e)}") + # 从对话历史中移除最后添加的用户输入,以便用户可以重试 + chat_history.pop() diff --git a/dsLightRag/Util/GoApiUtil.py b/dsLightRag/Util/GoApiUtil.py index ed6a0fda..5955e830 100644 --- a/dsLightRag/Util/GoApiUtil.py +++ b/dsLightRag/Util/GoApiUtil.py @@ -3,7 +3,7 @@ import requests from Config.Config import GPTNB_API_KEY class ModelInteractor: - def __init__(self, api_key=GPTNB_API_KEY, api_url="https://goapi.gptnb.ai/v1/chat/completions"): + def __init__(self, api_key=GPTNB_API_KEY, api_url="https://goapi.gptnb.ai/v1/chat/completions"): # 修复URL self.api_key = api_key self.api_url = api_url self.headers = { @@ -45,7 +45,7 @@ class ModelInteractor: response = requests.post( self.api_url, headers=self.headers, - data=json.dumps(payload), + data=json.dumps(payload), # 确保变量名一致 stream=True, timeout=30 ) diff --git a/dsLightRag/Util/__pycache__/GoApiUtil.cpython-310.pyc b/dsLightRag/Util/__pycache__/GoApiUtil.cpython-310.pyc index f46f98fb5605b04fe6f5a0026d56e8fb2bbc8856..f3f9f59cac636f6c85c64d96f6f121537d03ad37 100644 GIT binary patch delta 23 dcmZ1{yiS-epO=@50SKJ$EXrUK*vPkl697XE1;+pY delta 23 dcmZ1{yiS-epO=@50SJU|FUq*XzmabNCjdq}25$fW