Spaces:
Sleeping
Sleeping
Update searcher/sparse_retriever.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117# searcher/sparse_retriever.pyimport sqlite3import mathimport yamlfrom collections import defaultdictclass SparseRetriever:"""BM25 (Okapi BM25) lexical retrieval over the SQLite chunk store.Why BM25 alongside semantic search?- Dense retrieval can miss exact keyword matches (product codes, names, IDs)- BM25 is great for rare/specific terms that embeddings smooth over- Hybrid = best of both worldsBM25 formula:score(q, d) = Σ IDF(t) × (tf × (k1+1)) / (tf + k1 × (1 - b + b × dl/avgdl))"""def __init__(self, config_path="config.yaml"):with open(config_path) as f:config = yaml.safe_load(f)self.db_path = f"{config['data_dir']}/metadata.db"self.k1 = 1.5 # term frequency saturationself.b = 0.75 # length normalisation# Build in-memory BM25 index from SQLite on startupself._corpus = [] # list of (chunk_id, token_list)self._avgdl = 0.0self._df = defaultdict(int) # term → doc frequencyself._build_index()def _build_index(self):"""Load all chunks from SQLite and compute BM25 statistics."""conn = sqlite3.connect(self.db_path)rows = conn.execute("SELECT id, chunk_text FROM chunks").fetchall()conn.close()total_len = 0for chunk_id, text in rows:tokens = text.lower().split()self._corpus.append((chunk_id, tokens))total_len += len(tokens)for token in set(tokens):self._df[token] += 1self._avgdl = total_len / len(rows) if rows else 1.0self._N = len(rows)def _idf(self, term: str) -> float:"""Inverse document frequency for a term."""df = self._df.get(term, 0)return math.log((self._N - df + 0.5) / (df + 0.5) + 1)def retrieve(self, query: str, top_k: int = 20) -> list[dict]:"""Run BM25 retrieval over the corpus.Args:query (str) — raw or rewritten query (NOT expanded — BM25 is lexical)top_k (int) — number of results to returnReturns:list[dict] with chunk_id and sparse_score, sorted descending"""query_terms = query.lower().split()scores = {}for chunk_id, tokens in self._corpus:dl = len(tokens)score = 0.0tf_map = defaultdict(int)for t in tokens:tf_map[t] += 1for term in query_terms:if term not in tf_map:continuetf = tf_map[term]idf = self._idf(term)numerator = tf * (self.k1 + 1)denominator = tf + self.k1 * (1 - self.b + self.b * dl / self._avgdl)score += idf * numerator / denominatorif score > 0:scores[chunk_id] = scoresorted_results = sorted(scores.items(), key=lambda x: x[1], reverse=True)[:top_k]# Fetch text for top resultsconn = sqlite3.connect(self.db_path)results = []for chunk_id, score in sorted_results:row = conn.execute("SELECT chunk_text, filepath, chunk_index FROM chunks WHERE id = ?",(chunk_id,)).fetchone()if row:results.append({"chunk_id": chunk_id,"chunk_text": row[0],"filepath": row[1],"chunk_index": row[2],"sparse_score": score,})conn.close()return resultsif __name__ == "__main__":sr = SparseRetriever()results = sr.retrieve("quarterly budget", top_k=5)for r in results:print(f"[{r['sparse_score']:.4f}] {r['filepath']} → {r['chunk_text'][:80]}")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127# searcher/sparse_retriever.pyimport osimport sqlite3import mathimport yamlfrom collections import defaultdictclass SparseRetriever:"""BM25 (Okapi BM25) lexical retrieval over the SQLite chunk store.Why BM25 alongside semantic search?- Dense retrieval can miss exact keyword matches (product codes, names, IDs)- BM25 is great for rare/specific terms that embeddings smooth over- Hybrid = best of both worldsBM25 formula:score(q, d) = Σ IDF(t) × (tf × (k1+1)) / (tf + k1 × (1 - b + b × dl/avgdl))"""def __init__(self, config_path="config.yaml"):with open(config_path) as f:config = yaml.safe_load(f)self.data_dir = config["data_dir"]self.db_path = f"{self.data_dir}/metadata.db"self.k1 = 1.5 # term frequency saturationself.b = 0.75 # length normalisation# Build in-memory BM25 index from SQLite on startupself._corpus = [] # list of (chunk_id, token_list)self._avgdl = 0.0self._N = 0self._df = defaultdict(int) # term → doc frequencyself._build_index()def _build_index(self):"""Load all chunks from SQLite and compute BM25 statistics."""os.makedirs(self.data_dir, exist_ok=True)conn = sqlite3.connect(self.db_path)try:rows = conn.execute("SELECT id, chunk_text FROM chunks").fetchall()except sqlite3.OperationalError:rows = []conn.close()total_len = 0for chunk_id, text in rows:tokens = text.lower().split()self._corpus.append((chunk_id, tokens))total_len += len(tokens)for token in set(tokens):self._df[token] += 1self._avgdl = total_len / len(rows) if rows else 1.0self._N = len(rows)def _idf(self, term: str) -> float:"""Inverse document frequency for a term."""df = self._df.get(term, 0)return math.log((self._N - df + 0.5) / (df + 0.5) + 1)def retrieve(self, query: str, top_k: int = 20) -> list[dict]:"""Run BM25 retrieval over the corpus.Args:query (str) — raw or rewritten query (NOT expanded — BM25 is lexical)top_k (int) — number of results to returnReturns:list[dict] with chunk_id and sparse_score, sorted descending"""if not self._corpus:return []query_terms = query.lower().split()scores = {}for chunk_id, tokens in self._corpus:dl = len(tokens)score = 0.0tf_map = defaultdict(int)for t in tokens:tf_map[t] += 1for term in query_terms:if term not in tf_map:continuetf = tf_map[term]idf = self._idf(term)numerator = tf * (self.k1 + 1)denominator = tf + self.k1 * (1 - self.b + self.b * dl / self._avgdl)score += idf * numerator / denominatorif score > 0:scores[chunk_id] = scoresorted_results = sorted(scores.items(), key=lambda x: x[1], reverse=True)[:top_k]# Fetch text for top resultsconn = sqlite3.connect(self.db_path)results = []for chunk_id, score in sorted_results:row = conn.execute("SELECT chunk_text, filepath, chunk_index FROM chunks WHERE id = ?",(chunk_id,)).fetchone()if row:results.append({"chunk_id": chunk_id,"chunk_text": row[0],"filepath": row[1],"chunk_index": row[2],"sparse_score": score,})conn.close()return resultsif __name__ == "__main__":sr = SparseRetriever()results = sr.retrieve("quarterly budget", top_k=5)for r in results:print(f"[{r['sparse_score']:.4f}] {r['filepath']} → {r['chunk_text'][:80]}")
searcher/sparse_retriever.py+127-117