# ZaiProxy **Repository Path**: superdu6/zai-proxy ## Basic Information - **Project Name**: ZaiProxy - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-30 - **Last Updated**: 2026-01-30 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # zai-proxy zai-proxy 是一个基于 Go 语言的代理服务,将 z.ai 网页聊天转换为 **Anthropic API** 兼容格式。用户使用自己的 z.ai token 进行调用。 ## 功能特性 - **Anthropic API 兼容格式** (`/v1/messages`) - 支持流式和非流式响应 - 支持多种 GLM 模型和 Claude 模型名称映射 - 支持思考模式 (thinking) - 使用 Anthropic extended thinking 格式 - 支持联网搜索模式 (search) - 支持多模态图片输入 - 支持匿名 Token(免登录) - **自动生成签名** - **自动更新签名版本号** - **智能上下文管理** - 自动限制消息数量,避免 token 过长 - **完善的错误处理** - 详细的错误信息返回 - **支持 system prompt 数组格式** - 兼容 Claude Code 等客户端 ## 快速开始 ### 安装运行 ```bash # 克隆项目 git clone https://gitee.com/superdu6/zai-proxy.git cd zai-proxy # 安装依赖 go mod download # 运行服务 go run main.go ``` ### Docker 一键部署 ```bash docker run -d -p 8000:8000 ghcr.io/superdu6/zai-proxy:latest ``` 自定义端口和日志级别: ```bash docker run -d -p 8080:8000 -e LOG_LEVEL=debug ghcr.io/superdu6/zai-proxy:latest ``` ## 环境变量 | 变量名 | 说明 | 默认值 | |--------|------|--------| | PORT | 监听端口 | 8000 | | LOG_LEVEL | 日志级别 | info | ## 获取 z.ai Token ### 方式一:使用匿名 Token(免登录) 直接使用 `free` 作为 API key,服务会自动获取一个匿名 token: ```bash curl http://localhost:8000/v1/messages \ -H "x-api-key: free" \ -H "anthropic-version: 2023-06-01" \ -H "Content-Type: application/json" \ -d '{ "model": "GLM-4.7", "max_tokens": 4096, "messages": [{"role": "user", "content": "hello"}] }' ``` ### 方式二:使用个人 Token 1. 登录 https://chat.z.ai 2. 打开浏览器开发者工具 (F12) 3. 切换到 Application/Storage 标签 4. 在 Cookies 中找到 `token` 字段 5. 复制其值作为 API 调用的 `x-api-key` ## 支持的模型 ### GLM 模型 | 模型名称 | 上游模型 | |----------|----------| | GLM-4.5 | 0727-360B-API | | GLM-4.6 | GLM-4-6-API-V1 | | GLM-4.7 | glm-4.7 | | GLM-4.5-V | glm-4.5v | | GLM-4.6-V | glm-4.6v | | GLM-4.5-Air | 0727-106B-API | ### Claude 模型映射 以下 Claude 模型名称会自动映射到 GLM-4.7: - `claude-sonnet-4-5-20250929` - `claude-haiku-4-5-20251001` - `claude-opus-4-5-20250514` - `claude-opus-4-5-20251101` - `claude-3-5-sonnet-20241022` - `claude-3-5-haiku-20241022` ### 模型标签 模型名称支持以下后缀标签(可组合使用): - `-thinking`: 启用思考模式,响应会包含 `{"type": "thinking", "text": "..."}` content block - `-search`: 启用联网搜索模式 - (TODO) `-deepsearch`: 启用多轮搜索,深入研究分析 示例: - `GLM-4.7-thinking` - `GLM-4.7-search` - `GLM-4.7-thinking-search` ## 使用示例 ### curl 测试 **非流式请求:** ```bash curl http://localhost:8000/v1/messages \ -H "x-api-key: YOUR_ZAI_TOKEN" \ -H "anthropic-version: 2023-06-01" \ -H "Content-Type: application/json" \ -d '{ "model": "GLM-4.7", "max_tokens": 4096, "messages": [{"role": "user", "content": "hello"}] }' ``` **流式请求:** ```bash curl -N http://localhost:8000/v1/messages \ -H "x-api-key: YOUR_ZAI_TOKEN" \ -H "anthropic-version: 2023-06-01" \ -H "Content-Type: application/json" \ -d '{ "model": "GLM-4.7", "max_tokens": 4096, "stream": true, "messages": [{"role": "user", "content": "hello"}] }' ``` ### 多模态请求: ```json { "model": "GLM-4.6-V", "max_tokens": 4096, "messages": [ { "role": "user", "content": [ {"type": "text", "text": "描述这张图片"}, {"type": "image", "source": {"type": "url", "url": "https://example.com/image.jpg"}} ] } ] } ``` ### 支持的图片格式: - HTTP/HTTPS URL - Base64 编码 (data:image/jpeg;base64,...) ### 思考模式示例: ```bash curl http://localhost:8000/v1/messages \ -H "x-api-key: YOUR_ZAI_TOKEN" \ -H "anthropic-version: 2023-06-01" \ -d '{ "model": "GLM-4.7-thinking", "max_tokens": 4096, "messages": [{"role": "user", "content": "解释量子计算"}] }' ``` 响应格式: ```json { "id": "msg_xxx", "type": "message", "role": "assistant", "content": [ {"type": "thinking", "text": "思考内容..."}, {"type": "text", "text": "回答内容..."} ], "model": "GLM-4.7", "stop_reason": "end_turn", "usage": {"input_tokens": 10, "output_tokens": 20} } ``` ## API 响应格式 ### 成功响应(非流式) ```json { "id": "msg_abc123", "type": "message", "role": "assistant", "content": [ {"type": "text", "text": "Hello! How can I help you?"} ], "model": "GLM-4.7", "stop_reason": "end_turn", "stop_sequence": null, "usage": { "input_tokens": 10, "output_tokens": 20 } } ``` ### 流式响应事件流 ``` event: message_start data: {"type":"message_start","message":{"id":"msg_xxx","type":"message","role":"assistant",...}} event: content_block_start data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}} event: content_block_delta data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}} event: content_block_stop data: {"type":"content_block_stop","index":0} event: message_delta data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":20}} event: message_stop ``` ### 错误响应 ```json { "type": "error", "error": { "type": "invalid_request_error", "message": "Unauthorized" } } ``` ## 客户端兼容性 本代理兼容任何支持 Anthropic API 的客户端,例如: - [Anthropic 官方 SDK](https://docs.anthropic.com/en/api/client-sdks) - [Claude Code](https://claude.ai/download) - 完美兼容 - [Cursor IDE](https://cursor.sh/) - [Claude Dev](https://github.com/OthersideAI/self-operating-computer) - 其他支持 Anthropic API 的工具 ### Claude Code 配置示例 在 Claude Code 中使用本代理: ```json { "env": { "ANTHROPIC_AUTH_TOKEN": "你的z.ai token或使用free", "ANTHROPIC_BASE_URL": "http://localhost:8000", "API_TIMEOUT_MS": "3000000", "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1" } } ``` ## 注意事项 1. **max_tokens 参数必需**: Anthropic API 要求必须提供 `max_tokens` 参数,默认值为 4096 2. **认证头**: 使用 `x-api-key` 头进行认证(也兼容 `Authorization: Bearer` 格式) 3. **版本头**: 请求时建议添加 `anthropic-version: 2023-06-01` 头 4. **上下文限制**: 代理会自动限制发送到上游的消息数量(默认最近 10 条),避免 token 过长 5. **推荐使用个人 token**: 匿名 token 有更严格的限制,建议使用个人 z.ai token ## 常见问题 ### 1. 如何获取 z.ai token? 访问 https://chat.z.ai 登录后,按 F12 打开开发者工具,在 Application → Cookies 中找到 `token` 字段。 ### 2. 为什么会出现签名验证失败? 确保使用的是有效的 z.ai token,匿名 token 可能会过期。建议使用个人 token。 ### 3. 如何处理长对话上下文? 代理默认只保留最近 10 条消息。如果需要更长的上下文,可以修改 `internal/chat.go` 中的 `maxMessages` 值。 ### 4. 支持哪些 Claude 模型? 所有 Claude 模型名称都会自动映射到 GLM-4.7。你可以使用任何 Claude 模型名称,代理会自动处理。 ## 贡献 欢迎提交 Issue 和 Pull Request! ## 许可证 MIT License ## 致谢 - 原项目:[kao0312/zai-proxy](https://github.com/kao0312/zai-proxy) - z.ai 提供的优秀 AI 服务