![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
In this tutorial, we will explore the integration of LangChain, a programming framework for using large language models (LLMs) in applications, with Google’s Gemini LLM to build a Q&A application based on a PDF.
Before going ahead with the tutorial, make sure you have an API key from Google AI Studio.
Create a Python virtual environment and install the required modules from the requirements.txt file.
python -m venv venv source venv/bin/activate
Create the requirements.txt file with the below content:
pypdf2 chromadb google.generativeai langchain-google-genai langchain langchain_community jupyter
pip install -r requirements.txt
Set an environment variable to access the API key implicitly from the code.
export GOOGLE_API_KEY="YOUR_GOOGLE_API_KEY"
Launch Jupyter Notebook and get started with the code.
from langchain_google_genai import ChatGoogleGenerativeAI from langchain_google_genai import GoogleGenerativeAIEmbeddings from langchain.prompts import PromptTemplate from langchain_community.document_loaders import PyPDFLoader from langchain_text_splitters import CharacterTextSplitter from langchain.chains.combine_documents import create_stuff_documents_chain from langchain.chains import create_retrieval_chain from langchain.vectorstores import Chroma import os
This step loads the LLM with the Embeddings models responsible for converting text into lower-dimension vectors.
llm = ChatGoogleGenerativeAI(model="gemini-pro") embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
You can use any PDF of your choice. But for this tutorial, we will load the employee handbook of a fictitious company.
The code below loads the PDF and splits it into chunks of 250 characters, with an overlap of 50 characters between each chunk.
loader = PyPDFLoader("handbook.pdf")
text_splitter = CharacterTextSplitter(
separator=".",
chunk_size=250,
chunk_overlap=50,
length_function=len,
is_separator_regex=False,
)
pages = loader.load_and_split(text_splitter)
In the next step, we will convert each chunk of text into embeddings and store them in the Chroma vector database for retrieval. The parameter search_kwargs={"k": 5} defines the top matches to retrieve from a search.
vectordb=Chroma.from_documents(pages,embeddings)
retriever = vectordb.as_retriever(search_kwargs={"k": 5})
In LangChain, each component is assembled together to form a logical chain. In this scenario, we have the prompt, the LLM, and the retriever as the key components. Let’s create a chain from them.
template = """
You are a helpful AI assistant.
Answer based on the context provided.
context: {context}
input: {input}
answer:
"""
prompt = PromptTemplate.from_template(template)
combine_docs_chain = create_stuff_documents_chain(llm, prompt)
retrieval_chain = create_retrieval_chain(retriever, combine_docs_chain)
Now is the time to test if all the pieces of the puzzle are connected. We will invoke the chain and check the response.
response=retrieval_chain.invoke({"input":"How do I apply for personal leave?"})
print(response)
As we can see, the answer key has the expected response from the chain. Let’s print it out.
response["answer"]
Below is the complete code: