init: api-gateway v2.4.0(Agnes/b.ai/火山方舟 三家供应商网关)
This commit is contained in:
commit
d143d8da22
91 files changed
+24653
No files matched your search
@@ -0,0 +1,31 @@
|
||||
"""供应商适配器注册表。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from app.adapters.base import BaseAdapter
|
||||
from app.adapters.openai_compat import OpenAICompatAdapter
|
||||
from app.config import ProviderConfig
|
||||
|
||||
|
||||
def create_adapter(
|
||||
pconfig: ProviderConfig, token: str, timeout: int = 120
|
||||
) -> BaseAdapter:
|
||||
"""根据供应商配置创建对应适配器。
|
||||
|
||||
当前三个供应商(Agnes / b.ai / 火山方舟)均为 OpenAI 兼容协议,
|
||||
统一使用 OpenAICompatAdapter,通过 base_url 和 extra_headers 区分。
|
||||
图像/视频端点通过 media 配置覆盖(Agnes 走 /videos + /agnesapi)。
|
||||
"""
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
headers.update(pconfig.extra_headers)
|
||||
|
||||
return OpenAICompatAdapter(
|
||||
name=pconfig.name,
|
||||
base_url=pconfig.base_url,
|
||||
headers=headers,
|
||||
timeout=timeout,
|
||||
media=pconfig.media,
|
||||
)
|
||||
@@ -0,0 +1,191 @@
|
||||
"""适配器基类:定义统一的 chat / 流式 / 图像 / 视频接口。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
from abc import ABC, abstractmethod
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
import httpx
|
||||
|
||||
from app.config import MediaConfig
|
||||
|
||||
|
||||
class UpstreamResponseError(RuntimeError):
|
||||
"""上游返回了"协议外"的响应(HTTP 200 但不是 JSON / body 为空)。
|
||||
|
||||
第六轮: 此前这类响应直接在 `resp.json()` 处抛 json.JSONDecodeError,
|
||||
错误信息是 `Expecting value: line 1 column 1 (char 0)` —— 与真正的上游 5xx
|
||||
无法区分,排查时极具误导性。
|
||||
"""
|
||||
|
||||
|
||||
def json_or_raise(resp) -> dict:
|
||||
"""解析上游 JSON;失败时抛带 HTTP 状态与响应片段的 UpstreamResponseError。"""
|
||||
try:
|
||||
data = resp.json()
|
||||
except (ValueError, json.JSONDecodeError) as e:
|
||||
snippet = (resp.text or "")[:200]
|
||||
raise UpstreamResponseError(
|
||||
f"上游返回非 JSON 响应(HTTP {resp.status_code}): {snippet!r}") from e
|
||||
if not isinstance(data, dict):
|
||||
raise UpstreamResponseError(
|
||||
f"上游返回了非预期的 JSON 类型({type(data).__name__}),HTTP {resp.status_code}")
|
||||
return data
|
||||
|
||||
|
||||
class BaseAdapter(ABC):
|
||||
"""所有供应商适配器必须实现此接口。"""
|
||||
|
||||
def __init__(self, name: str, base_url: str, timeout: int = 120, media: MediaConfig | None = None):
|
||||
self.name = name
|
||||
self.base_url = base_url.rstrip("/")
|
||||
self.timeout = timeout
|
||||
self.media = media or MediaConfig()
|
||||
self._client: httpx.AsyncClient | None = None
|
||||
self._client_lock = asyncio.Lock() # Bug 1: 防并发竞态建多个 client
|
||||
self._closing = False # 正在物理关闭连接
|
||||
self._close_requested = False # 收到关闭请求,但仍有在途请求,等待排空后再关
|
||||
self._active = 0 # 在途请求/流占用数(Bug 4: 热重载不打断在途请求)
|
||||
|
||||
async def get_client(self) -> httpx.AsyncClient:
|
||||
"""获取共享 client 并占用一次(并发安全)。
|
||||
|
||||
之前:两个 coroutine 同时 if self._client is None 各建一个 → 泄漏。
|
||||
现在:用 asyncio.Lock 保证只建一次,并 +1 占用计数(每次在途请求/流占用一次)。
|
||||
"""
|
||||
async with self._client_lock:
|
||||
if self._client is None or self._closing:
|
||||
self._closing = False
|
||||
self._client = httpx.AsyncClient(
|
||||
base_url=self.base_url,
|
||||
timeout=httpx.Timeout(self.timeout),
|
||||
)
|
||||
self._active += 1
|
||||
return self._client
|
||||
|
||||
async def _release(self) -> None:
|
||||
"""归还一次占用。最后一次归还且曾收到关闭请求时,才真正关闭连接池。
|
||||
|
||||
Bug 4 修复: 让 close() 能感知『仍有请求在用』而延迟真正的网络关闭,
|
||||
避免热重载关闭 client 时打断正在流式/在途的请求。
|
||||
"""
|
||||
async with self._client_lock:
|
||||
self._active = max(0, self._active - 1)
|
||||
if self._active == 0 and self._close_requested and self._client is not None:
|
||||
self._closing = True
|
||||
try:
|
||||
await self._client.aclose()
|
||||
except Exception:
|
||||
pass
|
||||
self._client = None
|
||||
self._closing = False
|
||||
self._close_requested = False
|
||||
|
||||
@asynccontextmanager
|
||||
async def _lease(self) -> AsyncIterator[httpx.AsyncClient]:
|
||||
"""async context manager:占用 client 执行一次网络操作,结束自动归还占用。"""
|
||||
client = await self.get_client()
|
||||
try:
|
||||
yield client
|
||||
finally:
|
||||
await self._release()
|
||||
|
||||
async def _close_now_locked(self) -> None:
|
||||
"""物理关闭连接池。**调用方必须已持有 _client_lock。**
|
||||
|
||||
第六轮并发审计实测的竞态:`close()` 先在锁内判定 `_active == 0`,出锁后再调
|
||||
`_close_now()` 重新抢锁 —— 两步之间排队的 `get_client()` 可能先拿到锁、把
|
||||
`_active` 加到 1 并返回该 client,随后它被 aclose() 掉,在途请求直接
|
||||
`RuntimeError: Cannot send a request, as the client has been closed.`
|
||||
现在把"判定 + 关闭"合并到同一次持锁内完成。
|
||||
"""
|
||||
if self._client is not None:
|
||||
self._closing = True
|
||||
try:
|
||||
await self._client.aclose()
|
||||
except Exception:
|
||||
pass
|
||||
self._client = None
|
||||
self._closing = False
|
||||
self._close_requested = False
|
||||
|
||||
async def close(self) -> None:
|
||||
"""关闭 client(幂等可重入)。
|
||||
|
||||
之前:并发两次 close → 第二次对已关闭 client 抛 RuntimeError。
|
||||
现在:有在途请求时置 _close_requested 等待排空(由最后一次 _release 关闭);
|
||||
无在途请求则立即关闭。重复调用安全。
|
||||
"""
|
||||
async with self._client_lock:
|
||||
if self._client is None and not self._close_requested:
|
||||
return
|
||||
if self._active > 0:
|
||||
self._close_requested = True # 等在途请求排空后由最后一次 _release 关闭
|
||||
return
|
||||
# 第六轮: 置位 + 关闭都在同一次持锁内完成(见 _close_now_locked 的说明)
|
||||
self._close_requested = True
|
||||
await self._close_now_locked()
|
||||
|
||||
@abstractmethod
|
||||
async def chat(
|
||||
self,
|
||||
model: str,
|
||||
messages: list[dict],
|
||||
temperature: float | None = None,
|
||||
top_p: float | None = None,
|
||||
max_tokens: int | None = None,
|
||||
stream: bool = False,
|
||||
stop: str | list[str] | None = None,
|
||||
tools: list[dict] | None = None,
|
||||
tool_choice: str | dict | None = None,
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""非流式 chat completion,返回统一格式的 dict。"""
|
||||
...
|
||||
|
||||
@abstractmethod
|
||||
async def chat_stream(
|
||||
self,
|
||||
model: str,
|
||||
messages: list[dict],
|
||||
temperature: float | None = None,
|
||||
top_p: float | None = None,
|
||||
max_tokens: int | None = None,
|
||||
stop: str | list[str] | None = None,
|
||||
tools: list[dict] | None = None,
|
||||
tool_choice: str | dict | None = None,
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> AsyncIterator[dict]:
|
||||
"""流式 chat completion,逐块 yield。"""
|
||||
...
|
||||
|
||||
# ── 图像 / 视频(可选实现,默认不支持) ──────────
|
||||
async def generate_image(
|
||||
self,
|
||||
model: str,
|
||||
prompt: str,
|
||||
size: str | None = None,
|
||||
ratio: str | None = None,
|
||||
image: list[str] | None = None,
|
||||
n: int = 1,
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
raise NotImplementedError(f"供应商 {self.name} 不支持图像生成")
|
||||
|
||||
async def create_video(
|
||||
self,
|
||||
model: str,
|
||||
prompt: str,
|
||||
seconds: str = "5",
|
||||
mode: str = "text",
|
||||
aspect_ratio: str = "16:9",
|
||||
size: str = "720P",
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
raise NotImplementedError(f"供应商 {self.name} 不支持视频创建")
|
||||
|
||||
async def poll_video(self, video_id: str, model: str = "", extra: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
raise NotImplementedError(f"供应商 {self.name} 不支持视频查询")
|
||||
@@ -0,0 +1,228 @@
|
||||
"""OpenAI 兼容协议适配器。
|
||||
|
||||
Agnes / b.ai / 火山方舟 coding plan 三家均使用 OpenAI 兼容 API
|
||||
(POST {base_url}/chat/completions),本适配器统一处理。
|
||||
|
||||
图像/视频端点可通过 media 配置覆盖:
|
||||
- 图像:默认 {base_url}/images/generations(OpenAI 兼容)
|
||||
- 视频:Agnes 用 {base_url}/videos 创建 + {poll_base}{poll_path}?video_id=...&model_name=... 轮询
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import Any, AsyncIterator
|
||||
|
||||
|
||||
from app.adapters.base import BaseAdapter, json_or_raise
|
||||
from app.models import CHAT_CORE_KEYS, PROTECTED_VIDEO_KEYS, strip_protected
|
||||
|
||||
|
||||
class OpenAICompatAdapter(BaseAdapter):
|
||||
"""适用于所有 OpenAI 兼容 /chat/completions 端点的适配器。"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
base_url: str,
|
||||
headers: dict[str, str] | None = None,
|
||||
timeout: int = 120,
|
||||
media=None,
|
||||
):
|
||||
super().__init__(name=name, base_url=base_url, timeout=timeout, media=media)
|
||||
self._headers: dict[str, str] = headers or {}
|
||||
|
||||
def _build_payload(
|
||||
self,
|
||||
model: str,
|
||||
messages: list[dict],
|
||||
temperature: float | None,
|
||||
top_p: float | None,
|
||||
max_tokens: int | None,
|
||||
stream: bool,
|
||||
stop: str | list[str] | None,
|
||||
tools: list[dict] | None,
|
||||
tool_choice: str | dict | None,
|
||||
extra: dict[str, Any] | None,
|
||||
) -> dict[str, Any]:
|
||||
payload: dict[str, Any] = {"model": model, "messages": messages, "stream": stream}
|
||||
if temperature is not None:
|
||||
payload["temperature"] = temperature
|
||||
if top_p is not None:
|
||||
payload["top_p"] = top_p
|
||||
if max_tokens is not None:
|
||||
payload["max_tokens"] = max_tokens
|
||||
if stop is not None:
|
||||
payload["stop"] = stop
|
||||
if tools:
|
||||
payload["tools"] = tools
|
||||
if tool_choice is not None:
|
||||
payload["tool_choice"] = tool_choice
|
||||
# 合并供应商特有参数(如 Agnes 的 chat_template_kwargs)。
|
||||
# 第六轮安全修复: 先剔除核心键 —— 否则 `extra={"model": ..., "stream": ...}`
|
||||
# 会覆盖掉上面写好的值(路由劫持 / 协议错配)。models.upstream_extra() 已过滤,
|
||||
# 这里是适配器侧的最后一道防线。
|
||||
# 只剔除"本函数上面刚写好的核心键";其余(网关 _PASSTHROUGH 的标准参数、
|
||||
# 以及供应商特有参数)必须放行 —— 见 models.CHAT_CORE_KEYS 的说明。
|
||||
if extra:
|
||||
payload.update(strip_protected(extra, CHAT_CORE_KEYS))
|
||||
return payload
|
||||
|
||||
async def chat(
|
||||
self,
|
||||
model: str,
|
||||
messages: list[dict],
|
||||
temperature: float | None = None,
|
||||
top_p: float | None = None,
|
||||
max_tokens: int | None = None,
|
||||
stream: bool = False,
|
||||
stop: str | list[str] | None = None,
|
||||
tools: list[dict] | None = None,
|
||||
tool_choice: str | dict | None = None,
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
payload = self._build_payload(
|
||||
model, messages, temperature, top_p, max_tokens,
|
||||
stream, stop, tools, tool_choice, extra,
|
||||
)
|
||||
# Bug 4 修复: 用 lease 占用 client,热重载 close 会等本次请求结束才真正关连接
|
||||
async with self._lease() as client:
|
||||
resp = await client.post("/chat/completions", json=payload, headers=self._headers)
|
||||
resp.raise_for_status()
|
||||
data = json_or_raise(resp)
|
||||
|
||||
# 统一返回格式
|
||||
return {
|
||||
"provider": self.name,
|
||||
"model": model,
|
||||
"choices": data.get("choices", []),
|
||||
"usage": data.get("usage"),
|
||||
"raw": data,
|
||||
"id": data.get("id", ""),
|
||||
}
|
||||
|
||||
async def chat_stream(
|
||||
self,
|
||||
model: str,
|
||||
messages: list[dict],
|
||||
temperature: float | None = None,
|
||||
top_p: float | None = None,
|
||||
max_tokens: int | None = None,
|
||||
stop: str | list[str] | None = None,
|
||||
tools: list[dict] | None = None,
|
||||
tool_choice: str | dict | None = None,
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> AsyncIterator[dict]:
|
||||
payload = self._build_payload(
|
||||
model, messages, temperature, top_p, max_tokens,
|
||||
True, stop, tools, tool_choice, extra,
|
||||
)
|
||||
# Bug 4 修复: lease 在整个流式期间持有占用,热重载/关闭不会掐断在途流
|
||||
async with self._lease() as client:
|
||||
async with client.stream(
|
||||
"POST", "/chat/completions", json=payload, headers=self._headers
|
||||
) as resp:
|
||||
resp.raise_for_status()
|
||||
async for line in resp.aiter_lines():
|
||||
if not line.startswith("data: "):
|
||||
continue
|
||||
raw = line[6:]
|
||||
if raw.strip() == "[DONE]":
|
||||
yield {"done": True}
|
||||
break
|
||||
# 本轮修复: 上游可能有非 JSON 的行(keep-alive `data: ping`、空行、
|
||||
# 注释、被截断的半行)。此前 json.loads 抛异常会直接**中断整个流式响应**,
|
||||
# 客户端拿到一半就断。现在跳过坏行并继续。
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
except (json.JSONDecodeError, ValueError):
|
||||
continue
|
||||
yield data
|
||||
|
||||
# ── 图像生成 ────────────────────────────────────
|
||||
async def generate_image(
|
||||
self,
|
||||
model: str,
|
||||
prompt: str,
|
||||
size: str | None = None,
|
||||
ratio: str | None = None,
|
||||
image: list[str] | None = None,
|
||||
n: int = 1,
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
endpoint = self.media.image_endpoint or "/images/generations"
|
||||
# Bug 12: 默认档位来自 media.image_default_size(默认 1K 兜底,Agnes 要求 size 必填)
|
||||
size = size or self.media.image_default_size or "1K"
|
||||
payload: dict[str, Any] = {"model": model, "prompt": prompt, "size": size}
|
||||
# #5: 仅当需要多张才带 n(Agnes 一次一张,n=1 省略避免噪音)
|
||||
if n and n != 1:
|
||||
payload["n"] = n
|
||||
if ratio:
|
||||
payload["ratio"] = ratio
|
||||
# 图生图 / 多图:图像 URL 放 extra_body.image(Agnes 写法)
|
||||
extra_body: dict[str, Any] = dict(extra or {})
|
||||
if image:
|
||||
extra_body["image"] = image
|
||||
if extra_body:
|
||||
payload["extra_body"] = extra_body
|
||||
|
||||
async with self._lease() as client:
|
||||
resp = await client.post(endpoint, json=payload, headers=self._headers)
|
||||
resp.raise_for_status()
|
||||
data = json_or_raise(resp)
|
||||
# 统一:返回 data 数组
|
||||
return {"data": data.get("data", []), "raw": data}
|
||||
|
||||
# ── 视频创建(异步任务) ────────────────────────
|
||||
async def create_video(
|
||||
self,
|
||||
model: str,
|
||||
prompt: str,
|
||||
seconds: str = "5",
|
||||
mode: str = "text",
|
||||
aspect_ratio: str = "16:9",
|
||||
size: str = "720P",
|
||||
extra: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
endpoint = self.media.video_create_endpoint or "/videos"
|
||||
payload: dict[str, Any] = {
|
||||
"model": model,
|
||||
"prompt": prompt,
|
||||
"seconds": seconds,
|
||||
"mode": mode,
|
||||
"size": size,
|
||||
"aspect_ratio": aspect_ratio,
|
||||
"n": 1,
|
||||
}
|
||||
# 合并模式专用字段(first_frame/last_frame/images/audios 等);
|
||||
# 第六轮: 但不允许覆盖 model/prompt/seconds/mode/size/aspect_ratio(同 chat 的越权面)。
|
||||
for k, v in strip_protected(extra, PROTECTED_VIDEO_KEYS).items():
|
||||
payload[k] = v
|
||||
|
||||
async with self._lease() as client:
|
||||
resp = await client.post(endpoint, json=payload, headers=self._headers)
|
||||
resp.raise_for_status()
|
||||
data = json_or_raise(resp)
|
||||
# video_id 用于查询;id/task_id 是任务 id
|
||||
return {
|
||||
"video_id": data.get("video_id") or data.get("id") or data.get("task_id"),
|
||||
"raw": data,
|
||||
}
|
||||
|
||||
# ── 视频轮询 ────────────────────────────────────
|
||||
async def poll_video(self, video_id: str, model: str = "", extra: dict[str, Any] | None = None) -> dict[str, Any]:
|
||||
mname = self.media.video_model_name or model or ""
|
||||
poll_base = self.media.video_poll_base or self.base_url
|
||||
poll_path = self.media.video_poll_path or "/agnesapi"
|
||||
|
||||
params: dict[str, str] = {"video_id": video_id}
|
||||
if mname:
|
||||
params["model_name"] = mname
|
||||
|
||||
# 轮询用绝对 URL(poll_base 可能是去掉 /v1 的域名根)
|
||||
url = poll_base.rstrip("/") + poll_path
|
||||
async with self._lease() as client:
|
||||
resp = await client.get(url, params=params, headers=self._headers)
|
||||
resp.raise_for_status()
|
||||
data = json_or_raise(resp)
|
||||
return {"raw": data}
|
||||
Reference in new issue
Block a user