Integrate Portkey with Vercel AI SDK for production-ready and reliable AI apps
Portkey natively integrates with the Vercel AI SDK to make your apps production-ready and reliable. Just import Portkey’s Vercel package and use it as a provider in your Vercel AI app to enable all of Portkey features:
Full-stack observability and tracing for all requests
Interoperability across 250+ LLMS
Built-in 50+ SOTA guardrails
Simple & semantic caching to save costs & time
Route requests conditionally and make them robust with fallbacks, load-balancing, automatic retries, and more
Portkey provider works with all of Vercel functions generateText & streamText.
Here’s how to use them with Portkey:
Copy
Ask AI
import { createPortkey } from '@portkey-ai/vercel-provider';import { generateText } from 'ai';const portkeyConfig = { "provider": "openai", // Choose your provider (e.g., 'anthropic') "api_key": "OPENAI_API_KEY", "override_params": { "model": "gpt-4o" }};const portkey = createPortkey({ apiKey: 'YOUR_PORTKEY_API_KEY', config: portkeyConfig,});const { text } = await generateText({ model: portkey.chatModel(''), // Provide an empty string, we defined the model in the config prompt: 'What is Portkey?',});console.log(text);
Portkey supports chatModel and completionModel to easily handle chatbots or text completions. In the above examples, we used portkey.chatModel for generateText and portkey.completionModel for streamText.
Portkey supports Tool calling with Vercel AI SDK. Here’s how-
Copy
Ask AI
import { z } from 'zod';import { generateText, tool } from 'ai';const result = await generateText({ model: portkey.chatModel('gpt-4-turbo'), tools: { weather: tool({ description: 'Get the weather in a location', parameters: z.object({ location: z.string().describe('The location to get the weather for'), }), execute: async ({ location }) => ({ location, temperature: 72 + Math.floor(Math.random() * 21) - 10, }), }), }, prompt: 'What is the weather in San Francisco?',});
Portkey allows you to easily switch between 250+ AI models by simply changing the model name in your configuration. This flexibility enables you to adapt to the evolving AI landscape without significant code changes.
Portkey’s OpenTelemetry-compliant observability suite gives you complete control over all your requests. And Portkey’s analytics dashboards provide 40+ key insights you’re looking for including cost, tokens, latency, etc. Fast.