MCPcopy Create free account
hub / github.com/1Panel-dev/MaxKB / save

Method save

apps/knowledge/serializers/paragraph.py:337–393  ·  view source on GitHub ↗
(self, instance: Dict, with_valid=True, with_embedding=True)

Source from the content-addressed store, hash-verified

335
336 @transaction.atomic
337 def save(self, instance: Dict, with_valid=True, with_embedding=True):
338 if with_valid:
339 ParagraphSerializers(data=instance).is_valid(raise_exception=True)
340 self.is_valid()
341 knowledge_id = self.data.get("knowledge_id")
342 document_id = self.data.get("document_id")
343
344 # 先将同一文档中的所有段落位置向下移动一位
345 Paragraph.objects.filter(document_id=document_id).update(position=F("position") + 1)
346
347 paragraph_problem_model = self.get_paragraph_problem_model(knowledge_id, document_id, instance)
348 paragraph = paragraph_problem_model.get("paragraph")
349 problem_paragraph_object_list = paragraph_problem_model.get("problem_paragraph_object_list")
350 problem_model_list, problem_paragraph_mapping_list = ProblemParagraphManage(
351 problem_paragraph_object_list, knowledge_id
352 ).to_problem_model_list()
353 # 新加的在最上面
354 paragraph.position = 1
355 paragraph.save()
356 # 调整位置
357 if "position" in instance:
358 if type(instance["position"]) is not int:
359 instance["position"] = 1
360 else:
361 instance["position"] = 1
362
363 ParagraphSerializers.AdjustPosition(
364 data={
365 "paragraph_id": str(paragraph.id),
366 "knowledge_id": knowledge_id,
367 "document_id": document_id,
368 "workspace_id": self.data.get("workspace_id"),
369 }
370 ).adjust_position(instance.get("position"))
371 # 插入問題
372 QuerySet(Problem).bulk_create(problem_model_list) if len(problem_model_list) > 0 else None
373 # 插入问题关联关系
374 QuerySet(ProblemParagraphMapping).bulk_create(problem_paragraph_mapping_list) if len(
375 problem_paragraph_mapping_list
376 ) > 0 else None
377 # 修改长度
378 update_document_char_length(document_id)
379 if with_embedding:
380 model_id = get_embedding_model_id_by_knowledge_id(knowledge_id)
381 embedding_by_paragraph(str(paragraph.id), model_id)
382 ListenerManagement.update_status(
383 QuerySet(Document).filter(id=document_id), TaskType.EMBEDDING, State.SUCCESS
384 )
385 ListenerManagement.get_aggregation_document_status(document_id)()
386 return ParagraphSerializers.Operate(
387 data={
388 "paragraph_id": str(paragraph.id),
389 "knowledge_id": knowledge_id,
390 "document_id": document_id,
391 "workspace_id": self.data.get("workspace_id"),
392 }
393 ).one(with_valid=True)
394

Callers 5

saveMethod · 0.45
editMethod · 0.45
associationMethod · 0.45
migrateMethod · 0.45
adjust_positionMethod · 0.45

Calls 14

is_validMethod · 0.95
embedding_by_paragraphFunction · 0.90
to_problem_model_listMethod · 0.80
adjust_positionMethod · 0.80
is_validMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected