datarobot-moderations 11.1.15__py3-none-any.whl → 11.1.16__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
datarobot_dome/guard.py CHANGED
@@ -49,11 +49,13 @@ from datarobot_dome.constants import GuardStage
49
49
  from datarobot_dome.constants import GuardTimeoutAction
50
50
  from datarobot_dome.constants import GuardType
51
51
  from datarobot_dome.constants import OOTBType
52
+ from datarobot_dome.guard_helpers import DEFAULT_OPEN_AI_API_VERSION
52
53
  from datarobot_dome.guard_helpers import ModerationDeepEvalLLM
53
54
  from datarobot_dome.guard_helpers import get_azure_openai_client
54
55
  from datarobot_dome.guard_helpers import get_chat_nvidia_llm
55
56
  from datarobot_dome.guard_helpers import get_datarobot_endpoint_and_token
56
- from datarobot_dome.guard_helpers import try_to_fallback_to_llm_gateway
57
+ from datarobot_dome.guard_helpers import get_llm_gateway_client
58
+ from datarobot_dome.guard_helpers import use_llm_gateway_inference
57
59
  from datarobot_dome.guards.guard_llm_mixin import GuardLLMMixin
58
60
 
59
61
  MAX_GUARD_NAME_LENGTH = 255
@@ -142,6 +144,7 @@ guard_intervention_trafaret = t.Dict(
142
144
  additional_guard_config_trafaret = t.Dict(
143
145
  {
144
146
  t.Key("cost", to_name="cost", optional=True): t.Or(cost_metric_trafaret, t.Null),
147
+ t.Key("tool_call", to_name="tool_call", optional=True): t.Or(t.Any(), t.Null),
145
148
  }
146
149
  )
147
150
 
@@ -484,12 +487,18 @@ class NeMoGuard(Guard, GuardLLMMixin):
484
487
  self.openai_api_base = config.get("openai_api_base")
485
488
  self.openai_deployment_id = config.get("openai_deployment_id")
486
489
  llm_id = None
490
+ credentials = None
491
+ use_llm_gateway = use_llm_gateway_inference(self._llm_type)
487
492
  try:
488
493
  self.openai_api_key = self.get_openai_api_key(config, self._llm_type)
489
494
  if self._llm_type != GuardLLMType.NIM and self.openai_api_key is None:
490
495
  raise ValueError("OpenAI API key is required for NeMo Guardrails")
491
496
 
492
497
  if self.llm_type == GuardLLMType.OPENAI:
498
+ credentials = {
499
+ "credential_type": "openai",
500
+ "api_key": self.openai_api_key,
501
+ }
493
502
  os.environ["OPENAI_API_KEY"] = self.openai_api_key
494
503
  llm = None
495
504
  elif self.llm_type == GuardLLMType.AZURE_OPENAI:
@@ -497,6 +506,12 @@ class NeMoGuard(Guard, GuardLLMMixin):
497
506
  raise ValueError("Azure OpenAI API base url is required for LLM Guard")
498
507
  if self.openai_deployment_id is None:
499
508
  raise ValueError("Azure OpenAI deployment ID is required for LLM Guard")
509
+ credentials = {
510
+ "credential_type": "azure_openai",
511
+ "api_base": self.openai_api_base,
512
+ "api_version": DEFAULT_OPEN_AI_API_VERSION,
513
+ "api_key": self.openai_api_key,
514
+ }
500
515
  azure_openai_client = get_azure_openai_client(
501
516
  openai_api_key=self.openai_api_key,
502
517
  openai_api_base=self.openai_api_base,
@@ -537,15 +552,20 @@ class NeMoGuard(Guard, GuardLLMMixin):
537
552
  raise ValueError(f"Invalid LLMType: {self.llm_type}")
538
553
 
539
554
  except Exception as e:
540
- llm = try_to_fallback_to_llm_gateway(
541
- # Currently only OPENAI and AZURE_OPENAI are supported by NeMoGuard
542
- # For Bedrock and Vertex the model in the config is actually the LLM ID
543
- # For OpenAI we use the default model defined in get_llm_gateway_client
544
- # For Azure we use the deployment ID
555
+ # no valid user credentials provided, raise if not using LLM Gateway
556
+ credentials = None
557
+ if not use_llm_gateway:
558
+ raise e
559
+
560
+ if use_llm_gateway:
561
+ # Currently only OPENAI and AZURE_OPENAI are supported by NeMoGuard
562
+ # For Bedrock and Vertex the model in the config is actually the LLM ID
563
+ # For OpenAI we use the default model defined in get_llm_gateway_client
564
+ # For Azure we use the deployment ID
565
+ llm = get_llm_gateway_client(
545
566
  llm_id=llm_id,
546
567
  openai_deployment_id=self.openai_deployment_id,
547
- llm_type=self.llm_type,
548
- e=e,
568
+ credentials=credentials,
549
569
  )
550
570
 
551
571
  # Use guard stage to determine whether to read from prompt/response subdirectory
@@ -47,7 +47,7 @@ from datarobot_dome.llm import DataRobotLLM
47
47
  # but for ROUGE-1 guard, UI allows the user to configure value between
48
48
  # 0 and 1, so making scaling factor 1.
49
49
  SCALING_FACTOR = 1
50
- DEFAULT_OPEN_AI_API_VERSION = "2023-03-15-preview"
50
+ DEFAULT_OPEN_AI_API_VERSION = "2024-10-21"
51
51
 
52
52
  _logger = logging.getLogger(LOGGER_NAME_PREFIX + ".guard_helpers")
53
53
 
@@ -195,8 +195,10 @@ def get_llm_gateway_client(
195
195
  model: str | None = None,
196
196
  llm_id: str | None = None,
197
197
  openai_deployment_id: str | None = None,
198
+ credentials: dict | None = None,
198
199
  ) -> ChatOpenAI:
199
200
  """The LLM gateway client enables chat completions with DR provided credentials and metering.
201
+ User provided credentials are optional and passed to the completion request as json string.
200
202
 
201
203
  Providing model is always required due to openai's chat api.
202
204
  llm_id and deployment_id override model if provided.
@@ -208,7 +210,8 @@ def get_llm_gateway_client(
208
210
  model=model or "azure/gpt-4o",
209
211
  api_key=datarobot_api_token,
210
212
  base_url=f"{datarobot_endpoint}/genai/llmgw",
211
- max_retries=0, # retries are handled by the LLM Gateway
213
+ # retries are handled by the LLM Gateway
214
+ max_retries=0,
212
215
  default_headers={
213
216
  # used for metering
214
217
  "Client-Id": "moderations",
@@ -217,28 +220,24 @@ def get_llm_gateway_client(
217
220
  # optional model overrides
218
221
  "deployment_id": openai_deployment_id,
219
222
  "llm_id": llm_id,
223
+ # optional user provided credentials
224
+ "credential_json": json.dumps(credentials) if credentials else None,
220
225
  },
221
226
  )
222
227
  return client
223
228
 
224
229
 
225
- def try_to_fallback_to_llm_gateway(
226
- llm_id: str | None,
227
- openai_deployment_id: str | None,
228
- llm_type: GuardLLMType,
229
- e: Exception,
230
- ) -> ChatOpenAI:
231
- # USE the LLM gateway if its runtime parameter is available and enabled
232
- # DO NOT USE the gateway if user provided credentials are specified
233
- # which is the case if no exception was raised trying to create the LLM
234
- # DATAROBOT and NIM LLM types are not supported by the gateway
235
- if not json.loads(os.environ.get("ENABLE_LLM_GATEWAY_INFERENCE", "false")) or llm_type in [
230
+ def use_llm_gateway_inference(llm_type: GuardLLMType):
231
+ """
232
+ USE the LLM gateway if its runtime parameter is available and enabled
233
+ DATAROBOT and NIM LLM types are not supported by the gateway
234
+ """
235
+ if json.loads(os.environ.get("ENABLE_LLM_GATEWAY_INFERENCE", "false")) and llm_type not in [
236
236
  GuardLLMType.DATAROBOT,
237
237
  GuardLLMType.NIM,
238
238
  ]:
239
- raise e
240
- llm = get_llm_gateway_client(llm_id=llm_id, openai_deployment_id=openai_deployment_id)
241
- return llm
239
+ return True
240
+ return False
242
241
 
243
242
 
244
243
  def get_azure_openai_client(
@@ -22,11 +22,13 @@ from datarobot_dome.constants import SECRET_DEFINITION_PREFIX
22
22
  from datarobot_dome.constants import GuardLLMType
23
23
  from datarobot_dome.constants import GuardType
24
24
  from datarobot_dome.constants import OOTBType
25
+ from datarobot_dome.guard_helpers import DEFAULT_OPEN_AI_API_VERSION
25
26
  from datarobot_dome.guard_helpers import get_azure_openai_client
26
27
  from datarobot_dome.guard_helpers import get_bedrock_client
27
28
  from datarobot_dome.guard_helpers import get_datarobot_llm
29
+ from datarobot_dome.guard_helpers import get_llm_gateway_client
28
30
  from datarobot_dome.guard_helpers import get_vertex_client
29
- from datarobot_dome.guard_helpers import try_to_fallback_to_llm_gateway
31
+ from datarobot_dome.guard_helpers import use_llm_gateway_inference
30
32
 
31
33
  basic_credential_trafaret = t.Dict(
32
34
  {
@@ -156,6 +158,8 @@ class GuardLLMMixin:
156
158
  openai_api_base = config.get("openai_api_base")
157
159
  openai_deployment_id = config.get("openai_deployment_id")
158
160
  llm_id = None
161
+ credentials = None
162
+ use_llm_gateway = use_llm_gateway_inference(llm_type)
159
163
  try:
160
164
  if llm_type in [GuardLLMType.OPENAI, GuardLLMType.AZURE_OPENAI]:
161
165
  openai_api_key = self.get_openai_api_key(config, llm_type)
@@ -163,6 +167,10 @@ class GuardLLMMixin:
163
167
  raise ValueError("OpenAI API key is required for Faithfulness guard")
164
168
 
165
169
  if llm_type == GuardLLMType.OPENAI:
170
+ credentials = {
171
+ "credential_type": "openai",
172
+ "api_key": openai_api_key,
173
+ }
166
174
  os.environ["OPENAI_API_KEY"] = openai_api_key
167
175
  llm = "default"
168
176
  elif llm_type == GuardLLMType.AZURE_OPENAI:
@@ -170,6 +178,12 @@ class GuardLLMMixin:
170
178
  raise ValueError("OpenAI API base url is required for LLM Guard")
171
179
  if openai_deployment_id is None:
172
180
  raise ValueError("OpenAI deployment ID is required for LLM Guard")
181
+ credentials = {
182
+ "credential_type": "azure_openai",
183
+ "api_key": openai_api_key,
184
+ "api_base": openai_api_base,
185
+ "api_version": DEFAULT_OPEN_AI_API_VERSION,
186
+ }
173
187
  azure_openai_client = get_azure_openai_client(
174
188
  openai_api_key=openai_api_key,
175
189
  openai_api_base=openai_api_base,
@@ -182,9 +196,15 @@ class GuardLLMMixin:
182
196
  raise ValueError("Google model is required for LLM Guard")
183
197
  if config.get("google_region") is None:
184
198
  raise ValueError("Google region is required for LLM Guard")
199
+ service_account_info = self.get_google_service_account(config)
200
+ credentials = {
201
+ "credential_type": "google_vertex_ai",
202
+ "region": config["google_region"],
203
+ "service_account_info": service_account_info,
204
+ }
185
205
  llm = get_vertex_client(
186
206
  google_model=llm_id,
187
- google_service_account=self.get_google_service_account(config),
207
+ google_service_account=service_account_info,
188
208
  google_region=config["google_region"],
189
209
  )
190
210
  elif llm_type == GuardLLMType.AMAZON:
@@ -194,6 +214,13 @@ class GuardLLMMixin:
194
214
  if config.get("aws_region") is None:
195
215
  raise ValueError("AWS region is required for LLM Guard")
196
216
  credential_config = self.get_aws_account(config)
217
+ credentials = {
218
+ "credential_type": "amazon_bedrock",
219
+ "access_key_id": credential_config["aws_access_key_id"],
220
+ "secret_access_key": credential_config["aws_secret_access_key"],
221
+ "session_token": credential_config["aws_session_token"],
222
+ "region": config["aws_region"],
223
+ }
197
224
  llm = get_bedrock_client(
198
225
  aws_model=llm_id,
199
226
  aws_access_key_id=credential_config["aws_access_key_id"],
@@ -219,14 +246,19 @@ class GuardLLMMixin:
219
246
  raise ValueError(f"Invalid LLMType: {llm_type}")
220
247
 
221
248
  except Exception as e:
222
- llm = try_to_fallback_to_llm_gateway(
223
- # For Bedrock and Vertex the model in the config is actually the LLM ID
224
- # For OpenAI we use the default model defined in get_llm_gateway_client
225
- # For Azure we use the deployment ID
249
+ # no valid user credentials provided, raise if not using LLM Gateway
250
+ credentials = None
251
+ if not use_llm_gateway:
252
+ raise e
253
+
254
+ if use_llm_gateway:
255
+ # For Bedrock and Vertex the model in the config is actually the LLM ID
256
+ # For OpenAI we use the default model defined in get_llm_gateway_client
257
+ # For Azure we use the deployment ID
258
+ llm = get_llm_gateway_client(
226
259
  llm_id=llm_id,
227
260
  openai_deployment_id=openai_deployment_id,
228
- llm_type=llm_type,
229
- e=e,
261
+ credentials=credentials,
230
262
  )
231
263
 
232
264
  return llm
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: datarobot-moderations
3
- Version: 11.1.15
3
+ Version: 11.1.16
4
4
  Summary: DataRobot Monitoring and Moderation framework
5
5
  License: DataRobot Tool and Utility Agreement
6
6
  Author: DataRobot
@@ -3,11 +3,11 @@ datarobot_dome/async_http_client.py,sha256=wkB4irwvnchNGzO1bk2C_HWM-GOSB3AUn5TXK
3
3
  datarobot_dome/chat_helper.py,sha256=BzvtUyZSZxzOqq-5a2wQKhHhr2kMlcP1MFrHaDAeD_o,9671
4
4
  datarobot_dome/constants.py,sha256=mnSa8rUAha4XlsS2lwPmFCkH2RzfSL_MMkErsWHqIbA,9040
5
5
  datarobot_dome/drum_integration.py,sha256=nULpLYVMiS5vihfNUyuq-nvZpgXrQibQbVu2UMAscu8,42102
6
- datarobot_dome/guard.py,sha256=7T0a1gsWqVmVvEf4SLkVBi8lIRYl8PeMB7TnQGszWtc,32371
6
+ datarobot_dome/guard.py,sha256=1INYx17n9ToiB5bzI-jIReUUuqkK_ucxpOx4jQLts6g,33264
7
7
  datarobot_dome/guard_executor.py,sha256=AOI8MZeZETHMoFgBePe0wa2vE9d2975MYQnEDHLZL7s,35462
8
- datarobot_dome/guard_helpers.py,sha256=YHhSUSuvxAgDdWPXiwYiHtrl-6ZlObE9n6CjYPQNSuA,16375
8
+ datarobot_dome/guard_helpers.py,sha256=Bfdi8gow2_TAVzRHYUDqEfcG5bWx2KR1dnpxt9E850Y,16311
9
9
  datarobot_dome/guards/__init__.py,sha256=B5Rx8_CNCNsOpxBbRj27XOXCfRZmvmrAR-NzlzIKnDw,583
10
- datarobot_dome/guards/guard_llm_mixin.py,sha256=ON-zuVL3xhQmXv0rFkalWrW_Q67Wwya2IQerHO8WkKU,10694
10
+ datarobot_dome/guards/guard_llm_mixin.py,sha256=VovlpNZjWIGamF4SSvLF5lzOFyApH5IoOiB_qtCmRg0,12216
11
11
  datarobot_dome/llm.py,sha256=L02OvTrflmD34-FrfXebfF-zzKTeuin7fpne1Cl5psg,5719
12
12
  datarobot_dome/metrics/__init__.py,sha256=B5Rx8_CNCNsOpxBbRj27XOXCfRZmvmrAR-NzlzIKnDw,583
13
13
  datarobot_dome/metrics/citation_metrics.py,sha256=q0hTMWuk6wy_jqk2UjFPON3kU94HN3W2vxr9giJ8O8E,3544
@@ -18,6 +18,6 @@ datarobot_dome/pipeline/llm_pipeline.py,sha256=fOp_OJnQMDUJH-LKv12kEqli-EqfHjAiS
18
18
  datarobot_dome/pipeline/pipeline.py,sha256=_pZ_4K2LMnfYCYj_ur9EwJzo3T-pbO6lFYz1O-_3uQ4,16491
19
19
  datarobot_dome/pipeline/vdb_pipeline.py,sha256=WTOGn1qe_ZvEcdlvHgeXxl2xTqp7GjfL13c6S-FmAfM,5146
20
20
  datarobot_dome/streaming.py,sha256=6nYvh6SoxPRLfO6GGdEoHsQuyLP9oX1lDMe8IeGo4lw,17801
21
- datarobot_moderations-11.1.15.dist-info/METADATA,sha256=zHt26VnmHpn-0cL-egKPqdcTvKPTittBNtVHLVylbHo,4827
22
- datarobot_moderations-11.1.15.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
23
- datarobot_moderations-11.1.15.dist-info/RECORD,,
21
+ datarobot_moderations-11.1.16.dist-info/METADATA,sha256=WBOhNlF-pwpbJ2PvlkhwqlKbF3vtWkbNdu6vjqvmvaQ,4827
22
+ datarobot_moderations-11.1.16.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
23
+ datarobot_moderations-11.1.16.dist-info/RECORD,,