Spaces:
Sleeping
Sleeping
Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -5,36 +5,134 @@ app_port: 7860
|
|
| 5 |
pinned: false
|
| 6 |
---
|
| 7 |
|
| 8 |
-
#
|
| 9 |
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
- GET /ingest/jobs/{job_id}/events
|
| 16 |
-
- POST /query
|
| 17 |
-
- POST /query/pipeline
|
| 18 |
-
- GET /collections
|
| 19 |
-
- GET /collections/{collection_name}/viz
|
| 20 |
-
- POST /collections/{collection_name}/query_similarity
|
| 21 |
-
- GET /documents/{collection_name}/raw
|
| 22 |
-
- POST /evaluate
|
| 23 |
|
| 24 |
-
##
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
## Environment Variables
|
| 30 |
|
| 31 |
Required:
|
| 32 |
-
|
| 33 |
-
-
|
| 34 |
-
-
|
| 35 |
|
| 36 |
Optional:
|
| 37 |
-
|
| 38 |
-
-
|
| 39 |
-
-
|
| 40 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
pinned: false
|
| 6 |
---
|
| 7 |
|
| 8 |
+
# SeeRAG Backend
|
| 9 |
|
| 10 |
+

|
| 11 |
|
| 12 |
+
Production RAG backend for [SeeRAG](https://seerag.vercel.app) (Live Link)
|
| 13 |
+
- Frontend Repo: <https://github.com/Sambhaji-Patil/seerag-frontend>
|
| 14 |
|
| 15 |
+
This service handles all AI/ML work: document ingestion, chunking, embedding, retrieval, reranking, generation, safety checks, caching, and RAG routing.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
## What This Backend Does
|
| 18 |
|
| 19 |
+
- Ingests PDFs, TXT, and Markdown files.
|
| 20 |
+
- Chunks documents and stores them in FAISS collections.
|
| 21 |
+
- Supports multiple retrieval strategies: vector, BM25, hybrid RRF, and MMR.
|
| 22 |
+
- Uses OpenAI or local BGE embeddings depending on the selected mode.
|
| 23 |
+
- Builds answers with an LLM and streams the pipeline step-by-step.
|
| 24 |
+
- Applies guardrails with Llama Guard plus regex fallback.
|
| 25 |
+
- Caches exact and semantic answers in Redis.
|
| 26 |
+
- Generates live similarity data for the frontend visualizations.
|
| 27 |
+
|
| 28 |
+
## Architecture
|
| 29 |
+
|
| 30 |
+

|
| 31 |
+
|
| 32 |
+
### Request Flow
|
| 33 |
+
|
| 34 |
+
1. The request enters the FastAPI app.
|
| 35 |
+
2. Guardrails block unsafe input before retrieval starts.
|
| 36 |
+
3. Exact cache is checked first.
|
| 37 |
+
4. Semantic cache is checked next using the query embedding.
|
| 38 |
+
5. The query is rewritten for retrieval when needed.
|
| 39 |
+
6. The router can skip retrieval if the chat history already answers the question.
|
| 40 |
+
7. Retrieval runs with the selected strategy and weights.
|
| 41 |
+
8. The prompt is built from the retrieved context.
|
| 42 |
+
9. The answer is generated and cached again for future reuse.
|
| 43 |
+
|
| 44 |
+
## Retrieval And Ranking
|
| 45 |
+
|
| 46 |
+
The backend supports a few retrieval styles so you can compare quality and behavior:
|
| 47 |
+
|
| 48 |
+
- Vector: dense semantic similarity only.
|
| 49 |
+
- BM25: keyword-driven sparse search.
|
| 50 |
+
- Hybrid: BM25 + vector fusion using RRF.
|
| 51 |
+
- MMR: diversity-aware retrieval for less repetitive context.
|
| 52 |
+
|
| 53 |
+
Retrieval settings are configurable per request:
|
| 54 |
+
|
| 55 |
+
- `top_k`: final chunks shown to the LLM.
|
| 56 |
+
- `top_k_retrieval`: initial candidate pool.
|
| 57 |
+
- `mmr_lambda`: relevance vs diversity balance for MMR.
|
| 58 |
+
- `bm25_weight` and `vector_weight`: hybrid weighting.
|
| 59 |
+
|
| 60 |
+
## Embeddings
|
| 61 |
+
|
| 62 |
+
Embedding mode is selected per request and can use:
|
| 63 |
+
|
| 64 |
+
- `bge-large`
|
| 65 |
+
- `bge-small`
|
| 66 |
+
- `openai-small`
|
| 67 |
+
- `auto`
|
| 68 |
+
|
| 69 |
+
The backend chooses the right embedding runtime for the collection and normalizes vectors for cosine-based semantic search.
|
| 70 |
+
|
| 71 |
+
## Caching
|
| 72 |
+
|
| 73 |
+
Two cache layers are used:
|
| 74 |
+
|
| 75 |
+
- Exact cache: stores the full answer for an identical query, collection, and retrieval settings.
|
| 76 |
+
- Semantic cache: stores query embeddings in Redis and reuses an answer when a new query is close enough.
|
| 77 |
+
|
| 78 |
+
Cache behavior is keyed by retrieval mode and tuning parameters, so a hybrid answer does not collide with a vector-only or MMR answer.
|
| 79 |
+
|
| 80 |
+
## Safety
|
| 81 |
+
|
| 82 |
+
Incoming queries go through a guardrail layer before generation.
|
| 83 |
+
|
| 84 |
+
- Llama Guard is used when available.
|
| 85 |
+
- Regex fallback keeps the app working if the model cannot load.
|
| 86 |
+
- The backend also checks context for sensitive content before sending it to the LLM.
|
| 87 |
+
|
| 88 |
+
## API Endpoints
|
| 89 |
+
|
| 90 |
+
Core endpoints used by the frontend:
|
| 91 |
+
|
| 92 |
+
- `POST /ingest/file`
|
| 93 |
+
- `GET /ingest/jobs/{job_id}/events`
|
| 94 |
+
- `POST /query`
|
| 95 |
+
- `POST /query/pipeline`
|
| 96 |
+
- `GET /collections`
|
| 97 |
+
- `GET /collections/{collection_name}/viz`
|
| 98 |
+
- `POST /collections/{collection_name}/query_similarity`
|
| 99 |
+
- `POST /evaluate`
|
| 100 |
+
|
| 101 |
+
## Runtime And Deployment
|
| 102 |
+
|
| 103 |
+
This project uses the Docker runtime on Hugging Face Spaces.
|
| 104 |
+
|
| 105 |
+
- Backend repo: <https://github.com/Sambhaji-Patil/SeeRag-Backend>
|
| 106 |
+
- Frontend repo: <https://github.com/Sambhaji-Patil/seerag-frontend>
|
| 107 |
+
- Frontend app: <https://seerag.vercel.app>
|
| 108 |
|
| 109 |
## Environment Variables
|
| 110 |
|
| 111 |
Required:
|
| 112 |
+
|
| 113 |
+
- `OPENAI_API_KEY`
|
| 114 |
+
- `API_BEARER_TOKEN`
|
| 115 |
|
| 116 |
Optional:
|
| 117 |
+
|
| 118 |
+
- `EMBEDDING_DEVICE` default: `cuda`
|
| 119 |
+
- `CACHE_ENABLED` default: `false`
|
| 120 |
+
- `REDIS_URL` default: `redis://localhost:6379`
|
| 121 |
+
- `HF_TOKEN` required only when downloading gated Llama Guard weights
|
| 122 |
+
|
| 123 |
+
## Notes For Production
|
| 124 |
+
|
| 125 |
+
- CORS is restricted to the deployed frontend origin.
|
| 126 |
+
- The frontend must send the same bearer token in `Authorization: Bearer ...`.
|
| 127 |
+
- Restart the backend after changing secrets or environment variables.
|
| 128 |
+
|
| 129 |
+
## Local Development
|
| 130 |
+
|
| 131 |
+
The FastAPI app is started through the Docker container and listens on port `7860` in Spaces.
|
| 132 |
+
|
| 133 |
+
If you run locally, make sure the following are available:
|
| 134 |
+
|
| 135 |
+
- Python dependencies from `requirements.txt`
|
| 136 |
+
- A valid `OPENAI_API_KEY`
|
| 137 |
+
- Redis if you want caching enabled
|
| 138 |
+
- FAISS index data under `faiss_indexes/`
|