sanbo110 commited on
Commit
dcedadb
·
1 Parent(s): 35ab070

update sth at 2025-10-16 15:32:02

Browse files
app/admin/api.py CHANGED
@@ -194,7 +194,7 @@ async def save_config(request: Request):
194
  "",
195
  "# ========== 服务器配置 ==========",
196
  f"SERVICE_NAME={form_data.get('service_name', 'Z.AI2API')}",
197
- f"LISTEN_PORT={form_data.get('listen_port', '7860')}",
198
  f"DEBUG_LOGGING={'true' if 'debug_logging' in form_data else 'false'}",
199
  "",
200
  "# ========== 认证配置 ==========",
 
194
  "",
195
  "# ========== 服务器配置 ==========",
196
  f"SERVICE_NAME={form_data.get('service_name', 'Z.AI2API')}",
197
+ f"LISTEN_PORT={form_data.get('listen_port', '8080')}",
198
  f"DEBUG_LOGGING={'true' if 'debug_logging' in form_data else 'false'}",
199
  "",
200
  "# ========== 认证配置 ==========",
app/core/config.py CHANGED
@@ -54,7 +54,7 @@ class Settings(BaseSettings):
54
  }
55
 
56
  # Server Configuration
57
- LISTEN_PORT: int = int(os.getenv("LISTEN_PORT", "7860"))
58
  DEBUG_LOGGING: bool = os.getenv("DEBUG_LOGGING", "true").lower() == "true"
59
  SERVICE_NAME: str = os.getenv("SERVICE_NAME", "z-ai2api-server")
60
  ROOT_PATH: str = os.getenv("ROOT_PATH", "") # For Nginx reverse proxy path prefix, e.g., "/api" or "/path-prefix"
 
54
  }
55
 
56
  # Server Configuration
57
+ LISTEN_PORT: int = int(os.getenv("LISTEN_PORT", "8080"))
58
  DEBUG_LOGGING: bool = os.getenv("DEBUG_LOGGING", "true").lower() == "true"
59
  SERVICE_NAME: str = os.getenv("SERVICE_NAME", "z-ai2api-server")
60
  ROOT_PATH: str = os.getenv("ROOT_PATH", "") # For Nginx reverse proxy path prefix, e.g., "/api" or "/path-prefix"
app/core/openai.py CHANGED
@@ -93,8 +93,6 @@ async def handle_non_stream_response(stream_response, request: OpenAIRequest) ->
93
 
94
 
95
  @router.get("/v1/models")
96
- @router.get("/api/v1/models")
97
- @router.get("/hf/v1/models")
98
  async def list_models():
99
  """List available models from all providers"""
100
  try:
@@ -117,8 +115,6 @@ async def list_models():
117
 
118
 
119
  @router.post("/v1/chat/completions")
120
- @router.post("/api/v1/chat/completions")
121
- @router.post("/hf/v1/chat/completions")
122
  async def chat_completions(request: OpenAIRequest, authorization: str = Header(...)):
123
  """Handle chat completion requests with multi-provider architecture"""
124
  role = request.messages[0].role if request.messages else "unknown"
 
93
 
94
 
95
  @router.get("/v1/models")
 
 
96
  async def list_models():
97
  """List available models from all providers"""
98
  try:
 
115
 
116
 
117
  @router.post("/v1/chat/completions")
 
 
118
  async def chat_completions(request: OpenAIRequest, authorization: str = Header(...)):
119
  """Handle chat completion requests with multi-provider architecture"""
120
  role = request.messages[0].role if request.messages else "unknown"
deploy/.env.example CHANGED
@@ -32,4 +32,4 @@ TOKEN_RECOVERY_TIMEOUT=300
32
 
33
  # 服务配置
34
  SERVICE_NAME=Z.AI_API_Server
35
- LISTEN_PORT=7860
 
32
 
33
  # 服务配置
34
  SERVICE_NAME=Z.AI_API_Server
35
+ LISTEN_PORT=8080
deploy/Dockerfile CHANGED
@@ -18,7 +18,7 @@ COPY . .
18
  ENV DB_PATH=/app/data/tokens.db
19
 
20
  # Expose port
21
- EXPOSE 7860
22
 
23
  # Run the application
24
  CMD ["python", "main.py"]
 
18
  ENV DB_PATH=/app/data/tokens.db
19
 
20
  # Expose port
21
+ EXPOSE 8080
22
 
