A FastAPI service for managing context retrieval and trace storage using Gemini File Search and Supabase.
- FastAPI backend deployed on Railway
- Supabase: users, API keys, trace metadata
- Gemini File Search: trace storage (global store with user_id namespacing)
- Flow: SDK → API (retrieve context) → SDK calls LLM → SDK → API (store trace)
pip install -r requirements.txt- Create a Supabase project
- Run the SQL schema from
supabase_schema.sqlin the Supabase SQL editor - Get your Supabase URL and anon key
- Get a Gemini API key from Google AI Studio
- Enable File Search API access
Create a .env file:
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_KEY=your-supabase-anon-key
GEMINI_API_KEY=your-gemini-api-keyuvicorn src.api.main:app --reloadThe API will be available at http://localhost:8000
POST /api/v1/auth/create-key
- Creates a new API key for a user
- Request:
{"email": "user@example.com"} - Response:
{"api_key": "ctx_...", "user_id": "..."}
POST /api/v1/context/retrieve
- Retrieves relevant context for a prompt
- Headers:
X-API-Key: your-api-key - Request:
{"prompt": "...", "system_prompt": "...", "provider": "openai", "model": "gpt-4"} - Response:
{"enhanced_context": "...", "relevant_traces": [...], "suggestions": {...}}
POST /api/v1/traces/store
- Stores a trace after an LLM call
- Headers:
X-API-Key: your-api-key - Request:
{"input": {...}, "output": {...}, "metadata": {...}} - Response:
{"trace_id": "...", "stored": true}
- Push code to GitHub
- Connect Railway to your GitHub repo
- Add environment variables in Railway dashboard:
SUPABASE_URLSUPABASE_KEYGEMINI_API_KEY
- Deploy!
The Procfile and railway.toml are already configured.
Run tests with:
pytest src/tests/import requests
# Create API key
response = requests.post("https://your-api.railway.app/api/v1/auth/create-key",
json={"email": "user@example.com"})
api_key = response.json()["api_key"]
# Retrieve context
response = requests.post(
"https://your-api.railway.app/api/v1/context/retrieve",
headers={"X-API-Key": api_key},
json={
"prompt": "Write a story",
"provider": "openai",
"model": "gpt-4"
}
)
context = response.json()
# Store trace after LLM call
response = requests.post(
"https://your-api.railway.app/api/v1/traces/store",
headers={"X-API-Key": api_key},
json={
"input": {"prompt": "Write a story", "parameters": {}},
"output": {"text": "Once upon a time...", "tokens_used": 100},
"metadata": {"provider": "openai", "model": "gpt-4", "success": True}
}
)