Backend service for RAG, document ingestion, and SEC EDGAR workflows. Built on FastAPI with LLM/embedding providers, PostgreSQL + pgvector (or Chroma), and API-key protection.
- RAG queries with source retrieval and query logging
- Document upload → chunking → embeddings → vector storage
- SEC EDGAR search, ingestion, and queued background processing
- LLM completions with optional streaming responses
- Optional evaluation framework for RAG quality checks
python3 -m venv venv
source venv/bin/activate # macOS/Linux
# or: venv\Scripts\activate # Windowspip install -r requirements.txtCreate a .env file:
# Core
APP_NAME=ai-rag-service
APP_VERSION=0.1.0
DEBUG=true
# Database (required when DEBUG=false)
DATABASE_URL=postgresql+psycopg2://user:pass@localhost:5432/ai_rag
# API access
API_KEY=change-me
API_KEY_HASH_PEPPER=optional-pepper
# LLMs
LLM_PROVIDER=anthropic # or "openai"
ANTHROPIC_API_KEY=your_anthropic_key
OPENAI_API_KEY=your_openai_key
# Vector store
VECTOR_STORE_PROVIDER=pgvector # or "chroma"
CHROMA_PERSIST_DIR=./chroma_db
# SEC EDGAR
SEC_USER_AGENT=ai-rag-service (contact: you@example.com)
SEC_RATE_LIMIT_PER_SEC=8
SEC_CACHE_DIR=./sec_cache
SEC_WORKER_POLL_SECONDS=3python scripts/init_db.pyuvicorn app.main:app --reload --host 0.0.0.0 --port 8000python scripts/sec_worker.pyVisit http://localhost:8000/docs for interactive API docs.
All endpoints (except / and /health) require X-API-Key unless DEBUG=true and API_KEY is unset.
POST /ask- LLM completionPOST /ask/stream- streaming completion (SSE)POST /documents/upload- upload and index a documentGET /documents/count- document/chunk countsGET /documents/list- list documentsDELETE /documents/{document_id}- delete a document and its chunksPOST /rag/query- RAG query with optional SEC filtersPOST /sec/search- search EDGAR filingsPOST /sec/ingest- ingest a filing immediatelyPOST /sec/ingest/queue- enqueue a filing for background ingestPOST /sec/ingest/queue/process-next- process one queued jobGET /sec/ingest/jobs- list ingest jobsGET /sec/ingest/jobs/{job_id}- get a jobGET /sec/filings- list indexed filingsPOST /sec/research- research question against filingsPOST /sec/compare- compare two filings
Example request:
curl -X POST "http://localhost:8000/rag/query" \
-H "Content-Type: application/json" \
-H "X-API-Key: $API_KEY" \
-d '{
"question": "Summarize the risk factors for Apple",
"form_type": "10-K",
"top_k": 5
}'The default vector store is pgvector. Ensure the Postgres extension is installed:
./install_pgvector.shTo use Chroma instead, set VECTOR_STORE_PROVIDER=chroma and CHROMA_PERSIST_DIR.
scripts/init_db.py- create all database tablesscripts/reset_db.py- drop and recreate tablesscripts/sec_worker.py- background worker for SEC ingestion queue