Skip to main content
This program builds a simple Retrieval-Augmented Generation (RAG) application using:
  • Hugging Face
  • Sentence embeddings
  • FAISS
  • LangChain
  • A local language model
No API key is required for this implementation.

Overall Workflow


Output

Step 1: Import the Hugging Face Pipeline

The pipeline function is provided by the transformers library. It provides a simple way to load and use Hugging Face models. In this program, it is used to load:
for text generation. Conceptually:

Step 2: Import Document

The Document class is used to represent text in LangChain. A document can contain:
Example:
The main text is stored inside:

Step 3: Import PromptTemplate

PromptTemplate is used to create reusable prompts. Instead of manually creating a new prompt every time, placeholders can be used. Example:
Later, LangChain replaces:
with retrieved documents and:
with the user’s question.

Step 4: Import StrOutputParser

Language models can return different output formats. StrOutputParser() converts the model output into a plain Python string. Conceptually:

Step 5: Import FAISS

FAISS is used as the vector store. Its main purpose is to:
  • Store embeddings
  • Search for similar embeddings
  • Retrieve relevant documents
Conceptually:

Step 6: Import Hugging Face LangChain Components

Two important components are imported.

HuggingFaceEmbeddings

Used to convert text into numerical vectors.

HuggingFacePipeline

Used to connect a Hugging Face pipeline with LangChain.

Step 7: Create Sample Documents

The program creates a list of documents. Each document contains information about a specific topic. For example:
These documents act as the knowledge base for the RAG application. The complete flow starts with:

Step 8: Load the Embedding Model

The model:
converts text into numerical vectors called embeddings. Example:
Documents with similar meanings generally have embeddings that are closer together in vector space. For example:
These may have a higher similarity than:

Step 9: Create the FAISS Vector Store

This line performs several operations.
The from_documents() method automatically:
  1. Takes the documents
  2. Converts each document into an embedding
  3. Creates a FAISS index
  4. Stores the embeddings
  5. Maintains the connection between vectors and original documents
Conceptually:

Step 10: Create a Retriever

A retriever is responsible for finding relevant documents. The parameter:
means:
The retrieval process is:
For example:

Step 11: Create a Local Hugging Face Pipeline

This creates a local Hugging Face text-generation pipeline.

task="text-generation"

Specifies that the model should generate text. Conceptually:

model="distilgpt2"

Loads the distilgpt2 language model. It is a smaller version of GPT-2.
It is useful for:
  • Learning
  • Testing
  • Small local experiments
The model runs locally after it is downloaded.

max_new_tokens=100

Limits the maximum number of new tokens generated by the model. Example:
This prevents the model from generating excessively long responses.

do_sample=False

Disables random sampling. This makes generation more deterministic. Conceptually:
For a simple RAG application, predictable output is generally useful.

Step 12: Convert the Pipeline Into a LangChain LLM

The Hugging Face pipeline is wrapped inside a LangChain component. Before:
After:
This allows the model to be connected with:
  • Prompts
  • Chains
  • Output parsers
  • Other LangChain components

Step 13: Create the RAG Prompt

This creates a reusable prompt template. There are two placeholders:
and:

{context}

Contains the retrieved documents. Example:

{question}

Contains the user’s question. Example:
The final prompt sent to the model becomes:

Step 14: Create the Output Parser

This creates an output parser. The flow is:
For example:

Step 15: Get the Question From the User

This waits for user input. Example:
The value is stored in:
So:
contains:
The \n adds a new line before displaying the message.

Step 16: Retrieve Relevant Documents

This starts the retrieval process. Suppose:
The process becomes:
The result is stored in:
This is a list of LangChain Document objects.

Step 17: Display Retrieved Documents

This prints a heading. Then:
enumerate() loops through the retrieved documents. Example:
The parameter:
starts ranking from 1 instead of 0. Without start=1:
With:
the result is:
Then:
prints:
And:
prints the actual content of each retrieved document. Example:

Step 18: Combine Retrieved Documents Into Context

The retrieved documents are combined into one string. Suppose three documents are retrieved:
The join() operation creates:
The:
adds two newline characters between documents. The final context is stored in:

Step 19: Build the RAG Chain

This uses LCEL, which stands for:
The | operator connects components. The chain is:
More specifically:
This creates one complete generation workflow.

Step 20: Generate the Answer

The dictionary provides values for the placeholders.
These values replace:
and:
The complete process is:
The final result is stored in:

Step 21: Print the Final Answer

Prints the heading:
Then:
prints the generated answer. Example:

Complete Program Flow

Important Components

Final RAG Architecture