Closed LogCreative closed 2 days ago
将 create_react_agent
展开可以用下面的方式绕过 astream_log
未进行的判断,但也会导致后续推理结果的偏差:
import os
from langchain_community.chat_models import QianfanChatEndpoint
from langchain_core.messages import HumanMessage
from langchain_core.tools import tool
from langgraph.prebuilt import ToolNode
@tool
def get_weather(location: str):
"""Call to get the current weather."""
if location.lower() in ["sf", "san francisco"]:
return "It's 60 degrees and foggy."
else:
return "It's 90 degrees and sunny."
@tool
def get_coolest_cities():
"""Get a list of coolest cities"""
return "nyc, sf"
tools = [get_weather, get_coolest_cities]
tool_node = ToolNode(tools)
os.environ["QIANFAN_AK"] = "*"
os.environ["QIANFAN_SK"] = "*"
model = QianfanChatEndpoint(
streaming=False,
model="ernie-4.0-8k-preview",
**{"top_p": 0.4, "temperature": 0.1, "penalty_score": 1},
)
from langgraph.graph import StateGraph, MessagesState, START, END
model_with_tools = model.bind_tools(tools)
def should_continue(state: MessagesState):
messages = state["messages"]
last_message = messages[-1]
if last_message.tool_calls:
state["messages"][-1].content = "使用工具调用" # 绕过判断
return "tools"
return END
def call_model(state: MessagesState):
messages = state["messages"]
response = model_with_tools.invoke(messages)
return {"messages": [response]}
workflow = StateGraph(MessagesState)
# Define the two nodes we will cycle between
workflow.add_node("agent", call_model)
workflow.add_node("tools", tool_node)
workflow.add_edge(START, "agent")
workflow.add_conditional_edges("agent", should_continue, ["tools", END])
workflow.add_edge("tools", "agent")
graph = workflow.compile()
async def main():
async for s in graph.astream_log({"messages": [HumanMessage("what's the weather in sf?")]}):
print(s)
import asyncio
asyncio.run(main())
但是如果此时再改用 astream
就会弹出如下的错误:
qianfan.errors.APIError: api return error, req_id: as-jmwha8b6j4 code: 336003, msg: Message format error, index [2] message content and function_call is mutually exclusive, only one must be null
感谢您的反馈,在较旧版本的 Langchain 中并未出现上述问题。我们这边会对新版本中引入的这个问题进行排查 :)
修复 PR 已提 @LogCreative
System Info
qianfan version
System Version
Reproduction
继续 #842:
考虑到不再支持老式的 AgentExecutor 用法,在尝试使用 LangGraph 进行工具调用时依然出现了一些问题,修改 LangGraph 官方示例 使得调用
astream_log
函数输出结果:就会报错:
而如果换成
astream
就没问题:初步 debug 发现,在使用
astream_log
时没有对含有 tool_call 的那一个 AIMessage 做特殊处理(该 message 的 content 应该为空),导致 ToolMessage 产生再输入大模型时:[HumanMessage, AIMessage(content=''), ToolMessage] 就没有被校验通过。而使用astream
函数就没有这个问题。由于
astream_log
是 LangServe Playground 中使用的函数,希望能够给出一种解决方案。