-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathdataset_client.py
More file actions
277 lines (230 loc) · 9.58 KB
/
Copy pathdataset_client.py
File metadata and controls
277 lines (230 loc) · 9.58 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
"""DatasetClient for managing evaluation datasets."""
import logging
from typing import Any, Dict, Optional
import boto3
from botocore.config import Config
from bedrock_agentcore._utils.config import WaitConfig
from bedrock_agentcore._utils.polling import wait_until, wait_until_deleted
from bedrock_agentcore._utils.snake_case import accept_snake_case_kwargs, convert_kwargs
from bedrock_agentcore._utils.user_agent import build_user_agent_suffix
logger = logging.getLogger(__name__)
class DatasetClient:
"""Client for managing evaluation datasets.
Provides pass-through access to all dataset management APIs on the
bedrock-agentcore-control service, plus *_and_wait helpers for async operations.
Example::
client = DatasetClient(region_name="us-west-2")
# Create a dataset and wait for ACTIVE
dataset = client.create_dataset_and_wait(
datasetName="my-dataset",
schemaType="AGENTCORE_EVALUATION_PREDEFINED_V1",
source={"inlineExamples": {"examples": [...]}},
)
# Pass-through to any dataset API
client.list_datasets(maxResults=10)
client.add_dataset_examples(datasetId="ds-123", examples=[...])
"""
_ALLOWED_CP_METHODS = {
# Dataset CRUD
"create_dataset",
"get_dataset",
"list_datasets",
"update_dataset",
"delete_dataset",
# Version management
"create_dataset_version",
"list_dataset_versions",
# Examples management
"add_dataset_examples",
"update_dataset_examples",
"delete_dataset_examples",
"list_dataset_examples",
}
def __init__(
self,
region_name: Optional[str] = None,
integration_source: Optional[str] = None,
boto3_session: Optional[boto3.Session] = None,
):
"""Initialize the DatasetClient.
Args:
region_name: AWS region. Falls back to boto3 session region or us-west-2.
integration_source: Optional integration framework identifier for telemetry.
boto3_session: Optional boto3 Session. If not provided, a default is created.
"""
session = boto3_session if boto3_session else boto3.Session()
self.region_name = region_name or session.region_name or "us-west-2"
self.integration_source = integration_source
user_agent_extra = build_user_agent_suffix(integration_source)
client_config = Config(user_agent_extra=user_agent_extra)
self._cp_client = session.client(
"bedrock-agentcore-control",
region_name=self.region_name,
config=client_config,
)
logger.info("Initialized DatasetClient in region %s", self.region_name)
def __getattr__(self, name: str):
"""Dynamically forward allowlisted method calls to the boto3 client."""
if "_cp_client" not in self.__dict__:
raise AttributeError(name)
if name in self._ALLOWED_CP_METHODS and hasattr(self._cp_client, name):
method = getattr(self._cp_client, name)
logger.debug("Forwarding method '%s' to _cp_client", name)
return accept_snake_case_kwargs(method)
raise AttributeError(
f"'{self.__class__.__name__}' object has no attribute '{name}'. "
f"Method not found on control plane client. "
f"Available methods can be found in the boto3 documentation for "
f"'bedrock-agentcore-control' service."
)
# *_and_wait methods
# -------------------------------------------------------------------------
def create_dataset_and_wait(
self,
wait_config: Optional[WaitConfig] = None,
**kwargs,
) -> Dict[str, Any]:
"""Create a dataset and wait for it to reach ACTIVE status.
Args:
wait_config: Optional WaitConfig for polling behavior.
**kwargs: Arguments forwarded to the create_dataset API.
Returns:
Dataset details when ACTIVE.
Raises:
RuntimeError: If the dataset reaches CREATE_FAILED status.
TimeoutError: If the dataset doesn't become ACTIVE within max_wait.
"""
response = self._cp_client.create_dataset(**convert_kwargs(kwargs))
dataset_id = response["datasetId"]
return wait_until(
lambda: self._cp_client.get_dataset(datasetId=dataset_id),
"ACTIVE",
{"CREATE_FAILED"},
wait_config,
)
def delete_dataset_and_wait(
self,
wait_config: Optional[WaitConfig] = None,
**kwargs,
) -> Optional[Dict[str, Any]]:
"""Delete a dataset (or a single version) and wait for completion.
- Full delete (no ``datasetVersion``): polls until ``GetDataset``
raises ``ResourceNotFoundException``. Fails on ``DELETE_FAILED``.
- Version-specific delete (``datasetVersion`` provided): the dataset
itself is not removed. Polls ``GetDataset`` until status returns to
``ACTIVE``. Fails on ``UPDATE_FAILED``. Returns the dataset details.
Args:
wait_config: Optional WaitConfig for polling behavior.
**kwargs: Arguments forwarded to the delete_dataset API.
Raises:
RuntimeError: On ``DELETE_FAILED`` or ``UPDATE_FAILED``.
TimeoutError: If the operation does not finish within ``max_wait``.
"""
converted = convert_kwargs(kwargs)
response = self._cp_client.delete_dataset(**converted)
dataset_id = response["datasetId"]
if "datasetVersion" in converted:
return wait_until(
lambda: self._cp_client.get_dataset(datasetId=dataset_id),
"ACTIVE",
{"UPDATE_FAILED"},
wait_config,
)
wait_until_deleted(
lambda: self._cp_client.get_dataset(datasetId=dataset_id),
failed={"DELETE_FAILED"},
wait_config=wait_config,
)
return None
def create_dataset_version_and_wait(
self,
wait_config: Optional[WaitConfig] = None,
**kwargs,
) -> Dict[str, Any]:
"""Create a dataset version and wait for the dataset to reach ACTIVE status.
Args:
wait_config: Optional WaitConfig for polling behavior.
**kwargs: Arguments forwarded to the create_dataset_version API.
Returns:
Dataset details when ACTIVE.
Raises:
RuntimeError: If the dataset reaches UPDATE_FAILED status.
TimeoutError: If the dataset doesn't become ACTIVE within max_wait.
"""
response = self._cp_client.create_dataset_version(**convert_kwargs(kwargs))
dataset_id = response["datasetId"]
return wait_until(
lambda: self._cp_client.get_dataset(datasetId=dataset_id),
"ACTIVE",
{"UPDATE_FAILED"},
wait_config,
)
def add_examples_and_wait(
self,
wait_config: Optional[WaitConfig] = None,
**kwargs,
) -> Dict[str, Any]:
"""Add examples to a dataset and wait for ACTIVE status.
Args:
wait_config: Optional WaitConfig for polling behavior.
**kwargs: Arguments forwarded to the add_dataset_examples API.
Returns:
Dataset details when ACTIVE.
Raises:
RuntimeError: If the dataset reaches UPDATE_FAILED status.
TimeoutError: If the dataset doesn't become ACTIVE within max_wait.
"""
response = self._cp_client.add_dataset_examples(**convert_kwargs(kwargs))
dataset_id = response["datasetId"]
return wait_until(
lambda: self._cp_client.get_dataset(datasetId=dataset_id),
"ACTIVE",
{"UPDATE_FAILED"},
wait_config,
)
def update_examples_and_wait(
self,
wait_config: Optional[WaitConfig] = None,
**kwargs,
) -> Dict[str, Any]:
"""Update examples in a dataset and wait for ACTIVE status.
Args:
wait_config: Optional WaitConfig for polling behavior.
**kwargs: Arguments forwarded to the update_dataset_examples API.
Returns:
Dataset details when ACTIVE.
Raises:
RuntimeError: If the dataset reaches UPDATE_FAILED status.
TimeoutError: If the dataset doesn't become ACTIVE within max_wait.
"""
response = self._cp_client.update_dataset_examples(**convert_kwargs(kwargs))
dataset_id = response["datasetId"]
return wait_until(
lambda: self._cp_client.get_dataset(datasetId=dataset_id),
"ACTIVE",
{"UPDATE_FAILED"},
wait_config,
)
def delete_examples_and_wait(
self,
wait_config: Optional[WaitConfig] = None,
**kwargs,
) -> Dict[str, Any]:
"""Delete examples from a dataset and wait for ACTIVE status.
Args:
wait_config: Optional WaitConfig for polling behavior.
**kwargs: Arguments forwarded to the delete_dataset_examples API.
Returns:
Dataset details when ACTIVE.
Raises:
RuntimeError: If the dataset reaches UPDATE_FAILED status.
TimeoutError: If the dataset doesn't become ACTIVE within max_wait.
"""
response = self._cp_client.delete_dataset_examples(**convert_kwargs(kwargs))
dataset_id = response["datasetId"]
return wait_until(
lambda: self._cp_client.get_dataset(datasetId=dataset_id),
"ACTIVE",
{"UPDATE_FAILED"},
wait_config,
)