|
12 | 12 |
|
13 | 13 | from django.db import transaction |
14 | 14 | from django.db.models import QuerySet |
| 15 | +from django.db.models.aggregates import Max |
| 16 | +from django.utils.translation import gettext_lazy as _, gettext |
15 | 17 | from rest_framework import serializers |
16 | 18 | from rest_framework.utils.formatting import lazy_format |
17 | 19 |
|
|
21 | 23 | from common.exception.app_exception import AppApiException |
22 | 24 | from common.utils.common import post |
23 | 25 | from knowledge.models import Paragraph, Document, Problem, ProblemParagraphMapping |
24 | | - |
25 | | -from django.utils.translation import gettext_lazy as _, gettext |
26 | | - |
27 | 26 | from knowledge.serializers.common import get_embedding_model_id_by_knowledge_id, update_document_char_length |
28 | 27 | from knowledge.serializers.paragraph import ParagraphSerializers |
29 | 28 | from knowledge.task.embedding import embedding_by_paragraph, embedding_by_paragraph_list |
@@ -229,6 +228,11 @@ def post_improve(self, instance: Dict): |
229 | 228 | chat_record.improve_paragraph_id_list.append(paragraph.id) |
230 | 229 |
|
231 | 230 | # 批量保存段落和问题映射 |
| 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 |
232 | 236 | Paragraph.objects.bulk_create(paragraphs) |
233 | 237 | ProblemParagraphMapping.objects.bulk_create(problem_paragraph_mappings) |
234 | 238 |
|
@@ -276,11 +280,17 @@ def improve(self, instance: Dict, with_valid=True): |
276 | 280 |
|
277 | 281 | document_id = self.data.get("document_id") |
278 | 282 | 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 | + ) |
284 | 294 | problem_text = instance.get('problem_text') if instance.get( |
285 | 295 | 'problem_text') is not None else chat_record.problem_text |
286 | 296 | problem, _ = QuerySet(Problem).get_or_create(content=problem_text, knowledge_id=knowledge_id) |
|
0 commit comments