openJiuwen DeepSearch 模板驱动大纲生成:outliner_template Prompt 的变量契约、触发链路与结构化输出协议

原创2026-10-15 09:13:101,682 阅读
文章标签:人工智能AI Agent深度研究RAG代码智能体搜索引擎后端

openJiuwen DeepSearch 模板驱动大纲生成:outliner_template Prompt 的变量契约、触发链路与结构化输出协议

导读

本文围绕 openJiuwen DeepSearch 研究框架中的 outliner_template Prompt 模板展开,深入讲解它在"基于用户提供的报告模板生成研究大纲"这一核心场景中的完整工作机制。读者将掌握该模板的输入变量契约(current_date、original_query、questions、language、user_feedback、report_template、entry_search_results 七大占位符)、它在 OutlineNode 中的触发条件与依赖驱动切换逻辑,以及 generate_outline() 函数工具的结构化输出协议(sections 契约字段、核心章节规则、模板优先级原则),从而能够直接理解并复用于自定义 DeepSearch 工作流。

一、模板定位:模板驱动大纲生成在 DeepSearch 中的角色

在 openJiuwen DeepSearch 的深度研究工作流中,outliner_template 是一组专门用于"模板驱动(template-driven)大纲生成"的 Prompt 文件,位于 prompts/outliner_template 目录,由 system.md 与 user.md 两个文件构成:

  • system.md:定义大纲生成 Agent 的角色、核心原则、函数工具调用协议、章节契约字段、执行约束与示例;
  • user.md:以 Jinja2 模板形式声明用户消息中的上下文变量占位符,是本次分析的主体。

与之并列的是普通大纲模板 prompts/outliner(outliner,面向"从零自由规划大纲")以及交互式大纲模板 outliner_interaction、用户修订模板 outliner_user_revised、依赖驱动大纲模板 dep_driving_outliner。四者共享同一个 Outline/Section 数据结构,区别在于输入上下文与生成策略。

从源码结构看,outliner_template 的核心能力可概括为三句话:

  1. 模板即权威:用户提供的 report_template(即 <reference_report_template> 块)是报告结构的第一优先级约束,大纲 Agent 必须严格基于它实例化各章节;
  2. 函数工具输出:大纲不是以自由文本返回,而是必须通过 generate_outline() 函数工具以结构化 JSON 返回;
  3. 章节契约驱动下游:每个章节携带 format_requirements、section_focus、focus_dimensions 等契约字段,后续被 planner/reporter/editor 节点消费,决定信息收集与撰写方式。

二、user.md 变量契约:七大占位符逐项解析

user.md 全文为一段 Jinja2 模板,完整原文如下:

Current date (UTC): {{ current_date }}

Original user request: {{ original_query }}
Research question: {{ questions }}
Output language: {{ language }}
User feedback: {{ user_feedback }}
<reference_report_template>
{{ report_template }}
</reference_report_template>
Entry search results: {{ entry_search_results }}

这些占位符并非凭空定义,而是由 OutlineNode._pre_handle() 从会话全局状态(search_context.*)中组装后注入,再由 message_builder.py 的 build_prompt_messages() 渲染为最终的 user 消息。各变量的来源与语义如下表:

占位符 渲染来源(源码路径) 语义与用途
{{ current_date }} build_prompt_messages() 自动注入当前 UTC 日期(ISO 格式,如 2026-10-12) 让 LLM 感知"现在"的时间边界,用于时间敏感型研究(如"截至 202X 年")
{{ original_query }} search_context.original_query 用户最初的原始请求,大纲生成的基础上下文
{{ questions }} search_context.questions(由 GenerateQuestionsNode 产出) 细化后的研究问题列表
{{ language }} search_context.language 输出语言,如 zh-CN、en-US,决定模板实例化时的中文/英文元数据措辞
{{ user_feedback }} search_context.user_feedback 或交互轮 outline_interactions[].feedback 用户对既有大纲的修订反馈;交互模式下覆盖为当前轮反馈
{{ report_template }} search_context.report_template 用户/模板库提供的报告结构模板,被包裹在 <reference_report_template> 标签内
{{ entry_search_results }} search_context.entry_search_results 前置预搜索(entry search)返回的结果,用于帮助 Agent 更准确地实例化模板

2.1 current_date 的自动注入机制

值得注意的是,current_date 并不需要外部调用方显式传入。在 message_builder.py 中有专门的兜底逻辑:

user_variables = _template_variables(environment, user_name, recursive=True)
if "current_date" in user_variables and "current_date" not in render_context:
    render_context["current_date"] = datetime.now(timezone.utc).date().isoformat()

即:只要 user 模板声明了 current_date 变量而调用方未提供,渲染器就会自动填入 datetime.now(timezone.utc).date().isoformat()(UTC 时区的 ISO 日期)。这与 Prompt 中标注的 "Current date (UTC)" 完全一致。这保证了时间感知能力无需调用方额外实现。

2.2 report_template 的隔离与权威性

report_template 被 <reference_report_template> 与 </reference_report_template> 两个标签包裹,这是 Prompt 工程中的"上下文隔离"设计:系统提示词(system.md)中明确声明——模板只指 user 消息中 <reference_report_template> 内的内容,原始请求、研究问题、反馈与预搜索结果都是独立上下文,不属于模板本身。对应测试 test_prompt_migration_regressions.py 中的 test_template_scope_excludes_other_user_context 精确验证了这一点:渲染后 <reference_report_template> 与 </reference_report_template> 之间只包含模板正文,其他上下文不会混入模板边界。

三、触发链路:OutlineNode 如何在模板驱动与自由规划之间分流

outliner_template 并非始终被使用。在 main_graph_nodes.py 的 OutlineNode._select_prompt_name() 中,Prompt 选择遵循以下优先级:

def _select_prompt_name(self, current_inputs: dict) -> str:
    report_template = current_inputs.get("report_template", "")
    outline_interaction_mode = current_inputs.get("outline_interaction_mode", "")
    feedback = current_inputs.get("user_feedback", "")
    if report_template and not outline_interaction_mode:
        return "outliner_template"
    if outline_interaction_mode == "revise_comment":
        ...  # outliner_interaction / dep_driving_outliner_interaction
    if outline_interaction_mode == "revise_outline":
        ...  # outliner_user_revised
    if self._get_with_dep_driving(current_inputs):
        return "dep_driving_outliner"
    return self.outline_prompt  # 默认 "outliner"

由此可得出四条明确的分流规则:

  1. 存在 report_template 且非交互模式 → 命中 outliner_template。这是该模板唯一的激活条件;
  2. 交互模式 revise_comment(评论修订) → 走 outliner_interaction(或启用依赖驱动时走 dep_driving_outliner_interaction);
  3. 交互模式 revise_outline(大纲直接修订) → 走 outliner_user_revised,此时用户反馈被解析为 Outline 对象写入 user_outline;
  4. 无模板、无交互且未启用依赖驱动 → 走默认 outliner(自由规划)。

3.1 模板驱动模式禁用依赖驱动

在 main_graph_nodes.py 中,_select_prompt_and_dep_driving() 有一个关键判断:

if prompt_name in {"outliner_template", "outliner_user_revised"}:
    return prompt_name, False, ExecutionMethod.PARALLEL.value

即:一旦命中 outliner_template,强制使用普通大纲工具 schema(generate_outline),并强制采用 PARALLEL 并行执行模式,不会切换到依赖驱动大纲工具(dep_driving_generate_outline)。这是因为模板已经规定了章节结构,无需依赖驱动模式再生成 parent_ids/relationships 依赖关系(详见第五节)。

3.2 Brief 大纲注入时的降级

另一处分支在 main_graph_nodes.py:当会话存在注入的 brief_outline(精简版报告升级专业版的场景)且非交互模式时,首版生成强制使用普通 outliner,即使存在报告模板也忽略模板(brief 结构优先)。这解释了为什么模板驱动模式只在常规(非 brief 注入、非交互)的 OutlineNode 运行中生效。

四、模板语义:从结构模板到章节契约

要理解 outliner_template 如何消费 report_template,需要先看模板本身长什么样。报告模板由"结构提取"环节生成——template_semantic_extract/system.md 定义了从已有报告提取/泛化模板的规则。一个标准模板的形态如下(摘自该文件示例):

# 简介
> 是否核心章节: false
> 功能概述: 介绍研究对象的整体概况

# 核心环节竞争力分析
> 是否核心章节: true
> 功能概述: 详细分析研究对象在核心环节的竞争力

## 整车制造环节竞争力
> 功能概述: 详细分析研究对象在整车制造环节的竞争力

其中:

  • # 级标题:只能由 # 级标题标记核心章节(is_core_section / 是否核心章节),系统按"字数、细节与数据支撑"打分,最多选取 3 个核心章节;"摘要、简介、概述、前言、总结、结论、致谢、参考文献(Abstract/Introduction/Overview/Summary/Conclusion/Final Remarks)"等标题强制标记为 false;
  • ## 级标题:只标注 > Function / > 功能概述 一行,不标记核心章节;
  • 泛化要求:模板中的具体实体(城市名、公司名等)必须替换为 "某城市""某公司""该方法" 等通用描述词,以便跨问题复用。

4.1 system.md 中的模板实例化规则

outliner_template/system.md 围绕这份模板定义了如下生成规则(核心原则):

  • 全面覆盖:主流视角 + 替代视角;深度要求:细节数据点 + 多源验证;
  • 模板引用边界:模板只指 <reference_report_template> 内的内容;原始请求、研究问题、反馈、预搜索结果是独立上下文;
  • 实体实例化:模板中的通用词("某城市""某公司""该方法")必须用用户问题或反馈提供的具体实体与范围替换,同时保留模板结构;当用户提供了具体实体时,不得在大纲中遗留占位符或未解析的实体引用;
  • 结构优先:模板结构优先于其他一切考虑;不逐字复制以 > Function: 开头的行,而是改写并概括其含义。

4.2 函数工具调用协议(STRICT)

system.md 用大写强调的协议是 outliner_template 与其他大纲模板共有的硬约束:

  • 调用的是函数工具,而不是输出 JSON 文本;
  • 所有工具参数必须是结构化值;
  • sections 必须是 JSON 对象数组,而不是字符串;不得对 sections 加引号或字符串化;
  • 无论用户输入什么(即使是闲聊),都必须先调用大纲生成函数工具,再作回复。

五、generate_outline() 工具:结构化大纲输出的完整契约

outliner_template 的最终落点是函数工具调用。工具定义位于 outliner.py 的 create_outline_tool()(普通模式)与 creat_dep_driving_outline_tool()(依赖驱动模式),二者都以 generate_outline() 为执行函数。

5.1 顶层参数

generate_outline() 函数工具要求以下必填参数:

参数 类型 说明
language string 输出语言,如 zh-CN / en-US
title string 最终报告标题
thought string 生成大纲的详细思考过程
sections array 最终报告章节数组(对象数组,禁止字符串化)

5.2 Section 契约字段(Required Section Contract Fields)

每个章节对象必须包含如下契约字段,这也是 test_outliner.py 中 test_outliner_template_prompt_requires_section_contract_fields 验证的核心内容:

字段 类型 必填 说明
title string 是 纯章节标题;用户给了明确标题就用原标题,不追加副标题/覆盖实体/改写范围
description string 是 单字符串(非数组);简洁的纯文本研究要求,描述本章节的研究范围、时间范围、维度、示例等;不得内嵌序列化的章节对象或 "title":/"description":/}, {/\"id\" 等 JSON 片段
format_requirements array 是 章节级输出格式要求:表格、精确列名/列顺序、所需行对象、逐项枚举、长度/样式约束、来源使用限制;无则用 []
is_core_section boolean 否(默认 false) 核心章节标记;作为固定指令,不可重新评估或曲解,决定该章节所需的研究深度
id string 否 章节唯一标识,格式如 '1'、'2'
section_focus string 是(minLength 1) 章节分析职责标签,如 market_size_and_growth、vendors_and_supply、technology_drivers、risks_and_barriers、use_cases_and_commercialization、recommendation_and_ranking、section_specific_analysis;只有承载最终判断/推荐结论的章节才使用 recommendation_and_ranking
focus_dimensions array 是(minItems 1) 本章节主展开的 2–4 个分析维度;其他章节不应深入展开这些维度
material_bindings array 否 本章节必须用作证据的用户素材:material_id、role、claims_to_use

5.3 章节映射规则(Section Mapping Rules)

system.md 规定的映射规则是大纲结构落地的关键,逐条拆解如下:

  • 层级限制:最终模板只允许两级标题(Level 1 与 Level 2);不得为 Level 2 副标题生成独立的 Level 3 标题,Level 2 的内容必须合并进其父 Level 1 章节的 description 中;
  • 职责划分:实质性的 Level 2 范围与功能放进 description;表格、精确列、枚举、长度/样式、来源使用约束放进 format_requirements;
  • 忽略更深层级:Level 3 及更深层级一律忽略;
  • 保留所有 Level 1 章节:模板中的所有 Level 1 章节必须保留,唯一例外是总结性质章节(如 "摘要""简介""总结""结论""Summary""Conclusion""Final Remarks"),这类章节显式丢弃;
  • 核心章节字段是固定指令:is_core_section 是布尔值,必须视为固定指令,不得评估或重新解读;该规则只作用于 Level 1 标题,子章节继承父级核心状态;
  • 非核心 ≠ 总结:is_core_section: false 的章节同样必须保留——"非核心"不等于"总结性",只有总结性章节才可以移除;
  • 核心章节深度要求:is_core_section: true 的章节要收集更广泛的支持信息,包括多视角、案例研究与数据来源。

5.4 description 的格式化规则

description 必须是单个字符串(不是数组),需要把所有 Level 2 副标题及其功能概述以项目符号列表方式拼接,例如:

- 历史演进与产业基础: 详细介绍研究对象的历史发展历程,按照时间维度列举发展历程中的重要事件,并分析重要事件的标志性影响
- 产业结构: 详细分析研究对象的核心结构(例如有哪些环节,每个环节的具体概念)
- 产业核心环节: 详细介绍产业的核心环节有哪些,并分析研究对象在这些核心环节的发展情况

5.5 源码侧的参数化示例

system.md 附带的示例演示了完整的输入输出映射:给定包含"简介/发展概况/核心环节竞争力分析/总结"的报告模板,正确输出是丢弃"简介"与"总结",将 Level 2 副标题合并进父章节 description,并给每个章节附加 section_focus 与 focus_dimensions。例如:

{
  "language": "zh-CN",
  "thought": "The template emphasizes discarding summarizing sections and merging Level 2 subtitles into parent Level 1 descriptions. Core sections are preserved and expanded with richer detail.",
  "title": "研究报告",
  "sections": [
    {
      "title": "发展概况",
      "description": "- 历史演进与产业基础: ...\n- 产业结构: ...\n- 产业核心环节: ...",
      "format_requirements": [],
      "is_core_section": false,
      "section_focus": "section_specific_analysis",
      "focus_dimensions": ["development_history", "industry_structure", "core_segments"]
    },
    {
      "title": "核心环节竞争力分析",
      "description": "- 整车制造环节竞争力: ...\n- 关键零部件环节竞争力: ...\n- 新能源技术环节竞争力: ...",
      "format_requirements": [],
      "is_core_section": true,
      "section_focus": "section_specific_analysis",
      "focus_dimensions": ["vehicle_manufacturing", "key_components", "new_energy_technology"]
    }
  ]
}

六、调用链与数据流:从模板到结构化大纲的完整旅程

结合 outliner.py 的 Outliner.generate_outline() 与 main_graph_nodes.py 的 OutlineNode._do_invoke(),outliner_template 模式下的完整数据流如下:

  1. 状态组装:OutlineNode._pre_handle() 从 search_context 读取 original_query、questions、language、user_feedback、report_template、entry_search_results、section_num(取自 research_intent.section_count,受 OUTLINER_SECTION_NUM_MAX 上限约束,否则用 config.outliner_max_section_num)等;
  2. Prompt 分流:_select_prompt_name() 判定存在 report_template 且非交互 → 返回 outliner_template;_select_prompt_and_dep_driving() 随之强制 PARALLEL 模式与普通工具 schema;
  3. 渲染:Outliner.generate_outline() 调用 build_prompt_messages() 渲染 outliner_template/system.md 与 user.md,后者由 Jinja2 引擎按上下文变量实例化(current_date 缺失时自动补 UTC 日期);
  4. 工具装配:create_outline_tool(section_num) 生成 generate_outline 工具,若配置了 api_tools_config.query_understanding_tools,还会通过 merge_runtime_api_tools() 合并运行时 API 工具(对应测试 test_outliner.py 的 test_generate_outline_with_runtime_api_tool);
  5. LLM 调用:ainvoke_llm_with_stats() 携带工具 schema 调用模型(AgentLlmName.OUTLINE),要求返回 tool_calls;
  6. 工具调用校验:check_tool_call() 对 tool_call 的名称、参数、必填字段、sections 列表逐项校验;normalize_sections() 负责把 LLM 可能返回的字符串化 sections 递归解析为 list(最多解析 3 层);
  7. 执行与后处理:调用 generate_outline() 构造 Outline 对象,随后执行 ID 格式校验(正则 ^\d+(\.\d+)*$)、ID 重排兜底(fix_section_ids,重新编号为 '1'、'2'…)、依赖关系校验与自动修复(validate_section_dependencies/fix_section_dependency_issues,处理自依赖、反向依赖、parent_ids 与 relationships 数量不一致等问题);
  8. 素材绑定:若存在用户素材(material_first 场景),normalize_material_bindings() 与 ensure_material_first_bindings() 会保证每个章节至少绑定一个素材证据,空绑定章节会被自动分配 user_provided_evidence 角色绑定;
  9. 失败重试:_do_invoke() 中按 max_outline_retry_num(默认 1,即最多重试 1 次)循环重试,达到上限后抛出 StatusCode.OUTLINER_GENERATE_ERROR 并返回 success_flag=False;
  10. 流式输出与落库:成功后调用 custom_stream_output() 将 Outline 以 JSON 流式输出,_post_handle() 写入 search_context.current_outline,随后由 _get_next_node_after_outline() 路由到下一节点(PARALLEL 模式 → EDITOR_TEAM,依赖驱动模式 → DEPENDENCY_EDITOR_TEAM)。

6.1 章节契约如何传导到下游

大纲生成后,章节契约字段并非只用于展示。在 editor_team_manager_node.py 中,每个章节被展开为章节级状态,其中 section_task(标题)、section_description(研究要求)、section_format_requirements(格式约束)、section_iscore(是否核心章节)被显式传递给后续的规划/写作节点;section_focus 与 focus_dimensions 也被 _build_section_local_contract() 封装进 section_local_contract。这意味着:

  • 核心章节(is_core_section: true)在规划与写作阶段获得更高的信息收集配额与更严格的多源验证要求;
  • format_requirements 会成为撰写阶段的硬性输出约束(如必须产出指定列名的表格、逐项枚举等);
  • material_bindings 约束写作阶段的素材使用范围(只能用绑定素材支持的论断)。

七、与其他大纲模板的对比与选用建议

为帮助开发者在自定义工作流中正确选择,这里以源码分流逻辑为准列出四类大纲模板的适用场景:

Prompt 名称 触发条件 工具 schema 执行模式 适用场景
outliner_template 存在 report_template 且非交互 generate_outline(普通) PARALLEL 用户指定了报告结构模板,需要按模板骨架实例化章节
outliner 无模板、无交互、未启用依赖驱动 generate_outline(普通) PARALLEL 从用户请求自由规划大纲
outliner_interaction 交互模式 revise_comment 视依赖驱动开关而定 视配置 用户以评论形式反馈修订意见
outliner_user_revised 交互模式 revise_outline generate_outline(普通) PARALLEL 用户直接提交修订后的大纲 JSON
dep_driving_outliner 启用依赖驱动 dep_driving_generate_outline DEPENDENCY_DRIVING 章节间存在强依赖关系(如后续章节依赖前置章节结论)

选用建议:当用户/调用方提供了结构化报告模板时,优先依赖 outliner_template,它会强制丢弃总结性章节、合并二级标题并输出带契约字段的章节数组,为下游规划提供最清晰的任务边界;当研究问题没有既定结构、需要 Agent 自主探索维度时,使用默认 outliner;当大纲需要分阶段串行推进(章节间有数据依赖)时,再考虑依赖驱动模式。

八、测试与验证:如何确认模板行为符合预期

仓库为 outliner_template 提供了多层测试保障,可作为自定义改造时的回归基线:

  • test_outliner.py:test_outliner_template_prompt_requires_section_contract_fields 验证模板渲染后必须包含 "Required Section Contract Fields"、format_requirements、section_focus、focus_dimensions 等契约字样;
  • test_prompt_migration_regressions.py:test_template_scope_excludes_other_user_context 验证 <reference_report_template> 标签的隔离性——模板正文不会与原始请求、搜索结果混淆;
  • test_outliner.py:test_generate_outline_failure 验证 LLM 调用异常时返回 success_flag: False 与 OUTLINER_GENERATE_ERROR 错误码(211800);
  • test_outliner.py:test_generate_outline_with_runtime_api_tool 验证运行时 API 工具与内置大纲工具合并执行;
  • test_agent_node.py:覆盖 OutlineNode 的 Prompt 分流与依赖驱动切换逻辑。

结语

outliner_template 是 openJiuwen DeepSearch 模板驱动研究报告生成的核心 Prompt 组件:它以 <reference_report_template> 标签隔离并确立模板权威,以 generate_outline() 函数工具强制结构化输出,以 format_requirements/section_focus/focus_dimensions/is_core_section 契约字段衔接规划与写作下游。理解其变量契约、触发链路与章节映射规则,是扩展自定义研究模板、接入外部报告结构或调试大纲生成质量的前提。如需进一步深入,可依次阅读 outliner_template/system.md、outliner.py、main_graph_nodes.py 与 search_context.py 中对应的 Outline/Section 模型定义。

登录后查看全文
deepsearch