llama_index [问题]:ReAct代理是否可以在没有工具的情况下进行推理?

oipij1gg  于 2个月前  发布在  React
关注(0)|答案(3)|浏览(31)

问题验证

  • 我已经在文档和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找不到正确的工具时利用大型模型本身的推理能力呢?

lg40wkob

lg40wkob1#

嘿,@nanyoullm!我在这里帮助你解决bug、回答问题,并成为贡献者。让我们一起解决这个挑战吧!🤖
要配置ReActAgent以便在代理没有必要的工具时利用大型模型本身的推理能力,你可以在初始化ReActAgent时不指定任何工具或工具检索器。这将确保代理仅依赖于大型模型进行推理。
以下是如何配置ReActAgent的示例:

from llama_index.llms.llama_api import LlamaAPI
from llama_index.memory.chat_memory_buffer import ChatMemoryBuffer
from llama_index.agent.react.base import ReActAgent

# Initialize the LLM
llm = LlamaAPI(model='llama3-70b', api_key='LL-5k8J6fjCyrIXrlWruKbr34Ky0npKy64U3bTZbHtxwELI5i8nTLau2Kp5MU6AopSG')

# Initialize the memory
memory = ChatMemoryBuffer.from_defaults(chat_history=[], llm=llm)

# Create the ReActAgent without tools
react_agent = ReActAgent(
    tools=[],  # No tools specified
    llm=llm,
    memory=memory,
    max_iterations=10,
    verbose=True
)

# Now you can use the react_agent for inference
response = react_agent.chat("What is 20+(2*4)? Calculate step by step")
print(response.response)

在此配置中,ReActAgent将没有任何工具可以使用,并依赖于大型模型(llm)进行所有推理任务。ReActAgent类设计用于处理没有提供工具或工具检索器的场景,默认情况下,它会使用大型模型进行推理和响应生成。

  • 要继续与Dosu交谈,请提及@dosu。*
xnifntxz

xnifntxz2#

你找到解决方案了吗?

kxeu7u2r

kxeu7u2r3#

如果没有给出正确的工具,它将无法推断出正确的工具,并尝试使用不存在的工具和浪费循环。你可以做的就是定义一个简单的捕获所有工具,它本身只是再次调用LLM,而不知道代理流程。

然后可以这样使用:

现在它总是只使用一个普通的LLM并传递用户问题。但要注意,它会执行代理操作、重述等。但至少它看起来是一个合理的没有给定工具的后备方案。不知道在llamaindex的深处是否已经存在这样一个工具,或者是否可以通过某种魔法函数生成。我需要它,尤其是在使用openai的时候。

相关问题