Dspy.ReAct (DSPex v0.11.0)

Copy Markdown View Source

Wrapper for Python class ReAct.

Summary

Functions

Python method ReAct._async_call_with_potential_trajectory_truncation.

Python method ReAct._base_init.

Python method ReAct._call_with_potential_trajectory_truncation.

Python method ReAct._format_trajectory.

Python method ReAct._set_lm_usage.

Python method ReAct.acall.

Python method ReAct.aforward.

Processes a list of dspy.Example instances in parallel using the Parallel module.

Deep copy the module.

Python method ReAct.dump_state.

Python method ReAct.forward.

Python method ReAct.get_lm.

Python method ReAct.inspect_history.

Load the saved module. You may also want to check out dspy.load, if you want to

Python method ReAct.load_state.

Applies a function to all named predictors.

Unlike PyTorch, handles (non-recursive) lists of parameters too.

Python method ReAct.named_predictors.

Find all sub-modules in the module, as well as their names.

ReAct stands for "Reasoning and Acting," a popular paradigm for building tool-using agents.

Python method ReAct.parameters.

Python method ReAct.predictors.

Deep copy the module and reset all parameters.

Python method ReAct.set_lm.

Truncates the trajectory so that it fits in the context window.

Types

t()

@opaque t()

Functions

_async_call_with_potential_trajectory_truncation(ref, module, trajectory, opts \\ [])

