fix: stabilize admin ui and agent chat model fallback

This commit is contained in:
2026-06-14 10:32:47 +08:00
parent 58ce877e64
commit c293cb5e3b
4 changed files with 166 additions and 9 deletions
@@ -75,7 +75,8 @@ class AgentService:
流式事件
"""
# 检查模型是否支持 Function Calling
supports_function_call = await self._check_model_supports_function_call(agent.model_id)
model_id = await self._resolve_agent_model_id(agent)
supports_function_call = await self._check_model_supports_function_call(model_id)
if supports_function_call:
async for event in self._chat_autonomous_function_calling(agent, conversation, user_message, attachments):
@@ -94,6 +95,43 @@ class AgentService:
)
model = result.scalar_one_or_none()
return model.supports_function_call if model else False
async def _resolve_agent_model_id(self, agent: Agent) -> Optional[str]:
if not self._db:
return str(agent.model_id) if agent.model_id else None
if agent.model_id:
result = await self._db.execute(
select(LLMModel).where(
LLMModel.id == agent.model_id,
LLMModel.is_deleted == False,
LLMModel.is_active == True,
)
)
if result.scalar_one_or_none():
return str(agent.model_id)
result = await self._db.execute(
select(LLMModel)
.where(
LLMModel.is_deleted == False,
LLMModel.is_active == True,
LLMModel.model_type == "chat",
)
.order_by(LLMModel.sort.desc(), LLMModel.sys_create_datetime.desc())
)
model = result.scalars().first()
if not model:
return None
agent.model_id = model.id
await self._db.flush()
logger.info(
"Agent %s has no active model, fallback to default chat model %s",
getattr(agent, "code", agent.id),
model.id,
)
return str(model.id)
async def _chat_autonomous_function_calling(
self,
@@ -214,6 +252,9 @@ class AgentService:
# 检查是否启用流式输出
enable_streaming = getattr(agent, 'enable_streaming', True)
model_id = await self._resolve_agent_model_id(agent)
if not model_id:
raise ValueError('未找到可用的 chat 模型,请先在模型配置中启用一个模型')
total_tokens = 0
final_answer = ''
@@ -222,7 +263,7 @@ class AgentService:
# 流式输出
accumulated_content = ''
async for chunk in self.llm_service.chat_stream(
model_id=str(agent.model_id),
model_id=model_id,
messages=messages,
temperature=agent.temperature or 0.7,
max_tokens=agent.max_tokens or 2048,
@@ -241,7 +282,7 @@ class AgentService:
else:
# 非流式输出
response = await self.llm_service.chat_async(
model_id=str(agent.model_id),
model_id=model_id,
messages=messages,
temperature=agent.temperature or 0.7,
max_tokens=agent.max_tokens or 2048,