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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167 | class QdrantVectorStore(VectorStore):
"""Vector store backed by Qdrant.
Requires the ``qdrant-client`` package (install via ``teff[embedding]``).
"""
def __init__(
self,
host: str = "localhost",
port: int = 6333,
collection: str = "teff",
client=None,
):
if client is None:
try:
from qdrant_client import QdrantClient
except ImportError as e:
raise ImportError("install qdrant-client for QdrantVectorStore") from e
client = QdrantClient(host=host, port=port)
self._client = client
self._collection = collection
def _ensure_collection(self, dim: int) -> None:
from qdrant_client.models import Distance, VectorParams
collections = {c.name for c in self._client.get_collections().collections}
if self._collection not in collections:
self._client.create_collection(
collection_name=self._collection,
vectors_config=VectorParams(size=dim, distance=Distance.COSINE),
)
@staticmethod
def _hash_id(vid: str) -> int:
return int(hashlib.md5(vid.encode()).hexdigest()[:16], 16)
async def add(self, vectors: list[tuple[str, list[float], dict]]) -> None:
from qdrant_client.models import PointStruct
if vectors:
self._ensure_collection(len(vectors[0][1]))
points = [
PointStruct(
id=self._hash_id(vid),
vector=vec,
payload={"doc_id": vid, **meta},
)
for vid, vec, meta in vectors
]
self._client.upsert(collection_name=self._collection, points=points)
async def search(
self,
query: list[float],
k: int = 10,
filter: dict | None = None,
hybrid: bool = False,
query_text: str | None = None,
) -> list[tuple[str, float, dict]]:
results = self._client.query_points(
collection_name=self._collection,
query=query,
limit=k,
query_filter=_to_qdrant_filter(filter),
)
return [
(r.payload.get("doc_id", str(r.id)), r.score, r.payload) # type: ignore[misc, union-attr]
for r in results.points
]
async def delete(self, ids: list[str]) -> None:
hashed = [self._hash_id(vid) for vid in ids]
self._client.delete(collection_name=self._collection, points_selector=hashed) # type: ignore[arg-type]
async def count(self) -> int:
return self._client.count(collection_name=self._collection).count
async def entries(
self, limit: int = 100, offset: int = 0
) -> list[tuple[str, dict]]:
res = self._client.scroll(
collection_name=self._collection,
limit=limit,
offset=offset,
with_payload=True,
with_vectors=False,
)
points, _ = res
return [(p.payload.get("doc_id", str(p.id)), p.payload) for p in points] # type: ignore[misc, union-attr]
async def get(self, ids: list[str]) -> list[tuple[str, dict]]:
hashed = [self._hash_id(vid) for vid in ids]
res = self._client.retrieve(
collection_name=self._collection,
ids=hashed,
with_payload=True,
with_vectors=False,
)
return [(p.payload.get("doc_id", str(p.id)), p.payload) for p in res] # type: ignore[misc, union-attr]
async def update_metadata(self, id: str, metadata: dict) -> None:
from qdrant_client import models
self._client.set_payload(
collection_name=self._collection,
payload=metadata,
points=models.FilterSelector(
filter=models.Filter(
must=[
models.FieldCondition(
key="doc_id", match=models.MatchValue(value=id)
)
]
)
),
)
async def clear(self) -> None:
from qdrant_client import models
self._client.delete(
collection_name=self._collection,
points_selector=models.FilterSelector(filter=models.Filter(must=[])),
)
|