Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Enhance docstring for Parallel class (#9390)

Juntao You7794dbf54b3e
diff --git a/dspy/predict/parallel.py b/dspy/predict/parallel.py
index 057ae43a7..e5b587be1 100644
--- a/dspy/predict/parallel.py
+++ b/dspy/predict/parallel.py
@@ -18,6 +18,46 @@ class Parallel:
timeout: int = 120,
straggler_limit: int = 3,
):
+ """
+ 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 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