An intelligent RAG-based platform for querying books and personal knowledge
Features β’ Quick Start β’ Architecture β’ Backend Setup β’ Frontend Setup β’ Usage β’ Contributing
- π€ Intelligent Q&A: Query your personal document collection using natural language
- π§ Advanced RAG: Confidence-based routing with knowledge strip decomposition
- π Smart Search: Automatic web search fallback for low-confidence queries
- π Multi-format Support: PDF, TXT, MD, DOCX, and PPTX document ingestion
- π¨ Modern UI: Beautiful, responsive chat interface with real-time math rendering
- β‘ Fast Retrieval: FAISS vector store for lightning-fast document search
- π Dark/Light Mode: Seamless theme switching for better user experience
# Clone the repository
git clone https://github.com/your-username/personal-knowledge-assistant.git
cd personal-knowledge-assistant
# Backend setup
cd backend
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
# Add your Google API key to .env
echo "GOOGLE_API_KEY=your_key_here" > .env
# Ingest your documents
python ingest.py --dir your_books_directory
# Start backend
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
# Frontend setup (new terminal)
cd ../frontend
npm install
npm run devOpen http://localhost:3000 to start using the application!
Our system employs a sophisticated multi-stage architecture designed for optimal knowledge retrieval and response generation:
- π Document Loading: Processes PDF documents using PyPDFLoader
- βοΈ Text Chunking: Splits documents into manageable chunks using RecursiveCharacterTextSplitter
- π Embedding Generation: Converts chunks into vector representations using HuggingFaceEmbeddings
- πΎ Vector Storage: Stores embeddings in a FAISS vector store for efficient retrieval
- π§ Query Rewriting: Rewrites the original query to be more effective for retrieval
- π Base Retrieval: Retrieves initial set of relevant documents from the vector store
- π― Contextual Compression: Applies filtering and extraction to improve retrieval quality
- π Document Evaluation: Evaluates each retrieved document for relevance and reliability
- π’ Score Calculation: Combines relevance and reliability into a confidence score
- π€οΈ Confidence Routing: Routes the query to different processing paths based on confidence:
- π’ High Confidence (>0.7): Uses direct knowledge refinement
- π‘ Medium Confidence (0.3-0.7): Uses hybrid approach
- π΄ Low Confidence (<0.3): Falls back to web search
- π§© Knowledge Strip Decomposition: Breaks documents into individual "knowledge strips"
- β Strip Relevance Scoring: Scores each strip's relevance to the query
- π·οΈ Strip Filtering: Filters strips based on relevance threshold
- π Search Query Generation: Creates optimized search queries for low confidence scenarios
- π¦ DuckDuckGo Search: Performs web search using DuckDuckGo API
- β‘ Result Processing: Extracts and processes relevant information from search results
- π Prompt Template: Assembles a prompt with context, confidence level, and query
- π Conversation Memory: Maintains chat history for contextual responses
- π§ LLM Generation: Generates final response using Google Gemini 2.0 Flash model
- β¨ Response Formatting: Formats response based on confidence level with appropriate caveats
- π― Confidence-Based Routing: Intelligently routes queries based on document relevance
- π§© Knowledge Strip Decomposition: Extracts and filters relevant information pieces
- π Dynamic Web Search Fallback: Uses web search when document knowledge is insufficient
- π Document Evaluation: Explicitly evaluates document relevance and reliability
- π― Contextual Compression: Uses embeddings filtering and LLM extraction to improve retrieval quality
|
π Python 3.9+ Backend development |
π’ Node.js 18+ Frontend development |
π PDF Documents Your knowledge base |
Create and organize your project directory:
mkdir personal-knowledge-assistant
cd personal-knowledge-assistant
mkdir backend frontendπ View Complete Directory Structure
backend/
βββ app/
β βββ main.py # FastAPI application entry point
β βββ api/
β β βββ __init__.py
β β βββ routes/
β β βββ __init__.py
β β βββ chat.py # Chat endpoints
β β βββ upload.py # File upload endpoints
β βββ core/
β β βββ __init__.py
β β βββ config.py # Configuration settings
β β βββ security.py # Security utilities
β βββ db/
β β βββ __init__.py
β β βββ vector_store.py # Vector database operations
β βββ models/
β β βββ __init__.py
β β βββ schemas.py # Pydantic models
β βββ services/
β β βββ __init__.py
β β βββ rag.py # RAG implementation
β β βββ llm.py # LLM service
β βββ utils/
β βββ __init__.py
β βββ text_processing.py # Text utilities
βββ data/
β βββ vector_store/ # FAISS index storage
βββ knowledge_base/ # Uploaded documents
βββ ingest.py # Document ingestion script
βββ requirements.txt # Python dependencies
βββ .env # Environment variables
cd backend
python -m venv venv
# Activate virtual environment
source venv/bin/activate # Linux/macOS
# venv\Scripts\activate # Windowsπ¦ Install Dependencies:
π requirements.txt
```txt
fastapi>=0.104.1
uvicorn[standard]>=0.24.0
pydantic>=2.5.0
pydantic-settings>=2.1.0
langchain>=0.1.0
langchain-google-genai>=1.0.0
langchain-community>=0.0.13
langchain-huggingface>=0.0.1
faiss-cpu>=1.7.4
python-dotenv>=1.0.0
pypdf>=3.17.0
sentence-transformers>=2.2.2
python-multipart>=0.0.6pip install -r requirements.txtCreate a .env file in the backend directory:
# API Keys
GOOGLE_API_KEY=your_google_api_key_here
# Application Settings
ENVIRONMENT=development
DEBUG=true
HOST=0.0.0.0
PORT=8000
# Vector Store Settings
VECTOR_STORE_PATH=./data/vector_store
CHUNK_SIZE=1000
CHUNK_OVERLAP=200
TOP_K_RESULTS=5π Get your Google API key:
- Visit Google AI Studio
- Create a new API key
- Copy and paste it in your
.envfile
# Create all necessary __init__.py files
touch app/__init__.py
touch app/api/__init__.py
touch app/api/routes/__init__.py
touch app/core/__init__.py
touch app/db/__init__.py
touch app/models/__init__.py
touch app/services/__init__.py
touch app/utils/__init__.py
# Create data directories
mkdir -p data/vector_store
mkdir -p knowledge_base# Place your PDF documents in the knowledge_base directory
cp /path/to/your/books/*.pdf knowledge_base/
# Ingest documents into the vector store
python ingest.py --dir knowledge_base```bash
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000β Backend is ready! Your API will be available at
http://localhost:8000
cd ../frontend
npx create-next-app@latest . --typescript --eslint --tailwind --src-dir --app --import-alias="@/*"# Core dependencies
npm install lucide-react react-markdown framer-motion next-themes
# Math rendering
npm install katex remark-math rehype-katex
# UI components
npm install @radix-ui/react-dialog @radix-ui/react-slot class-variance-authority clsx tailwind-merge
# Additional utilities
npm install uuid @types/uuidnpx shadcn-ui@latest initConfiguration Options:
- Style:
Default - Base Color:
Neutral - CSS variables:
Yes
Install UI Components:
npx shadcn-ui@latest add button textarea card dialog alertCreate .env.local in the frontend directory:
# API Configuration
NEXT_PUBLIC_API_URL=http://localhost:8000/api
# Application Settings
NEXT_PUBLIC_APP_NAME=Personal Knowledge Assistant
NEXT_PUBLIC_APP_DESCRIPTION=Intelligent RAG-based knowledge assistantnpm run devβ Frontend is ready! Open http://localhost:3000 to view your application
-
π Document Questions: Ask questions about your uploaded documents
"What are the main themes in the uploaded book?" "Explain the concept of neural networks from the documents" -
π Specific Information: Search for specific facts or details
"What is the definition of machine learning?" "List the key algorithms mentioned in chapter 5" -
π Mathematical Concepts: Get beautifully rendered mathematical explanations
"Explain the backpropagation algorithm with equations" "What is the formula for gradient descent?"
- π€ Smart Responses: Confidence-based answers with source attribution
- π Web Search Fallback: Automatic web search for unknown topics
- π Math Rendering: Beautiful LaTeX equation rendering
- π Theme Support: Toggle between light and dark modes
- π± Responsive Design: Works seamlessly on all devices
ποΈ Vector Store Issues
Problem: Vector store corruption or missing embeddings
Solution:
# Remove corrupted vector store
rm -rf data/vector_store
# Re-ingest documents
python ingest.py --dir knowledge_baseπ API Connection Issues
Problem: Frontend can't connect to backend
Solutions:
- Ensure backend is running on port 8000
- Check CORS configuration in FastAPI
- Verify
.env.localhas correct API URL - Check firewall settings
π Authentication Errors
Problem: Google API key issues
Solutions:
- Verify API key in
.envfile - Check API key permissions
- Ensure billing is enabled for Google AI
- Test API key independently
# app/core/config.py
LLM_MODEL: str = "gemini-1.5-pro" # Options: gemini-1.5-pro, gemini-1.5-flash
TEMPERATURE: float = 0.7
MAX_TOKENS: int = 2048# app/core/config.py
CHUNK_SIZE: int = 1000 # Increase for larger context
CHUNK_OVERLAP: int = 200 # Reduce information loss
TOP_K_RESULTS: int = 5 # More comprehensive results
CONFIDENCE_THRESHOLD: float = 0.7 # Adjust routing sensitivity# app/core/config.py
EMBEDDING_MODEL: str = "sentence-transformers/all-MiniLM-L6-v2"
# Alternatives: "all-mpnet-base-v2", "all-roberta-large-v1"We welcome contributions! Here's how to get started:
- Fork the repository
- Create a feature branch
git checkout -b feature/amazing-feature
- Make your changes
- Run tests
# Backend tests cd backend && python -m pytest # Frontend tests cd frontend && npm test
- Submit a pull request
- Follow PEP 8 for Python code
- Use TypeScript for all new frontend code
- Add tests for new features
- Update documentation as needed
- Ensure all tests pass before submitting
This project is licensed under the MIT License - see the LICENSE file for details.
- π€ Hugging Face for embedding models
- π LangChain for RAG framework
- β‘ FastAPI for the robust backend
- βοΈ Next.js for the amazing frontend
- π¨ Tailwind CSS for beautiful styling
