Skip to content

rafgger/RAG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– RAG (Retrieval-Augmented Generation) System

A simple implementation of a Retrieval-Augmented Generation system using OpenAI's GPT-4o, sentence transformers, and FAISS for efficient similarity search.

πŸ“– Overview

This project demonstrates how to build a RAG system that:

  • πŸ“ Splits text into sentences for better granular retrieval
  • πŸ”— Creates embeddings using SentenceTransformers
  • πŸ—ƒοΈ Stores embeddings in a FAISS index for fast similarity search
  • πŸ” Retrieves relevant context for user queries
  • πŸ€– Generates responses using OpenAI's GPT-4o with proper citations

✨ Features

  • πŸ” Environment-based configuration - API keys stored securely in .env file
  • πŸ“– Sentence-level retrieval - Fine-grained context retrieval using NLTK tokenization
  • ⚑ Efficient similarity search - FAISS indexing for fast vector search
  • πŸ“š Citation support - Generated responses include source citations
  • πŸ“Š Confidence scoring - AI-generated confidence scores for answers
  • πŸš€ Modern OpenAI API - Compatible with OpenAI Python library v1.0+

πŸ“‹ Prerequisites

  • 🐍Python 3.7+
  • πŸ”‘OpenAI API key
  • πŸ““Jupyter Notebook or JupyterLab

πŸš€ Installation

  1. πŸ“Clone or download this repository

  2. πŸ“¦Install required packages:

    pip install openai sentence-transformers faiss-cpu nltk tiktoken python-dotenv
  3. πŸ”Set up your environment variables:

    Create a .env file in the project directory:

    OPENAI_API_KEY=sk-your-actual-api-key-here
    

    ⚠️ Important: Replace sk-your-actual-api-key-here with your actual OpenAI API key.

πŸ’» Usage

πŸ”΄ Running the Notebook

  1. Open the Jupyter notebook: πŸ““

    jupyter notebook RAG.ipynb
  2. ⚑Execute the cells in order:

    • πŸ“¦Cell 1: Install dependencies
    • πŸ”§Cell 2: Import libraries and load environment variables
    • πŸ”Cell 3: Process text (split, embed, index)
    • πŸ“Cell 4: Define retrieval function
    • πŸ€–Cell 5: Define answer generation function
    • ❓Cell 6: Test with example query

πŸ’‘ Example Query

query = "What is RAG and how does it reduce hallucinations?"
print(generate_answer(query))

✨Expected Output:

Retrieval-Augmented Generation (RAG) is a method that improves large language models by allowing them to retrieve information from external documents [1]. It reduces hallucinations by first looking up relevant information and then generating a response using that retrieved context [3]. This approach makes the answers more fact-based [2].

Confidence Score: 1

βš™οΈ How It Works

1. πŸ“ Text Processing

  • Tokenization: Uses NLTK's sentence tokenizer to split documents into sentences πŸ“–
  • Embedding: Converts each sentence to a vector using all-MiniLM-L6-v2 model πŸ”’

2. πŸ—ƒοΈ Indexing

  • FAISS Index: Creates an efficient L2 distance-based index for similarity search 🎯
  • Storage: Embeddings are stored in memory for fast retrieval πŸ’Ύ

3. πŸ” Retrieval

  • Query Embedding: User queries are converted to the same vector space πŸ”„
  • Similarity Search: FAISS finds the most similar sentences 🎯
  • Ranking: Results are ranked by cosine similarity distance πŸ“Š

4. πŸ€– Generation

  • Context Assembly: Retrieved sentences are formatted with citation numbers πŸ“
  • Prompt Engineering: Structured prompt guides the AI to cite sources and provide confidence 🎯
  • Response Generation: OpenAI GPT-4o generates the final answer ✨

πŸ“ Project Structure

RAG/
β”œβ”€β”€ RAG.ipynb           # Main Jupyter notebook with implementation πŸ““
β”œβ”€β”€ .env                # Environment variables (API keys) πŸ”
β”œβ”€β”€ README.md           # This file πŸ“–
└── requirements.txt    # Python dependencies (optional) πŸ“¦

πŸ”§ Key Components

πŸ“¦ Dependencies

  • openai - OpenAI API client for GPT-4o πŸ€–
  • sentence-transformers - For creating sentence embeddings πŸ”’
  • faiss-cpu - Efficient similarity search and clustering 🎯
  • nltk - Natural language processing toolkit πŸ“
  • python-dotenv - Environment variable management πŸ”
  • tiktoken - OpenAI's tokenization library ⚑

πŸ› οΈ Main Functions

  • retrieve_sentences(query, top_k=4) - Retrieves most relevant sentences πŸ”
  • format_context(snippets) - Formats retrieved sentences with citations πŸ“
  • generate_answer(query) - Generates final answer with citations and confidence πŸ€–

🎨 Customization

πŸ”„ Changing the Embedding Model

embedder = SentenceTransformer("your-preferred-model")

βš™οΈ Adjusting Retrieval Parameters

top_snippets = retrieve_sentences(query, top_k=6)  # Retrieve more context

🎯 Modifying the Prompt

Edit the prompt in the generate_answer function to change response style or requirements.

πŸ”’ Security Notes

  • πŸ” API keys are stored in .env file (not in code)
  • πŸ“ Add .env to your .gitignore file
  • β›” Never commit API keys to version control
  • 🏭 Use environment variables in production

πŸ› Troubleshooting

⚠️ Common Issues

  1. NLTK Data Missing πŸ“š

    LookupError: Resource punkt_tab not found
    

    Solution: The notebook automatically downloads required NLTK data βœ…

  2. OpenAI API Error πŸ€–

    APIRemovedInV1: You tried to access openai.ChatCompletion
    

    Solution: Code uses the new OpenAI v1.0+ API syntax βœ…

  3. Missing API Key πŸ”‘

    ValueError: OPENAI_API_KEY not found in environment variables
    

    Solution: Check your .env file and ensure the API key is correct βœ…

πŸš€ Performance Tips

  • For larger documents, consider chunking strategies πŸ“„
  • Use GPU-accelerated FAISS for better performance with large datasets ⚑
  • Consider caching embeddings for frequently accessed documents πŸ’Ύ

πŸ“„ License

This project is open source and available under the MIT License.

🀝 Contributing

Feel free to submit issues, feature requests, or pull requests to improve this RAG implementation.

πŸ™ Acknowledgments

About

RAG with citing and confidence score

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors