|
1 | 1 | class Example: |
| 2 | + """A flexible data container for DSPy examples and training data. |
| 3 | +
|
| 4 | + The `Example` class is the standard data format used in DSPy evaluation and optimization. |
| 5 | +
|
| 6 | + Key features: |
| 7 | + - Dictionary-like access patterns (item access, iteration, etc.) |
| 8 | + - Flexible initialization from dictionaries, other `Example` instances, or keyword arguments |
| 9 | + - Input/output field separation for training data |
| 10 | + - Serialization support for saving/loading `Example` instances |
| 11 | + - Immutable operations that return new `Example` instances |
| 12 | +
|
| 13 | + Examples: |
| 14 | +
|
| 15 | + Basic usage with keyword arguments: |
| 16 | +
|
| 17 | + ```python |
| 18 | + import dspy |
| 19 | +
|
| 20 | + # Create an example with input and output fields |
| 21 | + example = dspy.Example( |
| 22 | + question="What is the capital of France?", |
| 23 | + answer="Paris", |
| 24 | + ) |
| 25 | + print(example.question) # "What is the capital of France?" |
| 26 | + print(example.answer) # "Paris" |
| 27 | + ``` |
| 28 | +
|
| 29 | + Initialize from a dictionary: |
| 30 | +
|
| 31 | + ```python |
| 32 | + data = {"question": "What is 2+2?", "answer": "4"} |
| 33 | + example = dspy.Example(data) |
| 34 | + print(example["question"]) # "What is 2+2?" |
| 35 | + ``` |
| 36 | +
|
| 37 | + Copy from another Example: |
| 38 | +
|
| 39 | + ```python |
| 40 | + original = dspy.Example(question="Hello", answer="World") |
| 41 | + copy = dspy.Example(original) |
| 42 | + print(copy.question) # "Hello" |
| 43 | + ``` |
| 44 | +
|
| 45 | + Working with input/output separation: |
| 46 | +
|
| 47 | + ```python |
| 48 | + # Mark which fields are inputs for training |
| 49 | + example = dspy.Example( |
| 50 | + question="What is the weather?", |
| 51 | + answer="It's sunny", |
| 52 | + ).with_inputs("question") |
| 53 | +
|
| 54 | + # Get only input fields |
| 55 | + inputs = example.inputs() |
| 56 | + print(inputs.question) # "What is the weather?" |
| 57 | +
|
| 58 | + # Get only output fields (labels) |
| 59 | + labels = example.labels() |
| 60 | + print(labels.answer) # "It's sunny" |
| 61 | + ``` |
| 62 | +
|
| 63 | + Dictionary-like operations: |
| 64 | +
|
| 65 | + ```python |
| 66 | + example = dspy.Example(name="Alice", age=30) |
| 67 | +
|
| 68 | + # Check if key exists |
| 69 | + if "name" in example: |
| 70 | + print("Name field exists") |
| 71 | +
|
| 72 | + # Get with default value |
| 73 | + city = example.get("city", "Unknown") |
| 74 | + print(city) # "Unknown" |
| 75 | + ``` |
| 76 | + """ |
| 77 | + |
2 | 78 | def __init__(self, base=None, **kwargs): |
| 79 | + """Initialize an Example instance. |
| 80 | +
|
| 81 | + Args: |
| 82 | + base: Optional base data source. Can be: |
| 83 | + - Another Example instance (copies its data) |
| 84 | + - A dictionary (copies its key-value pairs) |
| 85 | + - None (creates empty Example) |
| 86 | + **kwargs: Additional key-value pairs to store in the Example. |
| 87 | + """ |
3 | 88 | # Internal storage and other attributes |
4 | 89 | self._store = {} |
5 | 90 | self._demos = [] |
|
0 commit comments