feat: 模型管理独立页与会话级选模型

This commit is contained in:
2026-09-20 15:42:25 +08:00
parent ffb8d5f7dc
commit aac3424911
24 changed files with 845 additions and 523 deletions
+7 -1
View File
@@ -52,7 +52,13 @@ class ChatService:
raise ValueError("消息不能为空")
session = await self._sessions.require_owned(actor, thread_id)
llm = await self._llm_runtime.resolve_runtime(actor.company_id)
llm = await self._llm_runtime.resolve_runtime(
actor.company_id, model=session.llm_model
)
# 会话未记模型或回落后写回,保证列表/选框有稳定值
if not session.llm_model or session.llm_model != llm.model:
session.llm_model = llm.model
await self._session_repo.update(session)
just_saved = await self._memory.maybe_capture_from_user(
actor, thread_id=thread_id, text=text
)
+16 -6
View File
@@ -1,4 +1,4 @@
"""租户 LLM 运行时解析:Redis(Server 写入)→ Server 内部 API → 本地 fallback。"""
"""租户 LLM 运行时解析:Redis → Server 内部 API → 本地 fallback。"""
from __future__ import annotations
@@ -20,21 +20,28 @@ class LlmRuntimeService:
self._cache = cache
self._gateway = gateway
async def resolve_runtime(self, company_id: int) -> LlmRuntimeConfig:
cached = await self._cache.get(company_id)
async def resolve_runtime(
self, company_id: int, model: str | None = None
) -> LlmRuntimeConfig:
want = (model or "").strip() or None
cached = await self._cache.get(company_id, want)
if cached is not None and cached.api_key:
return cached
remote = await self._gateway.fetch(company_id)
remote = await self._gateway.fetch(company_id, want)
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
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
):
return LlmRuntimeConfig(
base_url=self._settings.llm_fallback_base_url.rstrip("/"),
@@ -45,5 +52,8 @@ class LlmRuntimeService:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="当前公司尚未配置模型 API,请先在智能助手设置中填写 OpenAI Compatible 凭证",
detail=(
"当前公司尚未配置模型 API,或所选模型未启用;"
"请在「模型管理」中配置凭证并启用模型"
),
)
+31 -2
View File
@@ -5,22 +5,41 @@ from __future__ import annotations
import uuid
from datetime import datetime
from application.llm_settings_service import LlmRuntimeService
from domain.models import Actor, Session, SessionStatus
from domain.ports import SessionRepository
class SessionService:
def __init__(self, sessions: SessionRepository) -> None:
def __init__(
self,
sessions: SessionRepository,
llm_runtime: LlmRuntimeService | None = None,
) -> None:
self._sessions = sessions
self._llm_runtime = llm_runtime
async def create(self, actor: Actor, title: str | None = None) -> Session:
async def create(
self,
actor: Actor,
title: str | 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:
try:
rt = await self._llm_runtime.resolve_runtime(actor.company_id)
model = rt.model or None
except Exception:
model = None
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_model=model,
created_at=now,
updated_at=now,
)
@@ -40,6 +59,16 @@ 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
) -> Session:
model = (llm_model or "").strip()
if not model:
raise ValueError("模型不能为空")
session = await self.require_owned(actor, thread_id)
session.llm_model = model
return await self._sessions.update(session)
async def archive(self, actor: Actor, thread_id: str) -> Session:
session = await self.require_owned(actor, thread_id)
session.status = SessionStatus.ARCHIVED