datarobot-moderations 11.1.12__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/__init__.py +11 -0
- datarobot_dome/async_http_client.py +248 -0
- datarobot_dome/chat_helper.py +227 -0
- datarobot_dome/constants.py +318 -0
- datarobot_dome/drum_integration.py +977 -0
- datarobot_dome/guard.py +736 -0
- datarobot_dome/guard_executor.py +755 -0
- datarobot_dome/guard_helpers.py +457 -0
- datarobot_dome/guards/__init__.py +11 -0
- datarobot_dome/guards/guard_llm_mixin.py +232 -0
- datarobot_dome/llm.py +148 -0
- datarobot_dome/metrics/__init__.py +11 -0
- datarobot_dome/metrics/citation_metrics.py +98 -0
- datarobot_dome/metrics/factory.py +52 -0
- datarobot_dome/metrics/metric_scorer.py +78 -0
- datarobot_dome/pipeline/__init__.py +11 -0
- datarobot_dome/pipeline/llm_pipeline.py +474 -0
- datarobot_dome/pipeline/pipeline.py +376 -0
- datarobot_dome/pipeline/vdb_pipeline.py +127 -0
- datarobot_dome/streaming.py +395 -0
- datarobot_moderations-11.1.12.dist-info/METADATA +113 -0
- datarobot_moderations-11.1.12.dist-info/RECORD +23 -0
- datarobot_moderations-11.1.12.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# ---------------------------------------------------------------------------------
|
|
2
|
+
# Copyright (c) 2025 DataRobot, Inc. and its affiliates. All rights reserved.
|
|
3
|
+
# Last updated 2025.
|
|
4
|
+
#
|
|
5
|
+
# DataRobot, Inc. Confidential.
|
|
6
|
+
# This is proprietary source code of DataRobot, Inc. and its affiliates.
|
|
7
|
+
#
|
|
8
|
+
# This file and its contents are subject to DataRobot Tool and Utility Agreement.
|
|
9
|
+
# For details, see
|
|
10
|
+
# https://www.datarobot.com/wp-content/uploads/2021/07/DataRobot-Tool-and-Utility-Agreement.pdf.
|
|
11
|
+
# ---------------------------------------------------------------------------------
|
|
12
|
+
from enum import Enum
|
|
13
|
+
from typing import Self
|
|
14
|
+
|
|
15
|
+
__GUARD_ASSOCIATION_IDS_COLUMN_NAME__ = "datarobot_guard_association_id"
|
|
16
|
+
|
|
17
|
+
LOGGER_NAME_PREFIX = "moderations"
|
|
18
|
+
|
|
19
|
+
DEFAULT_PROMPT_COLUMN_NAME = "promptText"
|
|
20
|
+
DEFAULT_RESPONSE_COLUMN_NAME = "completion"
|
|
21
|
+
|
|
22
|
+
NEMO_GUARDRAILS_DIR = "nemo_guardrails"
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
TOKEN_COUNT_COLUMN_NAME = "token_count"
|
|
26
|
+
ROUGE_1_COLUMN_NAME = "rouge_1"
|
|
27
|
+
NEMO_GUARD_COLUMN_NAME = "nemo_guard"
|
|
28
|
+
COST_COLUMN_NAME = "cost"
|
|
29
|
+
FAITHFULLNESS_COLUMN_NAME = "faithfulness"
|
|
30
|
+
AGENT_GOAL_ACCURACY_COLUMN_NAME = "agent_goal_accuracy"
|
|
31
|
+
TASK_ADHERENCE_SCORE_COLUMN_NAME = "task_adherence_score"
|
|
32
|
+
|
|
33
|
+
CUSTOM_METRIC_DESCRIPTION_SUFFIX = "Created by DataRobot Moderation System"
|
|
34
|
+
|
|
35
|
+
# Setting timeout at 10 sec, we have 5 retries, so we approximately wait for
|
|
36
|
+
# 50 sec, before giving up on the guard.
|
|
37
|
+
DEFAULT_GUARD_PREDICTION_TIMEOUT_IN_SEC = 10
|
|
38
|
+
|
|
39
|
+
# Connect and read retries count
|
|
40
|
+
RETRY_COUNT = 10
|
|
41
|
+
|
|
42
|
+
MODERATION_CONFIG_FILE_NAME = "moderation_config.yaml"
|
|
43
|
+
DATAROBOT_SERVERLESS_PLATFORM = "datarobotServerless"
|
|
44
|
+
|
|
45
|
+
SECRET_DEFINITION_PREFIX = "MLOPS_RUNTIME_PARAM_MODERATION"
|
|
46
|
+
OPENAI_SECRET_DEFINITION_SUFFIX = "OPENAI_API_KEY"
|
|
47
|
+
GOOGLE_SERVICE_ACCOUNT_SECRET_DEFINITION_SUFFIX = "GOOGLE_SERVICE_ACCOUNT"
|
|
48
|
+
AWS_ACCOUNT_SECRET_DEFINITION_SUFFIX = "AWS_ACCOUNT"
|
|
49
|
+
|
|
50
|
+
NONE_CUSTOM_PY_RESPONSE = "None (No response was generated by LLM)"
|
|
51
|
+
|
|
52
|
+
MODERATION_MODEL_NAME = "DataRobot Moderation"
|
|
53
|
+
CHAT_COMPLETION_OBJECT = "chat.completion"
|
|
54
|
+
CHAT_COMPLETION_CHUNK_OBJECT = "chat.completion.chunk"
|
|
55
|
+
DATAROBOT_MODERATIONS_ATTR = "datarobot_moderations"
|
|
56
|
+
CITATIONS_ATTR = "citations"
|
|
57
|
+
USAGE_ATTR = "usage"
|
|
58
|
+
AGENTIC_PIPELINE_INTERACTIONS_ATTR = "pipeline_interactions"
|
|
59
|
+
LLM_BLUEPRINT_ID_ATTR = "llm_blueprint_id"
|
|
60
|
+
LLM_PROVIDER_GUARDS_ATTR = "llm_provider_guards"
|
|
61
|
+
PROMPT_VECTOR_ATTR = "prompt_vector"
|
|
62
|
+
|
|
63
|
+
DATAROBOT_CONFIGURED_ON_PREM_ST_SAAS_URL = "http://datarobot-nginx/api/v2"
|
|
64
|
+
DATAROBOT_ACTUAL_ON_PREM_ST_SAAS_URL = "http://datarobot-prediction-server:80/predApi/v1.0"
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
DISABLE_MODERATION_RUNTIME_PARAM_NAME = "MLOPS_RUNTIME_PARAM_DISABLE_MODERATION"
|
|
68
|
+
|
|
69
|
+
LLM_CONTEXT_COLUMN_NAME = "_LLM_CONTEXT"
|
|
70
|
+
PROMPT_TOKEN_COUNT_COLUMN_NAME_FROM_USAGE = "prompt_token_count_from_usage"
|
|
71
|
+
RESPONSE_TOKEN_COUNT_COLUMN_NAME_FROM_USAGE = "response_token_count_from_usage"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class TargetType(str, Enum):
|
|
75
|
+
"""Target types that may be handed to moderations from DRUM -- casing must align."""
|
|
76
|
+
|
|
77
|
+
BINARY = "binary"
|
|
78
|
+
REGRESSION = "regression"
|
|
79
|
+
ANOMALY = "anomaly"
|
|
80
|
+
UNSTRUCTURED = "unstructured"
|
|
81
|
+
MULTICLASS = "multiclass"
|
|
82
|
+
TRANSFORM = "transform"
|
|
83
|
+
TEXT_GENERATION = "textgeneration"
|
|
84
|
+
GEO_POINT = "geopoint"
|
|
85
|
+
VECTOR_DATABASE = "vectordatabase"
|
|
86
|
+
AGENTIC_WORKFLOW = "agenticworkflow"
|
|
87
|
+
|
|
88
|
+
@staticmethod
|
|
89
|
+
def guards() -> list[Self]:
|
|
90
|
+
return [TargetType.TEXT_GENERATION, TargetType.AGENTIC_WORKFLOW]
|
|
91
|
+
|
|
92
|
+
@staticmethod
|
|
93
|
+
def vdb() -> list[Self]:
|
|
94
|
+
return [TargetType.VECTOR_DATABASE]
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class GuardType:
|
|
98
|
+
OOTB = "ootb" # Out of the Box
|
|
99
|
+
MODEL = "model" # wraps a deployed model
|
|
100
|
+
NEMO_GUARDRAILS = "nemo_guardrails" # Nemo guardrails
|
|
101
|
+
|
|
102
|
+
ALL = [MODEL, NEMO_GUARDRAILS, OOTB]
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class OOTBType:
|
|
106
|
+
TOKEN_COUNT = "token_count"
|
|
107
|
+
ROUGE_1 = "rouge_1"
|
|
108
|
+
FAITHFULNESS = "faithfulness"
|
|
109
|
+
AGENT_GOAL_ACCURACY = "agent_goal_accuracy"
|
|
110
|
+
CUSTOM_METRIC = "custom_metric"
|
|
111
|
+
COST = "cost"
|
|
112
|
+
TASK_ADHERENCE = "task_adherence"
|
|
113
|
+
|
|
114
|
+
ALL = [
|
|
115
|
+
TOKEN_COUNT,
|
|
116
|
+
ROUGE_1,
|
|
117
|
+
FAITHFULNESS,
|
|
118
|
+
CUSTOM_METRIC,
|
|
119
|
+
COST,
|
|
120
|
+
AGENT_GOAL_ACCURACY,
|
|
121
|
+
TASK_ADHERENCE,
|
|
122
|
+
]
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
class CostCurrency:
|
|
126
|
+
USD = "USD"
|
|
127
|
+
|
|
128
|
+
ALL = [USD]
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
class GuardStage:
|
|
132
|
+
"""When can this guard operate?"""
|
|
133
|
+
|
|
134
|
+
PROMPT = "prompt"
|
|
135
|
+
RESPONSE = "response"
|
|
136
|
+
|
|
137
|
+
ALL = [PROMPT, RESPONSE]
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class GuardExecutionLocation(Enum):
|
|
141
|
+
LOCAL = "local"
|
|
142
|
+
REMOTE = "remote"
|
|
143
|
+
|
|
144
|
+
ALL = [LOCAL, REMOTE]
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
class GuardAction:
|
|
148
|
+
"""
|
|
149
|
+
Defines actions a guard can take.
|
|
150
|
+
All guards report their decisions; 'report' means do nothing else.
|
|
151
|
+
"""
|
|
152
|
+
|
|
153
|
+
BLOCK = "block"
|
|
154
|
+
REPORT = "report"
|
|
155
|
+
REPLACE = "replace"
|
|
156
|
+
NONE = None
|
|
157
|
+
|
|
158
|
+
ALL = [BLOCK, REPORT, REPLACE, NONE]
|
|
159
|
+
|
|
160
|
+
@classmethod
|
|
161
|
+
def possible_column_names(cls, input_column_name):
|
|
162
|
+
return [
|
|
163
|
+
f"{cls.BLOCK}ed_{input_column_name}",
|
|
164
|
+
f"{cls.REPORT}ed_{input_column_name}",
|
|
165
|
+
f"{cls.REPLACE}d_{input_column_name}",
|
|
166
|
+
]
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
class GuardModelTargetType:
|
|
170
|
+
"""
|
|
171
|
+
Guards support a subset of DataRobot model target types.
|
|
172
|
+
Ref: common.engine.TargetType
|
|
173
|
+
"""
|
|
174
|
+
|
|
175
|
+
BINARY = "Binary"
|
|
176
|
+
REGRESSION = "Regression"
|
|
177
|
+
MULTICLASS = "Multiclass"
|
|
178
|
+
TEXT_GENERATION = "TextGeneration"
|
|
179
|
+
|
|
180
|
+
ALL = [BINARY, REGRESSION, MULTICLASS, TEXT_GENERATION]
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
class GuardOperatorType:
|
|
184
|
+
"""
|
|
185
|
+
Defines what the guard should do with the metric or prediction.
|
|
186
|
+
Applies when the parameter type is OPERATOR.
|
|
187
|
+
Typically this compares against a number, or looks for a matching category or string.
|
|
188
|
+
"""
|
|
189
|
+
|
|
190
|
+
GREATER_THAN = "greaterThan"
|
|
191
|
+
LESS_THAN = "lessThan"
|
|
192
|
+
EQUALS = "equals"
|
|
193
|
+
NOT_EQUALS = "notEquals"
|
|
194
|
+
IS = "is"
|
|
195
|
+
IS_NOT = "isNot"
|
|
196
|
+
MATCHES = "matches"
|
|
197
|
+
DOES_NOT_MATCH = "doesNotMatch"
|
|
198
|
+
CONTAINS = "contains"
|
|
199
|
+
DOES_NOT_CONTAIN = "doesNotContain"
|
|
200
|
+
|
|
201
|
+
ALL = [
|
|
202
|
+
GREATER_THAN,
|
|
203
|
+
LESS_THAN,
|
|
204
|
+
EQUALS,
|
|
205
|
+
NOT_EQUALS,
|
|
206
|
+
IS,
|
|
207
|
+
IS_NOT,
|
|
208
|
+
MATCHES,
|
|
209
|
+
DOES_NOT_MATCH,
|
|
210
|
+
CONTAINS,
|
|
211
|
+
DOES_NOT_CONTAIN,
|
|
212
|
+
]
|
|
213
|
+
|
|
214
|
+
REQUIRES_LIST_COMPARAND = [MATCHES, DOES_NOT_MATCH, CONTAINS, DOES_NOT_CONTAIN]
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
class GuardLLMType:
|
|
218
|
+
"""LLM Types to use for guards"""
|
|
219
|
+
|
|
220
|
+
OPENAI = "openAi"
|
|
221
|
+
AZURE_OPENAI = "azureOpenAi"
|
|
222
|
+
GOOGLE = "google"
|
|
223
|
+
AMAZON = "amazon"
|
|
224
|
+
DATAROBOT = "datarobot"
|
|
225
|
+
NIM = "nim"
|
|
226
|
+
|
|
227
|
+
ALL = [OPENAI, AZURE_OPENAI, GOOGLE, AMAZON, DATAROBOT, NIM]
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
class GoogleModel:
|
|
231
|
+
CHAT_BISON = "chat-bison"
|
|
232
|
+
GEMINI_15_FLASH = "google-gemini-1.5-flash"
|
|
233
|
+
GEMINI_15_PRO = "google-gemini-1.5-pro"
|
|
234
|
+
|
|
235
|
+
ALL = [CHAT_BISON, GEMINI_15_FLASH, GEMINI_15_PRO]
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
class AwsModel:
|
|
239
|
+
TITAN = "amazon-titan"
|
|
240
|
+
ANTHROPIC_CLAUDE_2 = "anthropic-claude-2"
|
|
241
|
+
ANTHROPIC_CLAUDE_3_HAIKU = "anthropic-claude-3-haiku"
|
|
242
|
+
ANTHROPIC_CLAUDE_3_SONNET = "anthropic-claude-3-sonnet"
|
|
243
|
+
ANTHROPIC_CLAUDE_3_OPUS = "anthropic-claude-3-opus"
|
|
244
|
+
|
|
245
|
+
ALL = [
|
|
246
|
+
TITAN,
|
|
247
|
+
ANTHROPIC_CLAUDE_2,
|
|
248
|
+
ANTHROPIC_CLAUDE_3_HAIKU,
|
|
249
|
+
ANTHROPIC_CLAUDE_3_SONNET,
|
|
250
|
+
ANTHROPIC_CLAUDE_3_OPUS,
|
|
251
|
+
]
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
class GoogleModelVersion:
|
|
255
|
+
CHAT_BISON = "chat-bison"
|
|
256
|
+
GEMINI_15_FLASH = "gemini-1.5-flash-002"
|
|
257
|
+
GEMINI_15_PRO = "gemini-1.5-pro-002"
|
|
258
|
+
|
|
259
|
+
ALL = [CHAT_BISON, GEMINI_15_FLASH, GEMINI_15_PRO]
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
class AwsModelVersion:
|
|
263
|
+
TITAN = "amazon.titan-text-express-v1"
|
|
264
|
+
ANTHROPIC_CLAUDE_2 = "anthropic.claude-v2:1"
|
|
265
|
+
ANTHROPIC_CLAUDE_3_HAIKU = "anthropic.claude-3-haiku-20240307-v1:0"
|
|
266
|
+
ANTHROPIC_CLAUDE_3_SONNET = "anthropic.claude-3-sonnet-20240229-v1:0"
|
|
267
|
+
ANTHROPIC_CLAUDE_3_OPUS = "anthropic.claude-3-opus-20240229-v1:0"
|
|
268
|
+
|
|
269
|
+
ALL = [
|
|
270
|
+
TITAN,
|
|
271
|
+
ANTHROPIC_CLAUDE_2,
|
|
272
|
+
ANTHROPIC_CLAUDE_3_HAIKU,
|
|
273
|
+
ANTHROPIC_CLAUDE_3_SONNET,
|
|
274
|
+
ANTHROPIC_CLAUDE_3_OPUS,
|
|
275
|
+
]
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
GOOGLE_MODEL_TO_GOOGLE_MODEL_VERSION_MAP = {
|
|
279
|
+
GoogleModel.CHAT_BISON: GoogleModelVersion.CHAT_BISON,
|
|
280
|
+
GoogleModel.GEMINI_15_FLASH: GoogleModelVersion.GEMINI_15_FLASH,
|
|
281
|
+
GoogleModel.GEMINI_15_PRO: GoogleModelVersion.GEMINI_15_PRO,
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
AWS_MODEL_TO_AWS_MODEL_VERSION_MAP = {
|
|
285
|
+
AwsModel.TITAN: AwsModelVersion.TITAN,
|
|
286
|
+
AwsModel.ANTHROPIC_CLAUDE_2: AwsModelVersion.ANTHROPIC_CLAUDE_2,
|
|
287
|
+
AwsModel.ANTHROPIC_CLAUDE_3_HAIKU: AwsModelVersion.ANTHROPIC_CLAUDE_3_HAIKU,
|
|
288
|
+
AwsModel.ANTHROPIC_CLAUDE_3_SONNET: AwsModelVersion.ANTHROPIC_CLAUDE_3_SONNET,
|
|
289
|
+
AwsModel.ANTHROPIC_CLAUDE_3_OPUS: AwsModelVersion.ANTHROPIC_CLAUDE_3_OPUS,
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
class GuardTimeoutAction:
|
|
294
|
+
"""Actions if the guard times out"""
|
|
295
|
+
|
|
296
|
+
# Block the prompt / response if the guard times out
|
|
297
|
+
BLOCK = "block"
|
|
298
|
+
|
|
299
|
+
# Continue scoring prompt / returning response if the
|
|
300
|
+
# guard times out
|
|
301
|
+
SCORE = "score"
|
|
302
|
+
|
|
303
|
+
ALL = [BLOCK, SCORE]
|
|
304
|
+
|
|
305
|
+
|
|
306
|
+
class ModerationEventTypes:
|
|
307
|
+
MODERATION_METRIC_CREATION_ERROR = "moderationMetricCreationError"
|
|
308
|
+
MODERATION_METRIC_REPORTING_ERROR = "moderationMetricReportingError"
|
|
309
|
+
MODERATION_MODEL_CONFIG_ERROR = "moderationModelConfigError"
|
|
310
|
+
MODERATION_MODEL_RUNTIME_ERROR = "moderationModelRuntimeError"
|
|
311
|
+
MODERATION_MODEL_SCORING_ERROR = "moderationModelScoringError"
|
|
312
|
+
|
|
313
|
+
# Reported in sync context
|
|
314
|
+
SYNC_EVENTS = [
|
|
315
|
+
MODERATION_METRIC_CREATION_ERROR,
|
|
316
|
+
MODERATION_METRIC_REPORTING_ERROR,
|
|
317
|
+
MODERATION_MODEL_SCORING_ERROR,
|
|
318
|
+
]
|