| 1234567891011121314151617181920212223242526272829303132333435363738 |
- """解构:给知识片段补上阶段(灵感/选题/脚本)和作用域(五棵树)。"""
- from __future__ import annotations
- from typing import Optional
- from creation_knowledge.integrations.llm import ChatFn, default_chat
- from creation_knowledge.models import Deconstruction, KnowledgeItem, Scope
- from creation_knowledge.prompts import load_prompt
- SYSTEM = "你是严谨的创作知识解构器,按阶段和五棵分类树归类。"
- VALID_STAGES = {"灵感", "选题", "脚本"}
- VALID_SCOPES = {"substance", "form", "feeling", "effect", "intent"}
- def deconstruct_item(
- item: KnowledgeItem,
- *,
- chat: Optional[ChatFn] = None,
- env_file: str = ".env",
- ) -> Deconstruction:
- chat = chat or default_chat(env_file)
- user = load_prompt("deconstruct").format(
- item=item.model_dump_json(indent=2)
- )
- data = chat(SYSTEM, user)
- stages = [s for s in (data.get("stages") or []) if s in VALID_STAGES]
- scopes = [
- Scope(scope_type=s["scope_type"], value=str(s["value"]).strip())
- for s in (data.get("scopes") or [])
- if isinstance(s, dict) and s.get("scope_type") in VALID_SCOPES and s.get("value")
- ]
- return Deconstruction(
- stages=stages,
- scopes=scopes,
- stage_reason=str(data.get("stage_reason") or ""),
- scope_reason=str(data.get("scope_reason") or ""),
- )
|