Files
OpenMaopaoya/app/services/llm.py
T

55 lines
2.6 KiB
Python

"""OpenAI 兼容客户端:聊天、生图、TTS,全部按渠道转发。"""
import base64
import httpx
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:
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 ""
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", ""))
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
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]