reviewer.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. """报告审核:由本机 DeepSeek 14B 对 27B 生成的报告做二次复核。
  2. 审核只读取报告文字和系统已计算的关键事实,不修改数值、不重新计算;
  3. 审核意见作为报告第 8 章追加,便于人工复核。
  4. """
  5. from __future__ import annotations
  6. import json
  7. import os
  8. from typing import Any, Dict, Optional
  9. from docx import Document
  10. from app.report import ollama_client
  11. REVIEW_SYSTEM = (
  12. "你是风电机组可靠性、健康评估与运维报告审核专家。"
  13. "你的任务是审核一份由系统生成的风电场健康度分析报告,"
  14. "检查结论与给定事实是否一致、是否存在无依据推断、章节是否完整、"
  15. "建议是否可执行。只输出中文审核意见,使用“审核结论 / 主要问题 / 修改建议”三部分;"
  16. "不要编造报告中没有的数据,不要输出与审核无关的内容。"
  17. )
  18. def _document_text(path: str, limit: int = 12000) -> str:
  19. doc = Document(path)
  20. parts = []
  21. for para in doc.paragraphs:
  22. text = (para.text or "").strip()
  23. if text:
  24. parts.append(text)
  25. for table in doc.tables:
  26. for row in table.rows:
  27. cells = [(cell.text or "").strip() for cell in row.cells]
  28. if any(cells):
  29. parts.append(" | ".join(cells))
  30. return "\n".join(parts)[:limit]
  31. def _facts(data: Dict[str, Any]) -> Dict[str, Any]:
  32. wind = data.get("wind") or {}
  33. overview = data.get("overview") or {}
  34. health = data.get("health") or {}
  35. fault = data.get("fault") or {}
  36. vibration = data.get("vibration") or {}
  37. return {
  38. "wind": wind.get("name"),
  39. "period": f"{data.get('period', {}).get('start')} 至 {data.get('period', {}).get('end')}",
  40. "overview": {
  41. "total_mwh": overview.get("total_mwh"),
  42. "equivalent_hours": overview.get("equivalent_hours"),
  43. "avg_wind": overview.get("avg_wind"),
  44. "completeness": overview.get("completeness"),
  45. },
  46. "health": {
  47. "avg_score": health.get("avg_score"),
  48. "levels": health.get("levels"),
  49. "low_turbines": (health.get("low_turbines") or [])[:8],
  50. },
  51. "fault": {
  52. "total": fault.get("total"),
  53. "affected_turbines": fault.get("affected_turbines"),
  54. "top_types": (fault.get("top_types") or [])[:8],
  55. },
  56. "vibration": {
  57. "total": vibration.get("total"),
  58. "points": vibration.get("points"),
  59. "frequency": vibration.get("frequency"),
  60. },
  61. }
  62. def review_report(path: str, data: Dict[str, Any]) -> Optional[str]:
  63. """调用 DeepSeek 14B 审核报告;不可用时返回 None。"""
  64. try:
  65. report_text = _document_text(path)
  66. prompt = (
  67. "请审核下面这份风电场健康度分析报告。\n\n"
  68. "系统计算事实(以此为准,不得改写):\n"
  69. + json.dumps(_facts(data), ensure_ascii=False, indent=2)
  70. + "\n\n报告正文(截取):\n"
  71. + report_text
  72. + "\n\n请按以下格式输出:\n"
  73. "审核结论:通过 / 需修改(并说明理由)\n"
  74. "主要问题:逐条列出事实不一致、无依据推断、缺失章节或表述风险;没有则写“未发现明显问题”\n"
  75. "修改建议:给出可执行的修改意见\n"
  76. )
  77. text = ollama_client.chat_model(
  78. ollama_client.REVIEW_MODEL,
  79. REVIEW_SYSTEM,
  80. prompt,
  81. temperature=0.1,
  82. num_predict=700,
  83. )
  84. return text.strip() if text else None
  85. except Exception:
  86. return None
  87. def append_review(
  88. path: str,
  89. review_text: Optional[str],
  90. report_model: str,
  91. review_model: str,
  92. ) -> str:
  93. """把审核意见追加为报告第 8 章,返回最终使用的文本。"""
  94. doc = Document(path)
  95. doc.add_page_break()
  96. doc.add_heading("8 报告审核", level=1)
  97. if review_text:
  98. for block in review_text.split("\n"):
  99. block = block.strip()
  100. if block:
  101. doc.add_paragraph(block)
  102. status = "已完成模型审核"
  103. else:
  104. doc.add_paragraph(
  105. "本次未能获取 DeepSeek 14B 审核结果(模型服务不可用或超时),"
  106. "报告已按生成结果正常输出,请专业人员复核后使用。"
  107. )
  108. status = "审核模型不可用,已降级"
  109. doc.add_paragraph(
  110. f"审核说明:报告由本机 {report_model} 生成,由本机 {review_model} 进行文字复核;"
  111. "审核不修改系统计算数值,最终结论仍需专业技术人员确认。"
  112. )
  113. doc.save(path)
  114. return status