A language model specification gives provider integrations a common interface. Instead of writing separate text-generation code for each provider’s client and API, an application can pass a provider’s model to the Vercel AI SDK’s generateText function. This reduces duplication and makes switching models a smaller change. section 3.3.1 section 3.3.2
The specification also defines the interface methods needed to add a provider. Provider integrations supply model instances behind that interface, decoupling application code from particular implementations. This resembles the abstract factory pattern, although not every SDK function follows that pattern in its traditional form. section 3.3.2
Listing 3.10 Calling the generateText function with different models
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { anthropic } from '@ai-sdk/anthropic';
// OpenAI example
const openaiResponse = await generateText({.
model: openai('gpt-3.5-turbo'),
prompt: 'What is 7+7 and why? ',
});
// Anthropic example
const anthropicResponse = await generateText({.
model: anthropic('claude-v1'),
prompt: ''What is 7+7 and why?',
});
Compare the two generateText calls: the prompt and function stay the same while the model parameter selects an OpenAI or Anthropic integration. section 3.3.2
Table 3.4 AI providers that implement the language model specification
| Provider |
Package |
| OpenAI |
|
| Anthropic |
|
| Google generative AI |
|
| Google Vertex |
|
| Mistral |
|
The provider-package table identifies available integrations, including OpenAI, Anthropic, Google generative AI, Google Vertex, and Mistral. section 3.3.2
A common model interface does not make every workflow interchangeable. LangChain.js chains lack an adapter for this specification in the described integration, so their results cannot be passed directly through streamUI. Stream the chain with chain.stream(), then push its results into createStreamableUI to update a Next.js interface. section 6.1.2
