Skip to content

Commit 3c39c50

Browse files
committed
fix: set paragraph positions based on maximum existing position
1 parent 93daee5 commit 3c39c50

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

apps/application/serializers/application_chat_record.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
from django.db import transaction
1414
from django.db.models import QuerySet
15+
from django.db.models.aggregates import Max
16+
from django.utils.translation import gettext_lazy as _, gettext
1517
from rest_framework import serializers
1618
from rest_framework.utils.formatting import lazy_format
1719

@@ -21,9 +23,6 @@
2123
from common.exception.app_exception import AppApiException
2224
from common.utils.common import post
2325
from knowledge.models import Paragraph, Document, Problem, ProblemParagraphMapping
24-
25-
from django.utils.translation import gettext_lazy as _, gettext
26-
2726
from knowledge.serializers.common import get_embedding_model_id_by_knowledge_id, update_document_char_length
2827
from knowledge.serializers.paragraph import ParagraphSerializers
2928
from knowledge.task.embedding import embedding_by_paragraph, embedding_by_paragraph_list
@@ -229,6 +228,11 @@ def post_improve(self, instance: Dict):
229228
chat_record.improve_paragraph_id_list.append(paragraph.id)
230229

231230
# 批量保存段落和问题映射
231+
max_position = Paragraph.objects.filter(document_id=document_id).aggregate(
232+
max_position=Max('position')
233+
)['max_position']
234+
for i, paragraph in enumerate(paragraphs):
235+
paragraph.position = max_position + i + 1
232236
Paragraph.objects.bulk_create(paragraphs)
233237
ProblemParagraphMapping.objects.bulk_create(problem_paragraph_mappings)
234238

@@ -276,11 +280,17 @@ def improve(self, instance: Dict, with_valid=True):
276280

277281
document_id = self.data.get("document_id")
278282
knowledge_id = self.data.get("knowledge_id")
279-
paragraph = Paragraph(id=uuid.uuid1(),
280-
document_id=document_id,
281-
content=instance.get("content"),
282-
knowledge_id=knowledge_id,
283-
title=instance.get("title") if 'title' in instance else '')
283+
max_position = Paragraph.objects.filter(document_id=document_id).aggregate(
284+
max_position=Max('position')
285+
)['max_position'] or 0
286+
paragraph = Paragraph(
287+
id=uuid.uuid1(),
288+
document_id=document_id,
289+
content=instance.get("content"),
290+
knowledge_id=knowledge_id,
291+
title=instance.get("title") if 'title' in instance else '',
292+
position=max_position + 1
293+
)
284294
problem_text = instance.get('problem_text') if instance.get(
285295
'problem_text') is not None else chat_record.problem_text
286296
problem, _ = QuerySet(Problem).get_or_create(content=problem_text, knowledge_id=knowledge_id)

0 commit comments

Comments
 (0)