Skip to content

8.4 多模态应用(AI 写作与内容生成)

图像理解 + 语音交互 + 结构化写作 + 多模态内容创作——覆盖从单模态到跨模态的完整实战。

难度:⭐⭐⭐ | 预计时长:2-3 周


AI 写作是 LLM 最直接的商业应用之一,核心挑战是:保持长文一致性、控制输出结构、融合用户风格偏好、多模态协同生成。本节构建一个涵盖文章、报告、PPT 及图文创作的完整内容生成系统。

系统架构

用户输入(主题/大纲/素材/风格要求)

   ┌─────────▼──────────┐
   │    内容规划层      │  ← 意图理解、大纲生成、篇幅规划
   └─────────┬──────────┘

   ┌─────────▼──────────┐
   │    内容生成层      │  ← 分段生成、风格控制、一致性维护
   └─────────┬──────────┘

   ┌─────────▼──────────┐
   │    后处理层        │  ← 润色、排版、事实核查、SEO 优化
   └─────────┬──────────┘

   ┌─────────▼──────────┐
   │    多模态层        │  ← 配图生成、PPT 渲染、视频脚本
   └──────────────────┘

8.4.1 结构化长文章生成

长文章(2000+ 词)的核心难题是上下文长度限制全篇一致性,需要将生成过程拆分为:规划 → 分段生成 → 整合润色。

大纲驱动的长文生成器

python
from openai import OpenAI
from dataclasses import dataclass, field
from typing import List, Optional
import json

client = OpenAI()

@dataclass
class ArticleSection:
    """文章章节"""
    title: str
    key_points: List[str]
    word_count: int = 300
    content: str = ""

@dataclass
class ArticlePlan:
    """文章写作计划"""
    topic: str
    target_audience: str
    tone: str                    # 正式/轻松/专业/口语化
    total_word_count: int
    sections: List[ArticleSection] = field(default_factory=list)
    keywords: List[str] = field(default_factory=list)

class LongArticleWriter:
    """长文章生成器:大纲驱动,保持全篇一致性"""

    def __init__(self, model: str = "gpt-4o"):
        self.model = model
        self.client = OpenAI()

    def plan_article(
        self,
        topic: str,
        target_audience: str = "通用读者",
        tone: str = "专业",
        word_count: int = 2000,
        extra_requirements: str = ""
    ) -> ArticlePlan:
        """第一步:生成文章写作计划"""

        prompt = f"""请为以下文章生成详细的写作计划。

主题:{topic}
目标读者:{target_audience}
写作风格:{tone}
目标字数:{word_count}
额外要求:{extra_requirements}

请以 JSON 格式返回:
{{
  "title": "文章标题",
  "keywords": ["关键词1", "关键词2"],
  "sections": [
    {{
      "title": "章节标题",
      "key_points": ["要点1", "要点2", "要点3"],
      "word_count": 400
    }}
  ]
}}

要求:
- sections 的 word_count 之和约等于总字数
- 每个章节包含 2-4 个清晰的写作要点
- 结构逻辑清晰,由浅入深
"""

        response = self.client.chat.completions.create(
            model=self.model,
            messages=[{"role": "user", "content": prompt}],
            response_format={"type": "json_object"}
        )

        plan_data = json.loads(response.choices[0].message.content)

        return ArticlePlan(
            topic=topic,
            target_audience=target_audience,
            tone=tone,
            total_word_count=word_count,
            sections=[
                ArticleSection(
                    title=s["title"],
                    key_points=s["key_points"],
                    word_count=s.get("word_count", 300)
                )
                for s in plan_data.get("sections", [])
            ],
            keywords=plan_data.get("keywords", [])
        )

    def write_section(
        self,
        plan: ArticlePlan,
        section: ArticleSection,
        previous_sections: List[ArticleSection] = None
    ) -> str:
        """第二步:逐段生成内容(含上下文衔接)"""

        # 构建前文摘要(保持上下文一致性)
        context_summary = ""
        if previous_sections:
            written = [s for s in previous_sections if s.content]
            if written:
                context_summary = "**已写内容摘要(保持风格和逻辑一致):**\n"
                for prev in written[-2:]:  # 只参考最近 2 个章节
                    context_summary += f"- {prev.title}{prev.content[:200]}...\n"

        prompt = f"""你正在写一篇关于「{plan.topic}」的文章。

文章整体信息:
- 目标读者:{plan.target_audience}
- 写作风格:{plan.tone}
- 关键词:{', '.join(plan.keywords)}

{context_summary}

现在请写「{section.title}」这一章节:
写作要点:
{chr(10).join(f'- {p}' for p in section.key_points)}

要求:
- 字数约 {section.word_count}
- 风格与前文保持一致
- 自然融入关键词,避免堆砌
- 不要重复前文已经详细阐述的内容
- 直接输出章节正文,不要包含章节标题
"""

        response = self.client.chat.completions.create(
            model=self.model,
            messages=[{"role": "user", "content": prompt}],
            temperature=0.7
        )

        return response.choices[0].message.content.strip()

    def write_full_article(
        self,
        topic: str,
        **kwargs
    ) -> str:
        """完整流程:规划 → 分段写作 → 整合"""

        # 1. 生成写作计划
        plan = self.plan_article(topic, **kwargs)
        print(f"写作计划完成,共 {len(plan.sections)} 个章节")

        # 2. 逐段生成
        for i, section in enumerate(plan.sections):
            print(f"正在写第 {i+1}/{len(plan.sections)} 章:{section.title}")
            section.content = self.write_section(
                plan=plan,
                section=section,
                previous_sections=plan.sections[:i]
            )

        # 3. 整合与润色
        return self._assemble_article(plan)

    def _assemble_article(self, plan: ArticlePlan) -> str:
        """整合各章节并做全局润色"""
        parts = []

        for section in plan.sections:
            parts.append(f"## {section.title}\n\n{section.content}")

        full_draft = "\n\n".join(parts)

        # 全局润色(检查连贯性、消除重复)
        polish_prompt = f"""请对下面的文章草稿进行润色:

1. 检查章节间的过渡是否自然
2. 消除明显的重复内容
3. 确保整体风格统一({plan.tone}
4. 不要改变核心内容,只做语言层面的优化

草稿:
{full_draft[:4000]}

(如文章较长,请只润色前半部分,保持后半部分不变)
"""

        response = self.client.chat.completions.create(
            model=self.model,
            messages=[{"role": "user", "content": polish_prompt}],
            temperature=0.3
        )

        return response.choices[0].message.content.strip()

8.4.2 结构化报告生成

报告生成的关键是数据驱动——从原始数据自动提炼洞察,生成图表描述与结论。

python
from typing import Any, Dict
import pandas as pd

class ReportGenerator:
    """自动报告生成器:数据 → 分析 → 报告"""

    REPORT_TEMPLATES = {
        "business": {
            "sections": ["执行摘要", "市场分析", "竞争格局", "财务表现", "风险评估", "战略建议"],
            "tone": "正式、客观、数据驱动"
        },
        "research": {
            "sections": ["摘要", "研究背景", "研究方法", "数据分析", "研究发现", "结论与展望"],
            "tone": "学术、严谨、引用规范"
        },
        "weekly": {
            "sections": ["本周亮点", "数据表现", "问题与风险", "下周计划"],
            "tone": "简洁、直接、重点突出"
        }
    }

    def __init__(self, model: str = "gpt-4o"):
        self.client = OpenAI()
        self.model = model

    def analyze_data(self, df: pd.DataFrame) -> Dict[str, Any]:
        """自动分析数据,提取关键洞察"""
        stats = df.describe().to_dict()

        # 生成数据洞察
        data_summary = f"""
数据概览:
- 行数:{len(df)},列数:{len(df.columns)}
- 列名:{', '.join(df.columns.tolist())}
- 基础统计:{pd.DataFrame(stats).to_markdown()}
"""

        response = self.client.chat.completions.create(
            model=self.model,
            messages=[{
                "role": "user",
                "content": f"请从以下数据中提取 5 条最重要的业务洞察,每条洞察必须有数据支撑:\n{data_summary}"
            }]
        )

        return {
            "stats": stats,
            "insights": response.choices[0].message.content,
            "data_summary": data_summary
        }

    def generate_report(
        self,
        report_type: str,
        background: str,
        data: pd.DataFrame = None,
        extra_context: str = ""
    ) -> str:
        """生成完整报告"""
        template = self.REPORT_TEMPLATES.get(report_type, self.REPORT_TEMPLATES["business"])

        # 分析数据(如果有)
        data_insights = ""
        if data is not None:
            analysis = self.analyze_data(data)
            data_insights = f"\n\n数据洞察:\n{analysis['insights']}"

        sections_content = []

        for section_name in template["sections"]:
            section_prompt = f"""你正在撰写一份{report_type}报告的「{section_name}」部分。

背景信息:{background}
{data_insights}
{extra_context}

写作风格:{template['tone']}

请直接输出「{section_name}」的内容(200-400字),结构清晰,语言专业。
"""
            response = self.client.chat.completions.create(
                model=self.model,
                messages=[{"role": "user", "content": section_prompt}],
                temperature=0.4
            )

            sections_content.append(
                f"## {section_name}\n\n{response.choices[0].message.content.strip()}"
            )

        return "\n\n".join(sections_content)

    def generate_executive_summary(self, full_report: str) -> str:
        """从完整报告提炼执行摘要(TL;DR)"""
        response = self.client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[{
                "role": "user",
                "content": f"请将以下报告提炼为一段200字以内的执行摘要,突出核心结论和最重要的3个行动建议:\n\n{full_report[:6000]}"
            }],
            temperature=0.2
        )
        return response.choices[0].message.content.strip()

