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.
- 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
- Python 3.8 or higher
- Google AI Studio API key
- Windows/Linux/macOS
git clone https://github.com/kawish918/RAG_chatbot.git
cd rag_chatbot# Create virtual environment
python -m venv rag_env
# Activate virtual environment
# On Windows:
rag_env\Scripts\activate
# On macOS/Linux:
source rag_env/bin/activatepip install langchain-community
pip install langchain
pip install langchain-huggingface
pip install faiss-cpu
pip install langchain-google-genai
pip install sentence-transformers- Visit Google AI Studio
- Sign in with your Google account
- Create a new API key
- Copy the API key
- Copy the example environment file and create your own
.envfile:
# Copy the template
cp .env.example .env- Edit the
.envfile and add your actual Google API key:
GOOGLE_API_KEY=your_actual_api_key_herepython chatbot.py- Document Loading: The system loads text documents using LangChain's TextLoader
- Text Splitting: Documents are split into smaller chunks (500 characters with 50-character overlap)
- Embedding Generation: Each chunk is converted to vector embeddings using HuggingFace's
all-MiniLM-L6-v2model - Vector Storage: Embeddings are stored in FAISS for fast similarity search
- Query Processing: User questions are embedded and matched against stored vectors
- Context Retrieval: Most relevant document chunks are retrieved
- Response Generation: Google Gemini 2.0 generates answers based on retrieved context
rag_chatbot/
βββ chatbot.py # Main chatbot application
βββ machine_learning.txt # Sample knowledge base
βββ README.md # This file
βββ rag_env/ # Virtual environment
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=500, # Adjust chunk size
chunk_overlap=50 # Adjust overlap
)embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2" # Change model
)llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash-exp") # Available models- Replace or add content to
machine_learning.txt - 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()π 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!
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-cpuorconda install faiss-cpu
This project is licensed under the MIT License - see the LICENSE file for details.
- LangChain for the RAG framework
- HuggingFace for embedding models
- Google AI for Gemini 2.0 model
- FAISS for vector similarity search