Tools
Last updated
import { Tool } from "@ainulabs/ainu";
import { z } from "zod";
const weatherTool = new Tool("getWeather", {
description: "Fetches the weather for a given location.",
parameters: z.object({
location: z.string(),
}),
handler: ({ location }) => `The weather in ${location} is sunny.`,
});const agent = new Agent({
provider,
tools: [weatherTool],
});agent.putTool(weatherTool); // Add a tool
agent.deleteTool("getWeather"); // Remove a tool
const tool = agent.findTool("getWeather"); // Retrieve a toolconst response = await agent.generateText({
prompt: "What's the weather in New York?",
tools: [weatherTool],
});import { Agent, Anthropic, Tool } from "@ainulabs/ainu";
import { z } from "zod";
// Define a provider
const provider = new Anthropic({
apiKey: "your-api-key",
});
// Define a tool
const weatherTool = new Tool("getWeather", {
description: "Fetches the weather for a given location.",
parameters: z.object({
location: z.string(),
}),
handler: ({ location }) => `The weather in ${location} is sunny.`,
});
// Create an agent with the tool
const agent = new Agent({
provider,
tools: [weatherTool],
});
// Use the agent
(async () => {
const response = await agent.generateText({
prompt: "What's the weather in Paris?",
});
console.log(response.data?.text); // Output: "The weather in Paris is sunny."
})();