Skip to content

robinpats182/Document-Intelligence-Agent-RAG

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“„ Document Intelligence Agent - RAG

A powerful PDF chat application that leverages Retrieval-Augmented Generation (RAG) to enable intelligent conversations with your documents.

Live Demo Hugging Face

🎯 Overview

This application allows users to upload PDF documents and interact with them through natural language questions. Using advanced AI techniques, it retrieves relevant information from your documents and provides accurate, context-aware answers.

πŸ”— Try it Live

✨ Features

  • πŸ“€ Easy PDF Upload: Drag and drop PDF documents through an intuitive interface
  • πŸ€– AI-Powered Conversations: Ask questions in natural language and get intelligent responses
  • ⚑ Auto-Indexing: Documents are automatically processed and indexed upon upload
  • πŸ” Semantic Search: Uses vector embeddings for accurate information retrieval
  • πŸ’¬ Chat History: Maintains conversation context throughout your session
  • 🎨 Clean Interface: Modern, user-friendly Streamlit interface

πŸ—οΈ Architecture

Technology Stack

Component Technology
Frontend Streamlit
LLM Meta-Llama-3-8B-Instruct
Embeddings sentence-transformers/all-MiniLM-L6-v2
Vector Store FAISS
Framework LangChain 0.3+ (LCEL)
PDF Processing PyPDFLoader
Text Splitting RecursiveCharacterTextSplitter

System Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  PDF Upload β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ PyPDFLoader     β”‚
β”‚ Extract Text    β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Text Splitter       β”‚
β”‚ (1000 chars, 150    β”‚
β”‚  overlap)           β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ HuggingFace         β”‚
β”‚ Embeddings          β”‚
β”‚ (all-MiniLM-L6-v2)  β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ FAISS Vector Store  β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ User Query          β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Retriever (k=3)     β”‚
β”‚ Get relevant chunks β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ RAG Chain (LCEL)    β”‚
β”‚ Context + Question  β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Llama 3 Model       β”‚
β”‚ Generate Answer     β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Display Response    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Getting Started

Prerequisites

  • Python 3.8+
  • Hugging Face API Token

Installation

  1. Clone the repository

    git clone https://huggingface.co/spaces/Pats182/Document-Intelligence-Agent-RAG
    cd Document-Intelligence-Agent-RAG
  2. Install dependencies

    pip install -r requirements.txt
  3. Set up environment variables

    export HF_Token="your_huggingface_token_here"
  4. Run the application

    streamlit run src/streamlit_app.py

Usage

  1. Upload a PDF: Click the "Upload PDF" button in the sidebar
  2. Wait for Processing: The document will be automatically indexed
  3. Ask Questions: Type your questions in the chat input
  4. Get Answers: Receive AI-generated responses based on your document

πŸ”§ Configuration

Model Parameters

  • Temperature: 0.3 (for more focused responses)
  • Max Tokens: 512
  • Retrieval Top-K: 3 chunks
  • Chunk Size: 1000 characters
  • Chunk Overlap: 150 characters

Customization

You can modify these parameters in streamlit_app.py:

# Adjust LLM parameters
base_llm = HuggingFaceEndpoint(
    repo_id="meta-llama/Meta-Llama-3-8B-Instruct",
    temperature=0.3,  # Adjust for creativity
    max_new_tokens=512,  # Adjust response length
)

# Adjust text splitting
text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=1000,  # Adjust chunk size
    chunk_overlap=150  # Adjust overlap
)

# Adjust retrieval
retriever = vector_store.as_retriever(
    search_kwargs={"k": 3}  # Number of chunks to retrieve
)

πŸ“Š Key Components

PDF Processing

The application uses PyPDFLoader to extract text from PDF documents with proper error handling and temporary file management.

Vector Embeddings

Documents are converted into vector embeddings using sentence-transformers/all-MiniLM-L6-v2, enabling semantic search capabilities.

RAG Chain

Built with LangChain's modern LCEL (LangChain Expression Language), the RAG chain:

  1. Retrieves relevant document chunks
  2. Formats them with the user's question
  3. Generates contextual responses using Llama 3

Prompt Template

Based on the following context, answer the question accurately and concisely.
If the answer is not in the context, say "I don't have enough information to answer that."

Context: {context}
Question: {question}
Answer:

🎨 Features in Detail

Auto-Indexing

  • Documents are processed immediately upon upload
  • No manual indexing button required
  • Visual feedback during processing

Session Management

  • Chat history persists during the session
  • Current document tracking
  • Clear chat option available

Error Handling

  • Comprehensive try-catch blocks
  • User-friendly error messages
  • Proper temporary file cleanup

πŸ› οΈ Advanced Usage

Re-indexing Documents

If you need to reprocess a document:

  1. Click the "πŸ”„ Re-index" button in the sidebar
  2. Wait for processing to complete

Clearing Chat History

To start a fresh conversation:

  1. Click the "πŸ—‘οΈ Clear Chat" button in the sidebar

πŸ“ˆ Performance

  • Fast Retrieval: FAISS enables efficient similarity search
  • Optimized Chunks: 1000-character chunks with 150-character overlap ensure context preservation
  • Cached Models: Streamlit caching reduces model loading time

πŸ”’ Security

  • API tokens are securely managed through environment variables
  • Temporary files are properly cleaned up after processing
  • No data persistence beyond the session

🀝 Contributing

Contributions are welcome! Feel free to:

  • Report bugs
  • Suggest features
  • Submit pull requests

πŸ“ License

This project is open source and available under standard terms.

πŸ™ Acknowledgments

πŸ“ž Contact

For questions or feedback, please visit the Hugging Face Space.


πŸš€ Launch Live Demo

About

AI agent that understands the content of the pdf

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors