Skip to content

A RAG-based system for efficiently querying and interacting with your KnowledgeBase

License

Notifications You must be signed in to change notification settings

ahammadnafiz/Personal-Knowledge-Assistant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

28 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“š Personal Knowledge Assistant

Personal Knowledge Assistant

An intelligent RAG-based platform for querying books and personal knowledge

Features β€’ Quick Start β€’ Architecture β€’ Backend Setup β€’ Frontend Setup β€’ Usage β€’ Contributing

Python Version Node.js Version FastAPI Next.js License


✨ Features

  • πŸ€– 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

πŸš€ Quick Start

# 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 dev

Open http://localhost:3000 to start using the application!


πŸ—οΈ System Architecture

System Architecture

Our system employs a sophisticated multi-stage architecture designed for optimal knowledge retrieval and response generation:

πŸ”„ Document Processing Pipeline

  • πŸ“– 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 Processing Engine

  • πŸ”§ 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

🎯 Confidence-Based Evaluation

  • πŸ“ˆ 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 Refinement

  • 🧩 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

🌐 Web Search Integration

  • πŸ”Ž 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

πŸ€– Response Generation

  • πŸ“ 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

πŸš€ Key Innovations

  1. 🎯 Confidence-Based Routing: Intelligently routes queries based on document relevance
  2. 🧩 Knowledge Strip Decomposition: Extracts and filters relevant information pieces
  3. πŸ”„ Dynamic Web Search Fallback: Uses web search when document knowledge is insufficient
  4. πŸ“Š Document Evaluation: Explicitly evaluates document relevance and reliability
  5. 🎯 Contextual Compression: Uses embeddings filtering and LLM extraction to improve retrieval quality

πŸ“‹ Prerequisites

🐍 Python 3.9+ Backend development

🟒 Node.js 18+ Frontend development

πŸ“š PDF Documents Your knowledge base


πŸ”§ Backend Setup

1. πŸ“ Project Structure

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

2. 🐍 Virtual Environment & Dependencies

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.6
pip install -r requirements.txt

3. πŸ”‘ Environment Configuration

Create 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:

  1. Visit Google AI Studio
  2. Create a new API key
  3. Copy and paste it in your .env file

4. πŸ“ Initialize Project Structure

# 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

5. πŸ“š Document Ingestion

# 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

6. πŸš€ Start the Backend Server

```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


🎨 Frontend Setup

1. πŸ†• Initialize Next.js Project

cd ../frontend
npx create-next-app@latest . --typescript --eslint --tailwind --src-dir --app --import-alias="@/*"

2. πŸ“¦ Install Dependencies

# 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/uuid

3. 🎨 Setup shadcn/ui

npx shadcn-ui@latest init

Configuration Options:

  • Style: Default
  • Base Color: Neutral
  • CSS variables: Yes

Install UI Components:

npx shadcn-ui@latest add button textarea card dialog alert

4. πŸ”§ Environment Configuration

Create .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 assistant

5. πŸš€ Start Development Server

npm run dev

βœ… Frontend is ready! Open http://localhost:3000 to view your application


🎯 Usage

πŸ“ Basic Queries

  1. πŸ“– 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"
    
  2. πŸ” Specific Information: Search for specific facts or details

    "What is the definition of machine learning?"
    "List the key algorithms mentioned in chapter 5"
    
  3. πŸ“Š Mathematical Concepts: Get beautifully rendered mathematical explanations

    "Explain the backpropagation algorithm with equations"
    "What is the formula for gradient descent?"
    

🎨 Features in Action

  • πŸ€– 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

πŸ”§ Troubleshooting

πŸ—‚οΈ 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:

  1. Ensure backend is running on port 8000
  2. Check CORS configuration in FastAPI
  3. Verify .env.local has correct API URL
  4. Check firewall settings
πŸ”‘ Authentication Errors

Problem: Google API key issues

Solutions:

  1. Verify API key in .env file
  2. Check API key permissions
  3. Ensure billing is enabled for Google AI
  4. Test API key independently

βš™οΈ Customization

🧠 LLM Model Configuration

# 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

πŸ”§ RAG Parameters

# 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

🎯 Embedding Model

# app/core/config.py
EMBEDDING_MODEL: str = "sentence-transformers/all-MiniLM-L6-v2"
# Alternatives: "all-mpnet-base-v2", "all-roberta-large-v1"

🀝 Contributing

We welcome contributions! Here's how to get started:

πŸ”§ Development Setup

  1. Fork the repository
  2. Create a feature branch
    git checkout -b feature/amazing-feature
  3. Make your changes
  4. Run tests
    # Backend tests
    cd backend && python -m pytest
    
    # Frontend tests
    cd frontend && npm test
  5. Submit a pull request

πŸ“‹ Guidelines

  • 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

πŸ“„ License

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


πŸ™ Acknowledgments

  • πŸ€— 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

About

A RAG-based system for efficiently querying and interacting with your KnowledgeBase

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •