SOLUTION-LAB-007: Production RAG¶
Reference solution for LAB-007: Production RAG System.
Overview¶
Complete solution for deploying a production-ready RAG system.
Core Components¶
from fastapi import FastAPI, BackgroundTasks
from qdrant_client import QdrantClient
import redis
class ProductionRAG:
"""Production RAG with caching, monitoring, and reliability."""
def __init__(self):
self.qdrant = QdrantClient(url="http://localhost:6333")
self.redis = redis.Redis(host="localhost", port=6379)
self.embedder = EmbeddingService()
self.llm = OllamaLLM()
async def query(self, question: str, use_cache: bool = True):
"""Query with caching."""
# Check cache
if use_cache:
cached = await self._get_cache(question)
if cached:
return cached
# Retrieve
docs = await self._retrieve(question)
# Generate
answer = await self._generate(question, docs)
# Cache result
if use_cache:
await self._set_cache(question, answer)
return {"question": question, "answer": answer, "sources": docs}
async def _retrieve(self, query: str):
"""Retrieve with hybrid search."""
# Vector search
vector_results = self._vector_search(query)
# Keyword search
keyword_results = self._keyword_search(query)
# Combine and re-rank
return self._rerank(vector_results, keyword_results)
Deployment¶
# Dockerfile
FROM python:3.13
WORKDIR /app
# Official uv-in-Docker pattern: copy the uv binary from the uv image
# (https://docs.astral.sh/uv/guides/integration/docker/)
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
# Dependency layer: only manifest/lockfile changes rebuild this.
COPY pyproject.toml uv.lock ./
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked --no-install-project
COPY . .
CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]
Difficulty: ⭐⭐⭐ Advanced