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

Class ParagraphSerializers

apps/knowledge/serializers/paragraph.py:93–805  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

91
92
93class ParagraphSerializers(serializers.Serializer):
94 title = serializers.CharField(
95 required=False, max_length=256, label=_("section title"), allow_null=True, allow_blank=True
96 )
97 content = serializers.CharField(required=True, max_length=102400, label=_("section title"))
98
99 class Problem(serializers.Serializer):
100 workspace_id = serializers.CharField(required=True, label=_("workspace id"))
101 knowledge_id = serializers.UUIDField(required=True, label=_("knowledge id"))
102 document_id = serializers.UUIDField(required=True, label=_("document id"))
103 paragraph_id = serializers.UUIDField(required=True, label=_("paragraph id"))
104
105 def is_valid(self, *, raise_exception=False):
106 super().is_valid(raise_exception=True)
107 workspace_id = self.data.get("workspace_id")
108 query_set = QuerySet(Knowledge).filter(id=self.data.get("knowledge_id"))
109 if workspace_id:
110 query_set = query_set.filter(workspace_id=workspace_id)
111 if not query_set.exists():
112 raise AppApiException(500, _("Knowledge id does not exist"))
113 if not QuerySet(Paragraph).filter(id=self.data.get("paragraph_id")).exists():
114 raise AppApiException(500, _("Paragraph id does not exist"))
115
116 def list(self, with_valid=False):
117 """
118 获取问题列表
119 :param with_valid: 是否校验
120 :return: 问题列表
121 """
122 if with_valid:
123 self.is_valid(raise_exception=True)
124 problem_paragraph_mapping = QuerySet(ProblemParagraphMapping).filter(
125 knowledge_id=self.data.get("knowledge_id"), paragraph_id=self.data.get("paragraph_id")
126 )
127 return [
128 ProblemSerializer(row).data
129 for row in QuerySet(Problem).filter(id__in=[row.problem_id for row in problem_paragraph_mapping])
130 ]
131
132 @transaction.atomic
133 def save(self, instance: Dict, with_valid=True, with_embedding=True, embedding_by_problem=None):
134 if with_valid:
135 self.is_valid()
136 ProblemInstanceSerializer(data=instance).is_valid(raise_exception=True)
137 problem = (
138 QuerySet(Problem)
139 .filter(knowledge_id=self.data.get("knowledge_id"), content=instance.get("content"))
140 .first()
141 )
142 if problem is None:
143 problem = Problem(
144 id=uuid.uuid7(), knowledge_id=self.data.get("knowledge_id"), content=instance.get("content")
145 )
146 problem.save()
147 if (
148 QuerySet(ProblemParagraphMapping)
149 .filter(
150 knowledge_id=self.data.get("knowledge_id"),

Callers 1

saveMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected