subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel)

我的代码

from langchain.chains import LLMMathChain
from langchain.utilities import SerpAPIWrapper
from langchain.agents import AgentType, initialize_agent
from langchain.chat_models import ChatOpenAI
from langchain.tools import BaseTool, StructuredTool, Tool, tool

from pydantic import BaseModel, Field


class CalculatorInput(BaseModel):
    question: str = Field()

tools = []
tools.append(
    Tool.from_function(
        func=llm_math_chain.run,
        name="Calculator",
        description="useful for when you need to answer questions about math",
        args_schema=CalculatorInput
        # coroutine= ... <- you can specify an async method if desired as well
    )
)

报错详情

---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
/Users/jshen/Documents/github/bigModel/LangChain/tutorials/cn/agents/tools/custom_tools.ipynb Cell 11 line 1
----> 1 Tool.from_function(
      2         func=llm_math_chain.run,
      3         name="Calculator",
      4         description="useful for when you need to answer questions about math",
      5         args_schema=CalculatorInput
      6         # coroutine= ... <- you can specify an async method if desired as well
      7     )

File ~/anaconda3/envs/torch/lib/python3.9/site-packages/langchain/tools/base.py:559, in Tool.from_function(cls, func, name, description, return_direct, args_schema, coroutine, **kwargs)
    557 if func is None and coroutine is None:
    558     raise ValueError("Function and/or coroutine must be provided")
--> 559 return cls(
    560     name=name,
    561     func=func,
    562     coroutine=coroutine,
    563     description=description,
    564     return_direct=return_direct,
    565     args_schema=args_schema,
    566     **kwargs,
    567 )

File ~/anaconda3/envs/torch/lib/python3.9/site-packages/langchain/tools/base.py:539, in Tool.__init__(self, name, func, description, **kwargs)
    535 def __init__(
    536     self, name: str, func: Optional[Callable], description: str, **kwargs: Any
    537 ) -> None:
    538     """Initialize tool."""
--> 539     super(Tool, self).__init__(
    540         name=name, func=func, description=description, **kwargs
    541     )

File ~/anaconda3/envs/torch/lib/python3.9/site-packages/pydantic/v1/main.py:341, in BaseModel.__init__(__pydantic_self__, **data)
    339 values, fields_set, validation_error = validate_model(__pydantic_self__.__class__, data)
    340 if validation_error:
--> 341     raise validation_error
    342 try:
    343     object_setattr(__pydantic_self__, '__dict__', values)

ValidationError: 1 validation error for Tool
args_schema
  subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel)

解决方案

GitHub 关于此报错的issues : https://github.com/langchain-ai/langchain/issues/9981

!pip install pydantic==1.10.13

我原来的pydantic的版本是 2.3.0,会报错。降低版本后解决了这个问题。

你可能感兴趣的:(调bug,langchain)