section_reference_block/services/report_prompt_service.py
xxy aa98ea2623 @
Initial commit

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@
2026-06-05 18:45:29 +08:00

136 lines
5.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from __future__ import annotations
from services.prompt_template_service import render_prompt
from prompts.report_generation.prompt_defaults import (
DEFAULT_SECTION_PROMPT_FALLBACK,
DEFAULT_SELECTED_EXAMPLE_FALLBACK,
)
def chapter_generation_system_prompt() -> str:
return render_prompt("report_generation/chapter_generation_system.md")
def repair_missing_tables_system_prompt() -> str:
return render_prompt("report_generation/repair_missing_tables_system.md")
def table_format_repair_system_prompt() -> str:
return render_prompt("report_generation/table_format_repair_system.md")
def _build_prior_sibling_sections_prompt_block(prior_sibling_sections_text: str) -> str:
body = str(prior_sibling_sections_text or "").strip()
if not body:
return ""
return (
"【同章前序小节正文(时间与金额须保持一致)】\n"
f"{body}\n\n"
"【同章一致性约束】\n"
"1. 竣工时间、开工/中交/投产/验收等关键里程碑日期,以及建设投资、总投资、营业收入、利润等各类金额数字,"
"须与本章前序小节已写明的口径完全一致(年月日表述可适度简化,但不得出现另一套矛盾日期或金额);\n"
"2. 若【证据包】或【字段级已抽取结果】中某日期/金额与前序小节矛盾,以前序小节为准写入本节,"
"不得在正文中另写一套矛盾数值;\n"
"3. 前序小节为「待补充」的字段,本节仍写「待补充」,不得自行编造;\n"
"4. 可补充本节新增信息,但不得改写或否定前序小节已确立的时间与金额。"
)
def _build_prior_chapters_prompt_block(prior_chapters_text: str) -> str:
body = str(prior_chapters_text or "").strip()
if not body:
return ""
return (
"【前序章节正文第16章本章须据此总结\n"
f"{body}\n\n"
"【前序章节使用约束】\n"
"1. 第7章各节是对第16章已生成正文的归纳、提炼与升华不得与前面章节结论矛盾\n"
"2. 可概括前文要点,禁止大段照搬;数据与结论须与前文一致;\n"
"3. 若前序章节某处为「待补充」,本节对应表述也应为「待补充」,不得编造;\n"
"4. 须由要素管理直出的表格如表7-1仍按【章节输出结构约束】执行不受本条限制。"
)
def _build_section_reference_block(section_reference: str) -> str:
body = str(section_reference or "").strip()
if not body:
return ""
return (
"【本章参考范文(本节写作蓝本:结构与行文风格须高度贴合;禁止复用数据、禁止照抄)】\n"
f"{body}\n\n"
"【参考范文使用约束】\n"
"1. 以范文为写作蓝本:段落数量与顺序、每段主题、论述逻辑、句式笔法与篇幅颗粒度均须与范文高度一致,做到逐段对应、同一笔法;\n"
"2. 严禁复用范文中的项目名称、时间、金额、指标值等任何事实数据,须全部替换为当前项目证据包的真实值;\n"
"3. 范文中的表格结构(表头、列顺序、行项)须沿用,但表内数据必须替换为当前项目证据包的值;\n"
"4. 禁止逐字照抄:不得出现与范文连续相同超过 15 字的文字,须改写措辞做到“形似而文不同”;\n"
"5. 若范文与证据包存在矛盾,以证据包为准。"
)
def build_report_chapter_prompt(
*,
section_title: str,
section_prompt: str,
required_tables_text: str,
structured_tables_text: str,
canonical_fields_text: str,
selected_example: str,
heading_rule: str,
section_contract: str,
evidence_json: str,
prior_sibling_sections_text: str = "",
prior_chapters_text: str = "",
section_reference: str = "",
) -> str:
return render_prompt(
"report_generation/chapter_generation_user_ref_aligned.md",
section_title=section_title,
section_prompt=section_prompt or DEFAULT_SECTION_PROMPT_FALLBACK,
required_tables_text=required_tables_text or "",
structured_tables_text=structured_tables_text,
canonical_fields_text=canonical_fields_text,
selected_example=selected_example or DEFAULT_SELECTED_EXAMPLE_FALLBACK,
heading_rule=heading_rule,
section_contract=section_contract,
evidence_json=evidence_json,
prior_sibling_sections_block=_build_prior_sibling_sections_prompt_block(
prior_sibling_sections_text
),
prior_chapters_block=_build_prior_chapters_prompt_block(prior_chapters_text),
section_reference_block=_build_section_reference_block(section_reference),
)
def build_repair_missing_tables_prompt(
*,
section_title: str,
original_prompt: str,
content: str,
missing_tables: list[str],
evidence_json: str,
) -> str:
return render_prompt(
"report_generation/repair_missing_tables_user.md",
section_title=section_title,
missing_tables=", ".join(missing_tables),
content=content,
original_prompt=original_prompt[:8000],
evidence_json=evidence_json[:12000],
)
def build_table_format_repair_prompt(
*,
section_title: str,
table_specs_json: str,
content: str,
evidence_json: str,
) -> str:
return render_prompt(
"report_generation/table_format_repair_user.md",
section_title=section_title,
table_specs_json=table_specs_json,
content=content,
evidence_json=evidence_json[:12000],
)