Connect Vercel AI SDK
TypeScript toolkit for LLM apps, connected through its OpenAI-compatible provider.
Use cases
Good for TypeScript server code such as Next.js or Node.js. @ai-sdk/openai-compatible calls /chat/completions over the OpenAI-compatible protocol, so generateText, streamText and tool calling all work.
Tips
Create the provider only in server routes or backend code, keep the key in an environment variable, and never ship it to the browser.
If it cannot connect
- Use
https://modelportal.ai/v1as the API host, without/chat/completions. - The key starts with
mp-(older keys:key_) and is not paused or revoked;invalid_api_keymeans the key cannot be used. - The model ID matches the catalog and is in the key's allowed models.
insufficient_balancemeans the balance is too low; see common errors for more.
Setup
- Install the dependencies: npm install ai @ai-sdk/openai-compatible.
- Create a provider with createOpenAICompatible, using the API host below as baseURL and your own key as apiKey.
- Pass the provider's model to generateText or streamText; keep the key in server-side code.
Settings
- API host:
https://modelportal.ai/v1 - API key:
YOUR_API_KEY - Model:
MODEL_ID
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
import { generateText } from "ai";
const modelportal = createOpenAICompatible({
name: "modelportal",
baseURL: "https://modelportal.ai/v1",
apiKey: "YOUR_API_KEY",
});
const { text } = await generateText({
model: modelportal("MODEL_ID"),
prompt: "你好",
});
console.log(text);
FAQ
- Why not @ai-sdk/openai?
- Recent @ai-sdk/openai versions default to the Responses API, so the openai-compatible package is the more direct fit. The platform supports Responses too, but not stateful conversations.