-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
85 lines (67 loc) · 2.81 KB
/
main.py
File metadata and controls
85 lines (67 loc) · 2.81 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
import os
from qdrant_client import QdrantClient
from qdrant_client.models import Distance, VectorParams, PointStruct
from flask import Flask, render_template
QDRANT_HOST = os.environ['QDRANT_HOST']
client = QdrantClient(url=QDRANT_HOST)
app = Flask(__name__)
@app.route('/init')
def init_db():
"""
Init the QDrant DB with a simple collection.
"""
# Create a collection
client.create_collection(
collection_name="test_collection",
vectors_config=VectorParams(size=4, distance=Distance.DOT),
)
# Add a vector
operation_info = client.upsert(
collection_name="test_collection",
wait=True,
points=[
PointStruct(id=1, vector=[0.05, 0.61, 0.76, 0.74], payload={"city": "Berlin"}),
PointStruct(id=2, vector=[0.19, 0.81, 0.75, 0.11], payload={"city": "London"}),
PointStruct(id=3, vector=[0.36, 0.55, 0.47, 0.94], payload={"city": "Moscow"}),
PointStruct(id=4, vector=[0.18, 0.01, 0.85, 0.80], payload={"city": "New York"}),
PointStruct(id=5, vector=[0.24, 0.18, 0.22, 0.44], payload={"city": "Beijing"}),
PointStruct(id=6, vector=[0.35, 0.08, 0.11, 0.44], payload={"city": "Mumbai"}),
],
)
print(operation_info)
return render_template('index.html', info="Database initialized!")
@app.route('/')
def index():
"""
This route performs a Qdrant query and displays the results on an HTML page.
"""
try:
# Perform the Qdrant search
# Ensure 'test_collection' exists and has data
search_result = client.query_points(
collection_name="test_collection",
query=[0.2, 0.1, 0.9, 0.7],
with_payload=True, # Set to True if you want to display payload data
with_vectors=True, # return the vectors
limit=3
).points
print(search_result)
# Extract the IDs, vectors, and city from payload for display
points_data = []
for point in search_result:
city_name = point.payload.get("city", "N/A") if point.payload else "N/A" # Safely get city
points_data.append({
"id": point.id,
"vector_preview": str(point.vector)[:50] + "..." if point.vector else "N/A",
"city": city_name # Pass the city data
})
# Render the HTML template, passing the results
return render_template('index.html', points=points_data)
except Exception as e:
# Basic error handling for display
error_message = f"An error occurred: {e}"
print(f"Error: {e}") # Log the full error to console
return render_template('index.html', error=error_message)
if __name__ == '__main__':
# Run the Flask app on localhost:3000
app.run(host='0.0.0.0', port=3000, debug=True)