Repository navigation
Expand file tree
/
Copy pathweb.py
More file actions
196 lines (162 loc) · 6.58 KB
/
Copy pathweb.py
File metadata and controls
196 lines (162 loc) · 6.58 KB
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
import os
import json
import asyncio
from contextlib import asynccontextmanager
from functools import lru_cache
from fastapi import FastAPI, Request, HTTPException, Depends, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.responses import JSONResponse, StreamingResponse
from starlette.background import BackgroundTask
from dotenv import load_dotenv
from log import log
from models import Model, ModelList, ChatCompletionRequest
from geminicli.client import GeminiCLIClient
from geminicli.web_routes import router as geminicli_router
load_dotenv()
# 认证配置
PASSWORD = os.getenv("PASSWORD", "pwd")
# 全局变量
geminicli_client = None
security = HTTPBearer()
def authenticate(credentials: HTTPAuthorizationCredentials = Depends(security)) -> str:
token = credentials.credentials
if token != PASSWORD:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="密码错误")
return token
async def fake_stream_response(request_data: ChatCompletionRequest):
"""处理假流式响应"""
async def stream_generator():
try:
# 发送心跳
heartbeat = {
"choices": [{
"index": 0,
"delta": {"role": "assistant", "content": ""},
"finish_reason": None
}]
}
yield f"data: {json.dumps(heartbeat)}\n\n".encode()
await asyncio.sleep(0.1)
# 调用后端
task = asyncio.create_task(geminicli_client.chat_completion(request_data))
# 等待完成,期间发送心跳
while not task.done():
await asyncio.sleep(5)
if not task.done():
yield f"data: {json.dumps(heartbeat)}\n\n".encode()
# 处理结果
result = await task
if hasattr(result, "body"):
body_str = result.body.decode() if isinstance(result.body, bytes) else str(result.body)
elif isinstance(result, dict):
body_str = json.dumps(result)
else:
body_str = str(result)
try:
response_data = json.loads(body_str)
if "choices" in response_data and response_data["choices"]:
content = response_data["choices"][0].get("message", {}).get("content", "")
content_chunk = {
"choices": [{
"index": 0,
"delta": {"role": "assistant", "content": content},
"finish_reason": "stop"
}]
}
yield f"data: {json.dumps(content_chunk)}\n\n".encode()
else:
yield f"data: {body_str}\n\n".encode()
except json.JSONDecodeError:
error_chunk = {
"choices": [{
"index": 0,
"delta": {"role": "assistant", "content": body_str},
"finish_reason": "stop"
}]
}
yield f"data: {json.dumps(error_chunk)}\n\n".encode()
yield "data: [DONE]\n\n".encode()
except Exception as e:
log('error', f"假流式错误: {e}")
yield "data: [DONE]\n\n".encode()
response = StreamingResponse(stream_generator(), media_type="text/event-stream")
response.background = BackgroundTask(lambda: asyncio.run(response.body_iterator.aclose()))
return response
@asynccontextmanager
async def lifespan(app: FastAPI):
global geminicli_client
log('info', "启动应用")
try:
geminicli_client = GeminiCLIClient()
await geminicli_client.initialize()
except Exception as e:
log('error', f"GeminiCLIClient 初始化失败: {e}")
geminicli_client = None
yield
if geminicli_client:
await geminicli_client.close()
app = FastAPI(lifespan=lifespan)
app.include_router(geminicli_router)
@lru_cache()
def get_model_list():
models = [
"gemini-2.5-pro-preview-06-05",
"gemini-2.5-pro-preview-06-05-假流式",
"gemini-2.5-pro",
"gemini-2.5-pro-假流式",
"gemini-2.5-pro-preview-05-06",
"gemini-2.5-pro-preview-05-06-假流式"
]
return ModelList(data=[Model(id=m) for m in models])
@app.get("/v1/models", response_model=ModelList)
async def list_models():
response = JSONResponse(content=get_model_list().model_dump())
return response
@app.post("/v1/chat/completions")
async def chat_completions(
request_data: ChatCompletionRequest,
credentials: HTTPAuthorizationCredentials = Depends(security)
):
token = authenticate(credentials)
# 健康检查
if (len(request_data.messages) == 1 and
getattr(request_data.messages[0], "role", None) == "user" and
getattr(request_data.messages[0], "content", None) == "Hi"):
return JSONResponse(content={
"choices": [{"delta": {"role": "assistant", "content": "公益站正常工作中"}}]
})
# 限制max_tokens
if getattr(request_data, "max_tokens", None) is not None and request_data.max_tokens > 65535:
request_data.max_tokens = 65535
# 过滤空消息
request_data.messages = [m for m in request_data.messages
if isinstance(getattr(m, "content", None), str) and
getattr(m, "content", "").strip()]
# 处理模型
model = request_data.model
if model.endswith("-假流式"):
real_model = model.replace("-假流式", "")
else:
real_model = model
request_data.model = real_model
# 假流式处理
if model.endswith("-假流式") and getattr(request_data, "stream", False):
request_data.stream = False
return await fake_stream_response(request_data, real_model)
# 调用GeminiCLI客户端
return await geminicli_client.chat_completion(request_data)
if __name__ == "__main__":
from hypercorn.asyncio import serve
from hypercorn.config import Config
print("启动配置:")
print(f" GeminiCLI客户端: 启用")
print("API地址 http://127.0.0.1:7861/v1")
print("OAuth认证管理地址 http://127.0.0.1:7861/auth")
print("默认密码 pwd")
print("使用PASSWORD环境变量来设置密码")
config = Config()
config.bind = ["0.0.0.0:7861"]
config.accesslog = "-"
config.errorlog = "-"
config.loglevel = "INFO"
asyncio.run(serve(app, config))