Skip to content

Commit ddc0581

Browse files
Add docstring for dspy.ReAct (#8334)
* Add docstring for dspy.ReAct * improve docstring * fix type hint * Update react.py * Update react.py --------- Co-authored-by: Omar Khattab <okhat@users.noreply.github.com>
1 parent baae93e commit ddc0581

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

dspy/predict/react.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Any, Callable, Literal
2+
from typing import TYPE_CHECKING, Any, Callable, Literal, Type
33

44
from litellm import ContextWindowExceededError
55

@@ -10,11 +10,33 @@
1010

1111
logger = logging.getLogger(__name__)
1212

13+
if TYPE_CHECKING:
14+
from dspy.signatures.signature import Signature
15+
1316

1417
class ReAct(Module):
15-
def __init__(self, signature, tools: list[Callable], max_iters=5):
18+
def __init__(self, signature: Type["Signature"], tools: list[Callable], max_iters: int = 10):
1619
"""
17-
`tools` is either a list of functions, callable classes, or `dspy.Tool` instances.
20+
ReAct stands for "Reasoning and Acting," a popular paradigm for building tool-using agents.
21+
In this approach, the language model is iteratively provided with a list of tools and has
22+
to reason about the current situation. The model decides whether to call a tool to gather more
23+
information or to finish the task based on its reasoning process. The DSPy version of ReAct is
24+
generalized to work over any signature, thanks to signature polymorphism.
25+
26+
Args:
27+
signature: The signature of the module, which defines the input and output of the react module.
28+
tools (list[Callable]): A list of functions, callable objects, or `dspy.Tool` instances.
29+
max_iters (Optional[int]): The maximum number of iterations to run. Defaults to 10.
30+
31+
Example:
32+
33+
```python
34+
def get_weather(city: str) -> str:
35+
return f"The weather in {city} is sunny."
36+
37+
react = dspy.ReAct(signature="question->answer", tools=[get_weather])
38+
pred = react(question="What is the weather in Tokyo?")
39+
```
1840
"""
1941
super().__init__()
2042
self.signature = signature = ensure_signature(signature)

0 commit comments

Comments
 (0)