Spaces:
Sleeping
RAG Agent Workbench – Backend
Lightweight FastAPI backend for ingesting documents into Pinecone (with integrated embeddings), searching over them, and serving a production-style RAG chat endpoint.
Prerequisites
- Python 3.11+
- A Pinecone account and an index configured with integrated embeddings
- A Groq account and API key for chat
- (Optional) Tavily API key for web search fallback
- (Optional) LangSmith account + API key for tracing
- Environment variables set (see
.env.example)
Setup
cd backend
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env # then edit with your Pinecone, Groq, and optional Tavily/LangSmith credentials
Required .env values:
PINECONE_API_KEY– your Pinecone API keyPINECONE_INDEX_NAME– the index name (used for configuration checks)PINECONE_HOST– the index host URL (use host targeting for production)PINECONE_NAMESPACE– default namespace (e.g.dev)PINECONE_TEXT_FIELD– text field name used by the integrated embedding index (e.g.chunk_textorcontent)LOG_LEVEL– e.g.INFO,DEBUG
Required for /chat:
GROQ_API_KEY– your Groq API keyGROQ_BASE_URL– Groq OpenAI-compatible endpoint (defaulthttps://api.groq.com/openai/v1)GROQ_MODEL– Groq chat model name (defaultllama-3.1-8b-instant)
Optional for web search fallback:
TAVILY_API_KEY– Tavily API key (enables web search in/chatwhen retrieval is weak)
Optional for LangSmith tracing:
LANGCHAIN_TRACING_V2– set totrueto enable tracingLANGCHAIN_API_KEY– your LangSmith API keyLANGCHAIN_PROJECT– project name for traces (e.g.rag-agent-workbench)
Optional for basic API protection:
API_KEY– when set, all routers except/healthare protected byX-API-Key(including/chat,/search,/documents/*,/ingest/*,/metrics, and the OpenAPI/Swagger docs).- In production-like environments (
ENV=productionor on Hugging Face Spaces),API_KEYmust be set or the backend will fail to start. - In local development (no Spaces and
ENVnot set toproduction),API_KEYis optional; when omitted, the API (including docs) is open.
- In production-like environments (
Optional for CORS:
ALLOWED_ORIGINS– comma-separated list of allowed origins.- If unset, defaults to
"*"(useful for local dev and quick demos).
- If unset, defaults to
Optional for rate limiting and caching:
RATE_LIMIT_ENABLED– defaults totrue. Set tofalseto disable SlowAPI limits.CACHE_ENABLED– defaults totrue. Set tofalseto disable in-memory TTL caches.
Your Pinecone index must be configured for integrated embeddings (e.g. via create_index_for_model or configure_index(embed=...)), with a field mapping that includes the configured PINECONE_TEXT_FIELD.
Run locally
cd backend
uvicorn app.main:app --reload --port 8000
The API will be available at http://localhost:8000.
Sample endpoints
Health
curl http://localhost:8000/health
Ingest from arXiv
curl -X POST "http://localhost:8000/ingest/arxiv" \
-H "Content-Type: application/json" \
-d '{
"query": "retrieval augmented generation",
"max_docs": 5,
"namespace": "dev",
"category": "papers"
}'
Ingest from OpenAlex
curl -X POST "http://localhost:8000/ingest/openalex" \
-H "Content-Type: application/json" \
-d '{
"query": "retrieval augmented generation",
"max_docs": 5,
"namespace": "dev",
"mailto": "you@example.com"
}'
Ingest from Wikipedia
curl -X POST "http://localhost:8000/ingest/wiki" \
-H "Content-Type: application/json" \
-d '{
"titles": ["Retrieval-augmented generation", "Vector database"],
"namespace": "dev"
}'
Manual text upload
curl -X POST "http://localhost:8000/documents/upload-text" \
-H "Content-Type: application/json" \
-d '{
"title": "My manual note",
"source": "manual",
"text": "This is some example text describing RAG pipelines...",
"namespace": "dev",
"metadata": {
"url": "https://example.com/my-note"
}
}'
Search
curl -X POST "http://localhost:8000/search" \
-H "Content-Type: application/json" \
-H "X-API-Key: $API_KEY" \ # only if API_KEY is enabled
-d '{
"query": "what is RAG",
"top_k": 5,
"namespace": "dev",
"filters": {"source": "arxiv"}
}'
Document stats
curl "http://localhost:8000/documents/stats?namespace=dev"
Chat (non-streaming)
curl -X POST "http://localhost:8000/chat" \
-H "Content-Type: application/json" \
-H "X-API-Key: $API_KEY" \ # only if API_KEY is enabled
-d '{
"query": "What is retrieval-augmented generation?",
"namespace": "dev",
"top_k": 5,
"use_web_fallback": true,
"min_score": 0.25,
"max_web_results": 5,
"chat_history": [
{"role": "user", "content": "You are helping me understand RAG."}
]
}'
Example JSON response:
{
"answer": "...",
"sources": [
{
"source": "wiki",
"title": "Retrieval-augmented generation",
"url": "https://en.wikipedia.org/wiki/...",
"score": 0.91,
"chunk_text": "..."
}
],
"timings": {
"retrieve_ms": 35.2,
"web_ms": 0.0,
"generate_ms": 420.7,
"total_ms": 470.1
},
"trace": {
"langsmith_project": "rag-agent-workbench",
"trace_enabled": true
}
}
Chat (SSE streaming)
curl -N -X POST "http://localhost:8000/chat/stream" \
-H "Content-Type: application/json" \
-H "X-API-Key: $API_KEY" \ # only if API_KEY is enabled
-d '{
"query": "Summarise retrieval-augmented generation.",
"namespace": "dev",
"top_k": 5,
"use_web_fallback": true
}'
- The response will be
text/event-stream. - Individual SSE events stream tokens (space-delimited).
- The final event (
event: end) includes the full JSON payload as in/chat.
Metrics
curl "http://localhost:8000/metrics"
Returns JSON with:
requests_by_pathanderrors_by_pathtimings(average and p50/p95 forretrieve_ms,web_ms,generate_ms,total_ms)cachestats- Last 20 timing samples for chat.
Seeding data
A helper script is provided to seed the index with multiple arXiv and OpenAlex queries:
python ../scripts/seed_ingest.py --base-url http://localhost:8000 --namespace dev --mailto you@example.com
Docling integration (external scripts)
Docling is used via separate scripts so the backend container stays small and does not depend on Docling. To convert local documents and upload them as text:
Single file
cd scripts
pip install docling # optional but recommended for rich formats
python docling_convert_and_upload.py \
--file /path/to/file.pdf \
--backend-url http://localhost:8000 \
--namespace dev \
--title "My local document" \
--source local-file \
--api-key "$API_KEY"
- Supported formats when Docling is installed include: PDF, DOCX, PPT/PPTX, XLS/XLSX, HTML/HTM, MD, AsciiDoc, and TXT.
- If Docling is not installed:
.txtand.mdfiles are ingested as raw text.- Other formats will fail with a clear message instructing you to install Docling.
Batch ingest a folder
cd scripts
pip install docling # optional but recommended
python batch_ingest_local_folder.py \
--folder /path/to/folder \
--backend-url http://localhost:8000 \
--namespace dev \
--source local-folder \
--max-files 200 \
--api-key "$API_KEY"
- Recursively scans the folder for supported extensions and ingests up to
max-filesdocuments. - Each file is converted via
docling_convert_and_upload.pylogic and uploaded to/documents/upload-text.
Upload documents via UI (Streamlit dialog)
The Streamlit chat frontend also supports uploading documents directly from the browser:
- Click the “📄 Upload Document” button at the top of the chat page.
- A modal dialog opens with:
- File chooser (
.pdf,.md,.txt,.docx,.pptx,.xlsx,.html,.htm). - Title (defaults to filename without extension).
- Namespace (defaults to the backend namespace, e.g.
dev). - Source label (defaults to
ui-upload). - Optional metadata: tags (comma-separated) and free-form notes.
- File chooser (
- On upload:
- The frontend converts the file to markdown/text and calls
POST /documents/upload-textwith:title,source,text,namespace, and ametadatadictionary containing conversion and UI metadata.
- On success, the upload is recorded in a “Recent uploads” section in the sidebar and can be quickly queried via “Search this document”.
- The frontend converts the file to markdown/text and calls
Notes:
- Conversion happens entirely in the frontend:
.txtand.mdfiles are read as raw text.- For richer formats (PDF/Office/HTML), the frontend attempts to use Docling if installed.
- If Docling is not available, an informative error is shown and the user is asked to upload
.md/.txtinstead.
- On Streamlit Cloud, Docling must be added to the app’s Python environment (e.g.
requirements.txt) for PDF/Office uploads to work. - Streamlit’s file uploader has a default maximum size (typically 200 MB); check Streamlit documentation if you need to increase or restrict this limit.
Deploy Backend on Hugging Face Spaces (Docker)
Create a new Space
- Go to Hugging Face → New Space.
- Choose:
- SDK: Docker
- Space name: e.g.
your-name/rag-agent-workbench-backend.
- Point the Space to this repository and configure it to use the
backend/subdirectory (or copybackend/Dockerfileto the root if you prefer).
Environment variables / secrets
In the Space settings, configure the following (as “Secrets” where appropriate):
Required:
PINECONE_API_KEYPINECONE_HOSTPINECONE_INDEX_NAMEPINECONE_NAMESPACEPINECONE_TEXT_FIELD=content(or your actual text field)GROQ_API_KEYGROQ_BASE_URL(optional, defaults tohttps://api.groq.com/openai/v1)GROQ_MODEL(optional, defaults tollama-3.1-8b-instant)
Optional:
TAVILY_API_KEY(web search fallback for/chat)LANGCHAIN_TRACING_V2LANGCHAIN_API_KEYLANGCHAIN_PROJECTAPI_KEY(to protect/ingest/*,/documents/*,/search,/chat*)ALLOWED_ORIGINS(e.g. your Streamlit frontend origin)RATE_LIMIT_ENABLEDandCACHE_ENABLED(rarely need to change from defaults)
Ports and startup
- The Docker image exposes port 7860 by default.
- Hugging Face Spaces sets the
PORTenvironment variable; theCMDhonours it:uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-7860}
- On successful startup, logs include:
Starting on port=<port> hf_spaces_mode=<bool>
Verify
- Open your Space URL:
https://<your-space>.hf.space/docs– interactive API docs.https://<your-space>.hf.space/health– health check.
- If
API_KEYis set, test protected endpoints usingX-API-Key.
- Open your Space URL:
Deploy Frontend on Streamlit Community Cloud
Prepare the repo
- The minimal Streamlit frontend lives under
frontend/app.py. - Root
requirements.txtincludes:streamlithttpx
- The minimal Streamlit frontend lives under
Create Streamlit app
- Go to Streamlit Community Cloud and create a new app.
- Point it at this repository.
- Set the main file to
frontend/app.py.
Configure Streamlit secrets
In the Streamlit app settings, configure Secrets (YAML):
BACKEND_BASE_URL: "https://<your-backend-space>.hf.space" API_KEY: "your-backend-api-key" # only if backend API_KEY is setDo not commit secrets into the repo.
Verify connectivity
- Open the Streamlit app.
- In the sidebar “Connectivity” panel:
- Confirm the backend URL is correct.
- Click “Ping /health” to verify backend connectivity.
- Use the chat panel to send a question:
- The app will call
/chaton the backend and display answer, timings, and sources.
- The app will call
Local Test Checklist – Work Package C
Configure environment
- Set
PINECONE_*variables for an integrated embeddings index. - Set
GROQ_API_KEY(and optionally overrideGROQ_BASE_URL,GROQ_MODEL). - Optionally set
TAVILY_API_KEYfor web fallback. - Optionally enable LangSmith:
LANGCHAIN_TRACING_V2=trueLANGCHAIN_API_KEY=...LANGCHAIN_PROJECT=rag-agent-workbench
- Optionally set:
API_KEYfor basic protection.ALLOWED_ORIGINSif you are calling from a browser origin.RATE_LIMIT_ENABLED/CACHE_ENABLEDfor tuning.
- Set
Start the backend
cd backend uvicorn app.main:app --reload --port 8000Ingest data
Quick Wikipedia smoke test (also see
scripts/smoke_chat.py):python ../scripts/smoke_chat.py --backend-url http://localhost:8000 --namespace dev
Test
/searchcurl -X POST "http://localhost:8000/search" \ -H "Content-Type: application/json" \ -H "X-API-Key: $API_KEY" \ # only if API_KEY is enabled -d '{"query": "what is RAG", "namespace": "dev", "top_k": 5}'Test
/chatUse the curl example above or run:
curl -X POST "http://localhost:8000/chat" \ -H "Content-Type: application/json" \ -H "X-API-Key: $API_KEY" \ # only if API_KEY is enabled -d '{"query": "What is retrieval-augmented generation?", "namespace": "dev"}'
Test
/chatwith web fallbackRequires
TAVILY_API_KEY:python ../scripts/smoke_chat_web.py --backend-url http://localhost:8000 --namespace dev
Inspect
/metricscurl "http://localhost:8000/metrics"- Confirm:
- Request counts are increasing.
- Timing stats (
average_ms,p50_ms,p95_ms) are populated after several/chatcalls. - Cache hit/miss counters change when repeating identical
/searchor/chatrequests.
- Confirm:
Run the benchmark script
From the repo root:
python scripts/bench_local.py \ --backend-url http://localhost:8000 \ --namespace dev \ --concurrency 10 \ --requests 50 \ --api-key "$API_KEY"Review reported:
- Average latency.
- p50 / p95 latency.
- Error rate.
Optional: Test Streamlit frontend locally
Install root requirements:
pip install -r requirements.txtRun:
streamlit run frontend/app.pyConfigure
BACKEND_BASE_URLandAPI_KEYvia environment or.streamlit/secrets.toml, and verify chat works end-to-end.