File size: 15,481 Bytes
4d2e96d | 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 | import json
import os
import time
import uuid
import logging
import asyncio
from typing import Optional
from contextlib import asynccontextmanager
from fastapi import FastAPI, Request, HTTPException
from fastapi.responses import StreamingResponse, JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from duck_client import DuckAIClient
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger(__name__)
CONFIG_FILE = os.environ.get("CONFIG_FILE", "config.json")
def load_config() -> dict:
try:
with open(CONFIG_FILE, "r", encoding="utf-8") as f:
return json.load(f)
except FileNotFoundError:
return {}
config = load_config()
def env_or_config(env_name: str, config_key: str, default=None, cast=None):
value = os.environ.get(env_name)
if value is None:
value = config.get(config_key, default)
if value is None:
return None
if cast is bool:
if isinstance(value, bool):
return value
return str(value).strip().lower() in {"1", "true", "yes", "on"}
if cast is int:
return int(value)
if cast:
return cast(value)
return value
API_KEY = env_or_config("API_KEY", "api_key", "sk-duck-ai")
PROXY = env_or_config("PROXY", "proxy", None)
HOST = env_or_config("HOST", "host", "0.0.0.0")
PORT = env_or_config("PORT", "port", 7860, cast=int)
DEFAULT_MODEL = env_or_config("DEFAULT_MODEL", "default_model", "claude-haiku-4-5")
ASSISTANT_NAME = env_or_config("ASSISTANT_NAME", "assistant_name", None)
SYSTEM_PROMPT = env_or_config("SYSTEM_PROMPT", "system_prompt", None)
WEB_SEARCH = env_or_config("WEB_SEARCH", "web_search", True, cast=bool)
POOL_SIZE = env_or_config("POOL_SIZE", "pool_size", 2, cast=int)
# Single shared client with page pool
_shared_client: Optional[DuckAIClient] = None
_client_lock = asyncio.Lock()
async def get_client() -> DuckAIClient:
global _shared_client
async with _client_lock:
if _shared_client is None:
_shared_client = DuckAIClient(
proxy=PROXY,
model=DEFAULT_MODEL,
assistant_name=ASSISTANT_NAME,
system_prompt=SYSTEM_PROMPT,
pool_size=POOL_SIZE,
)
return _shared_client
async def return_client(c: DuckAIClient):
return None
@asynccontextmanager
async def lifespan(app: FastAPI):
logger.info("Duck.ai 2API server starting on %s:%s", HOST, PORT)
logger.info("Default model: %s", DEFAULT_MODEL)
logger.info("Web search: %s", WEB_SEARCH)
logger.info("Pool size: %s", POOL_SIZE)
logger.info("Proxy: %s", PROXY or "None")
yield
global _shared_client
if _shared_client:
await _shared_client.close()
_shared_client = None
logger.info("Server shutdown complete")
app = FastAPI(title="Duck.ai 2API", lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
def verify_auth(request: Request):
if not API_KEY:
return
auth = request.headers.get("Authorization", "")
if auth.startswith("Bearer "):
token = auth[7:]
else:
token = auth
if token != API_KEY:
raise HTTPException(status_code=401, detail="Invalid API key")
MODEL_MAP = {
"claude-haiku-4-5": "claude-haiku-4-5",
"claude-3-haiku": "claude-haiku-4-5",
"claude-3-5-haiku": "claude-haiku-4-5",
"claude-3-haiku-20240307": "claude-haiku-4-5",
"gpt-4o-mini": "gpt-4o-mini",
"gpt-4o": "gpt-4o-mini",
"gpt-3.5-turbo": "gpt-4o-mini",
"llama-3.3-70b": "meta-llama/Llama-3.3-70B-Instruct-Turbo",
"mixtral-8x7b": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"o3-mini": "o3-mini",
}
def map_model(model: str) -> str:
return MODEL_MAP.get(model, DEFAULT_MODEL)
@app.get("/v1/models")
@app.get("/models")
async def list_models(request: Request):
verify_auth(request)
models = [
{"id": "claude-haiku-4-5", "object": "model", "owned_by": "anthropic"},
{"id": "gpt-4o-mini", "object": "model", "owned_by": "openai"},
{"id": "o3-mini", "object": "model", "owned_by": "openai"},
{"id": "meta-llama/Llama-3.3-70B-Instruct-Turbo", "object": "model", "owned_by": "meta"},
{"id": "mistralai/Mixtral-8x7B-Instruct-v0.1", "object": "model", "owned_by": "mistral"},
]
return {"object": "list", "data": models}
@app.post("/v1/chat/completions")
@app.post("/chat/completions")
async def chat_completions(request: Request):
verify_auth(request)
try:
body = await request.json()
except Exception:
raise HTTPException(status_code=400, detail="Invalid JSON body")
messages = body.get("messages", [])
if not messages:
raise HTTPException(status_code=400, detail="messages is required")
req_model = body.get("model", DEFAULT_MODEL)
duck_model = map_model(req_model)
stream = body.get("stream", False)
web_search = body.get("web_search", WEB_SEARCH)
custom_instructions = None
assistant_name = ASSISTANT_NAME
for msg in messages:
if msg.get("role") == "system":
custom_instructions = msg.get("content", "")
break
completion_id = f"chatcmpl-{uuid.uuid4().hex[:24]}"
created = int(time.time())
client = await get_client()
client.model = duck_model
if stream:
return StreamingResponse(
_stream_response(
client,
messages,
web_search,
custom_instructions,
assistant_name,
completion_id,
created,
req_model,
),
media_type="text/event-stream",
headers={
"Cache-Control": "no-cache",
"Connection": "keep-alive",
"X-Accel-Buffering": "no",
},
)
return await _non_stream_response(
client,
messages,
web_search,
custom_instructions,
assistant_name,
completion_id,
created,
req_model,
)
async def _stream_response(client: DuckAIClient, messages: list,
web_search: bool, custom_instructions: str,
assistant_name: str, completion_id: str,
created: int, model: str):
try:
first_chunk = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{
"index": 0,
"delta": {"role": "assistant", "content": ""},
"finish_reason": None,
}],
}
yield f"data: {json.dumps(first_chunk)}\n\n"
search_sources = []
async for event in client.chat_stream(
messages=messages,
web_search=web_search,
custom_instructions=custom_instructions,
assistant_name=assistant_name,
):
etype = event.get("type")
if etype == "text":
text = event.get("data", "")
if text and isinstance(text, str):
chunk = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{
"index": 0,
"delta": {"content": text},
"finish_reason": None,
}],
}
yield f"data: {json.dumps(chunk)}\n\n"
elif etype == "message":
data = event.get("data", {})
text = ""
if isinstance(data, dict):
text = data.get("message", data.get("content", ""))
elif isinstance(data, str):
text = data
if text and isinstance(text, str):
chunk = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{
"index": 0,
"delta": {"content": text},
"finish_reason": None,
}],
}
yield f"data: {json.dumps(chunk)}\n\n"
elif etype == "search_source":
src = event.get("data", {})
if isinstance(src, dict) and src.get("url"):
search_sources.append(src)
elif etype in ("search_begin", "search_results", "search_end"):
pass
elif etype == "done":
break
elif etype == "event":
data = event.get("data", {})
if isinstance(data, dict):
msg = data.get("message", data.get("content", ""))
if msg and isinstance(msg, str):
chunk = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{
"index": 0,
"delta": {"content": msg},
"finish_reason": None,
}],
}
yield f"data: {json.dumps(chunk)}\n\n"
if search_sources:
refs = "\n\n---\n**搜索结果:**\n"
for i, src in enumerate(search_sources[:8], 1):
title = src.get("title", "")
url = src.get("url", "")
site = src.get("site", "")
favicon = f"https://www.google.com/s2/favicons?domain={site}&sz=32" if site else ""
if title and url:
icon = f" " if favicon else ""
refs += f"{i}. {icon}[{title}]({url}) - {site}\n"
chunk = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{
"index": 0,
"delta": {"content": refs},
"finish_reason": None,
}],
}
yield f"data: {json.dumps(chunk)}\n\n"
final_chunk = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{
"index": 0,
"delta": {},
"finish_reason": "stop",
}],
}
yield f"data: {json.dumps(final_chunk)}\n\n"
yield "data: [DONE]\n\n"
except Exception as e:
logger.error("Stream error: %s", e, exc_info=True)
error_chunk = {
"id": completion_id,
"object": "chat.completion.chunk",
"created": created,
"model": model,
"choices": [{
"index": 0,
"delta": {"content": f"\n\n[Error: {str(e)}]"},
"finish_reason": "stop",
}],
}
yield f"data: {json.dumps(error_chunk)}\n\n"
yield "data: [DONE]\n\n"
finally:
await return_client(client)
async def _non_stream_response(client: DuckAIClient, messages: list,
web_search: bool, custom_instructions: str,
assistant_name: str, completion_id: str,
created: int, model: str):
full_content = ""
search_sources = []
try:
async for event in client.chat_stream(
messages=messages,
web_search=web_search,
custom_instructions=custom_instructions,
assistant_name=assistant_name,
):
etype = event.get("type")
if etype == "text":
val = event.get("data", "")
if isinstance(val, str):
full_content += val
elif etype == "message":
data = event.get("data", {})
if isinstance(data, dict):
msg = data.get("message", data.get("content", ""))
if isinstance(msg, str):
full_content += msg
elif isinstance(data, str):
full_content += data
elif etype == "search_source":
src = event.get("data", {})
if isinstance(src, dict) and src.get("url"):
search_sources.append(src)
elif etype == "event":
data = event.get("data", {})
if isinstance(data, dict):
msg = data.get("message", data.get("content", ""))
if isinstance(msg, str):
full_content += msg
elif etype == "done":
break
except Exception as e:
logger.error("Chat error: %s", e, exc_info=True)
raise HTTPException(status_code=500, detail=str(e))
finally:
await return_client(client)
if search_sources:
refs = "\n\n---\n**搜索结果:**\n"
for i, src in enumerate(search_sources[:8], 1):
title = src.get("title", "")
url = src.get("url", "")
site = src.get("site", "")
favicon = f"https://www.google.com/s2/favicons?domain={site}&sz=32" if site else ""
if title and url:
icon = f" " if favicon else ""
refs += f"{i}. {icon}[{title}]({url}) - {site}\n"
full_content += refs
prompt_tokens = sum(len(m.get("content", "")) for m in messages) // 4
completion_tokens = len(full_content) // 4
return JSONResponse({
"id": completion_id,
"object": "chat.completion",
"created": created,
"model": model,
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": full_content},
"finish_reason": "stop",
}],
"usage": {
"prompt_tokens": prompt_tokens,
"completion_tokens": completion_tokens,
"total_tokens": prompt_tokens + completion_tokens,
},
})
@app.get("/")
async def root():
return {
"service": "Duck.ai 2API",
"status": "running",
"model": DEFAULT_MODEL,
"docs": "/docs",
"health": "/health",
}
@app.get("/health")
async def health():
client = None
try:
client = await get_client()
return {
"status": "ok",
"model": DEFAULT_MODEL,
"pool": client.pool_status(),
}
except Exception as e:
return JSONResponse(
status_code=503,
content={"status": "error", "detail": str(e)},
)
if __name__ == "__main__":
import uvicorn
uvicorn.run("server:app", host=HOST, port=PORT, reload=False)
|