-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexer.py
More file actions
91 lines (71 loc) · 2.83 KB
/
Copy pathindexer.py
File metadata and controls
91 lines (71 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Legacy indexer module - maintained for backward compatibility.
For new implementations, use retriever.py with HybridRetriever.
This module provides basic FAISS indexing with sentence transformers.
"""
import numpy as np
import faiss
from sentence_transformers import SentenceTransformer
from typing import List, Tuple
from data_loader import Document
class Indexer:
"""
Basic vector indexer using sentence transformers and FAISS.
Note: For production use, prefer HybridRetriever from retriever.py
which provides:
- Hybrid search (semantic + BM25)
- Cross-encoder reranking
- Query expansion
- Caching
"""
def __init__(self, model_name: str = "all-MiniLM-L6-v2"):
"""
Initialize indexer with sentence transformer model.
Args:
model_name: Sentence transformer model to use
"""
self.model = SentenceTransformer(model_name)
self.index = None
self.documents: List[Document] = []
self.dimension = 384 # Dimension for all-MiniLM-L6-v2
def index_documents(self, documents: List[Document]):
"""
Create embeddings and build FAISS index.
Args:
documents: List of Document objects to index
"""
self.documents = documents
# Generate embeddings
texts = [doc.content for doc in documents]
embeddings = self.model.encode(texts, show_progress_bar=True)
# Normalize for cosine similarity
faiss.normalize_L2(embeddings)
# Build FAISS index (Inner Product = Cosine similarity with normalized vectors)
self.index = faiss.IndexFlatIP(self.dimension)
self.index.add(embeddings.astype('float32'))
print(f"Indexed {len(documents)} documents")
def search(self, query: str, k: int = 5) -> List[Tuple[Document, float]]:
"""
Search for similar documents.
Args:
query: Search query
k: Number of results to return
Returns:
List of (document, score) tuples sorted by relevance
"""
if self.index is None:
raise ValueError("Index not built. Call index_documents first.")
# Encode query
query_embedding = self.model.encode([query])
faiss.normalize_L2(query_embedding)
# Search
scores, indices = self.index.search(query_embedding.astype('float32'), k)
# Build results
results = []
for idx, score in zip(indices[0], scores[0]):
if 0 <= idx < len(self.documents):
results.append((self.documents[idx], float(score)))
return results
def get_document_count(self) -> int:
"""Get number of indexed documents."""
return len(self.documents)