Find answers from the community

Updated last month

How to force an agent to always use tools in an order?

@kapa.ai How to force an agent to always use tools in an order?
k
2 comments
----------------------
To force an agent to always use tools in a specific order, you can use the OpenAIAgent or ReActAgent from the LlamaIndex library. You can specify the tools in the order you want them to be used when creating the agent. Here's a general example:

Plain Text
from llama_index.core.tools import FunctionTool
from llama_index.llms.openai import OpenAI
from llama_index.agent.openai import OpenAIAgent

# Define your tools
tool1 = FunctionTool.from_defaults(fn=your_function1)
tool2 = FunctionTool.from_defaults(fn=your_function2)
tool3 = FunctionTool.from_defaults(fn=your_function3)

# Define the language model
llm = OpenAI(model="gpt-4")

# Create the agent with the tools in the desired order
agent = OpenAIAgent.from_tools([tool1, tool2, tool3], llm=llm, verbose=True)


In this example, your_function1, your_function2, and your_function3 should be replaced with your actual functions. The agent will use the tools in the order they are listed.

However, please note that the agent will not necessarily use the tools in the exact order they are provided. The agent's behavior is determined by the underlying language model (in this case, gpt-4), which makes decisions based on the input and the system prompt. The agent will try to use the most appropriate tool for each task, which may not always be the next tool in the list.

If you want to force the agent to use a specific tool for a specific task, you can use the tool_choice parameter when calling the chat or query method on the agent. For example:

Plain Text
response = agent.chat("Your question here", tool_choice="tool1")


In this case, the agent will be forced to use tool1 to answer the question.
For more details, you can refer to the LlamaIndex documentation () and the GitHub examples ().
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------
Add a reply
Sign up and join the conversation on Discord