Spaces:
Running
Running
#10 RAG Q&A with Citations - construction KB (private validation build)
Browse files- .env.example +4 -0
- .gitignore +7 -0
- Dockerfile +11 -0
- README.md +45 -5
- app.py +358 -0
- requirements.txt +5 -0
.env.example
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Claude API key — required to generate the grounded, cited answers.
|
| 2 |
+
ANTHROPIC_API_KEY=sk-ant-...
|
| 3 |
+
ANTHROPIC_MODEL=claude-haiku-4-5
|
| 4 |
+
# Embeddings run locally via fastembed (BAAI/bge-small-en-v1.5, ONNX/CPU) — no key needed.
|
.gitignore
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
*.pyc
|
| 3 |
+
.env
|
| 4 |
+
.venv/
|
| 5 |
+
venv/
|
| 6 |
+
*.log
|
| 7 |
+
.fastembed_cache/
|
Dockerfile
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# RAG Q&A with Citations — runs on Hugging Face Spaces, Render, Railway, Fly.io, etc.
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
COPY requirements.txt .
|
| 5 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 6 |
+
COPY . .
|
| 7 |
+
ENV PORT=7860
|
| 8 |
+
# fastembed downloads its model to a cache; keep it writable
|
| 9 |
+
ENV HF_HOME=/tmp/hf FASTEMBED_CACHE_PATH=/tmp/fastembed
|
| 10 |
+
EXPOSE 7860
|
| 11 |
+
CMD ["python", "app.py"]
|
README.md
CHANGED
|
@@ -1,10 +1,50 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
pinned: false
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: RAG Q&A with Citations
|
| 3 |
+
emoji: 📚
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
+
short_description: Grounded, cited answers over a construction KB · 30-in-15 #10
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# RAG Q&A with Citations 📚
|
| 13 |
+
|
| 14 |
+
Ask a question and get an answer **grounded only in a curated knowledge base**, with **inline `[n]`
|
| 15 |
+
citations** to the exact sources — and an honest **"I don't know"** when the answer isn't in the KB.
|
| 16 |
+
No hallucinating past the sources. Demo knowledge base: **construction & home remodeling**.
|
| 17 |
+
|
| 18 |
+
> Project **#10** of my *"30 AI Projects in 15 Days"* build-in-public challenge.
|
| 19 |
+
|
| 20 |
+
**▶ Live demo: https://ask.gritai.solutions**
|
| 21 |
+
|
| 22 |
+

