-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathdataset.py
More file actions
536 lines (453 loc) · 18.2 KB
/
Copy pathdataset.py
File metadata and controls
536 lines (453 loc) · 18.2 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
from __future__ import annotations
import logging
from datetime import datetime
from typing import Any
from galileo.__future__.shared.base import StateManagementMixin, SyncState
from galileo.__future__.shared.exceptions import ValidationError
from galileo.datasets import Datasets
from galileo.resources.models.dataset_content import DatasetContent
from galileo.resources.models.dataset_row import DatasetRow
from galileo.resources.types import Unset
logger = logging.getLogger(__name__)
class Dataset(StateManagementMixin):
"""
Object-centric interface for Galileo datasets.
This class provides an intuitive way to work with Galileo datasets,
encapsulating dataset management operations and providing seamless
integration with dataset content management.
Attributes
----------
id (str): The unique dataset identifier.
name (str): The dataset name.
created_at (datetime.datetime): When the dataset was created.
updated_at (datetime.datetime): When the dataset was last updated.
num_rows (int): Number of rows in the dataset.
column_names (list[str]): Column names in the dataset.
draft (bool): Whether the dataset is in draft state.
Examples
--------
# Create a new dataset locally, then persist
dataset = Dataset(
name="ml-knowledge-evaluation_3",
content=[
{"input": "What is machine learning?", "output": "Machine learning ..."},
{"input": "How does deep learning work?", "output": "Deep learning uses ..."}
]
).create()
# Get an existing dataset
dataset = Dataset.get(name="geography-questions")
# List all datasets
datasets = Dataset.list(limit=50)
# Get dataset content
content = dataset.get_content()
# Add rows to dataset
dataset.add_rows([
{"input": "Australia", "output": "Oceania"},
{"input": "Egypt", "output": "Africa"},
])
# Get version history
history = dataset.get_version_history()
# Delete dataset
dataset.delete()
"""
# Type annotations for instance attributes
id: str | None
name: str
created_at: datetime | None
updated_at: datetime | None
num_rows: int | None
column_names: list[str] | None
draft: bool | None
content: list[dict[str, Any]]
def __str__(self) -> str:
"""String representation of the dataset."""
return f"Dataset(name='{self.name}', id='{self.id}')"
def __repr__(self) -> str:
"""Detailed string representation of the dataset."""
return f"Dataset(name='{self.name}', id='{self.id}', rows={self.num_rows})"
def __init__(self, name: str | None = None, content: list[dict[str, Any]] | None = None) -> None:
"""
Initialize a Dataset instance locally.
Creates a local dataset object that exists only in memory until .create()
is called to persist it to the API.
Args:
name (Optional[str]): The name of the dataset to create.
content (Optional[list[dict[str, Any]]]): The content for the dataset.
Raises
------
ValidationError: If name is not provided.
"""
super().__init__()
if name is None:
raise ValidationError(
"'name' must be provided to create a dataset. Use Dataset.get() to retrieve an existing dataset."
)
# Initialize attributes locally
self.name = name
self.content = content or []
self.id = None
self.created_at = None
self.updated_at = None
self.num_rows = None
self.column_names = None
self.draft = None
# Set initial state
self._set_state(SyncState.LOCAL_ONLY)
@classmethod
def _create_empty(cls) -> Dataset:
"""Internal constructor bypassing __init__ for API hydration."""
instance = cls.__new__(cls)
super(Dataset, instance).__init__()
return instance
@classmethod
def _from_api_response(cls, retrieved_dataset: Any) -> Dataset:
"""
Factory method to create a Dataset instance from an API response.
Args:
retrieved_dataset: The dataset data retrieved from the API.
Returns
-------
Dataset: A new Dataset instance populated with the API data.
"""
instance = cls._create_empty()
instance.id = retrieved_dataset.id
instance.name = retrieved_dataset.name
instance.created_at = retrieved_dataset.created_at
instance.updated_at = retrieved_dataset.updated_at
instance.num_rows = retrieved_dataset.num_rows
instance.column_names = retrieved_dataset.column_names
instance.draft = retrieved_dataset.draft
instance.content = [] # Content is not loaded in get()/list(), use get_content() for that
# Set state to synced since we just retrieved from API
instance._set_state(SyncState.SYNCED)
return instance
def create(self) -> Dataset:
"""
Persist this dataset to the API.
Returns
-------
Dataset: This dataset instance with updated attributes from the API.
Raises
------
Exception: If the API call fails.
Examples
--------
dataset = Dataset(name="test", content=[...]).create()
assert dataset.is_synced()
"""
try:
logger.info(f"Dataset.create: name='{self.name}' - started")
datasets_service = Datasets()
# Don't pass project_id at all - it defaults to None which is handled correctly
created_dataset = datasets_service.create(name=self.name, content=self.content)
# Update attributes from response
self.id = created_dataset.id
self.name = created_dataset.name
self.created_at = created_dataset.created_at
self.updated_at = created_dataset.updated_at
self.num_rows = created_dataset.num_rows
self.column_names = created_dataset.column_names
self.draft = created_dataset.draft
# Set state to synced
self._set_state(SyncState.SYNCED)
logger.info(f"Dataset.create: id='{self.id}' - completed")
return self
except Exception as e:
self._set_state(SyncState.FAILED_SYNC, error=e)
logger.error(f"Dataset.create: name='{self.name}' - failed: {e}")
raise
@classmethod
def get(cls, *, id: str | None = None, name: str | None = None) -> Dataset | None:
"""
Get an existing dataset by ID or name.
Args:
id (Optional[str]): The dataset ID.
name (Optional[str]): The dataset name.
Returns
-------
Optional[Dataset]: The dataset if found, None otherwise.
Raises
------
ValueError: If neither or both id and name are provided.
Examples
--------
# Get by name
dataset = Dataset.get(name="geography-questions")
# Get by ID
dataset = Dataset.get(id="dataset-123")
"""
datasets_service = Datasets()
# Call service with the appropriate signature based on which parameter is provided
if id is not None:
retrieved_dataset = datasets_service.get(id=id)
elif name is not None:
retrieved_dataset = datasets_service.get(name=name)
else:
raise ValueError("Either 'id' or 'name' must be provided")
if retrieved_dataset is None:
return None
return cls._from_api_response(retrieved_dataset)
@classmethod
def list(cls, *, limit: Unset | int = 100) -> list[Dataset]:
"""
List all available datasets.
Args:
limit (Union[Unset, int]): Maximum number of datasets to return.
Returns
-------
list[Dataset]: A list of all datasets.
Examples
--------
datasets = Dataset.list()
datasets = Dataset.list(limit=50)
"""
datasets_service = Datasets()
retrieved_datasets = datasets_service.list(limit=limit)
return [cls._from_api_response(retrieved_dataset) for retrieved_dataset in retrieved_datasets]
@classmethod
def generate(
cls,
*,
prompt: str | None = None,
instructions: str | None = None,
examples: list[str] | None = None, # type: ignore[valid-type]
count: int = 10,
data_types: list[str] | None = None, # type: ignore[valid-type]
prompt_settings: dict[str, Any] | None = None,
) -> list[DatasetRow]: # type: ignore[valid-type]
"""
Generate synthetic dataset rows.
Args:
prompt (Optional[str]): A description of the assistant's role.
instructions (Optional[str]): Instructions for the assistant.
examples (Optional[list[str]]): Examples of user prompts.
count (int): The number of synthetic examples to generate.
data_types (Optional[list[str]]): The types of data to generate.
prompt_settings (Optional[dict[str, Any]]): Settings for the prompt generation.
Returns
-------
list[DatasetRow]: A list of generated dataset rows.
Examples
--------
rows = Dataset.generate(
prompt="Financial planning assistant...",
instructions="You are a financial planning assistant...",
examples=["I want to invest $1000 per month."],
count=3,
)
"""
datasets_service = Datasets()
return datasets_service.extend(
prompt=prompt,
instructions=instructions,
examples=examples,
count=count,
data_types=data_types,
prompt_settings=prompt_settings,
)
def get_content(self) -> DatasetContent | None:
"""
Get the content of this dataset.
Returns
-------
Optional[DatasetContent]: The dataset content if available.
Examples
--------
dataset = Dataset.get(name="my-dataset")
content = dataset.get_content()
"""
if self.id is None:
raise ValueError("Dataset ID is not set. Cannot get content for a local-only dataset.")
datasets_service = Datasets()
dataset = datasets_service.get(id=self.id)
if dataset is None:
return None
return dataset.get_content()
def add_rows(self, rows: list[dict[str, Any]]) -> Dataset: # type: ignore[valid-type]
"""
Add rows to this dataset (active mutation).
This method performs an API call and atomically updates the state.
Args:
rows (list[dict[str, Any]]): The rows to add to the dataset.
Returns
-------
Dataset: This dataset instance for method chaining.
Examples
--------
dataset.add_rows([
{"input": "Australia", "output": "Oceania"},
{"input": "Egypt", "output": "Africa"},
])
"""
if self.id is None:
raise ValueError("Dataset ID is not set. Cannot add rows to a local-only dataset.")
try:
datasets_service = Datasets()
dataset = datasets_service.get(id=self.id)
if dataset is not None:
dataset.add_rows(rows)
# Refresh attributes after adding rows
self.num_rows = dataset.num_rows
self.updated_at = dataset.updated_at
# Set state to synced after successful update
self._set_state(SyncState.SYNCED)
return self
except Exception as e:
self._set_state(SyncState.FAILED_SYNC, error=e)
raise
def get_version_history(self) -> list[dict[str, Any]]: # type: ignore[valid-type]
"""
Get the version history of this dataset.
Returns
-------
list[dict[str, Any]]: The version history of the dataset.
Examples
--------
dataset = Dataset.get(name="my-dataset")
history = dataset.get_version_history()
"""
if self.id is None:
raise ValueError("Dataset ID is not set. Cannot get version history for a local-only dataset.")
datasets_service = Datasets()
dataset = datasets_service.get(id=self.id)
if dataset is None:
return []
return dataset.get_version_history()
def get_version(self, *, index: int) -> DatasetContent | None:
"""
Get a specific version of this dataset.
Args:
index (int): The version index to retrieve.
Returns
-------
Optional[DatasetContent]: The dataset content for the specified version.
Examples
--------
dataset = Dataset.get(name="my-dataset")
version = dataset.get_version(index=0)
"""
if self.id is None:
raise ValueError("Dataset ID is not set. Cannot get version for a local-only dataset.")
datasets_service = Datasets()
dataset = datasets_service.get(id=self.id)
if dataset is None:
return None
return dataset.load_version(index)
def extend(
self,
*,
prompt: str | None = None,
instructions: str | None = None,
examples: list[str] | None = None, # type: ignore[valid-type]
count: int = 10,
data_types: list[str] | None = None, # type: ignore[valid-type]
prompt_settings: dict[str, Any] | None = None,
) -> list[DatasetRow]: # type: ignore[valid-type]
"""
Extend this dataset with synthetically generated data.
Args:
prompt (Optional[str]): A description of the assistant's role.
instructions (Optional[str]): Instructions for the assistant.
examples (Optional[list[str]]): Examples of user prompts.
count (int): The number of synthetic examples to generate.
data_types (Optional[list[str]]): The types of data to generate.
prompt_settings (Optional[dict[str, Any]]): Settings for the prompt generation.
Returns
-------
list[DatasetRow]: A list of generated dataset rows.
Examples
--------
extended_rows = dataset.extend(
prompt="Financial planning assistant...",
instructions="You are a financial planning assistant...",
examples=["I want to invest $1000 per month."],
count=3,
)
"""
if self.id is None:
raise ValueError("Dataset ID is not set. Cannot extend a local-only dataset.")
datasets_service = Datasets()
dataset = datasets_service.get(id=self.id)
if dataset is None:
return []
return dataset.extend(
prompt=prompt,
instructions=instructions,
examples=examples,
count=count,
data_types=data_types,
prompt_settings=prompt_settings,
)
def delete(self) -> None:
"""
Delete this dataset.
Examples
--------
dataset = Dataset.get(name="my-dataset")
dataset.delete()
"""
if self.id is None:
raise ValueError("Dataset ID is not set. Cannot delete a local-only dataset.")
try:
logger.info(f"Dataset.delete: name='{self.name}' - started")
datasets_service = Datasets()
datasets_service.delete(id=self.id)
# Set state to deleted after successful deletion
self._set_state(SyncState.DELETED)
logger.info(f"Dataset.delete: name='{self.name}' - completed")
except Exception as e:
self._set_state(SyncState.FAILED_SYNC, error=e)
logger.error(f"Dataset.delete: name='{self.name}' - failed: {e}")
raise
def refresh(self) -> None:
"""
Refresh this dataset's state from the API.
Updates all attributes with the latest values from the remote API
and sets the state to SYNCED.
Raises
------
Exception: If the API call fails or the dataset no longer exists.
Examples
--------
dataset.refresh()
assert dataset.is_synced()
"""
if self.id is None:
raise ValueError("Dataset ID is not set. Cannot refresh a local-only dataset.")
try:
logger.debug(f"Dataset.refresh: id='{self.id}' - started")
datasets_service = Datasets()
retrieved_dataset = datasets_service.get(id=self.id)
if retrieved_dataset is None:
raise ValueError(f"Dataset with id '{self.id}' no longer exists")
# Update all attributes from response
self.id = retrieved_dataset.id
self.name = retrieved_dataset.name
self.created_at = retrieved_dataset.created_at
self.updated_at = retrieved_dataset.updated_at
self.num_rows = retrieved_dataset.num_rows
self.column_names = retrieved_dataset.column_names
self.draft = retrieved_dataset.draft
# Set state to synced
self._set_state(SyncState.SYNCED)
logger.debug(f"Dataset.refresh: id='{self.id}' - completed")
except Exception as e:
self._set_state(SyncState.FAILED_SYNC, error=e)
logger.error(f"Dataset.refresh: id='{self.id}' - failed: {e}")
raise
def save(self) -> Dataset:
"""
Save changes to this dataset.
This method is a placeholder for future functionality to update
dataset properties.
Returns
-------
Dataset: This dataset instance.
Raises
------
NotImplementedError: This functionality is not yet implemented.
"""
raise NotImplementedError(
"Dataset updates are not yet implemented. "
"Use add_rows() to add content or other specific methods to modify dataset state."
)