@roo_validator 在导入 langchain.text_splitter Python 时出现错误。

huangapple go评论115阅读模式
英文:

@roo_validator error when importing langchain.text_splitter Python

问题

Here's the translated part of your text without the code:

"I have been working through a LangChain tutorial and have hit this problem when trying to import langchain into python in vscode on macOS 13.4.1.

I see a suggested fix, but I do not understand how to implement it.

I get the following error on the Streamlit web page and the VSCode terminal:
PydanticUserError: If you use @root_validator with pre=False (the default) you MUST specify skip_on_failure=True. Note that @root_validator is deprecated and should be replaced with @model_validator. For further information visit https://errors.pydantic.dev/2.0/u/root-validator-pre-skip"

英文:

I have been working through a LangChain tutorial and have hit this problem when trying to import langchain into python in vscode on macos 13.4.1.

`from dotenv import load_dotenv
import os
import streamlit as st
from PyPDF2 import PdfReader
from langchain.text_splitter import CharacterTextSplitter


def main():
    load_dotenv()
    # print(os.getenv("OPENAI_API_KEY"))
    st.set_page_config(page_title="Select the Data PDF")
    st.header("Load your PDF below: ⚡︎")

    pdf = st.file_uploader("Upload your PDF", type="pdf")

    if pdf is not None:
        pdf_reader = PdfReader(pdf)

        # the PdfReader reads in chunks of one page each
        text = ""
        for page in pdf_reader.pages:
            text += page.extract_text()
            st.write(text)
   
    # split text into chunks (inside outer if not None)
    # the CharacterTextSplitter class has properties that  need to be set...
    # so pass in property parameters to the class inititializer
    text_splitter = CharacterTextSplitter(
        separator="\n",
        chunk_size=1000,
        chunk_overlap=200,
        length_function=len #python's basic length function
        )
    chunks = text_splitter.split_text(text)
    st.write(chunks)



if __name__ == '__main__' :
    main()`

I see a suggested fix, but I do not understand how to implement it.

I get the following error on the Stringlit web page and the VSCode terminal:
PydanticUserError: If you use @root_validator with pre=False (the default) you MUST specify skip_on_failure=True. Note that @root_validator is deprecated and should be replaced with @model_validator. For further information visit https://errors.pydantic.dev/2.0/u/root-validator-pre-skip
Traceback:
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 552, in _run_script
exec(code, module.dict)
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/app.py", line 6, in <module>
from langchain.embeddings import ImportError
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/init.py", line 8, in <module>
from langchain.agents import MRKLChain, ReActChain, SelfAskWithSearchChain
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/agents/init.py", line 2, in <module>
from langchain.agents.agent import Agent
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/agents/agent.py", line 11, in <module>
from langchain.chains.llm import LLMChain
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/chains/init.py", line 2, in <module>
from langchain.chains.conversation.base import ConversationChain
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/chains/conversation/base.py", line 7, in <module>
from langchain.chains.conversation.memory import ConversationBufferMemory
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/chains/conversation/memory.py", line 7, in <module>
from langchain.chains.conversation.prompt import SUMMARY_PROMPT
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/chains/conversation/prompt.py", line 2, in <module>
from langchain.prompts.prompt import PromptTemplate
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/prompts/init.py", line 2, in <module>
from langchain.prompts.base import BasePromptTemplate
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/prompts/base.py", line 35, in <module>
class BasePromptTemplate(BaseModel, ABC):
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/langchain/prompts/base.py", line 41, in BasePromptTemplate
@root_validator()
File "/Users/jamesallison/Desktop/Python Apps Temp/LangchainPDF/.venv/lib/python3.8/site-packages/pydantic/deprecated/class_validators.py", line 228, in root_validator
raise PydanticUserError(

答案1

得分: 2

我在更新我的pydantic版本到2.0.3时遇到了这个问题,尝试了很多方法,但都没有成功。

后来,我观察到OpenAI和langchain的导入需要pydantic的版本小于2。

我将pydantic降级到了1.10.2(同时使用Python版本3.10.6),一切都正常工作了。

pip install pydantic==1.10.2
英文:

I had this issue when I updated my pydantic version to 2.0.3, tried many thing but nothing worked.

Later, I observed many that imports from OpenAI and langchain requires pydantic to be less than version 2.

I lowered pydantic to 1.10.2 (and python 3.10.6) and everything worked just fine.

pip install pydantic==1.10.2

答案2

得分: 0

我使用的Python和Langchain版本是多少?我之前使用的是0.0.20版本,遇到了相同的问题。升级到Python3.9和Langchain 0.0.224版本后,这个问题就解决了。

英文:

What is your Python and Langchain version? I was using 0.0.20 and I got the same issue. Upgrading to Python3.9 and langchain 0.0.224 fixed this issue for me.

答案3

得分: -1

以下是翻译好的部分:

与以下版本一起使用:

Python 3.7.12
langchain 0.0.27
pydantic 1.10.9

英文:

Worked with below versions:

Python 3.7.12
langchain  0.0.27
pydantic 1.10.9

huangapple
  • 本文由 发表于 2023年7月5日 00:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76614379.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定