7
7
from azure .core .credentials import AzureKeyCredential
8
8
from azure .search .documents .indexes .models import SearchIndexerDataUserAssignedIdentity
9
9
10
+
10
11
class IndexerType (Enum ):
11
12
"""The type of the indexer"""
12
13
13
14
RAG_DOCUMENTS = "rag-documents"
14
15
16
+
15
17
class IdentityType (Enum ):
16
18
"""The type of the indexer"""
17
19
18
20
USER_ASSIGNED = "user_assigned"
19
21
SYSTEM_ASSIGNED = "system_assigned"
20
22
KEY = "key"
21
23
24
+
22
25
class AISearchEnvironment :
23
26
"""This class is used to get the environment variables for the AI search service."""
27
+
24
28
def __init__ (self , indexer_type : IndexerType ):
25
29
"""Initialize the AISearchEnvironment class.
26
30
@@ -33,7 +37,7 @@ def __init__(self, indexer_type: IndexerType):
33
37
@property
34
38
def normalised_indexer_type (self ) -> str :
35
39
"""This function returns the normalised indexer type.
36
-
40
+
37
41
Returns:
38
42
str: The normalised indexer type
39
43
"""
@@ -46,7 +50,7 @@ def normalised_indexer_type(self) -> str:
46
50
@property
47
51
def identity_type (self ) -> IdentityType :
48
52
"""This function returns the identity type.
49
-
53
+
50
54
Returns:
51
55
IdentityType: The identity type
52
56
"""
@@ -60,54 +64,100 @@ def identity_type(self) -> IdentityType:
60
64
return IdentityType .KEY
61
65
else :
62
66
raise ValueError ("Invalid identity type" )
63
-
67
+
64
68
@property
65
69
def ai_search_endpoint (self ) -> str :
66
70
"""This function returns the ai search endpoint.
67
-
71
+
68
72
Returns:
69
73
str: The ai search endpoint
70
74
"""
71
75
return os .environ .get ("AIService__AzureSearchOptions__Endpoint" )
72
-
76
+
73
77
@property
74
78
def ai_search_identity_id (self ) -> str :
75
79
"""This function returns the ai search identity id.
76
-
80
+
77
81
Returns:
78
82
str: The ai search identity id
79
83
"""
80
84
return os .environ .get ("AIService__AzureSearchOptions__Identity__ClientId" )
81
-
85
+
82
86
@property
83
87
def ai_search_user_assigned_identity (self ) -> SearchIndexerDataUserAssignedIdentity :
84
88
"""This function returns the ai search user assigned identity.
85
-
89
+
86
90
Returns:
87
- SearchIndexerDataUserAssignedIdentity: The ai search user assigned identity"""
91
+ SearchIndexerDataUserAssignedIdentity: The ai search user assigned identity
92
+ """
88
93
user_assigned_identity = SearchIndexerDataUserAssignedIdentity (
89
- user_assigned_identity = os .environ .get ("AIService__AzureSearchOptions__Identity__FQName" )
94
+ user_assigned_identity = os .environ .get (
95
+ "AIService__AzureSearchOptions__Identity__FQName"
96
+ )
90
97
)
91
98
return user_assigned_identity
92
99
93
100
@property
94
101
def ai_search_credential (self ) -> DefaultAzureCredential | AzureKeyCredential :
95
102
"""This function returns the ai search credential.
96
-
103
+
97
104
Returns:
98
105
DefaultAzureCredential | AzureKeyCredential: The ai search credential
99
106
"""
100
107
if self .identity_type in IdentityType .SYSTEM_ASSIGNED :
101
108
return DefaultAzureCredential ()
102
109
elif self .identity_type in IdentityType .USER_ASSIGNED :
103
- return DefaultAzureCredential (managed_identity_client_id = self .ai_search_identity_id )
110
+ return DefaultAzureCredential (
111
+ managed_identity_client_id = self .ai_search_identity_id
112
+ )
104
113
else :
105
- return AzureKeyCredential (os .environ .get ("AIService__AzureSearchOptions__Key" ))
114
+ return AzureKeyCredential (
115
+ os .environ .get ("AIService__AzureSearchOptions__Key" )
116
+ )
117
+
118
+ @property
119
+ def open_ai_api_key (self ) -> str :
120
+ """This function returns the open ai api key.
121
+
122
+ Returns:
123
+ str: The open ai api key
124
+ """
125
+ return os .environ .get ("OpenAI__ApiKey" )
126
+
127
+ @property
128
+ def open_ai_endpoint (self ) -> str :
129
+ """This function returns the open ai endpoint.
130
+
131
+ Returns:
132
+ str: The open ai endpoint
133
+ """
134
+ return os .environ .get ("OpenAI__Endpoint" )
135
+
136
+ @property
137
+ def open_ai_embedding_model (self ) -> str :
138
+ """This function returns the open ai embedding model.
139
+
140
+ Returns:
141
+ str: The open ai embedding model
142
+ """
143
+ return os .environ .get ("OpenAI__EmbeddingModel" )
144
+
145
+ @property
146
+ def open_ai_embedding_deployment (self ) -> str :
147
+ """This function returns the open ai embedding deployment.
148
+
149
+ Returns:
150
+ str: The open ai embedding deployment
151
+ """
152
+ return os .environ .get ("OpenAI__EmbeddingDeployment" )
106
153
107
154
@property
108
155
def storage_account_connection_string (self ) -> str :
109
156
"""This function returns the blob connection string. If the identity type is user_assigned or system_assigned, it returns the FQEndpoint, otherwise it returns the ConnectionString"""
110
- if self .identity_type in [IdentityType .SYSTEM_ASSIGNED , IdentityType .USER_ASSIGNED ]:
157
+ if self .identity_type in [
158
+ IdentityType .SYSTEM_ASSIGNED ,
159
+ IdentityType .USER_ASSIGNED ,
160
+ ]:
111
161
return os .environ .get ("StorageAccount__FQEndpoint" )
112
162
else :
113
163
return os .environ .get ("StorageAccount__ConnectionString" )
@@ -118,8 +168,10 @@ def storage_account_blob_container_name(self) -> str:
118
168
This function returns azure blob container name
119
169
"""
120
170
121
- return os .environ .get (f"StorageAccount__{ self .normalised_indexer_type } __Container" )
122
-
171
+ return os .environ .get (
172
+ f"StorageAccount__{ self .normalised_indexer_type } __Container"
173
+ )
174
+
123
175
@property
124
176
def function_app_end_point (self ) -> str :
125
177
"""
@@ -133,14 +185,14 @@ def function_app_key(self) -> str:
133
185
This function returns function app key
134
186
"""
135
187
return os .environ .get ("FunctionApp__Key" )
136
-
188
+
137
189
@property
138
190
def function_app_app_registration_resource_id (self ) -> str :
139
191
"""
140
192
This function returns function app app registration resource id
141
193
"""
142
194
return os .environ .get ("FunctionApp__AppRegistrationResourceId" )
143
-
195
+
144
196
@property
145
197
def function_app_pre_embedding_cleaner_route (self ) -> str :
146
198
"""
@@ -161,26 +213,27 @@ def function_app_key_phrase_extractor_route(self) -> str:
161
213
This function returns function app keyphrase extractor name
162
214
"""
163
215
return os .environ .get ("FunctionApp__KeyPhraseExtractor__FunctionName" )
164
-
216
+
165
217
@property
166
- def ai_search_embedding_model_dimensions (self ) -> str :
218
+ def open_ai_embedding_dimensions (self ) -> str :
167
219
"""
168
220
This function returns dimensions for embedding model.
169
221
170
222
Returns:
171
223
str: The dimensions for embedding model
172
224
"""
173
225
174
- return os .environ .get (
175
- f"AIService__AzureSearchOptions__{ self .normalised_indexer_type } __EmbeddingDimensions"
176
- )
177
-
226
+ return os .environ .get ("OpenAI__EmbeddingDimensions" )
227
+
178
228
@property
179
229
def use_private_endpoint (self ) -> bool :
180
230
"""
181
231
This function returns true if private endpoint is used
182
232
"""
183
- return os .environ .get ("AIService__AzureSearchOptions__UsePrivateEndpoint" ) == "true"
233
+ return (
234
+ os .environ .get ("AIService__AzureSearchOptions__UsePrivateEndpoint" )
235
+ == "true"
236
+ )
184
237
185
238
def get_custom_skill_function_url (self , skill_type : str ):
186
239
"""
@@ -194,7 +247,9 @@ def get_custom_skill_function_url(self, skill_type: str):
194
247
route = self .function_app_key_phrase_extractor_route
195
248
else :
196
249
raise ValueError (f"Invalid skill type: { skill_type } " )
197
-
198
- full_url = f"{ self .function_app_end_point } /api/{ route } ?code={ self .function_app_key } "
199
250
200
- return full_url
251
+ full_url = (
252
+ f"{ self .function_app_end_point } /api/{ route } ?code={ self .function_app_key } "
253
+ )
254
+
255
+ return full_url
0 commit comments