Skip to content

Commit 046abd8

Browse files
authored
Add doc page to learn tool usage in DSPy (#8709)
1 parent b4b9bab commit 046abd8

File tree

2 files changed

+195
-0
lines changed

2 files changed

+195
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
---
2+
sidebar_position: 2
3+
---
4+
5+
# Tools
6+
7+
DSPy provides powerful support for **tool-using agents** that can interact with external functions, APIs, and services. Tools enable language models to go beyond text generation by performing actions, retrieving information, and processing data dynamically.
8+
9+
There are two main approaches to using tools in DSPy:
10+
11+
1. **`dspy.ReAct`** - A fully managed tool agent that handles reasoning and tool calls automatically
12+
2. **Manual tool handling** - Direct control over tool calls using `dspy.Tool`, `dspy.ToolCalls`, and custom signatures
13+
14+
## Approach 1: Using `dspy.ReAct` (Fully Managed)
15+
16+
The `dspy.ReAct` module implements the Reasoning and Acting (ReAct) pattern, where the language model iteratively reasons about the current situation and decides which tools to call.
17+
18+
### Basic Example
19+
20+
```python
21+
import dspy
22+
23+
# Define your tools as functions
24+
def get_weather(city: str) -> str:
25+
"""Get the current weather for a city."""
26+
# In a real implementation, this would call a weather API
27+
return f"The weather in {city} is sunny and 75°F"
28+
29+
def search_web(query: str) -> str:
30+
"""Search the web for information."""
31+
# In a real implementation, this would call a search API
32+
return f"Search results for '{query}': [relevant information...]"
33+
34+
# Create a ReAct agent
35+
react_agent = dspy.ReAct(
36+
signature="question -> answer",
37+
tools=[get_weather, search_web],
38+
max_iters=5
39+
)
40+
41+
# Use the agent
42+
result = react_agent(question="What's the weather like in Tokyo?")
43+
print(result.answer)
44+
print("Tool calls made:", result.trajectory)
45+
```
46+
47+
### ReAct Features
48+
49+
- **Automatic reasoning**: The model thinks through the problem step by step
50+
- **Tool selection**: Automatically chooses which tool to use based on the situation
51+
- **Iterative execution**: Can make multiple tool calls to gather information
52+
- **Error handling**: Built-in error recovery for failed tool calls
53+
- **Trajectory tracking**: Complete history of reasoning and tool calls
54+
55+
### ReAct Parameters
56+
57+
```python
58+
react_agent = dspy.ReAct(
59+
signature="question -> answer", # Input/output specification
60+
tools=[tool1, tool2, tool3], # List of available tools
61+
max_iters=10 # Maximum number of tool call iterations
62+
)
63+
```
64+
65+
## Approach 2: Manual Tool Handling
66+
67+
For more control over the tool calling process, you can manually handle tools using DSPy's tool types.
68+
69+
### Basic Setup
70+
71+
```python
72+
import dspy
73+
74+
class ToolSignature(dspy.Signature):
75+
"""Signature for manual tool handling."""
76+
question: str = dspy.InputField()
77+
tools: list[dspy.Tool] = dspy.InputField()
78+
outputs: dspy.ToolCalls = dspy.OutputField()
79+
80+
def weather(city: str) -> str:
81+
"""Get weather information for a city."""
82+
return f"The weather in {city} is sunny"
83+
84+
def calculator(expression: str) -> str:
85+
"""Evaluate a mathematical expression."""
86+
try:
87+
result = eval(expression) # Note: Use safely in production
88+
return f"The result is {result}"
89+
except:
90+
return "Invalid expression"
91+
92+
# Create tool instances
93+
tools = {
94+
"weather": dspy.Tool(weather),
95+
"calculator": dspy.Tool(calculator)
96+
}
97+
98+
# Create predictor
99+
predictor = dspy.Predict(ToolSignature)
100+
101+
# Make a prediction
102+
response = predictor(
103+
question="What's the weather in New York?",
104+
tools=list(tools.values())
105+
)
106+
107+
# Execute the tool calls
108+
for call in response.outputs.tool_calls:
109+
if call.name in tools:
110+
result = tools[call.name](**call.args)
111+
print(f"Tool: {call.name}")
112+
print(f"Args: {call.args}")
113+
print(f"Result: {result}")
114+
```
115+
116+
### Understanding `dspy.Tool`
117+
118+
The `dspy.Tool` class wraps regular Python functions to make them compatible with DSPy's tool system:
119+
120+
```python
121+
def my_function(param1: str, param2: int = 5) -> str:
122+
"""A sample function with parameters."""
123+
return f"Processed {param1} with value {param2}"
124+
125+
# Create a tool
126+
tool = dspy.Tool(my_function)
127+
128+
# Tool properties
129+
print(tool.name) # "my_function"
130+
print(tool.desc) # The function's docstring
131+
print(tool.args) # Parameter schema
132+
print(str(tool)) # Full tool description
133+
```
134+
135+
### Understanding `dspy.ToolCalls`
136+
137+
The `dspy.ToolCalls` type represents the output from a model that can make tool calls:
138+
139+
```python
140+
# After getting a response with tool calls
141+
for call in response.outputs.tool_calls:
142+
print(f"Tool name: {call.name}")
143+
print(f"Arguments: {call.args}")
144+
145+
# Execute the tool
146+
if call.name in available_tools:
147+
result = available_tools[call.name](**call.args)
148+
print(f"Result: {result}")
149+
```
150+
151+
## Best Practices
152+
153+
### 1. Tool Function Design
154+
155+
- **Clear docstrings**: Tools work better with descriptive documentation
156+
- **Type hints**: Provide clear parameter and return types
157+
- **Simple parameters**: Use basic types (str, int, bool, dict, list) or Pydantic models
158+
159+
```python
160+
def good_tool(city: str, units: str = "celsius") -> str:
161+
"""
162+
Get weather information for a specific city.
163+
164+
Args:
165+
city: The name of the city to get weather for
166+
units: Temperature units, either 'celsius' or 'fahrenheit'
167+
168+
Returns:
169+
A string describing the current weather conditions
170+
"""
171+
# Implementation with proper error handling
172+
if not city.strip():
173+
return "Error: City name cannot be empty"
174+
175+
# Weather logic here...
176+
return f"Weather in {city}: 25°{units[0].upper()}, sunny"
177+
```
178+
179+
### 2. Choosing Between ReAct and Manual Handling
180+
181+
**Use `dspy.ReAct` when:**
182+
- You want automatic reasoning and tool selection
183+
- The task requires multiple tool calls
184+
- You need built-in error recovery
185+
- You want to focus on tool implementation rather than orchestration
186+
187+
**Use manual tool handling when:**
188+
- You need precise control over tool execution
189+
- You want custom error handling logic
190+
- You want to minimize the latency
191+
- Your tool returns nothing (void function)
192+
193+
194+
Tools in DSPy provide a powerful way to extend language model capabilities beyond text generation. Whether using the fully automated ReAct approach or manual tool handling, you can build sophisticated agents that interact with the world through code.

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ nav:
1818
- Signatures: learn/programming/signatures.md
1919
- Modules: learn/programming/modules.md
2020
- Adapters: learn/programming/adapters.md
21+
- Tools: learn/programming/tools.md
2122
- DSPy Evaluation:
2223
- Evaluation Overview: learn/evaluation/overview.md
2324
- Data Handling: learn/evaluation/data.md

0 commit comments

Comments
 (0)