-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathqdrant_setup.py
More file actions
73 lines (63 loc) · 2.19 KB
/
Copy pathqdrant_setup.py
File metadata and controls
73 lines (63 loc) · 2.19 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
import os
import random
from dotenv import load_dotenv
from qdrant_client import QdrantClient, models
load_dotenv()
client = QdrantClient(url=os.getenv("QDRANT_URL"))
collection_name = os.getenv("COLLECTION_NAME")
model_name = os.getenv("EMBEDDING_MODEL")
if not client.collection_exists(collection_name=collection_name):
client.create_collection(
collection_name=collection_name,
vectors_config=models.VectorParams(size=384, distance=models.Distance.COSINE))
def retrieve_from_store(question: str, user_id:str, n_points: int = 3) -> str:
results = client.query_points(
collection_name=collection_name,
query=models.Document(text=question, model=model_name),
query_filter=models.Filter(
must=[
models.FieldCondition(
key="group_id",
match=models.MatchValue(
value=user_id,
),
)
]
),
limit=n_points,
)
return results.points
def remove_data_from_store(user_id:str) -> str:
client.delete(
collection_name=collection_name,
points_selector=models.FilterSelector(
filter=models.Filter(
must=[
models.FieldCondition(
key="group_id",
match=models.MatchValue(
value=user_id,
),
)
]
)
)
)
def rag_pipeline_setup(user_id, documents):
client.upsert(
collection_name=collection_name,
points=[
models.PointStruct(
id=idx,
vector=models.Document(text=document["page_content"], model=model_name),
payload={"group_id": user_id, "document": document},
)
for idx, document in enumerate(documents)
],)
def select_random_chunk(documents):
if not documents:
return None, None
idx = random.randint(0, len(documents) - 1)
selected_doc = documents[idx]
content = f"filename:{selected_doc['filename']}\nPage_number:{selected_doc['page_number']}\nPage_Content: {selected_doc["page_content"]}\n\n\n"
return idx, content