-
Notifications
You must be signed in to change notification settings - Fork 67
fix(sdk): support vercel AI SDK tool calling + structured outputs #675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e3b77d6
fix(sdk): support vercel AI SDK tool calling + structured outputs
nirga 9bf6de1
refactor(ai-sdk): clean up transformations code and fix lint issues
nirga 35f9f34
Merge remote-tracking branch 'origin/main' into vercel-ai-sdk-fix
nirga deae8d6
fix: update vendor mapping to handle azure-openai providers correctly
nirga f24d54b
fix: lint
nirga a8adf1d
fix: azure vendor
nirga fb2f250
chore: remove un-needed package.json script
nirga 4ffe0ca
fix: prettier
nirga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import * as traceloop from "@traceloop/node-server-sdk"; | ||
| import { openai } from "@ai-sdk/openai"; | ||
| import { generateObject } from "ai"; | ||
| import { z } from "zod"; | ||
|
|
||
| import "dotenv/config"; | ||
|
|
||
| traceloop.initialize({ | ||
| appName: "sample_vercel_ai_object", | ||
| disableBatch: true, | ||
| }); | ||
|
|
||
| const PersonSchema = z.object({ | ||
| name: z.string(), | ||
| age: z.number(), | ||
| occupation: z.string(), | ||
| skills: z.array(z.string()), | ||
| location: z.object({ | ||
| city: z.string(), | ||
| country: z.string(), | ||
| }), | ||
| }); | ||
|
|
||
| async function generatePersonProfile(description: string) { | ||
| return await traceloop.withWorkflow( | ||
| { name: "generate_person_profile" }, | ||
| async () => { | ||
| const { object } = await generateObject({ | ||
| model: openai("gpt-4o"), | ||
| schema: PersonSchema, | ||
| prompt: `Based on this description, generate a detailed person profile: ${description}`, | ||
| experimental_telemetry: { isEnabled: true }, | ||
| }); | ||
|
|
||
| return object; | ||
| }, | ||
| { description }, | ||
| ); | ||
| } | ||
|
|
||
| async function main() { | ||
| const profile = await generatePersonProfile( | ||
| "A talented software engineer from Paris who loves working with AI and machine learning, speaks multiple languages, and enjoys traveling.", | ||
| ); | ||
|
|
||
| console.log("Generated person profile:", JSON.stringify(profile, null, 2)); | ||
| } | ||
|
|
||
| main().catch(console.error); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import * as traceloop from "@traceloop/node-server-sdk"; | ||
| import { openai } from "@ai-sdk/openai"; | ||
| import { generateText, tool } from "ai"; | ||
| import { z } from "zod"; | ||
|
|
||
| import "dotenv/config"; | ||
|
|
||
| traceloop.initialize({ | ||
| appName: "sample_vercel_ai_tools", | ||
| disableBatch: true, | ||
| }); | ||
|
|
||
| // Define tools | ||
| const getWeather = tool({ | ||
| description: "Get the current weather for a specified location", | ||
| parameters: z.object({ | ||
| location: z.string().describe("The location to get the weather for"), | ||
| }), | ||
| execute: async ({ location }) => { | ||
| console.log(`🔧 Tool 'getWeather' called with location: ${location}`); | ||
|
|
||
| // Simulate API call delay | ||
| await new Promise((resolve) => setTimeout(resolve, 100)); | ||
|
|
||
| // Simulate weather data | ||
| const weatherData = { | ||
| location, | ||
| temperature: Math.floor(Math.random() * 30) + 60, // 60-90°F | ||
| condition: ["Sunny", "Cloudy", "Rainy", "Snowy"][ | ||
| Math.floor(Math.random() * 4) | ||
| ], | ||
| humidity: Math.floor(Math.random() * 40) + 40, // 40-80% | ||
| }; | ||
|
|
||
| console.log(`🌤️ Weather data retrieved for ${location}:`, weatherData); | ||
| return weatherData; | ||
| }, | ||
| }); | ||
|
|
||
| const calculateDistance = tool({ | ||
| description: "Calculate the distance between two cities", | ||
| parameters: z.object({ | ||
| fromCity: z.string().describe("The starting city"), | ||
| toCity: z.string().describe("The destination city"), | ||
| }), | ||
| execute: async ({ fromCity, toCity }) => { | ||
| console.log( | ||
| `🔧 Tool 'calculateDistance' called from ${fromCity} to ${toCity}`, | ||
| ); | ||
|
|
||
| // Simulate API call delay | ||
| await new Promise((resolve) => setTimeout(resolve, 150)); | ||
|
|
||
| // Simulate distance calculation | ||
| const distance = Math.floor(Math.random() * 2000) + 100; // 100-2100 miles | ||
| const result = { | ||
| from: fromCity, | ||
| to: toCity, | ||
| distance: `${distance} miles`, | ||
| drivingTime: `${Math.floor(distance / 60)} hours`, | ||
| }; | ||
|
|
||
| console.log(`🗺️ Distance calculated:`, result); | ||
| return result; | ||
| }, | ||
| }); | ||
|
|
||
| const searchRestaurants = tool({ | ||
| description: "Search for restaurants in a specific city", | ||
| parameters: z.object({ | ||
| city: z.string().describe("The city to search for restaurants"), | ||
| cuisine: z | ||
| .string() | ||
| .optional() | ||
| .describe("Optional cuisine type (e.g., Italian, Mexican)"), | ||
| }), | ||
| execute: async ({ city, cuisine }) => { | ||
| console.log( | ||
| `🔧 Tool 'searchRestaurants' called for ${city}${cuisine ? ` (${cuisine} cuisine)` : ""}`, | ||
| ); | ||
|
|
||
| // Simulate API call delay | ||
| await new Promise((resolve) => setTimeout(resolve, 200)); | ||
|
|
||
| // Simulate restaurant data | ||
| const restaurantNames = [ | ||
| "The Golden Fork", | ||
| "Sunset Bistro", | ||
| "Ocean View", | ||
| "Mountain Top", | ||
| "Urban Kitchen", | ||
| "Garden Cafe", | ||
| "Heritage House", | ||
| "Modern Table", | ||
| ]; | ||
|
|
||
| const restaurants = Array.from({ length: 3 }, (_, i) => ({ | ||
| name: restaurantNames[Math.floor(Math.random() * restaurantNames.length)], | ||
| cuisine: | ||
| cuisine || | ||
| ["Italian", "Mexican", "Asian", "American"][ | ||
| Math.floor(Math.random() * 4) | ||
| ], | ||
| rating: (Math.random() * 2 + 3).toFixed(1), // 3.0-5.0 rating | ||
| priceRange: ["$", "$$", "$$$"][Math.floor(Math.random() * 3)], | ||
| })); | ||
|
|
||
| console.log( | ||
| `🍽️ Found ${restaurants.length} restaurants in ${city}:`, | ||
| restaurants, | ||
| ); | ||
| return { city, restaurants }; | ||
| }, | ||
| }); | ||
|
|
||
| async function planTrip(destination: string) { | ||
| return await traceloop.withWorkflow( | ||
| { name: "plan_trip" }, | ||
| async () => { | ||
| console.log(`\n🌟 Planning a trip to ${destination}...\n`); | ||
|
|
||
| const result = await generateText({ | ||
| model: openai("gpt-4o"), | ||
| prompt: `Help me plan a trip to ${destination}. I'd like to know: | ||
| 1. What's the weather like there? | ||
| 2. Find some good restaurants to try | ||
| 3. If I'm traveling from New York, how far is it? | ||
|
|
||
| Please use the available tools to get current information and provide a comprehensive travel guide.`, | ||
| tools: { | ||
| getWeather, | ||
| calculateDistance, | ||
| searchRestaurants, | ||
| }, | ||
| maxSteps: 5, // Allow multiple tool calls | ||
| experimental_telemetry: { isEnabled: true }, | ||
| }); | ||
|
|
||
| return result.text; | ||
| }, | ||
| { destination }, | ||
| ); | ||
| } | ||
|
|
||
| async function main() { | ||
| try { | ||
| const travelGuide = await planTrip("San Francisco"); | ||
|
|
||
| console.log("\n" + "=".repeat(80)); | ||
| console.log("🗺️ TRAVEL GUIDE"); | ||
| console.log("=".repeat(80)); | ||
| console.log(travelGuide); | ||
| console.log("=".repeat(80)); | ||
| } catch (error) { | ||
| console.error("❌ Error planning trip:", error); | ||
| } | ||
| } | ||
|
|
||
| main().catch(console.error); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.