Files
20260327-c863ce53/app/agents/writer.py
2026-04-25 19:25:22 +08:00

87 lines
3.0 KiB
Python
Raw Permalink 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.
"""Writer Agent — synthesizes multilingual research tracks into a cohesive report."""
from __future__ import annotations
import json
from typing import Any
from .base import BaseAgent
from app.config import settings
class WriterAgent(BaseAgent):
name = "writer"
description = "汇聚多语言/多领域研究成果,撰写完整报告"
system_prompt = """\
You are an expert consulting report writer. Your task is to synthesize research
findings from MULTIPLE parallel tracks (some in English, some in Chinese) into
ONE cohesive, professional consulting report.
CRITICAL RULES:
1. The PRIMARY output language is Chinese (中文) — this is for Chinese clients
2. For global/international sections, the analysis depth must reflect the English research
3. For China-specific sections, preserve the precision of Chinese-native research
4. Maintain professional consulting tone throughout
5. Every claim should trace back to a research track's findings
6. Mark chart/table needs: {{CHART:描述}} and {{TABLE:描述}}
7. If a research track flags "data_gaps", acknowledge uncertainty rather than fabricating
Output (JSON):
{
"title": "报告标题(中文)",
"title_en": "Report Title (English)",
"chapters": [
{
"title": "章节标题",
"content": "章节正文Markdown 格式,中文)",
"source_tracks": ["引用的研究轨道名称"],
"charts": ["图表需求"],
"tables": ["表格需求"]
}
],
"executive_summary": "执行摘要中文300-500字",
"executive_summary_en": "Executive Summary (English, 200-400 words)"
}"""
def __init__(self):
super().__init__(model=settings.model_for_domain("reasoning"))
async def run(self, context: dict[str, Any]) -> dict[str, Any]:
research = context["research"]
requirement = context["requirement"]
revision_feedback = context.get("revision_feedback", "")
# Format multi-track, multilingual research
tracks_text = ""
for track in research.get("tracks", []):
lang_tag = f"[{track.get('native_language', '?').upper()}]"
domain_tag = f"[{track.get('domain', '?')}]"
tracks_text += f"\n### {domain_tag} {lang_tag} {track.get('track', '')}\n"
findings = track.get("findings", {})
tracks_text += json.dumps(findings, ensure_ascii=False, indent=2)
synthesis_guide = research.get("synthesis_guide", "")
prompt = f"""\
## 原始需求 / Original Requirement
{requirement}
## 报告标题
中文:{research.get("title_zh", "")}
English: {research.get("title_en", "")}
## 写作指导 / Synthesis Guide
{synthesis_guide}
## 各研究轨道成果 / Research Track Results
(注意:有些轨道是英文原版 [EN],有些是中文原版 [ZH],请综合使用)
{tracks_text}
{f"## 审稿反馈 / Review Feedback{revision_feedback}" if revision_feedback else ""}
请汇聚以上研究成果,撰写完整的中文报告。输出 JSON。"""
result = await self.call_llm_json(prompt, max_tokens=8192)
return {"draft": result}