feat: optimize langgraph chat route
This commit is contained in:
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user