Tool Agent
Using tools (formerly functions), you can define a built-in agent that can take decisions and reasoning based on the tools provided to it.
Letβs illustrate this concept using the llamaindex sum div sample: an agent that can sum or divide two numbers and needs to answer basic arithmetic questions.
Using tools
By declaring tools (and providing a descriptive description), you provide the opportunity for the LLM to requests a tool call during the output generation. In the snippet below, we declare a tool that can sum two numbers. It will be called by the LLM when a sum operation is required.
defTool( "sum", "Sum two numbers", { type: "object", properties: { a: { type: "number", description: "The first number", }, b: { type: "number", description: "The second number", }, }, required: ["a", "b"], }, ({ a, b }) => `${a + b}`)
You can also simplify the parameter definition by provider an example object and the schema will be inferred._createMdxContent
defTool("sum", "Sum two numbers", { a: 1, b: 2 }, ({ a, b }) => `${a + b}`)
Parameters
The arithmetic question can be declared as a script parameter to be used in the agent script.
script({ ..., parameters: { "question": { type: "string", default: "How much is 5 + 5? then divide by 2?" } }})
The parameter value are populated in the env.vars
object.
...$`Answer the following arithmetic question:
${env.vars.question}`
Putting it all together, we define another tool to divide two numbers and inline an arithmetic question.
script({ title: "math-agent", model: "small", description: "A port of https://ts.llamaindex.ai/examples/agent", parameters: { question: { type: "string", default: "How much is 11 + 4? then divide by 3?", }, }, tests: { description: "Testing the default prompt", keywords: "5", },})
defTool( "sum", "Use this function to sum two numbers", { type: "object", properties: { a: { type: "number", description: "The first number", }, b: { type: "number", description: "The second number", }, }, required: ["a", "b"], }, ({ a, b }) => `${a + b}`)
defTool( "divide", "Use this function to divide two numbers", { type: "object", properties: { a: { type: "number", description: "The first number", }, b: { type: "number", description: "The second number", }, }, required: ["a", "b"], }, ({ a, b }) => `${a / b}`)
$`Answer the following arithmetic question:
${env.vars.question}`
π€ user
Answer the following arithmetic question:
How much is 11 + 4? then divide by 3?
π€ assistant
- π tool call
divide({"a":15,"b":3})
(call_9p0oWdWpT6vGyxzwq2vJXHrq
)
π οΈ tool call_9p0oWdWpT6vGyxzwq2vJXHrq
5
π€ assistant
The result of (11 + 4) divided by 3 is 5.
Using system.math
The system prompt system.math
wraps the parsers.math
expression parser and evaluator and exposes it as a tool.
This simplifies the agent script as we do not have to define tools anymore.
script({ title: "math-agent-system", model: "small", description: "A port of https://ts.llamaindex.ai/examples/agent", system: ["system", "system.math", "system.tools"], parameters: { "question": { type: "string", default: "How much is (11 + 4 / 9.11)? then divide by 3.13?" } }, tests: { description: "Testing the default prompt", keywords: "5" }})
$`Respond this math question:
${env.vars.question}
- do not generate python code- print the final result in text format`