8.4.3 AI PPT 内容生成

自动生成 PPT 的核心流程:文本输入 → 大纲规划 → 幻灯片内容 → python-pptx 渲染

python
from pptx import Presentation
from pptx.util import Inches, Pt, Emu
from pptx.enum.text import PP_ALIGN
from pptx.dml.color import RGBColor
from dataclasses import dataclass
from typing import List
import json

@dataclass
class SlideContent:
    """单张幻灯片内容"""
    title: str
    bullet_points: List[str]
    speaker_notes: str = ""
    slide_type: str = "content"  # title/content/section/summary

class PPTGenerator:
    """AI 驱动的 PPT 内容生成器"""

    # 主题配色方案
    THEMES = {
        "business_blue": {
            "primary": RGBColor(0x1F, 0x49, 0x7D),
            "accent": RGBColor(0x2E, 0x75, 0xB6),
            "text": RGBColor(0x26, 0x26, 0x26),
            "bg": RGBColor(0xF5, 0xF5, 0xF5)
        },
        "modern_dark": {
            "primary": RGBColor(0x1A, 0x1A, 0x2E),
            "accent": RGBColor(0xE9, 0x4C, 0x60),
            "text": RGBColor(0xF0, 0xF0, 0xF0),
            "bg": RGBColor(0x16, 0x21, 0x3E)
        }
    }

    def __init__(self, model: str = "gpt-4o"):
        self.client = OpenAI()
        self.model = model

    def plan_slides(
        self,
        topic: str,
        slide_count: int = 12,
        audience: str = "企业管理层",
        purpose: str = "汇报"
    ) -> List[SlideContent]:
        """生成 PPT 大纲"""

        prompt = f"""为主题「{topic}」设计一份 PPT 大纲。

要求:
- 目标受众:{audience}
- 演讲目的:{purpose}
- 幻灯片数量:约 {slide_count} 页(含封面和结尾)
- 每页核心要点不超过 4 条,每条不超过 20 字
- 每页附带演讲者备注(50-100字,指导演讲思路)

以 JSON 格式返回:
{{
  "slides": [
    {{
      "title": "封面标题",
      "slide_type": "title",
      "bullet_points": ["副标题", "演讲人", "日期"],
      "speaker_notes": "开场白引导..."
    }},
    {{
      "title": "章节标题",
      "slide_type": "content",
      "bullet_points": ["要点1", "要点2", "要点3"],
      "speaker_notes": "这里重点强调..."
    }}
  ]
}}

slide_type 取值:title(封面)/ section(过渡页)/ content(内容页)/ summary(总结页)
"""

        response = self.client.chat.completions.create(
            model=self.model,
            messages=[{"role": "user", "content": prompt}],
            response_format={"type": "json_object"}
        )

        data = json.loads(response.choices[0].message.content)
        return [SlideContent(**s) for s in data["slides"]]

    def create_pptx(
        self,
        slides: List[SlideContent],
        output_path: str,
        theme: str = "business_blue"
    ) -> str:
        """将幻灯片内容渲染为 .pptx 文件"""

        prs = Presentation()
        prs.slide_width = Inches(13.33)
        prs.slide_height = Inches(7.5)
        colors = self.THEMES.get(theme, self.THEMES["business_blue"])

        for slide_content in slides:
            if slide_content.slide_type == "title":
                self._add_title_slide(prs, slide_content, colors)
            elif slide_content.slide_type == "section":
                self._add_section_slide(prs, slide_content, colors)
            else:
                self._add_content_slide(prs, slide_content, colors)

        prs.save(output_path)
        return output_path

    def _add_title_slide(self, prs, content: SlideContent, colors: dict):
        """封面幻灯片"""
        blank_layout = prs.slide_layouts[6]  # 空白布局
        slide = prs.slides.add_slide(blank_layout)

        # 背景色
        background = slide.background
        fill = background.fill
        fill.solid()
        fill.fore_color.rgb = colors["primary"]

        # 主标题
        txBox = slide.shapes.add_textbox(Inches(1), Inches(2.5), Inches(11.3), Inches(1.5))
        tf = txBox.text_frame
        tf.word_wrap = True
        p = tf.add_paragraph()
        p.text = content.title
        p.font.size = Pt(40)
        p.font.bold = True
        p.font.color.rgb = RGBColor(0xFF, 0xFF, 0xFF)
        p.alignment = PP_ALIGN.CENTER

        # 副标题等信息
        if content.bullet_points:
            txBox2 = slide.shapes.add_textbox(Inches(1), Inches(4.2), Inches(11.3), Inches(1.5))
            tf2 = txBox2.text_frame
            for bp in content.bullet_points:
                p2 = tf2.add_paragraph()
                p2.text = bp
                p2.font.size = Pt(20)
                p2.font.color.rgb = RGBColor(0xCC, 0xCC, 0xCC)
                p2.alignment = PP_ALIGN.CENTER

        # 添加演讲者备注
        if content.speaker_notes:
            slide.notes_slide.notes_text_frame.text = content.speaker_notes

    def _add_content_slide(self, prs, content: SlideContent, colors: dict):
        """内容幻灯片"""
        blank_layout = prs.slide_layouts[6]
        slide = prs.slides.add_slide(blank_layout)

        # 顶部色条
        shape = slide.shapes.add_shape(1, 0, 0, prs.slide_width, Inches(0.8))
        shape.fill.solid()
        shape.fill.fore_color.rgb = colors["primary"]
        shape.line.fill.background()

        # 标题
        txBox = slide.shapes.add_textbox(Inches(0.5), Inches(0.1), Inches(12.3), Inches(0.7))
        tf = txBox.text_frame
        p = tf.add_paragraph()
        p.text = content.title
        p.font.size = Pt(24)
        p.font.bold = True
        p.font.color.rgb = RGBColor(0xFF, 0xFF, 0xFF)

        # 要点列表
        txBox2 = slide.shapes.add_textbox(Inches(0.8), Inches(1.2), Inches(11.5), Inches(5.5))
        tf2 = txBox2.text_frame
        tf2.word_wrap = True

        for bp in content.bullet_points:
            p2 = tf2.add_paragraph()
            p2.text = f"• {bp}"
            p2.font.size = Pt(20)
            p2.font.color.rgb = colors["text"]
            p2.space_before = Pt(12)

        if content.speaker_notes:
            slide.notes_slide.notes_text_frame.text = content.speaker_notes

    def _add_section_slide(self, prs, content: SlideContent, colors: dict):
        """过渡章节页"""
        blank_layout = prs.slide_layouts[6]
        slide = prs.slides.add_slide(blank_layout)

        background = slide.background
        fill = background.fill
        fill.solid()
        fill.fore_color.rgb = colors["accent"]

        txBox = slide.shapes.add_textbox(Inches(1), Inches(3), Inches(11.3), Inches(1.5))
        tf = txBox.text_frame
        p = tf.add_paragraph()
        p.text = content.title
        p.font.size = Pt(36)
        p.font.bold = True
        p.font.color.rgb = RGBColor(0xFF, 0xFF, 0xFF)
        p.alignment = PP_ALIGN.CENTER

    def generate_full_pptx(
        self,
        topic: str,
        output_path: str = "output.pptx",
        **kwargs
    ) -> str:
        """完整流程:主题 → PPT 文件"""
        print(f"规划幻灯片结构...")
        slides = self.plan_slides(topic, **kwargs)

        print(f"生成 {len(slides)} 页幻灯片...")
        return self.create_pptx(slides, output_path)

