I’m trying to learn building a RAG application by testing a python script with chromaDB and using local LLM for querying some pdf. Here is my code
import os
from langchain.document_loaders import PyPDFLoader, TextLoader, Docx2txtLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import Chroma
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
from gpt4all import GPT4All
# Set your Hugging Face API key as an environment variable
os.environ["HUGGINGFACE_API_KEY"] = "abc"
# Create a directory for documents if it doesn't exist
docs_dir = './docs'
if not os.path.exists(docs_dir):
os.makedirs(docs_dir)
# Load documents from the 'docs' directory
document = []
for file in os.listdir(docs_dir):
if file.endswith(".pdf"):
pdf_path = os.path.join(docs_dir, file)
loader = PyPDFLoader(pdf_path)
document.extend(loader.load())
elif file.endswith('.docx') or file.endswith('.doc'):
doc_path = os.path.join(docs_dir, file)
loader = Docx2txtLoader(doc_path)
document.extend(loader.load())
elif file.endswith('.txt'):
text_path = os.path.join(docs_dir, file)
loader = TextLoader(text_path)
document.extend(loader.load())
# Split documents into chunks
document_splitter = CharacterTextSplitter(separator='n', chunk_size=500, chunk_overlap=50)
document_chunks = document_splitter.split_documents(document)
# Initialize embeddings
embeddings = HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
# Initialize vector database (automatic persistence)
vectordb = Chroma.from_documents(document_chunks, embedding=embeddings, persist_directory='./data')
# Initialize local GPT4All model
model_path = "/Users/rayman/Library/Application Support/nomic.ai/GPT4All/nous-hermes-llama2-13b.Q4_0.gguf"
local_model = GPT4All(model_path)
# Initialize memory for conversation
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
# Create conversational QA Chain directly with GPT4All
pdf_qa = ConversationalRetrievalChain(llm=local_model,
retriever=vectordb.as_retriever(search_kwargs={'k': 6}),
verbose=False, memory=memory)
# Welcome message
print('---------------------------------------------------------------------------------')
print('Welcome to the DocBot. You are now ready to start interacting with your documents')
print('---------------------------------------------------------------------------------')
# Interactive loop
while True:
query = input(f"Prompt:")
if query.lower() in ["exit", "quit", "q", "f"]:
print('Exiting')
break
if query == '':
continue
result = pdf_qa({"question": query})
print(f"Answer: " + result["answer"])
but there is an error:
pydantic.v1.error_wrappers.ValidationError: 3 validation errors for ConversationalRetrievalChain
combine_docs_chain
field required (type=value_error.missing)
question_generator
field required (type=value_error.missing)
llm
extra fields not permitted (type=value_error.extra)