OpenAI compatible Providers

You can use the OpenAI Provider to use language model providers that implement the OpenAI API. We provide detailed documentation for the following OpenAI compatible providers:

The general setup and provider instance creation is the same for all OpenAI compatible providers.

Setup

The OpenAI provider is available via the @ai-sdk/openai module. You can install it with:

pnpm
npm
yarn
pnpm add @ai-sdk/openai

Provider Instance

To use an OpenAI compatible provider, you can create a custom provider instance with the createOpenAI function from @ai-sdk/openai:

import { createOpenAI } from '@ai-sdk/openai';
const provider = createOpenAI({
name: 'provider-name',
apiKey: process.env.PROVIDER_API_KEY ?? '',
baseURL: 'https://api.provider.com/v1',
});

Language Models

You can create provider models using a provider instance. The first argument is the model id, e.g. model-id.

const model = provider('model-id');

Example

You can use provider language models to generate text with the generateText function:

import { createOpenAI } from '@ai-sdk/openai'
import { generateText } from 'ai'
const provider = createOpenAI({
name: 'provider-name',
apiKey: process.env.PROVIDER_API_KEY ?? '',
baseURL: 'https://api.provider.com/v1'
})
const { text } = await generateText({
model: provider('model-id')
prompt: 'Write a vegetarian lasagna recipe for 4 people.'
})