langchain 工具调用代理:在调用工具时函数名错误,

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

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

(venv) Cipher@Crippd assistcx-agent % python task.py
Entering new AgentExecutor chain...
Traceback (most recent call last):
 File "/Users/Cipher/AssistCX/assistcx-agent/task.py", line 63, in 
 result = agent_executor.invoke({"input": query})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/Cipher/AssistCX/assistcx-agent/.venv/lib/python3.11/site-packages/langchain/chains/base.py", line 166, in invoke
 raise e
File "/Users/Cipher/AssistCX/assistcx-agent/.venv/lib/python3.11/site-packages/langchain/chains/base.py", line 156, in invoke
 self._call(inputs, run_manager=run_manager)
File "/Users/Cipher/AssistCX/assistcx-agent/.venv/lib/python3.11/site-packages/langchain/agents/agent.py", line 1612, in _call
 next_step_output = self._take_next_step(
 File "/Users/Cipher/AssistCX/assistcx-agent/.venv/lib/python3.11/site-packages/langchain/agents/agent.py", line 1318, in _take_next_step
 [
 File "/Users/Cipher/AssistCX/assistcx-agent/.venv/lib/python3.11/site-packages/langchain/agents/agent.py", line 1318, in 
 [
 File "/Users/Cipher/AssistCX/assistcx-agent/.venv/lib/python3.11/site-packages/langchain/agents/agent.py", line 1346, in _iter_next_step
 output = self.agent.plan(
 File "/Users/Cipher/AssistCX/assistcx-agent/.venv/lib/python3.11/site-packages/langchain/agents/agent.py", line 580, in plan
 for chunk in self.runnable.stream(inputs, config={"callbacks": callbacks}):
 File "/Users/Cipher/AssistCX/assistcx-agent/.venv/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3253, in stream
 yield from self.transform(iter([input]), config, **kwargs)
 File "/Users/Cipher/AssistCX/assistcx-agent/.venv/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3240, in transform
 yield from self._transform_stream_with_config(
 File "/Users/Cipher
openai.BadRequestError: 错误代码:400 - {'error': {'message': "Invalid 'tools[0].function.name': string does not match pattern. Expected a string that matches the pattern '^[a-zA-Z0-9*-]+$'.", 'type': 'invalid_request_error', 'param': 'tools[0].function.name', 'code': 'invalid_value'}}
(.venv) Cipher@Crippd assistcx-agent %

#### 描述

我已经按照这里的指南设置了一个简单的工具调用代理。我使用 `StructuredTool` 类定义了工具,如我的代码所示。当我尝试运行这段代码时,我得到了上面共享的错误。如果我删除工具名称中的空格并用破折号(-)或下划线(_)替换它,代码就可以正常工作,代理执行成功。
我没有在任何地方看到工具名称不应该包含空格,而且Langchain文档中也使用了空格作为工具名称。我是不是漏掉了什么或者犯了什么错误?

#### 系统信息

langchain==0.2.11
 langchain-community==0.2.10
 langchain-core==0.2.25
 langchain-groq==0.1.8
 langchain-ollama==0.1.0
 langchain-openai==0.1.19
 langchain-text-splitters==0.2.2
 langchain-together==0.1.4
平台:Mac
 python版本:3.11.7
niknxzdl

niknxzdl1#

函数名不能包含空格。
你可以将其更改为:

add_numbers_tool = StructuredTool.from_function(
    func=add_numbers,
    name="Add_numbers",  # fixed
    description="Adds a and b.",
    parse_docstring=True,
)

multiply_numbers_tool = StructuredTool.from_function(
    func=multiply_numbers,
    name="Multiply_numbers",  # fixed
    description="Multiplies a and b.",
    parse_docstring=True,
)

parse_docstring = True 不是必须的,但我看到你使用了Google风格的文档字符串,所以也许这样会更好。
你可以使用 print(add_numbers_tool.get_input_schema().schema()) 来查看两者之间的差异。
或者你可以在LangSmith上进行检查。

相关问题