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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import asyncio
|
| 2 |
+
import json
|
| 3 |
+
import sys
|
| 4 |
+
import uuid
|
| 5 |
+
import base64
|
| 6 |
+
import re
|
| 7 |
+
import os
|
| 8 |
+
import argparse
|
| 9 |
+
import time
|
| 10 |
+
from datetime import datetime, timezone
|
| 11 |
+
from typing import List, Optional
|
| 12 |
+
|
| 13 |
+
import httpx
|
| 14 |
+
import uvicorn
|
| 15 |
+
from fastapi import (
|
| 16 |
+
BackgroundTasks,
|
| 17 |
+
FastAPI,
|
| 18 |
+
HTTPException,
|
| 19 |
+
Request,
|
| 20 |
+
Response,
|
| 21 |
+
status,
|
| 22 |
+
)
|
| 23 |
+
from fastapi.responses import HTMLResponse, JSONResponse, StreamingResponse
|
| 24 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 25 |
+
from fastapi.staticfiles import StaticFiles
|
| 26 |
+
|
| 27 |
+
from bearer_token import BearerTokenGenerator
|
| 28 |
+
|
| 29 |
+
from fastapi import Depends, HTTPException, Security
|
| 30 |
+
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
| 31 |
+
|
| 32 |
+
# 模型列表
|
| 33 |
+
MODELS = ["gpt-4o", "gpt-4o-mini", "claude-3-5-sonnet", "claude"]
|
| 34 |
+
|
| 35 |
+
# 默认端口
|
| 36 |
+
INITIAL_PORT = 3000
|
| 37 |
+
|
| 38 |
+
# 外部API的URL
|
| 39 |
+
EXTERNAL_API_URL = "https://api.chaton.ai/chats/stream"
|
| 40 |
+
|
| 41 |
+
# 初始化FastAPI应用
|
| 42 |
+
app = FastAPI()
|
| 43 |
+
|
| 44 |
+
# 添加CORS中间件
|
| 45 |
+
app.add_middleware(
|
| 46 |
+
CORSMiddleware,
|
| 47 |
+
allow_origins=["*"], # 允许所有来源
|
| 48 |
+
allow_credentials=True,
|
| 49 |
+
allow_methods=["GET", "POST", "OPTIONS"], # 允许GET, POST, OPTIONS方法
|
| 50 |
+
allow_headers=["Content-Type", "Authorization"], # 允许的头部
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# 挂载静态文件路由以提供 images 目录的内容
|
| 54 |
+
app.mount("/images", StaticFiles(directory="images"), name="images")
|
| 55 |
+
|
| 56 |
+
# 辅助函数
|
| 57 |
+
def send_error_response(message: str, status_code: int = 400):
|
| 58 |
+
"""构建错误响应,并确保包含CORS头"""
|
| 59 |
+
error_json = {"error": message}
|
| 60 |
+
headers = {
|
| 61 |
+
"Access-Control-Allow-Origin": "*",
|
| 62 |
+
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
|
| 63 |
+
"Access-Control-Allow-Headers": "Content-Type, Authorization",
|
| 64 |
+
}
|
| 65 |
+
return JSONResponse(status_code=status_code, content=error_json, headers=headers)
|
| 66 |
+
|
| 67 |
+
def extract_path_from_markdown(markdown: str) -> Optional[str]:
|
| 68 |
+
"""
|
| 69 |
+
提取 Markdown 图片链接中的路径,匹配以 https://spc.unk/ 开头的 URL
|
| 70 |
+
"""
|
| 71 |
+
pattern = re.compile(r'!\[.*?\]\(https://spc\.unk/(.*?)\)')
|
| 72 |
+
match = pattern.search(markdown)
|
| 73 |
+
if match:
|
| 74 |
+
return match.group(1)
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
async def fetch_get_url_from_storage(storage_url: str) -> Optional[str]:
|
| 78 |
+
"""
|
| 79 |
+
从 storage URL 获取 JSON 并提取 getUrl
|
| 80 |
+
"""
|
| 81 |
+
async with httpx.AsyncClient() as client:
|
| 82 |
+
try:
|
| 83 |
+
response = await client.get(storage_url)
|
| 84 |
+
if response.status_code != 200:
|
| 85 |
+
print(f"获取 storage URL 失败,状态码: {response.status_code}")
|
| 86 |
+
return None
|
| 87 |
+
json_response = response.json()
|
| 88 |
+
return json_response.get("getUrl")
|
| 89 |
+
except Exception as e:
|
| 90 |
+
print(f"Error fetching getUrl from storage: {e}")
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
async def download_image(image_url: str) -> Optional[bytes]:
|
| 94 |
+
"""
|
| 95 |
+
下载图像
|
| 96 |
+
"""
|
| 97 |
+
async with httpx.AsyncClient() as client:
|
| 98 |
+
try:
|
| 99 |
+
response = await client.get(image_url)
|
| 100 |
+
if response.status_code == 200:
|
| 101 |
+
return response.content
|
| 102 |
+
else:
|
| 103 |
+
print(f"下载图像失败,状态码: {response.status_code}")
|
| 104 |
+
return None
|
| 105 |
+
except Exception as e:
|
| 106 |
+
print(f"Error downloading image: {e}")
|
| 107 |
+
return None
|
| 108 |
+
|
| 109 |
+
def save_base64_image(base64_str: str, images_dir: str = "images") -> str:
|
| 110 |
+
"""
|
| 111 |
+
将Base64编码的图片保存到images目录,返回文件名
|
| 112 |
+
"""
|
| 113 |
+
if not os.path.exists(images_dir):
|
| 114 |
+
os.makedirs(images_dir)
|
| 115 |
+
image_data = base64.b64decode(base64_str)
|
| 116 |
+
filename = f"{uuid.uuid4()}.png" # 默认保存为png格式
|
| 117 |
+
file_path = os.path.join(images_dir, filename)
|
| 118 |
+
with open(file_path, "wb") as f:
|
| 119 |
+
f.write(image_data)
|
| 120 |
+
return filename
|
| 121 |
+
|
| 122 |
+
def is_base64_image(url: str) -> bool:
|
| 123 |
+
"""
|
| 124 |
+
判断URL是否为Base64编码的图片
|
| 125 |
+
"""
|
| 126 |
+
return url.startswith("data:image/")
|
| 127 |
+
|
| 128 |
+
# 添加 HTTPBearer 实例
|
| 129 |
+
security = HTTPBearer()
|
| 130 |
+
|
| 131 |
+
# 添加 API_KEY 验证函数
|
| 132 |
+
def verify_api_key(credentials: HTTPAuthorizationCredentials = Security(security)):
|
| 133 |
+
api_key = os.environ.get("API_KEY")
|
| 134 |
+
if api_key is None:
|
| 135 |
+
raise HTTPException(status_code=500, detail="API_KEY not set in environment variables")
|
| 136 |
+
if credentials.credentials != api_key:
|
| 137 |
+
raise HTTPException(status_code=401, detail="Invalid API key")
|
| 138 |
+
return credentials.credentials
|
| 139 |
+
|
| 140 |
+
# 根路径GET请求处理
|
| 141 |
+
@app.get("/")
|
| 142 |
+
async def root():
|
| 143 |
+
return JSONResponse(content={
|
| 144 |
+
"service": "AI Chat Completion Proxy",
|
| 145 |
+
"usage": {
|
| 146 |
+
"endpoint": "/ai/v1/chat/completions",
|
| 147 |
+
"method": "POST",
|
| 148 |
+
"headers": {
|
| 149 |
+
"Content-Type": "application/json",
|
| 150 |
+
"Authorization": "Bearer YOUR_API_KEY"
|
| 151 |
+
},
|
| 152 |
+
"body": {
|
| 153 |
+
"model": "One of: " + ", ".join(MODELS),
|
| 154 |
+
"messages": [
|
| 155 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 156 |
+
{"role": "user", "content": "Hello, who are you?"}
|
| 157 |
+
],
|
| 158 |
+
"stream": False,
|
| 159 |
+
"temperature": 0.7,
|
| 160 |
+
"max_tokens": 8000
|
| 161 |
+
}
|
| 162 |
+
},
|
| 163 |
+
"availableModels": MODELS,
|
| 164 |
+
"endpoints": {
|
| 165 |
+
"/ai/v1/chat/completions": "Chat completion endpoint",
|
| 166 |
+
"/ai/v1/images/generations": "Image generation endpoint",
|
| 167 |
+
"/ai/v1/models": "List available models"
|
| 168 |
+
},
|
| 169 |
+
"note": "Replace YOUR_API_KEY with your actual API key."
|
| 170 |
+
})
|
| 171 |
+
|
| 172 |
+
# 返回模型列表
|
| 173 |
+
@app.get("/ai/v1/models")
|
| 174 |
+
async def list_models():
|
| 175 |
+
"""返回可用模型列表。"""
|
| 176 |
+
models = [
|
| 177 |
+
{
|
| 178 |
+
"id": model,
|
| 179 |
+
"object": "model",
|
| 180 |
+
"created": int(time.time()),
|
| 181 |
+
"owned_by": "chaton",
|
| 182 |
+
"permission": [],
|
| 183 |
+
"root": model,
|
| 184 |
+
"parent": None,
|
| 185 |
+
} for model in MODELS
|
| 186 |
+
]
|
| 187 |
+
return JSONResponse(content={
|
| 188 |
+
"object": "list",
|
| 189 |
+
"data": models
|
| 190 |
+
})
|
| 191 |
+
|
| 192 |
+
# 聊天完成处理
|
| 193 |
+
@app.post("/ai/v1/chat/completions")
|
| 194 |
+
async def chat_completions(request: Request, background_tasks: BackgroundTasks, api_key: str = Depends(verify_api_key)):
|
| 195 |
+
"""
|
| 196 |
+
处理聊天完成请求
|
| 197 |
+
"""
|
| 198 |
+
try:
|
| 199 |
+
request_body = await request.json()
|
| 200 |
+
except json.JSONDecodeError:
|
| 201 |
+
raise HTTPException(status_code=400, detail="Invalid JSON")
|
| 202 |
+
|
| 203 |
+
# 打印接收到的请求
|
| 204 |
+
print("Received Completion JSON:", json.dumps(request_body, ensure_ascii=False))
|
| 205 |
+
|
| 206 |
+
# 处理消息内容
|
| 207 |
+
messages = request_body.get("messages", [])
|
| 208 |
+
temperature = request_body.get("temperature", 1.0)
|
| 209 |
+
top_p = request_body.get("top_p", 1.0)
|
| 210 |
+
max_tokens = request_body.get("max_tokens", 8000)
|
| 211 |
+
model = request_body.get("model", "gpt-4o")
|
| 212 |
+
is_stream = request_body.get("stream", False) # 获取 stream 字段
|
| 213 |
+
|
| 214 |
+
has_image = False
|
| 215 |
+
has_text = False
|
| 216 |
+
|
| 217 |
+
# 清理和提取消息内容
|
| 218 |
+
cleaned_messages = []
|
| 219 |
+
for message in messages:
|
| 220 |
+
content = message.get("content", "")
|
| 221 |
+
if isinstance(content, list):
|
| 222 |
+
text_parts = []
|
| 223 |
+
images = []
|
| 224 |
+
for item in content:
|
| 225 |
+
if "text" in item:
|
| 226 |
+
text_parts.append(item.get("text", ""))
|
| 227 |
+
elif "image_url" in item:
|
| 228 |
+
has_image = True
|
| 229 |
+
image_info = item.get("image_url", {})
|
| 230 |
+
url = image_info.get("url", "")
|
| 231 |
+
if is_base64_image(url):
|
| 232 |
+
# 解码并保存图片
|
| 233 |
+
base64_str = url.split(",")[1]
|
| 234 |
+
filename = save_base64_image(base64_str)
|
| 235 |
+
base_url = app.state.base_url
|
| 236 |
+
image_url = f"{base_url}/images/{filename}"
|
| 237 |
+
images.append({"data": image_url})
|
| 238 |
+
else:
|
| 239 |
+
images.append({"data": url})
|
| 240 |
+
extracted_content = " ".join(text_parts).strip()
|
| 241 |
+
if extracted_content:
|
| 242 |
+
has_text = True
|
| 243 |
+
message["content"] = extracted_content
|
| 244 |
+
if images:
|
| 245 |
+
message["images"] = images
|
| 246 |
+
cleaned_messages.append(message)
|
| 247 |
+
print("Extracted:", extracted_content)
|
| 248 |
+
else:
|
| 249 |
+
if images:
|
| 250 |
+
has_image = True
|
| 251 |
+
message["content"] = ""
|
| 252 |
+
message["images"] = images
|
| 253 |
+
cleaned_messages.append(message)
|
| 254 |
+
print("Extracted image only.")
|
| 255 |
+
else:
|
| 256 |
+
print("Deleted message with empty content.")
|
| 257 |
+
elif isinstance(content, str):
|
| 258 |
+
content_str = content.strip()
|
| 259 |
+
if content_str:
|
| 260 |
+
has_text = True
|
| 261 |
+
message["content"] = content_str
|
| 262 |
+
cleaned_messages.append(message)
|
| 263 |
+
print("Retained content:", content_str)
|
| 264 |
+
else:
|
| 265 |
+
print("Deleted message with empty content.")
|
| 266 |
+
else:
|
| 267 |
+
print("Deleted non-expected type of content message.")
|
| 268 |
+
|
| 269 |
+
if not cleaned_messages:
|
| 270 |
+
raise HTTPException(status_code=400, detail="所有消息的内容均为空。")
|
| 271 |
+
|
| 272 |
+
# 验证模型
|
| 273 |
+
if model not in MODELS:
|
| 274 |
+
model = "gpt-4o"
|
| 275 |
+
|
| 276 |
+
# 构建新的请求JSON
|
| 277 |
+
new_request_json = {
|
| 278 |
+
"function_image_gen": False,
|
| 279 |
+
"function_web_search": True,
|
| 280 |
+
"max_tokens": max_tokens,
|
| 281 |
+
"model": model,
|
| 282 |
+
"source": "chat/free",
|
| 283 |
+
"temperature": temperature,
|
| 284 |
+
"top_p": top_p,
|
| 285 |
+
"messages": cleaned_messages,
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
modified_request_body = json.dumps(new_request_json, ensure_ascii=False)
|
| 289 |
+
print("Modified Request JSON:", modified_request_body)
|
| 290 |
+
|
| 291 |
+
# 获取Bearer Token
|
| 292 |
+
tmp_token = BearerTokenGenerator.get_bearer(modified_request_body)
|
| 293 |
+
if not tmp_token:
|
| 294 |
+
raise HTTPException(status_code=500, detail="无法生成 Bearer Token")
|
| 295 |
+
|
| 296 |
+
bearer_token, formatted_date = tmp_token
|
| 297 |
+
|
| 298 |
+
headers = {
|
| 299 |
+
"Date": formatted_date,
|
| 300 |
+
"Client-time-zone": "-05:00",
|
| 301 |
+
"Authorization": bearer_token,
|
| 302 |
+
"User-Agent": "ChatOn_Android/1.53.502",
|
| 303 |
+
"Accept-Language": "en-US",
|
| 304 |
+
"X-Cl-Options": "hb",
|
| 305 |
+
"Content-Type": "application/json; charset=UTF-8",
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
if is_stream:
|
| 309 |
+
# 流式响应处理
|
| 310 |
+
async def event_generator():
|
| 311 |
+
async with httpx.AsyncClient(timeout=None) as client_stream:
|
| 312 |
+
try:
|
| 313 |
+
async with client_stream.stream("POST", EXTERNAL_API_URL, headers=headers, content=modified_request_body) as streamed_response:
|
| 314 |
+
async for line in streamed_response.aiter_lines():
|
| 315 |
+
if line.startswith("data: "):
|
| 316 |
+
data = line[6:].strip()
|
| 317 |
+
if data == "[DONE]":
|
| 318 |
+
# 通知客户端流结束
|
| 319 |
+
yield "data: [DONE]\n\n"
|
| 320 |
+
break
|
| 321 |
+
try:
|
| 322 |
+
sse_json = json.loads(data)
|
| 323 |
+
if "choices" in sse_json:
|
| 324 |
+
for choice in sse_json["choices"]:
|
| 325 |
+
delta = choice.get("delta", {})
|
| 326 |
+
content = delta.get("content")
|
| 327 |
+
if content:
|
| 328 |
+
new_sse_json = {
|
| 329 |
+
"choices": [
|
| 330 |
+
{
|
| 331 |
+
"index": choice.get("index", 0),
|
| 332 |
+
"delta": {"content": content},
|
| 333 |
+
}
|
| 334 |
+
],
|
| 335 |
+
"created": sse_json.get(
|
| 336 |
+
"created", int(datetime.now(timezone.utc).timestamp())
|
| 337 |
+
),
|
| 338 |
+
"id": sse_json.get(
|
| 339 |
+
"id", str(uuid.uuid4())
|
| 340 |
+
),
|
| 341 |
+
"model": sse_json.get("model", "gpt-4o"),
|
| 342 |
+
"system_fingerprint": f"fp_{uuid.uuid4().hex[:12]}",
|
| 343 |
+
}
|
| 344 |
+
new_sse_line = f"data: {json.dumps(new_sse_json, ensure_ascii=False)}\n\n"
|
| 345 |
+
yield new_sse_line
|
| 346 |
+
except json.JSONDecodeError:
|
| 347 |
+
print("JSON解析错误")
|
| 348 |
+
continue
|
| 349 |
+
except httpx.RequestError as exc:
|
| 350 |
+
print(f"外部API请求失败: {exc}")
|
| 351 |
+
yield f"data: {{\"error\": \"外部API请求失败: {str(exc)}\"}}\n\n"
|
| 352 |
+
|
| 353 |
+
return StreamingResponse(
|
| 354 |
+
event_generator(),
|
| 355 |
+
media_type="text/event-stream",
|
| 356 |
+
headers={
|
| 357 |
+
"Cache-Control": "no-cache",
|
| 358 |
+
"Connection": "keep-alive",
|
| 359 |
+
# CORS头已通过中间件处理,无需在这里重复添加
|
| 360 |
+
},
|
| 361 |
+
)
|
| 362 |
+
else:
|
| 363 |
+
# 非流式响应处理
|
| 364 |
+
async with httpx.AsyncClient(timeout=None) as client:
|
| 365 |
+
try:
|
| 366 |
+
response = await client.post(
|
| 367 |
+
EXTERNAL_API_URL,
|
| 368 |
+
headers=headers,
|
| 369 |
+
content=modified_request_body,
|
| 370 |
+
timeout=None
|
| 371 |
+
)
|
| 372 |
+
|
| 373 |
+
if response.status_code != 200:
|
| 374 |
+
raise HTTPException(
|
| 375 |
+
status_code=response.status_code,
|
| 376 |
+
detail=f"API 错误: {response.status_code}",
|
| 377 |
+
)
|
| 378 |
+
|
| 379 |
+
sse_lines = response.text.splitlines()
|
| 380 |
+
content_builder = ""
|
| 381 |
+
images_urls = []
|
| 382 |
+
|
| 383 |
+
for line in sse_lines:
|
| 384 |
+
if line.startswith("data: "):
|
| 385 |
+
data = line[6:].strip()
|
| 386 |
+
if data == "[DONE]":
|
| 387 |
+
break
|
| 388 |
+
try:
|
| 389 |
+
sse_json = json.loads(data)
|
| 390 |
+
if "choices" in sse_json:
|
| 391 |
+
for choice in sse_json["choices"]:
|
| 392 |
+
if "delta" in choice:
|
| 393 |
+
delta = choice["delta"]
|
| 394 |
+
if "content" in delta:
|
| 395 |
+
content_builder += delta["content"]
|
| 396 |
+
except json.JSONDecodeError:
|
| 397 |
+
print("JSON解析错误")
|
| 398 |
+
continue
|
| 399 |
+
|
| 400 |
+
openai_response = {
|
| 401 |
+
"id": f"chatcmpl-{uuid.uuid4()}",
|
| 402 |
+
"object": "chat.completion",
|
| 403 |
+
"created": int(datetime.now(timezone.utc).timestamp()),
|
| 404 |
+
"model": model,
|
| 405 |
+
"choices": [
|
| 406 |
+
{
|
| 407 |
+
"index": 0,
|
| 408 |
+
"message": {
|
| 409 |
+
"role": "assistant",
|
| 410 |
+
"content": content_builder,
|
| 411 |
+
},
|
| 412 |
+
"finish_reason": "stop",
|
| 413 |
+
}
|
| 414 |
+
],
|
| 415 |
+
}
|
| 416 |
+
|
| 417 |
+
# 处理图片(如果有)
|
| 418 |
+
if has_image:
|
| 419 |
+
images = []
|
| 420 |
+
for message in cleaned_messages:
|
| 421 |
+
if "images" in message:
|
| 422 |
+
for img in message["images"]:
|
| 423 |
+
images.append({"data": img["data"]})
|
| 424 |
+
openai_response["choices"][0]["message"]["images"] = images
|
| 425 |
+
|
| 426 |
+
return JSONResponse(content=openai_response, status_code=200)
|
| 427 |
+
except httpx.RequestError as exc:
|
| 428 |
+
raise HTTPException(status_code=500, detail=f"请求失败: {str(exc)}")
|
| 429 |
+
except Exception as exc:
|
| 430 |
+
raise HTTPException(status_code=500, detail=f"内部服务器错误: {str(exc)}")
|
| 431 |
+
|
| 432 |
+
# 图像生成处理
|
| 433 |
+
@app.post("/ai/v1/images/generations")
|
| 434 |
+
async def images_generations(request: Request, api_key: str = Depends(verify_api_key)):
|
| 435 |
+
"""
|
| 436 |
+
处理图像生成请求
|
| 437 |
+
"""
|
| 438 |
+
try:
|
| 439 |
+
request_body = await request.json()
|
| 440 |
+
except json.JSONDecodeError:
|
| 441 |
+
return send_error_response("Invalid JSON", status_code=400)
|
| 442 |
+
|
| 443 |
+
print("Received Image Generations JSON:", json.dumps(request_body, ensure_ascii=False))
|
| 444 |
+
|
| 445 |
+
# 验证必需的字段
|
| 446 |
+
if "prompt" not in request_body:
|
| 447 |
+
return send_error_response("缺少必需的字段: prompt", status_code=400)
|
| 448 |
+
|
| 449 |
+
user_prompt = request_body.get("prompt", "").strip()
|
| 450 |
+
response_format = request_body.get("response_format", "b64_json").strip()
|
| 451 |
+
|
| 452 |
+
if not user_prompt:
|
| 453 |
+
return send_error_response("Prompt 不能为空。", status_code=400)
|
| 454 |
+
|
| 455 |
+
print(f"Prompt: {user_prompt}")
|
| 456 |
+
|
| 457 |
+
# 构建新的 TextToImage JSON 请求体
|
| 458 |
+
text_to_image_json = {
|
| 459 |
+
"function_image_gen": True,
|
| 460 |
+
"function_web_search": True,
|
| 461 |
+
"image_aspect_ratio": "1:1",
|
| 462 |
+
"image_style": "photographic", # 暂时固定 image_style
|
| 463 |
+
"max_tokens": 8000,
|
| 464 |
+
"messages": [
|
| 465 |
+
{
|
| 466 |
+
"content": "You are a helpful artist, please based on imagination draw a picture.",
|
| 467 |
+
"role": "system"
|
| 468 |
+
},
|
| 469 |
+
{
|
| 470 |
+
"content": "Draw: " + user_prompt,
|
| 471 |
+
"role": "user"
|
| 472 |
+
}
|
| 473 |
+
],
|
| 474 |
+
"model": "gpt-4o", # 固定 model,只能gpt-4o或gpt-4o-mini
|
| 475 |
+
"source": "chat/pro_image" # 固定 source
|
| 476 |
+
}
|
| 477 |
+
|
| 478 |
+
modified_request_body = json.dumps(text_to_image_json, ensure_ascii=False)
|
| 479 |
+
print("Modified Request JSON:", modified_request_body)
|
| 480 |
+
|
| 481 |
+
# 获取Bearer Token
|
| 482 |
+
tmp_token = BearerTokenGenerator.get_bearer(modified_request_body, path="/chats/stream")
|
| 483 |
+
if not tmp_token:
|
| 484 |
+
return send_error_response("无法生成 Bearer Token", status_code=500)
|
| 485 |
+
|
| 486 |
+
bearer_token, formatted_date = tmp_token
|
| 487 |
+
|
| 488 |
+
headers = {
|
| 489 |
+
"Date": formatted_date,
|
| 490 |
+
"Client-time-zone": "-05:00",
|
| 491 |
+
"Authorization": bearer_token,
|
| 492 |
+
"User-Agent": "ChatOn_Android/1.53.502",
|
| 493 |
+
"Accept-Language": "en-US",
|
| 494 |
+
"X-Cl-Options": "hb",
|
| 495 |
+
"Content-Type": "application/json; charset=UTF-8",
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
async with httpx.AsyncClient(timeout=None) as client:
|
| 499 |
+
try:
|
| 500 |
+
response = await client.post(
|
| 501 |
+
EXTERNAL_API_URL, headers=headers, content=modified_request_body, timeout=None
|
| 502 |
+
)
|
| 503 |
+
if response.status_code != 200:
|
| 504 |
+
return send_error_response(f"API 错误: {response.status_code}", status_code=500)
|
| 505 |
+
|
| 506 |
+
# 初始化用于拼接 URL 的字符串
|
| 507 |
+
url_builder = ""
|
| 508 |
+
|
| 509 |
+
# 读取 SSE 流并拼接 URL
|
| 510 |
+
async for line in response.aiter_lines():
|
| 511 |
+
if line.startswith("data: "):
|
| 512 |
+
data = line[6:].strip()
|
| 513 |
+
if data == "[DONE]":
|
| 514 |
+
break
|
| 515 |
+
try:
|
| 516 |
+
sse_json = json.loads(data)
|
| 517 |
+
if "choices" in sse_json:
|
| 518 |
+
for choice in sse_json["choices"]:
|
| 519 |
+
delta = choice.get("delta", {})
|
| 520 |
+
content = delta.get("content")
|
| 521 |
+
if content:
|
| 522 |
+
url_builder += content
|
| 523 |
+
except json.JSONDecodeError:
|
| 524 |
+
print("JSON解析错误")
|
| 525 |
+
continue
|
| 526 |
+
|
| 527 |
+
image_markdown = url_builder
|
| 528 |
+
# Step 1: 检查Markdown文本是否为空
|
| 529 |
+
if not image_markdown:
|
| 530 |
+
print("无法从 SSE 流中构建图像 Markdown。")
|
| 531 |
+
return send_error_response("无法从 SSE 流中构建图像 Markdown。", status_code=500)
|
| 532 |
+
|
| 533 |
+
# Step 2, 3, 4, 5: 处理图像
|
| 534 |
+
extracted_path = extract_path_from_markdown(image_markdown)
|
| 535 |
+
if not extracted_path:
|
| 536 |
+
print("无法从 Markdown 中提取路径。")
|
| 537 |
+
return send_error_response("无法从 Markdown 中提取路径。", status_code=500)
|
| 538 |
+
|
| 539 |
+
print(f"提取的路径: {extracted_path}")
|
| 540 |
+
|
| 541 |
+
# Step 5: 拼接最终的存储URL
|
| 542 |
+
storage_url = f"https://api.chaton.ai/storage/{extracted_path}"
|
| 543 |
+
print(f"存储URL: {storage_url}")
|
| 544 |
+
|
| 545 |
+
# 获取最终下载URL
|
| 546 |
+
final_download_url = await fetch_get_url_from_storage(storage_url)
|
| 547 |
+
if not final_download_url:
|
| 548 |
+
return send_error_response("无法从 storage URL 获取最终下载链接。", status_code=500)
|
| 549 |
+
|
| 550 |
+
print(f"Final Download URL: {final_download_url}")
|
| 551 |
+
|
| 552 |
+
# 下载图像
|
| 553 |
+
image_bytes = await download_image(final_download_url)
|
| 554 |
+
if not image_bytes:
|
| 555 |
+
return send_error_response("无法从 URL 下载图像。", status_code=500)
|
| 556 |
+
|
| 557 |
+
# 转换为 Base64
|
| 558 |
+
image_base64 = base64.b64encode(image_bytes).decode('utf-8')
|
| 559 |
+
|
| 560 |
+
# 将图片保存到images目录并构建可访问的URL
|
| 561 |
+
filename = save_base64_image(image_base64)
|
| 562 |
+
base_url = app.state.base_url
|
| 563 |
+
accessible_url = f"{base_url}/images/{filename}"
|
| 564 |
+
|
| 565 |
+
# 根据 response_format 返回相应的响应
|
| 566 |
+
if response_format.lower() == "b64_json":
|
| 567 |
+
response_json = {
|
| 568 |
+
"data": [
|
| 569 |
+
{
|
| 570 |
+
"b64_json": image_base64
|
| 571 |
+
}
|
| 572 |
+
]
|
| 573 |
+
}
|
| 574 |
+
return JSONResponse(content=response_json, status_code=200)
|
| 575 |
+
else:
|
| 576 |
+
# 构建包含可访问URL的响应
|
| 577 |
+
response_json = {
|
| 578 |
+
"data": [
|
| 579 |
+
{
|
| 580 |
+
"url": accessible_url
|
| 581 |
+
}
|
| 582 |
+
]
|
| 583 |
+
}
|
| 584 |
+
return JSONResponse(content=response_json, status_code=200)
|
| 585 |
+
except httpx.RequestError as exc:
|
| 586 |
+
print(f"请求失败: {exc}")
|
| 587 |
+
return send_error_response(f"请求失败: {str(exc)}", status_code=500)
|
| 588 |
+
except Exception as exc:
|
| 589 |
+
print(f"内部服务器错误: {exc}")
|
| 590 |
+
return send_error_response(f"内部服务器错误: {str(exc)}", status_code=500)
|
| 591 |
+
|
| 592 |
+
# 运行服务器
|
| 593 |
+
def main():
|
| 594 |
+
parser = argparse.ArgumentParser(description="启动ChatOn API服务器")
|
| 595 |
+
parser.add_argument('--base_url', type=str, default='http://localhost', help='Base URL for accessing images')
|
| 596 |
+
parser.add_argument('--port', type=int, default=INITIAL_PORT, help='服务器监听端口')
|
| 597 |
+
args = parser.parse_args()
|
| 598 |
+
base_url = args.base_url
|
| 599 |
+
port = args.port
|
| 600 |
+
|
| 601 |
+
# 检查 API_KEY 是否设置
|
| 602 |
+
if not os.environ.get("API_KEY"):
|
| 603 |
+
print("警告: API_KEY 环境变量未设置。客户端验证将无法正常工作。")
|
| 604 |
+
|
| 605 |
+
# 确保 images 目录存在
|
| 606 |
+
if not os.path.exists("images"):
|
| 607 |
+
os.makedirs("images")
|
| 608 |
+
|
| 609 |
+
# 设置 FastAPI 应用的 state
|
| 610 |
+
app.state.base_url = base_url
|
| 611 |
+
|
| 612 |
+
print(f"Server started on port {port} with base_url: {base_url}")
|
| 613 |
+
|
| 614 |
+
# 运行FastAPI应用
|
| 615 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
| 616 |
+
|
| 617 |
+
async def get_available_port(start_port: int = INITIAL_PORT, end_port: int = 65535) -> int:
|
| 618 |
+
"""查找可用的端口号"""
|
| 619 |
+
for port in range(start_port, end_port + 1):
|
| 620 |
+
try:
|
| 621 |
+
server = await asyncio.start_server(lambda r, w: None, host="0.0.0.0", port=port)
|
| 622 |
+
server.close()
|
| 623 |
+
await server.wait_closed()
|
| 624 |
+
return port
|
| 625 |
+
except OSError:
|
| 626 |
+
continue
|
| 627 |
+
raise RuntimeError(f"No available ports between {start_port} and {end_port}")
|
| 628 |
+
|
| 629 |
+
if __name__ == "__main__":
|
| 630 |
+
main()
|