feat: 公司支持多模型提供商与会话消歧选型

This commit is contained in:
2026-09-20 16:35:06 +08:00
parent d7e4a987c1
commit 9350323ff2
30 changed files with 1031 additions and 448 deletions
+11 -3
View File
@@ -53,11 +53,19 @@ class ChatService:
session = await self._sessions.require_owned(actor, thread_id)
llm = await self._llm_runtime.resolve_runtime(
actor.company_id, model=session.llm_model
actor.company_id,
provider_id=session.llm_provider_id,
model=session.llm_model,
)
# 会话未记模型或回落后写回,保证列表/选框有稳定值
if not session.llm_model or session.llm_model != llm.model:
# 回落默认或消歧后写回会话,保证列表/选框稳定
changed = False
if session.llm_model != llm.model:
session.llm_model = llm.model
changed = True
if llm.provider_id is not None and session.llm_provider_id != llm.provider_id:
session.llm_provider_id = llm.provider_id
changed = True
if changed:
await self._session_repo.update(session)
just_saved = await self._memory.maybe_capture_from_user(
actor, thread_id=thread_id, text=text
+12 -7
View File
@@ -21,32 +21,37 @@ class LlmRuntimeService:
self._gateway = gateway
async def resolve_runtime(
self, company_id: int, model: str | None = None
self,
company_id: int,
provider_id: int | None = None,
model: str | None = None,
) -> LlmRuntimeConfig:
want = (model or "").strip() or None
cached = await self._cache.get(company_id, want)
want_model = (model or "").strip() or None
want_provider = provider_id
cached = await self._cache.get(company_id, want_provider, want_model)
if cached is not None and cached.api_key:
return cached
remote = await self._gateway.fetch(company_id, want)
remote = await self._gateway.fetch(company_id, want_provider, want_model)
if remote is not None and remote.api_key and remote.base_url and remote.model:
await self._cache.set(
company_id,
remote,
self._settings.llm_cache_ttl_seconds,
model=remote.model,
)
return remote
if (
self._settings.llm_fallback_enabled
and self._settings.llm_fallback_api_key
and not want
and not want_model
and want_provider is None
):
return LlmRuntimeConfig(
base_url=self._settings.llm_fallback_base_url.rstrip("/"),
api_key=self._settings.llm_fallback_api_key,
model=self._settings.llm_fallback_model,
provider_id=None,
context_window=None,
)
@@ -54,6 +59,6 @@ class LlmRuntimeService:
status_code=status.HTTP_400_BAD_REQUEST,
detail=(
"当前公司尚未配置模型 API,或所选模型未启用;"
"请在「模型管理」中配置凭证并启用模型"
"请在「模型管理」中配置提供商并启用模型"
),
)
+22 -6
View File
@@ -23,22 +23,30 @@ class SessionService:
self,
actor: Actor,
title: str | None = None,
llm_provider_id: int | None = None,
llm_model: str | None = None,
) -> Session:
now = datetime.utcnow()
model = (llm_model or "").strip() or None
if not model and self._llm_runtime is not None:
provider_id = llm_provider_id
if (not model or provider_id is None) and self._llm_runtime is not None:
try:
rt = await self._llm_runtime.resolve_runtime(actor.company_id)
model = rt.model or None
rt = await self._llm_runtime.resolve_runtime(
actor.company_id,
provider_id=provider_id,
model=model,
)
model = rt.model or model
provider_id = rt.provider_id if rt.provider_id is not None else provider_id
except Exception:
model = None
pass
session = Session(
thread_id=str(uuid.uuid4()),
company_id=actor.company_id,
user_id=actor.user_id,
title=(title or "新对话").strip() or "新对话",
status=SessionStatus.ACTIVE,
llm_provider_id=provider_id,
llm_model=model,
created_at=now,
updated_at=now,
@@ -59,13 +67,21 @@ class SessionService:
session.title = title.strip() or session.title
return await self._sessions.update(session)
async def update_llm_model(
self, actor: Actor, thread_id: str, llm_model: str
async def update_llm_selection(
self,
actor: Actor,
thread_id: str,
*,
llm_provider_id: int,
llm_model: str,
) -> Session:
model = (llm_model or "").strip()
if not model:
raise ValueError("模型不能为空")
if llm_provider_id is None or int(llm_provider_id) <= 0:
raise ValueError("提供商不能为空")
session = await self.require_owned(actor, thread_id)
session.llm_provider_id = int(llm_provider_id)
session.llm_model = model
return await self._sessions.update(session)