From 0c8266797e81a8591a9a21d07c4884d936b53cda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Wed, 26 Nov 2025 10:44:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E7=BA=BFTHINK=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/chat.py | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/routes/chat.py b/routes/chat.py index a671f2f..ab9bc27 100644 --- a/routes/chat.py +++ b/routes/chat.py @@ -26,11 +26,31 @@ agent_manager = init_global_sharded_agent_manager( ) -async def generate_stream_response(agent, messages, tool_response: bool, model: str): +async def generate_stream_response(agent, messages, thought_list, tool_response: bool, model: str): """生成流式响应""" accumulated_content = "" + + chunk_id = 0 try: + + if len(thought_list)>0: + accumulated_content = get_content_from_messages(thought_list, tool_response=tool_response) + chunk_data = { + "id": f"chatcmpl-thought", + "object": "chat.completion.chunk", + "created": int(__import__('time').time()), + "model": model, + "choices": [{ + "index": 0, + "delta": { + "content": accumulated_content + }, + "finish_reason": None + }] + } + yield f"data: {json.dumps(chunk_data, ensure_ascii=False)}\n\n" + for response in agent.run(messages=messages): previous_content = accumulated_content accumulated_content = get_content_from_messages(response, tool_response=tool_response) @@ -202,7 +222,8 @@ async def create_agent_and_generate_response( if all_checks: # 将checks数组格式化为JSON字符串 - guideline_analysis = json.dumps({"checks": all_checks}, ensure_ascii=False) + guideline_analysis = "\n".join([item["rationale"] for item in all_checks]) + # guideline_analysis = json.dumps({"checks": all_checks}, ensure_ascii=False) print(f"Merged guideline analysis result: {guideline_analysis}") # 将分析结果添加到最后一个消息的内容中 @@ -226,17 +247,20 @@ async def create_agent_and_generate_response( user_identifier=user_identifier ) + thought_list = [] + if guideline_analysis != '': + thought_list = [{"role": "assistant","content": guideline_analysis}] # 根据stream参数决定返回流式还是非流式响应 if stream: return StreamingResponse( - generate_stream_response(agent, messages, tool_response, model_name), + generate_stream_response(agent, messages, thought_list, tool_response, model_name), media_type="text/event-stream", headers={"Cache-Control": "no-cache", "Connection": "keep-alive"} ) else: # 非流式响应 - final_responses = agent.run_nonstream(messages) - + agent_responses = agent.run_nonstream(messages) + final_responses = thought_list+agent_responses if final_responses and len(final_responses) > 0: # 使用 get_content_from_messages 处理响应,支持 tool_response 参数 content = get_content_from_messages(final_responses, tool_response=tool_response)