Bases: Example
A prediction object that contains the output of a DSPy module.
Prediction inherits from Example.
To allow feedback-augmented scores, Prediction supports comparison operations
(<, >, <=, >=) for Predictions with a score field. The comparison operations
compare the 'score' values as floats. For equality comparison, Predictions are equal
if their underlying data stores are equal (inherited from Example).
Arithmetic operations (+, /, etc.) are also supported for Predictions with a 'score'
field, operating on the score value.
Source code in dspy/primitives/prediction.py
| def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
del self._demos
del self._input_keys
self._completions = None
self._lm_usage = None
|
Functions
copy(**kwargs)
Source code in dspy/primitives/example.py
| def copy(self, **kwargs):
return type(self)(base=self, **kwargs)
|
from_completions(list_or_dict, signature=None)
classmethod
Source code in dspy/primitives/prediction.py
| @classmethod
def from_completions(cls, list_or_dict, signature=None):
obj = cls()
obj._completions = Completions(list_or_dict, signature=signature)
obj._store = {k: v[0] for k, v in obj._completions.items()}
return obj
|
get(key, default=None)
Source code in dspy/primitives/example.py
| def get(self, key, default=None):
return self._store.get(key, default)
|
get_lm_usage()
Source code in dspy/primitives/prediction.py
| def get_lm_usage(self):
return self._lm_usage
|
Source code in dspy/primitives/example.py
| def inputs(self):
if self._input_keys is None:
raise ValueError("Inputs have not been set for this example. Use `example.with_inputs()` to set them.")
# return items that are in input_keys
d = {key: self._store[key] for key in self._store if key in self._input_keys}
# return type(self)(d)
new_instance = type(self)(base=d)
new_instance._input_keys = self._input_keys # Preserve input_keys in new instance
return new_instance
|
items(include_dspy=False)
Source code in dspy/primitives/example.py
| def items(self, include_dspy=False):
return [(k, v) for k, v in self._store.items() if not k.startswith("dspy_") or include_dspy]
|
keys(include_dspy=False)
Source code in dspy/primitives/example.py
| def keys(self, include_dspy=False):
return [k for k in self._store.keys() if not k.startswith("dspy_") or include_dspy]
|
labels()
Source code in dspy/primitives/example.py
| def labels(self):
# return items that are NOT in input_keys
input_keys = self.inputs().keys()
d = {key: self._store[key] for key in self._store if key not in input_keys}
return type(self)(d)
|
set_lm_usage(value)
Source code in dspy/primitives/prediction.py
| def set_lm_usage(self, value):
self._lm_usage = value
|
toDict()
Source code in dspy/primitives/example.py
| def toDict(self): # noqa: N802
def convert_to_serializable(value):
if hasattr(value, "toDict"):
return value.toDict()
elif isinstance(value, BaseModel):
# Handle Pydantic models (e.g., dspy.History)
return value.model_dump()
elif isinstance(value, list):
return [convert_to_serializable(item) for item in value]
elif isinstance(value, dict):
return {k: convert_to_serializable(v) for k, v in value.items()}
else:
return value
serializable_store = {}
for k, v in self._store.items():
serializable_store[k] = convert_to_serializable(v)
return serializable_store
|
values(include_dspy=False)
Source code in dspy/primitives/example.py
| def values(self, include_dspy=False):
return [v for k, v in self._store.items() if not k.startswith("dspy_") or include_dspy]
|
Source code in dspy/primitives/example.py
| def with_inputs(self, *keys):
copied = self.copy()
copied._input_keys = set(keys)
return copied
|
without(*keys)
Source code in dspy/primitives/example.py
| def without(self, *keys):
copied = self.copy()
for key in keys:
del copied[key]
return copied
|