23
  # Run the application
24
  CMD ["python", "main.py"]
deploy/NGINX_SETUP.md CHANGED
@@ -37,7 +37,7 @@ server {
37
 
38
  location /ai2api {
39
  # 代理到后端服务
40
- proxy_pass http://127.0.0.1:7860;
41
 
42
  # 传递原始请求信息
43
  proxy_set_header Host $host;
@@ -69,10 +69,10 @@ services:
69
  image: z-ai2api:latest
70
  environment:
71
  - ROOT_PATH=/ai2api
72
- - LISTEN_PORT=7860
73
  # ... 其他环境变量
74
  ports:
75
- - "7860:7860"
76
  ```
77
 
78
  ### 4. 重启服务
@@ -110,7 +110,7 @@ ROOT_PATH=/api
110
  **Nginx 配置**:
111
  ```nginx
112
  location /api {
113
- proxy_pass http://127.0.0.1:7860;
114
  # ... 其他配置
115
  }
116
  ```
@@ -127,7 +127,7 @@ ROOT_PATH=
127
  **Nginx 配置**:
128
  ```nginx
129
  location / {
130
- proxy_pass http://127.0.0.1:7860;
131
  # ... 其他配置
132
  }
133
  ```
@@ -144,7 +144,7 @@ ROOT_PATH=/services/ai/chat
144
  **Nginx 配置**:
145
  ```nginx
146
  location /services/ai/chat {
147
- proxy_pass http://127.0.0.1:7860;
148
  # ... 其他配置
149
  }
150
  ```
@@ -246,7 +246,7 @@ server {
246
  ssl_certificate_key /path/to/key.pem;
247
 
248
  location /ai2api {
249
- proxy_pass http://127.0.0.1:7860;
250
  proxy_set_header X-Forwarded-Proto https;
251
  # ... 其他配置
252
  }
@@ -257,7 +257,7 @@ server {
257
 
258
  ```nginx
259
  upstream ai2api_backend {
260
- server 127.0.0.1:7860;
261
  server 127.0.0.1:8081;
262
  server 127.0.0.1:8082;
263
  }
 
37
 
38
  location /ai2api {
39
  # 代理到后端服务
40
+ proxy_pass http://127.0.0.1:8080;
41
 
42
  # 传递原始请求信息
43
  proxy_set_header Host $host;
 
69
  image: z-ai2api:latest
70
  environment:
71
  - ROOT_PATH=/ai2api
72
+ - LISTEN_PORT=8080
73
  # ... 其他环境变量
74
  ports:
75
+ - "8080:8080"
76
  ```
77
 
78
  ### 4. 重启服务
 
110
  **Nginx 配置**:
111
  ```nginx
112
  location /api {
113
+ proxy_pass http://127.0.0.1:8080;
114
  # ... 其他配置
115
  }
116
  ```
 
127
  **Nginx 配置**:
128
  ```nginx
129
  location / {
130
+ proxy_pass http://127.0.0.1:8080;
131
  # ... 其他配置
132
  }
133
  ```
 
144
  **Nginx 配置**:
145
  ```nginx
146
  location /services/ai/chat {
147
+ proxy_pass http://127.0.0.1:8080;
148
  # ... 其他配置
149
  }
150
  ```
 
246
  ssl_certificate_key /path/to/key.pem;
247
 
248
  location /ai2api {
249
+ proxy_pass http://127.0.0.1:8080;
250
  proxy_set_header X-Forwarded-Proto https;
251
  # ... 其他配置
252
  }
 
257
 
258
  ```nginx
259
  upstream ai2api_backend {
260
+ server 127.0.0.1:8080;
261
  server 127.0.0.1:8081;
262
  server 127.0.0.1:8082;
263
  }
deploy/README_DOCKER.md CHANGED
@@ -16,7 +16,7 @@ mkdir -p data logs
16
  # 快速启动
17
  docker run -d \
18
  --name z-ai-api-server \
19
- -p 7860:7860 \
20
  -e ADMIN_PASSWORD=admin123 \
21
  -e AUTH_TOKEN=sk-your-api-key \
22
  -e ANONYMOUS_MODE=true \
@@ -48,7 +48,7 @@ docker compose up -d
48
  docker compose logs -f api-server
49
  ```
50
 
51
- 服务将在 `http://localhost:7860` 启动。
52
 
53
  ## 架构说明
54
 
@@ -222,10 +222,10 @@ docker compose config
222
 
223
  ### 端口冲突
224
 
225
- 如端口 7860 被占用,修改 `docker-compose.yml`:
226
  ```yaml
227
  ports:
228
- - "8081:7860" # 映射到宿主机 8081 端口
229
  ```
230
 
231
  ### 健康检查失败
@@ -235,7 +235,7 @@ ports:
235
  docker compose ps
236
 
237
  # 手动测试接口
238
- curl http://localhost:7860/v1/models
239
 
240
  # 进入容器排查
241
  docker exec -it z-ai-api-server bash
@@ -245,11 +245,11 @@ docker exec -it z-ai-api-server bash
245
 
246
  | 端点 | 地址 | 说明 |
247
  |------|------|------|
248
- | API 根路径 | `http://localhost:7860` | OpenAI 兼容 API |
249
- | 模型列表 | `http://localhost:7860/v1/models` | 获取可用模型 |
250
- | 管理后台 | `http://localhost:7860/admin` | Web 管理界面 |
251
- | API 文档 | `http://localhost:7860/docs` | OpenAPI/Swagger 文档 |
252
- | 健康检查 | `http://localhost:7860/v1/models` | 服务健康状态 |
253
 
254
  ## 高级配置
255
 
 
16
  # 快速启动
17
  docker run -d \
18
  --name z-ai-api-server \
19
+ -p 8080:8080 \
20
  -e ADMIN_PASSWORD=admin123 \
21
  -e AUTH_TOKEN=sk-your-api-key \
22
  -e ANONYMOUS_MODE=true \
 
48
  docker compose logs -f api-server
49
  ```
50
 
51
+ 服务将在 `http://localhost:8080` 启动。
52
 
53
  ## 架构说明
54
 
 
222
 
223
  ### 端口冲突
224
 
225
+ 如端口 8080 被占用,修改 `docker-compose.yml`:
226
  ```yaml
227
  ports:
228
+ - "8081:8080" # 映射到宿主机 8081 端口
229
  ```
230
 
231
  ### 健康检查失败
 
235
  docker compose ps
236
 
237
  # 手动测试接口
238
+ curl http://localhost:8080/v1/models
239
 
240
  # 进入容器排查
241
  docker exec -it z-ai-api-server bash
 
245
 
246
  | 端点 | 地址 | 说明 |
247
  |------|------|------|
248
+ | API 根路径 | `http://localhost:8080` | OpenAI 兼容 API |
249
+ | 模型列表 | `http://localhost:8080/v1/models` | 获取可用模型 |
250
+ | 管理后台 | `http://localhost:8080/admin` | Web 管理界面 |
251
+ | API 文档 | `http://localhost:8080/docs` | OpenAPI/Swagger 文档 |
252
+ | 健康检查 | `http://localhost:8080/v1/models` | 服务健康状态 |
253
 
254
  ## 高级配置
255
 
deploy/docker-compose.yml CHANGED
@@ -5,7 +5,7 @@ services:
5
  dockerfile: deploy/Dockerfile
6
  container_name: z-ai-api-server
7
  ports:
8
- - "7860:7860"
9
  volumes:
10
  # 数据库持久化存储
11
  - ./data:/app/data
@@ -29,7 +29,7 @@ services:
29
  - DB_PATH=/app/data/tokens.db
30
  restart: unless-stopped
31
  healthcheck:
32
- test: ["CMD", "curl", "-f", "http://localhost:7860/v1/models"]
33
  interval: 30s
34
  timeout: 10s
35
  retries: 3
 
5
  dockerfile: deploy/Dockerfile
6
  container_name: z-ai-api-server
7
  ports:
8
+ - "8080:8080"
9
  volumes:
10
  # 数据库持久化存储
11
  - ./data:/app/data
 
29
  - DB_PATH=/app/data/tokens.db
30
  restart: unless-stopped
31
  healthcheck:
32
+ test: ["CMD", "curl", "-f", "http://localhost:8080/v1/models"]
33
  interval: 30s
34
  timeout: 10s
35
  retries: 3
deploy/nginx.conf.example CHANGED
@@ -12,7 +12,7 @@ server {
12
  rewrite ^(/ai2api)$ $1/ permanent;
13
 
14
  # Proxy to the backend service
15
- proxy_pass http://127.0.0.1:7860;
16
 
17
  # Pass original host and IP information
18
  proxy_set_header Host $host;
@@ -47,7 +47,7 @@ server {
47
 
48
  location /api/chat {
49
  # Proxy configuration
50
- proxy_pass http://127.0.0.1:7860;
51
 
52
  # Headers
53
  proxy_set_header Host $host;
@@ -75,7 +75,7 @@ server {
75
  ssl_certificate_key /path/to/key.pem;
76
 
77
  location /ai2api {
78
- proxy_pass http://127.0.0.1:7860;
79
 
80
  proxy_set_header Host $host;
81
  proxy_set_header X-Real-IP $remote_addr;
@@ -100,7 +100,7 @@ server {
100
  # Example 4: Load balancing with multiple backend instances
101
  upstream ai2api_backend {
102
  # Round-robin by default
103
- server 127.0.0.1:7860;
104
  server 127.0.0.1:8081;
105
  server 127.0.0.1:8082;
106
 
@@ -148,7 +148,7 @@ server {
148
  #
149
  # 4. For Docker deployments, make sure to:
150
  # - Add ROOT_PATH to docker-compose.yml environment variables
151
- # - Expose the container port (7860 by default)
152
  #
153
  # 5. Common issues:
154
  # - 404 errors: Check that ROOT_PATH matches the Nginx location path exactly
 
12
  rewrite ^(/ai2api)$ $1/ permanent;
13
 
14
  # Proxy to the backend service
15
+ proxy_pass http://127.0.0.1:8080;
16
 
17
  # Pass original host and IP information
18
  proxy_set_header Host $host;
 
47
 
48
  location /api/chat {
49
  # Proxy configuration
50
+ proxy_pass http://127.0.0.1:8080;
51
 
52
  # Headers
53
  proxy_set_header Host $host;
 
75
  ssl_certificate_key /path/to/key.pem;
76
 
77
  location /ai2api {
78
+ proxy_pass http://127.0.0.1:8080;
79
 
80
  proxy_set_header Host $host;
81
  proxy_set_header X-Real-IP $remote_addr;
 
100
  # Example 4: Load balancing with multiple backend instances
101
  upstream ai2api_backend {
102
  # Round-robin by default
103
+ server 127.0.0.1:8080;
104
  server 127.0.0.1:8081;
105
  server 127.0.0.1:8082;
106
 
 
148
  #
149
  # 4. For Docker deployments, make sure to:
150
  # - Add ROOT_PATH to docker-compose.yml environment variables
151
+ # - Expose the container port (8080 by default)
152
  #
153
  # 5. Common issues:
154
  # - 404 errors: Check that ROOT_PATH matches the Nginx location path exactly
nginx.conf ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Nginx reverse proxy configuration example for Z.AI2API
2
+ # This example shows how to deploy the service behind Nginx with a custom path prefix
3
+
4
+ # Example 1: Deploy at http://your-domain.com/ai2api
5
+ server {
6
+ listen 80;
7
+ server_name your-domain.com;
8
+
9
+ # Forward requests with /ai2api prefix to the backend service
10
+ location /ai2api {
11
+ # Remove trailing slash redirect (optional, but recommended)
12
+ rewrite ^(/ai2api)$ $1/ permanent;
13
+
14
+ # Proxy to the backend service
15
+ proxy_pass http://127.0.0.1:7860;
16
+
17
+ # Pass original host and IP information
18
+ proxy_set_header Host $host;
19
+ proxy_set_header X-Real-IP $remote_addr;
20
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
21
+ proxy_set_header X-Forwarded-Proto $scheme;
22
+
23
+ # IMPORTANT: Tell the backend about the path prefix
24
+ # This ensures all generated URLs include the prefix
25
+ proxy_set_header X-Forwarded-Prefix /ai2api;
26
+
27
+ # WebSocket and SSE support (for streaming responses)
28
+ proxy_http_version 1.1;
29
+ proxy_set_header Upgrade $http_upgrade;
30
+ proxy_set_header Connection "upgrade";
31
+
32
+ # Disable buffering for streaming responses
33
+ proxy_buffering off;
34
+ proxy_cache off;
35
+
36
+ # Timeout settings (adjust as needed)
37
+ proxy_connect_timeout 60s;
38
+ proxy_send_timeout 300s;
39
+ proxy_read_timeout 300s;
40
+ }
41
+ }
42
+
43
+ # Example 2: Deploy at http://your-domain.com/api/chat
44
+ server {
45
+ listen 80;
46
+ server_name example.com;
47
+
48
+ location /api/chat {
49
+ # Proxy configuration
50
+ proxy_pass http://127.0.0.1:7860;
51
+
52
+ # Headers
53
+ proxy_set_header Host $host;
54
+ proxy_set_header X-Real-IP $remote_addr;
55
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
56
+ proxy_set_header X-Forwarded-Proto $scheme;
57
+ proxy_set_header X-Forwarded-Prefix /api/chat;
58
+
59
+ # SSE/WebSocket support
60
+ proxy_http_version 1.1;
61
+ proxy_set_header Upgrade $http_upgrade;
62
+ proxy_set_header Connection "upgrade";
63
+ proxy_buffering off;
64
+ proxy_cache off;
65
+ }
66
+ }
67
+
68
+ # Example 3: Deploy with SSL (HTTPS)
69
+ server {
70
+ listen 443 ssl http2;
71
+ server_name secure.example.com;
72
+
73
+ # SSL configuration
74
+ ssl_certificate /path/to/cert.pem;
75
+ ssl_certificate_key /path/to/key.pem;
76
+
77
+ location /ai2api {
78
+ proxy_pass http://127.0.0.1:7860;
79
+
80
+ proxy_set_header Host $host;
81
+ proxy_set_header X-Real-IP $remote_addr;
82
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
83
+ proxy_set_header X-Forwarded-Proto https;
84
+ proxy_set_header X-Forwarded-Prefix /ai2api;
85
+
86
+ # SSE/WebSocket support
87
+ proxy_http_version 1.1;
88
+ proxy_set_header Upgrade $http_upgrade;
89
+ proxy_set_header Connection "upgrade";
90
+ proxy_buffering off;
91
+ proxy_cache off;
92
+
93
+ # Security headers (optional)
94
+ add_header X-Content-Type-Options nosniff;
95
+ add_header X-Frame-Options DENY;
96
+ add_header X-XSS-Protection "1; mode=block";
97
+ }
98
+ }
99
+
100
+ # Example 4: Load balancing with multiple backend instances
101
+ upstream ai2api_backend {
102
+ # Round-robin by default
103
+ server 127.0.0.1:7860;
104
+ server 127.0.0.1:8081;
105
+ server 127.0.0.1:8082;
106
+
107
+ # Or use least connections
108
+ # least_conn;
109
+
110
+ # Or use IP hash for session persistence
111
+ # ip_hash;
112
+ }
113
+
114
+ server {
115
+ listen 80;
116
+ server_name loadbalanced.example.com;
117
+
118
+ location /ai2api {
119
+ proxy_pass http://ai2api_backend;
120
+
121
+ proxy_set_header Host $host;
122
+ proxy_set_header X-Real-IP $remote_addr;
123
+ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
124
+ proxy_set_header X-Forwarded-Proto $scheme;
125
+ proxy_set_header X-Forwarded-Prefix /ai2api;
126
+
127
+ proxy_http_version 1.1;
128
+ proxy_set_header Upgrade $http_upgrade;
129
+ proxy_set_header Connection "upgrade";
130
+ proxy_buffering off;
131
+ proxy_cache off;
132
+ }
133
+ }
134
+
135
+ # Important Notes:
136
+ #
137
+ # 1. Set ROOT_PATH in your .env file to match the Nginx location path:
138
+ # ROOT_PATH=/ai2api
139
+ #
140
+ # 2. Restart both Nginx and the application after configuration changes:
141
+ # sudo systemctl reload nginx
142
+ # docker-compose restart (or restart your application)
143
+ #
144
+ # 3. Access URLs will include the prefix:
145
+ # - Admin panel: http://your-domain.com/ai2api/admin/login
146
+ # - API endpoint: http://your-domain.com/ai2api/v1/chat/completions
147
+ # - Health check: http://your-domain.com/ai2api/v1/models
148
+ #
149
+ # 4. For Docker deployments, make sure to:
150
+ # - Add ROOT_PATH to docker-compose.yml environment variables
151
+ # - Expose the container port (7860 by default)
152
+ #
153
+ # 5. Common issues:
154
+ # - 404 errors: Check that ROOT_PATH matches the Nginx location path exactly
155
+ # - CORS errors: Verify proxy headers are set correctly
156
+ # - Streaming not working: Ensure proxy_buffering is off
157
+ # - Admin panel CSS/JS not loading: Confirm static files are served with the prefix