An AI-powered document analysis tool that uses LangChain and RAG (Retrieval-Augmented Generation) to enable intelligent querying of PDF documents. Built with FastAPI backend and Next.js frontend.
- Document Upload & Processing: Upload PDF documents with automatic text extraction and chunking
- Intelligent Q&A: Ask questions about your documents using natural language
- Document Summarization: Generate AI-powered summaries of uploaded documents
- Source Citations: Get references to specific pages and snippets from your documents
- Real-time Chat Interface: Interactive chat experience with your document knowledge base
- Dark/Light Theme: Modern UI with theme switching
- FastAPI: High-performance Python web framework
- LangChain: Core framework for building LLM applications
langchain-community: Community integrations (PyPDFLoader, FAISS)langchain-core: Core LangChain abstractions and interfaces
- FAISS: Facebook AI Similarity Search for vector storage
- Groq API: Fast inference API for Llama models
- PyPDF: PDF document processing
- Next.js 15: React framework with App Router
- Tailwind CSS 4: Utility-first CSS framework
- shadcn/ui: Modern UI component library
- React Dropzone: Drag-and-drop file upload
- Axios: HTTP client for API requests
- Docker: Containerization for consistent deployments
- Docker Compose: Multi-container orchestration
-
Document Loaders
from langchain_community.document_loaders import PyPDFLoader # Loads and extracts text from PDF documents
-
Text Splitters
from langchain.text_splitter import RecursiveCharacterTextSplitter # Intelligently chunks documents for optimal retrieval
-
Vector Stores
from langchain_community.vectorstores import FAISS # Stores document embeddings for similarity search
-
Custom Chat Models
from langchain_core.language_models.chat_models import BaseChatModel # Custom Groq integration for Llama model inference
-
RAG Chains
from langchain.chains import RetrievalQA # Combines retrieval with generation for accurate answers
-
Embeddings
from langchain_core.embeddings import Embeddings # Custom embedding implementation with fallback support
graph TD
A[PDF Upload] --> B[PyPDFLoader]
B --> C[RecursiveCharacterTextSplitter]
C --> D[Text Chunks]
D --> E[Custom Embeddings]
E --> F[FAISS Vector Store]
G[User Query] --> H[Similarity Search]
F --> H
H --> I[Retrieved Context]
I --> J[Groq LLM]
J --> K[Generated Answer]
K --> L[Response with Sources]
- Python 3.8+
- Node.js 18+
- Groq API key
- Docker and Docker Compose (for containerized setup)
-
Navigate to backend directory
cd backend -
Create virtual environment
python -m venv venv source venv/bin/activate # Linux/Mac # or venv\Scripts\activate # Windows
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables
# Create .env file echo "GROK_API_KEY=your_groq_api_key_here" > .env
-
Run the backend server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
-
Navigate to frontend directory
cd frontend -
Install dependencies
npm install
-
Run the development server
npm run dev
-
Create
.envfile in the project root# Create .env file echo "GROK_API_KEY=your_groq_api_key_here" > .env
-
Build and start the Docker containers
docker-compose up -d
-
Access the application
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Documentation: http://localhost:8000/docs
-
View logs (optional)
# View all logs docker-compose logs -f # View only backend logs docker-compose logs -f backend
-
Stop the containers when done
docker-compose down
GROK_API_KEY=your_groq_api_key_here- Visit Groq Console
- Sign up for an account
- Generate an API key
- Add it to your
.envfile
POST /documents/upload- Upload and process PDF documentsGET /documents/- List all processed documentsPOST /documents/{doc_id}/summarize- Generate document summary
POST /chat/- Ask questions about uploaded documents
import requests
# Upload a document
with open('document.pdf', 'rb') as f:
response = requests.post(
'http://localhost:8000/documents/upload',
files={'file': f}
)
# Ask a question
response = requests.post(
'http://localhost:8000/chat/',
json={
'question': 'What are the main conclusions of this research?',
'include_sources': True
}
)smart-research-assistant/
βββ backend/
β βββ app/
β β βββ config.py # Configuration and environment variables
β β βββ main.py # FastAPI application setup
β β βββ routers/
β β β βββ documents.py # Document upload/processing endpoints
β β β βββ chat.py # Chat/QA endpoints
β β βββ services/
β β β βββ document_processor.py # LangChain document processing
β β β βββ rag_service.py # RAG chain implementation
β β βββ utils/
β β βββ grok_integration.py # Custom Groq LLM integration
β β βββ error_handler.py # Error handling utilities
β βββ Dockerfile # Docker configuration for backend
β βββ requirements.txt # Python dependencies
β βββ .env # Environment variables
βββ frontend/
β βββ src/
β β βββ app/ # Next.js App Router
β β βββ components/ # React components
β β βββ lib/ # Utility functions
β βββ Dockerfile # Docker configuration for frontend
β βββ package.json # Node.js dependencies
β βββ tailwind.config.js # Tailwind configuration
βββ docker-compose.yml # Docker Compose configuration
βββ .env # Environment variables for Docker
The application is containerized using Docker with separate containers for the backend and frontend:
-
Backend Container:
- Python 3.10-based FastAPI service
- Auto-reload for development
- Persistent volume for uploaded files
- Health checks for reliability
-
Frontend Container:
- Node.js 18-based Next.js application
- Optimized production build
- Communication with backend API
- Health checks for reliability
Rebuilding after code changes:
docker-compose up -d --buildChecking container status:
docker-compose psViewing container logs:
docker-compose logs -fStopping and removing containers:
docker-compose down- PDF Loading: Uses
PyPDFLoaderto extract text from PDF files - Smart Chunking:
RecursiveCharacterTextSplitterbreaks documents into optimal chunks - Batch Processing: Handles large documents by processing in batches
- FAISS Integration: Efficient similarity search for document chunks
- Custom Embeddings: Fallback embedding system with normalized vectors
- Semantic Search: Retrieves most relevant document sections for queries
- Groq API Wrapper: Custom LangChain-compatible chat model
- Model Fallback: Automatic model selection from available Groq models
- Error Handling: Graceful degradation when API is unavailable
- RetrievalQA Chain: Combines document retrieval with answer generation
- Source Attribution: Returns specific document sources with answers
- Context Management: Optimizes context window usage for better responses
- Drag and drop a PDF file onto the upload area
- Wait for processing (chunking and embedding)
- Document appears in the sidebar
User: "What methodology was used in this research?"
Assistant: Based on the research paper, the methodology involved...
Sources: research_paper.pdf (Page 3) - "The study employed a mixed-methods approach..."
- Click "Summarize" button next to any uploaded document
- AI generates a structured summary with key points
- Summary appears in chat interface and modal dialog
- Chunk size: 1000 characters
- Overlap: 200 characters
- Preserves context across chunk boundaries
- Returns top 4 most relevant chunks
- Uses cosine similarity for ranking
- Includes source metadata (page numbers, snippets)
- Primary: Llama 3.1 8B Instant
- Fallback: Automatic model selection
- Temperature: 0.1 (focused responses)
- Model Availability: Automatically selects available Groq models
- File Processing: Handles corrupted PDFs and extraction failures
- API Limits: Graceful degradation with informative error messages
- Network Issues: Retry logic and offline fallbacks
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
For questions or issues:
- Check the API documentation at
/docs - Review the error logs in the console
- Ensure your Groq API key is valid
- Verify all dependencies are installed correctly
- Support for additional document formats (DOCX, TXT, HTML)
- Advanced search filters and sorting
- Document comparison capabilities
- Export chat history and summaries
- Multi-language support
- Integration with cloud storage providers
Built with β€οΈ using LangChain, FastAPI, and Next.js