-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathqdrant-search.py
81 lines (69 loc) · 2.09 KB
/
qdrant-search.py
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
#!/usr/bin/python3
"""
Search qdrant vector database.
This uses a sentence transformer for the embedding calculations.
Author: Jason A. Cox
11 October 2023
https://github.com/jasonacox/TinyLLM/
Requirements:
* pip install qdrant-client sentence-transformers pydantic~=2.4.2
"""
import os
from html import unescape
import qdrant_client as qc
import qdrant_client.http.models as qmodels
from sentence_transformers import SentenceTransformer
# Configuration Settings
MODEL = os.environ.get("MY_MODEL", "all-MiniLM-L6-v2")
DEBUG = os.environ.get("DEBUG", "False") == "True"
COLLECTION_NAME = os.environ.get("COLLECTION_NAME", "mylibrary")
QDRANT_HOST = os.environ.get("QDRANT_HOST", "localhost")
DEVICE = os.environ.get("DEVICE", "cuda")
RESULTS = 5
# Sentence Transformer Setup
print("Sentence Transformer starting...")
model = SentenceTransformer(MODEL, device=DEVICE)
# Qdrant Setup
print("Connecting to Qdrant DB...")
client = qc.QdrantClient(url=QDRANT_HOST)
# Create embeddings for text
def embed_text(text):
embeddings = model.encode(text, convert_to_tensor=True)
return embeddings
# Find document closely related to query
def query_index(query, top_k=5):
vector = embed_text(query)
results = client.search(
collection_name=COLLECTION_NAME,
query_vector=vector,
limit=top_k,
with_payload=True,
)
found=[]
for res in results:
found.append({"title": res.payload["title"],
"text": res.payload["text"],
"url": res.payload["url"],
"score": res.score})
return found
#
# Main - User Loop
#
# Query the collection - TEST
print("Connected to Vector Database\n")
print("Enter a query or 'q' to quit.")
while True:
prompt = input("\nQuery: ")
if prompt in ["q", "Q", ""]:
break
# Query the index
query_result = query_index(prompt, top_k=RESULTS)
# Print results
print("")
print("Prompt: " + prompt)
print(f"Top {RESULTS} Documents found:")
for result in query_result:
print(" * " + result['title'])
print("")
print("Done.")
# Done