ZtoApi 文档

目录

概述

这是一个为Z.ai GLM-4.6/4.5模型提供OpenAI兼容API接口的代理服务器。它允许你使用标准的OpenAI API格式与Z.ai的GLM-4.6/4.5模型进行交互,支持流式和非流式响应。

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

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

身份验证

所有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" } ] }

聊天完成

POST /v1/chat/completions

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

请求参数

参数名 类型 必需 说明
model string 要使用的模型ID,例如 "GLM-4.6", "GLM-4.5"
messages array 消息列表,包含角色和内容
stream boolean 是否使用流式响应,默认为true
temperature number 采样温度,控制随机性
max_tokens integer 生成的最大令牌数

消息格式

字段 类型 说明
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" ) # 非流式请求 - 使用GLM-4.6 response = client.chat.completions.create( model="GLM-4.6", messages=[{"role": "user", "content": "你好,请介绍一下自己"}] ) print(response.choices[0].message.content) # 流式请求 - 使用GLM-4.6 response = client.chat.completions.create( model="GLM-4.6", messages=[{"role": "user", "content": "请写一首关于春天的诗"}], stream=True ) for chunk in response: if chunk.choices[0].delta.content: print(chunk.choices[0].delta.content, end="")

cURL示例

# 非流式请求 curl -X POST http://localhost:9090/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer your-api-key" -d '{ "model": "GLM-4.6", "messages": [{"role": "user", "content": "你好"}], "stream": false }' # 流式请求 curl -X POST http://localhost:9090/v1/chat/completions -H "Content-Type: application/json" -H "Authorization: Bearer your-api-key" -d '{ "model": "GLM-4.6", "messages": [{"role": "user", "content": "你好"}], "stream": true }'

JavaScript示例

const fetch = require('node-fetch'); async function chatWithGLM(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: 'GLM-4.6', 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); } } // 使用示例 chatWithGLM('你好,请介绍一下JavaScript', false);

错误处理

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

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