我正在尝试使用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...
调用:
place_order
与{'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 是您定义的类。它没有定义构造函数(
__init__
)。因此它有一个空的默认构造函数,它接受 1 个参数 (self
)。你已经过了 7 分了。
您可能认为 order_ Three 应该从 pydantic-BaseModel 继承一个构造函数。但是,您稍后在代码中将 order_ Three 重新定义为另一个类。另一个类与
phone_number
和其他字段无关。