Skip to content

Commit 1c8172a

Browse files
authored
add developer guide for codeact (#8299)
1 parent 9c3b5ce commit 1c8172a

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

docs/docs/api/modules/CodeAct.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,107 @@
3131
separate_signature: false
3232
inherited_members: true
3333
<!-- END_API_REF -->
34+
35+
# CodeAct
36+
37+
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.
38+
39+
## Basic Usage
40+
41+
Here's a simple example of using CodeAct:
42+
43+
```python
44+
import dspy
45+
from dspy.predict import CodeAct
46+
47+
# Define a simple tool function
48+
def factorial(n: int) -> int:
49+
"""Calculate the factorial of a number."""
50+
if n == 1:
51+
return 1
52+
return n * factorial(n-1)
53+
54+
# Create a CodeAct instance
55+
act = CodeAct("n->factorial_result", tools=[factorial])
56+
57+
# Use the CodeAct instance
58+
result = act(n=5)
59+
print(result) # Will calculate factorial(5) = 120
60+
```
61+
62+
## How It Works
63+
64+
CodeAct operates in an iterative manner:
65+
66+
1. Takes input parameters and available tools
67+
2. Generates Python code snippets that use these tools
68+
3. Executes the code using a Python sandbox
69+
4. Collects the output and determines if the task is complete
70+
5. Answer the original question based on the collected information
71+
72+
## ⚠️ Limitations
73+
74+
### Only accepts pure functions as tools (no callable objects)
75+
76+
The following example does not work due to the usage of a callable object.
77+
78+
```python
79+
# ❌ NG
80+
class Add():
81+
def __call__(self, a: int, b: int):
82+
return a + b
83+
84+
dspy.CodeAct("question -> answer", tools=[Add()])
85+
```
86+
87+
### External libraries cannot be used
88+
89+
The following example does not work due to the usage of the external library `numpy`.
90+
91+
```python
92+
# ❌ NG
93+
import numpy as np
94+
95+
def exp(i: int):
96+
return np.exp(i)
97+
98+
dspy.CodeAct("question -> answer", tools=[exp])
99+
```
100+
101+
### All dependent functions need to be passed to `CodeAct`
102+
103+
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`.
104+
105+
```python
106+
# ❌ NG
107+
from pydantic import BaseModel
108+
109+
class Profile(BaseModel):
110+
name: str
111+
age: int
112+
113+
def age(profile: Profile):
114+
return
115+
116+
def secret_function():
117+
print("external")
118+
119+
def passed_function():
120+
secret_function()
121+
122+
dspy.CodeAct("question -> answer", tools=[age, secret_function])
123+
```
124+
125+
Instead, the following example works since all necessary tool functions are passed to `CodeAct`:
126+
127+
```python
128+
# ✅ OK
129+
130+
def parent_function():
131+
print("Hi!")
132+
133+
def child_function():
134+
parent_function()
135+
136+
dspy.CodeAct("question -> answer", tools=[parent_function, child_function])
137+
```

0 commit comments

Comments
 (0)