8.4.4 多模态内容创作

图文混排生成

python
import base64
import requests
from pathlib import Path

class MultimodalContentCreator:
    """多模态内容创作:图文/视频脚本/社交媒体内容"""

    def __init__(self):
        self.client = OpenAI()

    # ── 图片生成(DALL·E 3)──────────────────────────────────────────
    def generate_image(
        self,
        description: str,
        style: str = "modern, professional",
        size: str = "1792x1024",   # 宽屏 / 1024x1024 方图
        quality: str = "hd"
    ) -> str:
        """生成配图,返回图片 URL"""

        # 优化图片提示词
        enhanced_prompt = self._enhance_image_prompt(description, style)

        response = self.client.images.generate(
            model="dall-e-3",
            prompt=enhanced_prompt,
            size=size,
            quality=quality,
            n=1
        )

        return response.data[0].url

    def _enhance_image_prompt(self, description: str, style: str) -> str:
        """利用 LLM 优化图片提示词"""
        response = self.client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[{
                "role": "user",
                "content": f"""将下面的描述转化为适合 DALL·E 3 的英文图片提示词。
要求:专业、具体、包含构图和光线描述,风格:{style}

描述:{description}

只输出英文提示词,不要任何解释。"""
            }],
            max_tokens=200
        )
        return response.choices[0].message.content.strip()

    # ── 图文内容一体化生成 ──────────────────────────────────────────
    def create_illustrated_article(
        self,
        topic: str,
        section_count: int = 4
    ) -> dict:
        """生成带配图的图文文章"""

        # 1. 生成文章结构
        plan_response = self.client.chat.completions.create(
            model="gpt-4o",
            messages=[{
                "role": "user",
                "content": f"""为「{topic}」生成一篇图文并茂的文章结构。
以 JSON 格式返回:
{{
  "title": "文章标题",
  "sections": [
    {{
      "heading": "章节标题",
      "content": "200字章节内容",
      "image_description": "该章节配图的中文描述(用于生成图片)"
    }}
  ]
}}
{section_count} 个章节。"""
            }],
            response_format={"type": "json_object"}
        )

        article = json.loads(plan_response.choices[0].message.content)

        # 2. 为每个章节生成配图
        for section in article["sections"]:
            try:
                section["image_url"] = self.generate_image(
                    description=section["image_description"],
                    style="modern, clean, professional illustration"
                )
            except Exception as e:
                section["image_url"] = None
                print(f"图片生成失败: {e}")

        return article

    # ── 视频脚本生成 ──────────────────────────────────────────────
    def create_video_script(
        self,
        topic: str,
        duration_minutes: int = 5,
        video_type: str = "explainer",   # explainer/tutorial/marketing
        platform: str = "YouTube"
    ) -> dict:
        """生成视频脚本(含分镜头描述)"""

        prompt = f"""为以下视频创作完整脚本:

主题:{topic}
视频类型:{video_type}
目标时长:{duration_minutes} 分钟
发布平台:{platform}

以 JSON 格式返回:
{{
  "title": "视频标题(含SEO关键词)",
  "hook": "前3秒钩子语句(吸引观众继续看)",
  "scenes": [
    {{
      "scene_number": 1,
      "duration_seconds": 30,
      "narration": "旁白/解说词",
      "visual_description": "画面描述(用于指导拍摄或动画制作)",
      "on_screen_text": "画面上显示的文字(可选)"
    }}
  ],
  "call_to_action": "结尾引导语",
  "tags": ["标签1", "标签2"],
  "description": "视频简介(适合平台算法)"
}}

每个场景时长相加约等于总时长。旁白语速约 150字/分钟。
"""

        response = self.client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": prompt}],
            response_format={"type": "json_object"}
        )

        return json.loads(response.choices[0].message.content)

    # ── 社交媒体矩阵生成 ──────────────────────────────────────────
    def create_social_matrix(
        self,
        core_content: str,
        platforms: List[str] = None
    ) -> dict:
        """
        一键生成多平台社交媒体内容
        一篇核心内容 → 微博/公众号/小红书/LinkedIn/Twitter 的不同版本
        """
        if platforms is None:
            platforms = ["微博", "微信公众号", "小红书", "LinkedIn"]

        platform_specs = {
            "微博": "140字以内,口语化,带话题标签 #xx#,结尾引导互动",
            "微信公众号": "800-1200字,排版规范,有小标题,适合深度阅读",
            "小红书": "300字以内,emoji丰富,分点列举,带话题标签,种草风格",
            "LinkedIn": "200字以内,英文,专业语气,行业洞察,适合B2B",
            "Twitter": "280字符以内,英文,简洁有力,带hashtag"
        }

        results = {}

        for platform in platforms:
            spec = platform_specs.get(platform, "适合该平台的内容格式")

            response = self.client.chat.completions.create(
                model="gpt-4o-mini",
                messages=[{
                    "role": "user",
                    "content": f"""将以下核心内容改写为适合「{platform}」的版本。

平台规范:{spec}

核心内容:
{core_content}

直接输出改写后的内容。"""
                }],
                temperature=0.8
            )

            results[platform] = response.choices[0].message.content.strip()

        return results

