问题验证
- 我已经在文档和Discord上寻找答案。
问题
我正在尝试这个链接提供的ReActAgent示例:https://docs.llamaindex.ai/en/stable/examples/agent/react_agent/?h=react 。我尝试注解掉一个工具,希望当工具不足时,Agent会使用大型模型的推理能力。此外,我在.chat
方法中添加了tool_choice='auto'
。我选择LlamaAPI作为我的llm。
我的代码:
from llama_index.core.agent import ReActAgent
from llama_index.llms.openai import OpenAI
from llama_index.core.llms import ChatMessage
from llama_index.core.tools import BaseTool, FunctionTool
from llama_index.llms.llama_api import LlamaAPI
from llama_index.llms.groq import Groq
import os
os.environ["http_proxy"] = "http://127.0.0.1:10887"
os.environ["https_proxy"] = "http://127.0.0.1:10887"
os.environ["GROQ_API_KEY"] = ""
def multiply(a: int, b: int) -> int:
"""Multiply two integers and returns the result integer"""
return a * b
multiply_tool = FunctionTool.from_defaults(fn=multiply)
# def add(a: int, b: int) -> int:
# """Add two integers and returns the result integer"""
# return a + b
# add_tool = FunctionTool.from_defaults(fn=add)
llm2 = LlamaAPI(model='llama3-70b', api_key='')
agent = ReActAgent.from_tools([multiply_tool], llm=llm2, verbose=True)
response = agent.chat("What is 20+(2*4)? Calculate step by step ", tool_choice='auto')
print('end')
日志:
Thought: The current language of the user is: English. I need to use a tool to help me answer the question.
Action: multiply
Action Input: {'a': 2, 'b': 4}
Observation: 8
Observation: Error: Could not parse output. Please follow the thought-action-input format. Try again.
Thought: I have the result of the multiplication, now I need to add 20 to it.
Action: multiply
Action Input: {'a': 20, 'b': 1}
Observation: 20
Thought: Now I have the result of adding 20, I need to add 8 to it to get the final result.
Action: multiply
Action Input: {'a': 20, 'b': 1}
Observation: 20
Thought: I realize I made a mistake, I should add 8 to 20 instead of multiplying. I don't have an addition tool, but I can use the multiply tool to add 8 to 20 by multiplying 20 by 1 and then adding 8 to the result.
Action: multiply
Action Input: {'a': 8, 'b': 1}
Observation: 8
Observation: Error: Could not parse output. Please follow the thought-action-input format. Try again.
那么我应该如何配置它,以便在Agent找不到正确的工具时利用大型模型本身的推理能力呢?
3条答案
按热度按时间lg40wkob1#
嘿,@nanyoullm!我在这里帮助你解决bug、回答问题,并成为贡献者。让我们一起解决这个挑战吧!🤖
要配置
ReActAgent
以便在代理没有必要的工具时利用大型模型本身的推理能力,你可以在初始化ReActAgent
时不指定任何工具或工具检索器。这将确保代理仅依赖于大型模型进行推理。以下是如何配置
ReActAgent
的示例:在此配置中,
ReActAgent
将没有任何工具可以使用,并依赖于大型模型(llm
)进行所有推理任务。ReActAgent
类设计用于处理没有提供工具或工具检索器的场景,默认情况下,它会使用大型模型进行推理和响应生成。xnifntxz2#
你找到解决方案了吗?
kxeu7u2r3#
如果没有给出正确的工具,它将无法推断出正确的工具,并尝试使用不存在的工具和浪费循环。你可以做的就是定义一个简单的捕获所有工具,它本身只是再次调用LLM,而不知道代理流程。
然后可以这样使用:
现在它总是只使用一个普通的LLM并传递用户问题。但要注意,它会执行代理操作、重述等。但至少它看起来是一个合理的没有给定工具的后备方案。不知道在llamaindex的深处是否已经存在这样一个工具,或者是否可以通过某种魔法函数生成。我需要它,尤其是在使用openai的时候。