Created
February 8, 2025 22:42
-
-
Save eusoujuninho/44d231d6e8cc01e8ca57cd21760978dc to your computer and use it in GitHub Desktop.
Multi-Tool Assistant with Vercel AI SDK
This file contains 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
import { z } from 'zod'; | |
import { generateText, tool } from 'ai'; | |
// A set of tools to answer a variety of questions | |
const toolSet = { | |
weather: tool({ | |
description: 'Get the current weather in a specified location.', | |
parameters: z.object({ | |
location: z.string().describe('Location for which to fetch the weather'), | |
}), | |
execute: async ({ location }) => { | |
// Simulating a weather response | |
const temperature = 72 + Math.floor(Math.random() * 21) - 10; | |
return { location, temperature }; | |
}, | |
}), | |
time: tool({ | |
description: 'Get the current time in a specified time zone.', | |
parameters: z.object({ | |
timezone: z.string().describe('Time zone in which to get the current time'), | |
}), | |
execute: async ({ timezone }) => { | |
// Simulating a time response | |
const currentTime = new Date().toLocaleString('en-US', { | |
timeZone: timezone, | |
}); | |
return { timezone, currentTime }; | |
}, | |
}), | |
math: tool({ | |
description: 'Perform simple arithmetic calculations.', | |
parameters: z.object({ | |
expression: z.string().describe('Mathematical expression to calculate'), | |
}), | |
execute: async ({ expression }) => { | |
// Basic math evaluation (using eval, but use safely in a real scenario) | |
try { | |
const result = eval(expression); // It's important to sanitize user input | |
return { expression, result }; | |
} catch (error) { | |
throw new Error('Invalid expression'); | |
} | |
}, | |
}), | |
translator: tool({ | |
description: 'Translate text between languages.', | |
parameters: z.object({ | |
text: z.string().describe('Text to translate'), | |
fromLanguage: z.string().describe('Language to translate from'), | |
toLanguage: z.string().describe('Language to translate to'), | |
}), | |
execute: async ({ text, fromLanguage, toLanguage }) => { | |
// Simulating translation | |
const translatedText = `${text} (translated from ${fromLanguage} to ${toLanguage})`; | |
return { text, translatedText }; | |
}, | |
}), | |
fact: tool({ | |
description: 'Fetch a random fact.', | |
parameters: z.object({}), | |
execute: async () => { | |
// Simulating fetching a random fact | |
const randomFact = "Did you know? Honey never spoils!"; | |
return { randomFact }; | |
}, | |
}), | |
}; | |
// Example of using multiple tools | |
const { text, steps } = await generateText({ | |
model: openai('gpt-4-turbo'), | |
tools: toolSet, | |
maxSteps: 5, // Allowing multi-step processing | |
prompt: 'Tell me the weather in San Francisco, current time in Tokyo, a math fact, translate "hello" to French, and a random fact.', | |
}); | |
console.log('Generated Text:', text); | |
console.log('Steps:', steps); | |
// Access intermediate tool calls and results | |
const allToolCalls = steps.flatMap(step => step.toolCalls); | |
console.log('All Tool Calls:', allToolCalls); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment