Connect Vercel AI SDK

Updated

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

  1. Use https://modelportal.ai/v1 as the API host, without /chat/completions.
  2. The key starts with mp- (older keys: key_) and is not paused or revoked; invalid_api_key means the key cannot be used.
  3. The model ID matches the catalog and is in the key's allowed models.
  4. insufficient_balance means the balance is too low; see common errors for more.

Setup

  1. Install the dependencies: npm install ai @ai-sdk/openai-compatible.
  2. Create a provider with createOpenAICompatible, using the API host below as baseURL and your own key as apiKey.
  3. 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.