|
| 23 |
+
|
| 24 |
+
## Why it's different
|
| 25 |
+
Most chatbots will confidently make things up. This one is **retrieval-grounded and attributed**: it
|
| 26 |
+
answers *only* from the sources it retrieved, cites each claim, and refuses to guess when the KB doesn't
|
| 27 |
+
cover the question. That's the pattern real production RAG needs — trust and traceability.
|
| 28 |
+
|
| 29 |
+
## How it works
|
| 30 |
+
- **Chunk → embed → retrieve → answer.** The KB is embedded with **fastembed** (`BAAI/bge-small-en-v1.5`,
|
| 31 |
+
ONNX/CPU — no GPU, no torch). A question is embedded and matched by cosine similarity to the top sources.
|
| 32 |
+
- **Grounded answer with forced tool-use:** Claude answers using only the retrieved sources, returns
|
| 33 |
+
inline `[n]` citations, and a `found` flag — if the sources don't answer the question, it says so.
|
| 34 |
+
- The UI shows the answer with clickable citations and the exact source passages below.
|
| 35 |
+
|
| 36 |
+
## Run locally
|
| 37 |
+
```bash
|
| 38 |
+
pip install -r requirements.txt
|
| 39 |
+
export ANTHROPIC_API_KEY=sk-ant-...
|
| 40 |
+
python app.py # http://127.0.0.1:7860
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
## Deploy (always-on)
|
| 44 |
+
Ships with a `Dockerfile` — works on **Hugging Face Spaces**, Render, Railway, or Fly.io.
|
| 45 |
+
Set `ANTHROPIC_API_KEY` as a secret. Embeddings are local (no other keys).
|
| 46 |
+
|
| 47 |
+
*Pro tier (roadmap): point it at your own private document set — hosted, grounded, cited.*
|
| 48 |
+
|
| 49 |
+
---
|
| 50 |
+
Built by **Robert Lucyk** · [GritAI Solutions](https://gritai.solutions) · part of the 30-in-15 challenge.
|
app.py
ADDED
|
@@ -0,0 +1,358 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""RAG Q&A with Citations — ask a question, get an answer grounded ONLY in a curated
|
| 3 |
+
construction & remodeling knowledge base, with inline [n] citations to the exact sources —
|
| 4 |
+
and an honest "I couldn't find that in the knowledge base" when the answer isn't there.
|
| 5 |
+
|
| 6 |
+
Real RAG: chunk -> embed (fastembed, ONNX/CPU, no torch) -> retrieve (cosine) -> Claude answers
|
| 7 |
+
from the retrieved sources only, citing each. No hallucinating past the sources.
|
| 8 |
+
|
| 9 |
+
pip install flask requests anthropic fastembed numpy
|
| 10 |
+
python app.py # http://127.0.0.1:7860
|
| 11 |
+
|
| 12 |
+
Project #10 of the "30 Projects in 15 Days" challenge — GritAI.
|
| 13 |
+
"""
|
| 14 |
+
import os, re, json
|
| 15 |
+
import numpy as np
|
| 16 |
+
from flask import Flask, request, jsonify, Response
|
| 17 |
+
|
| 18 |
+
PORT = int(os.environ.get("PORT", "7860"))
|
| 19 |
+
ANTHROPIC_KEY = os.environ.get("ANTHROPIC_API_KEY")
|
| 20 |
+
ANTHROPIC_MODEL = os.environ.get("ANTHROPIC_MODEL", "claude-haiku-4-5")
|
| 21 |
+
TOP_K = 4
|
| 22 |
+
|
| 23 |
+
# ============================ Construction & Remodeling KB ============================
|
| 24 |
+
# General, public industry knowledge (no proprietary data). Each entry is a citable source.
|
| 25 |
+
KB = [
|
| 26 |
+
{"t": "Do I need a building permit?",
|
| 27 |
+
"x": "Most structural, electrical, plumbing, and mechanical work requires a permit from your local "
|
| 28 |
+
"building department — including additions, removing or moving walls, new circuits, water-heater "
|
| 29 |
+
"replacement, decks over a certain height, and re-roofing in many areas. Cosmetic work (paint, "
|
| 30 |
+
"flooring, cabinets, fixtures that reuse existing connections) usually does not. Permits exist to "
|
| 31 |
+
"ensure work meets code and passes inspection; unpermitted work can cause problems when you sell. "
|
| 32 |
+
"When unsure, call your local building department and ask — it's free."},
|
| 33 |
+
{"t": "How permits and inspections work",
|
| 34 |
+
"x": "You (or your licensed contractor) submit plans and pay a fee; the department issues the permit; "
|
| 35 |
+
"work is done in phases with inspections at key points — typically a rough-in inspection (framing, "
|
| 36 |
+
"electrical, plumbing before walls are closed) and a final inspection when complete. Don't cover "
|
| 37 |
+
"walls until the rough-in passes. Keep the permit posted on site. Final sign-off closes the permit."},
|
| 38 |
+
{"t": "Hiring a contractor: licenses and insurance",
|
| 39 |
+
"x": "Verify the contractor holds a current state license for the trade and carries liability insurance "
|
| 40 |
+
"and workers' compensation. Ask for the license number and confirm it with your state licensing "
|
| 41 |
+
"board. Uninsured workers injured on your property can become your liability. For larger jobs, a "
|
| 42 |
+
"general contractor coordinates subcontractors and pulls permits."},
|
| 43 |
+
{"t": "Getting and comparing bids",
|
| 44 |
+
"x": "Get at least three written bids for the same defined scope so they're comparable. A good bid itemizes "
|
| 45 |
+
"labor, materials, permits, timeline, and payment schedule. Be wary of a bid far below the others — it "
|
| 46 |
+
"often means missing scope or corners cut. Don't automatically pick the lowest; weigh reputation, "
|
| 47 |
+
"references, and clarity."},
|
| 48 |
+
{"t": "Contracts, deposits, and payment schedules",
|
| 49 |
+
"x": "Always use a written contract listing scope, materials, price, start/finish dates, and a payment "
|
| 50 |
+
"schedule tied to milestones. A common structure is a modest deposit, progress payments as phases "
|
| 51 |
+
"complete, and a final payment after the work passes inspection and you're satisfied. Avoid paying "
|
| 52 |
+
"large sums up front; never pay in full before work is done."},
|
| 53 |
+
{"t": "Mechanic's liens and lien waivers",
|
| 54 |
+
"x": "If a contractor or supplier isn't paid, they can file a mechanic's lien against your property even if "
|
| 55 |
+
"you paid your general contractor. Protect yourself by requesting lien waivers from subcontractors and "
|
| 56 |
+
"suppliers as they're paid, confirming the money reached them."},
|
| 57 |
+
{"t": "Budgeting and the contingency fund",
|
| 58 |
+
"x": "Set aside a contingency of 10–20% of the project budget for surprises — older homes especially hide "
|
| 59 |
+
"problems (outdated wiring, water damage, non-code framing) behind the walls. Renovations frequently "
|
| 60 |
+
"run over when hidden conditions surface; the contingency keeps a surprise from stalling the job."},
|
| 61 |
+
{"t": "Kitchen remodel: scope, cost drivers, and timeline",
|
| 62 |
+
"x": "Kitchens are among the most expensive rooms to remodel because they combine cabinetry, countertops, "
|
| 63 |
+
"appliances, plumbing, electrical, and often flooring. Cabinetry and countertops are usually the "
|
| 64 |
+
"biggest line items. Moving plumbing or gas lines and relocating walls adds significant cost. A typical "
|
| 65 |
+
"mid-range kitchen remodel runs several weeks to a couple of months depending on scope and lead times "
|
| 66 |
+
"on custom cabinets."},
|
| 67 |
+
{"t": "Bathroom remodel basics",
|
| 68 |
+
"x": "Bathrooms pack plumbing, waterproofing, tile, ventilation, and electrical into a small space, so labor "
|
| 69 |
+
"per square foot is high. Proper waterproofing behind tile in showers is critical to prevent hidden "
|
| 70 |
+
"rot and mold. A quality exhaust fan vented outside (not into the attic) prevents moisture damage. "
|
| 71 |
+
"Moving the toilet, tub, or shower drain locations raises cost because it means opening the floor."},
|
| 72 |
+
{"t": "Flooring options compared",
|
| 73 |
+
"x": "Common choices: hardwood (durable, refinishable, higher cost, sensitive to moisture); engineered wood "
|
| 74 |
+
"(more moisture-stable, thinner wear layer); luxury vinyl plank/LVP (waterproof, budget-friendly, great "
|
| 75 |
+
"for kitchens/baths/basements); tile (very durable and waterproof, harder and colder underfoot); and "
|
| 76 |
+
"carpet (soft and cheap, wears faster). Match the material to the room's moisture and traffic."},
|
| 77 |
+
{"t": "Interior painting: prep and product",
|
| 78 |
+
"x": "A lasting paint job is 80% prep: clean surfaces, fill and sand holes, caulk gaps, and prime bare or "
|
| 79 |
+
"patched areas and stains. Use the right sheen — flat/matte hides wall flaws, eggshell/satin is a good "
|
| 80 |
+
"durable middle for most walls, and semi-gloss resists moisture on trim, kitchens, and baths. One "
|
| 81 |
+
"gallon covers roughly 350–400 square feet per coat; most jobs need two coats."},
|
| 82 |
+
{"t": "Drywall: hanging, taping, and patching",
|
| 83 |
+
"x": "Standard wall drywall is ½ inch; ceilings often use 5/8 inch to resist sagging. Screw to the studs, "
|
| 84 |
+
"then tape and apply three coats of joint compound, sanding between coats for a smooth, feathered seam. "
|
| 85 |
+
"Small holes are patched with mesh or a patch kit and compound; large holes need a backed drywall "
|
| 86 |
+
"patch. Use moisture-resistant board in wet areas."},
|
| 87 |
+
{"t": "Framing and load-bearing walls",
|
| 88 |
+
"x": "Walls are framed with studs spaced 16 inches on-center (sometimes 24). A load-bearing wall carries "
|
| 89 |
+
"weight from above (roof, upper floors) down to the foundation; removing or altering one requires a "
|
| 90 |
+
"properly sized beam and usually an engineer and a permit. Never remove a wall without confirming "
|
| 91 |
+
"whether it's load-bearing — getting this wrong is dangerous."},
|
| 92 |
+
{"t": "Roofing: materials and when to replace",
|
| 93 |
+
"x": "Asphalt shingles are the most common and last roughly 15–30 years depending on quality; metal roofs "
|
| 94 |
+
"last longer at a higher cost. Signs you need a new roof include curling or missing shingles, granules "
|
| 95 |
+
"collecting in gutters, daylight or leaks in the attic, and sagging. Re-roofing usually needs a permit "
|
| 96 |
+
"and an inspection."},
|
| 97 |
+
{"t": "Insulation and energy efficiency (R-value)",
|
| 98 |
+
"x": "Insulation is rated by R-value — higher means more resistance to heat flow. Attics benefit most from "
|
| 99 |
+
"added insulation and are usually the cheapest place to improve efficiency. Air-sealing gaps and leaks "
|
| 100 |
+
"before insulating matters as much as the insulation itself. Common types include fiberglass batts, "
|
| 101 |
+
"blown-in cellulose, and spray foam (higher cost, also air-seals)."},
|
| 102 |
+
{"t": "Electrical basics and when to call a pro",
|
| 103 |
+
"x": "Simple like-for-like swaps (a switch or outlet) are often DIY, but new circuits, panel work, and "
|
| 104 |
+
"anything you're unsure about should go to a licensed electrician and be permitted. Kitchens, baths, "
|
| 105 |
+
"garages, and outdoors require GFCI protection; many circuits now require AFCI breakers. Never work on "
|
| 106 |
+
"a live circuit — shut off the breaker and verify it's dead."},
|
| 107 |
+
{"t": "Plumbing basics and rough-in",
|
| 108 |
+
"x": "Plumbing is installed in two stages: the rough-in (supply and drain lines inside the walls and floor, "
|
| 109 |
+
"inspected before closing up) and the finish (fixtures and trim). Proper drain slope (about ¼ inch per "
|
| 110 |
+
"foot) and venting are required for drains to flow and to keep sewer gas out. Relocating fixtures means "
|
| 111 |
+
"opening walls or floors, which drives cost."},
|
| 112 |
+
{"t": "Decks: footings, materials, and permits",
|
| 113 |
+
"x": "Decks generally need a permit and rest on footings dug below the local frost line so they don't heave. "
|
| 114 |
+
"Materials range from pressure-treated lumber (cheapest) to cedar to composite (low-maintenance, higher "
|
| 115 |
+
"cost). Proper flashing where the deck ledger meets the house prevents rot and is a common failure "
|
| 116 |
+
"point. Railings and stair dimensions are code-regulated for safety."},
|
| 117 |
+
{"t": "Concrete and foundation cracks",
|
| 118 |
+
"x": "Thin, hairline cracks in concrete are common from normal curing and shrinkage and are usually cosmetic. "
|
| 119 |
+
"Warning signs that need a professional include cracks wider than about ¼ inch, horizontal cracks in "
|
| 120 |
+
"foundation walls, cracks that are growing, or doors and windows that suddenly stick — these can signal "
|
| 121 |
+
"settlement or structural movement."},
|
| 122 |
+
{"t": "HVAC sizing and maintenance",
|
| 123 |
+
"x": "Heating and cooling equipment must be correctly sized for the home — an oversized system short-cycles "
|
| 124 |
+
"and controls humidity poorly, while undersized struggles to keep up. Sizing is done with a load "
|
| 125 |
+
"calculation (Manual J), not a rule of thumb. Basic maintenance — changing filters regularly and an "
|
| 126 |
+
"annual service — extends equipment life and efficiency."},
|
| 127 |
+
{"t": "Older homes: lead paint and asbestos",
|
| 128 |
+
"x": "Homes built before 1978 may contain lead-based paint, and homes from roughly the 1980s and earlier may "
|
| 129 |
+
"contain asbestos in flooring, popcorn ceilings, insulation, and duct wrap. Disturbing these during a "
|
| 130 |
+
"remodel releases hazardous dust. Testing before demolition and using certified abatement pros for "
|
| 131 |
+
"removal is the safe, and often legally required, path."},
|
| 132 |
+
{"t": "Project phases and realistic timelines",
|
| 133 |
+
"x": "A typical renovation flows through phases: design and permitting, demolition, rough-in (framing, "
|
| 134 |
+
"electrical, plumbing, HVAC) with inspections, insulation and drywall, then finishes (paint, flooring, "
|
| 135 |
+
"cabinets, trim, fixtures) and a final inspection. Material lead times — especially custom cabinets, "
|
| 136 |
+
"windows, and specialty tile — often drive the schedule more than the labor does."},
|
| 137 |
+
{"t": "Return on investment of common projects",
|
| 138 |
+
"x": "Not all projects return equal value at resale. Curb-appeal and maintenance items (a new front door, "
|
| 139 |
+
"garage door, fresh paint, and minor kitchen and bath updates) tend to recoup a larger share of their "
|
| 140 |
+
"cost than high-end additions. Over-improving beyond the neighborhood's price range rarely pays back "
|
| 141 |
+
"the full investment."},
|
| 142 |
+
]
|
| 143 |
+
|
| 144 |
+
# ============================ embeddings + retrieval ============================
|
| 145 |
+
_EMB = None
|
| 146 |
+
_MAT = None # normalized KB embedding matrix
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
def emb_model():
|
| 150 |
+
global _EMB
|
| 151 |
+
if _EMB is None:
|
| 152 |
+
from fastembed import TextEmbedding
|
| 153 |
+
_EMB = TextEmbedding("BAAI/bge-small-en-v1.5")
|
| 154 |
+
return _EMB
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
def _norm(m):
|
| 158 |
+
n = np.linalg.norm(m, axis=-1, keepdims=True)
|
| 159 |
+
return m / np.clip(n, 1e-8, None)
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def build_index():
|
| 163 |
+
global _MAT
|
| 164 |
+
texts = [f"{d['t']}. {d['x']}" for d in KB]
|
| 165 |
+
vecs = np.array(list(emb_model().passage_embed(texts)), dtype=np.float32)
|
| 166 |
+
_MAT = _norm(vecs)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
if os.environ.get("SPACE_ID"): # warm the index at startup on the Space
|
| 170 |
+
try:
|
| 171 |
+
build_index()
|
| 172 |
+
print("[startup] KB index built", flush=True)
|
| 173 |
+
except Exception as _e:
|
| 174 |
+
print(f"[startup] index build failed (will retry on first ask): {_e}", flush=True)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
def retrieve(question, k=TOP_K):
|
| 178 |
+
if _MAT is None:
|
| 179 |
+
build_index()
|
| 180 |
+
q = _norm(np.array(list(emb_model().query_embed([question]))[0], dtype=np.float32).reshape(1, -1))[0]
|
| 181 |
+
sims = _MAT @ q
|
| 182 |
+
idx = np.argsort(-sims)[:k]
|
| 183 |
+
return [(int(i), float(sims[i])) for i in idx]
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
TOOL = {
|
| 187 |
+
"name": "answer",
|
| 188 |
+
"description": "Answer the question using ONLY the provided sources.",
|
| 189 |
+
"input_schema": {"type": "object", "properties": {
|
| 190 |
+
"found": {"type": "boolean", "description": "true only if the sources actually answer the question."},
|
| 191 |
+
"answer": {"type": "string", "description": "The answer, grounded in the sources, with inline citations "
|
| 192 |
+
"like [1] or [2] pointing to the source numbers used. If found is false, briefly say the "
|
| 193 |
+
"knowledge base doesn't cover it."},
|
| 194 |
+
"used": {"type": "array", "items": {"type": "integer"}, "description": "Source numbers actually cited."}},
|
| 195 |
+
"required": ["found", "answer", "used"]},
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def answer_question(question):
|
| 200 |
+
hits = retrieve(question)
|
| 201 |
+
sources = [{"n": i + 1, "title": KB[idx]["t"], "text": KB[idx]["x"], "score": round(sc, 3)}
|
| 202 |
+
for i, (idx, sc) in enumerate(hits)]
|
| 203 |
+
if not ANTHROPIC_KEY:
|
| 204 |
+
return {"found": False, "answer": "Set ANTHROPIC_API_KEY on the server to generate answers.",
|
| 205 |
+
"used": [], "sources": sources}
|
| 206 |
+
block = "\n\n".join(f"[{s['n']}] {s['title']}\n{s['text']}" for s in sources)
|
| 207 |
+
sysp = ("You answer questions about construction and home remodeling using ONLY the numbered SOURCES "
|
| 208 |
+
"provided. Ground every claim in the sources and cite them inline as [n]. If the sources do not "
|
| 209 |
+
"actually contain the answer, set found=false and say the knowledge base doesn't cover it — never "
|
| 210 |
+
"guess or use outside knowledge. Be concise and practical.")
|
| 211 |
+
try:
|
| 212 |
+
import anthropic
|
| 213 |
+
client = anthropic.Anthropic(api_key=ANTHROPIC_KEY)
|
| 214 |
+
r = client.messages.create(model=ANTHROPIC_MODEL, max_tokens=900, system=sysp,
|
| 215 |
+
tools=[TOOL], tool_choice={"type": "tool", "name": "answer"},
|
| 216 |
+
messages=[{"role": "user", "content": f"SOURCES:\n{block}\n\nQUESTION: {question}"}])
|
| 217 |
+
data = next((b.input for b in r.content if getattr(b, "type", "") == "tool_use"), {})
|
| 218 |
+
except Exception as e:
|
| 219 |
+
import sys; print("answer error:", repr(e), file=sys.stderr, flush=True)
|
| 220 |
+
return {"found": False, "answer": "Something went wrong — please try again.", "used": [], "sources": sources}
|
| 221 |
+
used = set(data.get("used", []) or [])
|
| 222 |
+
shown = [s for s in sources if s["n"] in used] or (sources if data.get("found") else [])
|
| 223 |
+
return {"found": bool(data.get("found")), "answer": data.get("answer", ""), "used": sorted(used),
|
| 224 |
+
"sources": shown}
|
| 225 |
+
|
| 226 |
+
|
| 227 |
+
app = Flask(__name__)
|
| 228 |
+
app.config["MAX_CONTENT_LENGTH"] = 256 * 1024
|
| 229 |
+
|
| 230 |
+
|
| 231 |
+
@app.route("/")
|
| 232 |
+
def home():
|
| 233 |
+
return Response(PAGE, mimetype="text/html")
|
| 234 |
+
|
| 235 |
+
|
| 236 |
+
@app.route("/api/ask", methods=["POST"])
|
| 237 |
+
def api_ask():
|
| 238 |
+
q = (request.get_json(force=True).get("question") or "").strip()[:600]
|
| 239 |
+
if not q:
|
| 240 |
+
return jsonify(error="Ask a question."), 400
|
| 241 |
+
return jsonify(answer_question(q))
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
PAGE = """<!doctype html><html lang="en"><head><meta charset="utf-8">
|
| 245 |
+
<title>GritAI · Cited Answers — Construction & Remodeling</title>
|
| 246 |
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
| 247 |
+
<link rel="preconnect" href="https://fonts.googleapis.com"><link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
| 248 |
+
<link href="https://fonts.googleapis.com/css2?family=Archivo:wght@700;800;900&family=IBM+Plex+Mono:wght@400;500;600&family=IBM+Plex+Sans:wght@400;500;600&display=swap" rel="stylesheet">
|
| 249 |
+
<style>
|
| 250 |
+
:root{--bg:#0a0b0e;--bg2:#0e1015;--panel:#111318;--ink:#eef0f4;--dim:#9aa1b0;--dim2:#5f6675;--line:#20242e;--line2:#2b303c;
|
| 251 |
+
--grit:#ff5a2a;--grit2:#ff7d54;--grit-dim:#c9451f;--ok:#5fd08a;
|
| 252 |
+
--disp:'Archivo',system-ui,sans-serif;--body:'IBM Plex Sans',system-ui,sans-serif;--mono:'IBM Plex Mono',ui-monospace,monospace}
|
| 253 |
+
*{box-sizing:border-box}html,body{height:100%}
|
| 254 |
+
body{margin:0;background:var(--bg);color:var(--ink);font-family:var(--body);font-size:15px;line-height:1.6;position:relative;overflow-x:hidden}
|
| 255 |
+
body::before{content:"";position:fixed;inset:0;z-index:0;pointer-events:none;background:linear-gradient(0deg,transparent 95%,rgba(255,90,42,.04) 100%),
|
| 256 |
+
repeating-linear-gradient(90deg,transparent 0 63px,rgba(255,255,255,.018) 63px 64px),repeating-linear-gradient(0deg,transparent 0 63px,rgba(255,255,255,.018) 63px 64px),radial-gradient(120% 80% at 50% -10%,#141821 0%,var(--bg) 55%)}
|
| 257 |
+
.wrap{position:relative;z-index:1;max-width:900px;margin:0 auto;padding:0 28px 70px}
|
| 258 |
+
header{display:flex;align-items:center;gap:16px;padding:22px 0 18px;border-bottom:1px solid var(--line);animation:fade .6s .02s both}
|
| 259 |
+
.mark{width:38px;height:38px;flex:none}
|
| 260 |
+
.brand .co{font-family:var(--disp);font-weight:900;font-size:19px;letter-spacing:.5px}.brand .co b{color:var(--grit)}
|
| 261 |
+
.brand .sub{font-family:var(--mono);font-size:10.5px;letter-spacing:.28em;text-transform:uppercase;color:var(--dim2);margin-top:5px}
|
| 262 |
+
.tags{margin-left:auto;display:flex;gap:8px}
|
| 263 |
+
.tag{font-family:var(--mono);font-size:10px;letter-spacing:.14em;text-transform:uppercase;color:var(--dim);border:1px solid var(--line2);border-radius:2px;padding:5px 9px}
|
| 264 |
+
.tag.hot{color:var(--grit);border-color:var(--grit-dim)}
|
| 265 |
+
.hero{padding:32px 0 24px;animation:fade .6s .12s both}
|
| 266 |
+
.hero h1{font-family:var(--disp);font-weight:800;font-size:clamp(28px,4.8vw,46px);line-height:1.03;letter-spacing:-.02em;margin:0;max-width:18ch}
|
| 267 |
+
.hero h1 em{font-style:normal;color:var(--grit)}
|
| 268 |
+
.hero p{color:var(--dim);max-width:60ch;margin:14px 0 0}
|
| 269 |
+
.sec{display:flex;align-items:center;gap:14px;margin:26px 0 12px}
|
| 270 |
+
.sec .num{font-family:var(--mono);font-size:11px;font-weight:600;color:var(--grit)}
|
| 271 |
+
.sec .name{font-family:var(--mono);font-size:11px;font-weight:500;letter-spacing:.26em;text-transform:uppercase;color:var(--dim)}
|
| 272 |
+
.sec .rule{flex:1;height:1px;background:linear-gradient(90deg,var(--line2),transparent)}
|
| 273 |
+
.ask{animation:fade .6s .2s both}
|
| 274 |
+
.field{display:flex;align-items:stretch;border:1px solid var(--line2);border-radius:3px;background:var(--panel);overflow:hidden;transition:border-color .18s}
|
| 275 |
+
.field:focus-within{border-color:var(--grit-dim)}
|
| 276 |
+
.field .car{font-family:var(--mono);color:var(--grit);padding:16px 0 16px 16px;user-select:none}
|
| 277 |
+
.field input{flex:1;background:transparent;border:0;outline:none;color:var(--ink);font-family:var(--body);font-size:15.5px;padding:16px 14px}
|
| 278 |
+
.field input::placeholder{color:var(--dim2)}
|
| 279 |
+
.run{border:0;background:var(--grit);color:#140a06;font-family:var(--mono);font-weight:600;font-size:13px;letter-spacing:.14em;text-transform:uppercase;padding:0 24px;cursor:pointer;transition:.16s}
|
| 280 |
+
.run:hover{background:var(--grit2);box-shadow:0 0 26px -4px rgba(255,90,42,.55)}.run:disabled{opacity:.55;cursor:default}
|
| 281 |
+
.ex{display:flex;flex-wrap:wrap;gap:8px;margin-top:12px}
|
| 282 |
+
.chip{font-family:var(--mono);font-size:12px;color:var(--dim);background:var(--panel);border:1px solid var(--line);border-radius:2px;padding:8px 11px;cursor:pointer;transition:.15s}
|
| 283 |
+
.chip:hover{color:var(--grit);border-color:var(--grit-dim)}
|
| 284 |
+
.out{display:none}.out.show{display:block}
|
| 285 |
+
.ans{border:1px solid var(--line2);border-left:2px solid var(--grit);border-radius:3px;background:var(--panel);padding:20px 22px;font-size:16px;line-height:1.65}
|
| 286 |
+
.ans.miss{border-left-color:var(--dim2)}
|
| 287 |
+
.ans cite,.ct{color:var(--grit);font-family:var(--mono);font-size:.82em;font-weight:600;font-style:normal;vertical-align:super;cursor:pointer;text-decoration:none}
|
| 288 |
+
.src{border:1px solid var(--line);border-radius:3px;background:linear-gradient(180deg,var(--panel),var(--bg2))}
|
| 289 |
+
.srow{display:grid;grid-template-columns:30px 1fr;gap:0 12px;padding:13px 18px;border-bottom:1px solid var(--line)}
|
| 290 |
+
.srow:last-child{border-bottom:0}
|
| 291 |
+
.srow .sn{font-family:var(--mono);color:var(--grit);font-weight:600}
|
| 292 |
+
.srow .st{font-family:var(--mono);font-weight:600;color:var(--grit2);font-size:13px;margin-bottom:3px}
|
| 293 |
+
.srow .sx{color:var(--dim);font-size:13px;line-height:1.55}
|
| 294 |
+
.spin{display:inline-flex;gap:6px;padding:16px 18px}.spin span{width:8px;height:8px;border-radius:50%;background:var(--grit);opacity:.5;animation:bd 1.2s infinite}
|
| 295 |
+
.spin span:nth-child(2){animation-delay:.18s}.spin span:nth-child(3){animation-delay:.36s}
|
| 296 |
+
footer{margin-top:32px;padding-top:16px;border-top:1px solid var(--line);font-family:var(--mono);font-size:11px;letter-spacing:.06em;color:var(--dim2);display:flex;gap:16px;flex-wrap:wrap}
|
| 297 |
+
footer .sp{margin-left:auto}footer a{color:var(--dim);text-decoration:none}footer a:hover{color:var(--grit)}
|
| 298 |
+
@keyframes fade{from{opacity:0;transform:translateY(10px)}to{opacity:1;transform:none}}
|
| 299 |
+
@keyframes bd{0%,60%,100%{transform:translateY(0);opacity:.35}30%{transform:translateY(-6px);opacity:1}}
|
| 300 |
+
</style></head>
|
| 301 |
+
<body><div class="wrap">
|
| 302 |
+
<header>
|
| 303 |
+
<div class="mark"><svg width="38" height="38" viewBox="0 0 38 38" fill="none"><rect x="1" y="1" width="36" height="36" rx="2" stroke="#2b303c"/><path d="M27 12.5A9 9 0 1 0 28 22H19" stroke="#ff5a2a" stroke-width="2.4" stroke-linecap="square"/><rect x="10.5" y="10.5" width="3" height="3" fill="#ff5a2a"/></svg></div>
|
| 304 |
+
<div class="brand"><div class="co">GRIT<b>AI</b></div><div class="sub">Cited Answers</div></div>
|
| 305 |
+
<div class="tags"><span class="tag hot">▸ Live</span><span class="tag">RAG</span><span class="tag">SDVOSB</span></div>
|
| 306 |
+
</header>
|
| 307 |
+
<section class="hero">
|
| 308 |
+
<h1>Answers you can <em>trust</em> — every claim cited.</h1>
|
| 309 |
+
<p>Ask about construction and home remodeling. The answer is grounded only in a curated knowledge base and cites the exact sources — and it says <b>"I don't know"</b> when the answer isn't there. No made-up facts.</p>
|
| 310 |
+
</section>
|
| 311 |
+
<div class="sec"><span class="num">01</span><span class="name">Ask</span><span class="rule"></span></div>
|
| 312 |
+
<div class="ask">
|
| 313 |
+
<div class="field"><span class="car">>_</span>
|
| 314 |
+
<input id="q" placeholder="Do I need a permit to remove a wall?" autofocus>
|
| 315 |
+
<button class="run" id="go" onclick="ask()">Ask ▸</button></div>
|
| 316 |
+
<div class="ex" id="ex"></div>
|
| 317 |
+
</div>
|
| 318 |
+
<div class="out" id="out">
|
| 319 |
+
<div class="sec"><span class="num">02</span><span class="name" id="anslabel">Answer</span><span class="rule"></span></div>
|
| 320 |
+
<div class="ans" id="answer"></div>
|
| 321 |
+
<div class="sec" id="srchead" style="display:none"><span class="num">03</span><span class="name">Sources</span><span class="rule"></span></div>
|
| 322 |
+
<div class="src" id="sources"></div>
|
| 323 |
+
</div>
|
| 324 |
+
<footer><span>RAG · fastembed + Claude · grounded, cited, honest</span>
|
| 325 |
+
<span class="sp"><a href="https://gritai.solutions">GRITAI SOLUTIONS</a> · 30-in-15 №10</span></footer>
|
| 326 |
+
</div><script>
|
| 327 |
+
const EXAMPLES=["Do I need a permit to remove a wall?","How much contingency should I budget for a remodel?",
|
| 328 |
+
"What flooring is best for a bathroom?","How do I vet a contractor before hiring?","When should I replace my roof?",
|
| 329 |
+
"What's the best paint color for resale?"];
|
| 330 |
+
const $=id=>document.getElementById(id);const q=$('q'),go=$('go'),out=$('out');
|
| 331 |
+
function esc(s){return String(s==null?'':s).replace(/[&<>]/g,c=>({'&':'&','<':'<','>':'>'}[c]));}
|
| 332 |
+
$('ex').innerHTML=EXAMPLES.map(e=>'<span class="chip">'+esc(e)+'</span>').join('');
|
| 333 |
+
$('ex').querySelectorAll('.chip').forEach(c=>c.onclick=()=>{q.value=c.textContent;ask();});
|
| 334 |
+
function citelink(s){return esc(s).replace(/\\[(\\d+)\\]/g,'<a class="ct" href="#src$1">[$1]</a>');}
|
| 335 |
+
function ask(){if(go.disabled)return;const text=q.value.trim();if(!text){q.focus();return;}
|
| 336 |
+
go.disabled=true;go.textContent='…';out.classList.add('show');$('srchead').style.display='none';
|
| 337 |
+
$('answer').innerHTML='<span class="spin"><span></span><span></span><span></span></span>';$('sources').innerHTML='';
|
| 338 |
+
fetch('/api/ask',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({question:text})})
|
| 339 |
+
.then(r=>r.json()).then(d=>{
|
| 340 |
+
if(d.error){$('answer').textContent=d.error;return;}
|
| 341 |
+
const miss=!d.found;$('answer').className='ans'+(miss?' miss':'');
|
| 342 |
+
$('anslabel').textContent=miss?'Not in the knowledge base':'Answer';
|
| 343 |
+
$('answer').innerHTML=citelink(d.answer||'');
|
| 344 |
+
const src=d.sources||[];
|
| 345 |
+
if(src.length){$('srchead').style.display='flex';
|
| 346 |
+
$('sources').innerHTML=src.map(s=>'<div class="srow" id="src'+s.n+'"><span class="sn">['+s.n+']</span>'+
|
| 347 |
+
'<div><div class="st">'+esc(s.title)+'</div><div class="sx">'+esc(s.text)+'</div></div></div>').join('');}
|
| 348 |
+
}).catch(()=>{$('answer').textContent='Something went wrong — please try again.';})
|
| 349 |
+
.finally(()=>{go.disabled=false;go.textContent='Ask \\u25b8';});
|
| 350 |
+
}
|
| 351 |
+
q.addEventListener('keydown',e=>{if(e.key==='Enter')ask();});
|
| 352 |
+
</script></body></html>"""
|
| 353 |
+
|
| 354 |
+
|
| 355 |
+
if __name__ == "__main__":
|
| 356 |
+
backend = "Claude API" if ANTHROPIC_KEY else "NO KEY (retrieval only)"
|
| 357 |
+
print(f"RAG Q&A with Citations on http://127.0.0.1:{PORT} [backend: {backend}]", flush=True)
|
| 358 |
+
app.run(host="0.0.0.0", port=PORT, threaded=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask>=3.0
|
| 2 |
+
requests>=2.31
|
| 3 |
+
anthropic>=0.40
|
| 4 |
+
fastembed>=0.3
|
| 5 |
+
numpy>=1.24
|