Skip to content

About

RAG chat assistant that scopes retrieval to one asset's documents and cites pages you can open beside the chat. Built for injection molding troubleshooting, usable for any domain.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

Scoped RAG Assistant

A retrieval-augmented chat assistant that answers from your own documents, scoped to one asset at a time. Pick an asset, ask a question, and get an answer grounded in that asset's documents, with every claim cited back to a page you can open beside the chat.

It was designed as a troubleshooting assistant for injection molding machine (IMM) technicians, and the sample setup and wording still reflect that: the assets are machines and the documents are their manuals. Nothing in the design is specific to molding, though. Swap the folders for products, sites, vehicles, customers or any other unit that has its own documents, and the same scoped search, citations and page viewer apply.

This is a trimmed-down reference version of a project I built during an internship. Company-specific data, integrations and deployment pieces have been removed. What remains is the core of the app and the minimum it needs to run.

Using it for another domain

  • Put each asset's documents in its own folder under data/documents/, and anything shared by all assets in _general/.
  • Folder names are parsed in app/machines/registry.py; change the pattern there to fit your naming.
  • Curated identity facts go in app/machines/machine_facts.json.
  • The domain wording lives in the answering prompts under app/rag/ and the starter questions in web/app.js.

How it works

  • Machine-scoped retrieval. Documents live in one folder per machine (IMM <number> - <MAKE> (<model>) <serial>), plus a shared _general folder. A question only ever searches the selected machine's documents and the shared library.
  • Hybrid search. Vector similarity and an exact-match keyword pass (alarm codes, part numbers, oil grades) are merged with reciprocal rank fusion, then diversified so one long manual cannot crowd out a one-page datasheet.
  • Progressive retrieval with an evidence judge. A fresh question runs a multi-pass search. A separate model call judges whether the excerpts actually answer the question and, if not, widens the search with a better query.
  • Tool-calling answer loop. The answering model (OpenAI Responses API) decides whether to search again, read more of a cited page, or look at a rendered page image. After a full search it must verify the value it is about to state against the page image, because spec tables often lose their row structure in text extraction.
  • Machine identity. The model is told which machine it is answering about (number, model, curated facts from app/machines/machine_facts.json) so it can pick the right row out of tables that cover a whole product line.
  • Streaming UI. Progress, the model's reasoning summary and the answer stream to the browser over SSE. Answers carry numbered citation chips and source cards with page thumbnails, and full answers come with a short summary and a "More detail" toggle.
  • Source viewer. A citation opens the document beside the chat (full screen on a tablet) at the cited page, with a tab per source, Prev/Next and arrow keys to page through it, and a button to open it in a new tab at the current page.
  • Small extras. Login with PBKDF2-hashed passwords and rate limiting, chat history per machine, thumbs up/down feedback, and a hint when a similar question was answered before.

Everything is stored in one SQLite file (chunk index, users, chat history and retrieval diagnostics).

Layout

app/
  api/        FastAPI routes: auth, machine registry, chat (SSE), previews
  auth/       session login gate, password hashing, rate limiting
  db/         SQLite schema, chat history, feedback, diagnostics
  machines/   machine registry built from folder names, curated facts
  rag/        parsing, chunking, embeddings, hybrid retrieval, judge, tool loop
web/          single-page technician UI (vanilla JS, no build step)
scripts/      index_documents.py, create_user.py
tests/        offline end-to-end smoke test

Run it

Needs Python 3.11+ and an OpenAI API key (or Azure OpenAI, see .env.example).

python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
cp .env.example .env    # set OPENAI_API_KEY and SESSION_SECRET

Put documents (PDF, XLSX, PPTX) under data/documents/:

data/documents/
  IMM 101 - ACME (PX 200-50) SN12345/
    Operating Manual.pdf
  _general/
    Molding Defects.pdf

Index them, create a login, and start the app:

python scripts/index_documents.py
python scripts/create_user.py tech1 --display-name "Tech One"
uvicorn app.main:app --port 8000

Open http://localhost:8000/ and sign in with the printed password.

Or with Docker: docker build -t scoped-rag-assistant . and run it with the same environment variables and data/ mounted at /app/data.

Tests

pytest

The smoke test builds a small document tree, indexes it with stand-in embeddings, and drives a full chat turn through the real API with a scripted stand-in for the model. It makes no network calls.

About

RAG chat assistant that scopes retrieval to one asset's documents and cites pages you can open beside the chat. Built for injection molding troubleshooting, usable for any domain.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages