跳到內容

让 LLM 编写 XML

本頁內容尚未翻譯。

森空岛 Wiki XML 相比原始 Wiki JSON 节省约 75% 原始字节和 87% 行数,显著降低了大语言模型(LLM)读写条目时的上下文消耗。但模型仍容易产生 HTML 习惯迁移错误(例如写 <p>、<span style> 或把 <img src="..."/> 当作普通图片)。本指南提供精简、可直接复用的约束清单。

目标与前置

阅读本页后,你可以为任意 LLM 构造精简的系统提示词,把一次生成成功率最大化,并在输出后用转换器拦截常见幻觉错误。本指南面向 @eihrteam/xml v1.0.0。

给 LLM 的精简格式速查

可直接将以下结构摘要放入提示词上下文中,只保留模型必须遵守的硬性约束:

root: <sklandDocument>
required_children:
  - <itemId>string</itemId>
  - <metainfo><name>...</name><cover showInDetail="true|false">...</cover><subTypes><subType id="...">...</subType></subTypes></metainfo>
  - <description>mixed_blocks</description>
optional_children:
  - <commitMsg>...</commitMsg>
  - <publicMeta>JSON object</publicMeta>
  - <chapters name="..."><chapter size="large|middle|small" name="..." [table="true"|audio="true"|dialog="true"]>...</chapter></chapters>

chapter_modes (mutually exclusive):
  - common: mixed_blocks OR 1+ <tab [name] [icon]>[<imgIntro><name/><type/><imgUrl/><description/></imgIntro>] mixed_blocks</tab>
  - table="true": <table><row><cell label="...">text|<br/></cell><cell label="...">text|<br/></cell></row></table>
  - audio="true": <audios><audio name="..." src="...">text|<br/></audio></audios> OR 1+ <tab [name] [icon]><audios>...</audios></tab>
  - dialog="true": 1+ of <message side="me|other" avatar="..." [name]>(<text>plain|<emoji>|<br/></text> | <image src="..."/>)*</message> | <branches><branch name="...">me..other messages</branch></branches> | <divider kind="...">plain</divider>

blocks: plain line, <h1>..<h3>, <align value="left|center|right">, <quote>, <ul>/<ol>/<li>, <video kind="skland|bilibili" id="..."/>, <line kind="1..5"/>, <img><id/><format/><width/><height/><size/><url/><description/></img>, <table header="none|row|col|both" widths="n1,n2,..."><tr><th>|<td>...</td></tr></table>
inlines: <b>, <i>, <u>, <s>, <color value="s_1..4|r_1..6|f_orange|f_turquoise|f_green|f_sandstone|f_yellow|f_red|f_blue|f_brown|f_violet|f_blueness">, <br/>, <a href="...">plain</a>, <entry type="card-big|link-img|link-txt" count="..." id="..."/>, <emoji id="..."/>, <pron>plain</pron>, <annotation><title>inlines</title><content>blocks</content></annotation>

高频模型幻觉与明确禁令

在系统提示词中显式写出以下十条禁令,能消除绝大多数转换失败:

  1. 禁止使用 HTML 标签:不要输出 <p>、<div>、<span>、<thead>、<tbody>、<h4> 或 <img src="..."/>(正文图片必须是多子元素的 <img>,对话图片才是 <image src="..."/>)。
  2. 保留必填壳元素:即使没有封面、分类或简介,也必须保留 <cover/>、<subTypes/> 与 <description/>;保留既有 <publicMeta> 不要删除。
  3. 只用 20 种预设 <color value>:禁止十六进制颜色(如 #ff0000)或 CSS 颜色名(如 red)。
  4. 区分 <entry type> 命名:XML 中必须写 card-big、link-img、link-txt,不要写成 JSON 里的 link-imgText 或 link-text。
  5. 复杂表格严格对齐 header 与 <th>/<td>:header="row" 第一行全用 <th>,其余全用 <td>;不在 <chapter size="large|middle|small"> 内时必须提供 widths。
  6. 禁止嵌套表格和嵌套注释:<td>/<th> 内禁止直接或通过 <annotation> 间接嵌套 <table>;<annotation> 内禁止再嵌套 <annotation>。
  7. 对话分支起止与图片限制:<branch> 必须以 side="me" 开头、side="other" 结尾,且第一条消息不能包含 <image>。
  8. 对话 <text> 禁止样式元素:对话 <text> 内只能写纯文本、<emoji> 和 <br/>,不要放 <b>、<i> 或 <color>。
  9. 转义 XML 特殊字符:正文和属性中的 &、<、> 分别写作 &amp;、&lt;、&gt;,属性中的引号写作 &quot;。
  10. 每条 <img> 明确写出 <id> 和 <format>:避免因占位 URL 缺少 _ 而触发无法推断错误。

验证结果

切勿直接信任模型输出的原始 XML。每次生成或修改完成后,至少通过以下方式之一做机器验证:

  • 命令行转换验证:运行 xml convert --from xml --to json --input draft.xml,确认退出码为 0。
  • 双层检查:先用 xmllint --nonet --noout --schema skland-wiki-v1.xsd draft.xml 检查静态结构,再调用转换器检查表格几何、对话顺序与字段内容;或直接接入 @eihrteam/xml-lsp 的 createXmlLanguageService().diagnostics(uri) 一次性获取全部诊断。

下一步