|
| 1 | +import { Agent, VoltAgent, createTool } from "@voltagent/core"; |
| 2 | +import { createPinoLogger } from "@voltagent/logger"; |
| 3 | +import { honoServer } from "@voltagent/server-hono"; |
| 4 | +import { createOllama } from "ollama-ai-provider-v2"; |
| 5 | +import { z } from "zod"; |
| 6 | + |
| 7 | +const logger = createPinoLogger({ |
| 8 | + name: "with-ollama", |
| 9 | + level: "info", |
| 10 | +}); |
| 11 | + |
| 12 | +const ollama = createOllama({ |
| 13 | + baseURL: process.env.OLLAMA_HOST ?? "http://localhost:11434/api", |
| 14 | +}); |
| 15 | + |
| 16 | +const getCurrentWeather = createTool({ |
| 17 | + name: "get_current_weather", |
| 18 | + description: "Fetch the current weather conditions for a given city", |
| 19 | + parameters: z.object({ |
| 20 | + location: z.string().describe("City or location to inspect"), |
| 21 | + unit: z.enum(["celsius", "fahrenheit"]).default("celsius"), |
| 22 | + }), |
| 23 | + execute: async ({ location, unit }) => { |
| 24 | + return { |
| 25 | + location, |
| 26 | + temperature: unit === "fahrenheit" ? 72 : 22, |
| 27 | + condition: "Partly cloudy with light winds", |
| 28 | + unit, |
| 29 | + }; |
| 30 | + }, |
| 31 | +}); |
| 32 | + |
| 33 | +const agent = new Agent({ |
| 34 | + name: "ollama-tool-agent", |
| 35 | + instructions: "You are a helpful assistant", |
| 36 | + model: ollama("llama3.2:latest"), |
| 37 | + tools: [getCurrentWeather], |
| 38 | + logger, |
| 39 | +}); |
| 40 | + |
| 41 | +new VoltAgent({ |
| 42 | + agents: { |
| 43 | + agent, |
| 44 | + }, |
| 45 | + logger, |
| 46 | + server: honoServer(), |
| 47 | +}); |
0 commit comments