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