Tool

Extending agent capabilities

Agents, by default, are isolated and operate only within the context we provide them. To enable agents to interact with the external world, agents use tools. A tool allows an agent to perform actions or gather additional data based on the input it receives. The results of these actions can be sent back to the LLM, enabling it to refine its next response.

An agent can have multiple tools and can invoke them as needed. Agents may also call tools in sequence to handle more complex tasks.

Tools in AXAR

In AXAR, tools are defined as instance methods and annotated with the @tool decorator. Since tools are plain typescript methods, they provide a structured interface for the agent to interact with them.

Here’s an example:

import {
  model,
  systemPrompt,
  Agent,
  tool,
  schema,
  property,
} from '@axarai/axar';

@schema()
class WeatherParams {
  @property('Location of the user')
  location!: string;
}

@model('openai:gpt-4o-mini')
@systemPrompt(`
  Greet the user based on the current time and weather.
  Get the current time and weather if you need it.
`)
export class GreetingAgent extends Agent<string, string> {
  @tool('Get current time')
  getCurrentTime(): string {
    return `The current time is ${new Date().toLocaleString()}.`;
  }

  @tool('Get weather info')
  getWeatherInfo(weather: WeatherParams): string {
    return `The weather is rainy today in ${weather.location}.`;
  }
}

// Instantiate and run the agent
(async () => {
  const response = await new GreetingAgent().run(
    'Hello, my name is Alice. I am from San Francisco.',
  );
  console.log(response);
})();

What just happened?

1

Tools are methods The agent has two tools: getCurrentTime() and getWeatherInfo(). Both are annotated with @tool, which includes a brief description of what each tool does. This annotation makes the tools accessible for the agent to call whenever needed.

2

Structured input

Tools can only take a single parameter, which must be an object with a defined schema. In this example, getWeatherInfo() expects a parameter of type WeatherParams, and its schema is defined using the @schema annotation.

3

Dynamic calling The agent calls getCurrentTime() and getWeatherInfo() as needed, preparing any required parameters at runtime. These tools let the agent access external data, like the current time or weather, which it uses to create a more meaningful response.

By combining tools with the agent’s LLM, you can make your agent smarter, allowing it to perform actions, fetch external data, and respond in a more meaningful way.

Last updated