Files
OpenMaopaoya/app/services/llm.py
T

69 lines
2.9 KiB
Python

"""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):
self.base_url = base_url.rstrip("/")
self.api_key = api_key
async def chat(self, model: str, messages: list[dict], temperature: float = 0.9,
max_tokens: int = 2048) -> str:
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:
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 格式。"""
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:
reply = await self.chat(model, [{"role": "user", "content": "ping"}],
temperature=0, max_tokens=8)
return True, reply or "ok"
except Exception as e:
return False, str(e)[:300]