ai-sdk-zhipu
v0.0.2
Published
Zhipu AI provider for Vercel AI SDK with full V3 specification support
Maintainers
Readme
ai-sdk-zhipu
Zhipu AI (智谱AI) provider for Vercel AI SDK with full V3 specification support.
Features
- ✅ AI SDK V3 Compatible - Fully implements the Language Model Specification V3
- ✅ Compatibility - Supports
aiv5-v6,@ai-sdk/providerv3.x,@ai-sdk/openai-compatiblev2.x - ✅ OpenAI-Compatible API - Uses Zhipu AI's OpenAI-compatible API format
- ✅ All Latest Models - Supports GLM-4.7, GLM-4.6, GLM-4.5, GLM-4-Flash, and more
- ✅ Streaming Support - Full support for streaming responses
- ✅ Multi-modal - Supports vision models (GLM-4V series)
- ✅ Tool Calling - Supports function/tool calling
- ✅ TypeScript - Written in TypeScript with full type definitions
Installation
npm install ai-sdk-zhipu
# or
pnpm add ai-sdk-zhipu
# or
yarn add ai-sdk-zhipu
# or
bun add ai-sdk-zhipuSetup
- Get your API key from open.bigmodel.cn
- Set the
ZHIPU_API_KEYenvironment variable:
export ZHIPU_API_KEY=your-api-key-hereOr create a .env file:
ZHIPU_API_KEY=your-api-key-hereQuick Start
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
Streaming
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);
}Multi-turn Conversations
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);Custom Configuration
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!',
});Vision Models (Multi-modal)
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);Tool Calling
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
Text Models (文本模型)
| 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 |
Vision Models (视觉模型)
| 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.
Full Model ID List
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
Provider Settings
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',
},
});Model Settings
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
createZhipu(settings?)
Creates a Zhipu AI provider instance.
Parameters:
settings.baseURL(string) - Base URL for the APIsettings.apiKey(string) - API keysettings.headers(Record<string, string>) - Custom headers
Returns: Zhipu AI provider instance
zhipu(modelId, settings?)
Default provider instance.
Parameters:
modelId(string) - Model ID (e.g.,'glm-4.7')settings(object) - Model-specific settings
Documentation
- Zhipu AI Official Documentation
- Zhipu AI Model Overview
- Zhipu AI API Reference
- Vercel AI SDK Documentation
- Writing a Custom Provider
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
Acknowledgments
- Built with Vercel AI SDK
- Powered by Zhipu AI
- Based on the Language Model Specification V3
Support
- 📧 Email: [email protected]
- 📖 Documentation: https://docs.bigmodel.cn/
- 💬 Community: GitHub Issues
Note: This is an unofficial community provider. Zhipu AI is a trademark of Beijing Zhipu Huazhang Technology Co., Ltd.
