Langchain代理工具生成"StructuredTool不是JSON可序列化",

vdgimpew  于 2个月前  发布在  其他
关注(0)|答案(2)|浏览(34)

检查其他资源

  • 为这个问题添加了一个非常描述性的标题。
  • 使用集成搜索在LangChain文档中进行搜索。
  • 使用GitHub搜索查找类似的问题,但没有找到。
  • 我确信这是LangChain中的一个bug,而不是我的代码。
  • 通过更新到LangChain的最新稳定版本(或特定集成包)无法解决此bug。

示例代码

代码来自LangChain文档
https://python.langchain.com/v0.2/docs/how_to/tools_chain/

from langchain import hub
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_core.tools import tool

model = # A mistral model

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant"),
        ("placeholder", "{chat_history}"),
        ("human", "{input}"),
        ("placeholder", "{agent_scratchpad}"),
    ]
)

@tool
def multiply(first_int: int, second_int: int) -> int:
    """Multiply two integers together."""
    return first_int * second_int

@tool
def add(first_int: int, second_int: int) -> int:
    "Add two integers."
    return first_int + second_int

@tool
def exponentiate(base: int, exponent: int) -> int:
    "Exponentiate the base to the exponent power."
    return base**exponent

tools = [multiply, add, exponentiate]

# Construct the tool calling agent
agent = create_tool_calling_agent(model,tools, prompt)

# Create an agent executor by passing in the agent and tools
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

result = agent_executor.invoke(
    {
        "input": "Take 3 to the fifth power and multiply that by the sum of twelve and three, then square the whole result"
    }
)

错误信息和堆栈跟踪(如果适用)

TypeError: StructuredTool对象不可序列化为JSON

描述

我正在尝试运行https://python.langchain.com/v0.2/docs/how_to/tools_chain/中的示例代码,以调用配备工具的代理。我发现了两个问题:

  • 如果按照原样运行代码,它会生成错误:“StructuredTool对象不可序列化为JSON”。
  • 如果使用空工具列表(即tools=[])创建代理(即tools = []),它会产生响应。然而,根据我的理解,这不是创建代理的正确方法。除了mistral7b模型的答案非常不准确之外。即使在上面提供的链接中的示例中,当检查langSmith run时,答案似乎与正确的答案不同且错误。

系统信息

langchain-core==0.1.52
langchain==0.1.16

t2a7ltrp

t2a7ltrp1#

如果我们在LangChain中使用dumps函数,问题似乎会得到解决:

from langchain.load.dump import dumps

tools = [multiply, add, exponentiate]
tools_ = [dumps(multiply), dumps(add), dumps(exponentiate)]

agent = create_tool_calling_agent(model,tools_, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
jgzswidk

jgzswidk2#

我无法重现这个问题。你能提供完整的堆栈跟踪并分享你是如何示例化模型的吗?你也可以尝试升级到langchain和langchain-core >= 0.2吗?

相关问题