-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathdatasets.py
More file actions
744 lines (630 loc) · 26.1 KB
/
Copy pathdatasets.py
File metadata and controls
744 lines (630 loc) · 26.1 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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.
"""
Dataset adapters and utilities for prompt-ops.
This module provides a standardized way to load and process different datasets
for use with the prompt-ops tool.
"""
import csv
import json
import logging
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union
import dspy
import yaml
class DatasetAdapter(ABC):
"""
Base adapter class for transforming dataset-specific formats into a standardized format.
Subclasses should implement the adapt method to transform their specific dataset
format into the standardized format expected by the prompt-ops tool.
"""
def __init__(self, dataset_path: str, file_format: str = None):
"""
Initialize the dataset adapter with a path to the dataset file.
Args:
dataset_path: Path to the dataset file
file_format: Format of the file ('json', 'csv', 'yaml'). If None, inferred from file extension.
"""
self.dataset_path = Path(dataset_path)
self.file_format = file_format or self._infer_format(self.dataset_path)
def _infer_format(self, path: Path) -> str:
"""
Infer the file format from the file extension.
Args:
path: Path to the file
Returns:
Inferred file format
Raises:
ValueError: If the file format cannot be inferred
"""
extension = path.suffix.lower()
if extension == ".json":
return "json"
elif extension == ".csv":
return "csv"
elif extension in [".yaml", ".yml"]:
return "yaml"
else:
raise ValueError(
f"Unsupported file format: {extension}. Supported formats: .json, .csv, .yaml, .yml"
)
def _load_json(self) -> List[Dict[str, Any]]:
"""
Load data from a JSON file.
Returns:
List of data items
"""
with open(self.dataset_path, "r") as f:
return json.load(f)
def _load_csv(self) -> List[Dict[str, Any]]:
"""
Load data from a CSV file.
Returns:
List of data items
"""
with open(self.dataset_path, "r", newline="") as f:
reader = csv.DictReader(f)
return list(reader)
def _load_yaml(self) -> List[Dict[str, Any]]:
"""
Load data from a YAML file.
Returns:
List of data items
"""
with open(self.dataset_path, "r") as f:
data = yaml.safe_load(f)
# Ensure we return a list of dictionaries
if isinstance(data, list):
return data
elif isinstance(data, dict):
# If the YAML contains a single dictionary with a list field, return that list
for key, value in data.items():
if isinstance(value, list):
return value
# Otherwise, return a list with the dictionary as the only element
return [data]
else:
raise ValueError(f"Unexpected YAML structure: {type(data)}")
def load_raw_data(self) -> List[Dict[str, Any]]:
"""
Load raw data from the dataset path based on the file format.
Returns:
List of raw data items from the dataset
Raises:
ValueError: If the file format is not supported
"""
loaders = {
"json": self._load_json,
"csv": self._load_csv,
"yaml": self._load_yaml,
}
if self.file_format not in loaders:
raise ValueError(
f"Unsupported file format: {self.file_format}. Supported formats: {', '.join(loaders.keys())}"
)
return loaders[self.file_format]()
@abstractmethod
def adapt(self) -> List[Dict[str, Any]]:
"""
Transform dataset-specific format into standardized format.
The standardized format is a list of dictionaries, where each dictionary
represents a single example and has the following structure:
{
"inputs": {
"field1": value1,
"field2": value2,
...
},
"outputs": {
"field1": value1,
"field2": value2,
...
},
"metadata": { # Optional
"field1": value1,
"field2": value2,
...
}
}
Returns:
List of standardized examples
"""
pass
class ConfigurableJSONAdapter(DatasetAdapter):
"""
A configurable adapter for JSON datasets with flexible field mappings.
This adapter can be used with any JSON dataset by configuring the input and output
field mappings. It supports simple field names, nested paths, and custom mappings,
making it compatible with various JSON structures without requiring custom adapter classes.
"""
def __init__(
self,
dataset_path: Union[str, Path],
input_field: Union[str, List[str], Dict[str, str]],
golden_output_field: Union[str, List[str], Dict[str, str]],
file_format: Optional[str] = None,
input_transform: Optional[Callable] = None,
output_transform: Optional[Callable] = None,
default_value: Any = None,
**kwargs,
):
"""
Initialize the standardized JSON adapter.
Args:
dataset_path: Path to the dataset file
input_field: Field(s) to use as input. Can be:
- A string (field name)
- A list of strings (nested field path)
- A dict mapping from source fields to destination fields
golden_output_field: Field(s) to use as ground truth/reference output. Same format options as input_field
file_format: Format of the dataset file (defaults to json)
input_transform: Optional function to transform input values
output_transform: Optional function to transform output values
**kwargs: Additional arguments
"""
super().__init__(dataset_path, file_format)
self.input_field = input_field
self.golden_output_field = golden_output_field
self.input_transform = input_transform
self.output_transform = output_transform
self.default_value = default_value
def _get_nested_value(self, item: Dict[str, Any], field_path: List[str]) -> Any:
"""
Get a value from a nested dictionary using a field path.
Args:
item: Dictionary to extract value from
field_path: List of keys forming a path to the value
Returns:
The value at the specified path or default_value if not found
"""
value = item
for key in field_path:
if isinstance(value, dict) and key in value:
value = value[key]
else:
return self.default_value
return value
def _extract_value(
self, item: Dict[str, Any], field_spec: Union[str, List[str], Dict[str, str]]
) -> Any:
"""
Extract a value using a field specification.
Args:
item: Dictionary to extract value from
field_spec: Field specification (string, list, or dict)
Returns:
The extracted value(s)
"""
if isinstance(field_spec, str):
# Simple field name
return item.get(field_spec, self.default_value)
elif isinstance(field_spec, list):
# Nested field path
return self._get_nested_value(item, field_spec)
elif isinstance(field_spec, dict):
# Multiple fields mapping
result = {}
for src_field, dst_field in field_spec.items():
if isinstance(src_field, str):
value = item.get(src_field, self.default_value)
elif isinstance(src_field, list):
value = self._get_nested_value(item, src_field)
else:
continue
result[dst_field] = value
return result
return self.default_value
def _transform_value(self, value: Any, transform_func: Optional[Callable]) -> Any:
"""
Apply transformation with error handling.
Args:
value: Value to transform
transform_func: Function to apply to the value
Returns:
Transformed value or original value if transformation fails
"""
if transform_func is None or value is None:
return value
try:
return transform_func(value)
except Exception as e:
logging.warning(f"Error transforming value: {e}")
return value
def _ensure_string_value(
self, value: Any, field_spec: Union[str, List[str], Dict[str, str]]
) -> str:
"""
Ensure a value is a string suitable for DSPy.
Args:
value: The value to convert
field_spec: The field specification (for error messages)
Returns:
String representation of the value
Raises:
ValueError: If the value cannot be reasonably converted to a string
"""
# Already a string - perfect
if isinstance(value, str):
return value
# None/null - warn and use empty string or default
if value is None:
logging.warning(
f"Field '{field_spec}' is None. Using default value: '{self.default_value}'"
)
return str(self.default_value) if self.default_value is not None else ""
# Dict or list - this is likely an error, but handle gracefully
if isinstance(value, (dict, list)):
logging.error(
f"Field '{field_spec}' contains a {type(value).__name__} but should be a string. "
f"This will be JSON-stringified, but you should fix your field mapping. "
f"Hint: Did you mean to specify a nested path? "
f"For example, if your data has {{'fields': {{'input': '...'}}}}, "
f"use 'fields.input' instead of just 'fields'."
)
return json.dumps(value, ensure_ascii=False)
# Primitives (int, float, bool) - convert to string
if isinstance(value, (int, float, bool)):
logging.warning(
f"Field '{field_spec}' is a {type(value).__name__}. Converting to string."
)
return str(value)
# Unknown type - try to stringify but warn
logging.warning(
f"Field '{field_spec}' has unexpected type {type(value).__name__}. "
f"Attempting to convert to string."
)
return str(value)
def _map_to_standard_format(
self,
values: Any,
field_spec: Union[str, List[str], Dict[str, str]],
is_input: bool = True,
) -> Dict[str, Any]:
"""
Map extracted values to the standard format.
Args:
values: Extracted values (single value or dictionary)
field_spec: Original field specification (for reference)
is_input: Whether this is mapping input fields (True) or output fields (False)
Returns:
Dictionary with standardized field names
"""
result = {}
if isinstance(values, dict):
# Values already in dictionary format
# This happens when:
# 1. field_spec is a dict like {"source": "dest"} (intended behavior)
# 2. field_spec extracted a dict value (likely a configuration error)
# Check if this is likely a configuration error
if not isinstance(field_spec, dict):
logging.error(
f"⚠️ FIELD MAPPING ERROR: Field '{field_spec}' extracted a dict "
f"with keys {list(values.keys())}, but a string value was expected. "
f"\n Hint: If your data structure is {{'fields': {{'input': '...'}}}}, "
f"\n you should map to ['fields', 'input'] (nested path) instead of just 'fields'."
f"\n The dict will be converted to preserve individual fields, but this may cause issues with DSPy."
)
# Validate each value in the dict to ensure they're strings
validated_dict = {}
for key, value in values.items():
if not isinstance(value, str):
validated_dict[key] = self._ensure_string_value(
value, f"{field_spec}.{key}"
)
else:
validated_dict[key] = value
result.update(validated_dict)
# Ensure standard field exists (question/answer)
standard_field = "question" if is_input else "answer"
if standard_field not in result:
# Use the first value as the standard field
if validated_dict:
result[standard_field] = next(iter(validated_dict.values()))
logging.warning(
f"Added '{standard_field}' field automatically using value from '{list(validated_dict.keys())[0]}'. "
f"Consider fixing your field mapping to avoid confusion."
)
else:
# Single value - ensure it's a string for DSPy compatibility
standardized_value = self._ensure_string_value(values, field_spec)
if isinstance(field_spec, str):
# Keep original field name as well
result[field_spec] = standardized_value
# Add standardized field names for DSPy compatibility
if is_input:
result["question"] = standardized_value
else:
result["answer"] = standardized_value
return result
def _process_fields(
self,
item: Dict[str, Any],
field_spec: Union[str, List[str], Dict[str, str]],
transform: Optional[Callable] = None,
is_input: bool = True,
) -> Dict[str, Any]:
"""
Process fields according to the field specification.
Args:
item: Dictionary to extract values from
field_spec: Field specification (string, list, or dict)
transform: Optional function to transform values
is_input: Whether this is processing input fields (True) or output fields (False)
Returns:
Dictionary of processed fields
"""
# 1. Extract values based on field specification
extracted_values = self._extract_value(item, field_spec)
# 2. Apply transformation if provided
transformed_values = self._transform_value(extracted_values, transform)
# 3. Map to standard format
return self._map_to_standard_format(transformed_values, field_spec, is_input)
def adapt(self) -> List[Dict[str, Any]]:
"""
Transform the JSON dataset into standardized format.
Returns:
List of standardized examples
"""
# Load raw data
raw_data = self.load_raw_data()
# Transform into standardized format
standardized_data = []
for item in raw_data:
inputs = self._process_fields(
item, self.input_field, self.input_transform, is_input=True
)
outputs = self._process_fields(
item, self.golden_output_field, self.output_transform, is_input=False
)
standardized_example = {
"inputs": inputs,
"outputs": outputs,
"metadata": {},
}
standardized_data.append(standardized_example)
return standardized_data
class RAGJSONAdapter(ConfigurableJSONAdapter):
"""
Adapter for RAG (Retrieval-Augmented Generation) JSON datasets with question, context, and answer fields.
This adapter extends ConfigurableJSONAdapter to handle datasets that include retrieval contexts
or documents alongside questions and answers. It standardizes the format to be compatible with
RAG-based evaluation and optimization frameworks.
"""
def __init__(
self,
dataset_path: Union[str, Path],
question_field: Union[str, List[str], Dict[str, str]],
context_field: Union[str, List[str], Dict[str, str]],
golden_answer_field: Union[str, List[str], Dict[str, str]],
file_format: Optional[str] = None,
question_transform: Optional[Callable] = None,
context_transform: Optional[Callable] = None,
answer_transform: Optional[Callable] = None,
default_value: Any = None,
**kwargs,
):
"""
Initialize the RAG JSON adapter.
Args:
dataset_path: Path to the dataset file
question_field: Field(s) to use as question. Can be:
- A string (field name)
- A list of strings (nested field path)
- A dict mapping from source fields to destination fields
context_field: Field(s) to use as context/documents
golden_answer_field: Field(s) to use as ground truth/reference answer
file_format: Format of the dataset file (defaults to json)
question_transform: Optional function to transform question values
context_transform: Optional function to transform context values
answer_transform: Optional function to transform answer values
default_value: Default value to use when a field is not found
**kwargs: Additional arguments
"""
# Initialize with basic fields for backward compatibility
super().__init__(
dataset_path=dataset_path,
input_field=question_field,
golden_output_field=golden_answer_field,
file_format=file_format,
input_transform=question_transform,
output_transform=answer_transform,
default_value=default_value,
**kwargs,
)
# Store RAG-specific fields
self.question_field = question_field
self.context_field = context_field
self.golden_answer_field = golden_answer_field
self.question_transform = question_transform
self.context_transform = context_transform
self.answer_transform = answer_transform
def _map_field_to_standard_name(
self, field_data: Dict[str, Any], field_type: str
) -> Any:
"""
Extract the primary value from field data and map it to a standard name.
Args:
field_data: Dictionary of field data
field_type: Type of field ('question', 'context', 'answer')
Returns:
The primary value for the field (always a string)
"""
# If the standard name already exists, use it
if field_type in field_data:
value = field_data[field_type]
# Otherwise, use the first value in the dictionary
elif field_data:
value = next(iter(field_data.values()))
else:
value = self.default_value
# Ensure the value is a string for DSPy compatibility
return self._ensure_string_value(value, field_type)
def adapt(self) -> List[Dict[str, Any]]:
"""
Transform the JSON dataset into standardized format with question, context, and answer.
Returns:
List of standardized examples with question, context, and answer fields
"""
# Load raw data
raw_data = self.load_raw_data()
# Transform into standardized format
standardized_data = []
for item in raw_data:
# Process question, context, and answer fields
question_data = self._process_fields(
item, self.question_field, self.question_transform, is_input=True
)
context_data = self._process_fields(
item, self.context_field, self.context_transform, is_input=True
)
answer_data = self._process_fields(
item, self.golden_answer_field, self.answer_transform, is_input=False
)
# Create standardized inputs with question and context
inputs = {}
inputs.update(question_data) # Include all question fields
# Ensure 'question' field exists
if "question" not in inputs:
inputs["question"] = self._map_field_to_standard_name(
question_data, "question"
)
# Add context fields
context_value = self._map_field_to_standard_name(context_data, "context")
inputs["context"] = context_value
# Create standardized outputs
outputs = {}
outputs.update(answer_data) # Include all answer fields
# Ensure 'answer' field exists
if "answer" not in outputs:
outputs["answer"] = self._map_field_to_standard_name(
answer_data, "answer"
)
# Create standardized example
standardized_example = {
"inputs": inputs,
"outputs": outputs,
"metadata": {},
}
standardized_data.append(standardized_example)
return standardized_data
def create_dspy_example(doc: Dict[str, Any]) -> dspy.Example:
"""
Convert a standardized document into a DSPy example.
Args:
doc: Standardized document with 'inputs' and 'outputs' dictionaries
Returns:
DSPy example
Raises:
ValueError: If the document structure is invalid or contains non-string values
"""
# Validate document structure
if "inputs" not in doc or "outputs" not in doc:
raise ValueError(
f"Document must contain 'inputs' and 'outputs' keys. Found: {list(doc.keys())}"
)
if not isinstance(doc["inputs"], dict):
raise ValueError(
f"'inputs' must be a dictionary, got {type(doc['inputs']).__name__}"
)
if not isinstance(doc["outputs"], dict):
raise ValueError(
f"'outputs' must be a dictionary, got {type(doc['outputs']).__name__}"
)
# Validate that all input and output values are strings (or can be converted)
validated_inputs = {}
for key, value in doc["inputs"].items():
if not isinstance(value, str):
logging.warning(
f"Input field '{key}' is not a string (type: {type(value).__name__}). "
f"Converting to string for DSPy compatibility."
)
if isinstance(value, (dict, list)):
value = json.dumps(value, ensure_ascii=False)
else:
value = str(value)
validated_inputs[key] = value
validated_outputs = {}
for key, value in doc["outputs"].items():
if not isinstance(value, str):
logging.warning(
f"Output field '{key}' is not a string (type: {type(value).__name__}). "
f"Converting to string for DSPy compatibility."
)
if isinstance(value, (dict, list)):
value = json.dumps(value, ensure_ascii=False)
else:
value = str(value)
validated_outputs[key] = value
# Verify that standard fields exist
if "question" not in validated_inputs and "query" not in validated_inputs:
logging.error(
f"❌ DSPy Example missing 'question' field! "
f"Input keys: {list(validated_inputs.keys())}. "
f"This will cause DSPy optimization to fail. "
f"Check your dataset adapter configuration."
)
if "answer" not in validated_outputs:
logging.error(
f"❌ DSPy Example missing 'answer' field! "
f"Output keys: {list(validated_outputs.keys())}. "
f"This will cause DSPy optimization to fail. "
f"Check your dataset adapter configuration."
)
# Create example with validated inputs and outputs
example = dspy.Example(**validated_inputs, **validated_outputs)
# Set input and output keys explicitly
example._input_keys = set(validated_inputs.keys())
example._output_keys = set(validated_outputs.keys())
# Log for debugging
logging.debug(
f"Created DSPy Example with input_keys={example._input_keys}, "
f"output_keys={example._output_keys}"
)
# Add metadata if available
if "metadata" in doc:
for key, value in doc["metadata"].items():
setattr(example, key, value)
return example
def load_dataset(
adapter: DatasetAdapter,
train_size: float = 0.60,
validation_size: float = 0.20,
seed: int = 42,
) -> Tuple[List[dspy.Example], List[dspy.Example], List[dspy.Example]]:
"""
Load dataset using an adapter and split into train, validation, and test sets.
Args:
adapter: Dataset adapter
train_size: Fraction of data to use for training
validation_size: Fraction of data to use for validation
seed: Random seed for shuffling
Returns:
Tuple containing (trainset, valset, testset)
"""
# Get standardized data
data = adapter.adapt()
logging.info(f"Loaded {len(data)} examples from {adapter.dataset_path}")
# Convert to DSPy examples
dspy_dataset = [create_dspy_example(doc) for doc in data]
# Split dataset
total = len(dspy_dataset)
train_end = int(total * train_size)
val_end = train_end + int(total * validation_size)
trainset = dspy_dataset[:train_end]
valset = dspy_dataset[train_end:val_end]
testset = dspy_dataset[val_end:]
logging.info(f"Created dataset splits:")
logging.info(
f" - Training: {len(trainset)} examples ({train_size*100:.1f}% of total)"
)
logging.info(
f" - Validation: {len(valset)} examples ({validation_size*100:.1f}% of total)"
)
logging.info(
f" - Testing: {len(testset)} examples ({(1-train_size-validation_size)*100:.1f}% of total)"
)
return trainset, valset, testset