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(
I was using LangChain 0.0.20 and I got the same issue. Upgrading to Python 3.9 and LangChain 0.0.224 fixed this issue for me.