2025-07-08 18:24:23 +08:00

83 lines
3.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
内容生成流程管理器
"""
import logging
import json
from typing import List, Dict, Any
from core.config import get_config_manager, AIModelConfig, GenerateTopicConfig, SystemConfig
from core.ai import AIAgent
from .file_io import OutputManager
from .tweet.topic_generator import TopicGenerator
from .tweet.content_generator import ContentGenerator
from .tweet.content_judger import ContentJudger
logger = logging.getLogger(__name__)
class PipelineManager:
"""
协调从选题生成到内容审核的整个流程
"""
def __init__(self, config_dir: str, run_id: str):
# 1. 加载配置
config_manager = get_config_manager()
config_manager.load_from_directory(config_dir)
self.ai_config = config_manager.get_config('ai_model', AIModelConfig)
self.topic_config = config_manager.get_config('topic_gen', GenerateTopicConfig)
self.system_config = config_manager.get_config('system', SystemConfig)
# 2. 初始化组件
self.ai_agent = AIAgent(self.ai_config)
self.output_manager = OutputManager(self.topic_config.output.output_dir, run_id)
self.topic_generator = TopicGenerator(self.ai_agent, self.topic_config, self.output_manager)
self.content_generator = ContentGenerator(self.ai_agent, self.topic_config, self.output_manager)
judger_system_prompt = self.topic_config.judger_system_prompt # Assuming this key exists
self.content_judger = ContentJudger(self.ai_agent, judger_system_prompt)
logger.info("PipelineManager 初始化完成。")
def run_pipeline(self):
"""
执行完整的内容生成流程
"""
logger.info("开始执行内容生成流程...")
# 步骤 1: 生成选题
topics = self.topic_generator.generate()
if not topics:
logger.critical("无法生成选题,流程终止。")
return
# 步骤 2: 为每个选题生成内容
for i, topic in enumerate(topics):
logger.info(f"--- 开始处理选题 {i+1}/{len(topics)} ---")
# 步骤 2a: 生成内容变体
for j in range(self.topic_config.topic.variants):
content_result = self.content_generator.generate(topic, i, j)
if content_result.get("error"):
logger.error(f"为选题 {i} 生成内容变体 {j} 失败。")
continue
# 步骤 2b: (可选) 审核内容
if self.topic_config.content.enable_content_judge:
product_info = self.topic_generator.prompt_builder.product_content
judged_result = self.content_judger.judge(
product_info=product_info,
generated_content=json.dumps(content_result, ensure_ascii=False)
)
# 将审核结果保存或与原始内容合并
self.output_manager.save_json(judged_result, f"article_{i}_{j}_judged.json")
logger.info("内容生成流程全部完成。")
self.output_manager.finalize()