feat(proxy): T-X6 模型池能力位过滤——vision/tools/context 硬过滤(采纳 cortiq capabilities)
- model_pool:条目新增 capabilities{vision,tools,context_window},
归一化缺省全兼容(老条目行为零变化;非法形态回落缺省);
filter_by_capabilities 硬过滤(context_window=0 视为不限)
- proxy routes:_request_needs 从请求体推断需求(多模态 image_url → vision、
tools 非空 → tools、上下文需求 = 字符/3 + min(max_tokens,4096) 与预扣同口径);
条目不满足时在池内重定向到首个合格条目
- 与 T-X1/T-X2 组合语义:能力重定向 → 预算降档 → 降级链,逐级独立不互扰
pytest 461 passed(T-X3 后 455 + 6)
This commit is contained in:
+44
-1
@@ -13,7 +13,7 @@ import json
|
||||
import re
|
||||
import threading
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Optional
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
_POOL_PATH = Path(__file__).resolve().parent.parent / "config" / "model_pool.json"
|
||||
|
||||
@@ -28,6 +28,7 @@ ENTRY_FIELDS = {
|
||||
"id", "name", "tier", "backend", "base_url", "model", "api_key",
|
||||
"price_in", "price_out", "temperature", "max_tokens", "enabled",
|
||||
"provider", "in_hit_price", # 代理层扩展(D-P3):usage 归一化 / 按命中价选上游
|
||||
"capabilities", # 能力位(T-X6):vision/tools/context_window
|
||||
}
|
||||
|
||||
# 单价默认值($/1M tokens);local 档为 0
|
||||
@@ -41,6 +42,45 @@ def _empty_pool() -> Dict[str, Any]:
|
||||
}
|
||||
|
||||
|
||||
def _normalize_capabilities(raw: Any) -> Dict[str, Any]:
|
||||
"""能力位归一(T-X6):缺省全兼容。
|
||||
|
||||
- vision/tools:缺省 True(未声明 = 不设限,老条目行为不变);
|
||||
- context_window:缺省 0 = 不按上下文长度过滤。
|
||||
"""
|
||||
if not isinstance(raw, dict):
|
||||
return {"vision": True, "tools": True, "context_window": 0}
|
||||
try:
|
||||
ctx = int(raw.get("context_window", 0) or 0)
|
||||
except (TypeError, ValueError):
|
||||
ctx = 0
|
||||
return {"vision": bool(raw.get("vision", True)),
|
||||
"tools": bool(raw.get("tools", True)),
|
||||
"context_window": max(0, ctx)}
|
||||
|
||||
|
||||
def filter_by_capabilities(entries: List[Dict[str, Any]], *,
|
||||
need_vision: bool = False, need_tools: bool = False,
|
||||
min_context_tokens: int = 0) -> List[Dict[str, Any]]:
|
||||
"""能力位硬过滤(T-X6,采纳 cortiq capabilities 过滤)。
|
||||
|
||||
依次校验 vision/tools 声明位与 context_window >= 提示+输出预估
|
||||
(context_window=0 视为不限)。返回保持原顺序的合格条目子集。
|
||||
"""
|
||||
out: List[Dict[str, Any]] = []
|
||||
for e in entries:
|
||||
cap = e.get("capabilities") or _normalize_capabilities(None)
|
||||
if need_vision and not cap.get("vision", True):
|
||||
continue
|
||||
if need_tools and not cap.get("tools", True):
|
||||
continue
|
||||
ctx = int(cap.get("context_window") or 0)
|
||||
if min_context_tokens > 0 and 0 < ctx < min_context_tokens:
|
||||
continue
|
||||
out.append(e)
|
||||
return out
|
||||
|
||||
|
||||
class PoolError(ValueError):
|
||||
"""池条目/角色配置非法。"""
|
||||
|
||||
@@ -203,6 +243,7 @@ class PoolStore:
|
||||
max_tokens = int(entry.get("max_tokens", 4096))
|
||||
except (TypeError, ValueError):
|
||||
max_tokens = 4096
|
||||
capabilities = _normalize_capabilities(entry.get("capabilities"))
|
||||
return {
|
||||
"id": eid,
|
||||
"name": str(entry.get("name") or entry.get("model") or eid),
|
||||
@@ -220,6 +261,8 @@ class PoolStore:
|
||||
"provider": (str(entry["provider"]) if entry.get("provider") else "openai"),
|
||||
"in_hit_price": (float(entry["in_hit_price"]) if entry.get("in_hit_price") is not None
|
||||
else round(price_in / 30.0, 6)),
|
||||
# 能力位(T-X6):缺省全兼容(不破坏既有条目/选型行为)
|
||||
"capabilities": capabilities,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user