dspy.CodeAct
dspy.CodeAct(signature: Union[str, Type[Signature]], tools: list[Callable], max_iters: int = 5)
Bases: ReAct
, ProgramOfThought
CodeAct is a module that utilizes the Code Interpreter and predefined tools to solve the problem.
Initializes the CodeAct class with the specified model, temperature, and max tokens.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
signature
|
Union[str, Type[Signature]]
|
The signature of the module. |
required |
tools
|
list[Callable]
|
The tool callables to be used. CodeAct only accepts functions and not callable objects. |
required |
max_iters
|
int
|
The maximum number of iterations to generate the answer. |
5
|
Example
Source code in dspy/predict/code_act.py
Functions
__call__(*args, **kwargs)
Source code in dspy/primitives/program.py
batch(examples, num_threads: Optional[int] = None, max_errors: Optional[int] = None, return_failed_examples: bool = False, provide_traceback: Optional[bool] = None, disable_progress_bar: bool = False)
Processes a list of dspy.Example instances in parallel using the Parallel module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
examples
|
List of dspy.Example instances to process. |
required | |
num_threads
|
Optional[int]
|
Number of threads to use for parallel processing. |
None
|
max_errors
|
Optional[int]
|
Maximum number of errors allowed before stopping execution.
If |
None
|
return_failed_examples
|
bool
|
Whether to return failed examples and exceptions. |
False
|
provide_traceback
|
Optional[bool]
|
Whether to include traceback information in error logs. |
None
|
disable_progress_bar
|
bool
|
Whether to display the progress bar. |
False
|
Returns:
Type | Description |
---|---|
List of results, and optionally failed examples and exceptions. |
Source code in dspy/primitives/program.py
deepcopy()
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.
Source code in dspy/primitives/module.py
dump_state()
get_lm()
inspect_history(n: int = 1)
load(path)
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:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path to the saved state file, which should be a .json or a .pkl file |
required |
Source code in dspy/primitives/module.py
load_state(state)
map_named_predictors(func)
Applies a function to all named predictors.
named_parameters()
Unlike PyTorch, handles (non-recursive) lists of parameters too.
Source code in dspy/primitives/module.py
named_predictors()
named_sub_modules(type_=None, skip_compiled=False) -> Generator[tuple[str, BaseModule], None, None]
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.
Source code in dspy/primitives/module.py
parameters()
predictors()
reset_copy()
save(path, save_program=False, modules_to_serialize=None)
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:
Name | Type | Description | Default |
---|---|---|---|
path
|
str
|
Path to the saved state file, which should be a .json or .pkl file when |
required |
save_program
|
bool
|
If True, save the whole module to a directory via cloudpickle, otherwise only save the state. |
False
|
modules_to_serialize
|
list
|
A list of modules to serialize with cloudpickle's |
None
|
Source code in dspy/primitives/module.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
CodeAct
CodeAct is a DSPy module that combines code generation with tool execution to solve problems. It generates Python code snippets that use provided tools and the Python standard library to accomplish tasks.
Basic Usage
Here's a simple example of using CodeAct:
import dspy
from dspy.predict import CodeAct
# Define a simple tool function
def factorial(n: int) -> int:
"""Calculate the factorial of a number."""
if n == 1:
return 1
return n * factorial(n-1)
# Create a CodeAct instance
act = CodeAct("n->factorial_result", tools=[factorial])
# Use the CodeAct instance
result = act(n=5)
print(result) # Will calculate factorial(5) = 120
How It Works
CodeAct operates in an iterative manner:
- Takes input parameters and available tools
- Generates Python code snippets that use these tools
- Executes the code using a Python sandbox
- Collects the output and determines if the task is complete
- Answer the original question based on the collected information
⚠️ Limitations
Only accepts pure functions as tools (no callable objects)
The following example does not work due to the usage of a callable object.
# ❌ NG
class Add():
def __call__(self, a: int, b: int):
return a + b
dspy.CodeAct("question -> answer", tools=[Add()])
External libraries cannot be used
The following example does not work due to the usage of the external library numpy
.
# ❌ NG
import numpy as np
def exp(i: int):
return np.exp(i)
dspy.CodeAct("question -> answer", tools=[exp])
All dependent functions need to be passed to CodeAct
Functions that depend on other functions or classes not passed to CodeAct
cannot be used. The following example does not work because the tool functions depend on other functions or classes that are not passed to CodeAct
, such as Profile
or secret_function
.
# ❌ NG
from pydantic import BaseModel
class Profile(BaseModel):
name: str
age: int
def age(profile: Profile):
return
def parent_function():
print("Hi!")
def child_function():
parent_function()
dspy.CodeAct("question -> answer", tools=[age, child_function])
Instead, the following example works since all necessary tool functions are passed to CodeAct
: