Files
group_fqcd_jr/app/service/model_router_service.py
T
张胜宇 e239eb778b docs: 品牌全量口径统一为「南方基金」+ 作废文档清理
1) 客服 Agent 四份交付文档 + 构建脚手架:品牌由包装占位 XX科技 / 旧名 南方财富
   统一为南方基金(热线 400-889-8899 / 官网 nffund.com),系统名改为「智能服务系统」;
   同步追加 §0.4 修订记录行,工程记录行保留原占位字面以支撑硬编码扫描验收。
2) 开发文档:清理 28 份已作废/残留文档(14 份移出归档 + 14 份仓库副本),
   新增《文档规整方案与开发前待决事项-2026-09-17》。
3) 客服agent 四份交付文档首次纳入本分支。
2026-09-17 15:15:22 +08:00

54 lines
2.1 KiB
Python

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.model.configuration import ModelEndpointConfig
class ModelRouterService:
def __init__(self, session: AsyncSession) -> None:
self.session = session
self._unhealthy: set[str] = set()
def mark_health(self, endpoint_code: str, healthy: bool) -> None:
if healthy:
self._unhealthy.discard(endpoint_code)
else:
self._unhealthy.add(endpoint_code)
async def select_endpoint(
self, *, agent_type: str, task_type: str, required_capability: str | None = None
) -> ModelEndpointConfig | None:
statement = select(ModelEndpointConfig).where(ModelEndpointConfig.status == "active")
endpoints = list(await self.session.scalars(statement))
if required_capability:
endpoints = [e for e in endpoints if required_capability in e.capabilities]
endpoints = [e for e in endpoints if e.endpoint_code not in self._unhealthy]
return endpoints[0] if endpoints else None
async def select_with_fallback(
self,
*,
agent_type: str,
task_type: str,
fallback_codes: list[str] | None = None,
required_capability: str | None = None,
) -> list[ModelEndpointConfig]:
endpoints = list(
await self.session.scalars(
select(ModelEndpointConfig).where(ModelEndpointConfig.status == "active")
)
)
if required_capability:
endpoints = [e for e in endpoints if required_capability in e.capabilities]
endpoints = [e for e in endpoints if e.endpoint_code not in self._unhealthy]
by_code = {endpoint.endpoint_code: endpoint for endpoint in endpoints}
ordered: list[ModelEndpointConfig] = []
for code in fallback_codes or []:
endpoint = by_code.get(code)
if endpoint is not None and endpoint not in ordered:
ordered.append(endpoint)
for endpoint in endpoints:
if endpoint not in ordered:
ordered.append(endpoint)
return ordered