1
1
import boto3
2
+ import botocore
2
3
import json
3
4
from azure .identity import DefaultAzureCredential , get_bearer_token_provider
4
5
from google .oauth2 import service_account
17
18
from foundationallm .utils import ObjectUtils
18
19
19
20
class LanguageModelFactory :
20
-
21
+
21
22
def __init__ (self , objects :dict , config : Configuration ):
22
23
self .objects = objects
23
24
self .config = config
24
-
25
+
25
26
def get_language_model (self ,
26
27
ai_model_object_id :str ,
27
28
override_operation_type : OperationTypes = None
@@ -43,24 +44,24 @@ def get_language_model(self,
43
44
if ai_model is None :
44
45
raise LangChainException ("AI model configuration settings are missing." , 400 )
45
46
46
- api_endpoint = ObjectUtils .get_object_by_id (ai_model .endpoint_object_id , self .objects , APIEndpointConfiguration )
47
+ api_endpoint = ObjectUtils .get_object_by_id (ai_model .endpoint_object_id , self .objects , APIEndpointConfiguration )
47
48
if api_endpoint is None :
48
49
raise LangChainException ("API endpoint configuration settings are missing." , 400 )
49
-
50
+
50
51
match api_endpoint .provider :
51
52
case LanguageModelProvider .MICROSOFT :
52
53
op_type = api_endpoint .operation_type
53
54
if override_operation_type is not None :
54
55
op_type = override_operation_type
55
- if api_endpoint .authentication_type == AuthenticationTypes .AZURE_IDENTITY :
56
+ if api_endpoint .authentication_type == AuthenticationTypes .AZURE_IDENTITY :
56
57
try :
57
58
scope = api_endpoint .authentication_parameters .get ('scope' , 'https://cognitiveservices.azure.com/.default' )
58
59
# Set up a Azure AD token provider.
59
60
token_provider = get_bearer_token_provider (
60
61
DefaultAzureCredential (exclude_environment_credential = True ),
61
62
scope
62
63
)
63
-
64
+
64
65
if op_type == OperationTypes .CHAT :
65
66
language_model = AzureChatOpenAI (
66
67
azure_endpoint = api_endpoint .url ,
@@ -69,27 +70,27 @@ def get_language_model(self,
69
70
azure_ad_token_provider = token_provider ,
70
71
azure_deployment = ai_model .deployment_name
71
72
)
72
- elif op_type == OperationTypes .ASSISTANTS_API or op_type == OperationTypes .IMAGE_SERVICES :
73
+ elif op_type == OperationTypes .ASSISTANTS_API or op_type == OperationTypes .IMAGE_SERVICES :
73
74
# Assistants API clients can't have deployment as that is assigned at the assistant level.
74
75
language_model = async_aoi (
75
76
azure_endpoint = api_endpoint .url ,
76
- api_version = api_endpoint .api_version ,
77
+ api_version = api_endpoint .api_version ,
77
78
azure_ad_token_provider = token_provider
78
79
)
79
80
else :
80
81
raise LangChainException (f"Unsupported operation type: { op_type } " , 400 )
81
82
82
83
except Exception as e :
83
84
raise LangChainException (f"Failed to create Azure OpenAI API connector: { str (e )} " , 500 )
84
- else : # Key-based authentication
85
- try :
86
- api_key = self .config .get_value (api_endpoint .authentication_parameters .get ('api_key_configuration_name' ))
85
+ else : # Key-based authentication
86
+ try :
87
+ api_key = self .config .get_value (api_endpoint .authentication_parameters .get ('api_key_configuration_name' ))
87
88
except Exception as e :
88
89
raise LangChainException (f"Failed to retrieve API key: { str (e )} " , 500 )
89
90
90
91
if api_key is None :
91
92
raise LangChainException ("API key is missing from the configuration settings." , 400 )
92
-
93
+
93
94
if op_type == OperationTypes .CHAT :
94
95
language_model = AzureChatOpenAI (
95
96
azure_endpoint = api_endpoint .url ,
@@ -121,6 +122,8 @@ def get_language_model(self,
121
122
else OpenAI (base_url = api_endpoint .url , api_key = api_key )
122
123
)
123
124
case LanguageModelProvider .BEDROCK :
125
+ boto3_config = botocore .config .Config (connect_timeout = 60 , read_timeout = api_endpoint .timeout_seconds )
126
+
124
127
if api_endpoint .authentication_type == AuthenticationTypes .AZURE_IDENTITY :
125
128
# Get Azure scope for federated authentication as well as the AWS role ARN (Amazon Resource Name).
126
129
try :
@@ -159,7 +162,8 @@ def get_language_model(self,
159
162
region_name = region ,
160
163
aws_access_key_id = creds ["AccessKeyId" ],
161
164
aws_secret_access_key = creds ["SecretAccessKey" ],
162
- aws_session_token = creds ["SessionToken" ]
165
+ aws_session_token = creds ["SessionToken" ],
166
+ config = boto3_config
163
167
)
164
168
else : # Key-based authentication
165
169
try :
@@ -184,10 +188,11 @@ def get_language_model(self,
184
188
model = ai_model .deployment_name ,
185
189
region_name = region ,
186
190
aws_access_key_id = access_key ,
187
- aws_secret_access_key = secret_key
191
+ aws_secret_access_key = secret_key ,
192
+ config = boto3_config
188
193
)
189
194
case LanguageModelProvider .VERTEXAI :
190
- # Only supports service account authentication via JSON credentials stored in key vault.
195
+ # Only supports service account authentication via JSON credentials stored in key vault.
191
196
# Uses the authentication parameter: service_account_credentials to get the application configuration key for this value.
192
197
try :
193
198
service_account_credentials_definition = json .loads (self .config .get_value (api_endpoint .authentication_parameters .get ('service_account_credentials' )))
@@ -197,8 +202,8 @@ def get_language_model(self,
197
202
if not service_account_credentials_definition :
198
203
raise LangChainException ("Service account credentials are missing from the configuration settings." , 400 )
199
204
200
- service_account_credentials = service_account .Credentials .from_service_account_info (service_account_credentials_definition )
201
- language_model = ChatVertexAI (
205
+ service_account_credentials = service_account .Credentials .from_service_account_info (service_account_credentials_definition )
206
+ language_model = ChatVertexAI (
202
207
model = ai_model .deployment_name ,
203
208
temperature = 0 ,
204
209
max_tokens = None ,
@@ -211,5 +216,5 @@ def get_language_model(self,
211
216
for key , value in ai_model .model_parameters .items ():
212
217
if hasattr (language_model , key ):
213
218
setattr (language_model , key , value )
214
-
219
+
215
220
return language_model
0 commit comments