MCPcopy Index your code
hub / github.com/1Panel-dev/MaxKB / save

Method save

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

Source from the content-addressed store, hash-verified

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"),
151 problem_id=problem.id,
152 paragraph_id=self.data.get("paragraph_id"),
153 )
154 .exists()
155 ):
156 raise AppApiException(500, _("Already associated, please do not associate again"))
157 problem_paragraph_mapping = ProblemParagraphMapping(
158 id=uuid.uuid7(),
159 problem_id=problem.id,
160 document_id=self.data.get("document_id"),
161 paragraph_id=self.data.get("paragraph_id"),
162 knowledge_id=self.data.get("knowledge_id"),
163 )
164 problem_paragraph_mapping.save()
165 model_id = get_embedding_model_id_by_knowledge_id(self.data.get("knowledge_id"))
166 if with_embedding:
167 embedding_by_problem_task(
168 {
169 "text": problem.content,
170 "is_active": True,
171 "source_type": SourceType.PROBLEM,
172 "source_id": problem_paragraph_mapping.id,
173 "document_id": self.data.get("document_id"),
174 "paragraph_id": self.data.get("paragraph_id"),
175 "knowledge_id": self.data.get("knowledge_id"),
176 },
177 model_id,
178 )
179
180 return ProblemSerializers.Operate(
181 data={
182 "workspace_id": self.data.get("workspace_id"),
183 "knowledge_id": self.data.get("knowledge_id"),
184 "problem_id": problem.id,
185 }
186 ).one(with_valid=True)
187
188 class Operate(serializers.Serializer):
189 workspace_id = serializers.CharField(required=True, label=_("workspace id"))

Callers

nothing calls this directly

Calls 10

is_validMethod · 0.95
ProblemClass · 0.90
AppApiExceptionClass · 0.90
is_validMethod · 0.45
getMethod · 0.45
saveMethod · 0.45
oneMethod · 0.45

Tested by

no test coverage detected