The embedding model (all-MiniLM-L6-v2) is English-only, so retrieval quality degraded for questions asked in other languages even though the chat model itself answers fine in them. Switched the default EMBEDDING_MODEL to sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 (50+ languages, same 384-dim output). The vectorstore's on-disk cache was keyed only on the policy corpus fingerprint, so changing the embedding model would have silently reused a stale index built from the old model's (differently-meaning) vectors instead of rebuilding. Folded EMBEDDING_MODEL into that fingerprint so a model change now triggers a rebuild automatically. Refs #16
87 lines
3 KiB
Python
87 lines
3 KiB
Python
"""FAISS-backed vector store wrapper.
|
|
|
|
This is the one module that knows about the storage backend. Everything else in the RAG
|
|
subgraph talks to it through `load_or_build_index` / `similarity_search`, so a future move
|
|
to a different vector database only touches this file.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
|
|
from app.config import INDEX_DIR, POLICY_DOCS_DIR, RAG_TOP_K
|
|
from app.rag.ingest import chunk_documents, corpus_fingerprint, load_markdown_files
|
|
|
|
_FINGERPRINT_FILE = "corpus.fingerprint"
|
|
|
|
_embeddings_cache = {}
|
|
|
|
|
|
def get_embeddings():
|
|
"""Lazily construct (and cache) the local sentence-transformers embedding model."""
|
|
from app.config import EMBEDDING_MODEL
|
|
|
|
if "embeddings" not in _embeddings_cache:
|
|
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
|
_embeddings_cache["embeddings"] = HuggingFaceEmbeddings(model_name=EMBEDDING_MODEL)
|
|
return _embeddings_cache["embeddings"]
|
|
|
|
|
|
def _build_index(docs_dir: Path, embeddings):
|
|
from langchain_community.vectorstores import FAISS
|
|
from langchain_core.documents import Document
|
|
|
|
documents = load_markdown_files(docs_dir)
|
|
chunks = chunk_documents(documents)
|
|
lc_docs = [
|
|
Document(page_content=c.content, metadata={"source": c.source, "chunk_id": c.chunk_id})
|
|
for c in chunks
|
|
]
|
|
return FAISS.from_documents(lc_docs, embeddings)
|
|
|
|
|
|
def load_or_build_index(
|
|
docs_dir: Path = POLICY_DOCS_DIR, index_dir: Path = INDEX_DIR, force_rebuild: bool = False
|
|
):
|
|
"""Load a cached FAISS index from disk, rebuilding it if the corpus changed."""
|
|
from langchain_community.vectorstores import FAISS
|
|
|
|
from app.config import EMBEDDING_MODEL
|
|
|
|
docs_dir = Path(docs_dir)
|
|
index_dir = Path(index_dir)
|
|
embeddings = get_embeddings()
|
|
fingerprint_path = index_dir / _FINGERPRINT_FILE
|
|
# Include the embedding model name so switching models invalidates the cache instead of
|
|
# 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 = (
|
|
not force_rebuild
|
|
and (index_dir / "index.faiss").exists()
|
|
and fingerprint_path.exists()
|
|
and fingerprint_path.read_text(encoding="utf-8").strip() == current_fingerprint
|
|
)
|
|
|
|
if index_is_fresh:
|
|
return FAISS.load_local(
|
|
str(index_dir), embeddings, allow_dangerous_deserialization=True
|
|
)
|
|
|
|
store = _build_index(docs_dir, embeddings)
|
|
index_dir.mkdir(parents=True, exist_ok=True)
|
|
store.save_local(str(index_dir))
|
|
fingerprint_path.write_text(current_fingerprint, encoding="utf-8")
|
|
return store
|
|
|
|
|
|
def similarity_search(store, query: str, k: int = RAG_TOP_K) -> list[dict]:
|
|
results = store.similarity_search_with_score(query, k=k)
|
|
return [
|
|
{
|
|
"content": doc.page_content,
|
|
"source": doc.metadata.get("source", "unknown"),
|
|
"score": float(score),
|
|
}
|
|
for doc, score in results
|
|
]
|