ZyphrZero
commited on
Commit
·
7d20731
1
Parent(s):
6e09c2f
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,106 +1,49 @@
|
|
|
|
|
| 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 |
-
### 使用 pip
|
| 47 |
-
|
| 48 |
-
1. 安装依赖:
|
| 49 |
-
```bash
|
| 50 |
-
pip install -r requirements.txt
|
| 51 |
-
```
|
| 52 |
-
|
| 53 |
-
2. 配置服务(可选):
|
| 54 |
-
编辑 `main.py` 中的 `ServerConfig` 类以调整服务行为:
|
| 55 |
-
- `AUTH_TOKEN`: 客户端 API Key 密钥
|
| 56 |
-
- `API_ENDPOINT`: Z.ai 上游 API 地址
|
| 57 |
-
- `BACKUP_TOKEN`: 固定认证 token(匿名模式失败时使用)
|
| 58 |
-
- `LISTEN_PORT`: 服务监听端口
|
| 59 |
-
- `DEBUG_LOGGING`: 调试模式开关
|
| 60 |
-
- `THINKING_PROCESSING`: 思考内容处理策略
|
| 61 |
-
- `ANONYMOUS_MODE`: 匿名模式开关
|
| 62 |
-
- `TOOL_SUPPORT`: Function Call 功能开关
|
| 63 |
-
|
| 64 |
-
3. 运行服务:
|
| 65 |
-
```bash
|
| 66 |
-
python main.py
|
| 67 |
-
```
|
| 68 |
-
|
| 69 |
-
服务启动后,可以访问 http://localhost:8080/docs 查看自动生成的 Swagger API 文档
|
| 70 |
-
|
| 71 |
-
4. 使用 OpenAI 客户端库调用:
|
| 72 |
-
```python
|
| 73 |
-
import openai
|
| 74 |
-
|
| 75 |
-
# 初始化客户端
|
| 76 |
-
client = openai.OpenAI(
|
| 77 |
-
base_url="http://localhost:8080/v1",
|
| 78 |
-
api_key="sk-your-api-key"
|
| 79 |
-
)
|
| 80 |
-
|
| 81 |
-
# 流式调用示例
|
| 82 |
-
response = client.chat.completions.create(
|
| 83 |
-
model="GLM-4.5", # 可选: "GLM-4.5-Thinking", "GLM-4.5-Search"
|
| 84 |
-
messages=[{"role": "user", "content": "你好"}],
|
| 85 |
-
stream=True
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
for chunk in response:
|
| 89 |
-
content = chunk.choices[0].delta.content
|
| 90 |
-
reasoning = chunk.choices[0].delta.reasoning_content
|
| 91 |
-
if content:
|
| 92 |
-
print(content, end="")
|
| 93 |
-
if reasoning:
|
| 94 |
-
print(f"\n[思考] {reasoning}\n")
|
| 95 |
-
```
|
| 96 |
-
|
| 97 |
-
注意:请将 `api_key` 替换为您在 `main.py` 中配置的 `AUTH_TOKEN` 值。
|
| 98 |
-
|
| 99 |
-
### Function Call 使用示例
|
| 100 |
-
|
| 101 |
-
本项目完整支持 OpenAI 格式的工具调用功能,包括流式和非流式响应。实现原理是将 OpenAI 的工具定义转换为特殊的系统提示,让模型理解并生成符合格式的工具调用。
|
| 102 |
-
|
| 103 |
-
#### 基本工具调用
|
| 104 |
|
| 105 |
```python
|
| 106 |
import openai
|
|
@@ -108,281 +51,207 @@ import openai
|
|
| 108 |
# 初始化客户端
|
| 109 |
client = openai.OpenAI(
|
| 110 |
base_url="http://localhost:8080/v1",
|
| 111 |
-
api_key="
|
| 112 |
)
|
| 113 |
|
| 114 |
-
#
|
| 115 |
-
tools = [
|
| 116 |
-
{
|
| 117 |
-
"type": "function",
|
| 118 |
-
"function": {
|
| 119 |
-
"name": "get_weather",
|
| 120 |
-
"description": "获取指定城市的天气信息",
|
| 121 |
-
"parameters": {
|
| 122 |
-
"type": "object",
|
| 123 |
-
"properties": {
|
| 124 |
-
"city": {
|
| 125 |
-
"type": "string",
|
| 126 |
-
"description": "城市名称"
|
| 127 |
-
},
|
| 128 |
-
"unit": {
|
| 129 |
-
"type": "string",
|
| 130 |
-
"enum": ["celsius", "fahrenheit"],
|
| 131 |
-
"description": "温度单位",
|
| 132 |
-
"default": "celsius"
|
| 133 |
-
}
|
| 134 |
-
},
|
| 135 |
-
"required": ["city"]
|
| 136 |
-
}
|
| 137 |
-
}
|
| 138 |
-
}
|
| 139 |
-
]
|
| 140 |
-
|
| 141 |
-
# 使用工具调用
|
| 142 |
response = client.chat.completions.create(
|
| 143 |
model="GLM-4.5",
|
| 144 |
-
messages=[{"role": "user", "content": "
|
| 145 |
-
|
| 146 |
-
tool_choice="auto"
|
| 147 |
)
|
| 148 |
|
| 149 |
-
|
| 150 |
-
if message.tool_calls:
|
| 151 |
-
print("模型请求调用工具:")
|
| 152 |
-
for tool_call in message.tool_calls:
|
| 153 |
-
print(f"工具名称: {tool_call.function.name}")
|
| 154 |
-
print(f"参数: {tool_call.function.arguments}")
|
| 155 |
-
print(f"调用ID: {tool_call.id}")
|
| 156 |
-
else:
|
| 157 |
-
print(f"回复: {message.content}")
|
| 158 |
```
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
```python
|
| 163 |
-
# 流式工具调用示例
|
| 164 |
-
response = client.chat.completions.create(
|
| 165 |
-
model="GLM-4.5",
|
| 166 |
-
messages=[{"role": "user", "content": "帮我计算 2 的 10 次方"}],
|
| 167 |
-
tools=[{
|
| 168 |
-
"type": "function",
|
| 169 |
-
"function": {
|
| 170 |
-
"name": "calculate",
|
| 171 |
-
"description": "执行数学计算",
|
| 172 |
-
"parameters": {
|
| 173 |
-
"type": "object",
|
| 174 |
-
"properties": {
|
| 175 |
-
"expression": {
|
| 176 |
-
"type": "string",
|
| 177 |
-
"description": "数学表达式"
|
| 178 |
-
}
|
| 179 |
-
},
|
| 180 |
-
"required": ["expression"]
|
| 181 |
-
}
|
| 182 |
-
}
|
| 183 |
-
}],
|
| 184 |
-
stream=True
|
| 185 |
-
)
|
| 186 |
-
|
| 187 |
-
# 注意:工具调用模式下,流式响应会缓冲所有内容,
|
| 188 |
-
# 在最后一次性返回工具调用信息
|
| 189 |
-
tool_calls = None
|
| 190 |
-
content = ""
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
tool_calls = delta.tool_calls
|
| 196 |
-
if delta.content:
|
| 197 |
-
content += delta.content
|
| 198 |
-
|
| 199 |
-
if tool_calls:
|
| 200 |
-
print("工具调用:")
|
| 201 |
-
for tool_call in tool_calls:
|
| 202 |
-
print(f"函数: {tool_call.function.name}")
|
| 203 |
-
print(f"参数: {tool_call.function.arguments}")
|
| 204 |
-
else:
|
| 205 |
-
print("回复:", content)
|
| 206 |
```
|
| 207 |
|
| 208 |
-
|
| 209 |
|
| 210 |
-
|
| 211 |
-
# 强制使用特定工具
|
| 212 |
-
response = client.chat.completions.create(
|
| 213 |
-
model="GLM-4.5",
|
| 214 |
-
messages=[{"role": "user", "content": "今天是什么日子"}],
|
| 215 |
-
tools=[{
|
| 216 |
-
"type": "function",
|
| 217 |
-
"function": {
|
| 218 |
-
"name": "get_current_date",
|
| 219 |
-
"description": "获取当前日期和时间",
|
| 220 |
-
"parameters": {
|
| 221 |
-
"type": "object",
|
| 222 |
-
"properties": {},
|
| 223 |
-
"required": []
|
| 224 |
-
}
|
| 225 |
-
}
|
| 226 |
-
}],
|
| 227 |
-
tool_choice={"type": "function", "function": {"name": "get_current_date"}}
|
| 228 |
-
)
|
| 229 |
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
|
| 236 |
-
|
| 237 |
|
| 238 |
```python
|
| 239 |
-
#
|
| 240 |
-
tools = [
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
"
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
"
|
| 247 |
-
|
| 248 |
-
"
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
"description": "搜索关键词"
|
| 252 |
-
}
|
| 253 |
-
},
|
| 254 |
-
"required": ["query"]
|
| 255 |
-
}
|
| 256 |
-
}
|
| 257 |
-
},
|
| 258 |
-
{
|
| 259 |
-
"type": "function",
|
| 260 |
-
"function": {
|
| 261 |
-
"name": "summarize_text",
|
| 262 |
-
"description": "总结文本内容",
|
| 263 |
-
"parameters": {
|
| 264 |
-
"type": "object",
|
| 265 |
-
"properties": {
|
| 266 |
-
"text": {
|
| 267 |
-
"type": "string",
|
| 268 |
-
"description": "要总结的文本"
|
| 269 |
-
},
|
| 270 |
-
"max_length": {
|
| 271 |
-
"type": "integer",
|
| 272 |
-
"description": "最大长度",
|
| 273 |
-
"default": 100
|
| 274 |
-
}
|
| 275 |
-
},
|
| 276 |
-
"required": ["text"]
|
| 277 |
-
}
|
| 278 |
}
|
| 279 |
}
|
| 280 |
-
]
|
| 281 |
|
| 282 |
-
#
|
| 283 |
response = client.chat.completions.create(
|
| 284 |
model="GLM-4.5",
|
| 285 |
-
messages=[{"role": "user", "content": "
|
| 286 |
tools=tools,
|
| 287 |
tool_choice="auto"
|
| 288 |
)
|
| 289 |
-
|
| 290 |
-
message = response.choices[0].message
|
| 291 |
-
if message.tool_calls:
|
| 292 |
-
for tool_call in message.tool_calls:
|
| 293 |
-
print(f"调用工具: {tool_call.function.name}")
|
| 294 |
-
# 在实际应用中,这里需要执行相应的函数
|
| 295 |
-
# 并将结果通过工具消息返回给模型
|
| 296 |
```
|
| 297 |
|
| 298 |
-
###
|
| 299 |
|
| 300 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
-
|
| 303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 304 |
```
|
| 305 |
|
| 306 |
-
|
| 307 |
-
1. 基本的工具调用
|
| 308 |
-
2. 数学计算工具
|
| 309 |
-
3. 强制使用特定工具
|
| 310 |
-
4. 流式工具调用响应
|
| 311 |
|
| 312 |
-
###
|
| 313 |
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
-
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
```
|
| 327 |
|
| 328 |
-
|
| 329 |
-
```bash
|
| 330 |
-
docker-compose logs -f
|
| 331 |
-
```
|
| 332 |
|
| 333 |
-
|
| 334 |
-
```bash
|
| 335 |
-
docker-compose up -d --build
|
| 336 |
-
```
|
| 337 |
|
| 338 |
-
|
|
|
|
|
|
|
| 339 |
|
| 340 |
-
|
|
|
|
|
|
|
|
|
|
| 341 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
-
|
|
|
|
| 344 |
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
| `API_ENDPOINT` | Z.ai 的上游 API 地址 | `https://chat.z.ai/api/chat/completions` |
|
| 348 |
-
| `AUTH_TOKEN` | 下游客户端鉴权 key | `sk-your-api-key` |
|
| 349 |
-
| `BACKUP_TOKEN` | 上游 API 的 token (匿名模式失败时使用) | JWT token |
|
| 350 |
-
| `PRIMARY_MODEL` | 默认模型名称 | `GLM-4.5` |
|
| 351 |
-
| `THINKING_MODEL` | 思考模型名称 | `GLM-4.5-Thinking` |
|
| 352 |
-
| `SEARCH_MODEL` | 搜索模型名称 | `GLM-4.5-Search` |
|
| 353 |
-
| `LISTEN_PORT` | 服务监听端口 | `8080` |
|
| 354 |
-
| `DEBUG_LOGGING` | 调试模式开关 | `true` |
|
| 355 |
-
| `THINKING_PROCESSING` | 思考内容处理策略 | `think` (可选: `strip`, `raw`) |
|
| 356 |
-
| `ANONYMOUS_MODE` | 是否使用匿名 token | `true` |
|
| 357 |
-
| `TOOL_SUPPORT` | 是否启用 Function Call 功能 | `true` |
|
| 358 |
|
| 359 |
-
|
|
|
|
| 360 |
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
- **raw**: 保留原始格式,不做任何处理
|
| 364 |
|
| 365 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
|
| 367 |
-
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
-
|
| 370 |
-
- **Pydantic**: 数据验证和序列化,确保 API 兼容性
|
| 371 |
-
- **uvicorn**: ASGI 服务器,提供高性能服务
|
| 372 |
|
| 373 |
-
|
|
|
|
| 374 |
|
| 375 |
-
##
|
| 376 |
|
| 377 |
-
|
| 378 |
-
1. 遵循 PEP 8 规范
|
| 379 |
-
2. 提交前运行测试(如果有)
|
| 380 |
-
3. 更新相关文档
|
| 381 |
|
| 382 |
-
##
|
| 383 |
|
| 384 |
-
|
|
|
|
|
|
|
|
|
|
| 385 |
|
| 386 |
-
|
| 387 |
|
| 388 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
# Z.AI OpenAI API 代理服务
|
| 2 |
|
| 3 |
+

