性能: 共享httpx连接池/设置缓存/复合索引自动补建/GZip/分页 ; 商业化v1: APIKey(sha256+scope+限流)/用量账本/版本化端点
This commit is contained in:
+43
-29
@@ -1,8 +1,23 @@
|
||||
"""OpenAI 兼容客户端:聊天、生图、TTS,全部按渠道转发。"""
|
||||
"""OpenAI 兼容客户端:聊天、生图、TTS,全部按渠道转发。
|
||||
|
||||
性能:全局共享 AsyncClient(连接池复用,避免每请求握手)。
|
||||
"""
|
||||
import base64
|
||||
|
||||
import httpx
|
||||
|
||||
# 进程级共享连接池; comercial 场景下量大可调 max_connections
|
||||
_shared_client: httpx.AsyncClient | None = None
|
||||
|
||||
|
||||
def get_http() -> httpx.AsyncClient:
|
||||
global _shared_client
|
||||
if _shared_client is None or _shared_client.is_closed:
|
||||
_shared_client = httpx.AsyncClient(
|
||||
timeout=httpx.Timeout(180, connect=10),
|
||||
limits=httpx.Limits(max_connections=100, max_keepalive_connections=20))
|
||||
return _shared_client
|
||||
|
||||
|
||||
class LLMClient:
|
||||
def __init__(self, base_url: str, api_key: str):
|
||||
@@ -11,39 +26,38 @@ class LLMClient:
|
||||
|
||||
async def chat(self, model: str, messages: list[dict], temperature: float = 0.9,
|
||||
max_tokens: int = 2048) -> str:
|
||||
async with httpx.AsyncClient(timeout=120) as c:
|
||||
r = await c.post(f"{self.base_url}/chat/completions",
|
||||
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||
json={"model": model, "messages": messages,
|
||||
"temperature": temperature, "max_tokens": max_tokens})
|
||||
r.raise_for_status()
|
||||
return r.json()["choices"][0]["message"]["content"] or ""
|
||||
r = await get_http().post(
|
||||
f"{self.base_url}/chat/completions",
|
||||
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||
json={"model": model, "messages": messages,
|
||||
"temperature": temperature, "max_tokens": max_tokens})
|
||||
r.raise_for_status()
|
||||
return r.json()["choices"][0]["message"]["content"] or ""
|
||||
|
||||
async def image(self, model: str, prompt: str, size: str = "1024x1024") -> str:
|
||||
async with httpx.AsyncClient(timeout=180) as c:
|
||||
r = await c.post(f"{self.base_url}/images/generations",
|
||||
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||
json={"model": model, "prompt": prompt,
|
||||
"size": size, "n": 1, "response_format": "url"})
|
||||
r.raise_for_status()
|
||||
d = r.json()["data"][0]
|
||||
return d.get("url") or ("data:image/png;base64," + d.get("b64_json", ""))
|
||||
r = await get_http().post(
|
||||
f"{self.base_url}/images/generations",
|
||||
headers={"Authorization": f"Bearer {self.api_key}"},
|
||||
json={"model": model, "prompt": prompt,
|
||||
"size": size, "n": 1, "response_format": "url"})
|
||||
r.raise_for_status()
|
||||
d = r.json()["data"][0]
|
||||
return d.get("url") or ("data:image/png;base64," + d.get("b64_json", ""))
|
||||
|
||||
async def tts(self, model: str, text: str, voice: str) -> bytes:
|
||||
"""优先 OpenAI 兼容 /audio/speech;失败回落阶跃 stepaudio TTS 格式。"""
|
||||
async with httpx.AsyncClient(timeout=120) as c:
|
||||
headers = {"Authorization": f"Bearer {self.api_key}"}
|
||||
try:
|
||||
r = await c.post(f"{self.base_url}/audio/speech", headers=headers,
|
||||
json={"model": model, "input": text, "voice": voice})
|
||||
r.raise_for_status()
|
||||
return r.content
|
||||
except httpx.HTTPError:
|
||||
r = await c.post(f"{self.base_url}/audio/speech", headers=headers,
|
||||
json={"model": model, "text": text, "voice": voice,
|
||||
"response_format": "wav"})
|
||||
r.raise_for_status()
|
||||
return r.content
|
||||
headers = {"Authorization": f"Bearer {self.api_key}"}
|
||||
try:
|
||||
r = await get_http().post(f"{self.base_url}/audio/speech", headers=headers,
|
||||
json={"model": model, "input": text, "voice": voice})
|
||||
r.raise_for_status()
|
||||
return r.content
|
||||
except httpx.HTTPError:
|
||||
r = await get_http().post(f"{self.base_url}/audio/speech", headers=headers,
|
||||
json={"model": model, "text": text, "voice": voice,
|
||||
"response_format": "wav"})
|
||||
r.raise_for_status()
|
||||
return r.content
|
||||
|
||||
async def ping(self, model: str) -> tuple[bool, str]:
|
||||
try:
|
||||
|
||||
Reference in New Issue
Block a user