diff --git a/dsSchoolBuddy/Start.py b/dsSchoolBuddy/Start.py index c1610d04..454a8a22 100644 --- a/dsSchoolBuddy/Start.py +++ b/dsSchoolBuddy/Start.py @@ -74,7 +74,7 @@ async def chat(request: fastapi.Request): async def generate_response_stream(): try: stream = await client.chat.completions.create( - model=Config.MODEL_NAME if hasattr(Config, 'MODEL_NAME') else 'gpt-3.5-turbo', + model=Config.ALY_LLM_MODEL_NAME, messages=[ {'role': 'user', 'content': prompt} ], diff --git a/dsSchoolBuddy/TestStart.py b/dsSchoolBuddy/TestStart.py index e69de29b..a473c1a2 100644 --- a/dsSchoolBuddy/TestStart.py +++ b/dsSchoolBuddy/TestStart.py @@ -0,0 +1,87 @@ +import json +import uuid + +import requests + +# 服务器地址 +BASE_URL = "http://localhost:8000" +CHAT_ENDPOINT = f"{BASE_URL}/api/chat" + +# 用户ID(固定一个以便模拟多轮对话) +USER_ID = "test_user_123" +# 会话ID(固定一个以便模拟多轮对话) +SESSION_ID = str(uuid.uuid4()) + + +def send_message(query): + """发送消息到聊天API并接收流式响应""" + headers = { + "Content-Type": "application/json" + } + + data = { + "user_id": USER_ID, + "query": query, + "session_id": SESSION_ID, + "include_history": True # 包含历史记录 + } + + try: + print(f"\n你: {query}") + print("老师: ", end="", flush=True) + + # 发送POST请求,使用stream=True以流式接收响应 + with requests.post(CHAT_ENDPOINT, json=data, headers=headers, stream=True) as response: + if response.status_code == 200: + # 逐行处理流式响应 + for line in response.iter_lines(): + if line: + # 去掉前缀'data: ' + line = line.decode('utf-8').replace('data: ', '') + try: + # 解析JSON + data = json.loads(line) + if 'reply' in data: + print(data['reply'], end="", flush=True) + elif 'error' in data: + print(f"\n错误: {data['error']}") + except json.JSONDecodeError: + # 非JSON格式的响应 + print(f"\n无法解析的响应: {line}") + print() # 换行 + else: + print(f"\n请求失败,状态码: {response.status_code}") + print(f"错误信息: {response.text}") + + except requests.exceptions.RequestException as e: + print(f"\n请求异常: {str(e)}") + + +def main(): + """主函数,处理用户输入并调用聊天API""" + print("===== 教育助手对话系统 =====") + print("请输入您的问题,比如:帮我讲解一下勾股定理的证明。输入'退出'结束对话") + print("===========================") + + while True: + try: + # 获取用户输入 + query = input("\n你: ") + + # 检查是否退出 + if query.strip() == '退出': + print("对话已结束,再见!") + break + + # 发送消息 + send_message(query) + + except KeyboardInterrupt: + print("\n程序被中断,再见!") + break + except Exception as e: + print(f"\n发生错误: {str(e)}") + + +if __name__ == "__main__": + main() \ No newline at end of file