"""报告审核:由本机 DeepSeek 14B 对 27B 生成的报告做二次复核。 审核只读取报告文字和系统已计算的关键事实,不修改数值、不重新计算; 审核意见作为报告第 8 章追加,便于人工复核。 """ from __future__ import annotations import json import os from typing import Any, Dict, Optional from docx import Document from app.report import ollama_client REVIEW_SYSTEM = ( "你是风电机组可靠性、健康评估与运维报告审核专家。" "你的任务是审核一份由系统生成的风电场健康度分析报告," "检查结论与给定事实是否一致、是否存在无依据推断、章节是否完整、" "建议是否可执行。只输出中文审核意见,使用“审核结论 / 主要问题 / 修改建议”三部分;" "不要编造报告中没有的数据,不要输出与审核无关的内容。" ) def _document_text(path: str, limit: int = 12000) -> str: doc = Document(path) parts = [] for para in doc.paragraphs: text = (para.text or "").strip() if text: parts.append(text) for table in doc.tables: for row in table.rows: cells = [(cell.text or "").strip() for cell in row.cells] if any(cells): parts.append(" | ".join(cells)) return "\n".join(parts)[:limit] def _facts(data: Dict[str, Any]) -> Dict[str, Any]: wind = data.get("wind") or {} overview = data.get("overview") or {} health = data.get("health") or {} fault = data.get("fault") or {} vibration = data.get("vibration") or {} return { "wind": wind.get("name"), "period": f"{data.get('period', {}).get('start')} 至 {data.get('period', {}).get('end')}", "overview": { "total_mwh": overview.get("total_mwh"), "equivalent_hours": overview.get("equivalent_hours"), "avg_wind": overview.get("avg_wind"), "completeness": overview.get("completeness"), }, "health": { "avg_score": health.get("avg_score"), "levels": health.get("levels"), "low_turbines": (health.get("low_turbines") or [])[:8], }, "fault": { "total": fault.get("total"), "affected_turbines": fault.get("affected_turbines"), "top_types": (fault.get("top_types") or [])[:8], }, "vibration": { "total": vibration.get("total"), "points": vibration.get("points"), "frequency": vibration.get("frequency"), }, } def review_report(path: str, data: Dict[str, Any]) -> Optional[str]: """调用 DeepSeek 14B 审核报告;不可用时返回 None。""" try: report_text = _document_text(path) prompt = ( "请审核下面这份风电场健康度分析报告。\n\n" "系统计算事实(以此为准,不得改写):\n" + json.dumps(_facts(data), ensure_ascii=False, indent=2) + "\n\n报告正文(截取):\n" + report_text + "\n\n请按以下格式输出:\n" "审核结论:通过 / 需修改(并说明理由)\n" "主要问题:逐条列出事实不一致、无依据推断、缺失章节或表述风险;没有则写“未发现明显问题”\n" "修改建议:给出可执行的修改意见\n" ) text = ollama_client.chat_model( ollama_client.REVIEW_MODEL, REVIEW_SYSTEM, prompt, temperature=0.1, num_predict=700, timeout=int(os.getenv("REVIEW_TIMEOUT", "300")), ) return text.strip() if text else None except Exception: return None def append_review( path: str, review_text: Optional[str], report_model: str, review_model: str, ) -> str: """把审核意见追加为报告第 8 章,返回最终使用的文本。""" doc = Document(path) doc.add_page_break() doc.add_heading("报告审核", level=1) if review_text: for block in review_text.split("\n"): block = block.strip() if block: doc.add_paragraph(block) status = "已完成模型审核" else: doc.add_paragraph( "本次未能获取 DeepSeek 14B 审核结果(模型服务不可用或超时)," "报告已按生成结果正常输出,请专业人员复核后使用。" ) status = "审核模型不可用,已降级" doc.add_paragraph( f"审核说明:报告由本机 {report_model} 生成,由本机 {review_model} 进行文字复核;" "审核不修改系统计算数值,最终结论仍需专业技术人员确认。" ) doc.save(path) return status