Langchain中OpenAI函数代理传参报错:TypeError参数数量不符
Langchain下单代理多参数调用报错解决
问题描述
我正在使用Langchain框架构建一个可帮我向商店下单的代理。用于下单的工具/函数需要多输入参数,但运行时持续报错:TypeError: __init__() takes 1 positional argument but 7 were given。
原代码
from placeorder import order_three from langchain.chat_models import ChatOpenAI from pydantic import BaseModel, Field from typing import Optional, Type from langchain.agents import AgentType from langchain.tools import BaseTool from langchain.agents import initialize_agent, Tool class order_three(BaseModel): """Place an order""" first_name: str =Field(...,description= "The first name of person placing the order") last_name: str =Field(...,description= "The last name of person placing the order") phone_number: str =Field(...,description= "The phone number of person placing the order") product: str =Field(...,description= "The product they want to buy") date_of_birth: str =Field(...,description= "The date of birth of person placing the order") quantity: str =Field(...,description= "The amount of the product they want to buy") class order_three(BaseTool): name = "place_order" description = "Useful when the user wants to place an order. You should input the firstname, lastname, quantity, product, date of birth, and phone number" def _run(self, first_name: str, last_name: str, quantity: str, date_of_birth: str, product: str, phone_number: str): place_order_response = order_three(first_name, last_name, phone_number, date_of_birth, product, quantity) return place_order_response def _arun(self, first_name: str, last_name: str, quantity: str, date_of_birth: str, product: str, phone_number: str): raise NotImplementedError("This tool does not support async") args_schema: Optional[Type[BaseModel]] = order_three tools = [order_three()] llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613") open_ai_agent = initialize_agent(tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True) print(open_ai_agent.run("I want to place an order. My name is John smith my phone number is 5555555555. my date of birth is 030419999 I want to order 8 large Pizzas"))
报错信息
Entering new AgentExecutor chain...
Invoking:
place_orderwith{'first_name': 'John', 'last_name': 'Smith', 'phone_number': '5555555555', 'product': 'large Pizzas', 'date_of_birth': '03041999', 'quantity': '8'}
Traceback (most recent call last): File "/Users/izayahhudnut/Documents/Code/BudtenderAI/vite-project/testagent.py", line 45, in <module> print(open_ai_agent.run("I want to place an order. My name is John smith my phone number is 5555555555. my date of birth is 030419999 I want to order 8 large Pizzas")) File "/Users/izayahhudnut/Library/Python/3.9/lib/python/site-packages/langchain/chains/base.py", line 487, in run return self(args[0], callbacks=callbacks, tags=tags, metadata=metadata)[ File "/Users/izayahhudnut/Library/Python/3.9/lib/python/site-packages/langchain/chains/base.py", line 292, in __call__ raise e File "/Users/izayahhudnut/Library/Python/3.9/lib/python/site-packages/langchain/chains/base.py", line 286, in __call__ self._call(inputs, run_manager=run_manager) File "/Users/izayahhudnut/Library/Python/3.9/lib/python/site-packages/langchain/agents/agent.py", line 1039, in _call next_step_output = self._take_next_step( File "/Users/izayahhudnut/Library/Python/3.9/lib/python/site-packages/langchain/agents/agent.py", line 894, in _take_next_step observation = tool.run( File "/Users/izayahhudnut/Library/Python/3.9/lib/python/site-packages/langchain/tools/base.py", line 356, in run raise e File "/Users/izayahhudnut/Library/Python/3.9/lib/python/site-packages/langchain/tools/base.py", line 330, in run else self._run(*tool_args, **tool_kwargs) File "/Users/izayahhudnut/Documents/Code/BudtenderAI/vite-project/testagent.py", line 25, in _run place_order_response = order_three(first_name, last_name, phone_number, date_of_birth, product, quantity) TypeError: __init__() takes 1 positional argument but 7 were given
错误原因
- 类名重复冲突:先后定义了两个
order_three类,一个继承BaseModel,另一个继承BaseTool,后续代码中调用order_three(...)时,实际指向的是继承BaseTool的类,而BaseTool的构造函数不需要传入这些参数,导致参数数量不匹配。 - 未正确调用导入的下单函数:代码开头从
placeorder模块导入了order_three函数,但在_run方法中没有使用这个导入的函数,反而调用了自己定义的同名类。 - 缩进错误:
BaseModel的字段、BaseTool的属性和方法都没有正确缩进,这会导致语法错误,影响代码的正常运行。
修复后的代码
from placeorder import order_three # 导入的实际下单函数 from langchain.chat_models import ChatOpenAI from pydantic import BaseModel, Field from typing import Optional, Type from langchain.agents import AgentType from langchain.tools import BaseTool from langchain.agents import initialize_agent # 重命名BaseModel类,避免和导入的函数、工具类重名 class OrderThreeArgs(BaseModel): """Place an order""" first_name: str = Field(..., description="The first name of person placing the order") last_name: str = Field(..., description="The last name of person placing the order") phone_number: str = Field(..., description="The phone number of person placing the order") product: str = Field(..., description="The product they want to buy") date_of_birth: str = Field(..., description="The date of birth of person placing the order") quantity: str = Field(..., description="The amount of the product they want to buy") class OrderThreeTool(BaseTool): name = "place_order" description = "Useful when the user wants to place an order. You should input the firstname, lastname, quantity, product, date of birth, and phone number" args_schema: Optional[Type[BaseModel]] = OrderThreeArgs def _run(self, first_name: str, last_name: str, quantity: str, date_of_birth: str, product: str, phone_number: str): # 调用导入的实际下单函数 place_order_response = order_three(first_name, last_name, phone_number, date_of_birth, product, quantity) return place_order_response def _arun(self, first_name: str, last_name: str, quantity: str, date_of_birth: str, product: str, phone_number: str): raise NotImplementedError("This tool does not support async") tools = [OrderThreeTool()] llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0613") open_ai_agent = initialize_agent( tools, llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True ) print(open_ai_agent.run("I want to place an order. My name is John smith my phone number is 5555555555. my date of birth is 03041999 I want to order 8 large Pizzas"))
修复说明
- 重命名了
BaseModel类为OrderThreeArgs,避免和导入的下单函数、工具类名称冲突。 - 将工具类命名为
OrderThreeTool,明确区分类和导入的函数。 - 修正了所有代码的缩进问题,确保符合Python语法规范。
- 在
_run方法中明确调用从placeorder模块导入的order_three函数,而不是自己定义的类。
内容的提问来源于stack exchange,提问作者Izayah Hudnut
相关产品推荐
相关产品推荐

