Here’s README file OF Embeddings + Vector Database (FAISS) + RAG pipeline code:
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.
-
File Upload
- Upload a file using Google Colab.
-
Embedding Generation
- Convert text into dense vector representations using Sentence-BERT.
-
Vector Storage
- Store embeddings in a FAISS index for fast similarity search.
-
Retrieval
- Retrieve the most relevant documents based on a query.
-
Text Generation
- Use GPT-2 to generate answers based on retrieved context.
sentence-transformers(for embeddings)faiss-cpu(vector database)transformers(text generation)numpy- Google Colab (for file upload and execution)
Run the following commands in your environment:
pip install sentence-transformers
pip install faiss-cpu
pip install transformersfrom google.colab import files
uploaded = files.upload()
file_path = list(uploaded.keys())[0]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")import faiss
doc_embeddings = doc_embeddings.reshape(1, -1)
dimension = doc_embeddings.shape[1]
index = faiss.IndexFlatL2(dimension)
index.add(doc_embeddings)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 resultsfrom transformers import pipeline
generator = pipeline("text-generation", model="gpt2")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, answerwhile True:
q = input("\nAsk (type exit to stop): ")
if q.lower() == "exit":
break
answer = rag_pipeline(q)
print("\nAnswer:\n", answer)-
Upload a file (e.g.,
.txt,.pdfconverted text). -
Ask questions like:
What is this document about? -
The system retrieves relevant content and generates an answer.
- 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
- 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
- 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
This project is open-source and free to use for educational purposes.