beekeeper-monitors-watsonx 1.0.6__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.

Potentially problematic release.


This version of beekeeper-monitors-watsonx might be problematic. Click here for more details.

@@ -0,0 +1,2154 @@
1
+ import datetime
2
+ import json
3
+ import logging
4
+ import os
5
+ import uuid
6
+ from typing import Any, Dict, List, Literal, Optional, Union
7
+
8
+ import certifi
9
+ from beekeeper.core.monitors import PromptMonitor
10
+ from beekeeper.core.monitors.types import PayloadRecord
11
+ from beekeeper.core.prompts.utils import extract_template_vars
12
+ from beekeeper.monitors.watsonx.instrumentation import suppress_output
13
+ from deprecated import deprecated
14
+ from pydantic.v1 import BaseModel
15
+
16
+ os.environ["REQUESTS_CA_BUNDLE"] = certifi.where()
17
+ logging.getLogger("ibm_watsonx_ai.client").setLevel(logging.ERROR)
18
+ logging.getLogger("ibm_watsonx_ai.wml_resource").setLevel(logging.ERROR)
19
+
20
+ REGIONS_URL = {
21
+ "us-south": {
22
+ "wml": "https://us-south.ml.cloud.ibm.com",
23
+ "wos": "https://api.aiopenscale.cloud.ibm.com",
24
+ "factsheet": None,
25
+ },
26
+ "eu-de": {
27
+ "wml": "https://eu-de.ml.cloud.ibm.com",
28
+ "wos": "https://eu-de.api.aiopenscale.cloud.ibm.com",
29
+ "factsheet": "frankfurt",
30
+ },
31
+ "au-syd": {
32
+ "wml": "https://au-syd.ml.cloud.ibm.com",
33
+ "wos": "https://au-syd.api.aiopenscale.cloud.ibm.com",
34
+ "factsheet": "sydney",
35
+ },
36
+ }
37
+
38
+
39
+ def _filter_dict(original_dict: Dict, optional_keys: List, required_keys: List = []):
40
+ """
41
+ Filters a dictionary to keep only the specified keys and checks for required keys.
42
+
43
+ Args:
44
+ original_dict (Dict): The original dictionary.
45
+ optional_keys (list): A list of keys to retain.
46
+ required_keys (list, optional): A list of keys that must be present in the dictionary. Defaults to None.
47
+ """
48
+ # Ensure all required keys are in the source dict
49
+ missing_keys = [key for key in required_keys if key not in original_dict]
50
+ if missing_keys:
51
+ raise KeyError(f"Missing required parameter: {missing_keys}")
52
+
53
+ all_keys_to_keep = set(required_keys + optional_keys)
54
+
55
+ # Create a new dictionary with only the key-value pairs where the key is in 'keys' and value is not None
56
+ return {
57
+ key: original_dict[key]
58
+ for key in all_keys_to_keep
59
+ if key in original_dict and original_dict[key] is not None
60
+ }
61
+
62
+
63
+ def _convert_payload_format(
64
+ records: List[Dict],
65
+ feature_fields: List[str],
66
+ ) -> List[Dict]:
67
+ payload_data = []
68
+ response_fields = ["generated_text", "input_token_count", "generated_token_count"]
69
+
70
+ for record in records:
71
+ request = {"parameters": {"template_variables": {}}}
72
+ results = {}
73
+
74
+ request["parameters"]["template_variables"] = {
75
+ field: str(record.get(field, "")) for field in feature_fields
76
+ }
77
+
78
+ results = {
79
+ field: record.get(field) for field in response_fields if record.get(field)
80
+ }
81
+
82
+ pl_record = {
83
+ "request": request,
84
+ "response": {"results": [results]},
85
+ "scoring_id": str(uuid.uuid4()),
86
+ }
87
+
88
+ if "response_time" in record:
89
+ pl_record["response_time"] = record["response_time"]
90
+
91
+ payload_data.append(pl_record)
92
+
93
+ return payload_data
94
+
95
+
96
+ # ===== Credentials Classes =====
97
+ class CloudPakforDataCredentials(BaseModel):
98
+ """
99
+ Encapsulates the credentials required for IBM Cloud Pak for Data.
100
+
101
+ Attributes:
102
+ url (str): The host URL of the Cloud Pak for Data environment.
103
+ api_key (str, optional): The API key for the environment, if IAM is enabled.
104
+ username (str, optional): The username for the environment.
105
+ password (str, optional): The password for the environment.
106
+ bedrock_url (str, optional): The Bedrock URL. Required only when IAM integration is enabled on CP4D 4.0.x clusters.
107
+ instance_id (str, optional): The instance ID.
108
+ version (str, optional): The version of Cloud Pak for Data.
109
+ disable_ssl_verification (bool, optional): Indicates whether to disable SSL certificate verification.
110
+ Defaults to `True`.
111
+ """
112
+
113
+ url: str
114
+ api_key: Optional[str] = None
115
+ username: Optional[str] = None
116
+ password: Optional[str] = None
117
+ bedrock_url: Optional[str] = None
118
+ instance_id: Optional[Literal["icp", "openshift"]] = None
119
+ version: Optional[str] = None
120
+ disable_ssl_verification: bool = True
121
+
122
+ def __init__(
123
+ self,
124
+ url: str,
125
+ api_key: Optional[str] = None,
126
+ username: Optional[str] = None,
127
+ password: Optional[str] = None,
128
+ bedrock_url: Optional[str] = None,
129
+ instance_id: Optional[Literal["icp", "openshift"]] = None,
130
+ version: Optional[str] = None,
131
+ disable_ssl_verification: bool = True,
132
+ ) -> None:
133
+ super().__init__(
134
+ url=url,
135
+ api_key=api_key,
136
+ username=username,
137
+ password=password,
138
+ bedrock_url=bedrock_url,
139
+ instance_id=instance_id,
140
+ version=version,
141
+ disable_ssl_verification=disable_ssl_verification,
142
+ )
143
+
144
+ def to_dict(self) -> Dict[str, Any]:
145
+ cpd_creds = dict([(k, v) for k, v in self.__dict__.items()]) # noqa: C404
146
+
147
+ if "instance_id" in cpd_creds and self.instance_id.lower() not in [
148
+ "icp",
149
+ "openshift",
150
+ ]:
151
+ cpd_creds.pop("instance_id")
152
+
153
+ return cpd_creds
154
+
155
+
156
+ class IntegratedSystemCredentials(BaseModel):
157
+ """
158
+ Encapsulates the credentials for an Integrated System based on the authentication type.
159
+
160
+ Depending on the `auth_type`, only a subset of the properties is required.
161
+
162
+ Attributes:
163
+ auth_type (str): The type of authentication. Currently supports "basic" and "bearer".
164
+ username (str, optional): The username for Basic Authentication.
165
+ password (str, optional): The password for Basic Authentication.
166
+ token_url (str, optional): The URL of the authentication endpoint used to request a Bearer token.
167
+ token_method (str, optional): The HTTP method (e.g., "POST", "GET") used to request the Bearer token.
168
+ Defaults to "POST".
169
+ token_headers (Dict, optional): Optional headers to include when requesting the Bearer token.
170
+ Defaults to `None`.
171
+ token_payload (str | dict, optional): The body or payload to send when requesting the Bearer token.
172
+ Can be a string (e.g., raw JSON). Defaults to `None`.
173
+ """
174
+
175
+ auth_type: Literal["basic", "bearer"]
176
+ username: Optional[str] # basic
177
+ password: Optional[str] # basic
178
+ token_url: Optional[str] # bearer
179
+ token_method: Optional[str] = "POST" # bearer
180
+ token_headers: Optional[Dict] = {} # bearer
181
+ token_payload: Optional[Union[str, Dict]] = None # bearer
182
+
183
+ def __init__(
184
+ self,
185
+ auth_type: Literal["basic", "bearer"],
186
+ username: str = None,
187
+ password: str = None,
188
+ token_url: str = None,
189
+ token_method: str = "POST",
190
+ token_headers: Dict = {},
191
+ token_payload: Union[str, Dict] = None,
192
+ ) -> None:
193
+ if auth_type == "basic":
194
+ if not username or not password:
195
+ raise ValueError(
196
+ "`username` and `password` are required for auth_type = 'basic'.",
197
+ )
198
+ elif auth_type == "bearer":
199
+ if not token_url:
200
+ raise ValueError(
201
+ "`token_url` are required for auth_type = 'bearer'.",
202
+ )
203
+
204
+ super().__init__(
205
+ auth_type=auth_type,
206
+ username=username,
207
+ password=password,
208
+ token_url=token_url,
209
+ token_method=token_method,
210
+ token_headers=token_headers,
211
+ token_payload=token_payload,
212
+ )
213
+
214
+ def to_dict(self) -> Dict:
215
+ integrated_system_creds = {"auth_type": self.auth_type}
216
+
217
+ if self.auth_type == "basic":
218
+ integrated_system_creds["username"] = self.username
219
+ integrated_system_creds["password"] = self.password
220
+ elif self.auth_type == "bearer":
221
+ integrated_system_creds["token_info"] = {
222
+ "url": self.token_url,
223
+ "method": self.token_method,
224
+ "headers": self.token_headers,
225
+ "payload": self.token_payload,
226
+ }
227
+
228
+ return integrated_system_creds
229
+
230
+
231
+ # ===== Monitor Classes =====
232
+ class WatsonxExternalPromptMonitor(PromptMonitor):
233
+ """
234
+ Provides functionality to interact with IBM watsonx.governance for monitoring prompts executed on external LLMs.
235
+
236
+ Note:
237
+ One of the following parameters is required to create a prompt monitor:
238
+ `project_id` or `space_id`, but not both.
239
+
240
+ Attributes:
241
+ api_key (str): The API key for IBM watsonx.governance.
242
+ space_id (str, optional): The space ID in watsonx.governance.
243
+ project_id (str, optional): The project ID in watsonx.governance.
244
+ region (str, optional): The region where watsonx.governance is hosted when using IBM Cloud.
245
+ Defaults to `us-south`.
246
+ cpd_creds (CloudPakforDataCredentials, optional): The Cloud Pak for Data environment credentials.
247
+ subscription_id (str, optional): The subscription ID associated with the records being logged.
248
+
249
+ Example:
250
+ ```python
251
+ from beekeeper.monitors.watsonx import (
252
+ WatsonxExternalPromptMonitor,
253
+ CloudPakforDataCredentials,
254
+ )
255
+
256
+ # watsonx.governance (IBM Cloud)
257
+ wxgov_client = WatsonxExternalPromptMonitor(
258
+ api_key="API_KEY", space_id="SPACE_ID"
259
+ )
260
+
261
+ # watsonx.governance (CP4D)
262
+ cpd_creds = CloudPakforDataCredentials(
263
+ url="CPD_URL",
264
+ username="USERNAME",
265
+ password="PASSWORD",
266
+ version="5.0",
267
+ instance_id="openshift",
268
+ )
269
+
270
+ wxgov_client = WatsonxExternalPromptMonitor(
271
+ space_id="SPACE_ID", cpd_creds=cpd_creds
272
+ )
273
+ ```
274
+ """
275
+
276
+ def __init__(
277
+ self,
278
+ api_key: str = None,
279
+ space_id: str = None,
280
+ project_id: str = None,
281
+ region: Literal["us-south", "eu-de", "au-syd"] = "us-south",
282
+ cpd_creds: CloudPakforDataCredentials | Dict = None,
283
+ subscription_id: str = None,
284
+ **kwargs,
285
+ ) -> None:
286
+ import ibm_aigov_facts_client # noqa: F401
287
+ import ibm_cloud_sdk_core.authenticators # noqa: F401
288
+ import ibm_watson_openscale # noqa: F401
289
+ import ibm_watsonx_ai # noqa: F401
290
+
291
+ super().__init__(**kwargs)
292
+
293
+ self.space_id = space_id
294
+ self.project_id = project_id
295
+ self.region = region
296
+ self.subscription_id = subscription_id
297
+ self._api_key = api_key
298
+ self._wos_client = None
299
+
300
+ self._container_id = space_id if space_id else project_id
301
+ self._container_type = "space" if space_id else "project"
302
+ self._deployment_stage = "production" if space_id else "development"
303
+
304
+ if cpd_creds:
305
+ self._wos_cpd_creds = _filter_dict(
306
+ cpd_creds.to_dict(),
307
+ ["username", "password", "api_key", "disable_ssl_verification"],
308
+ ["url"],
309
+ )
310
+ self._fact_cpd_creds = _filter_dict(
311
+ cpd_creds.to_dict(),
312
+ ["username", "password", "api_key", "bedrock_url"],
313
+ ["url"],
314
+ )
315
+ self._fact_cpd_creds["service_url"] = self._fact_cpd_creds.pop("url")
316
+ self._wml_cpd_creds = _filter_dict(
317
+ cpd_creds.to_dict(),
318
+ [
319
+ "username",
320
+ "password",
321
+ "api_key",
322
+ "instance_id",
323
+ "version",
324
+ "bedrock_url",
325
+ ],
326
+ ["url"],
327
+ )
328
+
329
+ def _create_detached_prompt(
330
+ self,
331
+ detached_details: Dict,
332
+ prompt_template_details: Dict,
333
+ detached_asset_details: Dict,
334
+ ) -> str:
335
+ from ibm_aigov_facts_client import ( # type: ignore
336
+ AIGovFactsClient,
337
+ CloudPakforDataConfig,
338
+ DetachedPromptTemplate,
339
+ PromptTemplate,
340
+ )
341
+
342
+ try:
343
+ if hasattr(self, "_fact_cpd_creds") and self._fact_cpd_creds:
344
+ cpd_creds = CloudPakforDataConfig(**self._fact_cpd_creds)
345
+
346
+ aigov_client = AIGovFactsClient(
347
+ container_id=self._container_id,
348
+ container_type=self._container_type,
349
+ cloud_pak_for_data_configs=cpd_creds,
350
+ disable_tracing=True,
351
+ )
352
+
353
+ else:
354
+ aigov_client = AIGovFactsClient(
355
+ api_key=self._api_key,
356
+ container_id=self._container_id,
357
+ container_type=self._container_type,
358
+ disable_tracing=True,
359
+ region=REGIONS_URL[self.region]["factsheet"],
360
+ )
361
+
362
+ except Exception as e:
363
+ logging.error(
364
+ f"Error connecting to IBM watsonx.governance (factsheets): {e}",
365
+ )
366
+ raise
367
+
368
+ created_detached_pta = aigov_client.assets.create_detached_prompt(
369
+ **detached_asset_details,
370
+ prompt_details=PromptTemplate(**prompt_template_details),
371
+ detached_information=DetachedPromptTemplate(**detached_details),
372
+ )
373
+
374
+ return created_detached_pta.to_dict()["asset_id"]
375
+
376
+ def _create_deployment_pta(self, asset_id: str, name: str, model_id: str) -> str:
377
+ from ibm_watsonx_ai import APIClient, Credentials # type: ignore
378
+
379
+ try:
380
+ if hasattr(self, "_wml_cpd_creds") and self._wml_cpd_creds:
381
+ creds = Credentials(**self._wml_cpd_creds)
382
+
383
+ wml_client = APIClient(creds)
384
+ wml_client.set.default_space(self.space_id)
385
+
386
+ else:
387
+ creds = Credentials(
388
+ url=REGIONS_URL[self.region]["wml"],
389
+ api_key=self._api_key,
390
+ )
391
+ wml_client = APIClient(creds)
392
+ wml_client.set.default_space(self.space_id)
393
+
394
+ except Exception as e:
395
+ logging.error(f"Error connecting to IBM watsonx.ai Runtime: {e}")
396
+ raise
397
+
398
+ meta_props = {
399
+ wml_client.deployments.ConfigurationMetaNames.PROMPT_TEMPLATE: {
400
+ "id": asset_id,
401
+ },
402
+ wml_client.deployments.ConfigurationMetaNames.DETACHED: {},
403
+ wml_client.deployments.ConfigurationMetaNames.NAME: name
404
+ + " "
405
+ + "deployment",
406
+ wml_client.deployments.ConfigurationMetaNames.BASE_MODEL_ID: model_id,
407
+ }
408
+
409
+ created_deployment = wml_client.deployments.create(asset_id, meta_props)
410
+
411
+ return wml_client.deployments.get_uid(created_deployment)
412
+
413
+ @deprecated(
414
+ reason="'add_prompt_observer()' is deprecated and will be removed in a future version. Use 'create_prompt_monitor()' instead.",
415
+ version="1.0.5",
416
+ action="always",
417
+ )
418
+ def add_prompt_observer(
419
+ self,
420
+ name: str,
421
+ model_id: str,
422
+ task_id: Literal[
423
+ "extraction",
424
+ "generation",
425
+ "question_answering",
426
+ "retrieval_augmented_generation",
427
+ "summarization",
428
+ ],
429
+ detached_model_provider: str,
430
+ description: str = "",
431
+ model_parameters: Dict = None,
432
+ detached_model_name: str = None,
433
+ detached_model_url: str = None,
434
+ detached_prompt_url: str = None,
435
+ detached_prompt_additional_info: Dict = None,
436
+ prompt_variables: List[str] = None,
437
+ locale: str = "en",
438
+ input_text: str = None,
439
+ context_fields: List[str] = None,
440
+ question_field: str = None,
441
+ ) -> Dict:
442
+ return self.create_prompt_monitor(
443
+ name=name,
444
+ model_id=model_id,
445
+ task_id=task_id,
446
+ detached_model_provider=detached_model_provider,
447
+ description=description,
448
+ model_parameters=model_parameters,
449
+ detached_model_name=detached_model_name,
450
+ detached_model_url=detached_model_url,
451
+ detached_prompt_url=detached_prompt_url,
452
+ detached_prompt_additional_info=detached_prompt_additional_info,
453
+ prompt_variables=prompt_variables,
454
+ locale=locale,
455
+ input_text=input_text,
456
+ context_fields=context_fields,
457
+ question_field=question_field,
458
+ )
459
+
460
+ @deprecated(
461
+ reason="'add_prompt_monitor()' is deprecated and will be removed in a future version. Use 'create_prompt_monitor()' instead.",
462
+ version="1.0.6",
463
+ action="always",
464
+ )
465
+ def add_prompt_monitor(
466
+ self,
467
+ name: str,
468
+ model_id: str,
469
+ task_id: Literal[
470
+ "extraction",
471
+ "generation",
472
+ "question_answering",
473
+ "retrieval_augmented_generation",
474
+ "summarization",
475
+ ],
476
+ detached_model_provider: str,
477
+ description: str = "",
478
+ model_parameters: Dict = None,
479
+ detached_model_name: str = None,
480
+ detached_model_url: str = None,
481
+ detached_prompt_url: str = None,
482
+ detached_prompt_additional_info: Dict = None,
483
+ prompt_variables: List[str] = None,
484
+ locale: str = "en",
485
+ input_text: str = None,
486
+ context_fields: List[str] = None,
487
+ question_field: str = None,
488
+ ) -> Dict:
489
+ return self.create_prompt_monitor(
490
+ name=name,
491
+ model_id=model_id,
492
+ task_id=task_id,
493
+ detached_model_provider=detached_model_provider,
494
+ description=description,
495
+ model_parameters=model_parameters,
496
+ detached_model_name=detached_model_name,
497
+ detached_model_url=detached_model_url,
498
+ detached_prompt_url=detached_prompt_url,
499
+ detached_prompt_additional_info=detached_prompt_additional_info,
500
+ prompt_variables=prompt_variables,
501
+ locale=locale,
502
+ input_text=input_text,
503
+ context_fields=context_fields,
504
+ question_field=question_field,
505
+ )
506
+
507
+ def create_prompt_monitor(
508
+ self,
509
+ name: str,
510
+ model_id: str,
511
+ task_id: Literal[
512
+ "extraction",
513
+ "generation",
514
+ "question_answering",
515
+ "retrieval_augmented_generation",
516
+ "summarization",
517
+ ],
518
+ detached_model_provider: str,
519
+ description: str = "",
520
+ model_parameters: Dict = None,
521
+ detached_model_name: str = None,
522
+ detached_model_url: str = None,
523
+ detached_prompt_url: str = None,
524
+ detached_prompt_additional_info: Dict = None,
525
+ prompt_variables: List[str] = None,
526
+ locale: str = "en",
527
+ input_text: str = None,
528
+ context_fields: List[str] = None,
529
+ question_field: str = None,
530
+ ) -> Dict:
531
+ """
532
+ Creates a detached (external) prompt template asset and attaches a monitor to the specified prompt template asset.
533
+
534
+ Args:
535
+ name (str): The name of the External Prompt Template Asset.
536
+ model_id (str): The ID of the model associated with the prompt.
537
+ task_id (str): The task identifier.
538
+ detached_model_provider (str): The external model provider.
539
+ description (str, optional): A description of the External Prompt Template Asset.
540
+ model_parameters (Dict, optional): Model parameters and their respective values.
541
+ detached_model_name (str, optional): The name of the external model.
542
+ detached_model_url (str, optional): The URL of the external model.
543
+ detached_prompt_url (str, optional): The URL of the external prompt.
544
+ detached_prompt_additional_info (Dict, optional): Additional information related to the external prompt.
545
+ prompt_variables (List[str], optional): Values for the prompt variables.
546
+ locale (str, optional): Locale code for the input/output language. eg. "en", "pt", "es".
547
+ input_text (str, optional): The input text for the prompt.
548
+ context_fields (List[str], optional): A list of fields that will provide context to the prompt.
549
+ Applicable only for "retrieval_augmented_generation" task type.
550
+ question_field (str, optional): The field containing the question to be answered.
551
+ Applicable only for "retrieval_augmented_generation" task type.
552
+
553
+ Example:
554
+ ```python
555
+ wxgov_client.create_prompt_monitor(
556
+ name="Detached prompt (model AWS Anthropic)",
557
+ model_id="anthropic.claude-v2",
558
+ task_id="retrieval_augmented_generation",
559
+ detached_model_provider="AWS Bedrock",
560
+ detached_model_name="Anthropic Claude 2.0",
561
+ detached_model_url="https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-claude.html",
562
+ prompt_variables=["context1", "context2", "input_query"],
563
+ input_text="Prompt text to be given",
564
+ context_fields=["context1", "context2"],
565
+ question_field="input_query",
566
+ )
567
+ ```
568
+ """
569
+ if (not (self.project_id or self.space_id)) or (
570
+ self.project_id and self.space_id
571
+ ):
572
+ raise ValueError(
573
+ "Invalid configuration: Neither was provided: please set either 'project_id' or 'space_id'. "
574
+ "Both were provided: 'project_id' and 'space_id' cannot be set at the same time."
575
+ )
576
+
577
+ if task_id == "retrieval_augmented_generation":
578
+ if not context_fields or not question_field:
579
+ raise ValueError(
580
+ "For 'retrieval_augmented_generation' task, requires non-empty 'context_fields' and 'question_field'."
581
+ )
582
+
583
+ prompt_metadata = locals()
584
+ # Remove unused vars from dict
585
+ prompt_metadata.pop("self", None)
586
+ prompt_metadata.pop("context_fields", None)
587
+ prompt_metadata.pop("question_field", None)
588
+ prompt_metadata.pop("locale", None)
589
+
590
+ # Update name of keys to aigov_facts api
591
+ prompt_metadata["input"] = prompt_metadata.pop("input_text", None)
592
+ prompt_metadata["model_provider"] = prompt_metadata.pop(
593
+ "detached_model_provider",
594
+ None,
595
+ )
596
+ prompt_metadata["model_name"] = prompt_metadata.pop("detached_model_name", None)
597
+ prompt_metadata["model_url"] = prompt_metadata.pop("detached_model_url", None)
598
+ prompt_metadata["prompt_url"] = prompt_metadata.pop("detached_prompt_url", None)
599
+ prompt_metadata["prompt_additional_info"] = prompt_metadata.pop(
600
+ "detached_prompt_additional_info",
601
+ None,
602
+ )
603
+
604
+ # Update list of vars to dict
605
+ prompt_metadata["prompt_variables"] = Dict.fromkeys(
606
+ prompt_metadata["prompt_variables"], ""
607
+ )
608
+
609
+ from ibm_watson_openscale import APIClient as WosAPIClient # type: ignore
610
+
611
+ if not self._wos_client:
612
+ try:
613
+ if hasattr(self, "_wos_cpd_creds") and self._wos_cpd_creds:
614
+ from ibm_cloud_sdk_core.authenticators import (
615
+ CloudPakForDataAuthenticator, # type: ignore
616
+ )
617
+
618
+ authenticator = CloudPakForDataAuthenticator(**self._wos_cpd_creds)
619
+ self._wos_client = WosAPIClient(
620
+ authenticator=authenticator,
621
+ service_url=self._wos_cpd_creds["url"],
622
+ )
623
+
624
+ else:
625
+ from ibm_cloud_sdk_core.authenticators import (
626
+ IAMAuthenticator, # type: ignore
627
+ )
628
+
629
+ authenticator = IAMAuthenticator(apikey=self._api_key)
630
+ self._wos_client = WosAPIClient(
631
+ authenticator=authenticator,
632
+ service_url=REGIONS_URL[self.region]["wos"],
633
+ )
634
+
635
+ except Exception as e:
636
+ logging.error(
637
+ f"Error connecting to IBM watsonx.governance (openscale): {e}",
638
+ )
639
+ raise
640
+
641
+ detached_details = _filter_dict(
642
+ prompt_metadata,
643
+ ["model_name", "model_url", "prompt_url", "prompt_additional_info"],
644
+ ["model_id", "model_provider"],
645
+ )
646
+ detached_details["prompt_id"] = "detached_prompt_" + str(uuid.uuid4())
647
+
648
+ prompt_details = _filter_dict(
649
+ prompt_metadata,
650
+ ["prompt_variables", "input", "model_parameters"],
651
+ )
652
+
653
+ detached_asset_details = _filter_dict(
654
+ prompt_metadata,
655
+ ["description"],
656
+ ["name", "model_id", "task_id"],
657
+ )
658
+
659
+ detached_pta_id = suppress_output(
660
+ self._create_detached_prompt,
661
+ detached_details,
662
+ prompt_details,
663
+ detached_asset_details,
664
+ )
665
+ deployment_id = None
666
+ if self._container_type == "space":
667
+ deployment_id = suppress_output(
668
+ self._create_deployment_pta, detached_pta_id, name, model_id
669
+ )
670
+
671
+ monitors = {
672
+ "generative_ai_quality": {
673
+ "parameters": {"min_sample_size": 10, "metrics_configuration": {}},
674
+ },
675
+ }
676
+
677
+ max_attempt_execute_prompt_setup = 0
678
+ while max_attempt_execute_prompt_setup < 2:
679
+ try:
680
+ generative_ai_monitor_details = suppress_output(
681
+ self._wos_client.wos.execute_prompt_setup,
682
+ prompt_template_asset_id=detached_pta_id,
683
+ space_id=self.space_id,
684
+ project_id=self.project_id,
685
+ deployment_id=deployment_id,
686
+ label_column="reference_output",
687
+ context_fields=context_fields,
688
+ question_field=question_field,
689
+ operational_space_id=self._deployment_stage,
690
+ problem_type=task_id,
691
+ data_input_locale=[locale],
692
+ generated_output_locale=[locale],
693
+ input_data_type="unstructured_text",
694
+ supporting_monitors=monitors,
695
+ background_mode=False,
696
+ )
697
+
698
+ break
699
+
700
+ except Exception as e:
701
+ if (
702
+ e.code == 403
703
+ and "The user entitlement does not exist" in e.message
704
+ and max_attempt_execute_prompt_setup < 1
705
+ ):
706
+ max_attempt_execute_prompt_setup = (
707
+ max_attempt_execute_prompt_setup + 1
708
+ )
709
+
710
+ data_marts = self._wos_client.data_marts.list().result
711
+ if (data_marts.data_marts is None) or (not data_marts.data_marts):
712
+ raise ValueError(
713
+ "Error retrieving IBM watsonx.governance (openscale) data mart. \
714
+ Make sure the data mart are configured.",
715
+ )
716
+
717
+ data_mart_id = data_marts.data_marts[0].metadata.id
718
+
719
+ self._wos_client.wos.add_instance_mapping(
720
+ service_instance_id=data_mart_id,
721
+ space_id=self.space_id,
722
+ project_id=self.project_id,
723
+ )
724
+ else:
725
+ max_attempt_execute_prompt_setup = 2
726
+ raise
727
+
728
+ generative_ai_monitor_details = generative_ai_monitor_details.result._to_dict()
729
+
730
+ return {
731
+ "detached_prompt_template_asset_id": detached_pta_id,
732
+ "deployment_id": deployment_id,
733
+ "subscription_id": generative_ai_monitor_details["subscription_id"],
734
+ }
735
+
736
+ def store_payload_records(
737
+ self,
738
+ request_records: List[Dict],
739
+ subscription_id: str = None,
740
+ ) -> List[str]:
741
+ """
742
+ Stores records to the payload logging system.
743
+
744
+ Args:
745
+ request_records (List[Dict]): A list of records to be logged, where each record is represented as a dictionary.
746
+ subscription_id (str, optional): The subscription ID associated with the records being logged.
747
+
748
+ Example:
749
+ ```python
750
+ wxgov_client.store_payload_records(
751
+ request_records=[
752
+ {
753
+ "context1": "value_context1",
754
+ "context2": "value_context1",
755
+ "input_query": "What's Beekeeper Framework?",
756
+ "generated_text": "Beekeeper is a data framework to make AI easier to work with.",
757
+ "input_token_count": 25,
758
+ "generated_token_count": 150,
759
+ }
760
+ ],
761
+ subscription_id="5d62977c-a53d-4b6d-bda1-7b79b3b9d1a0",
762
+ )
763
+ ```
764
+ """
765
+ from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
766
+ from ibm_watson_openscale import APIClient as WosAPIClient
767
+ from ibm_watson_openscale.supporting_classes.enums import (
768
+ DataSetTypes,
769
+ TargetTypes,
770
+ )
771
+
772
+ # Expected behavior: Prefer using fn `subscription_id`.
773
+ # Fallback to `self.subscription_id` if `subscription_id` None or empty.
774
+ _subscription_id = subscription_id or self.subscription_id
775
+
776
+ if _subscription_id is None or _subscription_id == "":
777
+ raise ValueError(
778
+ "Unexpected value for 'subscription_id': Cannot be None or empty string."
779
+ )
780
+
781
+ if not self._wos_client:
782
+ try:
783
+ if hasattr(self, "_wos_cpd_creds") and self._wos_cpd_creds:
784
+ from ibm_cloud_sdk_core.authenticators import (
785
+ CloudPakForDataAuthenticator, # type: ignore
786
+ )
787
+
788
+ authenticator = CloudPakForDataAuthenticator(**self._wos_cpd_creds)
789
+ self._wos_client = WosAPIClient(
790
+ authenticator=authenticator,
791
+ service_url=self._wos_cpd_creds["url"],
792
+ )
793
+
794
+ else:
795
+ from ibm_cloud_sdk_core.authenticators import (
796
+ IAMAuthenticator, # type: ignore
797
+ )
798
+
799
+ authenticator = IAMAuthenticator(apikey=self._api_key)
800
+ self._wos_client = WosAPIClient(
801
+ authenticator=authenticator,
802
+ service_url=REGIONS_URL[self.region]["wos"],
803
+ )
804
+
805
+ except Exception as e:
806
+ logging.error(
807
+ f"Error connecting to IBM watsonx.governance (openscale): {e}",
808
+ )
809
+ raise
810
+
811
+ subscription_details = self._wos_client.subscriptions.get(
812
+ _subscription_id,
813
+ ).result
814
+ subscription_details = json.loads(str(subscription_details))
815
+
816
+ feature_fields = subscription_details["entity"]["asset_properties"][
817
+ "feature_fields"
818
+ ]
819
+
820
+ payload_data_set_id = (
821
+ self._wos_client.data_sets.list(
822
+ type=DataSetTypes.PAYLOAD_LOGGING,
823
+ target_target_id=_subscription_id,
824
+ target_target_type=TargetTypes.SUBSCRIPTION,
825
+ )
826
+ .result.data_sets[0]
827
+ .metadata.id
828
+ )
829
+
830
+ payload_data = _convert_payload_format(request_records, feature_fields)
831
+
832
+ suppress_output(
833
+ self._wos_client.data_sets.store_records,
834
+ data_set_id=payload_data_set_id,
835
+ request_body=payload_data,
836
+ background_mode=False,
837
+ )
838
+
839
+ return [data["scoring_id"] + "-1" for data in payload_data]
840
+
841
+ def __call__(self, payload: PayloadRecord) -> None:
842
+ if self.prompt_template:
843
+ template_vars = extract_template_vars(
844
+ self.prompt_template.template, payload.input_text
845
+ )
846
+
847
+ if not template_vars:
848
+ self.store_payload_records([payload.model_dump()])
849
+ else:
850
+ self.store_payload_records([{**payload.model_dump(), **template_vars}])
851
+
852
+
853
+ class WatsonxPromptMonitor(PromptMonitor):
854
+ """
855
+ Provides functionality to interact with IBM watsonx.governance for monitoring prompts executed within
856
+ IBM watsonx.ai LLMs.
857
+
858
+ Note:
859
+ One of the following parameters is required to create a prompt monitor:
860
+ `project_id` or `space_id`, but not both.
861
+
862
+ Attributes:
863
+ api_key (str): The API key for IBM watsonx.governance.
864
+ space_id (str, optional): The space ID in watsonx.governance.
865
+ project_id (str, optional): The project ID in watsonx.governance.
866
+ region (str, optional): The region where watsonx.governance is hosted when using IBM Cloud.
867
+ Defaults to `us-south`.
868
+ cpd_creds (CloudPakforDataCredentials, optional): The Cloud Pak for Data environment credentials.
869
+ subscription_id (str, optional): The subscription ID associated with the records being logged.
870
+
871
+ Example:
872
+ ```python
873
+ from beekeeper.monitors.watsonx import (
874
+ WatsonxPromptMonitor,
875
+ CloudPakforDataCredentials,
876
+ )
877
+
878
+ # watsonx.governance (IBM Cloud)
879
+ wxgov_client = WatsonxPromptMonitor(api_key="API_KEY", space_id="SPACE_ID")
880
+
881
+ # watsonx.governance (CP4D)
882
+ cpd_creds = CloudPakforDataCredentials(
883
+ url="CPD_URL",
884
+ username="USERNAME",
885
+ password="PASSWORD",
886
+ version="5.0",
887
+ instance_id="openshift",
888
+ )
889
+
890
+ wxgov_client = WatsonxPromptMonitor(space_id="SPACE_ID", cpd_creds=cpd_creds)
891
+ ```
892
+ """
893
+
894
+ def __init__(
895
+ self,
896
+ api_key: str = None,
897
+ space_id: str = None,
898
+ project_id: str = None,
899
+ region: Literal["us-south", "eu-de", "au-syd"] = "us-south",
900
+ cpd_creds: CloudPakforDataCredentials | Dict = None,
901
+ subscription_id: str = None,
902
+ **kwargs,
903
+ ) -> None:
904
+ import ibm_aigov_facts_client # noqa: F401
905
+ import ibm_cloud_sdk_core.authenticators # noqa: F401
906
+ import ibm_watson_openscale # noqa: F401
907
+ import ibm_watsonx_ai # noqa: F401
908
+
909
+ super().__init__(**kwargs)
910
+
911
+ self.space_id = space_id
912
+ self.project_id = project_id
913
+ self.region = region
914
+ self.subscription_id = subscription_id
915
+ self._api_key = api_key
916
+ self._wos_client = None
917
+
918
+ self._container_id = space_id if space_id else project_id
919
+ self._container_type = "space" if space_id else "project"
920
+ self._deployment_stage = "production" if space_id else "development"
921
+
922
+ if cpd_creds:
923
+ self._wos_cpd_creds = _filter_dict(
924
+ cpd_creds.to_dict(),
925
+ ["username", "password", "api_key", "disable_ssl_verification"],
926
+ ["url"],
927
+ )
928
+ self._fact_cpd_creds = _filter_dict(
929
+ cpd_creds.to_dict(),
930
+ ["username", "password", "api_key", "bedrock_url"],
931
+ ["url"],
932
+ )
933
+ self._fact_cpd_creds["service_url"] = self._fact_cpd_creds.pop("url")
934
+ self._wml_cpd_creds = _filter_dict(
935
+ cpd_creds.to_dict(),
936
+ [
937
+ "username",
938
+ "password",
939
+ "api_key",
940
+ "instance_id",
941
+ "version",
942
+ "bedrock_url",
943
+ ],
944
+ ["url"],
945
+ )
946
+
947
+ def _create_prompt_template(
948
+ self,
949
+ prompt_template_details: Dict,
950
+ asset_details: Dict,
951
+ ) -> str:
952
+ from ibm_aigov_facts_client import (
953
+ AIGovFactsClient,
954
+ CloudPakforDataConfig,
955
+ PromptTemplate,
956
+ )
957
+
958
+ try:
959
+ if hasattr(self, "_fact_cpd_creds") and self._fact_cpd_creds:
960
+ cpd_creds = CloudPakforDataConfig(**self._fact_cpd_creds)
961
+
962
+ aigov_client = AIGovFactsClient(
963
+ container_id=self._container_id,
964
+ container_type=self._container_type,
965
+ cloud_pak_for_data_configs=cpd_creds,
966
+ disable_tracing=True,
967
+ )
968
+
969
+ else:
970
+ aigov_client = AIGovFactsClient(
971
+ api_key=self._api_key,
972
+ container_id=self._container_id,
973
+ container_type=self._container_type,
974
+ disable_tracing=True,
975
+ region=REGIONS_URL[self.region]["factsheet"],
976
+ )
977
+
978
+ except Exception as e:
979
+ logging.error(
980
+ f"Error connecting to IBM watsonx.governance (factsheets): {e}",
981
+ )
982
+ raise
983
+
984
+ created_pta = aigov_client.assets.create_prompt(
985
+ **asset_details,
986
+ input_mode="freeform",
987
+ prompt_details=PromptTemplate(**prompt_template_details),
988
+ )
989
+
990
+ return created_pta.to_dict()["asset_id"]
991
+
992
+ def _create_deployment_pta(self, asset_id: str, name: str, model_id: str) -> str:
993
+ from ibm_watsonx_ai import APIClient, Credentials # type: ignore
994
+
995
+ try:
996
+ if hasattr(self, "_wml_cpd_creds") and self._wml_cpd_creds:
997
+ creds = Credentials(**self._wml_cpd_creds)
998
+
999
+ wml_client = APIClient(creds)
1000
+ wml_client.set.default_space(self.space_id)
1001
+
1002
+ else:
1003
+ creds = Credentials(
1004
+ url=REGIONS_URL[self.region]["wml"],
1005
+ api_key=self._api_key,
1006
+ )
1007
+
1008
+ wml_client = APIClient(creds)
1009
+ wml_client.set.default_space(self.space_id)
1010
+
1011
+ except Exception as e:
1012
+ logging.error(f"Error connecting to IBM watsonx.ai Runtime: {e}")
1013
+ raise
1014
+
1015
+ meta_props = {
1016
+ wml_client.deployments.ConfigurationMetaNames.PROMPT_TEMPLATE: {
1017
+ "id": asset_id,
1018
+ },
1019
+ wml_client.deployments.ConfigurationMetaNames.FOUNDATION_MODEL: {},
1020
+ wml_client.deployments.ConfigurationMetaNames.NAME: name
1021
+ + " "
1022
+ + "deployment",
1023
+ wml_client.deployments.ConfigurationMetaNames.BASE_MODEL_ID: model_id,
1024
+ }
1025
+
1026
+ created_deployment = wml_client.deployments.create(asset_id, meta_props)
1027
+
1028
+ return wml_client.deployments.get_uid(created_deployment)
1029
+
1030
+ @deprecated(
1031
+ reason="'add_prompt_observer()' is deprecated and will be removed in a future version. Use 'create_prompt_monitor()' instead.",
1032
+ version="1.0.5",
1033
+ action="always",
1034
+ )
1035
+ def add_prompt_observer(
1036
+ self,
1037
+ name: str,
1038
+ model_id: str,
1039
+ task_id: Literal[
1040
+ "extraction",
1041
+ "generation",
1042
+ "question_answering",
1043
+ "retrieval_augmented_generation",
1044
+ "summarization",
1045
+ ],
1046
+ description: str = "",
1047
+ model_parameters: Dict = None,
1048
+ prompt_variables: List[str] = None,
1049
+ locale: str = "en",
1050
+ input_text: str = None,
1051
+ context_fields: List[str] = None,
1052
+ question_field: str = None,
1053
+ ) -> Dict:
1054
+ return self.create_prompt_monitor(
1055
+ name=name,
1056
+ model_id=model_id,
1057
+ task_id=task_id,
1058
+ description=description,
1059
+ model_parameters=model_parameters,
1060
+ prompt_variables=prompt_variables,
1061
+ locale=locale,
1062
+ input_text=input_text,
1063
+ context_fields=context_fields,
1064
+ question_field=question_field,
1065
+ )
1066
+
1067
+ @deprecated(
1068
+ reason="'add_prompt_observer()' is deprecated and will be removed in a future version. Use 'create_prompt_monitor()' instead.",
1069
+ version="1.0.6",
1070
+ action="always",
1071
+ )
1072
+ def add_prompt_monitor(
1073
+ self,
1074
+ name: str,
1075
+ model_id: str,
1076
+ task_id: Literal[
1077
+ "extraction",
1078
+ "generation",
1079
+ "question_answering",
1080
+ "retrieval_augmented_generation",
1081
+ "summarization",
1082
+ ],
1083
+ description: str = "",
1084
+ model_parameters: Dict = None,
1085
+ prompt_variables: List[str] = None,
1086
+ locale: str = "en",
1087
+ input_text: str = None,
1088
+ context_fields: List[str] = None,
1089
+ question_field: str = None,
1090
+ ) -> Dict:
1091
+ return self.create_prompt_monitor(
1092
+ name=name,
1093
+ model_id=model_id,
1094
+ task_id=task_id,
1095
+ description=description,
1096
+ model_parameters=model_parameters,
1097
+ prompt_variables=prompt_variables,
1098
+ locale=locale,
1099
+ input_text=input_text,
1100
+ context_fields=context_fields,
1101
+ question_field=question_field,
1102
+ )
1103
+
1104
+ def create_prompt_monitor(
1105
+ self,
1106
+ name: str,
1107
+ model_id: str,
1108
+ task_id: Literal[
1109
+ "extraction",
1110
+ "generation",
1111
+ "question_answering",
1112
+ "retrieval_augmented_generation",
1113
+ "summarization",
1114
+ ],
1115
+ description: str = "",
1116
+ model_parameters: Dict = None,
1117
+ prompt_variables: List[str] = None,
1118
+ locale: str = "en",
1119
+ input_text: str = None,
1120
+ context_fields: List[str] = None,
1121
+ question_field: str = None,
1122
+ ) -> Dict:
1123
+ """
1124
+ Creates an IBM Prompt Template Asset and ssetup monitor for the given prompt template asset.
1125
+
1126
+ Args:
1127
+ name (str): The name of the Prompt Template Asset.
1128
+ model_id (str): The ID of the model associated with the prompt.
1129
+ task_id (str): The task identifier.
1130
+ description (str, optional): A description of the Prompt Template Asset.
1131
+ model_parameters (Dict, optional): A dictionary of model parameters and their respective values.
1132
+ prompt_variables (List[str], optional): A list of values for prompt input variables.
1133
+ locale (str, optional): Locale code for the input/output language. eg. "en", "pt", "es".
1134
+ input_text (str, optional): The input text for the prompt.
1135
+ context_fields (List[str], optional): A list of fields that will provide context to the prompt.
1136
+ Applicable only for the `retrieval_augmented_generation` task type.
1137
+ question_field (str, optional): The field containing the question to be answered.
1138
+ Applicable only for the `retrieval_augmented_generation` task type.
1139
+
1140
+ Example:
1141
+ ```python
1142
+ wxgov_client.create_prompt_monitor(
1143
+ name="IBM prompt template",
1144
+ model_id="ibm/granite-3-2b-instruct",
1145
+ task_id="retrieval_augmented_generation",
1146
+ prompt_variables=["context1", "context2", "input_query"],
1147
+ input_text="Prompt text to be given",
1148
+ context_fields=["context1", "context2"],
1149
+ question_field="input_query",
1150
+ )
1151
+ ```
1152
+ """
1153
+ if (not (self.project_id or self.space_id)) or (
1154
+ self.project_id and self.space_id
1155
+ ):
1156
+ raise ValueError(
1157
+ "Invalid configuration: Neither was provided: please set either 'project_id' or 'space_id'. "
1158
+ "Both were provided: 'project_id' and 'space_id' cannot be set at the same time."
1159
+ )
1160
+
1161
+ if task_id == "retrieval_augmented_generation":
1162
+ if not context_fields or not question_field:
1163
+ raise ValueError(
1164
+ "For 'retrieval_augmented_generation' task, requires non-empty 'context_fields' and 'question_field'."
1165
+ )
1166
+
1167
+ prompt_metadata = locals()
1168
+ # Remove unused vars from dict
1169
+ prompt_metadata.pop("self", None)
1170
+ prompt_metadata.pop("context_fields", None)
1171
+ prompt_metadata.pop("question_field", None)
1172
+ prompt_metadata.pop("locale", None)
1173
+
1174
+ # Update name of keys to aigov_facts api
1175
+ prompt_metadata["input"] = prompt_metadata.pop("input_text", None)
1176
+
1177
+ # Update list of vars to dict
1178
+ prompt_metadata["prompt_variables"] = Dict.fromkeys(
1179
+ prompt_metadata["prompt_variables"], ""
1180
+ )
1181
+
1182
+ from ibm_cloud_sdk_core.authenticators import IAMAuthenticator # type: ignore
1183
+ from ibm_watson_openscale import APIClient as WosAPIClient # type: ignore
1184
+
1185
+ if not self._wos_client:
1186
+ try:
1187
+ if hasattr(self, "_wos_cpd_creds") and self._wos_cpd_creds:
1188
+ from ibm_cloud_sdk_core.authenticators import (
1189
+ CloudPakForDataAuthenticator, # type: ignore
1190
+ )
1191
+
1192
+ authenticator = CloudPakForDataAuthenticator(**self._wos_cpd_creds)
1193
+
1194
+ self._wos_client = WosAPIClient(
1195
+ authenticator=authenticator,
1196
+ service_url=self._wos_cpd_creds["url"],
1197
+ )
1198
+
1199
+ else:
1200
+ from ibm_cloud_sdk_core.authenticators import (
1201
+ IAMAuthenticator, # type: ignore
1202
+ )
1203
+
1204
+ authenticator = IAMAuthenticator(apikey=self._api_key)
1205
+ self._wos_client = WosAPIClient(
1206
+ authenticator=authenticator,
1207
+ service_url=REGIONS_URL[self.region]["wos"],
1208
+ )
1209
+
1210
+ except Exception as e:
1211
+ logging.error(
1212
+ f"Error connecting to IBM watsonx.governance (openscale): {e}",
1213
+ )
1214
+ raise
1215
+
1216
+ prompt_details = _filter_dict(
1217
+ prompt_metadata,
1218
+ ["prompt_variables", "input", "model_parameters"],
1219
+ )
1220
+
1221
+ asset_details = _filter_dict(
1222
+ prompt_metadata,
1223
+ ["description"],
1224
+ ["name", "model_id", "task_id"],
1225
+ )
1226
+
1227
+ pta_id = suppress_output(
1228
+ self._create_prompt_template, prompt_details, asset_details
1229
+ )
1230
+ deployment_id = None
1231
+ if self._container_type == "space":
1232
+ deployment_id = suppress_output(
1233
+ self._create_deployment_pta, pta_id, name, model_id
1234
+ )
1235
+
1236
+ monitors = {
1237
+ "generative_ai_quality": {
1238
+ "parameters": {"min_sample_size": 10, "metrics_configuration": {}},
1239
+ },
1240
+ }
1241
+
1242
+ max_attempt_execute_prompt_setup = 0
1243
+ while max_attempt_execute_prompt_setup < 2:
1244
+ try:
1245
+ generative_ai_monitor_details = suppress_output(
1246
+ self._wos_client.wos.execute_prompt_setup,
1247
+ prompt_template_asset_id=pta_id,
1248
+ space_id=self.space_id,
1249
+ project_id=self.project_id,
1250
+ deployment_id=deployment_id,
1251
+ label_column="reference_output",
1252
+ context_fields=context_fields,
1253
+ question_field=question_field,
1254
+ operational_space_id=self._deployment_stage,
1255
+ problem_type=task_id,
1256
+ data_input_locale=[locale],
1257
+ generated_output_locale=[locale],
1258
+ input_data_type="unstructured_text",
1259
+ supporting_monitors=monitors,
1260
+ background_mode=False,
1261
+ ).result
1262
+
1263
+ break
1264
+
1265
+ except Exception as e:
1266
+ if (
1267
+ e.code == 403
1268
+ and "The user entitlement does not exist" in e.message
1269
+ and max_attempt_execute_prompt_setup < 1
1270
+ ):
1271
+ max_attempt_execute_prompt_setup = (
1272
+ max_attempt_execute_prompt_setup + 1
1273
+ )
1274
+
1275
+ data_marts = self._wos_client.data_marts.list().result
1276
+ if (data_marts.data_marts is None) or (not data_marts.data_marts):
1277
+ raise ValueError(
1278
+ "Error retrieving IBM watsonx.governance (openscale) data mart. \
1279
+ Make sure the data mart are configured.",
1280
+ )
1281
+
1282
+ data_mart_id = data_marts.data_marts[0].metadata.id
1283
+
1284
+ self._wos_client.wos.add_instance_mapping(
1285
+ service_instance_id=data_mart_id,
1286
+ space_id=self.space_id,
1287
+ project_id=self.project_id,
1288
+ )
1289
+ else:
1290
+ max_attempt_execute_prompt_setup = 2
1291
+ raise
1292
+
1293
+ generative_ai_monitor_details = generative_ai_monitor_details._to_dict()
1294
+
1295
+ return {
1296
+ "prompt_template_asset_id": pta_id,
1297
+ "deployment_id": deployment_id,
1298
+ "subscription_id": generative_ai_monitor_details["subscription_id"],
1299
+ }
1300
+
1301
+ def store_payload_records(
1302
+ self,
1303
+ request_records: List[Dict],
1304
+ subscription_id: str = None,
1305
+ ) -> List[str]:
1306
+ """
1307
+ Stores records to the payload logging system.
1308
+
1309
+ Args:
1310
+ request_records (List[Dict]): A list of records to be logged. Each record is represented as a dictionary.
1311
+ subscription_id (str, optional): The subscription ID associated with the records being logged.
1312
+
1313
+ Example:
1314
+ ```python
1315
+ wxgov_client.store_payload_records(
1316
+ request_records=[
1317
+ {
1318
+ "context1": "value_context1",
1319
+ "context2": "value_context1",
1320
+ "input_query": "What's Beekeeper Framework?",
1321
+ "generated_text": "Beekeeper is a data framework to make AI easier to work with.",
1322
+ "input_token_count": 25,
1323
+ "generated_token_count": 150,
1324
+ }
1325
+ ],
1326
+ subscription_id="5d62977c-a53d-4b6d-bda1-7b79b3b9d1a0",
1327
+ )
1328
+ ```
1329
+ """
1330
+ from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
1331
+ from ibm_watson_openscale import APIClient as WosAPIClient
1332
+ from ibm_watson_openscale.supporting_classes.enums import (
1333
+ DataSetTypes,
1334
+ TargetTypes,
1335
+ )
1336
+
1337
+ # Expected behavior: Prefer using fn `subscription_id`.
1338
+ # Fallback to `self.subscription_id` if `subscription_id` None or empty.
1339
+ _subscription_id = subscription_id or self.subscription_id
1340
+
1341
+ if _subscription_id is None or _subscription_id == "":
1342
+ raise ValueError(
1343
+ "Unexpected value for 'subscription_id': Cannot be None or empty string."
1344
+ )
1345
+
1346
+ if not self._wos_client:
1347
+ try:
1348
+ if hasattr(self, "_wos_cpd_creds") and self._wos_cpd_creds:
1349
+ from ibm_cloud_sdk_core.authenticators import (
1350
+ CloudPakForDataAuthenticator, # type: ignore
1351
+ )
1352
+
1353
+ authenticator = CloudPakForDataAuthenticator(**self._wos_cpd_creds)
1354
+
1355
+ self._wos_client = WosAPIClient(
1356
+ authenticator=authenticator,
1357
+ service_url=self._wos_cpd_creds["url"],
1358
+ )
1359
+
1360
+ else:
1361
+ from ibm_cloud_sdk_core.authenticators import (
1362
+ IAMAuthenticator, # type: ignore
1363
+ )
1364
+
1365
+ authenticator = IAMAuthenticator(apikey=self._api_key)
1366
+ self._wos_client = WosAPIClient(
1367
+ authenticator=authenticator,
1368
+ service_url=REGIONS_URL[self.region]["wos"],
1369
+ )
1370
+
1371
+ except Exception as e:
1372
+ logging.error(
1373
+ f"Error connecting to IBM watsonx.governance (openscale): {e}",
1374
+ )
1375
+ raise
1376
+
1377
+ subscription_details = self._wos_client.subscriptions.get(
1378
+ _subscription_id,
1379
+ ).result
1380
+ subscription_details = json.loads(str(subscription_details))
1381
+
1382
+ feature_fields = subscription_details["entity"]["asset_properties"][
1383
+ "feature_fields"
1384
+ ]
1385
+
1386
+ payload_data_set_id = (
1387
+ self._wos_client.data_sets.list(
1388
+ type=DataSetTypes.PAYLOAD_LOGGING,
1389
+ target_target_id=_subscription_id,
1390
+ target_target_type=TargetTypes.SUBSCRIPTION,
1391
+ )
1392
+ .result.data_sets[0]
1393
+ .metadata.id
1394
+ )
1395
+
1396
+ payload_data = _convert_payload_format(request_records, feature_fields)
1397
+
1398
+ suppress_output(
1399
+ self._wos_client.data_sets.store_records,
1400
+ data_set_id=payload_data_set_id,
1401
+ request_body=payload_data,
1402
+ background_mode=False,
1403
+ )
1404
+
1405
+ return [data["scoring_id"] + "-1" for data in payload_data]
1406
+
1407
+ def __call__(self, payload: PayloadRecord) -> None:
1408
+ if self.prompt_template:
1409
+ template_vars = extract_template_vars(
1410
+ self.prompt_template.template, payload.input_text
1411
+ )
1412
+
1413
+ if not template_vars:
1414
+ self.store_payload_records([payload.model_dump()])
1415
+ else:
1416
+ self.store_payload_records([{**payload.model_dump(), **template_vars}])
1417
+
1418
+
1419
+ # ===== Supporting Classes =====
1420
+ class WatsonxLocalMetric(BaseModel):
1421
+ """
1422
+ Provides the IBM watsonx.governance local monitor metric definition.
1423
+
1424
+ Attributes:
1425
+ name (str): The name of the metric.
1426
+ data_type (str): The data type of the metric. Currently supports "string", "integer", "double", and "timestamp".
1427
+ nullable (bool, optional): Indicates whether the metric can be null. Defaults to `False`.
1428
+
1429
+ Example:
1430
+ ```python
1431
+ from beekeeper.monitors.watsonx import WatsonxLocalMetric
1432
+
1433
+ WatsonxLocalMetric(name="context_quality", data_type="double")
1434
+ ```
1435
+ """
1436
+
1437
+ name: str
1438
+ data_type: Literal["string", "integer", "double", "timestamp"]
1439
+ nullable: bool = True
1440
+
1441
+ def to_dict(self) -> Dict:
1442
+ return {"name": self.name, "type": self.data_type, "nullable": self.nullable}
1443
+
1444
+
1445
+ class WatsonxMetricThreshold(BaseModel):
1446
+ """
1447
+ Defines the metric threshold for IBM watsonx.governance.
1448
+
1449
+ Attributes:
1450
+ threshold_type (str): The threshold type. Can be either `lower_limit` or `upper_limit`.
1451
+ default_value (float): The metric threshold value.
1452
+
1453
+ Example:
1454
+ ```python
1455
+ from beekeeper.monitors.watsonx import WatsonxMetricThreshold
1456
+
1457
+ WatsonxMetricThreshold(threshold_type="lower_limit", default_value=0.8)
1458
+ ```
1459
+ """
1460
+
1461
+ threshold_type: Literal["lower_limit", "upper_limit"]
1462
+ default_value: float = None
1463
+
1464
+ def to_dict(self) -> Dict:
1465
+ return {"type": self.threshold_type, "default": self.default_value}
1466
+
1467
+
1468
+ class WatsonxMetric(BaseModel):
1469
+ """
1470
+ Defines the IBM watsonx.governance global monitor metric.
1471
+
1472
+ Attributes:
1473
+ name (str): The name of the metric.
1474
+ applies_to (List[str]): A list of task types that the metric applies to. Currently supports:
1475
+ "summarization", "generation", "question_answering", "extraction", and "retrieval_augmented_generation".
1476
+ thresholds (List[WatsonxMetricThreshold]): A list of metric thresholds associated with the metric.
1477
+
1478
+ Example:
1479
+ ```python
1480
+ from beekeeper.monitors.watsonx import (
1481
+ WatsonxMetric,
1482
+ WatsonxMetricThreshold,
1483
+ )
1484
+
1485
+ WatsonxMetric(
1486
+ name="context_quality",
1487
+ applies_to=["retrieval_augmented_generation", "summarization"],
1488
+ thresholds=[
1489
+ WatsonxMetricThreshold(threshold_type="lower_limit", default_value=0.75)
1490
+ ],
1491
+ )
1492
+ ```
1493
+ """
1494
+
1495
+ name: str
1496
+ applies_to: List[
1497
+ Literal[
1498
+ "summarization",
1499
+ "generation",
1500
+ "question_answering",
1501
+ "extraction",
1502
+ "retrieval_augmented_generation",
1503
+ ]
1504
+ ]
1505
+ thresholds: Optional[List[WatsonxMetricThreshold]] = None
1506
+
1507
+ def to_dict(self) -> Dict:
1508
+ from ibm_watson_openscale.base_classes.watson_open_scale_v2 import (
1509
+ ApplicabilitySelection,
1510
+ MetricThreshold,
1511
+ )
1512
+
1513
+ monitor_metric = {
1514
+ "name": self.name,
1515
+ "applies_to": ApplicabilitySelection(problem_type=self.applies_to),
1516
+ }
1517
+
1518
+ if self.thresholds is not None:
1519
+ monitor_metric["thresholds"] = [
1520
+ MetricThreshold(**threshold.to_dict()) for threshold in self.thresholds
1521
+ ]
1522
+
1523
+ return monitor_metric
1524
+
1525
+
1526
+ # ===== Metric Classes =====
1527
+
1528
+
1529
+ class WatsonxCustomMetricsManager:
1530
+ """
1531
+ Provides functionality to set up a custom metric to measure your model's performance with IBM watsonx.governance.
1532
+
1533
+ Attributes:
1534
+ api_key (str): The API key for IBM watsonx.governance.
1535
+ region (str, optional): The region where IBM watsonx.governance is hosted when using IBM Cloud.
1536
+ Defaults to `us-south`.
1537
+ cpd_creds (CloudPakforDataCredentials, optional): IBM Cloud Pak for Data environment credentials.
1538
+
1539
+ Example:
1540
+ ```python
1541
+ from beekeeper.monitors.watsonx import (
1542
+ WatsonxCustomMetricsManager,
1543
+ CloudPakforDataCredentials,
1544
+ )
1545
+
1546
+ # watsonx.governance (IBM Cloud)
1547
+ wxgov_client = WatsonxCustomMetricsManager(api_key="API_KEY")
1548
+
1549
+ # watsonx.governance (CP4D)
1550
+ cpd_creds = CloudPakforDataCredentials(
1551
+ url="CPD_URL",
1552
+ username="USERNAME",
1553
+ password="PASSWORD",
1554
+ version="5.0",
1555
+ instance_id="openshift",
1556
+ )
1557
+
1558
+ wxgov_client = WatsonxCustomMetricsManager(cpd_creds=cpd_creds)
1559
+ ```
1560
+ """
1561
+
1562
+ def __init__(
1563
+ self,
1564
+ api_key: str = None,
1565
+ region: Literal["us-south", "eu-de", "au-syd"] = "us-south",
1566
+ cpd_creds: CloudPakforDataCredentials | Dict = None,
1567
+ ) -> None:
1568
+ from ibm_cloud_sdk_core.authenticators import IAMAuthenticator # type: ignore
1569
+ from ibm_watson_openscale import APIClient as WosAPIClient # type: ignore
1570
+
1571
+ self.region = region
1572
+ self._api_key = api_key
1573
+ self._wos_client = None
1574
+
1575
+ if cpd_creds:
1576
+ self._wos_cpd_creds = _filter_dict(
1577
+ cpd_creds.to_dict(),
1578
+ ["username", "password", "api_key", "disable_ssl_verification"],
1579
+ ["url"],
1580
+ )
1581
+
1582
+ if not self._wos_client:
1583
+ try:
1584
+ if hasattr(self, "_wos_cpd_creds") and self._wos_cpd_creds:
1585
+ from ibm_cloud_sdk_core.authenticators import (
1586
+ CloudPakForDataAuthenticator, # type: ignore
1587
+ )
1588
+
1589
+ authenticator = CloudPakForDataAuthenticator(**self._wos_cpd_creds)
1590
+
1591
+ self._wos_client = WosAPIClient(
1592
+ authenticator=authenticator,
1593
+ service_url=self._wos_cpd_creds["url"],
1594
+ )
1595
+
1596
+ else:
1597
+ from ibm_cloud_sdk_core.authenticators import (
1598
+ IAMAuthenticator, # type: ignore
1599
+ )
1600
+
1601
+ authenticator = IAMAuthenticator(apikey=self._api_key)
1602
+ self._wos_client = WosAPIClient(
1603
+ authenticator=authenticator,
1604
+ service_url=REGIONS_URL[self.region]["wos"],
1605
+ )
1606
+
1607
+ except Exception as e:
1608
+ logging.error(
1609
+ f"Error connecting to IBM watsonx.governance (openscale): {e}",
1610
+ )
1611
+ raise
1612
+
1613
+ def _add_integrated_system(
1614
+ self,
1615
+ credentials: IntegratedSystemCredentials,
1616
+ name: str,
1617
+ endpoint: str,
1618
+ ) -> str:
1619
+ custom_metrics_integrated_system = self._wos_client.integrated_systems.add(
1620
+ name=name,
1621
+ description="Integrated system created by Beekeeper.",
1622
+ type="custom_metrics_provider",
1623
+ credentials=credentials.to_dict(),
1624
+ connection={"display_name": name, "endpoint": endpoint},
1625
+ ).result
1626
+
1627
+ return custom_metrics_integrated_system.metadata.id
1628
+
1629
+ def _add_monitor_definitions(
1630
+ self,
1631
+ name: str,
1632
+ metrics: List[WatsonxMetric],
1633
+ schedule: bool,
1634
+ ):
1635
+ from ibm_watson_openscale.base_classes.watson_open_scale_v2 import (
1636
+ ApplicabilitySelection,
1637
+ MonitorInstanceSchedule,
1638
+ MonitorMetricRequest,
1639
+ MonitorRuntime,
1640
+ ScheduleStartTime,
1641
+ )
1642
+
1643
+ _metrics = [MonitorMetricRequest(**metric.to_dict()) for metric in metrics]
1644
+ _monitor_runtime = None
1645
+ _monitor_schedule = None
1646
+
1647
+ if schedule:
1648
+ _monitor_runtime = MonitorRuntime(type="custom_metrics_provider")
1649
+ _monitor_schedule = MonitorInstanceSchedule(
1650
+ repeat_interval=1,
1651
+ repeat_unit="hour",
1652
+ start_time=ScheduleStartTime(
1653
+ type="relative",
1654
+ delay_unit="minute",
1655
+ delay=30,
1656
+ ),
1657
+ )
1658
+
1659
+ custom_monitor_details = self._wos_client.monitor_definitions.add(
1660
+ name=name,
1661
+ metrics=_metrics,
1662
+ tags=[],
1663
+ schedule=_monitor_schedule,
1664
+ applies_to=ApplicabilitySelection(input_data_type=["unstructured_text"]),
1665
+ monitor_runtime=_monitor_runtime,
1666
+ background_mode=False,
1667
+ ).result
1668
+
1669
+ return custom_monitor_details.metadata.id
1670
+
1671
+ def _get_monitor_instance(self, subscription_id: str, monitor_definition_id: str):
1672
+ monitor_instances = self._wos_client.monitor_instances.list(
1673
+ monitor_definition_id=monitor_definition_id,
1674
+ target_target_id=subscription_id,
1675
+ ).result.monitor_instances
1676
+
1677
+ if len(monitor_instances) == 1:
1678
+ return monitor_instances[0]
1679
+ else:
1680
+ return None
1681
+
1682
+ def _update_monitor_instance(
1683
+ self,
1684
+ integrated_system_id: str,
1685
+ custom_monitor_id: str,
1686
+ ):
1687
+ payload = [
1688
+ {
1689
+ "op": "replace",
1690
+ "path": "/parameters",
1691
+ "value": {
1692
+ "custom_metrics_provider_id": integrated_system_id,
1693
+ "custom_metrics_wait_time": 60,
1694
+ "enable_custom_metric_runs": True,
1695
+ },
1696
+ },
1697
+ ]
1698
+
1699
+ return self._wos_client.monitor_instances.update(
1700
+ custom_monitor_id,
1701
+ payload,
1702
+ update_metadata_only=True,
1703
+ ).result
1704
+
1705
+ def _get_patch_request_field(
1706
+ self,
1707
+ field_path: str,
1708
+ field_value: Any,
1709
+ op_name: str = "replace",
1710
+ ) -> Dict:
1711
+ return {"op": op_name, "path": field_path, "value": field_value}
1712
+
1713
+ def _get_dataset_id(
1714
+ self,
1715
+ subscription_id: str,
1716
+ data_set_type: Literal["feedback", "payload_logging"],
1717
+ ) -> str:
1718
+ data_sets = self._wos_client.data_sets.list(
1719
+ target_target_id=subscription_id,
1720
+ type=data_set_type,
1721
+ ).result.data_sets
1722
+ data_set_id = None
1723
+ if len(data_sets) > 0:
1724
+ data_set_id = data_sets[0].metadata.id
1725
+ return data_set_id
1726
+
1727
+ def _get_dataset_data(self, data_set_id: str):
1728
+ json_data = self._wos_client.data_sets.get_list_of_records(
1729
+ data_set_id=data_set_id,
1730
+ format="list",
1731
+ ).result
1732
+
1733
+ if not json_data.get("records"):
1734
+ return None
1735
+
1736
+ return json_data["records"][0]
1737
+
1738
+ def _get_existing_data_mart(self):
1739
+ data_marts = self._wos_client.data_marts.list().result.data_marts
1740
+ if len(data_marts) == 0:
1741
+ raise Exception(
1742
+ "No data marts found. Please ensure at least one data mart is available.",
1743
+ )
1744
+
1745
+ return data_marts[0].metadata.id
1746
+
1747
+ # ===== Global Custom Metrics =====
1748
+ @deprecated(
1749
+ reason="'add_metric_definition()' is deprecated and will be removed in a future version. Use 'create_metric_definition()' instead.",
1750
+ version="1.0.6",
1751
+ action="always",
1752
+ )
1753
+ def add_metric_definition(
1754
+ self,
1755
+ name: str,
1756
+ metrics: List[WatsonxMetric],
1757
+ integrated_system_url: str,
1758
+ integrated_system_credentials: IntegratedSystemCredentials,
1759
+ schedule: bool = False,
1760
+ ):
1761
+ return self.create_metric_definition(
1762
+ name=name,
1763
+ metrics=metrics,
1764
+ integrated_system_url=integrated_system_url,
1765
+ integrated_system_credentials=integrated_system_credentials,
1766
+ schedule=schedule,
1767
+ )
1768
+
1769
+ def create_metric_definition(
1770
+ self,
1771
+ name: str,
1772
+ metrics: List[WatsonxMetric],
1773
+ integrated_system_url: str,
1774
+ integrated_system_credentials: IntegratedSystemCredentials,
1775
+ schedule: bool = False,
1776
+ ):
1777
+ """
1778
+ Creates a custom metric definition for IBM watsonx.governance.
1779
+
1780
+ This must be done before using custom metrics.
1781
+
1782
+ Args:
1783
+ name (str): The name of the custom metric group.
1784
+ metrics (List[WatsonxMetric]): A list of metrics to be measured.
1785
+ schedule (bool, optional): Enable or disable the scheduler. Defaults to `False`.
1786
+ integrated_system_url (str): The URL of the external metric provider.
1787
+ integrated_system_credentials (IntegratedSystemCredentials): The credentials for the integrated system.
1788
+
1789
+ Example:
1790
+ ```python
1791
+ from beekeeper.monitors.watsonx import (
1792
+ WatsonxMetric,
1793
+ IntegratedSystemCredentials,
1794
+ WatsonxMetricThreshold,
1795
+ )
1796
+
1797
+ wxgov_client.create_metric_definition(
1798
+ name="Custom Metric - Custom LLM Quality",
1799
+ metrics=[
1800
+ WatsonxMetric(
1801
+ name="context_quality",
1802
+ applies_to=[
1803
+ "retrieval_augmented_generation",
1804
+ "summarization",
1805
+ ],
1806
+ thresholds=[
1807
+ WatsonxMetricThreshold(
1808
+ threshold_type="lower_limit", default_value=0.75
1809
+ )
1810
+ ],
1811
+ )
1812
+ ],
1813
+ integrated_system_url="IS_URL", # URL to the endpoint computing the metric
1814
+ integrated_system_credentials=IntegratedSystemCredentials(
1815
+ auth_type="basic", username="USERNAME", password="PASSWORD"
1816
+ ),
1817
+ )
1818
+ ```
1819
+ """
1820
+ integrated_system_id = self._add_integrated_system(
1821
+ integrated_system_credentials,
1822
+ name,
1823
+ integrated_system_url,
1824
+ )
1825
+
1826
+ external_monitor_id = suppress_output(
1827
+ self._add_monitor_definitions,
1828
+ name,
1829
+ metrics,
1830
+ schedule,
1831
+ )
1832
+
1833
+ # Associate the external monitor with the integrated system
1834
+ payload = [
1835
+ {
1836
+ "op": "add",
1837
+ "path": "/parameters",
1838
+ "value": {"monitor_definition_ids": [external_monitor_id]},
1839
+ },
1840
+ ]
1841
+
1842
+ self._wos_client.integrated_systems.update(integrated_system_id, payload)
1843
+
1844
+ return {
1845
+ "integrated_system_id": integrated_system_id,
1846
+ "monitor_definition_id": external_monitor_id,
1847
+ }
1848
+
1849
+ @deprecated(
1850
+ reason="'add_observer_instance()' is deprecated and will be removed in a future version. Use 'attach_monitor_instance()' from 'beekeeper-monitors-watsonx' instead.",
1851
+ version="1.0.5",
1852
+ action="always",
1853
+ )
1854
+ def add_observer_instance(
1855
+ self,
1856
+ integrated_system_id: str,
1857
+ monitor_definition_id: str,
1858
+ subscription_id: str,
1859
+ ):
1860
+ return self.attach_monitor_instance(
1861
+ integrated_system_id=integrated_system_id,
1862
+ monitor_definition_id=monitor_definition_id,
1863
+ subscription_id=subscription_id,
1864
+ )
1865
+
1866
+ @deprecated(
1867
+ reason="'add_monitor_instance()' is deprecated and will be removed in a future version. Use 'attach_monitor_instance()' from 'beekeeper-monitors-watsonx' instead.",
1868
+ version="1.0.6",
1869
+ action="always",
1870
+ )
1871
+ def add_monitor_instance(
1872
+ self,
1873
+ integrated_system_id: str,
1874
+ monitor_definition_id: str,
1875
+ subscription_id: str,
1876
+ ):
1877
+ return self.attach_monitor_instance(
1878
+ integrated_system_id=integrated_system_id,
1879
+ monitor_definition_id=monitor_definition_id,
1880
+ subscription_id=subscription_id,
1881
+ )
1882
+
1883
+ def attach_monitor_instance(
1884
+ self,
1885
+ integrated_system_id: str,
1886
+ monitor_definition_id: str,
1887
+ subscription_id: str,
1888
+ ):
1889
+ """
1890
+ Attaches the specified monitor definition to the specified subscription.
1891
+
1892
+ Args:
1893
+ integrated_system_id (str): The ID of the integrated system.
1894
+ monitor_definition_id (str): The ID of the custom metric monitor instance.
1895
+ subscription_id (str): The ID of the subscription to associate the monitor with.
1896
+
1897
+ Example:
1898
+ ```python
1899
+ wxgov_client.attach_monitor_instance(
1900
+ integrated_system_id="019667ca-5687-7838-8d29-4ff70c2b36b0",
1901
+ monitor_definition_id="custom_llm_quality",
1902
+ subscription_id="0195e95d-03a4-7000-b954-b607db10fe9e",
1903
+ )
1904
+ ```
1905
+ """
1906
+ from ibm_watson_openscale.base_classes.watson_open_scale_v2 import Target
1907
+
1908
+ data_marts = self._wos_client.data_marts.list().result.data_marts
1909
+ if len(data_marts) == 0:
1910
+ raise Exception(
1911
+ "No data marts found. Please ensure at least one data mart is available.",
1912
+ )
1913
+
1914
+ data_mart_id = data_marts[0].metadata.id
1915
+ existing_monitor_instance = self._get_monitor_instance(
1916
+ subscription_id,
1917
+ monitor_definition_id,
1918
+ )
1919
+
1920
+ if existing_monitor_instance is None:
1921
+ target = Target(target_type="subscription", target_id=subscription_id)
1922
+
1923
+ parameters = {
1924
+ "custom_metrics_provider_id": integrated_system_id,
1925
+ "custom_metrics_wait_time": 60,
1926
+ "enable_custom_metric_runs": True,
1927
+ }
1928
+
1929
+ monitor_instance_details = suppress_output(
1930
+ self._wos_client.monitor_instances.create,
1931
+ data_mart_id=data_mart_id,
1932
+ background_mode=False,
1933
+ monitor_definition_id=monitor_definition_id,
1934
+ target=target,
1935
+ parameters=parameters,
1936
+ ).result
1937
+ else:
1938
+ existing_instance_id = existing_monitor_instance.metadata.id
1939
+ monitor_instance_details = self._update_monitor_instance(
1940
+ integrated_system_id,
1941
+ existing_instance_id,
1942
+ )
1943
+
1944
+ return monitor_instance_details
1945
+
1946
+ def publish_metrics(
1947
+ self,
1948
+ monitor_instance_id: str,
1949
+ run_id: str,
1950
+ request_records: Dict[str, Union[float, int]],
1951
+ ):
1952
+ """
1953
+ Publishes computed metrics to the specified global monitor instance.
1954
+
1955
+ Args:
1956
+ monitor_instance_id (str): The unique ID of the monitor instance.
1957
+ run_id (str): The ID of the monitor run that generated the metrics.
1958
+ request_records (Dict[str | float | int]): Dict containing the metrics to be published.
1959
+
1960
+ Example:
1961
+ ```python
1962
+ wxgov_client.publish_metrics(
1963
+ monitor_instance_id="01966801-f9ee-7248-a706-41de00a8a998",
1964
+ run_id="RUN_ID",
1965
+ request_records={"context_quality": 0.914, "sensitivity": 0.85},
1966
+ )
1967
+ ```
1968
+ """
1969
+ from ibm_watson_openscale.base_classes.watson_open_scale_v2 import (
1970
+ MonitorMeasurementRequest,
1971
+ Runs,
1972
+ )
1973
+
1974
+ measurement_request = MonitorMeasurementRequest(
1975
+ timestamp=datetime.datetime.now(datetime.timezone.utc).strftime(
1976
+ "%Y-%m-%dT%H:%M:%S.%fZ",
1977
+ ),
1978
+ run_id=run_id,
1979
+ metrics=[request_records],
1980
+ )
1981
+
1982
+ self._wos_client.monitor_instances.add_measurements(
1983
+ monitor_instance_id=monitor_instance_id,
1984
+ monitor_measurement_request=[measurement_request],
1985
+ ).result
1986
+
1987
+ run = Runs(watson_open_scale=self._wos_client)
1988
+ patch_payload = []
1989
+ patch_payload.append(self._get_patch_request_field("/status/state", "finished"))
1990
+ patch_payload.append(
1991
+ self._get_patch_request_field(
1992
+ "/status/completed_at",
1993
+ datetime.datetime.now(datetime.timezone.utc).strftime(
1994
+ "%Y-%m-%dT%H:%M:%S.%fZ",
1995
+ ),
1996
+ ),
1997
+ )
1998
+
1999
+ return run.update(
2000
+ monitor_instance_id=monitor_instance_id,
2001
+ monitoring_run_id=run_id,
2002
+ json_patch_operation=patch_payload,
2003
+ ).result
2004
+
2005
+ # ===== Local Custom Metrics =====
2006
+ @deprecated(
2007
+ reason="'add_local_metric_definition()' is deprecated and will be removed in a future version. Use 'create_local_metric_definition()' from 'beekeeper-monitors-watsonx' instead.",
2008
+ version="1.0.6",
2009
+ action="always",
2010
+ )
2011
+ def add_local_metric_definition(
2012
+ self,
2013
+ name: str,
2014
+ metrics: List[WatsonxMetric],
2015
+ subscription_id: str,
2016
+ ):
2017
+ return self.create_local_metric_definition(
2018
+ name=name,
2019
+ metrics=metrics,
2020
+ subscription_id=subscription_id,
2021
+ )
2022
+
2023
+ def create_local_metric_definition(
2024
+ self,
2025
+ name: str,
2026
+ metrics: List[WatsonxLocalMetric],
2027
+ subscription_id: str,
2028
+ ) -> str:
2029
+ """
2030
+ Creates a custom metric definition to compute metrics at the local (transaction) level for IBM watsonx.governance.
2031
+
2032
+ Args:
2033
+ name (str): The name of the custom transaction metric group.
2034
+ metrics (List[WatsonxLocalMetric]): A list of metrics to be monitored at the local (transaction) level.
2035
+ subscription_id (str): The IBM watsonx.governance subscription ID associated with the metric definition.
2036
+
2037
+ Example:
2038
+ ```python
2039
+ from beekeeper.monitors.watsonx import WatsonxLocalMetric
2040
+
2041
+ wxgov_client.create_local_metric_definition(
2042
+ name="Custom LLM Local Metric",
2043
+ subscription_id="019674ca-0c38-745f-8e9b-58546e95174e",
2044
+ metrics=[
2045
+ WatsonxLocalMetric(name="context_quality", data_type="double")
2046
+ ],
2047
+ )
2048
+ ```
2049
+ """
2050
+ from ibm_watson_openscale.base_classes.watson_open_scale_v2 import (
2051
+ LocationTableName,
2052
+ SparkStruct,
2053
+ SparkStructFieldPrimitive,
2054
+ Target,
2055
+ )
2056
+
2057
+ target = Target(target_id=subscription_id, target_type="subscription")
2058
+ data_mart_id = self._get_existing_data_mart()
2059
+ metrics = [SparkStructFieldPrimitive(**metric.to_dict()) for metric in metrics]
2060
+
2061
+ schema_fields = [
2062
+ SparkStructFieldPrimitive(
2063
+ name="scoring_id",
2064
+ type="string",
2065
+ nullable=False,
2066
+ ),
2067
+ SparkStructFieldPrimitive(
2068
+ name="run_id",
2069
+ type="string",
2070
+ nullable=True,
2071
+ ),
2072
+ SparkStructFieldPrimitive(
2073
+ name="computed_on",
2074
+ type="string",
2075
+ nullable=False,
2076
+ ),
2077
+ ]
2078
+
2079
+ schema_fields.extend(metrics)
2080
+
2081
+ data_schema = SparkStruct(type="struct", fields=schema_fields)
2082
+
2083
+ return self._wos_client.data_sets.add(
2084
+ target=target,
2085
+ name=name,
2086
+ type="custom",
2087
+ data_schema=data_schema,
2088
+ data_mart_id=data_mart_id,
2089
+ location=LocationTableName(
2090
+ table_name=name.lower().replace(" ", "_") + "_" + str(uuid.uuid4())[:8],
2091
+ ),
2092
+ background_mode=False,
2093
+ ).result.metadata.id
2094
+
2095
+ def publish_local_metrics(
2096
+ self,
2097
+ metric_instance_id: str,
2098
+ request_records: List[Dict],
2099
+ ):
2100
+ """
2101
+ Publishes computed metrics to the specified transaction record.
2102
+
2103
+ Args:
2104
+ metric_instance_id (str): The unique ID of the custom transaction metric.
2105
+ request_records (List[Dict]): A list of dictionaries containing the records to be stored.
2106
+
2107
+ Example:
2108
+ ```python
2109
+ wxgov_client.publish_local_metrics(
2110
+ metric_instance_id="0196ad39-1b75-7e77-bddb-cc5393d575c2",
2111
+ request_records=[
2112
+ {
2113
+ "scoring_id": "304a9270-44a1-4c4d-bfd4-f756541011f8",
2114
+ "run_id": "RUN_ID",
2115
+ "computed_on": "payload",
2116
+ "context_quality": 0.786,
2117
+ }
2118
+ ],
2119
+ )
2120
+ ```
2121
+ """
2122
+ return self._wos_client.data_sets.store_records(
2123
+ data_set_id=metric_instance_id,
2124
+ request_body=request_records,
2125
+ ).result
2126
+
2127
+ def list_local_metrics(
2128
+ self,
2129
+ metric_instance_id: str,
2130
+ ):
2131
+ """
2132
+ Lists records from a custom local metric definition.
2133
+
2134
+ Args:
2135
+ metric_instance_id (str): The unique ID of the custom transaction metric.
2136
+
2137
+ Example:
2138
+ ```python
2139
+ wxgov_client.list_local_metrics(
2140
+ metric_instance_id="0196ad47-c505-73c0-9d7b-91c082b697e3"
2141
+ )
2142
+ ```
2143
+ """
2144
+ return self._get_dataset_data(metric_instance_id)
2145
+
2146
+
2147
+ @deprecated(
2148
+ reason="'WatsonxCustomMetric()' is deprecated and will be removed in a future version. "
2149
+ "Use 'WatsonxCustomMetricsManager' instead.",
2150
+ version="1.0.6",
2151
+ action="always",
2152
+ )
2153
+ class WatsonxCustomMetric(WatsonxCustomMetricsManager):
2154
+ pass