How to Intermediate · 3 min read

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.

bash
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.

python
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-mini or Anthropic's claude-3-5-sonnet-20241022 by 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=True to 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_tokens or use smaller models like gpt-4o-mini.

Key Takeaways

  • Use create_retrieval_chain to 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.
Verified 2026-04 · gpt-4o, gpt-4o-mini, claude-3-5-sonnet-20241022
Verify ↗