feat: 通过判断langgraph_node以及消息是AIMessage还是ToolMessage,如果是AIMessage就跳过的方式来规避这个问题
This commit is contained in:
@@ -3,7 +3,7 @@ import time
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from langchain_core.messages import AIMessage, ToolMessage
|
||||
from langchain_core.messages import AIMessage, ToolMessage, AIMessageChunk
|
||||
from langgraph.graph.state import CompiledStateGraph
|
||||
from langgraph.prebuilt import create_react_agent
|
||||
from langgraph.types import Command
|
||||
@@ -122,6 +122,14 @@ def add_langgraph_chat_route(app: FastAPI):
|
||||
emit_chunk = {"stream_type": chunk[0], }
|
||||
|
||||
if emit_chunk['stream_type'] == "messages":
|
||||
|
||||
if isinstance(chunk[1], tuple):
|
||||
if isinstance(chunk[1][1], dict):
|
||||
langgraph_node = chunk[1][1].get("langgraph_node")
|
||||
if langgraph_node == "tools" and isinstance(chunk[1][0], AIMessage):
|
||||
print("忽略工具中的message消息")
|
||||
continue
|
||||
|
||||
# messages模式流式输出,此时 chunk[1][0] 为AIMessageChunk
|
||||
chunk_message = chunk[1][0]
|
||||
if not chunk_message.content:
|
||||
|
||||
Reference in New Issue
Block a user