This commit is contained in:
@@ -154,7 +154,7 @@ class TranscriptionTaskViewSet(viewsets.ModelViewSet):
|
||||
}
|
||||
}
|
||||
},
|
||||
responses={200: AIEvaluationSerializer}
|
||||
responses={200: AIEvaluationSerializer(many=True)}
|
||||
)
|
||||
def evaluate(self, request, pk=None):
|
||||
"""
|
||||
@@ -162,30 +162,63 @@ class TranscriptionTaskViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
task = self.get_object()
|
||||
|
||||
# 1. 检查或创建 Evaluation 对象
|
||||
evaluation, created = AIEvaluation.objects.get_or_create(task=task)
|
||||
# 1. 如果有 active template,触发所有 active template
|
||||
# 2. 如果请求体提供了 custom prompt,则创建一个 custom evaluation (no template)
|
||||
|
||||
# 2. 如果请求中有配置,更新配置
|
||||
from .models import AIEvaluationTemplate
|
||||
from .bailian_service import BailianService
|
||||
service = BailianService()
|
||||
|
||||
evaluations_to_process = []
|
||||
|
||||
# A. 如果指定了 Prompt/Model,视为手动单次评估
|
||||
model_selection = request.data.get('model_selection')
|
||||
prompt = request.data.get('prompt')
|
||||
|
||||
updated = False
|
||||
if model_selection:
|
||||
evaluation.model_selection = model_selection
|
||||
updated = True
|
||||
if prompt:
|
||||
evaluation.prompt = prompt
|
||||
updated = True
|
||||
# 创建一个不关联 Template 的评估
|
||||
eval, _ = AIEvaluation.objects.get_or_create(
|
||||
task=task,
|
||||
template=None,
|
||||
defaults={
|
||||
'model_selection': model_selection or 'qwen-plus',
|
||||
'prompt': prompt
|
||||
}
|
||||
)
|
||||
# 更新配置
|
||||
eval.model_selection = model_selection or eval.model_selection
|
||||
eval.prompt = prompt
|
||||
eval.save()
|
||||
evaluations_to_process.append(eval)
|
||||
else:
|
||||
# B. 否则触发所有 Active Templates
|
||||
active_templates = AIEvaluationTemplate.objects.filter(is_active=True)
|
||||
if not active_templates.exists():
|
||||
return Response({'message': 'No active templates and no custom prompt provided'}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
for t in active_templates:
|
||||
eval, _ = AIEvaluation.objects.get_or_create(
|
||||
task=task,
|
||||
template=t,
|
||||
defaults={
|
||||
'model_selection': t.model_selection,
|
||||
'prompt': t.prompt
|
||||
}
|
||||
)
|
||||
# 始终更新为模板最新配置? 或者保留历史? 用户意图似乎是"模版搭好...启用...生成几份"
|
||||
# 这里假设触发时应用模板当前配置
|
||||
eval.model_selection = t.model_selection
|
||||
eval.prompt = t.prompt
|
||||
eval.save()
|
||||
evaluations_to_process.append(eval)
|
||||
|
||||
# 执行评估
|
||||
for eval_obj in evaluations_to_process:
|
||||
service.evaluate_task(eval_obj)
|
||||
|
||||
if updated:
|
||||
evaluation.save()
|
||||
|
||||
# 3. 调用 Service 执行评估
|
||||
from .bailian_service import BailianService
|
||||
service = BailianService()
|
||||
service.evaluate_task(evaluation)
|
||||
|
||||
serializer = AIEvaluationSerializer(evaluation)
|
||||
# 返回该任务的所有评估结果
|
||||
all_evals = AIEvaluation.objects.filter(task=task)
|
||||
serializer = AIEvaluationSerializer(all_evals, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
@action(detail=True, methods=['get'])
|
||||
@@ -240,6 +273,9 @@ class TranscriptionTaskViewSet(viewsets.ModelViewSet):
|
||||
# 调用 Service 进行解析和更新
|
||||
service.parse_and_update_task(task, result)
|
||||
|
||||
# 重新获取 task 以包含更新后的关联字段 (如 ai_evaluations)
|
||||
task.refresh_from_db()
|
||||
|
||||
serializer = self.get_serializer(task)
|
||||
return Response(serializer.data)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user