0
0
LangchainHow-ToBeginner ยท 4 min read

How to Use RetrievalQA in Langchain: Simple Guide

Use RetrievalQA in Langchain by combining a retriever (like a vector store) with a language model to answer questions based on retrieved documents. Initialize RetrievalQA with your retriever and language model, then call run() with your query to get answers.
๐Ÿ“

Syntax

The RetrievalQA class in Langchain connects a retriever and a language model to answer questions from documents. You provide a retriever that fetches relevant documents and a language model that generates answers from those documents.

  • retriever: An object that fetches documents based on the query.
  • llm: The language model that processes documents and generates answers.
  • return_source_documents (optional): If true, returns the documents used to answer.
python
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI

retrieval_qa = RetrievalQA.from_chain_type(
    llm=OpenAI(),
    chain_type="stuff",
    retriever=my_retriever,
    return_source_documents=True
)

answer = retrieval_qa.run("Your question here")
๐Ÿ’ป

Example

This example shows how to create a simple RetrievalQA chain using an OpenAI language model and a FAISS vector store retriever. It demonstrates asking a question and getting an answer with source documents.

python
from langchain.vectorstores import FAISS
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain.llms import OpenAI
from langchain.chains import RetrievalQA

# Prepare embeddings and vector store (assume documents are already indexed)
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.load_local("faiss_index", embeddings)

# Create retriever from vectorstore
retriever = vectorstore.as_retriever()

# Initialize RetrievalQA with OpenAI LLM and retriever
qa = RetrievalQA.from_chain_type(
    llm=OpenAI(temperature=0),
    chain_type="stuff",
    retriever=retriever,
    return_source_documents=True
)

# Ask a question
query = "What is Langchain?"
result = qa.run(query)

print("Answer:", result)
Output
Answer: Langchain is a framework to build applications with language models by combining them with document retrieval and other tools.
โš ๏ธ

Common Pitfalls

Common mistakes when using RetrievalQA include:

  • Not providing a proper retriever object, causing no documents to be fetched.
  • Using a language model without specifying parameters like temperature, leading to inconsistent answers.
  • Forgetting to index documents into the vector store before creating the retriever.
  • Not handling the output format correctly when return_source_documents is true.
python
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI

# Wrong: passing None as retriever
retrieval_qa_wrong = RetrievalQA.from_chain_type(
    llm=OpenAI(),
    chain_type="stuff",
    retriever=None
)

# Right: pass a valid retriever
retrieval_qa_right = RetrievalQA.from_chain_type(
    llm=OpenAI(),
    chain_type="stuff",
    retriever=my_retriever
)
๐Ÿ“Š

Quick Reference

  • RetrievalQA.from_chain_type(): Main method to create the QA chain.
  • llm: Language model instance (e.g., OpenAI).
  • retriever: Retriever instance (e.g., FAISS retriever).
  • chain_type: How documents are combined ("stuff", "map_reduce", etc.).
  • return_source_documents: Whether to return documents with answers.
โœ…

Key Takeaways

Use RetrievalQA by combining a retriever and a language model to answer questions from documents.
Initialize RetrievalQA with from_chain_type(), providing llm and retriever parameters.
Ensure your retriever is properly set up with indexed documents before use.
Set return_source_documents=True to get documents that support the answer.
Avoid passing None as retriever and configure your language model for consistent results.