|
| 1 | +import * as traceloop from "@traceloop/node-server-sdk"; |
| 2 | +import { openai } from "@ai-sdk/openai"; |
| 3 | +import { generateText, tool } from "ai"; |
| 4 | +import { z } from "zod"; |
| 5 | + |
| 6 | +import "dotenv/config"; |
| 7 | + |
| 8 | +traceloop.initialize({ |
| 9 | + appName: "sample_vercel_ai_tools", |
| 10 | + disableBatch: true, |
| 11 | +}); |
| 12 | + |
| 13 | +// Define tools |
| 14 | +const getWeather = tool({ |
| 15 | + description: "Get the current weather for a specified location", |
| 16 | + parameters: z.object({ |
| 17 | + location: z.string().describe("The location to get the weather for"), |
| 18 | + }), |
| 19 | + execute: async ({ location }) => { |
| 20 | + console.log(`🔧 Tool 'getWeather' called with location: ${location}`); |
| 21 | + |
| 22 | + // Simulate API call delay |
| 23 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 24 | + |
| 25 | + // Simulate weather data |
| 26 | + const weatherData = { |
| 27 | + location, |
| 28 | + temperature: Math.floor(Math.random() * 30) + 60, // 60-90°F |
| 29 | + condition: ["Sunny", "Cloudy", "Rainy", "Snowy"][ |
| 30 | + Math.floor(Math.random() * 4) |
| 31 | + ], |
| 32 | + humidity: Math.floor(Math.random() * 40) + 40, // 40-80% |
| 33 | + }; |
| 34 | + |
| 35 | + console.log(`🌤️ Weather data retrieved for ${location}:`, weatherData); |
| 36 | + return weatherData; |
| 37 | + }, |
| 38 | +}); |
| 39 | + |
| 40 | +const calculateDistance = tool({ |
| 41 | + description: "Calculate the distance between two cities", |
| 42 | + parameters: z.object({ |
| 43 | + fromCity: z.string().describe("The starting city"), |
| 44 | + toCity: z.string().describe("The destination city"), |
| 45 | + }), |
| 46 | + execute: async ({ fromCity, toCity }) => { |
| 47 | + console.log( |
| 48 | + `🔧 Tool 'calculateDistance' called from ${fromCity} to ${toCity}`, |
| 49 | + ); |
| 50 | + |
| 51 | + // Simulate API call delay |
| 52 | + await new Promise((resolve) => setTimeout(resolve, 150)); |
| 53 | + |
| 54 | + // Simulate distance calculation |
| 55 | + const distance = Math.floor(Math.random() * 2000) + 100; // 100-2100 miles |
| 56 | + const result = { |
| 57 | + from: fromCity, |
| 58 | + to: toCity, |
| 59 | + distance: `${distance} miles`, |
| 60 | + drivingTime: `${Math.floor(distance / 60)} hours`, |
| 61 | + }; |
| 62 | + |
| 63 | + console.log(`🗺️ Distance calculated:`, result); |
| 64 | + return result; |
| 65 | + }, |
| 66 | +}); |
| 67 | + |
| 68 | +const searchRestaurants = tool({ |
| 69 | + description: "Search for restaurants in a specific city", |
| 70 | + parameters: z.object({ |
| 71 | + city: z.string().describe("The city to search for restaurants"), |
| 72 | + cuisine: z |
| 73 | + .string() |
| 74 | + .optional() |
| 75 | + .describe("Optional cuisine type (e.g., Italian, Mexican)"), |
| 76 | + }), |
| 77 | + execute: async ({ city, cuisine }) => { |
| 78 | + console.log( |
| 79 | + `🔧 Tool 'searchRestaurants' called for ${city}${cuisine ? ` (${cuisine} cuisine)` : ""}`, |
| 80 | + ); |
| 81 | + |
| 82 | + // Simulate API call delay |
| 83 | + await new Promise((resolve) => setTimeout(resolve, 200)); |
| 84 | + |
| 85 | + // Simulate restaurant data |
| 86 | + const restaurantNames = [ |
| 87 | + "The Golden Fork", |
| 88 | + "Sunset Bistro", |
| 89 | + "Ocean View", |
| 90 | + "Mountain Top", |
| 91 | + "Urban Kitchen", |
| 92 | + "Garden Cafe", |
| 93 | + "Heritage House", |
| 94 | + "Modern Table", |
| 95 | + ]; |
| 96 | + |
| 97 | + const restaurants = Array.from({ length: 3 }, (_, i) => ({ |
| 98 | + name: restaurantNames[Math.floor(Math.random() * restaurantNames.length)], |
| 99 | + cuisine: |
| 100 | + cuisine || |
| 101 | + ["Italian", "Mexican", "Asian", "American"][ |
| 102 | + Math.floor(Math.random() * 4) |
| 103 | + ], |
| 104 | + rating: (Math.random() * 2 + 3).toFixed(1), // 3.0-5.0 rating |
| 105 | + priceRange: ["$", "$$", "$$$"][Math.floor(Math.random() * 3)], |
| 106 | + })); |
| 107 | + |
| 108 | + console.log( |
| 109 | + `🍽️ Found ${restaurants.length} restaurants in ${city}:`, |
| 110 | + restaurants, |
| 111 | + ); |
| 112 | + return { city, restaurants }; |
| 113 | + }, |
| 114 | +}); |
| 115 | + |
| 116 | +async function planTrip(destination: string) { |
| 117 | + return await traceloop.withWorkflow( |
| 118 | + { name: "plan_trip" }, |
| 119 | + async () => { |
| 120 | + console.log(`\n🌟 Planning a trip to ${destination}...\n`); |
| 121 | + |
| 122 | + const result = await generateText({ |
| 123 | + model: openai("gpt-4o"), |
| 124 | + prompt: `Help me plan a trip to ${destination}. I'd like to know: |
| 125 | +1. What's the weather like there? |
| 126 | +2. Find some good restaurants to try |
| 127 | +3. If I'm traveling from New York, how far is it? |
| 128 | +
|
| 129 | +Please use the available tools to get current information and provide a comprehensive travel guide.`, |
| 130 | + tools: { |
| 131 | + getWeather, |
| 132 | + calculateDistance, |
| 133 | + searchRestaurants, |
| 134 | + }, |
| 135 | + maxSteps: 5, // Allow multiple tool calls |
| 136 | + experimental_telemetry: { isEnabled: true }, |
| 137 | + }); |
| 138 | + |
| 139 | + return result.text; |
| 140 | + }, |
| 141 | + { destination }, |
| 142 | + ); |
| 143 | +} |
| 144 | + |
| 145 | +async function main() { |
| 146 | + try { |
| 147 | + const travelGuide = await planTrip("San Francisco"); |
| 148 | + |
| 149 | + console.log("\n" + "=".repeat(80)); |
| 150 | + console.log("🗺️ TRAVEL GUIDE"); |
| 151 | + console.log("=".repeat(80)); |
| 152 | + console.log(travelGuide); |
| 153 | + console.log("=".repeat(80)); |
| 154 | + } catch (error) { |
| 155 | + console.error("❌ Error planning trip:", error); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +main().catch(console.error); |
0 commit comments