How to use Private LLM GPT4All with LangChain

LangChain, a language model processing library, provides an interface to work with various AI models including OpenAI’s gpt-3.5-turbo and Private LLM gpt4all. It enables users to embed documents, retrieve similar documents, and use document retrieval to augment Language Model conversations. This tutorial walks you through the process of using Private LLM gpt4all with LangChain to perform information extraction from PDF documents.
Prerequisites
Ensure that you have the following installed:
- langchain==0.0.173
- chromadb==0.3.23
- pypdf==3.8.1
- pygpt4all==1.1.0
- pdf2image==1.16.3
- poppler-utils
These packages are essential for processing PDFs, generating document embeddings, and using the gpt4all model. Poppler-utils is particularly important for converting PDF pages to images.
Step 1: Load the PDF Document
First, we need to load the PDF document. We use LangChain’s PyPDFLoader to load the document and split it into individual pages.
from langchain.document_loaders import PyPDFLoader
loader = PyPDFLoader("ms-financial-statement.pdf")
documents = loader.load_and_split()Step 2: Text Splitting
Next, we split the text into manageable chunks for the AI model. We use LangChain’s RecursiveCharacterTextSplitter for this task.
from langchain.text_splitter import RecursiveCharacterTextSplitter
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=64)
texts = text_splitter.split_documents(documents)Step 3: Creating Embeddings
We then create embeddings of the split text using HuggingFaceEmbeddings. This step creates a vector representation of each text chunk.
from langchain.embeddings import HuggingFaceEmbeddings
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")Step 4: Vector Store
We use the embeddings to create a vector store using Chroma. Chroma is a high-performance in-memory vector database designed for machine learning workloads.
from langchain.vectorstores import Chroma
db = Chroma.from_documents(texts, embeddings, persist_directory="db")Step 5: Load the gpt4all Model
We load the gpt4all model using LangChain’s GPT4All class.
from langchain.llms import GPT4All
model_path = "./ggml-gpt4all-j-v1.3-groovy.bin"
llm = GPT4All(model=model_path, n_ctx=1000, backend="gptj", verbose=False)Step 6: Create the Question-Answering Chain
We create a question-answering chain using LangChain’s RetrievalQA class. The retriever uses the Chroma vector store we created earlier.
from langchain.chains import RetrievalQA
qa = RetrievalQA.from_chain_type(
llm=llm,
chain_type="stuff",
retriever=db.as_retriever(search_kwargs={"k": 3}),
return_source_documents=True,
verbose=False,
)Step 7: Ask Questions
Finally, we can now ask questions. The question-answering chain will retrieve similar documents from the vector store, use the gpt4all model to generate responses, and return the responses.
res = qa("How much is the dividend per share during 2022? Extract it from the text.")This would generate a response like:
{'query': 'How much is the dividend per share during 2022? Extract it from the text.',
'result': ' The dividend per share during 2022 is $0.62.',
'source_documents': [Document(page_content='...', metadata={'source': 'ms-financial-statement.pdf', 'page': 0})]}You can print the result as follows:
print(res["result"])This will output:
The dividend per share during 2022 is $0.62.Conclusion
This tutorial walked you through using Private LLM gpt4all with LangChain. It included a step-by-step guide to loading and processing PDFs, generating embeddings, creating a vector store, and creating a question-answering chain. LangChain is a versatile and robust tool that allows you to leverage AI models, like Private LLM gpt4all, in various ways. Whether you’re trying to extract specific information from a document or aiming to augment your applications with AI, LangChain provides a streamlined and efficient approach.
Disclaimer: The code and models presented in this blog post are illustrative and not intended for production use without additional measures for handling edge cases, errors, and security.
Checkout ETL for Vector databases: https://metaheuristic.co




