ZtoApi 文档

返回首页 Deno Deploy 部署

目录

概述

ZtoApi 是一个高性能的 OpenAI 兼容 API 代理服务器,为 Z.ai 的 GLM 系列模型提供标准化的访问接口。支持流式和非流式响应,提供实时监控面板,并具备企业级的可用性和安全性。

基础URL: http://localhost:9090/v1

注意: 默认端口为9090,可以通过环境变量 PORT 进行修改。

支持的模型

ZtoApi 支持 Z.ai 的多个先进 AI 模型:

模型 ID 模型名称 特性
0727-360B-API GLM-4.5 通用对话、代码生成、思考过程
glm-4.6 GLM-4.6 🚀 增强推理、高级代码生成、深度搜索
glm-4.7 GLM-4.7 🆕 最新推理、更强思考能力、卓越编程
glm-5 GLM-5 🚀 旗舰模型、全方位能力提升
glm-4.5v GLM-4.5V 🎯 全方位多模态:图像、视频、文档、音频
glm-4.6v GLM-4.6V 🚀 增强多模态:高级视觉理解
0727-106B-API GLM-4.5-Air ⚡ 轻量快速、低延迟响应
0808-360B-DR 0808-360B-DR 🔬 深度研究专用、长文本分析
模型说明: 多模态模型(4.5V、4.6V)支持图像、视频、文档和音频内容处理。其他模型专注于文本对话和推理能力。
关于工具调用: ZtoApi 已完整支持 OpenAI 格式的 tools 参数解析和转发,但实际工具调用功能受限于上游 Z.ai API。目前测试显示 /api/v2/chat/completions 端点可能未完全启用工具调用功能,建议使用 reasoning: true 参数启用思考模式以获得类似的推理能力。

身份验证

所有API请求都需要在请求头中包含有效的API密钥进行身份验证:

Authorization: Bearer your-api-key

默认的API密钥为 sk-your-key,可以通过环境变量 DEFAULT_KEY 进行修改。

API端点

获取模型列表

GET /v1/models

获取可用模型列表。

请求参数

{ "object": "list", "data": [ { "id": "GLM-4.5", "object": "model", "created": 1756788845, "owned_by": "z.ai" }, { "id": "GLM-4.5V", "object": "model", "created": 1756788845, "owned_by": "z.ai" }, { "id": "GLM-4.6", "object": "model", "created": 1756788845, "owned_by": "z.ai" }, { "id": "GLM-4.6V", "object": "model", "created": 1756788845, "owned_by": "z.ai" }, { "id": "GLM-4.7", "object": "model", "created": 1756788845, "owned_by": "z.ai" }, { "id": "GLM-5", "object": "model", "created": 1756788845, "owned_by": "z.ai" }, { "id": "GLM-4.5-Air", "object": "model", "created": 1756788845, "owned_by": "z.ai" }, { "id": "GLM-DR", "object": "model", "created": 1756788845, "owned_by": "z.ai" } ] }

聊天完成

POST /v1/chat/completions

基于消息列表生成模型响应。支持流式和非流式两种模式。

请求参数

参数名 类型 必需 说明
model string 要使用的模型ID,例如 "GLM-4.5"
messages array 消息列表,包含角色和内容
stream boolean 是否使用流式响应,默认为true
temperature number 采样温度,控制随机性
max_tokens integer 生成的最大令牌数
reasoning boolean 启用思考模式,展示模型推理过程(推荐用于复杂任务)
tools array OpenAI 格式的工具定义列表(功能受上游 API 限制)

消息格式

字段 类型 说明
role string 消息角色,可选值:system、user、assistant
content string 消息内容

使用示例

Python示例

import openai # 配置客户端 client = openai.OpenAI( api_key="your-api-key", # 对应 DEFAULT_KEY base_url="http://localhost:9090/v1" ) # 示例 1: 使用旗舰模型 GLM-5 进行复杂推理 response = client.chat.completions.create( model="GLM-5", messages=[{"role": "user", "content": "分析并优化这段代码的时间复杂度"}] ) print(response.choices[0].message.content) # 示例 2: 使用 GLM-4.5-Air 快速响应(适合简单对话) response = client.chat.completions.create( model="GLM-4.5-Air", messages=[{"role": "user", "content": "今天天气怎么样?"}] ) print(response.choices[0].message.content) # 示例 3: 流式请求 - 使用 GLM-4.7 response = client.chat.completions.create( model="GLM-4.7", messages=[{"role": "user", "content": "请写一首关于春天的诗"}], stream=True ) for chunk in response: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="") # 示例 4: 启用思考模式(GLM-4.5/4.6/4.7/5 支持) response = client.chat.completions.create( model="GLM-5", messages=[{"role": "user", "content": "分析这段算法的时间复杂度并给出优化建议"}], reasoning=True # 启用思考模式,展示详细推理过程 ) print(response.choices[0].message.content) # 示例 5: 使用多模态模型 GLM-4.6V(支持图像) # response = client.chat.completions.create( # model="GLM-4.6V", # messages=[{ # "role": "user", # "content": [ # {"type": "text", "text": "描述这张图片"}, # {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}} # ] # }] # )

