assemble.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """组装:把帖子+知识片段+解构,组装成 ingest 请求体(对齐 创作知识-重构设计.md §6)。
  2. 纯函数,无 LLM、无 IO。同一帖子拆出的多条知识复用同一 source.id。
  3. """
  4. from __future__ import annotations
  5. import json
  6. from creation_knowledge.models import (
  7. Deconstruction,
  8. IngestPayload,
  9. KnowledgeItem,
  10. Post,
  11. )
  12. def build_ingest_payload(
  13. post: Post, item: KnowledgeItem, deco: Deconstruction
  14. ) -> IngestPayload:
  15. content = json.dumps(
  16. {"what": item.what, "why": item.why, "how": item.how},
  17. ensure_ascii=False,
  18. )
  19. custom_ext: list[dict] = []
  20. if item.evidence:
  21. custom_ext.append(
  22. {"key": "原文证据", "type": "str", "value": " / ".join(item.evidence)}
  23. )
  24. if deco.stage_reason:
  25. custom_ext.append(
  26. {"key": "阶段判断理由", "type": "str", "value": deco.stage_reason}
  27. )
  28. if deco.scope_reason:
  29. custom_ext.append(
  30. {"key": "作用域判断理由", "type": "str", "value": deco.scope_reason}
  31. )
  32. return IngestPayload(
  33. source={
  34. "id": post.id,
  35. "source_type": "post",
  36. "title": post.title,
  37. "author": post.author_name,
  38. "source_metadata": {"platform": post.platform, "url": post.url},
  39. },
  40. title=item.title,
  41. content=content,
  42. dim_attributes=list(item.knowledge_types),
  43. dim_creations=list(deco.stages),
  44. scopes=[{"scope_type": s.scope_type, "value": s.value} for s in deco.scopes],
  45. custom_ext=custom_ext,
  46. )