-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_query_results.py
More file actions
38 lines (30 loc) · 1016 Bytes
/
get_query_results.py
File metadata and controls
38 lines (30 loc) · 1016 Bytes
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
# Define a function to run vector search queries
from pymongo import MongoClient
from get_embeddings import get_embedding
from app.config import settings
# import pprint
def get_query_results(query):
"""Gets results from a vector search query."""
query_embedding = get_embedding(query)
pipeline = [
{
"$vectorSearch": {
"index": "vector_index",
"queryVector": query_embedding,
"path": "embedding",
"exact": True,
"limit": 5,
}
},
{"$project": {"_id": 0, "text": 1}},
]
# Connect to your Atlas cluster
client = MongoClient(settings.atlas_connection_string)
collection = client["rag-atlas"]["starter"]
results = collection.aggregate(pipeline)
array_of_results = []
for doc in results:
array_of_results.append(doc)
return array_of_results
# Test the function with a sample query
# pprint.pprint(get_query_results("AI technology"))