|
| 4 |
+

|
| 5 |
+

|
| 6 |
+
|
| 7 |
+
为 Z.AI 提供 OpenAI API 兼容接口的轻量级代理服务,支持 GLM-4.5 系列模型的完整功能。
|
| 8 |
+
|
| 9 |
+
## ✨ 核心特性
|
| 10 |
+
|
| 11 |
+
- 🔌 **完全兼容 OpenAI API** - 无缝集成现有应用
|
| 12 |
+
- 🚀 **高性能流式响应** - Server-Sent Events (SSE) 支持
|
| 13 |
+
- 🛠️ **Function Call 支持** - 完整的工具调用功能
|
| 14 |
+
- 🧠 **思考模式支持** - 智能处理模型推理过程
|
| 15 |
+
- 🔍 **搜索模型集成** - GLM-4.5-Search 网络搜索能力
|
| 16 |
+
- 🐳 **Docker 部署** - 一键容器化部署
|
| 17 |
+
- 🛡️ **会话隔离** - 匿名模式保护隐私
|
| 18 |
+
- 🔧 **高度可配置** - 环境变量灵活配置
|
| 19 |
+
|
| 20 |
+
## 🚀 快速开始
|
| 21 |
+
|
| 22 |
+
### 环境要求
|
| 23 |
+
|
| 24 |
+
- Python 3.8+
|
| 25 |
+
- pip 或 uv (推荐)
|
| 26 |
+
|
| 27 |
+
### 安装运行
|
| 28 |
+
|
| 29 |
+
```bash
|
| 30 |
+
# 克隆项目
|
| 31 |
+
git clone https://github.com/ZyphrZero/z.ai2api_python.git
|
| 32 |
+
cd z.ai2api_python
|
| 33 |
+
|
| 34 |
+
# 使用 uv (推荐)
|
| 35 |
+
curl -LsSf https://astral.sh/uv/install.sh | sh
|
| 36 |
+
uv sync
|
| 37 |
+
uv run python main.py
|
| 38 |
+
|
| 39 |
+
# 或使用 pip (推荐使用清华源)
|
| 40 |
+
pip install -r requirement.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
|
| 41 |
+
python main.py
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
服务启动后访问:http://localhost:8080/docs
|
| 45 |
+
|
| 46 |
+
### 基础使用
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
```python
|
| 49 |
import openai
|
|
|
|
| 51 |
# 初始化客户端
|
| 52 |
client = openai.OpenAI(
|
| 53 |
base_url="http://localhost:8080/v1",
|
| 54 |
+
api_key="your-auth-token" # 替换为你的 AUTH_TOKEN
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# 普通对话
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
response = client.chat.completions.create(
|
| 59 |
model="GLM-4.5",
|
| 60 |
+
messages=[{"role": "user", "content": "你好,介绍一下 Python"}],
|
| 61 |
+
stream=False
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
+
print(response.choices[0].message.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
```
|
| 66 |
|
| 67 |
+
### Docker 部署
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
```bash
|
| 70 |
+
cd deploy
|
| 71 |
+
docker-compose up -d
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
```
|
| 73 |
|
| 74 |
+
## 📖 详细指南
|
| 75 |
|
| 76 |
+
### 支持的模型
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
| 模型 | 描述 | 特性 |
|
| 79 |
+
|------|------|------|
|
| 80 |
+
| `GLM-4.5` | 标准模型 | 通用对话,平衡性能 |
|
| 81 |
+
| `GLM-4.5-Thinking` | 思考模型 | 显示推理过程,透明度高 |
|
| 82 |
+
| `GLM-4.5-Search` | 搜索模型 | 实时网络搜索,信息更新 |
|
| 83 |
|
| 84 |
+
### Function Call 功能
|
| 85 |
|
| 86 |
```python
|
| 87 |
+
# 定义工具
|
| 88 |
+
tools = [{
|
| 89 |
+
"type": "function",
|
| 90 |
+
"function": {
|
| 91 |
+
"name": "get_weather",
|
| 92 |
+
"description": "获取天气信息",
|
| 93 |
+
"parameters": {
|
| 94 |
+
"type": "object",
|
| 95 |
+
"properties": {
|
| 96 |
+
"city": {"type": "string", "description": "城市名称"}
|
| 97 |
+
},
|
| 98 |
+
"required": ["city"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
}
|
| 100 |
}
|
| 101 |
+
}]
|
| 102 |
|
| 103 |
+
# 使用工具
|
| 104 |
response = client.chat.completions.create(
|
| 105 |
model="GLM-4.5",
|
| 106 |
+
messages=[{"role": "user", "content": "北京天气怎么样?"}],
|
| 107 |
tools=tools,
|
| 108 |
tool_choice="auto"
|
| 109 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
```
|
| 111 |
|
| 112 |
+
### 流式响应
|
| 113 |
|
| 114 |
+
```python
|
| 115 |
+
response = client.chat.completions.create(
|
| 116 |
+
model="GLM-4.5-Thinking",
|
| 117 |
+
messages=[{"role": "user", "content": "解释量子计算"}],
|
| 118 |
+
stream=True
|
| 119 |
+
)
|
| 120 |
|
| 121 |
+
for chunk in response:
|
| 122 |
+
content = chunk.choices[0].delta.content
|
| 123 |
+
reasoning = chunk.choices[0].delta.reasoning_content
|
| 124 |
+
|
| 125 |
+
if content:
|
| 126 |
+
print(content, end="")
|
| 127 |
+
if reasoning:
|
| 128 |
+
print(f"\n🤔 思考: {reasoning}\n")
|
| 129 |
```
|
| 130 |
|
| 131 |
+
## ⚙️ 配置说明
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
+
### 环境变量配置
|
| 134 |
|
| 135 |
+
| 变量名 | 默认值 | 说明 |
|
| 136 |
+
|--------|--------|------|
|
| 137 |
+
| `AUTH_TOKEN` | `sk-your-api-key` | 客户端认证密钥 |
|
| 138 |
+
| `API_ENDPOINT` | `https://chat.z.ai/api/chat/completions` | 上游 API 地址 |
|
| 139 |
+
| `LISTEN_PORT` | `8080` | 服务监听端口 |
|
| 140 |
+
| `DEBUG_LOGGING` | `true` | 调试日志开关 |
|
| 141 |
+
| `THINKING_PROCESSING` | `think` | 思考内容处理策略 |
|
| 142 |
+
| `ANONYMOUS_MODE` | `true` | 匿名模式开关 |
|
| 143 |
+
| `TOOL_SUPPORT` | `true` | Function Call 功能开关 |
|
| 144 |
|
| 145 |
+
### 思考内容处理策略
|
| 146 |
|
| 147 |
+
- `think` - 转换为 `<thinking>` 标签(OpenAI 兼容)
|
| 148 |
+
- `strip` - 移除思考内容
|
| 149 |
+
- `raw` - 保留原始格式
|
|
|
|
| 150 |
|
| 151 |
+
## 🎯 使用场景
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
+
### 1. AI 应用开发
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
```python
|
| 156 |
+
# 集成到现有应用
|
| 157 |
+
from openai import OpenAI
|
| 158 |
|
| 159 |
+
client = OpenAI(
|
| 160 |
+
base_url="http://localhost:8080/v1",
|
| 161 |
+
api_key="your-token"
|
| 162 |
+
)
|
| 163 |
|
| 164 |
+
# 智能客服
|
| 165 |
+
def chat_with_ai(message):
|
| 166 |
+
response = client.chat.completions.create(
|
| 167 |
+
model="GLM-4.5",
|
| 168 |
+
messages=[{"role": "user", "content": message}]
|
| 169 |
+
)
|
| 170 |
+
return response.choices[0].message.content
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
### 2. 多模型对比测试
|
| 174 |
+
|
| 175 |
+
```python
|
| 176 |
+
models = ["GLM-4.5", "GLM-4.5-Thinking", "GLM-4.5-Search"]
|
| 177 |
+
|
| 178 |
+
for model in models:
|
| 179 |
+
response = client.chat.completions.create(
|
| 180 |
+
model=model,
|
| 181 |
+
messages=[{"role": "user", "content": "什么是机器学习?"}]
|
| 182 |
+
)
|
| 183 |
+
print(f"\n=== {model} ===")
|
| 184 |
+
print(response.choices[0].message.content)
|
| 185 |
+
```
|
| 186 |
+
|
| 187 |
+
### 3. 工具调用集成
|
| 188 |
+
|
| 189 |
+
```python
|
| 190 |
+
# 结合外部 API
|
| 191 |
+
def call_external_api(tool_name, arguments):
|
| 192 |
+
# 执行实际工具调用
|
| 193 |
+
return result
|
| 194 |
+
|
| 195 |
+
# 处理工具调用
|
| 196 |
+
if response.choices[0].message.tool_calls:
|
| 197 |
+
for tool_call in response.choices[0].message.tool_calls:
|
| 198 |
+
result = call_external_api(
|
| 199 |
+
tool_call.function.name,
|
| 200 |
+
json.loads(tool_call.function.arguments)
|
| 201 |
+
)
|
| 202 |
+
# 将结果返回给模型继续对话
|
| 203 |
+
```
|
| 204 |
+
|
| 205 |
+
## ❓ 常见问题
|
| 206 |
+
|
| 207 |
+
**Q: 如何获取 AUTH_TOKEN?**
|
| 208 |
+
A: `AUTH_TOKEN` 为自己自定义的api key,在 `main.py` 的 `ServerConfig` 类中或通过环境变量配置,需要保证客户端与服务端一致。
|
| 209 |
|
| 210 |
+
**Q: 匿名模式是什么?**
|
| 211 |
+
A: 匿名模式使用临时 token,避免对话历史共享,保护隐私。
|
| 212 |
|
| 213 |
+
**Q: Function Call 如何工作?**
|
| 214 |
+
A: 通过智能提示注入实现,将工具定义转换为系统提示。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
|
| 216 |
+
**Q: 支持哪些 OpenAI 功能?**
|
| 217 |
+
A: 支持聊天完成、模型列表、流式响应、工具调用等核心功能。
|
| 218 |
|
| 219 |
+
**Q: 如何自定义配置?**
|
| 220 |
+
A: 通过环境变量或修改 `main.py` 中的 `ServerConfig` 类。
|
|
|
|
| 221 |
|
| 222 |
+
## 🏗️ 技术架构
|
| 223 |
+
|
| 224 |
+
```
|
| 225 |
+
┌─────────────┐ ┌─────────────┐ ┌─────────────┐
|
| 226 |
+
│ OpenAI │ │ Proxy │ │ Z.AI │
|
| 227 |
+
│ Client │────▶│ Server │────▶│ API │
|
| 228 |
+
│ │ │ │ │ │
|
| 229 |
+
└─────────────┘ └─────────────┘ └─────────────┘
|
| 230 |
+
```
|
| 231 |
|
| 232 |
+
- **FastAPI** - 高性能 Web 框架
|
| 233 |
+
- **Pydantic** - 数据验证和序列化
|
| 234 |
+
- **Uvicorn** - ASGI 服务器
|
| 235 |
+
- **Requests** - HTTP 客户端
|
| 236 |
|
| 237 |
+
## 🤝 贡献指南
|
|
|
|
|
|
|
| 238 |
|
| 239 |
+
我们欢迎所有形式的贡献!
|
| 240 |
+
请确保代码符合 PEP 8 规范,并更新相关文档。
|
| 241 |
|
| 242 |
+
## 📄 许可证
|
| 243 |
|
| 244 |
+
本项目采用 MIT 许可证 - 查看 [LICENSE](LICENSE) 文件了解详情。
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
+
## ⚠️ 免责声明
|
| 247 |
|
| 248 |
+
- 本项目与 Z.AI 官方无关
|
| 249 |
+
- 使用前请确保遵守 Z.AI 服务条款
|
| 250 |
+
- 请勿用于商业用途或违反使用条款的场景
|
| 251 |
+
- 项目仅供学习和研究使用
|
| 252 |
|
| 253 |
+
---
|
| 254 |
|
| 255 |
+
<div align="center">
|
| 256 |
+
Made with ❤️ by the community
|
| 257 |
+
</div>
|