@spec _async_call_with_potential_trajectory_truncation(
  SnakeBridge.Ref.t(),
  term(),
  term(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct._async_call_with_potential_trajectory_truncation.

Parameters

  • module (term())
  • trajectory (term())
  • input_args (term())

Returns

  • term()

_base_init(ref, opts \\ [])

@spec _base_init(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct._base_init.

Returns

  • term()

_call_with_potential_trajectory_truncation(ref, module, trajectory, opts \\ [])

@spec _call_with_potential_trajectory_truncation(
  SnakeBridge.Ref.t(),
  term(),
  term(),
  keyword()
) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct._call_with_potential_trajectory_truncation.

Parameters

  • module (term())
  • trajectory (term())
  • input_args (term())

Returns

  • term()

_format_trajectory(ref, trajectory, opts \\ [])

@spec _format_trajectory(
  SnakeBridge.Ref.t(),
  %{optional(String.t()) => term()},
  keyword()
) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct._format_trajectory.

Parameters

  • trajectory (%{optional(String.t()) => term()})

Returns

  • term()

_set_lm_usage(ref, tokens, output, opts \\ [])

@spec _set_lm_usage(
  SnakeBridge.Ref.t(),
  %{optional(String.t()) => term()},
  term(),
  keyword()
) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct._set_lm_usage.

Parameters

  • tokens (%{optional(String.t()) => term()})
  • output (term())

Returns

  • term()

acall(ref, args, opts \\ [])

@spec acall(SnakeBridge.Ref.t(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.acall.

Parameters

  • args (term())
  • kwargs (term())

Returns

  • term()

aforward(ref, opts \\ [])

@spec aforward(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.aforward.

Parameters

  • input_args (term())

Returns

  • term()

batch(ref, examples, args, opts \\ [])

@spec batch(SnakeBridge.Ref.t(), [term()], [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Processes a list of dspy.Example instances in parallel using the Parallel module.

Parameters

  • examples - List of dspy.Example instances to process.
  • num_threads - Number of threads to use for parallel processing.
  • max_errors - Maximum number of errors allowed before stopping execution. If None, inherits from dspy.settings.max_errors.
  • return_failed_examples - Whether to return failed examples and exceptions.
  • provide_traceback - Whether to include traceback information in error logs.
  • disable_progress_bar - Whether to display the progress bar.
  • timeout - Seconds before a straggler task is resubmitted. Set to 0 to disable.
  • straggler_limit - Only check for stragglers when this many or fewer tasks remain.

Returns

  • term()

deepcopy(ref, opts \\ [])

@spec deepcopy(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Deep copy the module.

This is a tweak to the default python deepcopy that only deep copies self.parameters(), and for other attributes, we just do the shallow copy.

Returns

  • term()

dump_state(ref, args, opts \\ [])

@spec dump_state(SnakeBridge.Ref.t(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.dump_state.

Parameters

  • json_mode (term() default: True)

Returns

  • term()

forward(ref, opts \\ [])

@spec forward(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.forward.

Parameters

  • input_args (term())

Returns

  • term()

get_lm(ref, opts \\ [])

@spec get_lm(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.get_lm.

Returns

  • term()

inspect_history(ref, args, opts \\ [])

@spec inspect_history(SnakeBridge.Ref.t(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.inspect_history.

Parameters

  • n (integer() default: 1)

Returns

  • term()

load(ref, path, args, opts \\ [])

@spec load(SnakeBridge.Ref.t(), term(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Load the saved module. You may also want to check out dspy.load, if you want to

load an entire program, not just the state for an existing program.

Parameters

  • path - Path to the saved state file, which should be a .json or a .pkl file (type: String.t())
  • allow_pickle - If True, allow loading .pkl files, which can run arbitrary code. This is dangerous and should only be used if you are sure about the source of the file and in a trusted environment. (type: boolean())

Returns

  • term()

load_state(ref, state, opts \\ [])

@spec load_state(SnakeBridge.Ref.t(), term(), keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.load_state.

Parameters

  • state (term())

Returns

  • term()

map_named_predictors(ref, func, opts \\ [])

@spec map_named_predictors(SnakeBridge.Ref.t(), term(), keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Applies a function to all named predictors.

Parameters

  • func (term())

Returns

  • term()

named_parameters(ref, opts \\ [])

@spec named_parameters(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Unlike PyTorch, handles (non-recursive) lists of parameters too.

Returns

  • term()

named_predictors(ref, opts \\ [])

@spec named_predictors(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.named_predictors.

Returns

  • term()

named_sub_modules(ref, args, opts \\ [])

@spec named_sub_modules(SnakeBridge.Ref.t(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Find all sub-modules in the module, as well as their names.

Say self.children[4]['key'].sub_module is a sub-module. Then the name will be children[4]['key'].sub_module. But if the sub-module is accessible at different paths, only one of the paths will be returned.

Parameters

  • type_ (term() default: None)
  • skip_compiled (term() default: False)

Returns

  • term()

new(signature, tools, args, opts \\ [])

@spec new(term(), [term()], [term()], keyword()) ::
  {:ok, SnakeBridge.Ref.t()} | {:error, Snakepit.Error.t()}

ReAct stands for "Reasoning and Acting," a popular paradigm for building tool-using agents.

In this approach, the language model is iteratively provided with a list of tools and has to reason about the current situation. The model decides whether to call a tool to gather more information or to finish the task based on its reasoning process. The DSPy version of ReAct is generalized to work over any signature, thanks to signature polymorphism.

Parameters

  • signature - The signature of the module, which defines the input and output of the react module.
  • tools - A list of functions, callable objects, or dspy.Tool instances. (type: list(Callable))
  • max_iters - The maximum number of iterations to run. Defaults to 10. (type: integer() | nil)

parameters(ref, opts \\ [])

@spec parameters(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.parameters.

Returns

  • term()

predictors(ref, opts \\ [])

@spec predictors(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.predictors.

Returns

  • term()

reset_copy(ref, opts \\ [])

@spec reset_copy(
  SnakeBridge.Ref.t(),
  keyword()
) :: {:ok, term()} | {:error, Snakepit.Error.t()}

Deep copy the module and reset all parameters.

Returns

  • term()

save(ref, path, args, opts \\ [])

@spec save(SnakeBridge.Ref.t(), term(), [term()], keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Save the module.

Save the module to a directory or a file. There are two modes:

  • save_program=False: Save only the state of the module to a json or pickle file, based on the value of the file extension.
  • save_program=True: Save the whole module to a directory via cloudpickle, which contains both the state and architecture of the model.

If save_program=True and modules_to_serialize are provided, it will register those modules for serialization with cloudpickle's register_pickle_by_value. This causes cloudpickle to serialize the module by value rather than by reference, ensuring the module is fully preserved along with the saved program. This is useful when you have custom modules that need to be serialized alongside your program. If None, then no modules will be registered for serialization.

We also save the dependency versions, so that the loaded model can check if there is a version mismatch on critical dependencies or DSPy version.

Parameters

  • path - Path to the saved state file, which should be a .json or .pkl file when save_program=False, and a directory when save_program=True. (type: String.t())
  • save_program - If True, save the whole module to a directory via cloudpickle, otherwise only save the state. (type: boolean())
  • modules_to_serialize - A list of modules to serialize with cloudpickle's register_pickle_by_value. If None, then no modules will be registered for serialization. (type: list())

Returns

  • term()

set_lm(ref, lm, opts \\ [])

@spec set_lm(SnakeBridge.Ref.t(), term(), keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Python method ReAct.set_lm.

Parameters

  • lm (term())

Returns

  • term()

truncate_trajectory(ref, trajectory, opts \\ [])

@spec truncate_trajectory(SnakeBridge.Ref.t(), term(), keyword()) ::
  {:ok, term()} | {:error, Snakepit.Error.t()}

Truncates the trajectory so that it fits in the context window.

Users can override this method to implement their own truncation logic.

Parameters

  • trajectory (term())

Returns

  • term()