react#
React代理的实现和优化。
类
|
ReActAgent 使用生成器作为规划器,运行多个顺序功能调用步骤以生成最终响应。 |
常量
- 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
从给定对象创建一个新的字符串对象。如果指定了编码或错误处理,则对象必须暴露一个数据缓冲区,该缓冲区将使用给定的编码和错误处理程序进行解码。否则,返回 object.__str__() 的结果(如果已定义)或 repr(object)。 编码默认为 sys.getdefaultencoding()。 错误处理默认为 '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]#
基础:
ComponentReActAgent 使用生成器作为规划器,运行多个连续的功能调用步骤以生成最终响应。
用户需要设置: - tools: 用于完成任务的一系列工具。每个工具是一个函数或函数工具。 - max_steps: 代理完成任务可以采取的最大步骤数。 - use_llm_as_fallback: 一个布尔值,决定是否使用额外的LLM模型作为备用工具来回答问题。 - model_client: 用于生成响应的模型客户端。 - model_kwargs: 用于生成响应的模型参数。 - template: 用于生成提示的模板。默认是DEFAULT_REACT_AGENT_SYSTEM_PROMPT。
对于生成器,默认参数是: (1) 默认提示:DEFAULT_REACT_AGENT_SYSTEM_PROMPT (2) 默认输出处理器:JsonParser
有示例是可选的,提示中的字符串示例列表。
示例:
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.",
参考: [1] https://arxiv.org/abs/2210.03629, 发布于2023年3月。