-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvector_store.py
More file actions
152 lines (127 loc) · 4.77 KB
/
Copy pathvector_store.py
File metadata and controls
152 lines (127 loc) · 4.77 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from qdrant_client import QdrantClient, models
from config import QDRANT_HOST, QDRANT_PORT, QDRANT_URL, QDRANT_API_KEY
EMBEDDING_DIM = 3072
COLLECTION_NAME = "facts"
_client = None
qdrant_client = None # Module-level reference, initialized by init_collections()
def _date_to_int(date_val) -> int:
"""Convert date string or date object → YYYYMMDD integer for Qdrant range filters."""
try:
s = str(date_val)
return int(s.replace("-", ""))
except (ValueError, AttributeError, TypeError):
return 0
def _get_client():
global _client
if _client is None:
if QDRANT_URL:
_client = QdrantClient(url=QDRANT_URL, api_key=QDRANT_API_KEY, timeout=30)
else:
_client = QdrantClient(host=QDRANT_HOST, port=QDRANT_PORT, prefer_grpc=False, timeout=30)
return _client
def init_collections():
global qdrant_client
client = _get_client()
qdrant_client = client
if not client.collection_exists(COLLECTION_NAME):
client.create_collection(
collection_name=COLLECTION_NAME,
vectors_config=models.VectorParams(size=EMBEDDING_DIM, distance=models.Distance.COSINE),
)
# Create payload index for date filtering (required by Qdrant Cloud)
client.create_payload_index(
collection_name=COLLECTION_NAME,
field_name="date_int",
field_schema=models.PayloadSchemaType.INTEGER,
)
print(f"[qdrant] Created collection '{COLLECTION_NAME}' ({EMBEDDING_DIM}-dim) with date_int index")
def upsert_facts_batch(facts: list[dict]):
"""Batch upsert. Each dict: {fact_id, embedding, fact_text, conversation_date, category}."""
if not facts:
return
client = _get_client()
points = [
models.PointStruct(
id=f["fact_id"],
vector=f["embedding"],
payload={
"fact_text": f["fact_text"],
"conversation_date": f.get("conversation_date", ""),
"date_int": _date_to_int(f.get("conversation_date", "")),
"category": f.get("category", ""),
"fact_id": f["fact_id"],
},
)
for f in facts
]
client.upsert(collection_name=COLLECTION_NAME, points=points)
def search_facts(query_embedding: list[float], top_k: int = 5,
date_filter: dict = None) -> list[dict]:
"""Vector search on facts. Optional date filter."""
client = _get_client()
search_filter = None
if date_filter:
search_filter = models.Filter(
must=[
models.FieldCondition(
key="date_int",
range=models.Range(
gte=_date_to_int(date_filter["date_from"]),
lte=_date_to_int(date_filter["date_to"]),
),
)
]
)
results = client.query_points(
collection_name=COLLECTION_NAME,
query=query_embedding,
query_filter=search_filter,
limit=top_k,
)
return [
{
"fact_id": hit.payload["fact_id"],
"fact_text": hit.payload["fact_text"],
"conversation_date": hit.payload.get("conversation_date"),
"category": hit.payload.get("category"),
"score": hit.score,
}
for hit in results.points
]
def rebuild_from_db():
"""Delete Qdrant collection and re-embed all facts from PostgreSQL."""
import db
from retrieval.vectorize_service import embed_texts
facts = db.get_all_facts()
if not facts:
print("[qdrant] No facts in DB, nothing to rebuild.")
return
delete_collection()
init_collections()
BATCH_SIZE = 50
total = len(facts)
for i in range(0, total, BATCH_SIZE):
batch = facts[i:i + BATCH_SIZE]
texts = [f["fact_text"] for f in batch]
embeddings = embed_texts(texts)
points = [
models.PointStruct(
id=f["id"],
vector=emb,
payload={
"fact_text": f["fact_text"],
"conversation_date": f.get("conversation_date") or "",
"date_int": _date_to_int(f.get("conversation_date") or ""),
"category": f.get("category") or "",
"fact_id": f["id"],
},
)
for f, emb in zip(batch, embeddings)
]
_get_client().upsert(collection_name=COLLECTION_NAME, points=points)
print(f" [qdrant] Rebuilt {min(i + BATCH_SIZE, total)}/{total} facts")
print(f"[qdrant] Rebuild complete: {total} facts indexed.")
def delete_collection():
client = _get_client()
if client.collection_exists(COLLECTION_NAME):
client.delete_collection(COLLECTION_NAME)