Add analysis feature
This commit is contained in:
@@ -277,6 +277,17 @@ async def _validate_vision_model(
|
||||
raise HTTPException(400, "视觉模型必须支持图片输入")
|
||||
|
||||
|
||||
async def _validate_analysis_model(
|
||||
session: AsyncSession, body: AssistantUpsert
|
||||
) -> None:
|
||||
config = body.analysis_config
|
||||
if not config.enabled:
|
||||
return
|
||||
resource = await session.get(ModelResource, config.model_resource_id)
|
||||
if not resource or not resource.enabled or resource.capability != "LLM":
|
||||
raise HTTPException(400, "分析模型必须引用已启用的 LLM 模型资源")
|
||||
|
||||
|
||||
async def _validate_knowledge_base(session: AsyncSession, body: AssistantUpsert) -> None:
|
||||
if body.runtime_mode != "pipeline" or body.type not in {"prompt", "workflow"}:
|
||||
body.knowledge_base_id = None
|
||||
@@ -384,6 +395,7 @@ async def _to_out(session: AsyncSession, assistant: Assistant) -> AssistantOut:
|
||||
tool_ids=await _tool_ids(session, assistant.id),
|
||||
prompt=assistant.prompt,
|
||||
dynamic_variable_definitions=assistant.dynamic_variable_definitions or {},
|
||||
analysis_config=assistant.analysis_config or {},
|
||||
api_url=assistant.api_url,
|
||||
api_key=mask(assistant.api_key),
|
||||
app_id=assistant.app_id,
|
||||
@@ -413,6 +425,7 @@ async def create_assistant(
|
||||
await _validate_system_tool_selection(session, body)
|
||||
await _validate_startup_actions(session, body)
|
||||
await _validate_vision_model(session, body)
|
||||
await _validate_analysis_model(session, body)
|
||||
await _validate_knowledge_base(session, body)
|
||||
data = body.model_dump()
|
||||
resource_ids = data.pop("model_resource_ids")
|
||||
@@ -459,6 +472,7 @@ async def duplicate_assistant(
|
||||
knowledge_retrieval_config=dict(source.knowledge_retrieval_config or {}),
|
||||
prompt=source.prompt,
|
||||
dynamic_variable_definitions=dict(source.dynamic_variable_definitions or {}),
|
||||
analysis_config=dict(source.analysis_config or {}),
|
||||
api_url=source.api_url,
|
||||
api_key=source.api_key,
|
||||
app_id=source.app_id,
|
||||
@@ -489,6 +503,7 @@ async def update_assistant(
|
||||
await _validate_system_tool_selection(session, body)
|
||||
await _validate_startup_actions(session, body)
|
||||
await _validate_vision_model(session, body)
|
||||
await _validate_analysis_model(session, body)
|
||||
await _validate_knowledge_base(session, body)
|
||||
data = body.model_dump()
|
||||
resource_ids = data.pop("model_resource_ids")
|
||||
|
||||
@@ -8,6 +8,8 @@ from fastapi import APIRouter, Depends, HTTPException, Query, Response
|
||||
from loguru import logger
|
||||
from schemas import (
|
||||
ConversationArtifactOut,
|
||||
ConversationAnalysisFieldOut,
|
||||
ConversationAnalysisOut,
|
||||
ConversationDetailOut,
|
||||
ConversationListOut,
|
||||
ConversationMessageOut,
|
||||
@@ -40,6 +42,33 @@ def _session_out(row: ConversationSession) -> ConversationOut:
|
||||
)
|
||||
|
||||
|
||||
def _analysis_out(row: ConversationSession) -> ConversationAnalysisOut:
|
||||
data = row.analysis_data or {}
|
||||
plan = data.get("plan") if isinstance(data.get("plan"), dict) else {}
|
||||
definitions = plan.get("fields") if isinstance(plan.get("fields"), list) else []
|
||||
result = data.get("result") if isinstance(data.get("result"), dict) else {}
|
||||
fields = []
|
||||
for definition in definitions:
|
||||
if not isinstance(definition, dict):
|
||||
continue
|
||||
name = str(definition.get("name") or "")
|
||||
if not name:
|
||||
continue
|
||||
fields.append(
|
||||
ConversationAnalysisFieldOut(
|
||||
name=name,
|
||||
type=str(definition.get("type") or "string"),
|
||||
value=result.get(name),
|
||||
)
|
||||
)
|
||||
return ConversationAnalysisOut(
|
||||
status=row.analysis_status or "none",
|
||||
fields=fields,
|
||||
error=row.analysis_error or "",
|
||||
completed_at=data.get("completedAt"),
|
||||
)
|
||||
|
||||
|
||||
@router.get("", response_model=ConversationListOut)
|
||||
async def list_conversations(
|
||||
page: int = Query(1, ge=1),
|
||||
@@ -119,6 +148,7 @@ async def get_conversation(
|
||||
return ConversationDetailOut(
|
||||
**_session_out(conversation).model_dump(),
|
||||
extra=conversation.extra or {},
|
||||
analysis=_analysis_out(conversation),
|
||||
messages=[
|
||||
ConversationMessageOut(
|
||||
id=message.id,
|
||||
|
||||
Reference in New Issue
Block a user