import { openai } from "@llamaindex/openai";import { agent } from "@llamaindex/workflow";import { tool } from "llamaindex";import { z } from "zod";// Define a weather toolconst getWeatherTool = tool({ name: "get_weather", description: "Get the current weather for a location", parameters: z.object({ address: z.string().describe("The address"), }), execute: ({ address }) => `${address} is in a sunny location!`,});async function main() { // Create agent with tool const weatherAgent = agent({ llm: openai({ model: "gpt-4o", }), tools: [getWeatherTool], verbose: false, }); // First query const result = await weatherAgent.run( "What's the weather like in San Francisco?", ); console.log(JSON.stringify(result, null, 2)); // Reuse state from previous run const caResult = await weatherAgent.run("Compare it with California?", { state: result.data.state, }); console.log(JSON.stringify(caResult, null, 2)); console.log("assistant message:", caResult.data.message);}main().catch((error) => { console.error("Error:", error);});
import { tool } from "llamaindex";import { z } from "zod";const getWeatherTool = tool({ name: "get_weather", description: "Get the current weather for a location", parameters: z.object({ address: z.string().describe("The address"), }), execute: ({ address }) => { // Your tool logic here return `${address} is in a sunny location!`; },});
Key components:
name - Unique identifier for the tool
description - Helps the LLM understand when to use the tool