Skip to main content

Z.AI

还没翻译译自英文(ai-sdk.dev)本页 33 段都还没译

The Z.AI provider gives you access to GLM language and vision models through the Z.AI API. You can use Z.AI models through AI Gateway (recommended) or connect directly with @ai-sdk/zai.

import { generateText } from 'ai';

const { text } = await generateText({
  model: 'anthropic/claude-sonnet-5.5',
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

console.log(text);

Using AI Gateway

The AI SDK uses AI Gateway by default for model strings. Gateway provides access to models across providers through a single API key, with usage and cost tracking. Install ai:

pnpm add ai
npm install ai
yarn add ai
bun add ai

Set an AI Gateway API key in your environment:

export AI_GATEWAY_API_KEY=your_api_key_here

Vercel deployments can also use OIDC authentication. See the AI Gateway provider documentation for configuration and the model catalog for available models.

Gateway feature and provider-option support depends on the model and the provider serving the request. See Gateway provider options.

Using @ai-sdk/zai

For direct access to Z.AI, install ai and @ai-sdk/zai:

pnpm add ai @ai-sdk/zai
npm install ai @ai-sdk/zai
yarn add ai @ai-sdk/zai
bun add ai @ai-sdk/zai

Set your Z.AI API credential in the ZAI_API_KEY environment variable:

export ZAI_API_KEY=your_api_key_here

AI Gateway does not expose every API in @ai-sdk/zai. Use the direct provider for provider-specific APIs or custom connection settings.

Note

The following sections document the direct @ai-sdk/zai provider, including its model factories and settings. Gateway model and feature availability can differ.

API keys can be created in the Z.AI API key console.

Provider Instance

Import the default provider instance zai from @ai-sdk/zai:

import { zai } from '@ai-sdk/zai';

For custom configuration, use createZai:

import { createZai } from '@ai-sdk/zai';

const zai = createZai({
  apiKey: process.env.ZAI_API_KEY,
  baseURL: 'https://api.z.ai/api/paas/v4',
});

The provider accepts these optional settings:

  • apiKey string

    API key sent in the Authorization header. It defaults to the ZAI_API_KEY environment variable.

  • baseURL string

    URL prefix for API requests. It defaults to https://api.z.ai/api/paas/v4.

  • headers Record<string, string>

    Additional request headers.

  • fetch FetchFunction

    Custom fetch implementation, for example to intercept requests in middleware.

Language Models

Create a language model by passing its model id to the provider:

import { zai } from '@ai-sdk/zai';
import { generateText } from 'ai';

const { text } = await generateText({
  model: zai('glm-5.3'),
  prompt: 'Explain quantum entanglement in simple terms.',
});

Current model families include GLM 5, GLM 4.7, GLM 4.6, GLM 4.5, and the GLM vision models. Model availability changes over time; see the Z.AI model documentation for the current catalog.

The provider supports:

  • text generation and streaming
  • reasoning output and reasoning history
  • function calling, including incremental tool-call streaming
  • JSON object output
  • URL-based image and video inputs on compatible vision models

Provider Options

Z.AI-specific options can be passed through providerOptions.zai:

import { zai } from '@ai-sdk/zai';
import { generateText } from 'ai';

const result = await generateText({
  model: zai('glm-5.3'),
  prompt: 'Compare two approaches to implementing a rate limiter.',
  providerOptions: {
    zai: {
      thinking: { type: 'enabled', clearThinking: true },
      reasoningEffort: 'high',
      requestId: 'request-123456',
      userId: 'user-123456',
    },
  },
});

The following options are available:

  • doSample boolean

    Enables sampling. When disabled, temperature and topP do not take effect.

  • thinking object

    Controls thinking with type: 'enabled' | 'disabled'. Set clearThinking to false to retain reasoning from previous assistant messages.

  • reasoningEffort 'none' | 'minimal' | 'low' | 'medium' | 'high' | 'xhigh' | 'max'

    Controls reasoning effort on supported models.

  • toolStream boolean

    Enables incremental function-call argument streaming on supported models.

  • requestId string

    A caller-provided request id between 6 and 64 characters.

  • userId string

    A non-sensitive end-user id between 6 and 128 characters.

Streaming Tool Calls

Enable toolStream when you want supported GLM models to stream function-call arguments incrementally:

import { zai } from '@ai-sdk/zai';
import { streamText, tool } from 'ai';
import { z } from 'zod';

const result = streamText({
  model: zai('glm-5.3'),
  prompt: 'What is the weather in San Francisco?',
  tools: {
    weather: tool({
      description: 'Get the weather for a city',
      inputSchema: z.object({ city: z.string() }),
    }),
  },
  providerOptions: {
    zai: { toolStream: true },
  },
});

for await (const part of result.fullStream) {
  console.log(part);
}