How to use create_retrieval_chain in LangChain
Quick answer
Use
create_retrieval_chain from langchain.chains to quickly build a retrieval-augmented generation chain by combining a vectorstore retriever and an LLM. Pass your retriever and LLM instance to the function, then call the chain with a query to get context-aware answers.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install langchain openai faiss-cpu
Setup
Install the required packages and set your OpenAI API key in the environment variables.
pip install langchain openai faiss-cpu Step by step
This example shows how to create a simple retrieval chain using a FAISS vectorstore and OpenAI's GPT-4o model for answering questions based on documents.
import os
from langchain_openai import ChatOpenAI
from langchain_community.vectorstores import FAISS
from langchain.chains import create_retrieval_chain
from langchain_community.document_loaders import TextLoader
from langchain_openai import OpenAIEmbeddings
# Load documents
loader = TextLoader("example_docs.txt")
docs = loader.load()
# Create embeddings
embeddings = OpenAIEmbeddings(openai_api_key=os.environ["OPENAI_API_KEY"])
# Build FAISS vectorstore
vectorstore = FAISS.from_documents(docs, embeddings)
# Create retriever
retriever = vectorstore.as_retriever()
# Initialize LLM
llm = ChatOpenAI(model="gpt-4o", temperature=0, openai_api_key=os.environ["OPENAI_API_KEY"])
# Create retrieval chain
chain = create_retrieval_chain(llm=llm, retriever=retriever)
# Query the chain
query = "What is LangChain used for?"
result = chain.run(query)
print("Answer:", result) output
Answer: LangChain is used to build applications that combine language models with external data sources, enabling retrieval-augmented generation and complex workflows.
Common variations
- Use different LLMs like
gpt-4o-minior Anthropic'sclaude-3-5-sonnet-20241022by swapping the LLM instance. - Use async calls by importing and using async versions of the LLM and chain.
- Swap vectorstores like Chroma or Weaviate instead of FAISS.
- Customize the chain by passing additional parameters like
return_source_documents=Trueto get context documents.
Troubleshooting
- If you get empty answers, verify your documents are loaded and embedded correctly.
- Ensure your OpenAI API key is set in
os.environ["OPENAI_API_KEY"]. - Check that the vectorstore contains vectors by inspecting
vectorstore.index.ntotal. - For slow responses, reduce
max_tokensor use smaller models likegpt-4o-mini.
Key Takeaways
- Use
create_retrieval_chainto combine an LLM and retriever for retrieval-augmented QA. - Pass a vectorstore retriever and an LLM instance to build the chain quickly.
- Customize with different models, vectorstores, and chain parameters for your use case.