-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreate_index.py
More file actions
78 lines (69 loc) · 2.97 KB
/
create_index.py
File metadata and controls
78 lines (69 loc) · 2.97 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
import os
from dotenv import load_dotenv
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (SimpleField,
SearchIndex,
SearchFieldDataType,
SearchableField,
SearchField,
VectorSearch,
HnswAlgorithmConfiguration,
HnswParameters,
VectorSearchAlgorithmKind,
VectorSearchAlgorithmMetric,
VectorSearchProfile)
# load_dotenv('.env')
service_endpoint = os.getenv('SEARCH_SERVICE_NAME')
service_key = os.getenv('SEARCH_API_KEY')
# instantiate client
client = SearchIndexClient(service_endpoint,
AzureKeyCredential(service_key))
def create_index():
name= "wine-index"
# Check if the index already exists
existing_indexes = [index.name for index in client.list_indexes()]
if name in existing_indexes:
# Delete the existing index
client.delete_index(name)
print(f"Existing index '{name}' deleted successfully.")
fields = [
SimpleField(name="id", type=SearchFieldDataType.String, key=True),
SearchableField(name="content", type=SearchFieldDataType.String, searchable=True, retrievable=True),
SearchableField(name="metadata", type=SearchFieldDataType.String, searchable=True, retrievable=True),
SearchField(name="content_vector",
type=SearchFieldDataType.Collection(SearchFieldDataType.Single),
vector_search_dimensions=1536,
vector_search_profile_name="my-vector-config",
)
]
# configure the vector search
vector_search = VectorSearch(
algorithms=[
HnswAlgorithmConfiguration(
name="my-hnsw",
kind=VectorSearchAlgorithmKind.HNSW,
parameters=HnswParameters(
m=4,
ef_construction=400,
ef_search=500,
metric=VectorSearchAlgorithmMetric.COSINE,
)
)
],
profiles=[
VectorSearchProfile(
name="my-vector-config",
algorithm_configuration_name="my-hnsw"
)
]
)
# create the index
index = SearchIndex(name=name,
fields=fields,
vector_search=vector_search)
# create index
result = client.create_index(index)
print(f"Index '{result.name} created successfully")
if __name__ == "__main__":
create_index()