fix flowchart
This commit is contained in:
@@ -155,7 +155,7 @@ def stream_ai_response(client, system_prompt, user_prompt, on_complete=None):
|
||||
"""SSE 流式响应生成器,on_complete(full_text) 在流结束时调用"""
|
||||
try:
|
||||
stream = client.chat.completions.create(
|
||||
model="deepseek-reasoner",
|
||||
model="deepseek-v4-flash",
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_prompt},
|
||||
@@ -574,7 +574,7 @@ class AILoginSummaryAPI(APIView):
|
||||
|
||||
try:
|
||||
completion = client.chat.completions.create(
|
||||
model="deepseek-reasoner",
|
||||
model="deepseek-v4-flash",
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_prompt},
|
||||
@@ -608,7 +608,7 @@ class AIAnalysisAPI(APIView):
|
||||
AIAnalysis.objects.create(
|
||||
user=request.user,
|
||||
provider="deepseek",
|
||||
model="deepseek-reasoner",
|
||||
model="deepseek-v4-flash",
|
||||
data={"details": details, "duration": duration},
|
||||
system_prompt=system_prompt,
|
||||
user_prompt="这段时间内的详细数据,每周或每月的数据。",
|
||||
|
||||
@@ -11,6 +11,9 @@ class CreateFlowchartSubmissionSerializer(serializers.Serializer):
|
||||
def validate_mermaid_code(self, value):
|
||||
if not value.strip():
|
||||
raise serializers.ValidationError("Mermaid代码不能为空")
|
||||
lines = [line for line in value.split("\n") if line.strip()]
|
||||
if len(lines) > 200:
|
||||
raise serializers.ValidationError("流程图过于复杂,请简化后提交")
|
||||
return value
|
||||
|
||||
def validate_flowchart_data(self, value):
|
||||
@@ -21,11 +24,13 @@ class CreateFlowchartSubmissionSerializer(serializers.Serializer):
|
||||
|
||||
|
||||
class FlowchartSubmissionSerializer(serializers.ModelSerializer):
|
||||
username = serializers.CharField(source="user.username", read_only=True)
|
||||
|
||||
class Meta:
|
||||
model = FlowchartSubmission
|
||||
fields = [
|
||||
"id",
|
||||
"user",
|
||||
"username",
|
||||
"problem",
|
||||
"mermaid_code",
|
||||
"flowchart_data",
|
||||
|
||||
@@ -14,6 +14,7 @@ from .models import FlowchartSubmission, FlowchartSubmissionStatus
|
||||
@dramatiq.actor(**DRAMATIQ_WORKER_ARGS(max_retries=3))
|
||||
def evaluate_flowchart_task(submission_id):
|
||||
"""异步AI评分任务"""
|
||||
submission = None
|
||||
try:
|
||||
submission = FlowchartSubmission.objects.get(id=submission_id)
|
||||
|
||||
@@ -54,19 +55,19 @@ def evaluate_flowchart_task(submission_id):
|
||||
client = get_ai_client()
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="deepseek-reasoner",
|
||||
model="deepseek-v4-flash",
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": user_prompt}
|
||||
],
|
||||
temperature=0.3,
|
||||
)
|
||||
|
||||
|
||||
ai_response = response.choices[0].message.content
|
||||
score_data = parse_ai_evaluation_response(ai_response)
|
||||
|
||||
|
||||
processing_time = time.time() - start_time
|
||||
|
||||
|
||||
# 保存评分结果
|
||||
with transaction.atomic():
|
||||
submission.ai_score = score_data['score']
|
||||
@@ -75,7 +76,7 @@ def evaluate_flowchart_task(submission_id):
|
||||
submission.ai_suggestions = score_data.get('suggestions', '')
|
||||
submission.ai_criteria_details = score_data.get('criteria_details', {})
|
||||
submission.ai_provider = 'deepseek'
|
||||
submission.ai_model = 'deepseek-reasoner'
|
||||
submission.ai_model = 'deepseek-v4-flash'
|
||||
submission.processing_time = processing_time
|
||||
submission.status = FlowchartSubmissionStatus.COMPLETED
|
||||
submission.evaluation_time = timezone.now()
|
||||
@@ -94,21 +95,20 @@ def evaluate_flowchart_task(submission_id):
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
# 处理失败
|
||||
submission.status = FlowchartSubmissionStatus.FAILED
|
||||
submission.save()
|
||||
|
||||
# 推送错误通知
|
||||
from utils.websocket import push_flowchart_evaluation_update
|
||||
push_flowchart_evaluation_update(
|
||||
submission_id=str(submission.id),
|
||||
user_id=submission.user_id,
|
||||
data={
|
||||
"type": "flowchart_evaluation_failed",
|
||||
"submission_id": str(submission.id),
|
||||
"error": str(e)
|
||||
}
|
||||
)
|
||||
if submission is not None:
|
||||
submission.status = FlowchartSubmissionStatus.FAILED
|
||||
submission.save()
|
||||
|
||||
from utils.websocket import push_flowchart_evaluation_update
|
||||
push_flowchart_evaluation_update(
|
||||
submission_id=str(submission.id),
|
||||
user_id=submission.user_id,
|
||||
data={
|
||||
"type": "flowchart_evaluation_failed",
|
||||
"submission_id": str(submission.id),
|
||||
"error": str(e),
|
||||
},
|
||||
)
|
||||
raise e
|
||||
|
||||
def build_evaluation_prompt(problem):
|
||||
@@ -159,11 +159,17 @@ def build_evaluation_prompt(problem):
|
||||
def parse_ai_evaluation_response(ai_response):
|
||||
"""解析AI评分响应,解析失败时抛出异常由调用方处理"""
|
||||
import re
|
||||
json_match = re.search(r'\{.*\}', ai_response, re.DOTALL)
|
||||
if not json_match:
|
||||
raise ValueError("AI响应中未找到JSON数据")
|
||||
# 优先匹配代码块中的 JSON,避免贪婪匹配误抓 reasoning 段落
|
||||
code_block = re.search(r'```(?:json)?\s*(\{[\s\S]*?\})\s*```', ai_response, re.DOTALL)
|
||||
if code_block:
|
||||
json_str = code_block.group(1)
|
||||
else:
|
||||
json_match = re.search(r'\{.*\}', ai_response, re.DOTALL)
|
||||
if not json_match:
|
||||
raise ValueError("AI响应中未找到JSON数据")
|
||||
json_str = json_match.group()
|
||||
|
||||
data = json.loads(json_match.group())
|
||||
data = json.loads(json_str)
|
||||
|
||||
if "score" not in data or "grade" not in data:
|
||||
raise ValueError("AI响应缺少必要字段: score 或 grade")
|
||||
|
||||
@@ -80,10 +80,13 @@ class FlowchartSubmissionListAPI(APIView):
|
||||
except Problem.DoesNotExist:
|
||||
return self.error("Problem doesn't exist")
|
||||
queryset = queryset.filter(problem=problem)
|
||||
|
||||
if myself and myself == "1":
|
||||
queryset = queryset.filter(user=request.user)
|
||||
if username:
|
||||
elif username:
|
||||
queryset = queryset.filter(user__username__icontains=username)
|
||||
elif request.user.is_regular_user():
|
||||
queryset = queryset.filter(user=request.user)
|
||||
|
||||
data = self.paginate_data(request, queryset)
|
||||
data["results"] = FlowchartSubmissionListSerializer(
|
||||
@@ -151,7 +154,10 @@ class FlowchartSubmissionDetailAPI(APIView):
|
||||
except Problem.DoesNotExist:
|
||||
return self.error("Problem doesn't exist")
|
||||
|
||||
page = int(request.GET.get("page", 0))
|
||||
try:
|
||||
page = int(request.GET.get("page", 0))
|
||||
except ValueError:
|
||||
return self.error("page must be an integer")
|
||||
submissions = FlowchartSubmission.objects.filter(
|
||||
user=request.user,
|
||||
problem=problem,
|
||||
|
||||
@@ -494,7 +494,7 @@ class ProblemFlowchartAIGen(APIView):
|
||||
python_code = request.data.get("python", "")
|
||||
client = get_ai_client()
|
||||
response = client.chat.completions.create(
|
||||
model="deepseek-reasoner",
|
||||
model="deepseek-v4-flash",
|
||||
messages=[
|
||||
{
|
||||
"role": "system",
|
||||
|
||||
Reference in New Issue
Block a user