This project is a sophisticated chat application that combines local and cloud-based Large Language Models (LLMs) with Retrieval-Augmented Generation (RAG) capabilities. It focuses on providing accurate responses to academic and research-related queries by leveraging a local FAISS vector store containing arXiv paper abstracts.
The application offers two modes of operation:
- Local RAG Mode (Gemma): Uses a quantized Gemma 1B model (GGUF format) for fast local inference with RAG capabilities.
- Cloud API Mode (Gemini): Leverages Google's Gemini API for more powerful capabilities when needed.
Additionally, the system implements an innovative hybrid approach where the local model can delegate paper suggestion queries to Gemini API for improved accuracy.
- Dual Model Support: Switch between local Gemma (quantized GGUF model) and cloud-based Gemini API
- RAG Implementation: Vector similarity search using FAISS index of arXiv abstracts
- Context Preservation: Maintains chat history for continuous conversation
- Folder Organization: Organize chats into custom folders
- Responsive UI: Modern and intuitive user interface with animations
- Hybrid Model Approach: Delegates specific queries to cloud model when appropriate
- Streaming Responses: Real-time streaming of responses for better user experience
- Database Persistence: Stores conversations using SQLite
- Markdown Support: Renders responses with Markdown formatting
- Python 3.9+ (recommended: 3.10+)
- CUDA-enabled GPU (optional, for faster inference)
- At least 8GB RAM (16GB+ recommended)
- Google Gemini API key (optional, for Gemini model access)
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Create and activate a virtual environment:
python -m venv arxiv_env source arxiv_env/bin/activate # Linux/macOS # OR arxiv_env\Scripts\activate # Windows
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables (create a
.envfile in the project root):GEMINI_API_KEY=your_gemini_api_key_here -
Prepare the model (Option A - Download pre-quantized model):
- Download the quantized Gemma model (
gemma_1b_finetuned_q4_0.gguf) to the project directory
OR
Prepare the model (Option B - Quantize your own model):
python merge_and_save_gemma.py # Merges the model checkpoints # Then use a tool like llama.cpp to quantize the model
- Download the quantized Gemma model (
python collect_arxiv_abstracts.pyThis script fetches recent paper abstracts from arXiv and saves them to ./arxiv_abstracts/abstracts.json.
python generate_embeddings.pyThis creates vector embeddings for the abstracts using the Sentence Transformers model.
python build_faiss_index.pyCreates a raw FAISS index from the embeddings.
python create_lc_faiss_index.pyCreates a LangChain-compatible FAISS index in the langchain_faiss_store_optimized directory.
-
Initialize the database (happens automatically on first run):
# The database schema in static/schema.sql will be applied automatically -
Start the Flask server:
python app2.py
-
Access the application: Open your browser and navigate to
http://localhost:5001
-
Frontend:
- HTML/CSS/JavaScript interface
- GSAP for animations
- Marked.js for Markdown rendering
- Lottie for advanced animations
-
Backend:
- Flask web server
- SQLite database
- Llama.cpp for local model inference
- FAISS vector store for similarity search
- LangChain for RAG implementation
-
Models:
- Local: Quantized Gemma 1B model (GGUF format)
- Cloud: Google Gemini API
-
Data Flow:
- User query β Context retrieval β Model inference β Streaming response
- Folders Table: Organizes chats into categories
- Chats Table: Stores chat metadata and references to folders
- Messages Table: Contains individual chat messages
βββ app2.py # Main Flask application
βββ static/ # Static assets
β βββ style2.css # CSS styles
β βββ script2.js # Frontend JavaScript
β βββ schema.sql # Database schema
βββ templates/ # HTML templates
β βββ index2.html # Main interface template
βββ collect_arxiv_abstracts.py # Script to collect arXiv data
βββ generate_embeddings.py # Script to create embeddings
βββ build_faiss_index.py # Script to build raw FAISS index
βββ create_lc_faiss_index.py # Script for LangChain FAISS index
βββ gemma_1b_finetuned_q4_0.gguf # Quantized Gemma model
βββ langchain_faiss_store_optimized/ # FAISS vector store directory
β βββ index.faiss # FAISS index file
β βββ index.pkl # Python pickle file with metadata
βββ chat_app.db # SQLite database
The system implements a sophisticated approach for paper recommendations:
- When users ask for paper recommendations, the query is first sent to the Gemini API
- Gemini suggests relevant papers and potential arXiv IDs
- The local FAISS index verifies these suggestions against the local knowledge base
- Verified papers are presented to the user with detailed information
The application intelligently manages conversation context:
- Maintains a sliding window of previous messages (configured as MAX_HISTORY_MESSAGES)
- Formats the conversation history appropriately for the model
- Ensures coherent multi-turn conversations
The system implements efficient response streaming:
- Manages concurrent database operations during streaming
- Handles cleanup and resource management properly
- Provides real-time feedback to the user
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.