8.4.5 写作风格迁移与一致性控制

风格学习与复制

python
class StyleAdapter:
    """写作风格适配器:学习特定写作风格并应用"""

    def __init__(self):
        self.client = OpenAI()

    def analyze_style(self, sample_texts: List[str]) -> dict:
        """从样本文本中提取写作风格特征"""

        samples_combined = "\n\n---\n\n".join(sample_texts[:5])  # 最多5个样本

        response = self.client.chat.completions.create(
            model="gpt-4o",
            messages=[{
                "role": "user",
                "content": f"""分析以下文本的写作风格特征,从以下维度总结:
1. 句式结构(长句/短句/混合,使用的修辞手法)
2. 词汇偏好(正式/口语,常用词汇类型)
3. 段落结构(长度,段落间的逻辑关系)
4. 语气特点(客观/主观,情感温度)
5. 独特标志(特定的用词习惯、结构模式)

文本样本:
{samples_combined}

以 JSON 格式返回风格描述。"""
            }],
            response_format={"type": "json_object"}
        )

        return json.loads(response.choices[0].message.content)

    def write_in_style(
        self,
        topic: str,
        style_profile: dict,
        length: str = "500字"
    ) -> str:
        """按照指定风格特征写作"""

        style_description = json.dumps(style_profile, ensure_ascii=False, indent=2)

        response = self.client.chat.completions.create(
            model="gpt-4o",
            messages=[{
                "role": "user",
                "content": f"""请用以下风格特征写一篇关于「{topic}」的文章(约{length})。

风格要求(严格遵守):
{style_description}

直接输出文章正文,不要任何说明。"""
            }],
            temperature=0.8
        )

        return response.choices[0].message.content.strip()

