feat(llm): 上游多账号路由 + 账号级负载统计 (1.0.14)
一个 slot 挂多把上游账号 Key,解决厂商「单账号额度/并发受限」与 「同一用户缓存命中」两个矛盾诉求。 多账号路由 (plugin-llm) - models: Upstream.accounts[] (auth/note/resets) + 上游级 exhaust_status/exhaust_markers;顶层 auth 保留兼容(accounts 为空时合成单账号) - upstreams::pick_account:粘性绑定 ukb:<key> 优先 → 无绑定时 FNV-1a(下游 Key) % 可用数 冷分散 → 绑定账号冷却则重新分散 → 全冷却取最早恢复 * 分散避开厂商单账号 agent 并发上限;落点固定保住 prompt/token 缓存 - cool_until_until 天锚定冷却:boundary = day0 + ceil(已过/窗口)*窗口, 封顶次日 0 点(MiniMax 5h 窗口、24=5*4+4 → 末段 20:00-24:00 只有 4h 随天重置) - 耗尽双检测 (log 阶段):$upstream_status 命中 exhaust_status,或 usage_filter 流式扫到 marker → $llm_acct 定位本次账号 → 写冷却 uac: - shm: up:/upa:/uac:/ukb:/upe: 五类前缀;$llm_acct 变量 (NOCACHEABLE) 账号级负载统计(问题发现与跟踪) - 新增 uda:<slot>:<idx>:<day>(请求/token)与 uex:(耗尽次数) 日桶, 复用内核 usage::bump_day —— 无数据库、无新进程(项目红线) - 计数点只在 log 阶段一次;pick_account 会被 $llm_target/$llm_auth/$llm_acct 三个变量各触发一次,放进那里会三倍计数 - view() 投影带每账号 load 与 account_trend + trend_labels(日期由后端算) - 管理台「上游」页密钥列:各 Key 今日请求/token/耗尽 + 负载比例条 + 14 天迷你趋势(自绘 SVG,零第三方依赖)+ 冷却/耗尽徽标 + 上游合计 测试与验证 - 回归 279 条全绿(网关 102 + LLM 177),新增 6 条多账号/负载断言 - 假上游新增 UPSTREAM_FAIL_AUTH 模拟账号额度耗尽;靶机验证 Key 真被替换 - 前端以真实 admin.css + upstreams.js 渲染路径做无头浏览器断言: 比例条与负载成比例(412 vs 96 → 100%/23%)、每账号 14 条块、 悬停峰值日期与数据逐位吻合、单账号/无账号兜底正常、无横向溢出、零 JS 错误 文档 - 新增 docs/llm/MULTI_ACCOUNT_ROUTING.md(数据模型/路由/冷却数学/shm 布局/负载统计) - readme §11 数据模型 + §15 上游配置 + §15 用量统计 + §12.6 tag 表 + §16 变更记录 - 修正 docs/llm/*.md 中插件化前的过时路径(rust/src/* → crates/plugin-llm/src/*)
This commit is contained in:
1 parent
db99ce1521
commit
bfa3cf8ff8
21 files changed
+1506
-85
No files matched your search
@@ -79,6 +79,32 @@ fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||||
// 分隔符同时接受换行/逗号/顿号/分号——粘一份厂商文档里的模型列表是常见操作。
|
||||
let models = p.get("models").map(|raw| parse_model_list(raw));
|
||||
|
||||
// 多账号:前端把 `accounts` 作为 JSON 数组串传上来(每个 {auth, note, resets})。
|
||||
// 缺省/空 = 沿用旧逻辑(用 `auth` 维护单账号)。
|
||||
let accounts = p
|
||||
.get("accounts")
|
||||
.and_then(|s| {
|
||||
let s = s.trim();
|
||||
if s.is_empty() {
|
||||
None
|
||||
} else {
|
||||
serde_json::from_str::<Vec<crate::models::UpstreamAccount>>(s).ok()
|
||||
}
|
||||
});
|
||||
|
||||
// 上游级「额度耗尽」探测配置(JSON 数组;也兼容逗号串)
|
||||
let exhaust_status = parse_int_list(p.get("exhaust_status"));
|
||||
let exhaust_markers = p
|
||||
.get("exhaust_markers")
|
||||
.and_then(|s| {
|
||||
let s = s.trim();
|
||||
if s.is_empty() {
|
||||
None
|
||||
} else {
|
||||
serde_json::from_str::<Vec<String>>(s).ok()
|
||||
}
|
||||
});
|
||||
|
||||
let slot_raw = p.get("slot").map(String::as_str).unwrap_or("");
|
||||
|
||||
match upstreams::upsert(
|
||||
@@ -86,6 +112,9 @@ fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||||
p.get("base").map(String::as_str).unwrap_or(""),
|
||||
models,
|
||||
p.get("auth"),
|
||||
accounts,
|
||||
exhaust_status,
|
||||
exhaust_markers,
|
||||
enabled,
|
||||
p.get("note"),
|
||||
) {
|
||||
@@ -93,7 +122,8 @@ fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||||
// 用校验后的 slot 回显;直接拿原始入参会把大小写差异带进响应
|
||||
let slot = upstreams::validate_slot(slot_raw).unwrap_or_default();
|
||||
util::log_notice(&format!(
|
||||
"[api] 保存 LLM 上游 {slot} enabled={enabled} models={}",
|
||||
"[api] 保存 LLM 上游 {slot} enabled={enabled} accounts={} models={}",
|
||||
up.effective_accounts().len(),
|
||||
up.models.len()
|
||||
));
|
||||
// base_url 带「调用方正在用的端口」:见 upstreams::base_url 的说明
|
||||
@@ -103,6 +133,23 @@ fn h_admin_upstream_save(r: *mut ngx_http_request_t, _actor: Actor) -> Status {
|
||||
}
|
||||
}
|
||||
|
||||
/// 解析整数列表:优先 JSON 数组,回退逗号分隔串。
|
||||
fn parse_int_list(raw: Option<&String>) -> Option<Vec<i64>> {
|
||||
let s = raw?;
|
||||
let s = s.trim();
|
||||
if s.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if let Ok(v) = serde_json::from_str::<Vec<i64>>(s) {
|
||||
return Some(v);
|
||||
}
|
||||
Some(
|
||||
s.split(',')
|
||||
.filter_map(|x| x.trim().parse::<i64>().ok())
|
||||
.collect(),
|
||||
)
|
||||
}
|
||||
|
||||
/// 把管理台/接口传入的模型列表串拆成模型名数组。
|
||||
///
|
||||
/// 允许的写法(混用也认):`a,b` / `a、b` / `a;b` / 一行一个。
|
||||
|
||||
Reference in new issue
Block a user