Spaces:
Runtime error
Runtime error
qdrant client
Browse files
client.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from qdrant_client import QdrantClient
|
2 |
+
|
3 |
+
|
4 |
+
class HybridClient:
|
5 |
+
DENSE_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
6 |
+
SPARSE_MODEL = "prithivida/Splade_PP_en_v1"
|
7 |
+
|
8 |
+
def __init__(self):
|
9 |
+
self.qdrant_client = QdrantClient(
|
10 |
+
url="https://e8c7892c-84a5-4b73-9281-27d52258c6d8.europe-west3-0.gcp.cloud.qdrant.io:6333",
|
11 |
+
api_key="B2LX_KVbb7VemiHlxvXxl3ORdz4hxV7xycMRgPcylldtZ1Aakx1rmQ",
|
12 |
+
)
|
13 |
+
self.qdrant_client.set_model(self.DENSE_MODEL)
|
14 |
+
self.qdrant_client.set_sparse_model(self.SPARSE_MODEL)
|
15 |
+
|
16 |
+
def create(self, collection: str):
|
17 |
+
if not self.qdrant_client.collection_exists(collection):
|
18 |
+
self.create_collection(
|
19 |
+
collection_name=collection,
|
20 |
+
vectors_config=self.qdrant_client.get_fastembed_vector_params(),
|
21 |
+
sparse_vectors_config=self.qdrant_client.get_fastembed_sparse_vector_params(),
|
22 |
+
)
|
23 |
+
return collection
|
24 |
+
return None
|
25 |
+
|
26 |
+
def insert(self, collection, chunks):
|
27 |
+
documents = []
|
28 |
+
for chunk in chunks:
|
29 |
+
documents.append(chunk.pop("text"))
|
30 |
+
|
31 |
+
self.qdrant_client.add(
|
32 |
+
collection_name=collection,
|
33 |
+
documents=documents,
|
34 |
+
metadata=chunks,
|
35 |
+
parallel=0,
|
36 |
+
)
|
37 |
+
|
38 |
+
def search(self, collection, text: str, limit: int = 10):
|
39 |
+
search_result = self.qdrant_client.query(
|
40 |
+
collection_name=collection,
|
41 |
+
query_text=text,
|
42 |
+
query_filter=None,
|
43 |
+
limit=limit,
|
44 |
+
)
|
45 |
+
# Select and return metadata
|
46 |
+
# metadata = [hit.metadata for hit in search_result]
|
47 |
+
return search_result
|