Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions dspy/predict/parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,46 @@ def __init__(
provide_traceback: bool | None = None,
disable_progress_bar: bool = False,
):
"""
A utility class for parallel, multi-threaded execution of (module, example) pairs.

Supports various example formats (e.g., `Example`, dict, tuple, list), robust error handling,
optional progress tracking, and can optionally return failed examples and exceptions.

Args:
num_threads (Optional[int]): The number of threads to use. Defaults to `settings.num_threads`.
max_errors (Optional[int]): The maximum number of errors allowed before raising an exception. Defaults to `settings.max_errors`.
access_examples (bool): Whether to unpack `Example` objects via `.inputs()`. Defaults to True.
return_failed_examples (bool): Whether to return failed examples. Defaults to False.
provide_traceback (Optional[bool]): Whether to provide traceback. Defaults to None.
disable_progress_bar (bool): Whether to disable progress bar. Defaults to False.

Example:
```python
import dspy
from dspy.predict.parallel import Parallel
lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=lm)

examples = [
{"question": "What is the capital of Spain?"},
{"question": "What is 3 * 4?"},
{"question": "Who wrote Hamlet?"},
]

module=dspy.Predict("question->answer")
exec_pairs = [(module, example) for example in examples]
parallel = Parallel(num_threads=3, disable_progress_bar=False)
results = parallel(exec_pairs)
for i, result in enumerate(results):
print(f"Result {i+1}: {result.answer}")

# Expected Output:
# Result 1: Madrid
# Result 2: 12
# Result 3: William Shakespeare
```
"""
super().__init__()
self.num_threads = num_threads or settings.num_threads
self.max_errors = settings.max_errors if max_errors is None else max_errors
Expand Down