How to Use Retrieval-Augmented Generation (RAG) with LLMs

Sam Ozturk
3 min readJan 27, 2025

--

Library analogy for RAG systems

Large Language Models (LLMs) like GPT-4 are incredibly powerful, but they have limitations. One major challenge is their reliance on pre-trained knowledge, which can become outdated or lack domain-specific information. Retrieval-Augmented Generation (RAG) solves this problem by combining LLMs with external knowledge sources, enabling them to pull in relevant information dynamically.

In this article, we’ll explore what RAG is, how it works, and how you can implement it using popular tools like LangChain and Hugging Face Transformers.

What is Retrieval-Augmented Generation (RAG)?

RAG is a framework that enhances LLMs by integrating a retrieval mechanism. Instead of relying solely on the model’s internal knowledge, RAG retrieves relevant documents or data from an external source (e.g., a database or search engine) and uses this information to generate more accurate and context-aware responses.

Key Components of RAG:

  1. Retriever: Fetches relevant documents or data from an external source.
  2. Generator: An LLM that generates responses based on the retrieved information.

Why Use RAG?

  • Up-to-date Information: LLMs can access the latest data without retraining.
  • Domain-Specific Knowledge: RAG can pull in specialized information from curated datasets.
  • Improved Accuracy: By grounding responses in retrieved documents, RAG reduces hallucinations and errors.

Implementing RAG with LangChain and Hugging Face

Let’s walk through a practical example of building a RAG pipeline using LangChain (for orchestration) and Hugging Face Transformers (for the LLM).

Step 1: Install Required Libraries

First, install the necessary Python libraries:

pip install langchain transformers faiss-cpu sentence-transformers

Step 2: Set Up the Retriever

We’ll use FAISS (a library for efficient similarity search) to create a vector store for document retrieval. For simplicity, let’s assume we have a small dataset of documents.

from langchain.embeddings import HuggingFaceEmbeddings
from langchain.vectorstores import FAISS
from langchain.document_loaders import TextLoader

# Load documents (e.g., a text file)
loader = TextLoader("documents.txt")
documents = loader.load()

# Create embeddings and vector store
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vector_store = FAISS.from_documents(documents, embeddings)

# Set up the retriever
retriever = vector_store.as_retriever(search_kwargs={"k": 3}) # Retrieve top 3 documents

Step 3: Set Up the Generator

We’ll use Hugging Face’s transformers library to load a pre-trained LLM (e.g., GPT-2 or GPT-Neo).

from transformers import pipeline

# Load a text generation pipeline
generator = pipeline("text-generation", model="gpt2")

Step 4: Combine Retriever and Generator

Now, let’s integrate the retriever and generator using LangChain.

from langchain.chains import RetrievalQA

# Create a RetrievalQA chain
qa_chain = RetrievalQA.from_chain_type(
llm=generator,
chain_type="stuff",
retriever=retriever,
return_source_documents=True
)

# Define a query
query = "What is the capital of France?"

# Get the response
response = qa_chain({"query": query})
print("Answer:", response["result"])
print("Source Documents:", response["source_documents"])

Real-World Example: Building a FAQ Bot

Let’s say you want to build a FAQ bot for your company. You can use RAG to retrieve answers from a knowledge base and generate responses.

Step 1: Prepare the Knowledge Base

Store your FAQs in a text file (faq.txt):

Q: What is your return policy?
A: We offer a 30-day return policy for all products.

Q: Do you ship internationally?
A: Yes, we ship to over 100 countries worldwide.

Step 2: Query the FAQ Bot

Using the same RAG pipeline, you can now ask questions and get accurate answers:

query = "Do you ship internationally?"
response = qa_chain({"query": query})
print("Answer:", response["result"])

Output:

Answer: Yes, we ship to over 100 countries worldwide.

Best Practices for RAG

  1. Choose the Right Retriever: Use a retriever that matches your data type (e.g., FAISS for text, Elasticsearch for large datasets).
  2. Optimize Prompts: Craft prompts that guide the LLM to use retrieved information effectively.
  3. Evaluate Performance: Test your RAG pipeline with real-world queries to ensure accuracy and relevance.

Conclusion

Retrieval-Augmented Generation (RAG) is a game-changer for enhancing LLMs with external knowledge. By combining retrieval mechanisms with powerful generators, you can build applications that are more accurate, up-to-date, and domain-specific.

Whether you’re building a FAQ bot, a research assistant, or a customer support tool, RAG provides a flexible and scalable solution. Give it a try, and let me know how it works for your use case!

--

--

Sam Ozturk
Sam Ozturk

Written by Sam Ozturk

AI Engineer & Data Padawan. Non-technical post are at https://medium.com/@confused_matrix

No responses yet