muddasser/Retail_stores_Queues
Text-to-SQL Trace RAG
A retrieval-augmented Text-to-SQL prototype for retail BI questions. Instead of asking an LLM to generate SQL cold, it first retrieves verified solution traces from similar past questions (via FAISS semantic search) and uses them to ground a structured trace, which is then translated into DuckDB SQL and executed.
Built as a single-file FastAPI app (app.py) serving both the JSON API and a plain HTML/JS frontend — no Gradio, no build step, no frontend framework.
Status: prototype. There is no SQL execution guardrail (any statement the model returns is run directly against DuckDB), no persistence (the trace corpus and vector index live in memory and reset on restart), and no auth. Do not point this at anything you care about losing.
Pipeline
Question → Retrieve similar verified traces (FAISS, cosine ≥ 0.55)
→ Generate structured trace (Groq / llama-3.3-70b-versatile)
→ Generate SQL from the trace
→ Execute against an in-memory DuckDB "sales" tableRetrieved traces below the similarity threshold are dropped rather than injected into the prompt — a weak match is treated as noise, not evidence, so the model falls back to zero-shot generation instead of being anchored to an unrelated example.
Running locally with Docker
docker build -t text2sql-trace-rag .
docker run -p 7860:7860 -e GROQ_API_KEY=your_key_here text2sql-trace-ragThen open http://localhost:7860.
Running locally without Docker
pip install -r requirements.txt
export GROQ_API_KEY=your_key_here
python app.pyConfiguration
Deploying to Hugging Face Spaces
- Create a new Space, SDK = Docker.
- Push
Dockerfile,requirements.txt,app.py, and thisREADME.md(the YAML block above is what Spaces reads to configure the build — don't remove it). - Add
GROQ_API_KEYunder Settings → Repository secrets. Never commit it to the repo or bake it into the Dockerfile. - Push. Spaces builds the image and routes traffic to port
7860as declared inapp_portabove.
Using the app
- Ask Question tab — type a retail BI question, generates and runs SQL against the sample
salestable. - Add Verified Trace tab — after manually checking a trace + SQL pair is correct, add it to the corpus so future similar questions retrieve it. This rebuilds the FAISS index in memory; it is not saved to disk, so it will not survive a container restart.
API
Interactive API docs are auto-generated by FastAPI at /docs.
Known limitations (read before extending)
- No validation that generated SQL is read-only — treat this as an internal/dev tool, not something to expose publicly as-is.
- Trace corpus and FAISS index are process-global and in-memory only; single-worker only (
uvicorndefault). Running with multiple workers will give each worker its own copy of the trace corpus and index, so added traces won't be visible across workers. - No check that generated SQL actually reflects the structured trace it was conditioned on.
- Sample
salesdata is hardcoded for demo purposes. groqis pinned to1.6.0specifically becausegroq<1.xbreaks againsthttpx>=0.28(Client.__init__() got an unexpected keyword argument 'proxies'— the old SDK's internal HTTP client wrapper still passes an argumenthttpxremoved). If you ever bumpgroqdown for any reason, re-check this.
