Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

RAG Docs Processor: Intelligent Query-Retrieval System

πŸš€ High-Performance document analysis and query system optimized for sub-15 second responses with advanced caching and parallel processing.

⚑ Performance Features

  • Sub-15 Second Responses: Optimized pipeline achieving 4-12 second response times
  • Multi-Layer Caching: Redis + semantic similarity + in-memory caching
  • Smart Model Routing: Claude-3-Haiku for simple queries, GPT-4-Turbo for complex analysis
  • Parallel Processing: Concurrent question processing and async pipelines
  • Hot Document Indexing: FAISS in-memory search for frequently accessed documents
  • Cost Optimization: 60-80% cost reduction through intelligent caching

🎯 Core Features

  • Multi-format Document Processing: PDF, DOCX, and email document support
  • Hybrid Vector Search: Qdrant + Pinecone with semantic and keyword matching
  • LLM-Powered Responses: Dual-model architecture for optimal speed/accuracy
  • Explainable AI: Detailed reasoning, confidence scores, and source citations
  • Real-time Performance Monitoring: Comprehensive metrics and health checks
  • Scalable Architecture: Docker containerization with Redis, Qdrant, and PostgreSQL

πŸ—οΈ Optimized System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    Input Docs   β”‚    β”‚   Smart Caching  β”‚    β”‚   Model Router  β”‚
β”‚  PDF/DOCX/Email │───▢│ Redis + Semantic │───▢│  Claude/GPT-4   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                        β”‚                        β”‚
         β–Ό                        β–Ό                        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚Document Processorβ”‚    β”‚   Vector Search  β”‚    β”‚   Response Gen  β”‚
β”‚Parallel Chunking │───▢│  Qdrant + FAISS  │───▢│Sub-15s Response β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Performance Pipeline:

  1. Document Caching: Check Redis cache for processed documents
  2. Parallel Processing: Async document processing + question preprocessing
  3. Smart Routing: Complexity classifier routes to optimal model
  4. Vector Search: Qdrant primary, FAISS for hot documents
  5. Cached Results: Multi-layer caching for instant responses

Quick Start

Prerequisites

  • Python 3.11+
  • Primary: Anthropic API key (Claude-3-Haiku)
  • Fallback: OpenAI API key (GPT-4-Turbo + Embeddings)
  • Docker and Docker Compose (recommended)
  • Redis server (provided via Docker)
  • Qdrant server (provided via Docker)

Installation

Option 1: Docker Compose (Recommended)

  1. Clone the repository
git clone https://github.com/Aman-S-Rajput/RAG-docs-processor.git
cd rag-docs-processor
  1. Set up environment
cp .env.example .env
# Edit .env with your API keys (ANTHROPIC_API_KEY, OPENAI_API_KEY)
  1. Start services only (for development)
# Start Redis and Qdrant services
docker-compose -f docker-compose.dev.yml up -d

# Check services are running
docker-compose -f docker-compose.dev.yml ps
  1. Run the application locally
# Install Python dependencies
pip install -r requirements.txt

# Run the FastAPI application
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

Option 2: Full Stack with Docker

# Run everything (Redis + Qdrant + PostgreSQL + App)
docker-compose --profile full up --build

# Or run without the app profile (services only)
docker-compose up -d

Option 3: Manual Installation

  1. Clone the repository
git clone https://github.com/Aman-S-Rajput/RAG-docs-processor.git
cd rag-docs-processor
  1. Set up environment
cp .env.example .env
# Edit .env with your API keys
  1. Install dependencies
pip install -r requirements.txt
  1. Start external services manually
# You'll need to install and run Redis and Qdrant manually
# Redis: redis-server
# Qdrant: https://qdrant.tech/documentation/quick-start/
  1. Run the application
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000

🐳 Docker Deployment (Recommended)

Full Stack with Optimizations:

# Build and run optimized stack (Redis + Qdrant + PostgreSQL)
docker-compose up --build

# Check all services are healthy
docker-compose ps

# View logs
docker-compose logs -f app

Manual Docker Build:

docker build -t intelligent-query-engine .
docker run -p 8000:8000 --env-file .env intelligent-query-engine

Performance Testing:

# Test the optimized system
python test_api_sample.py

API Usage

Main Endpoint

POST /api/v1/engine/run

Process documents and answer questions:

{
  "documents": "https://example.com/policy.pdf",
  "questions": [
    "What is the grace period for premium payment?",
    "Does this policy cover maternity expenses?",
    "What is the waiting period for pre-existing diseases?"
  ]
}

Optimized Response:

{
  "answers": [
    "A grace period of thirty days is provided for premium payment...",
    "Yes, the policy covers maternity expenses with conditions...",
    "There is a waiting period of thirty-six (36) months..."
  ],
  "metadata": {
    "processing_time": 8.3,
    "total_tokens": 1850,
    "avg_confidence": 0.92,
    "target_time_met": true,
    "cache_hit_rate": 0.67,
    "model_usage": {
      "claude_haiku": 2,
      "gpt4_turbo": 1
    },
    "performance_metrics": {
      "questions_processed": 3,
      "avg_time_per_question": 2.8,
      "optimization_level": "high"
    }
  }
}

