react#

Implementation and optimization of React agent.

Classes

ReActAgent([tools, max_steps, ...])

ReActAgent uses generator as a planner that runs multiple and sequential functional call steps to generate the final response.

Constants

DEFAULT_REACT_AGENT_SYSTEM_PROMPT = '<SYS>\n{# role/task description #}\nYou are a helpful assistant.\nAnswer the user\'s query using the tools provided below with minimal steps and maximum accuracy.\n{# REACT instructions #}\nEach step you will read the previous Thought, Action, and Observation(execution result of the action) and then provide the next Thought and Action.\n{# Tools #}\n{% if tools %}\n<TOOLS>\nYou available tools are:\n{% for tool in tools %}\n{{ loop.index }}.\n{{tool}}\n------------------------\n{% endfor %}\n</TOOLS>\n{% endif %}\n{# output format and examples for output format #}\n<OUTPUT_FORMAT>\n{{output_format_str}}\n</OUTPUT_FORMAT>\n<TASK_SPEC>\n{# Task specification to teach the agent how to think using \'divide and conquer\' strategy #}\n- For simple queries: Directly call the ``finish`` action and provide the answer.\n- For complex queries:\n    - Step 1: Read the user query and potentially divide it into subqueries. And get started with the first subquery.\n    - Call one available tool at a time to solve each subquery/subquestion. \\\n    - At step \'finish\', join all subqueries answers and finish the task.\nRemember:\n- Action must call one of the above tools with name. It can not be empty.\n- You will always end with \'finish\' action to finish the task. The answer can be the final answer or failure message.\n</TASK_SPEC>\n</SYS>\n-----------------\nUser query:\n{{ input_str }}\n{# Step History #}\n{% if step_history %}\n<STEPS>\nYour previous steps:\n{% for history in step_history %}\nStep {{ loop.index }}.\n"Thought": "{{history.action.thought}}",\n"Action": "{{history.action.action}}",\n"Observation": "{{history.observation}}"\n------------------------\n{% endfor %}\n</STEPS>\n{% endif %}\nYou:'#

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to ‘strict’.

class ReActAgent(tools: List[Callable | Callable[[...], Awaitable[Any]] | FunctionTool] = [], max_steps: int = 10, add_llm_as_fallback: bool = True, examples: List[FunctionExpression] = [], *, model_client: ModelClient, model_kwargs: Dict = {}, template: str | None = None)[source]#

Bases: Component

ReActAgent uses generator as a planner that runs multiple and sequential functional call steps to generate the final response.

Users need to set up: - tools: a list of tools to use to complete the task. Each tool is a function or a function tool. - max_steps: the maximum number of steps the agent can take to complete the task. - use_llm_as_fallback: a boolean to decide whether to use an additional LLM model as a fallback tool to answer the query. - model_client: the model client to use to generate the response. - model_kwargs: the model kwargs to use to generate the response. - template: the template to use to generate the prompt. Default is DEFAULT_REACT_AGENT_SYSTEM_PROMPT.

For the generator, the default arguments are: (1) default prompt: DEFAULT_REACT_AGENT_SYSTEM_PROMPT (2) default output_processors: JsonParser

There are examples which is optional, a list of string examples in the prompt.

Example:

from core.openai_client import OpenAIClient
from components.agent.react import ReActAgent
from core.func_tool import FunctionTool
# define the tools
def multiply(a: int, b: int) -> int:
    '''Multiply two numbers.'''
    return a * b
def add(a: int, b: int) -> int:
    '''Add two numbers.'''
    return a + b
agent = ReActAgent(
    tools=[multiply, add],
    model_client=OpenAIClient(),
    model_kwargs={"model": "gpt-3.5-turbo"},
)

# Using examples:

call_multiply = FunctionExpression.from_function(
    thought="I want to multiply 3 and 4.",

Reference: [1] https://arxiv.org/abs/2210.03629, published in Mar, 2023.

reset()[source]#

Reset the agent to start a new query.

call(input: str, promt_kwargs: Dict | None = {}, model_kwargs: Dict | None = {}) Any[source]#

prompt_kwargs: additional prompt kwargs to either replace or add to the preset prompt kwargs.