8.4.6 FastAPI 服务与流式输出

python
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from typing import Optional
import asyncio

app = FastAPI(title="AI 内容生成服务")

class ArticleRequest(BaseModel):
    topic: str
    tone: str = "专业"
    word_count: int = 1500
    target_audience: str = "通用读者"

class PPTRequest(BaseModel):
    topic: str
    slide_count: int = 12
    audience: str = "企业管理层"
    purpose: str = "汇报"

class VideoScriptRequest(BaseModel):
    topic: str
    duration_minutes: int = 5
    video_type: str = "explainer"
    platform: str = "YouTube"

@app.post("/article/stream")
async def stream_article(req: ArticleRequest):
    """流式生成文章(边写边输出)"""
    from openai import AsyncOpenAI

    async_client = AsyncOpenAI()

    async def generate():
        writer = LongArticleWriter()
        plan = writer.plan_article(
            topic=req.topic,
            tone=req.tone,
            word_count=req.word_count,
            target_audience=req.target_audience
        )

        # 流式输出大纲信息
        yield f"data: {json.dumps({'type': 'plan', 'sections': [s.title for s in plan.sections]}, ensure_ascii=False)}\n\n"

        # 流式生成各章节
        for i, section in enumerate(plan.sections):
            yield f"data: {json.dumps({'type': 'section_start', 'title': section.title}, ensure_ascii=False)}\n\n"

            # 使用流式 API 输出段落内容
            stream = await async_client.chat.completions.create(
                model="gpt-4o",
                messages=[{
                    "role": "user",
                    "content": f"写「{section.title}」章节({section.word_count}字):{', '.join(section.key_points)}"
                }],
                stream=True
            )

            async for chunk in stream:
                delta = chunk.choices[0].delta.content or ""
                if delta:
                    section.content += delta
                    yield f"data: {json.dumps({'type': 'content', 'delta': delta}, ensure_ascii=False)}\n\n"

        yield f"data: {json.dumps({'type': 'done'}, ensure_ascii=False)}\n\n"

    return StreamingResponse(generate(), media_type="text/event-stream")

