AI Skills AI技能 5d ago Updated 4d ago 更新于 4天前 49

Assemble Each RAG Generation Prompt from a Base Prompt Plus the Rules Each Question Needs 根据每个问题所需的规则,从基础提示词组装每个RAG生成提示词

Introduces a modular dispatcher for Enterprise Document Intelligence that replaces monolithic "mega-prompts" with dynamic, shape-specific prompt composition. Utilizes a `ParsedQuestion` schema to extract `expected_answer_shape`, `generation` constraints, and `keywords`, enabling precise routing to typed schemas via `ANSWER_REGISTRY`. Combines a fixed base system prompt with modular fragments based on the question brief, ensuring scalability and maintainability without combinatorial explosion. In Enterprise Document Intelligence采用模块化RAG架构,将系统拆分为文档解析、问题解析、检索和生成四个独立砖块。 生成模块通过Dispatcher机制动态组装Prompt,避免使用臃肿的“超级Prompt”,实现按答案形状(如金额、日期、表格)精准匹配Schema。 引入`ParsedQuestion`结构化对象,将用户自然语言转化为包含关键词、执行计划和生成约束的关系型数据,强化上下文传递。 支持通过问题中的结构提示(如页码、工作表名)直接控制检索范围,无需额外配置标志位。 代码开源并配套Notebook,强调可追溯性(Trace),保留完整原始响应以便后续验证

65
Hot 热度
75
Quality 质量
70
Impact 影响力

Analysis 深度分析

TL;DR

  • Introduces a modular dispatcher for Enterprise Document Intelligence that replaces monolithic "mega-prompts" with dynamic, shape-specific prompt composition.
  • Utilizes a ParsedQuestion schema to extract expected_answer_shape, generation constraints, and keywords, enabling precise routing to typed schemas via ANSWER_REGISTRY.
  • Combines a fixed base system prompt with modular fragments based on the question brief, ensuring scalability and maintainability without combinatorial explosion.
  • Integrates tightly with previous pipeline stages (document parsing, question parsing, retrieval) to pass structured context like RetrievalQuery and GenerationBrief directly to the generation model.

Why It Matters

This approach addresses a critical scalability bottleneck in enterprise RAG systems where prompt engineering often becomes unmanageable due to complex, hardcoded conditional logic. By decoupling prompt assembly from the model call and relying on structured metadata from earlier pipeline stages, developers can add new answer types or formatting constraints with minimal code changes, significantly reducing maintenance overhead and token waste.

Technical Details

  • Dispatcher Architecture: The core component receives a ParsedQuestion object, queries the ANSWER_REGISTRY to select the appropriate schema based on expected_answer_shape, and dynamically composes the system prompt by merging a static BASE with specific fragments required by the question's constraints.
  • Structured Input Schema: The ParsedQuestion model includes nested Pydantic objects such as Keyword (with weight and source), RetrievalQuery (for scope and anchors), and GenerationBrief (containing format constraints and disambiguation rules), providing rich context beyond simple text.
  • Prompt Composition Strategy: Avoids the "mega-prompt" anti-pattern by injecting only relevant constraints (e.g., ISO 8601 for dates, ISO 4217 for amounts) rather than loading all possible rules into every inference call, thereby saving tokens and reducing hallucination risks from irrelevant instructions.
  • Trace Persistence: The dispatcher captures the full raw response and the complete prompt history (system + user) on a trace, facilitating debugging and validation in subsequent steps (Article 8C).
  • Integration with Retrieval: Leverages structural_hints from the question parsing stage to scope retrieval operations, allowing natural language pointers (e.g., "on page 1") to filter the search space without explicit pipeline flags.

Industry Insight

  • Modular Prompt Engineering: Treat prompt templates as composable modules rather than static strings. This allows teams to iterate on specific formatting rules or answer shapes independently, similar to microservices architecture.
  • Schema-Driven Pipelines: Invest heavily in the quality of intermediate structured representations (like ParsedQuestion). The richer the metadata passed between pipeline stages, the more precise and efficient the final generation step becomes.
  • Maintainability over Complexity: As enterprise RAG systems grow, avoid adding conditional clauses to a single prompt. Instead, use a registry-based dispatch mechanism to keep the codebase clean and prevent "prompt rot" where instructions become contradictory or forgotten.

TL;DR

  • Enterprise Document Intelligence采用模块化RAG架构,将系统拆分为文档解析、问题解析、检索和生成四个独立砖块。
  • 生成模块通过Dispatcher机制动态组装Prompt,避免使用臃肿的“超级Prompt”,实现按答案形状(如金额、日期、表格)精准匹配Schema。
  • 引入ParsedQuestion结构化对象,将用户自然语言转化为包含关键词、执行计划和生成约束的关系型数据,强化上下文传递。
  • 支持通过问题中的结构提示(如页码、工作表名)直接控制检索范围,无需额外配置标志位。
  • 代码开源并配套Notebook,强调可追溯性(Trace),保留完整原始响应以便后续验证。

为什么值得看

本文展示了企业级RAG系统中生成环节的工程化最佳实践,解决了传统RAG中Prompt管理混乱、Token浪费及调试困难的核心痛点。对于构建高可靠性、可维护的企业智能应用,其模块化设计和动态Prompt组装策略具有极高的参考价值。

技术解析

  • 动态Prompt组装机制:摒弃将所有条件逻辑写入单一System Prompt的做法,采用“固定BASE + 动态片段”的组合方式。Dispatcher根据ParsedQuestion中的expected_answer_shapeANSWER_REGISTRY选取对应Schema和Prompt片段,确保每次调用仅加载必要指令,降低Token消耗并提升模型专注度。
  • 结构化中间表示 (ParsedQuestion):定义了一套基于Pydantic的关系型数据结构,不仅包含原始问题和关键词,还整合了GenerationBrief(格式约束、消歧要求)和RetrievalQuery。这种强类型设计确保了各模块间数据传递的一致性和可扩展性。
  • 模块化架构解耦:系统将RAG流程明确划分为四个阶段:文档解析(PDF转结构化表)、问题解析(字符串转ParsedQuestion)、检索(过滤相关段落)和生成(组装Prompt并调用模型)。每个阶段独立开发和维护,便于迭代和替换组件。
  • 结构感知检索:利用ParsedQuestion中的structural_hints字段,直接从用户问题的自然语言表述中提取页码或章节限制,并将其传递给检索模块进行范围过滤,实现了零配置的结构化查询控制。
  • 全链路追踪与验证:生成过程记录完整的Trace(包括输入Prompt、模型原始输出等),为后续的Article 8C(答案验证)提供数据基础,确保结果的可审计性和错误排查能力。

行业启示

  • Prompt工程应向软件工程演进:企业级AI应用不应依赖静态、庞大的Prompt模板,而应建立类似代码库的Prompt版本管理和动态组装机制,以提高系统的可维护性和稳定性。
  • 结构化数据是RAG可靠性的基石:在LLM调用前,通过严格的Schema定义(如Pydantic模型)对查询意图、约束条件和上下文进行结构化封装,能显著减少幻觉并提升复杂任务的处理精度。
  • 模块化设计利于长期迭代:将RAG管道拆分为独立且接口清晰的模块,允许团队针对特定环节(如检索算法或生成策略)进行优化,而不必重构整个系统,符合企业软件敏捷开发的需求。

Disclaimer: The above content is generated by AI and is for reference only. 免责声明:以上内容由 AI 生成,仅供参考。

RAG 检索增强生成 LLM 大模型 Fine-tuning 微调