Files
OpenMaopaoya/app/services/llm.py
T

77 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""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", ""))
# 阶跃 stepaudio 系合法音色(system_voices 实测 + 官方文档)
TTS_VOICES = {"elegantgentle-female", "livelybreezy-female", "lively-girl",
"cixingnansheng", "wenrounansheng", "shenchennanyin",
"zhengpaiqingnian", "shuangkuainansheng", "ganliannvsheng",
"qinhenvsheng", "huolinvsheng"}
@staticmethod
def fix_voice(voice: str) -> str:
return voice if voice in LLMClient.TTS_VOICES else "elegantgentle-female"
async def tts(self, model: str, text: str, voice: str) -> bytes:
"""OpenAI 兼容 /audio/speech;input 上限 1000 字符(stepaudio-2.5-tts)。"""
voice = self.fix_voice(voice)
r = await get_http().post(
f"{self.base_url}/audio/speech",
headers={"Authorization": f"Bearer {self.api_key}"},
json={"model": model, "input": text[:1000], "voice": voice,
"response_format": "wav"})
if r.status_code >= 400:
detail = r.text[:150]
raise RuntimeError(f"{r.status_code}: {detail}")
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]