Skip to content

dspy.Module

dspy.Module(callbacks=None)

Bases: BaseModule

Source code in dspy/primitives/program.py
def __init__(self, callbacks=None):
    self.callbacks = callbacks or []
    self._compiled = False

Functions

__call__(*args, **kwargs)

Source code in dspy/primitives/program.py
@with_callbacks
def __call__(self, *args, **kwargs):
    return self.forward(*args, **kwargs)

batch(examples, num_threads: int = 32, max_errors: int = 10, return_failed_examples: bool = False, provide_traceback: bool = False, disable_progress_bar: bool = False)

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

:param examples: List of dspy.Example instances to process. :param batch_size: Number of threads to use for parallel processing. :param max_errors: Maximum number of errors allowed before stopping execution. :param return_failed_examples: Whether to return failed examples and exceptions. :param provide_traceback: Whether to include traceback information in error logs. :return: List of results, and optionally failed examples and exceptions.

Source code in dspy/primitives/program.py
def batch(
    self,
    examples,
    num_threads: int = 32,
    max_errors: int = 10,
    return_failed_examples: bool = False,
    provide_traceback: bool = False,
    disable_progress_bar: bool = False,
):
    """
    Processes a list of dspy.Example instances in parallel using the Parallel module.

    :param examples: List of dspy.Example instances to process.
    :param batch_size: Number of threads to use for parallel processing.
    :param max_errors: Maximum number of errors allowed before stopping execution.
    :param return_failed_examples: Whether to return failed examples and exceptions.
    :param provide_traceback: Whether to include traceback information in error logs.
    :return: List of results, and optionally failed examples and exceptions.
    """
    # Create a list of execution pairs (self, example)
    exec_pairs = [(self, example.inputs()) for example in examples]

    # Create an instance of Parallel
    parallel_executor = Parallel(
        num_threads=num_threads,
        max_errors=max_errors,
        return_failed_examples=return_failed_examples,
        provide_traceback=provide_traceback,
        disable_progress_bar=disable_progress_bar,
    )

    # Execute the forward method of Parallel
    if return_failed_examples:
        results, failed_examples, exceptions = parallel_executor.forward(exec_pairs)
        return results, failed_examples, exceptions
    else:
        results = parallel_executor.forward(exec_pairs)
        return results

get_lm()

Source code in dspy/primitives/program.py
def get_lm(self):
    all_used_lms = [param.lm for _, param in self.named_predictors()]

    if len(set(all_used_lms)) == 1:
        return all_used_lms[0]

    raise ValueError("Multiple LMs are being used in the module. There's no unique LM to return.")

map_named_predictors(func)

Applies a function to all named predictors.

Source code in dspy/primitives/program.py
def map_named_predictors(self, func):
    """Applies a function to all named predictors."""
    for name, predictor in self.named_predictors():
        set_attribute_by_name(self, name, func(predictor))
    return self

named_predictors()

Source code in dspy/primitives/program.py
def named_predictors(self):
    from dspy.predict.predict import Predict

    return [(name, param) for name, param in self.named_parameters() if isinstance(param, Predict)]

predictors()

Source code in dspy/primitives/program.py
def predictors(self):
    return [param for _, param in self.named_predictors()]

set_lm(lm)

Source code in dspy/primitives/program.py
def set_lm(self, lm):
    for _, param in self.named_predictors():
        param.lm = lm