@app.post("/ppt/generate")
async def generate_ppt(req: PPTRequest):
    """生成 PPT 文件"""
    import tempfile
    import os
    from fastapi.responses import FileResponse

    generator = PPTGenerator()
    slides = generator.plan_slides(
        topic=req.topic,
        slide_count=req.slide_count,
        audience=req.audience,
        purpose=req.purpose
    )

    with tempfile.NamedTemporaryFile(delete=False, suffix=".pptx") as tmp:
        output_path = generator.create_pptx(slides, tmp.name)

    return FileResponse(
        output_path,
        media_type="application/vnd.openxmlformats-officedocument.presentationml.presentation",
        filename=f"{req.topic}.pptx",
        background=asyncio.create_task(asyncio.sleep(60))  # 60s 后清理临时文件
    )

@app.post("/video-script")
async def generate_video_script(req: VideoScriptRequest):
    """生成视频脚本"""
    creator = MultimodalContentCreator()
    script = creator.create_video_script(
        topic=req.topic,
        duration_minutes=req.duration_minutes,
        video_type=req.video_type,
        platform=req.platform
    )
    return script

@app.post("/social-matrix")
async def generate_social_matrix(core_content: str, platforms: List[str] = None):
    """一键生成多平台内容"""
    creator = MultimodalContentCreator()
    return creator.create_social_matrix(core_content, platforms)

内容质量评估

python
class ContentQualityEvaluator:
    """LLM-as-Judge:自动评估生成内容质量"""

    EVALUATION_CRITERIA = {
        "article": ["准确性", "连贯性", "可读性", "深度", "原创性"],
        "report": ["数据支撑", "逻辑严密性", "结论清晰度", "专业性"],
        "marketing": ["吸引力", "品牌一致性", "行动号召力", "平台适配性"]
    }

    def __init__(self):
        self.client = OpenAI()

    def evaluate(
        self,
        content: str,
        content_type: str = "article",
        reference: str = ""
    ) -> dict:
        """对生成内容进行多维度评分"""

        criteria = self.EVALUATION_CRITERIA.get(content_type, self.EVALUATION_CRITERIA["article"])
        criteria_str = "\n".join([f"- {c}(1-10分)" for c in criteria])

        prompt = f"""请对以下{content_type}内容进行专业评估:

{content[:3000]}

评估维度:
{criteria_str}
- 总体评分(1-10分)

以 JSON 格式返回,每个维度包含分数和改进建议:
{{
  "scores": {{{", ".join([f'"{c}": {{"score": 8, "comment": "..."}}' for c in criteria[:2]])}, ...}},
  "overall_score": 8.5,
  "strengths": ["亮点1", "亮点2"],
  "improvements": ["改进建议1", "改进建议2"]
}}
"""

        response = self.client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": prompt}],
            response_format={"type": "json_object"},
            temperature=0.1
        )

        return json.loads(response.choices[0].message.content)

系统关键指标

指标目标值优化方向
内容生成速度≥ 500 字/分钟流式输出、模型并发
长文一致性评分≥ 8/10大纲驱动 + 前文摘要注入
幻觉/事实错误率≤ 5%搜索增强 + 事实核查
风格匹配度≥ 85%风格分析 + Few-shot 示例
用户采纳率(无需大改)≥ 70%多轮迭代 + 用户偏好学习

坚持是一种品格