DSPy: The framework for programming—not prompting—language models
Python
Add docstring for dspy.ReAct (#8334)
Chen Qianddc05817b302
diff --git a/dspy/predict/react.py b/dspy/predict/react.pyindex cc2a8a027..9b19482bd 100644--- a/dspy/predict/react.py+++ b/dspy/predict/react.py@@ -1,5 +1,5 @@import logging-from typing import Any, Callable, Literal+from typing import TYPE_CHECKING, Any, Callable, Literal, Typefrom litellm import ContextWindowExceededError@@ -10,11 +10,33 @@ from dspy.signatures.signature import ensure_signaturelogger = logging.getLogger(__name__)+if TYPE_CHECKING:+ from dspy.signatures.signature import Signature+class ReAct(Module):- def __init__(self, signature, tools: list[Callable], max_iters=5):+ def __init__(self, signature: Type["Signature"], tools: list[Callable], max_iters: int = 10):"""- `tools` is either a list of functions, callable classes, or `dspy.Tool` instances.+ ReAct stands for "Reasoning and Acting," a popular paradigm for building tool-using agents.+ In this approach, the language model is iteratively provided with a list of tools and has+ to reason about the current situation. The model decides whether to call a tool to gather more+ information or to finish the task based on its reasoning process. The DSPy version of ReAct is+ generalized to work over any signature, thanks to signature polymorphism.++ Args:+ signature: The signature of the module, which defines the input and output of the react module.+ tools (list[Callable]): A list of functions, callable objects, or `dspy.Tool` instances.+ max_iters (Optional[int]): The maximum number of iterations to run. Defaults to 10.++ Example:++ ```python+ def get_weather(city: str) -> str:+ return f"The weather in {city} is sunny."++ react = dspy.ReAct(signature="question->answer", tools=[get_weather])+ pred = react(question="What is the weather in Tokyo?")+ ```"""super().__init__()self.signature = signature = ensure_signature(signature)