-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_vector_index.py
More file actions
46 lines (37 loc) · 1.31 KB
/
create_vector_index.py
File metadata and controls
46 lines (37 loc) · 1.31 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
from pymongo import MongoClient
from pymongo.operations import SearchIndexModel
import time
from app.config import settings
# Create a vector search index in mongodb atlas
def create_vector_index():
# Create your index model, then create the search index
index_name = "vector_index"
search_index_model = SearchIndexModel(
definition={
"fields": [
{
"type": "vector",
"numDimensions": 768,
"path": "embedding",
"similarity": "cosine",
}
]
},
name=index_name,
type="vectorSearch",
)
# Connect to your Atlas cluster
client = MongoClient(settings.atlas_connection_string)
collection = client["rag-atlas"]["starter"]
collection.create_search_index(model=search_index_model)
# Wait for initial sync to complete
print("Polling to check if the index is ready. This may take up to a minute.")
predicate = None
if predicate is None:
predicate = lambda index: index.get("queryable") is True
while True:
indices = list(collection.list_search_indexes(index_name))
if len(indices) and predicate(indices[0]):
break
time.sleep(5)
print(index_name + " is ready for querying.")