Add prompt improvement button in drawer

This commit is contained in:
Xin Wang
2026-08-26 12:18:18 +08:00
parent 239b9027ca
commit ab409ba9c0
13 changed files with 2067 additions and 39 deletions
+63 -1
View File
@@ -15,12 +15,19 @@ from db.models import (
)
from db.session import get_session
from fastapi import APIRouter, Depends, HTTPException
from models import RuntimeModelResource
from services.auth import require_admin
from services.config_resolver import resolve_runtime_config
from services.runtime_variables import DynamicVariableError, prepare_dynamic_config
from services.realtime.launcher import validate_visual_runtime
from services.test_case_assets import canonicalize_test_assets, image_asset_ids
from services.test_runs.orchestrator import test_run_orchestrator
from services.test_runs.prompt_improvement import (
PROMPT_IMPROVEMENT_SNAPSHOT_KEY,
PromptImprovementAgent,
PromptImprovementError,
public_test_run_config,
)
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from test_schemas import (
@@ -31,6 +38,7 @@ from test_schemas import (
BatchStartupResult,
BatchTurnResult,
BatchEvaluationResult,
PromptImprovementOut,
TestCaseDefinition,
)
@@ -92,7 +100,7 @@ async def _run_out(
status=run.status,
title=run.title,
assistant_name=run.assistant_name,
config=dict(run.config or {}),
config=public_test_run_config(run.config),
cases=cases,
started_at=run.started_at or run.created_at,
finished_at=run.finished_at,
@@ -245,6 +253,60 @@ async def create_test_run(
return await _run_out(session, run)
@router.post(
"/{run_id}/prompt-improvement",
response_model=PromptImprovementOut,
)
async def create_prompt_improvement(
run_id: str,
session: AsyncSession = Depends(get_session),
):
run = await session.get(TestRun, run_id)
if run is None:
raise HTTPException(404, "测试运行不存在")
rows = (
await session.execute(
select(TestRunCase)
.where(TestRunCase.run_id == run.id)
.order_by(TestRunCase.position)
)
).scalars().all()
if len(rows) != 1:
raise HTTPException(409, "Prompt 改进仅支持单用例测试运行")
run_case = rows[0]
if run.status != "completed" or run_case.status != "fail":
raise HTTPException(409, "只有已完成且断言失败的用例可以生成 Prompt 改进")
config = dict(run.config or {})
snapshot = config.get(PROMPT_IMPROVEMENT_SNAPSHOT_KEY)
if not isinstance(snapshot, dict):
raise HTTPException(409, "该运行缺少 Prompt 配置快照,请重新测试")
if snapshot.get("assistantType") != "prompt" or snapshot.get("runtimeMode") != "pipeline":
raise HTTPException(409, "Prompt 改进仅支持 Pipeline Prompt 助手")
evaluator_id = str(config.get("evaluatorModelResourceId") or "")
evaluator = await session.get(ModelResource, evaluator_id)
if evaluator is None or evaluator.capability != "LLM" or not evaluator.enabled:
raise HTTPException(422, "该运行使用的评估模型已不存在或不可用")
resource = RuntimeModelResource(
id=evaluator.id,
name=evaluator.name,
capability=evaluator.capability,
interface_type=evaluator.interface_type,
values=evaluator.values or {},
secrets=evaluator.secrets or {},
support_image_input=bool(evaluator.support_image_input),
)
try:
return await PromptImprovementAgent(resource).improve(
snapshot=snapshot,
case_snapshot=dict(run_case.case_snapshot or {}),
result=dict(run_case.result or {}),
)
except PromptImprovementError as exc:
raise HTTPException(exc.status_code, exc.message) from exc
@router.get("/{run_id}", response_model=BatchRunSnapshotOut)
async def get_test_run(
run_id: str,