An open API service indexing awesome lists of open source software.

https://github.com/zakariaf/rag-cache


https://github.com/zakariaf/rag-cache

Last synced: 5 months ago
JSON representation

Awesome Lists containing this project

README

          

# RAG Cache

A high-performance caching layer for Large Language Model (LLM) queries with exact and semantic matching capabilities.

## ๐ŸŽฏ What is RAG Cache?

RAG Cache sits between your application and LLM providers (OpenAI, Anthropic) to:

- **๐Ÿ’ฐ Save Money** - Avoid paying for duplicate queries
- **โšก Reduce Latency** - Return cached responses in ~2ms instead of ~10 seconds
- **๐Ÿ›ก๏ธ Increase Reliability** - Reduce dependency on external APIs

### How It Works

```
User Query โ†’ Exact Match (Redis) โ†’ Semantic Match (Qdrant) โ†’ LLM (if needed)
โ†“ โ†“ โ†“
~1ms hit ~450ms hit ~8,500ms
```

## ๐Ÿš€ Quick Start

### Prerequisites

- Python 3.11+
- Docker & Docker Compose
- OpenAI API key

### Installation

```bash
# Clone the repository
git clone
cd "Rag cache"

# Create virtual environment
python3.11 -m venv venv
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

# Configure environment
cp .env.example .env
# Edit .env and add your OPENAI_API_KEY

# Start Redis & Qdrant
docker-compose up -d redis qdrant

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

### Verify Installation

```bash
# Health check
curl http://localhost:8000/health
```

Expected response:
```json
{
"status": "healthy",
"environment": "development",
"version": "0.1.0"
}
```

## ๐Ÿ“ก API Usage

### Query Endpoint

```bash
curl -X POST http://localhost:8000/api/v1/query \
-H "Content-Type: application/json" \
-d '{
"query": "What is machine learning?",
"use_cache": true
}'
```

**First Request (Cache Miss):**
```json
{
"response": "Machine learning is a subset of AI...",
"provider": "openai",
"model": "gpt-4o-mini-2024-07-18",
"usage": {
"prompt_tokens": 12,
"completion_tokens": 418,
"total_tokens": 430
},
"cache_info": {
"cache_hit": false,
"cache_type": null
},
"latency_ms": 10090.93
}
```

**Second Request (Exact Cache Hit - Redis):**
```json
{
"response": "Machine learning is a subset of AI...",
"cache_info": {
"cache_hit": true,
"cache_type": "exact"
},
"latency_ms": 1.11
}
```

**Similar Query (Semantic Cache Hit - Qdrant):**
```bash
curl -X POST http://localhost:8000/api/v1/query \
-H "Content-Type: application/json" \
-d '{"query": "Can you explain machine learning?", "use_cache": true}'
```
```json
{
"response": "Machine learning is a subset of AI...",
"cache_info": {
"cache_hit": true,
"cache_type": "semantic",
"similarity_score": 0.8563
},
"latency_ms": 457.89
}
```

### Other Endpoints

| Endpoint | Method | Description |
|----------|--------|-------------|
| `/health` | GET | Health check |
| `/docs` | GET | Swagger UI documentation |
| `/api/v1/query` | POST | Process query with caching |
| `/api/v1/metrics` | GET | Application metrics |
| `/api/v1/metrics/prometheus` | GET | Prometheus format metrics |

## ๐Ÿ“Š Real Performance Tests

Tests performed on November 27, 2025:

### Cache Hit Performance

| Test | Cache Type | Latency | Speedup |
|------|------------|---------|---------|
| First Query (Cache Miss) | LLM Call | 8,573ms | baseline |
| Same Query (Exact Match) | **Redis** | 1.11ms | **7,723x faster** |
| Similar Query (Semantic Match) | **Qdrant** | 457ms | **18.7x faster** |
| Different Query (Cache Miss) | LLM Call | 8,154ms | - |

### Semantic Cache Example

```bash
# Original cached query
"What is deep learning?"

# Similar query (semantic match)
"Can you explain deep learning to me?"
# โ†’ Similarity Score: 0.856
# โ†’ Latency: 457ms (instead of ~8,500ms)
# โ†’ Saved: 811 tokens
```

### Cache Types Explained

| Cache | Storage | Latency | Use Case |
|-------|---------|---------|----------|
| **Exact** | Redis | ~1ms | Identical queries |
| **Semantic** | Qdrant | ~450ms | Similar questions |
| **Miss** | LLM API | ~8,500ms | New unique queries |

## ๐Ÿ“ˆ Metrics

```bash
curl http://localhost:8000/api/v1/metrics
```

```json
{
"application": {
"name": "RAGCache",
"environment": "development",
"version": "0.1.0"
},
"pipeline": {
"total_requests": 0,
"cache_hits": 0,
"cache_hit_rate": 0.0,
"avg_latency_ms": 0.0
},
"cache": {
"total_keys": 286,
"memory_used_bytes": 1585248,
"hits": 2,
"misses": 2032,
"hit_rate": 0.1
},
"config": {
"semantic_cache_enabled": true,
"exact_cache_enabled": true,
"similarity_threshold": 0.85,
"cache_ttl_seconds": 3600
}
}
```

### Prometheus Metrics

```bash
curl http://localhost:8000/api/v1/metrics/prometheus
```

```
# HELP ragcache_info Application information
# TYPE ragcache_info gauge
ragcache_info{version="0.1.0",environment="development"} 1

