-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathagent.py
More file actions
634 lines (501 loc) · 20.6 KB
/
Copy pathagent.py
File metadata and controls
634 lines (501 loc) · 20.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
from .agent_tool import DSSAgentTool, DSSAgentToolListItem
from .utils import DSSTaggableObjectListItem, DSSTaggableObjectSettings
class DSSAgentInteractionLoggingSettings(object):
"""
Settings for agent interaction logging.
.. important::
Do not instantiate this class directly, use :attr:`dataikuapi.dss.agent.DSSAgentInteractionLoggingSelection.settings` instead.
"""
CONTENT_MODE_FULL = "FULL"
CONTENT_MODE_NO_LOGS = "NO_LOGS"
CONTENT_MODE_NO_LOGS_NO_TRACE = "NO_LOGS_NO_TRACE"
_VALID_CONTENT_MODES = (
CONTENT_MODE_FULL,
CONTENT_MODE_NO_LOGS,
CONTENT_MODE_NO_LOGS_NO_TRACE,
)
def __init__(self, settings):
self._settings = settings
def get_raw(self):
"""
Returns the raw interaction logging settings.
:rtype: dict
"""
return self._settings
def get(self, key, default=None):
return self._settings.get(key, default)
def __getitem__(self, key):
return self._settings[key]
def __setitem__(self, key, value):
if key == "writeAsUser":
raise ValueError("writeAsUser is read-only")
self._settings[key] = value
@property
def dataset_name(self):
"""
The dataset name used for interaction logging.
:rtype: str | None
"""
return self._settings.get("datasetName")
@dataset_name.setter
def dataset_name(self, value):
self._settings["datasetName"] = value
@property
def write_as_user(self):
"""
The DSS user used to write logs.
This value is read-only and is set automatically to the user who saves
the agent settings.
:rtype: str | None
"""
return self._settings.get("writeAsUser")
@property
def flush_every_s(self):
"""
The flush interval, in seconds.
:rtype: int | None
"""
return self._settings.get("flushEveryS")
@flush_every_s.setter
def flush_every_s(self, value):
self._settings["flushEveryS"] = value
@property
def flush_every_bytes(self):
"""
The maximum buffered payload size before a flush.
:rtype: int | None
"""
return self._settings.get("flushEveryBytes")
@flush_every_bytes.setter
def flush_every_bytes(self, value):
self._settings["flushEveryBytes"] = value
@property
def content_mode(self):
"""
The content logging mode.
:rtype: str | None
"""
return self._settings.get("contentMode")
@content_mode.setter
def content_mode(self, value):
if value not in self._VALID_CONTENT_MODES:
raise ValueError("Invalid content mode: %s" % value)
self._settings["contentMode"] = value
class DSSAgentInteractionLoggingSelection(object):
"""
Selection for agent interaction logging.
.. important::
Do not instantiate this class directly, use :attr:`dataikuapi.dss.agent.DSSAgentVersionSettings.interaction_logging_selection` instead.
"""
MODE_INHERIT = "INHERIT"
MODE_EXPLICIT = "EXPLICIT"
MODE_NONE = "NONE"
_VALID_MODES = (
MODE_INHERIT,
MODE_EXPLICIT,
MODE_NONE,
)
def __init__(self, selection):
self._selection = selection
def get_raw(self):
"""
Returns the raw interaction logging selection.
:rtype: dict
"""
return self._selection
def get(self, key, default=None):
return self._selection.get(key, default)
def __getitem__(self, key):
return self._selection[key]
def __setitem__(self, key, value):
self._selection[key] = value
def _get_or_create_settings_raw(self):
settings = self._selection.get("explicitSettings")
if settings is None:
settings = {}
self._selection["explicitSettings"] = settings
return settings
@property
def mode(self):
"""
The interaction logging mode. One of INHERIT, EXPLICIT or NONE.
In ``INHERIT`` mode, settings are inherited from the project-level
configuration.
:rtype: str | None
"""
return self._selection.get("mode")
@mode.setter
def mode(self, value):
if value not in self._VALID_MODES:
raise ValueError("Invalid interaction logging mode: %s" % value)
self._selection["mode"] = value
@property
def settings(self):
"""
The explicit interaction logging settings.
These settings are only used when the selection is in ``EXPLICIT`` mode.
:rtype: :class:`dataikuapi.dss.agent.DSSAgentInteractionLoggingSettings`
"""
return DSSAgentInteractionLoggingSettings(self._get_or_create_settings_raw())
@settings.setter
def settings(self, value):
if isinstance(value, DSSAgentInteractionLoggingSettings):
value = value.get_raw()
if value is None:
value = {}
self._selection["explicitSettings"] = value
def enable(self, dataset_name, settings=None):
"""
Enable interaction logging on this agent version with explicit settings.
This only controls the agent version setting itself. Interaction logging
can still be effectively unavailable if it is disabled at the instance level.
:param dataset_name: Dataset name used for interaction logging.
:type dataset_name: str
:param settings: Optional explicit settings payload or
:class:`dataikuapi.dss.agent.DSSAgentInteractionLoggingSettings`.
:type settings: dict | :class:`dataikuapi.dss.agent.DSSAgentInteractionLoggingSettings` | None
"""
self.mode = self.MODE_EXPLICIT
if settings is not None:
self.settings = settings
self.settings.dataset_name = dataset_name
def inherit(self):
"""
Enable interaction logging on this agent version in inherited mode.
In this mode, the version inherits the project-level interaction logging settings.
"""
self.mode = self.MODE_INHERIT
def disable(self):
"""
Disable interaction logging on this agent version.
"""
self.mode = self.MODE_NONE
# Neutral aliases for shared LLM interaction logging concepts.
DSSLLMInteractionLoggingSettings = DSSAgentInteractionLoggingSettings
DSSLLMInteractionLoggingSelection = DSSAgentInteractionLoggingSelection
class DSSAgentListItem(DSSTaggableObjectListItem):
"""
An item in a list of agents
.. important::
Do not instantiate this class directly, instead use :meth:`dataikuapi.dss.project.DSSProject.list_agents`.
"""
def __init__(self, client, data):
super(DSSAgentListItem, self).__init__(data)
self.client = client
@property
def project_key(self):
"""
:returns: The project
:rtype: string
"""
return self._data["projectKey"]
@property
def id(self):
"""
:returns: The id of the agent.
:rtype: string
"""
return self._data["id"]
@property
def name(self):
"""
:returns: The name of the agent.
:rtype: string
"""
return self._data["name"]
def as_llm(self):
"""
:returns: this agent as a usable :class:`dataikuapi.dss.llm.DSSLLM` for querying
:rtype: dataikuapi.dss.llm.DSSLLM
"""
return self.client.get_project(self.project_key).get_llm("agent:%s" % self.id)
class DSSAgent(object):
"""
A handle to interact with a DSS-managed agent.
.. important::
Do not create this class directly, use :meth:`dataikuapi.dss.project.DSSProject.get_agent` instead.
"""
def __init__(self, client, project_key, id):
self.client = client
self.project_key = project_key
self._id = id
@property
def id(self):
return self._id
def as_llm(self):
"""
:returns: this agent as a usable :class:`dataikuapi.dss.llm.DSSLLM` for querying
:rtype: dataikuapi.dss.llm.DSSLLM
"""
return self.client.get_project(self.project_key).get_llm("agent:%s" % self.id)
def get_settings(self):
"""
:return: a handle on the agent's definition
:rtype: :class:`dataikuapi.dss.agent.DSSAgentSettings`
"""
settings = self.client._perform_json(
"GET", "/projects/%s/agents/%s" % (self.project_key, self.id))
return DSSAgentSettings(self.client, settings)
def delete(self):
"""
Delete the agent
"""
return self.client._perform_empty("DELETE", "/projects/%s/agents/%s" % (self.project_key, self.id))
def shutdown(self, version_id=None, force=False):
"""
Shutdown all instances of the given version of this agent
:param version_id: If unspecified, uses the active version.
:type version_id: str | None
:param force: If True, cancel requests being processed and stop the instances. If False, let those active requests complete before stopping.
:type force: bool
"""
return self.client._perform_empty(
"POST", "/projects/%s/agents/%s/actions/shutdown" % (self.project_key, self.id), body={
"versionId": version_id,
"force": force
})
def status(self, version_id=None):
"""
Query status of instances of the given version of this agent
:param version_id: If unspecified, uses the active version.
:type version_id: str | None
:return: A dict holding the list of the status for each instance.
"""
return self.client._perform_json(
"GET", "/projects/%s/agents/%s/status" % (self.project_key, self.id), body={
"versionId": version_id
})
def wake_up(self, version_id=None):
"""
Start an instance of an agent if none is started
:param version_id: If unspecified, uses the active version.
:type version_id: str | None
"""
return self.client._perform_empty(
"POST", "/projects/%s/agents/%s/actions/wakeup" % (self.project_key, self.id), body={
"versionId": version_id
})
def get_metrics_series(self, from_timestamp_ms=None, to_timestamp_ms=None, aggregation="MINUTE", timezone=None):
"""
Get the operational metrics series for this agent.
The returned payload may include the ongoing interval for the requested granularity.
As a consequence, the latest datapoint is temporarily inconsistent and may evolve
as more raw events are flushed and aggregated at read time.
The requested time window is aligned to the bucket boundaries of the selected
aggregation before being read.
:param int from_timestamp_ms: Beginning of the requested window, inclusive, as an
epoch timestamp in milliseconds. The effective lower bound is rounded down
to the start of its bucket. Optional, defaults to the oldest retained
timestamp available for the requested aggregation.
:param int to_timestamp_ms: End of the requested window, exclusive, as an epoch
timestamp in milliseconds. The effective upper bound is rounded up to the
next bucket boundary when it falls inside a bucket.
Optional, defaults to the current time when omitted.
:param str aggregation: Aggregation granularity. Supported values are ``MINUTE``,
``FIVE_MINUTES``, ``HOUR``, ``DAY`` and ``MONTH``.
:param str timezone: Timezone used to align bucket boundaries. Optional,
defaults to ``UTC``. Can be a timezone name like ``Europe/Paris``.
:return: The list of datapoints. Each datapoint contains a ``timestampMs``
expressed as the start timestamp of its bucket in epoch
milliseconds. For example, with ``HOUR`` aggregation, a datapoint
at ``18:00`` represents the interval ``[18:00, 19:00)``.
:rtype: list[dict]
"""
if from_timestamp_ms is not None and not isinstance(from_timestamp_ms, int):
raise TypeError("Expected int for from_timestamp_ms, got %s" % type(from_timestamp_ms).__name__)
if to_timestamp_ms is not None and not isinstance(to_timestamp_ms, int):
raise TypeError("Expected int for to_timestamp_ms, got %s" % type(to_timestamp_ms).__name__)
if timezone is not None and not isinstance(timezone, str):
raise TypeError("Expected str for timezone, got %s" % type(timezone).__name__)
return self.client._perform_json(
"GET", "/projects/%s/agents/%s/operational-metrics/series" % (self.project_key, self.id),
params={
"fromTimestampMs": from_timestamp_ms,
"toTimestampMs": to_timestamp_ms,
"aggregation": aggregation,
"timezone": timezone
}
)
class DSSAgentSettings(DSSTaggableObjectSettings):
"""
Settings for a agent
.. important::
Do not instantiate directly, use :meth:`dataikuapi.dss.agent.DSSAgent.get_settings` instead
"""
def __init__(self, client, settings):
super(DSSAgentSettings, self).__init__(settings)
self._client = client
self._settings = settings
def get_version_ids(self):
"""
List the ids of each version of this agent
:rtype: list[str]
"""
return [v["versionId"] for v in self._settings["versions"]]
@property
def active_version(self):
"""
:returns: the active version of this agent, or None if no version is declared as active
:rtype: str | None
"""
return self._settings.get("activeVersion")
def get_version_settings(self, version_id):
"""
:returns: the settings of the given version of this agent
:rtype: DSSAgentVersionSettings
"""
version_settings = None
for vs in self._settings["versions"]:
if vs["versionId"] == version_id:
version_settings = vs
break
if version_settings is None:
raise Exception("version %s not found" % version_id)
return DSSAgentVersionSettings(self, version_settings)
@property
def type(self):
return self._settings["type"]
def get_raw(self):
"""
:returns: the raw settings of this agent
:rtype: dict
"""
return self._settings
def save(self):
"""
Saves the settings for this agent
"""
self._client._perform_empty(
"PUT", "/projects/%s/agents/%s" % (self._settings["projectKey"], self._settings["id"]), body=self._settings)
class DSSAgentVersionSettings(object):
def __init__(self, settings, version_settings):
self._agent_settings = settings
self._version_settings = version_settings
def get_raw(self):
"""
:returns: the raw settings of this agent version
:rtype: dict
"""
return self._version_settings
@property
def llm_id(self):
"""
Only for Visual Agents
:rtype: str
"""
if not self._agent_settings.type == "TOOLS_USING_AGENT":
raise ValueError("Only valid for Simple Visual Agents")
return self._version_settings["toolsUsingAgentSettings"]["llmId"]
@llm_id.setter
def llm_id(self, value):
if not self._agent_settings.type == "TOOLS_USING_AGENT":
raise ValueError("Only valid for Simple Visual Agents")
self._version_settings["toolsUsingAgentSettings"]["llmId"] = value
@property
def tools(self):
"""
Returns the list of tools of the agent. The list can be modified.
Each tool is a dict, containing at least "toolRef", which is the identifier of the tool.
The dict may also contain "additionalDescription" which is added to the description of the tool
"""
if not self._agent_settings.type == "TOOLS_USING_AGENT":
raise ValueError("Only valid for Simple Visual Agents")
return self._version_settings["toolsUsingAgentSettings"]["tools"]
def add_tool(self, tool):
"""
Adds a tool to the agent
:param tool: a string (identifier of the tool), or a :class:`dataikuapi.dss.agent_tool.DSSAgentTool`
"""
def get_tool_ref():
is_foreign = self._agent_settings._settings["projectKey"] != tool.project_key
if is_foreign:
return tool.project_key + "." + tool.id
else:
return tool.id
if isinstance(tool, DSSAgentToolListItem):
tool_dict = { "toolRef" : get_tool_ref()}
elif isinstance(tool, DSSAgentTool):
tool_dict = { "toolRef" : get_tool_ref()}
elif isinstance(tool, str):
tool_dict = { "toolRef" : tool}
else:
raise Exception("Cannot add agent tool: %s" % tool)
self.tools.append(tool_dict)
def _get_internal_agent_settings(self):
"""
Returns internal agent settings (SavedModel#AgentSettings in Java) matching the agent type.
:rtype: dict
"""
settings_key_by_type = {
"TOOLS_USING_AGENT": "toolsUsingAgentSettings",
"STRUCTURED_AGENT": "structuredAgentSettings",
"PYTHON_AGENT": "pythonAgentSettings",
"PLUGIN_AGENT": "pluginAgentSettings",
}
try:
settings_key = settings_key_by_type[self._agent_settings.type]
return self._version_settings[settings_key]
except KeyError:
raise ValueError("Unsupported agent type: %s" % self._agent_settings.type)
def _get_or_create_interaction_logging_selection(self):
internal_settings = self._get_internal_agent_settings()
interaction_logging_selection = internal_settings.get("interactionLoggingSelection")
if interaction_logging_selection is None:
interaction_logging_selection = {
"mode": DSSAgentInteractionLoggingSelection.MODE_INHERIT,
"explicitSettings": {},
}
internal_settings["interactionLoggingSelection"] = interaction_logging_selection
return interaction_logging_selection
@property
def interaction_logging_selection(self):
"""
Get the interaction logging selection for this version.
Before configuring interaction logging on an agent version, create the
target dataset on the project:
.. code-block:: python
project = client.get_project("MYPROJECT")
project.create_llm_interaction_logging_dataset(
"agent_logs",
connection_id="filesystem_managed",
time_partitioning="DAY",
)
Example using inherited settings:
.. code-block:: python
agent = project.get_agent("my_agent")
agent_settings = agent.get_settings()
version_settings = agent_settings.get_version_settings("v1")
agent_logging_selection = version_settings.interaction_logging_selection
agent_logging_selection.inherit()
agent_settings.save()
Example using explicit settings:
.. code-block:: python
agent = project.get_agent("my_agent")
agent_settings = agent.get_settings()
version_settings = agent_settings.get_version_settings("v1")
agent_logging_selection = version_settings.interaction_logging_selection
agent_logging_selection.enable(
"agent_logs",
settings={
"flushEveryS": 60,
"flushEveryBytes": 1_000_000,
"contentMode": "FULL",
},
)
agent_settings.save()
Example disabling interaction logging:
.. code-block:: python
agent = project.get_agent("my_agent")
agent_settings = agent.get_settings()
version_settings = agent_settings.get_version_settings("v1")
agent_logging_selection = version_settings.interaction_logging_selection
agent_logging_selection.disable()
agent_settings.save()
:rtype: :class:`dataikuapi.dss.agent.DSSAgentInteractionLoggingSelection`
"""
return DSSAgentInteractionLoggingSelection(self._get_or_create_interaction_logging_selection())
@interaction_logging_selection.setter
def interaction_logging_selection(self, value):
if isinstance(value, DSSAgentInteractionLoggingSelection):
value = value.get_raw()
self._get_internal_agent_settings()["interactionLoggingSelection"] = value