OrgAI / backend /README.md
Phonex
TheTruthSchool_RAG
167596f

Enhanced RAG-Anything Backend API

Production-ready FastAPI backend for the RAG-Anything system with multi-domain support and advanced AI features.

Features

🎯 Multi-Domain Support

  • Medical & Healthcare: Medical documents, research papers, clinical guidelines
  • Legal & Compliance: Legal documents, contracts, regulations, case law
  • Financial & Analytics: Financial reports, analysis, market research
  • Technical Documentation: Technical docs, APIs, code, architecture
  • Academic Research: Research papers, academic publications, studies

🚀 Advanced AI Capabilities

  • Query Improvement: Automatic query enhancement with abbreviation expansion
  • Dual-LLM Verification: Two-stage answer verification for quality assurance
  • Conversation Memory: Context-aware responses with conversation history
  • Multimodal Processing: Support for images, tables, and equations
  • Domain-Specific Prompts: Optimized prompts for each domain

🔧 Technical Features

  • Gemini API Integration: Free-tier Gemini 1.5 Flash model
  • Async Processing: Background document processing
  • RESTful API: Clean, well-documented endpoints
  • CORS Support: Cross-origin resource sharing enabled
  • Error Handling: Comprehensive error handling and logging

Installation

Prerequisites

Setup

  1. Clone the repository
cd /mnt/data/Agentic_RAG/backend
  1. Install dependencies
pip install -r requirements.txt
  1. Set up environment variables
export GEMINI_API_KEY="your-api-key-here"

Or create a .env file:

GEMINI_API_KEY=your-api-key-here
  1. Run the server
python main.py

Or using uvicorn directly:

uvicorn main:app --host 0.0.0.0 --port 8000 --reload

API Endpoints

Health Check

GET /health

Response:

{
  "status": "healthy",
  "timestamp": "2025-01-04T10:00:00",
  "version": "1.0.0",
  "features": {
    "query_improvement": true,
    "dual_llm_verification": true,
    "conversation_memory": true,
    "multi_domain": true,
    "multimodal_processing": true,
    "gemini_integration": true
  },
  "domains": ["medical", "legal", "financial", "technical", "academic"]
}

List Domains

GET /domains

Upload Document

POST /upload
Content-Type: multipart/form-data

file: <document file>
domain: medical

Response:

{
  "success": true,
  "message": "Document uploaded and queued for processing",
  "file_name": "research_paper.pdf",
  "domain": "medical",
  "processing_id": "uuid-here"
}

Query Documents

POST /query
Content-Type: application/json

{
  "query": "What are the treatment options for hypertension?",
  "domain": "medical",
  "mode": "mix",
  "conversation_id": "conv_123",
  "return_metadata": true
}

Response:

{
  "answer": "Hypertension treatment includes lifestyle modifications...",
  "sources": ["medical_guidelines.pdf"],
  "confidence_score": 0.92,
  "query_improved": true,
  "verification_performed": true,
  "conversation_id": "conv_123",
  "metadata": {
    "original_query": "What is HTN treatment?",
    "improved_query": "What are the treatment options for hypertension?",
    "verification_score": 8.5,
    "modification_attempts": 1
  }
}

Get Conversation History

GET /conversation/{conversation_id}

Clear Conversation

DELETE /conversation/{conversation_id}

Clear Domain Data

DELETE /clear/{domain}

Usage Examples

Using cURL

Upload a document:

curl -X POST "http://localhost:8000/upload" \
  -F "file=@medical_paper.pdf" \
  -F "domain=medical"

Query documents:

curl -X POST "http://localhost:8000/query" \
  -H "Content-Type: application/json" \
  -d '{
    "query": "What are the side effects of ACE inhibitors?",
    "domain": "medical",
    "mode": "mix",
    "return_metadata": true
  }'

Using Python

import requests

# Upload document
with open("medical_paper.pdf", "rb") as f:
    files = {"file": f}
    data = {"domain": "medical"}
    response = requests.post("http://localhost:8000/upload", files=files, data=data)
    print(response.json())

# Query documents
query_data = {
    "query": "What are the treatment options for hypertension?",
    "domain": "medical",
    "mode": "mix",
    "return_metadata": True
}
response = requests.post("http://localhost:8000/query", json=query_data)
print(response.json())

Using JavaScript/TypeScript

// Upload document
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('domain', 'medical');

const uploadResponse = await fetch('http://localhost:8000/upload', {
  method: 'POST',
  body: formData
});

// Query documents
const queryResponse = await fetch('http://localhost:8000/query', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    query: 'What are the treatment options for hypertension?',
    domain: 'medical',
    mode: 'mix',
    return_metadata: true
  })
});

const result = await queryResponse.json();
console.log(result);

Configuration

Domain-Specific Settings

Each domain has customized settings in DOMAIN_CONFIGS:

{
    "medical": {
        "enable_query_improvement": True,
        "query_improvement_method": "hybrid",
        "expand_abbreviations": True,
        "verification_threshold": 7.5,
        # ... more settings
    }
}

Gemini Model Configuration

Currently using gemini-1.5-flash (free tier). To use a different model:

GEMINI_MODEL = "gemini-1.5-pro"  # More capable, paid tier

Architecture

backend/
├── main.py              # FastAPI application
├── requirements.txt     # Python dependencies
└── README.md           # This file

storage/                 # Created at runtime
├── medical/            # Medical domain storage
├── legal/              # Legal domain storage
├── financial/          # Financial domain storage
├── technical/          # Technical domain storage
└── academic/           # Academic domain storage

uploads/                # Uploaded files
├── medical/
├── legal/
└── ...

API Documentation

Interactive API documentation is available at:

Error Handling

The API uses standard HTTP status codes:

  • 200: Success
  • 400: Bad Request (invalid parameters)
  • 404: Not Found
  • 500: Internal Server Error

All errors return JSON:

{
  "detail": "Error message here"
}

Logging

Logs are output to console with the format:

2025-01-04 10:00:00 - main - INFO - Message here

Production Deployment

For production deployment:

  1. Set proper CORS origins in main.py:
allow_origins=["https://your-frontend-domain.com"]
  1. Use a production ASGI server:
gunicorn main:app -w 4 -k uvicorn.workers.UvicornWorker
  1. Set up environment variables securely (don't commit .env files)

  2. Enable HTTPS using a reverse proxy (nginx, Caddy, etc.)

  3. Set up proper logging (file-based, log rotation)

  4. Monitor with tools like Prometheus, Grafana

Troubleshooting

"GEMINI_API_KEY not set"

Set your API key as an environment variable or in a .env file.

"Failed to initialize RAG system"

Check that the storage directories are writable and all dependencies are installed.

"File type not supported"

Verify the file extension is in the allowed list for the target domain.

License

[Your License Here]

Support

For issues and questions, please open an issue on GitHub.