cURL示例

# 示例 1: 使用旗舰模型 GLM-5(复杂任务) curl -X POST http://localhost:9090/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer your-api-key" -d '{ "model": "GLM-5", "messages": [{"role": "user", "content": "请分析这段代码的性能瓶颈"}], "stream": false }' # 示例 2: 使用 GLM-4.5-Air 快速响应(简单对话) curl -X POST http://localhost:9090/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer your-api-key" -d '{ "model": "GLM-4.5-Air", "messages": [{"role": "user", "content": "你好"}], "stream": false }' # 示例 3: 流式请求 - 使用 GLM-4.7 curl -X POST http://localhost:9090/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer your-api-key" -d '{ "model": "GLM-4.7", "messages": [{"role": "user", "content": "讲一个有趣的故事"}], "stream": true }' # 示例 4: 启用思考模式 - 使用 GLM-5(展示详细推理过程) curl -X POST http://localhost:9090/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer your-api-key" -d '{ "model": "GLM-5", "messages": [{"role": "user", "content": "分析这段算法的时间复杂度并给出优化建议"}], "reasoning": true, "stream": false }' # 示例 5: 多模态请求 - 使用 GLM-4.6V(支持图像) # curl -X POST http://localhost:9090/v1/chat/completions # -H "Content-Type: application/json" # -H "Authorization: Bearer your-api-key" # -d '{ # "model": "GLM-4.6V", # "messages": [{ # "role": "user", # "content": [ # {"type": "text", "text": "这张图片里有什么?"}, # {"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}} # ] # }], # "stream": false # }'

JavaScript示例

const fetch = require('node-fetch'); async function chatWithGLM(model, message, stream = false) { const response = await fetch('http://localhost:9090/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer your-api-key' }, body: JSON.stringify({ model: model, messages: [{ role: 'user', content: message }], stream: stream }) }); if (stream) { // 处理流式响应 const reader = response.body.getReader(); const decoder = new TextDecoder(); while (true) { const { done, value } = await reader.read(); if (done) break; const chunk = decoder.decode(value); const lines = chunk.split(' '); for (const line of lines) { if (line.startsWith('data: ')) { const data = line.slice(6); if (data === '[DONE]') { console.log(' 流式响应完成'); return; } try { const parsed = JSON.parse(data); const content = parsed.choices[0]?.delta?.content; if (content) { process.stdout.write(content); } } catch (e) { // 忽略解析错误 } } } } } else { // 处理非流式响应 const data = await response.json(); console.log(data.choices[0].message.content); } } // 使用示例 1: 旗舰模型 GLM-5(复杂任务) chatWithGLM('GLM-5', '请分析并优化这段算法的时间复杂度', false); // 使用示例 2: GLM-4.5-Air(快速响应) chatWithGLM('GLM-4.5-Air', '你好', false); // 使用示例 3: GLM-4.7 流式响应 chatWithGLM('GLM-4.7', '写一个关于未来的短篇故事', true); // 使用示例 4: 启用思考模式(GLM-4.5/4.6/4.7/5 支持) async function chatWithReasoning(model, message) { const response = await fetch('http://localhost:9090/v1/chat/completions', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer your-api-key' }, body: JSON.stringify({ model: model, messages: [{ role: 'user', content: message }], reasoning: true, // 启用思考模式 stream: false }) }); const data = await response.json(); console.log(data.choices[0].message.content); } chatWithReasoning('GLM-5', '分析这段算法的时间复杂度并给出优化建议'); // 使用示例 5: 多模态模型 GLM-4.6V(支持图像) // chatWithGLM('GLM-4.6V', [ // { type: 'text', text: '描述这张图片' }, // { type: 'image_url', image_url: { url: 'https://example.com/image.jpg' } } // ], false);

错误处理

API使用标准HTTP状态码来表示请求的成功或失败:

状态码 说明
200 OK 请求成功
400 Bad Request 请求格式错误或参数无效
401 Unauthorized API密钥无效或缺失
502 Bad Gateway 上游服务错误
注意: 在调试模式下,服务器会输出详细的日志信息,可以通过设置环境变量 DEBUG_MODE=true 来启用。