Authentication

All API endpoints require Bearer token authentication:

curl -H "Authorization: Bearer <YOUR_ACCESS_TOKEN>" \
     -X POST "http://localhost:8000/api/v1/engine/run" \
     -H "Content-Type: application/json" \
     -d @request.json

πŸš€ Performance & Management Endpoints

  • GET /api/v1/health - Health check
  • GET /api/v1/engine/performance - Performance metrics and statistics
  • POST /api/v1/engine/batch - Optimized batch question processing
  • POST /api/v1/engine/warm-up - Pre-warm system with common documents
  • POST /api/v1/engine/clear-cache - Clear all caches for fresh start
  • POST /api/v1/engine/analyze - Document structure analysis

Configuration

Environment Variables

Variable Description Default
ANTHROPIC_API_KEY Anthropic API key (Primary) Required
OPENAI_API_KEY OpenAI API key (Fallback) Required
REDIS_URL Redis cache server redis://localhost:6379
QDRANT_HOST Qdrant vector DB host localhost
TARGET_RESPONSE_TIME Performance target (seconds) 15
CHUNK_SIZE Optimized chunk size 1500
MAX_TOKENS Max LLM tokens 1500

βš™οΈ Performance Tuning

Model Configuration:

  • Primary LLM: Claude-3-Haiku (3x faster than GPT-4)
  • Complex Queries: Auto-fallback to GPT-4-Turbo
  • Embeddings: OpenAI text-embedding-3-large (reduced to 1536D)

Caching Strategy:

  • Redis TTL: Documents (2h), Embeddings (24h), Q&A (30m)
  • Semantic Cache: 95% similarity threshold for question matching
  • Hot Documents: Top 10 documents kept in FAISS memory

Performance Targets:

  • Response Time: 4-12 seconds (target: <15s)
  • Cache Hit Rate: 60-80% for repeated queries
  • Token Efficiency: 60-80% reduction vs baseline
  • Concurrency: 50 concurrent requests supported

Development

Project Structure

app/
β”œβ”€β”€ main.py                 # FastAPI application
β”œβ”€β”€ api/endpoints/          # API route handlers
β”œβ”€β”€ core/                   # Configuration and security
β”œβ”€β”€ services/               # Business logic services
β”œβ”€β”€ models/                 # Data models and schemas
└── utils/                  # Utility functions

services/
β”œβ”€β”€ document_processor.py          # Document ingestion
β”œβ”€β”€ cache_service.py               # Multi-layer caching
β”œβ”€β”€ fast_vector_service.py         # Qdrant + FAISS operations
β”œβ”€β”€ optimized_llm_service.py       # Claude + GPT-4 routing
└── optimized_retrieval_service.py # High-performance orchestration

πŸ§ͺ Running Performance Tests

Comprehensive Test Suite:

# Run optimized performance tests
python test_api_sample.py

# Unit tests
pytest tests/ -v

# Performance benchmarking
pytest tests/ -v --benchmark-only

Test Coverage:

  • βœ… Sub-15 second response validation
  • βœ… Cache performance and hit rates
  • βœ… Concurrent request handling
  • βœ… Model routing efficiency
  • βœ… Memory usage optimization

Code Quality

# Format code
black app/

# Type checking
mypy app/

# Linting
flake8 app/

Evaluation Metrics

The system is optimized for:

  • Accuracy: Precision of query understanding and clause matching
  • Token Efficiency: Optimized LLM token usage and cost-effectiveness
  • Latency: Response speed and real-time performance
  • Reusability: Modular code and extensible architecture
  • Explainability: Clear reasoning and source traceability

πŸ“Š System Benchmarks

System Performance Metrics

Metric Target Achieved Status
Response Time < 15s 4-12s 🟒 Optimized
Cache Hit Rate > 60% 60-80% 🟒 High Efficiency
Token Usage < 2000/query 1200-1800 🟒 Reduced
Accuracy > 85% 87-94% 🟒 High Accuracy
Confidence Score > 0.8 0.85-0.95 🟒 High Confidence
Cost per Request Minimize $0.02-0.04 🟒 Low Cost
Concurrent Users 50+ Tested: 50 🟒 Scalable

Troubleshooting

Common Issues

  1. Authentication Errors
  • Verify API key in Authorization header
  • Check .env file configuration
  1. Document Processing Failures
  • Ensure document URL is accessible
  • Check file format (PDF, DOCX supported)
  • Verify file size limits
  1. Embedding Service Issues
  • Verify Pinecone API key and index configuration
  • Check OpenAI API key and rate limits

Health Checks

Monitor system health:

curl http://localhost:8000/api/v1/health

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make changes with tests
  4. Submit a pull request

License

This project is licensed under the MIT License.

Support

For issues and questions:

  • Create an issue in the repository
  • Check the troubleshooting section
  • Review the API documentation

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages