From 1036a124dd4815b07ae7e5a99c37d43a8ce16396 Mon Sep 17 00:00:00 2001 From: martsforever Date: Wed, 10 Sep 2025 22:02:59 +0800 Subject: [PATCH] feat: optimize langgraph chat route --- app/controller/add_langgraph_chat_route.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controller/add_langgraph_chat_route.py b/app/controller/add_langgraph_chat_route.py index 536e715..ae36ba7 100644 --- a/app/controller/add_langgraph_chat_route.py +++ b/app/controller/add_langgraph_chat_route.py @@ -61,6 +61,11 @@ class ChatAgent: def add_langgraph_chat_route(app: FastAPI): + @app.post("/project/analysis") + async def project_analysis(body: dict): + project_name = body.get('name') + return {} + # 流式对话接口 @app.post("/langgraph/stream") async def langgraph_stream( @@ -90,9 +95,9 @@ def add_langgraph_chat_route(app: FastAPI): # chat_state = await ChatAgent.get_chat_state(thread_id) # chat_history_list = chat_state.get('messages') - # has_emit_chunk 用来优化流式传输,避免将content为空的为AIMessageChunk发送给前端(实际上此时正在输出思考内容) + # has_emit_message_id 用来优化流式传输,避免将content为空的为AIMessageChunk发送给前端(实际上此时正在输出思考内容) # 第一次的时候输出,后续再输出content为空的AIMessageChunk不发送给前端 - has_emit_chunk = False + has_emit_message_id = {} async for chunk in graph.astream( stream_input, @@ -114,7 +119,7 @@ def add_langgraph_chat_route(app: FastAPI): # messages模式流式输出,此时 chunk[1][0] 为AIMessageChunk chunk_message = chunk[1][0] if not chunk_message.content: - if has_emit_chunk: + if has_emit_message_id.get(chunk_message.id): continue else: # updates模式流式输出 @@ -132,7 +137,7 @@ def add_langgraph_chat_route(app: FastAPI): "msg_type": chunk_message.type, "msg_content": chunk_message.content, } - has_emit_chunk = True + has_emit_message_id[chunk_message.id] = True if isinstance(chunk_message, AIMessage): if chunk_message.tool_calls: