Spaces:
Paused
Paused
File size: 12,898 Bytes
bad0b35 a6b23ff bad0b35 3af2cb4 bad0b35 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf 553c00b c9a9c69 d41adbf 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf 71280f6 553c00b 71280f6 553c00b 71280f6 d41adbf cbe30d3 d41adbf 553c00b d41adbf 553c00b cbe30d3 553c00b c9a9c69 d41adbf c9a9c69 cbe30d3 c9a9c69 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf 5a55e77 d41adbf cbe30d3 d41adbf 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf 553c00b d41adbf cbe30d3 d41adbf cbe30d3 d41adbf cbe30d3 d41adbf cbe30d3 d41adbf 553c00b cbe30d3 553c00b d41adbf 553c00b d41adbf 5a55e77 d41adbf 5a55e77 d41adbf 5a55e77 d41adbf 5a55e77 cbe30d3 5a55e77 cbe30d3 5a55e77 cbe30d3 5a55e77 d41adbf 5a55e77 d41adbf cbe30d3 d41adbf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | ---
title: Row Proxy
emoji: 🚀
colorFrom: green
colorTo: gray
sdk: docker
pinned: false
---

# zai-proxy
zai-proxy 是一个基于 Go 语言的代理服务,将 z.ai 网页聊天转换为 **OpenAI API** 和 **Anthropic Messages API** 兼容格式。用户使用自己的 z.ai token 或 `free` 匿名令牌进行调用。
## 功能特性
- OpenAI `/v1/chat/completions` 兼容
- Anthropic `/v1/messages` 兼容(支持 Claude SDK 直连)
- 支持流式 (SSE) 和非流式响应
- 支持多种 GLM 模型,Claude 模型名自动映射
- 支持思考模式 (thinking)
- 支持联网搜索模式 (search)
- 支持工具/函数调用 (function calling)
- 支持多模态图片输入
- 支持匿名 Token(免登录)
- 自动生成请求签名
- 自动跟踪前端版本号
## 快速开始
### 从源码运行
要求 Go 1.21+:
```bash
git clone https://github.com/yurika0211/zai-proxy.git
cd zai-proxy
go mod download
go run main.go
```
编译为可执行文件:
```bash
go build -o zai-proxy .
./zai-proxy
```
### Docker 部署
#### 使用预构建镜像
```bash
docker run -d --name zai-proxy -p 8000:8000 ghcr.io/yurika0211/zai-proxy:latest
```
自定义端口和日志级别:
```bash
docker run -d --name zai-proxy \
-p 8080:8000 \
-e LOG_LEVEL=debug \
ghcr.io/yurika0211/zai-proxy:latest
```
> 镜像仅支持 `linux/amd64` 平台。
#### 挂载代理文件(可选)
```bash
docker run -d --name zai-proxy \
-p 8000:8000 \
-v ./proxies.txt:/app/proxies.txt \
ghcr.io/yurika0211/zai-proxy:latest
```
#### 可用镜像标签
| 标签 | 说明 |
|------|------|
| `latest` | 默认分支的最新构建 |
| `v*`(如 `v1.0.0`) | 语义化版本发布 |
| `<commit-sha>` | 特定 commit 的构建 |
#### 自行构建镜像
```bash
docker build -t zai-proxy .
docker run -d --name zai-proxy -p 8000:8000 zai-proxy
```
#### Docker Compose
```yaml
services:
zai-proxy:
image: ghcr.io/yurika0211/zai-proxy:latest
ports:
- "8000:8000"
environment:
- LOG_LEVEL=info
restart: unless-stopped
```
```bash
docker compose up -d
```
## 环境变量
| 变量名 | 说明 | 默认值 |
|--------|------|--------|
| `PORT` | 监听端口 | `8000` |
| `LOG_LEVEL` | 日志级别 (`debug` / `info` / `warn` / `error`) | `info` |
支持 `.env` 文件自动加载。
## 代理池(可选)
在项目根目录放置 `proxies.txt` 文件,每行一个 SOCKS5 代理,格式:
```
ip:port:username:password
ip:port
```
启动时自动加载,每次请求随机选择一个代理。不存在该文件则直连。
## 获取 z.ai Token
### 方式一:使用匿名 Token(免登录)
直接使用 `free` 作为 API key,服务会自动获取匿名 token:
```bash
curl http://localhost:8000/v1/chat/completions \
-H "Authorization: Bearer free" \
-H "Content-Type: application/json" \
-d '{"model": "glm-4.7", "messages": [{"role": "user", "content": "hello"}]}'
```
### 方式二:使用个人 Token
1. 登录 https://chat.z.ai
2. 打开浏览器开发者工具 (F12)
3. 切换到 Application/Storage 标签
4. 在 Cookies 中找到 `token` 字段
5. 复制其值作为 API 调用的 Authorization
## API 端点
### `GET /v1/models`
返回可用模型列表(OpenAI 兼容格式)。
### `POST /v1/chat/completions`
OpenAI 兼容的聊天补全接口。
**认证方式:** `Authorization: Bearer <token>` 或 `x-api-key: <token>`
```bash
curl http://localhost:8000/v1/chat/completions \
-H "Authorization: Bearer YOUR_ZAI_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-4.7",
"messages": [{"role": "user", "content": "hello"}],
"stream": true
}'
```
### `POST /v1/messages`
Anthropic Messages API 兼容接口,可直接使用 Anthropic SDK 连接。
**认证方式:** `x-api-key: <token>` 或 `Authorization: Bearer <token>`
```bash
curl http://localhost:8000/v1/messages \
-H "x-api-key: free" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "hello"}],
"stream": true
}'
```
支持的 Anthropic 特性:
- `system` 字段(字符串或内容块数组)
- `thinking` 推理模式(`{"type": "enabled"}`)
- `tools` / `tool_choice` 工具调用
- 流式 SSE 事件(`message_start` / `content_block_delta` / `message_stop` 等)
- 非流式 JSON 响应
## 支持的模型
### GLM 模型
| 模型名称 | 上游模型 | 说明 |
|----------|----------|------|
| `glm-4.5` | 0727-360B-API | |
| `glm-4.6` | GLM-4-6-API-V1 | |
| `glm-4.7` | glm-4.7 | |
| `glm-5` | glm-5 | 最新 |
| `glm-4.5-v` | glm-4.5v | 视觉 |
| `glm-4.6-v` | glm-4.6v | 视觉(最新) |
| `glm-4.5-air` | 0727-106B-API | 轻量 |
### Claude 模型名映射
通过 `/v1/messages` 端点使用 Claude 模型名时,会自动映射到对应的 GLM 模型:
| Claude 模型 | 映射到 | 备注 |
|-------------|--------|------|
| `claude-opus-4-6` | glm-4.7 | 自动启用 thinking |
| `claude-opus-4-5-20250514` | glm-4.7 | 自动启用 thinking |
| `claude-sonnet-4-6` | glm-4.7 | |
| `claude-sonnet-4-5-20241022` | glm-4.7 | |
| `claude-haiku-4-5` | glm-4.5-air | |
| `claude-haiku-4-5-20251001` | glm-4.5-air | |
| `claude-3-5-sonnet-20241022` | glm-4.7 | |
| `claude-3-5-haiku-20241022` | glm-4.5-air | |
> Opus 系列模型始终自动启用 thinking 模式。未识别的模型名会回退到 glm-4.7。
### 模型标签
模型名称支持以下后缀标签(可组合使用,顺序不限):
| 标签 | 说明 |
|------|------|
| `-thinking` | 启用思考模式,响应包含 `reasoning_content` 字段 |
| `-search` | 启用联网搜索,搜索结果自动转换为 markdown 引用 |
| `-tools` | 自动注入内置工具定义,模型可进行函数调用 |
组合示例:
- `glm-4.7-thinking`
- `glm-4.7-search`
- `glm-4.7-thinking-search`
- `glm-4.7-tools`
- `glm-4.7-tools-thinking`
- `glm-5-thinking`
- `glm-5-tools`
## 使用示例
### 多模态请求
```json
{
"model": "glm-4.6-v",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "描述这张图片"},
{"type": "image_url", "image_url": {"url": "https://example.com/image.jpg"}}
]
}
]
}
```
支持的图片格式:
- HTTP/HTTPS URL
- Base64 编码 (`data:image/jpeg;base64,...`)
### Anthropic SDK (Python)
```python
import anthropic
client = anthropic.Anthropic(
api_key="free",
base_url="http://localhost:8000",
)
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[{"role": "user", "content": "hello"}],
)
print(message.content[0].text)
```
### Anthropic SDK 思考模式
```python
message = client.messages.create(
model="claude-opus-4-6",
max_tokens=8192,
thinking={"type": "enabled", "budget_tokens": 4096},
messages=[{"role": "user", "content": "解释量子纠缠"}],
)
for block in message.content:
if block.type == "thinking":
print("思考:", block.thinking)
elif block.type == "text":
print("回答:", block.text)
```
## 工具调用 (Function Calling)
### 内置工具
使用 `-tools` 后缀时,代理会自动注入以下 6 个内置工具定义:
| 工具名 | 描述 | 主要参数 |
|--------|------|----------|
| `get_current_time` | 获取当前时间 | `timezone`, `format` |
| `calculate` | 执行数学计算 | `expression` |
| `search_web` | 搜索网络信息 | `query`, `num_results` |
| `query_database` | 执行 SQL 查询 | `sql`, `database` |
| `file_operations` | 文件读写列表 | `operation`, `path`, `content` |
| `call_external_api` | 调用外部 API | `url`, `method`, `headers`, `body` |
### 基本调用
```bash
curl http://localhost:8000/v1/chat/completions \
-H "Authorization: Bearer YOUR_ZAI_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-4.7-tools",
"messages": [{"role": "user", "content": "现在几点了?"}],
"stream": true
}'
```
模型会返回 `tool_calls`(`finish_reason` 为 `"tool_calls"`),由客户端自行执行工具并将结果发回。
### 多轮调用流程
```
第1轮:用户提问 → 模型返回 tool_calls
第2轮:发送工具执行结果 → 模型生成最终回答
```
```bash
curl http://localhost:8000/v1/chat/completions \
-H "Authorization: Bearer YOUR_ZAI_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-4.7-tools",
"messages": [
{"role": "user", "content": "现在几点了?"},
{"role": "assistant", "content": "", "tool_calls": [
{"id": "call_xxx", "type": "function", "function": {"name": "get_current_time", "arguments": "{}"}}
]},
{"role": "tool", "tool_call_id": "call_xxx", "content": "{\"time\": \"2026-03-14 15:30:00\"}"}
],
"stream": true
}'
```
### 自定义工具
也可以不使用 `-tools` 后缀,直接在请求中传入 `tools` 字段(标准 OpenAI 格式):
```json
{
"model": "glm-4.7",
"messages": [{"role": "user", "content": "北京天气怎么样?"}],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}
}],
"tool_choice": "auto"
}
```
`tool_choice` 支持:
- `"auto"` — 模型自行决定是否调用工具
- `"required"` — 强制调用工具
- `"none"` — 禁用工具调用
- `{"type": "function", "function": {"name": "xxx"}}` — 强制调用指定工具
两者可混合使用:`-tools` 模型名 + 自定义 `tools` 字段。**客户端自带的同名工具优先**,不会被内置工具覆盖。
### Anthropic 格式工具调用
通过 `/v1/messages` 端点同样支持工具调用:
```bash
curl http://localhost:8000/v1/messages \
-H "x-api-key: free" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "北京天气怎么样?"}],
"tools": [{
"name": "get_weather",
"description": "获取天气信息",
"input_schema": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"}
},
"required": ["city"]
}
}]
}'
```
`tool_choice` 映射关系:
| Anthropic | OpenAI 等价 |
|-----------|-------------|
| `{"type": "auto"}` | `"auto"` |
| `{"type": "any"}` | `"required"` |
| `{"type": "none"}` | `"none"` |
| `{"type": "tool", "name": "xxx"}` | `{"type": "function", ...}` |
## 项目结构
```
zai-proxy/
├── main.go # 入口,路由注册
├── internal/
│ ├── auth/ # 认证与签名
│ │ ├── anonymous.go # 匿名 token 获取
│ │ ├── jwt.go # JWT 解码
│ │ └── signature.go # HMAC-SHA256 请求签名
│ ├── config/ # 配置加载 (.env)
│ ├── filter/ # 响应处理过滤器
│ │ ├── prompttool.go # 提取 prompt 注入的 tool call
│ │ ├── toolcall.go # 解析 JSON 格式的 tool call
│ │ ├── thinking.go # 思考/推理内容处理
│ │ └── search.go # 搜索结果引用转换
│ ├── handler/ # HTTP 处理器
│ │ ├── chat.go # /v1/chat/completions
│ │ ├── anthropic.go # /v1/messages
│ │ └── models.go # /v1/models
│ ├── logger/ # 日志系统
│ ├── model/ # 类型定义
│ │ ├── types.go # OpenAI 兼容类型
│ │ ├── anthropic.go # Anthropic API 类型
│ │ └── mapping.go # 模型名映射与解析
│ ├── proxy/ # SOCKS5 代理池
│ │ └── pool.go # 代理加载与随机选择
│ ├── tools/ # 内置工具定义
│ │ ├── builtin.go # 6 个内置工具
│ │ └── prompt.go # 工具系统提示词构建
│ ├── upstream/ # 上游 z.ai 客户端
│ │ ├── client.go # 请求构建与发送
│ │ └── upload.go # 图片上传
│ └── version/ # 前端版本号跟踪
├── scripts/
│ └── test_tool_call.sh # 工具调用集成测试
├── Dockerfile
└── .github/workflows/ # CI/CD
```
|