Skip to content

About

This project demonstrates a simple Retrieval-Augmented Generation (RAG) pipeline using: Sentence embeddings (Sentence-BERT) Vector database (FAISS) Text generation model (GPT-2) The system allows users to upload a file, generate embeddings, store them in a vector database, and query the content using natural language.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Here’s README file OF Embeddings + Vector Database (FAISS) + RAG pipeline code:


📄 Embeddings & Vector Database (FAISS) – RAG Pipeline

📌 Overview

This project demonstrates a simple Retrieval-Augmented Generation (RAG) pipeline using:

  • Sentence embeddings (Sentence-BERT)
  • Vector database (FAISS)
  • Text generation model (GPT-2)

The system allows users to upload a file, generate embeddings, store them in a vector database, and query the content using natural language.


⚙️ Workflow

  1. File Upload

    • Upload a file using Google Colab.
  2. Embedding Generation

    • Convert text into dense vector representations using Sentence-BERT.
  3. Vector Storage

    • Store embeddings in a FAISS index for fast similarity search.
  4. Retrieval

    • Retrieve the most relevant documents based on a query.
  5. Text Generation

    • Use GPT-2 to generate answers based on retrieved context.

🧠 Technologies Used

  • sentence-transformers (for embeddings)
  • faiss-cpu (vector database)
  • transformers (text generation)
  • numpy
  • Google Colab (for file upload and execution)

📦 Installation

Run the following commands in your environment:

pip install sentence-transformers
pip install faiss-cpu
pip install transformers

📁 Code Structure

1️⃣ File Upload

from google.colab import files 
uploaded = files.upload()
file_path = list(uploaded.keys())[0]

2️⃣ Generate Embeddings

from sentence_transformers import SentenceTransformer
import numpy as np

model = SentenceTransformer("all-MiniLM-L6-v2")

doc_embeddings = model.encode(file_path)
doc_embeddings = np.array(doc_embeddings).astype("float32")

3️⃣ Store in FAISS

import faiss

doc_embeddings = doc_embeddings.reshape(1, -1)

dimension = doc_embeddings.shape[1]
index = faiss.IndexFlatL2(dimension)
index.add(doc_embeddings)

4️⃣ Retrieval Function

def retrieve(query, top_k=3):
    query_embedding = model.encode([query])
    query_embedding = np.array(query_embedding).astype("float32")

    distances, indices = index.search(query_embedding, top_k)
    results = [file_path[i] for i in indices[0]]
    return results

5️⃣ Text Generation Model

from transformers import pipeline
generator = pipeline("text-generation", model="gpt2")

6️⃣ RAG Pipeline

def rag_pipeline(query):
    retrieved_docs = retrieve(query, top_k=3)

    context = " ".join(retrieved_docs)

    prompt = f"""
    Answer the question based on the context below:

    Context: {context}

    Question: {query}
    Answer:
    """

    response = generator(prompt, max_length=150, num_return_sequences=1)
    answer = response[0]["generated_text"]

    return retrieved_docs, answer

7️⃣ Interactive Query Loop

while True:
    q = input("\nAsk (type exit to stop): ")
    if q.lower() == "exit":
        break
    answer = rag_pipeline(q)
    print("\nAnswer:\n", answer)

🚀 Example Usage

  1. Upload a file (e.g., .txt, .pdf converted text).

  2. Ask questions like:

    What is this document about?
    
  3. The system retrieves relevant content and generates an answer.


⚠️ Limitations

  • Currently embeds only the file name, not actual file content ❗
  • No document chunking (recommended for large files)
  • GPT-2 is not optimized for factual Q&A
  • FAISS index contains only a single vector

✅ Suggested Improvements

  • Read and embed actual file content
  • Split documents into chunks before embedding
  • Use better LLMs (e.g., GPT-3.5/4 or open-source alternatives)
  • Store multiple documents in FAISS
  • Add metadata for better retrieval

📚 Concept Summary

  • Embeddings: Convert text into numerical vectors
  • Vector Database: Stores vectors for similarity search
  • FAISS: Efficient similarity search library
  • RAG: Combines retrieval + generation for better answers

📜 License

This project is open-source and free to use for educational purposes.

About

This project demonstrates a simple Retrieval-Augmented Generation (RAG) pipeline using: Sentence embeddings (Sentence-BERT) Vector database (FAISS) Text generation model (GPT-2) The system allows users to upload a file, generate embeddings, store them in a vector database, and query the content using natural language.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages