-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit_index.py
58 lines (50 loc) · 2.35 KB
/
init_index.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
import os
from dotenv import load_dotenv
from azure.core.credentials import AzureKeyCredential
from azure.search.documents import SearchClient
from azure.search.documents.indexes import SearchIndexClient
# from azure.search.documents.indexes.models import SearchIndex, SearchField, SimpleField, SemanticSearch, SemanticConfiguration
from azure.search.documents.indexes.models import *
load_dotenv()
service_endpoint = f"{os.getenv('AZURE_SEARCH_SERVICE_ENDPOINT')}"
index_creds = AzureKeyCredential(os.getenv("AZURE_SEARCH_INDEX_KEY"))
index_name = os.getenv("AZURE_SEARCH_INDEX_NAME")
# Create a client for querying the index
search_client = SearchClient(endpoint=service_endpoint, index_name=index_name, credential=index_creds)
# Create an index
client = SearchIndexClient(service_endpoint, index_creds)
fields = [
SimpleField(name="id", type="Edm.String", key=True),
SearchableField(name="content", type="Edm.String", analyzer_name="standard.lucene"),
SearchField(name="embedding", type=SearchFieldDataType.Collection(SearchFieldDataType.Single),
hidden=False, searchable=True, filterable=False, sortable=False, facetable=False,
vector_search_dimensions=1536, vector_search_profile_name="my-vector-config"),
SimpleField(name="sourcepage", type="Edm.String", filterable=True, facetable=True),
SimpleField(name="sourcefile", type="Edm.String", filterable=True, facetable=True),
]
index = SearchIndex(
name=index_name,
fields=fields,
# semantic_search=SemanticSearch(
# configurations=[SemanticConfiguration(
# name="default",
# prioritized_fields=[SemanticPrioritizedFields(title_field=None, content_fields=[SemanticField(field_name="content")])],
# )]
# ),
vector_search=VectorSearch(
profiles=[VectorSearchProfile(
name="my-vector-config",
algorithm_configuration_name="my-hnsw")
],
algorithms=[
# VectorSearchAlgorithmConfiguration(
# name="myHnsw",
# # kind="hnsw"
# kind=VectorSearchAlgorithmKind.HNSW,
# ) # I followed the documents on official website, but it doesn't work
HnswAlgorithmConfiguration(name="my-hnsw")
]
)
)
result = client.create_index(index)
# result = client.create_or_update_index(index, allow_index_downtime=True)