# HELP ragcache_requests_total Total requests processed
# TYPE ragcache_requests_total counter
ragcache_requests_total 0

# HELP ragcache_cache_hits_total Total cache hits
# TYPE ragcache_cache_hits_total counter
ragcache_cache_hits_total 0

# HELP ragcache_redis_keys Total Redis keys
# TYPE ragcache_redis_keys gauge
ragcache_redis_keys 283
```

## ๐Ÿ—๏ธ Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Your Application โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
โ”‚ HTTP
โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ RAG Cache (FastAPI) โ”‚
โ”‚ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚ Query Service โ”‚ โ”‚
โ”‚ โ”‚ 1. Check Redis (exact match) โ”‚ โ”‚
โ”‚ โ”‚ 2. Check Qdrant (semantic match) โ”‚ โ”‚
โ”‚ โ”‚ 3. Call LLM if no cache hit โ”‚ โ”‚
โ”‚ โ”‚ 4. Store response in cache โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ”‚ โ”‚
โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚
โ”‚ โ”‚ Redis โ”‚ โ”‚ Qdrant โ”‚ โ”‚ LLM Providers โ”‚ โ”‚
โ”‚ โ”‚ (Exact) โ”‚ โ”‚ (Semantic) โ”‚ โ”‚ OpenAI / Anthropic โ”‚ โ”‚
โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

## โš™๏ธ Configuration

### Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `OPENAI_API_KEY` | - | OpenAI API key (required) |
| `ANTHROPIC_API_KEY` | - | Anthropic API key (optional) |
| `REDIS_HOST` | `localhost` | Redis host |
| `QDRANT_HOST` | `localhost` | Qdrant host |
| `SEMANTIC_SIMILARITY_THRESHOLD` | `0.85` | Similarity threshold (0.0-1.0) |
| `CACHE_TTL_SECONDS` | `3600` | Cache TTL in seconds |
| `DEFAULT_MODEL` | `gpt-3.5-turbo` | Default LLM model |

## ๐Ÿ“ Project Structure

```
app/
โ”œโ”€โ”€ api/ # FastAPI routes & middleware
โ”‚ โ”œโ”€โ”€ routes/ # API endpoints
โ”‚ โ””โ”€โ”€ middleware/ # Auth, rate limiting, logging
โ”œโ”€โ”€ cache/ # Redis & Qdrant clients
โ”œโ”€โ”€ llm/ # LLM provider integrations
โ”‚ โ”œโ”€โ”€ openai_provider.py
โ”‚ โ””โ”€โ”€ anthropic_provider.py
โ”œโ”€โ”€ embeddings/ # Text embedding generation
โ”œโ”€โ”€ pipeline/ # Query processing pipeline
โ”œโ”€โ”€ monitoring/ # Prometheus metrics & alerts
โ”œโ”€โ”€ optimization/ # Performance optimization
โ”œโ”€โ”€ models/ # Pydantic data models
โ”œโ”€โ”€ services/ # Business logic
โ””โ”€โ”€ utils/ # Utilities (logger, hasher)
```

## ๐Ÿงช Running Tests

```bash
# Run all tests
pytest

# Run with coverage
pytest --cov=app

# Run specific test file
pytest tests/unit/test_config.py -v
```

## ๐Ÿณ Docker

### Start Services

```bash
# Start all services
docker-compose up -d

# Start only Redis and Qdrant
docker-compose up -d redis qdrant

# View logs
docker-compose logs -f

# Stop services
docker-compose down
```

### Production Deployment

```bash
# Use production compose file
docker-compose -f docker-compose.prod.yml up -d

# Or use deployment script
./scripts/deploy.sh production
```

## ๐Ÿ’ฐ Cost Savings Example

| Scenario | Without Cache | With Cache (50% hit rate) |
|----------|---------------|---------------------------|
| 10,000 queries/day | ~$50/day | ~$25/day |
| Average latency | ~10,000ms | ~5,000ms |
| P95 latency | ~15,000ms | ~2ms (cache hit) |

## ๐Ÿ“š Documentation

- [API Documentation](docs/API.md)
- [Architecture](docs/ARCHITECTURE.md)
- [Deployment Guide](docs/DEPLOYMENT.md)
- [Configuration Guide](docs/CONFIGURATION.md)
- [Testing Guide](docs/TESTING.md)
- [Troubleshooting](docs/TROUBLESHOOTING.md)
- [Security Best Practices](docs/SECURITY.md)
- [Performance Tuning](docs/PERFORMANCE.md)

## ๐Ÿ› ๏ธ Tech Stack

| Component | Technology |
|-----------|------------|
| Language | Python 3.11 |
| Framework | FastAPI |
| Exact Cache | Redis 7.2 |
| Semantic Cache | Qdrant 1.6 |
| LLM Providers | OpenAI, Anthropic |
| Embeddings | sentence-transformers |
| Testing | pytest |
| Containerization | Docker |

## ๐Ÿค Contributing

See [CONTRIBUTING.md](docs/CONTRIBUTING.md) for guidelines.

## ๐Ÿ“„ License

MIT License - See [LICENSE](LICENSE) file.

---

**Built with โค๏ธ**