Thanks to visit codestin.com
Credit goes to github.com

Skip to content

A Retrieval-Augmented Generation (RAG) chatbot built with LangChain, HuggingFace embeddings, FAISS vector store, and Google's Gemini 2.0 Flash model.

License

Notifications You must be signed in to change notification settings

kawish918/RAG_chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– RAG Chatbot with Google Gemini 2.0

A Retrieval-Augmented Generation (RAG) chatbot built with LangChain, HuggingFace embeddings, FAISS vector store, and Google's Gemini 2.0 Flash model. This chatbot can answer questions by retrieving relevant information from your documents and generating contextual responses.

✨ Features

  • Document Loading: Automatically loads and processes text documents
  • Text Chunking: Intelligently splits documents into manageable chunks
  • Vector Embeddings: Uses HuggingFace sentence transformers for semantic search
  • FAISS Vector Store: Fast similarity search and clustering of dense vectors
  • Google Gemini 2.0: Powered by Google's latest generative AI model
  • Interactive Chat: Real-time question-answering interface
  • RAG Pipeline: Combines retrieval and generation for accurate, context-aware responses

πŸ“‹ Prerequisites

  • Python 3.8 or higher
  • Google AI Studio API key
  • Windows/Linux/macOS

πŸš€ Quick Start

1. Clone the Repository

git clone https://github.com/kawish918/RAG_chatbot.git
cd rag_chatbot

2. Set Up Virtual Environment

# Create virtual environment
python -m venv rag_env

# Activate virtual environment
# On Windows:
rag_env\Scripts\activate
# On macOS/Linux:
source rag_env/bin/activate

3. Install Dependencies

pip install langchain-community
pip install langchain
pip install langchain-huggingface
pip install faiss-cpu
pip install langchain-google-genai
pip install sentence-transformers

4. Get Google AI API Key

  1. Visit Google AI Studio
  2. Sign in with your Google account
  3. Create a new API key
  4. Copy the API key

5. Configure API Key

  1. Copy the example environment file and create your own .env file:
# Copy the template
cp .env.example .env
  1. Edit the .env file and add your actual Google API key:
GOOGLE_API_KEY=your_actual_api_key_here

6. Run the Chatbot

python chatbot.py

πŸ’‘ How It Works

RAG Architecture

  1. Document Loading: The system loads text documents using LangChain's TextLoader
  2. Text Splitting: Documents are split into smaller chunks (500 characters with 50-character overlap)
  3. Embedding Generation: Each chunk is converted to vector embeddings using HuggingFace's all-MiniLM-L6-v2 model
  4. Vector Storage: Embeddings are stored in FAISS for fast similarity search
  5. Query Processing: User questions are embedded and matched against stored vectors
  6. Context Retrieval: Most relevant document chunks are retrieved
  7. Response Generation: Google Gemini 2.0 generates answers based on retrieved context

Code Structure

rag_chatbot/
β”œβ”€β”€ chatbot.py              # Main chatbot application
β”œβ”€β”€ machine_learning.txt    # Sample knowledge base
β”œβ”€β”€ README.md              # This file
└── rag_env/               # Virtual environment

πŸ”§ Configuration

Customizing Text Chunking

text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=500,         # Adjust chunk size
    chunk_overlap=50        # Adjust overlap
)

Changing Embedding Model

embeddings = HuggingFaceEmbeddings(
    model_name="sentence-transformers/all-MiniLM-L6-v2"  # Change model
)

Using Different LLM Models

llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp")  # Available models

πŸ“š Adding Your Own Documents

  1. Replace or add content to machine_learning.txt
  2. Or modify the code to load multiple documents:
# Load multiple files
from langchain_community.document_loaders import DirectoryLoader

loader = DirectoryLoader("./documents", glob="*.txt")
documents = loader.load()

Example Usage

πŸš€ Gemini 2.0 RAG Chatbot is ready! Ask a question about Machine Learning.

Ask a question (or type 'exit' to quit): What is machine learning?

πŸ€– AI: Machine learning is a subset of artificial intelligence (AI) that enables 
systems to learn from data and make decisions without explicit programming. It allows 
computers to automatically improve their performance on a specific task through 
experience, without being explicitly programmed for every possible scenario.

Ask a question (or type 'exit' to quit): What are the types of machine learning?

πŸ€– AI: Based on the information provided, machine learning can be categorized into 
three main types:

1. **Supervised Learning** - Learning with labeled data
2. **Unsupervised Learning** - Learning patterns from unlabeled data  
3. **Reinforcement Learning** - Learning through interaction and feedback

Ask a question (or type 'exit' to quit): exit
πŸ‘‹ Goodbye!

πŸ› οΈ Troubleshooting

Common Issues

1. API Key Error

Error: Invalid API key
  • Solution: Ensure your Google AI API key is correct and active

2. Import Errors

ModuleNotFoundError: No module named 'langchain'
  • Solution: Activate virtual environment and install dependencies

3. FAISS Installation Issues

Error installing faiss-cpu
  • Solution: Use pip install faiss-cpu or conda install faiss-cpu

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

About

A Retrieval-Augmented Generation (RAG) chatbot built with LangChain, HuggingFace embeddings, FAISS vector store, and Google's Gemini 2.0 Flash model.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages