Pydantic ошибка: ожидается подкласс BaseModel (type=type_error.subclass; ожидаемый_класс=BaseModel)Python

Программы на Python
Anonymous
Pydantic ошибка: ожидается подкласс BaseModel (type=type_error.subclass; ожидаемый_класс=BaseModel)

Сообщение Anonymous »


For the below given code i am getting pydantic error:

from langchain.chains import LLMChain from langchain.output_parsers import PydanticOutputParser from langchain.prompts import PromptTemplate from pydantic import BaseModel, Field query = "Do you offer vegetarian food?" class LineList(BaseModel): lines: list[str] = Field(description="Lines of text") class LineListOutputParser(PydanticOutputParser): def __init__(self) -> None: super().__init__(pydantic_object=LineList) def parse(self, text: str) -> list[str]: lines = text.strip().split("\n") return lines output_parser = LineListOutputParser() QUERY_PROMPT = PromptTemplate( input_variables=["question"], template="""You are an AI language model assistant. Your task is to generate five different versions of the given user question to retrieve relevant documents from a vector database. By generating multiple perspectives on the user question, your goal is to help the user overcome some of the limitations of the distance-based similarity search. Provide these alternative questions separated by newlines. Only provide the query, no numbering. Original question: {question}""", ) llm_chain = LLMChain(llm=llm, prompt=QUERY_PROMPT, output_parser=output_parser) queries = llm_chain.invoke(query) I am getting the below error:

--------------------------------------------------------------------------- ValidationError Traceback (most recent call last) Cell In[93], line 20 16 lines = text.strip().split("\n") 17 return lines ---> 20 output_parser = LineListOutputParser() 22 QUERY_PROMPT = PromptTemplate( 23 input_variables=["question"], 24 template="""You are an AI language model assistant. Your task is to generate five (...) 29 Original question: {question}""", 30 ) 32 llm_chain = LLMChain(llm=llm, prompt=QUERY_PROMPT, output_parser=output_parser) Cell In[93], line 13, in LineListOutputParser.__init__(self) 12 def __init__(self) -> None: ---> 13 super().__init__(pydantic_object=LineList) File ~\anaconda3\Lib\site-packages\langchain_core\load\serializable.py:120, in Serializable.__init__(self, **kwargs) 119 def __init__(self, **kwargs: Any) -> None: --> 120 super().__init__(**kwargs) 121 self._lc_kwargs = kwargs File ~\anaconda3\Lib\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 LineListOutputParser pydantic_object subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel) I am using pydantic-2.5.3


Источник: https://stackoverflow.com/questions/780 ... ass-expect

Вернуться в «Python»