Zhipu AI provider for Vercel AI SDK with full V3 specification support
npm install ai-sdk-zhipuai v5-v6, @ai-sdk/provider v3.x, @ai-sdk/openai-compatible v2.xbash
npm install ai-sdk-zhipu
or
pnpm add ai-sdk-zhipu
or
yarn add ai-sdk-zhipu
or
bun add ai-sdk-zhipu
`
Setup
1. Get your API key from open.bigmodel.cn
2. Set the ZHIPU_API_KEY environment variable:
`bash
export ZHIPU_API_KEY=your-api-key-here
`
Or create a .env file:
`env
ZHIPU_API_KEY=your-api-key-here
`
Quick Start
`typescript
import { generateText } from 'ai';
import { zhipu } from 'ai-sdk-zhipu';
const result = await generateText({
model: zhipu('glm-4.7'),
prompt: 'Why is the sky blue?',
});
console.log(result.text);
`
Usage Examples
$3
`typescript
import { streamText } from 'ai';
import { zhipu } from 'ai-sdk-zhipu';
const result = await streamText({
model: zhipu('glm-4.7'),
prompt: 'Write a short story about a robot.',
});
for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}
`
$3
`typescript
import { generateText } from 'ai';
import { zhipu } from 'ai-sdk-zhipu';
const result = await generateText({
model: zhipu('glm-4.7'),
messages: [
{ role: 'system', content: 'You are a helpful assistant.' },
{ role: 'user', content: 'What is the capital of France?' },
{ role: 'assistant', content: 'The capital of France is Paris.' },
{ role: 'user', content: 'And what about Germany?' },
],
});
console.log(result.text);
`
$3
`typescript
import { createZhipu } from 'ai-sdk-zhipu';
const zhipu = createZhipu({
baseURL: 'https://open.bigmodel.cn/api/paas/v4',
apiKey: process.env.ZHIPU_API_KEY,
});
const result = await generateText({
model: zhipu('glm-4.7'),
prompt: 'Hello, world!',
});
`
$3
`typescript
import { generateText } from 'ai';
import { zhipu } from 'ai-sdk-zhipu';
const result = await generateText({
model: zhipu('glm-4.6v'), // Vision model
messages: [
{
role: 'user',
content: [
{ type: 'text', text: 'What do you see in this image?' },
{ type: 'image', image: 'https://example.com/image.jpg' },
],
},
],
});
console.log(result.text);
`
$3
`typescript
import { generateText } from 'ai';
import { zhipu } from 'ai-sdk-zhipu';
const result = await generateText({
model: zhipu('glm-4.7'),
tools: {
weather: {
description: 'Get the weather for a location',
parameters: {
type: 'object',
properties: {
location: {
type: 'string',
description: 'The city and state',
},
},
required: ['location'],
},
},
},
prompt: 'What is the weather in San Francisco?',
});
console.log(result.toolCalls);
`
Available Models
$3
| Model ID | Description | Context | Max Output |
|----------|-------------|---------|------------|
| glm-4.7 | Latest flagship model | 200K | 128K |
| glm-4.6 | High-performance model | 200K | 128K |
| glm-4.5 | Excellent performance | 128K | 96K |
| glm-4-plus | Excellent performance | 128K | 4K |
| glm-4-air-250414 | High cost-performance | 128K | 16K |
| glm-4-long | Ultra-long input | 1M | 4K |
| glm-4-flash-250414 | Free model | 128K | 16K |
| glm-4.5-flash | Free model | 128K | 96K |
$3
| Model ID | Description | Context | Max Output |
|----------|-------------|---------|------------|
| glm-4.6v | Flagship vision reasoning | 128K | 32K |
| glm-4.5v | Vision reasoning | 64K | 16K |
| glm-4.6v-flash | Free vision model | 128K | 32K |
| glm-4v-flash | Free vision model | 16K | 1K |
For the complete model list, see the official documentation.$3
The full typed list is maintained in
src/zhipu-chat-setting.ts and mirrors the
official model overview (including less common and legacy IDs such as
glm-4, glm-4-airx, glm-4-flashx-250414, glm-4.1v-thinking, and glm-4-0520).
Configuration
$3
`typescript
import { createZhipu } from 'ai-sdk-zhipu';
const zhipu = createZhipu({
baseURL: 'https://open.bigmodel.cn/api/paas/v4', // optional
apiKey: 'your-api-key', // optional, defaults to ZHIPU_API_KEY env var
headers: { // optional custom headers
'X-Custom-Header': 'value',
},
});
`
$3
`typescript
await generateText({
model: zhipu('glm-4.7', {
temperature: 0.7,
maxTokens: 1000,
topP: 0.9,
topK: 40,
stopSequences: ['END'],
presencePenalty: 0.5,
frequencyPenalty: 0.5,
}),
prompt: 'Your prompt here',
});
`
API Reference
$3
Creates a Zhipu AI provider instance.
Parameters:
- settings.baseURL (string) - Base URL for the API
- settings.apiKey (string) - API key
- settings.headers (Record) - Custom headers
Returns: Zhipu AI provider instance
$3
Default provider instance.
Parameters:
- modelId (string) - Model ID (e.g., 'glm-4.7')
- settings` (object) - Model-specific settings