Compare commits

..

No commits in common. "e6770ba164b762efa83581193e4308d33234b792" and "1c471534ac770c0486b00b664dfaa8377de63936" have entirely different histories.

3 changed files with 5 additions and 12 deletions

View file

@ -79,9 +79,8 @@ retrieve → grade_relevance ──relevant──→ generate_answer → (end)
└──not relevant & retries exhausted──→ generate_answer (confidence="low") └──not relevant & retries exhausted──→ generate_answer (confidence="low")
``` ```
1. **`retrieve`** – FAISS similarity search (`k=4`) using the local 1. **`retrieve`** – FAISS similarity search (`k=4`) using the local `all-MiniLM-L6-v2`
`paraphrase-multilingual-MiniLM-L12-v2` embedding model (multilingual, so non-English embedding model.
questions still retrieve reasonably against the English policy corpus).
2. **`grade_relevance`** – the LLM decides whether the retrieved snippets are sufficient. 2. **`grade_relevance`** – the LLM decides whether the retrieved snippets are sufficient.
3. **`rewrite_query`** – if not, the LLM rewrites the question (e.g. resolves pronouns like 3. **`rewrite_query`** – if not, the LLM rewrites the question (e.g. resolves pronouns like
"this"/"that", sharpens keywords), and returns to `retrieve` — at most 2 times. "this"/"that", sharpens keywords), and returns to `retrieve` — at most 2 times.
@ -144,7 +143,7 @@ documents, and a "Rebuild index" button that clears the cached graph/store and f
| Component | Choice | Rationale | | Component | Choice | Rationale |
|---|---|---| |---|---|---|
| LLM | **Ollama runtime + `qwen2.5:14b`** (configurable via `OLLAMA_MODEL` env var) | No paid API; Ollama is just the local model server, so the actual model can be swapped via env var at any time, with no code change. The project originally started with `llama3.2:3b` (smaller, fast even on CPU), then switched to `qwen2.5:14b` once a GPU became available (RTX 5070 Ti, 16 GB VRAM) — see the `deploy.resources.reservations.devices` (NVIDIA GPU) block on the `ollama` service in `docker-compose.yml`. **The real functional eval and load test were run against both models** (see sections 5-6) — the GPU-backed Qwen gave not only better quality but also roughly a **4.3× latency improvement**. The trade-off that persisted across both models: smaller/mid-size LLMs are less reliable at strictly following a JSON format → so `app/llm.py` defensively parses the JSON response (extracts the first `{...}` block via regex, falls back to a sensible default on any missing/invalid field). | | LLM | **Ollama runtime + `qwen2.5:14b`** (configurable via `OLLAMA_MODEL` env var) | No paid API; Ollama is just the local model server, so the actual model can be swapped via env var at any time, with no code change. The project originally started with `llama3.2:3b` (smaller, fast even on CPU), then switched to `qwen2.5:14b` once a GPU became available (RTX 5070 Ti, 16 GB VRAM) — see the `deploy.resources.reservations.devices` (NVIDIA GPU) block on the `ollama` service in `docker-compose.yml`. **The real functional eval and load test were run against both models** (see sections 5-6) — the GPU-backed Qwen gave not only better quality but also roughly a **4.3× latency improvement**. The trade-off that persisted across both models: smaller/mid-size LLMs are less reliable at strictly following a JSON format → so `app/llm.py` defensively parses the JSON response (extracts the first `{...}` block via regex, falls back to a sensible default on any missing/invalid field). |
| Embedding | **`sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2`** (local, runs on CPU) | Small (~470 MB), no API key, and multilingual (50+ languages) so retrieval still works reasonably when an employee asks in a language other than English, even though the policy corpus itself is English-only. The vectorstore cache key includes the embedding model name, so switching models (e.g. back to the smaller English-only `all-MiniLM-L6-v2`) auto-rebuilds the index instead of silently reusing incompatible vectors. | | Embedding | **`sentence-transformers/all-MiniLM-L6-v2`** (local, runs on CPU) | Small (~90 MB), fast, no API key, good enough retrieval quality for a corpus of this size. |
| Vectorstore | **FAISS** (in-process, persisted to disk) | No separate infrastructure needed for a prototype; see the 2.4 scalability note. | | Vectorstore | **FAISS** (in-process, persisted to disk) | No separate infrastructure needed for a prototype; see the 2.4 scalability note. |
## 4. Installation and running ## 4. Installation and running

View file

@ -13,9 +13,7 @@ PROJECT_ROOT = Path(__file__).resolve().parent.parent
OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "qwen2.5:14b") OLLAMA_MODEL = os.getenv("OLLAMA_MODEL", "qwen2.5:14b")
OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434") OLLAMA_BASE_URL = os.getenv("OLLAMA_BASE_URL", "http://localhost:11434")
EMBEDDING_MODEL = os.getenv( EMBEDDING_MODEL = os.getenv("EMBEDDING_MODEL", "sentence-transformers/all-MiniLM-L6-v2")
"EMBEDDING_MODEL", "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
)
POLICY_DOCS_DIR = Path(os.getenv("POLICY_DOCS_DIR", PROJECT_ROOT / "app" / "data" / "policies")) POLICY_DOCS_DIR = Path(os.getenv("POLICY_DOCS_DIR", PROJECT_ROOT / "app" / "data" / "policies"))
INDEX_DIR = Path(os.getenv("INDEX_DIR", PROJECT_ROOT / "app" / "data" / "index")) INDEX_DIR = Path(os.getenv("INDEX_DIR", PROJECT_ROOT / "app" / "data" / "index"))

View file

@ -46,15 +46,11 @@ def load_or_build_index(
"""Load a cached FAISS index from disk, rebuilding it if the corpus changed.""" """Load a cached FAISS index from disk, rebuilding it if the corpus changed."""
from langchain_community.vectorstores import FAISS from langchain_community.vectorstores import FAISS
from app.config import EMBEDDING_MODEL
docs_dir = Path(docs_dir) docs_dir = Path(docs_dir)
index_dir = Path(index_dir) index_dir = Path(index_dir)
embeddings = get_embeddings() embeddings = get_embeddings()
fingerprint_path = index_dir / _FINGERPRINT_FILE fingerprint_path = index_dir / _FINGERPRINT_FILE
# Include the embedding model name so switching models invalidates the cache instead of current_fingerprint = corpus_fingerprint(docs_dir)
# silently loading vectors built by a different model (same FAISS dimension can mask this).
current_fingerprint = f"{corpus_fingerprint(docs_dir)}::{EMBEDDING_MODEL}"
index_is_fresh = ( index_is_fresh = (
not force_rebuild not force_rebuild