Current section

Files

Jump to
dspex lib snakebridge_generated dspy predict parallel.ex
Raw

lib/snakebridge_generated/dspy/predict/parallel.ex

# Generated by SnakeBridge v0.16.0 - DO NOT EDIT MANUALLY
# Regenerate with: mix compile
# Library: dspy 3.2.0
# Python module: dspy.predict
# Python class: Parallel
defmodule Dspy.Predict.Parallel do
@moduledoc """
Wrapper for Python class Parallel.
"""
def __snakebridge_python_name__, do: "dspy.predict"
def __snakebridge_python_class__, do: "Parallel"
def __snakebridge_library__, do: "dspy"
@opaque t :: SnakeBridge.Ref.t()
@doc """
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.
## Parameters
- `num_threads` - The number of threads to use. Defaults to `settings.num_threads`. (type: `integer() | nil`)
- `max_errors` - The maximum number of errors allowed before raising an exception. Defaults to `settings.max_errors`. (type: `integer() | nil`)
- `access_examples` - Whether to unpack `Example` objects via `.inputs()`. Defaults to True. (type: `boolean()`)
- `return_failed_examples` - Whether to return failed examples. Defaults to False. (type: `boolean()`)
- `provide_traceback` - Whether to provide traceback. Defaults to None. (type: `boolean() | nil`)
- `disable_progress_bar` - Whether to disable progress bar. Defaults to False. (type: `boolean()`)
## Examples
```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
```
"""
@spec new(list(term()), keyword()) :: {:ok, SnakeBridge.Ref.t()} | {:error, Snakepit.Error.t()}
def new(args, opts \\ []) do
{args, opts} = SnakeBridge.Runtime.normalize_args_opts(args, opts)
SnakeBridge.Runtime.call_class(__MODULE__, :__init__, [] ++ List.wrap(args), opts)
end
@doc """
Python method `Parallel.forward`.
## Parameters
- `exec_pairs` (list({term(), term()}))
- `num_threads` (term() default: None)
## Returns
- `list(term())`
"""
@spec forward(SnakeBridge.Ref.t(), list({term(), term()}), list(term()), keyword()) ::
{:ok, list(term())} | {:error, Snakepit.Error.t()}
def forward(ref, exec_pairs, args, opts \\ []) do
{args, opts} = SnakeBridge.Runtime.normalize_args_opts(args, opts)
SnakeBridge.Runtime.call_method(ref, :forward, [exec_pairs] ++ List.wrap(args), opts)
end
end