|
1 | 1 | import logging
|
2 |
| -from typing import Any, Callable, Literal |
| 2 | +from typing import TYPE_CHECKING, Any, Callable, Literal, Type |
3 | 3 |
|
4 | 4 | from litellm import ContextWindowExceededError
|
5 | 5 |
|
|
10 | 10 |
|
11 | 11 | logger = logging.getLogger(__name__)
|
12 | 12 |
|
| 13 | +if TYPE_CHECKING: |
| 14 | + from dspy.signatures.signature import Signature |
| 15 | + |
13 | 16 |
|
14 | 17 | 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): |
16 | 19 | """
|
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 | + ``` |
18 | 40 | """
|
19 | 41 | super().__init__()
|
20 | 42 | self.signature = signature = ensure_signature(signature)
|
|
0 commit comments