Files
article-workflow/main.py
hubian 3094a5cb56 初始化文章撰写大模型工作流系统
功能模块:
- 主题选择器: 基于历史文章和热门趋势自动推荐主题
- 资料收集器: 自动搜索和收集相关资料
- 资料分析器: 深度分析资料内容,提取关键信息
- 大纲生成器: 基于资料自动生成文章大纲
- 文章撰写器: 分段撰写,支持多种文章类型
- 资料池管理: 中间产物持久化存储,建立索引

支持的文字类型:
- 技术解析
- 技术文档翻译
- 项目介绍分析
- 综述文章
- 实践教程
- 问题分析

技术栈:
- Python 3.x
- 大模型API (OpenAI格式)
- Requests库

配置的大模型:
- URL: http://192.168.2.5:1234/v1
- Model: qwen3.5-4b
2026-04-08 11:40:20 +08:00

143 lines
4.6 KiB
Python

"""
文章撰写工作流系统 - 主入口
"""
import os
import sys
import json
import argparse
from datetime import datetime
from pathlib import Path
# 添加src到路径
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from config.settings import (
LLM_CONFIG, ARTICLE_TYPES, RESOURCE_POOL_CONFIG,
SEARCH_CONFIG, OUTPUT_CONFIG
)
from src.topic_selector import TopicSelector
from src.resource_collector import ResourceCollector
from src.resource_analyzer import ResourceAnalyzer
from src.outline_generator import OutlineGenerator
from src.article_writer import ArticleWriter
from src.resource_pool import ResourcePool
def main():
parser = argparse.ArgumentParser(description='文章撰写大模型工作流系统')
parser.add_argument('--mode', choices=['full', 'topic', 'collect', 'analyze', 'outline', 'write'],
default='full', help='运行模式')
parser.add_argument('--topic', type=str, help='指定文章主题')
parser.add_argument('--type', type=str, choices=list(ARTICLE_TYPES.keys()),
help='指定文章类型')
parser.add_argument('--interactive', action='store_true', help='交互式模式')
args = parser.parse_args()
print("=" * 60)
print("文章撰写大模型工作流系统")
print("=" * 60)
# 初始化组件
pool = ResourcePool(RESOURCE_POOL_CONFIG)
topic_selector = TopicSelector(LLM_CONFIG, pool)
collector = ResourceCollector(LLM_CONFIG, SEARCH_CONFIG, pool)
analyzer = ResourceAnalyzer(LLM_CONFIG, pool)
outline_gen = OutlineGenerator(LLM_CONFIG, pool)
writer = ArticleWriter(LLM_CONFIG, OUTPUT_CONFIG, pool)
if args.mode == 'full':
# 完整工作流
run_full_workflow(args, topic_selector, collector, analyzer, outline_gen, writer, pool)
elif args.mode == 'topic':
# 仅选择主题
topic, article_type = topic_selector.select(args.interactive)
print(f"\n确定主题: {topic}")
print(f"文章类型: {article_type}")
elif args.mode == 'collect':
# 仅收集资料
if not args.topic:
print("请指定主题: --topic")
return
collector.collect(args.topic)
elif args.mode == 'analyze':
# 仅分析资料
analyzer.analyze_all()
elif args.mode == 'outline':
# 仅生成大纲
if not args.topic:
print("请指定主题: --topic")
return
outline = outline_gen.generate(args.topic, args.type or "技术解析")
print(f"\n文章大纲:\n{outline}")
elif args.mode == 'write':
# 仅写作文章
if not args.topic:
print("请指定主题: --topic")
return
article = writer.write(args.topic, args.type or "技术解析")
print(f"\n文章已生成: {article}")
def run_full_workflow(args, topic_selector, collector, analyzer, outline_gen, writer, pool):
"""运行完整工作流"""
# Step 1: 确定主题和类型
print("\n【阶段1】确定文章主题和类型")
print("-" * 40)
if args.topic and args.type:
topic = args.topic
article_type = args.type
print(f"使用指定主题: {topic}")
print(f"文章类型: {article_type}")
else:
topic, article_type = topic_selector.select(args.interactive)
print(f"\n确定主题: {topic}")
print(f"文章类型: {article_type}")
# Step 2: 收集相关资料
print("\n【阶段2】收集相关资料")
print("-" * 40)
resources = collector.collect(topic)
print(f"收集到 {len(resources)} 份资料")
# Step 3: 分析资料
print("\n【阶段3】分析资料内容")
print("-" * 40)
analyzed_resources = analyzer.analyze_all(topic)
print(f"完成 {len(analyzed_resources)} 份资料分析")
# Step 4: 生成文章大纲
print("\n【阶段4】生成文章大纲")
print("-" * 40)
outline = outline_gen.generate(topic, article_type)
print(f"大纲生成完成")
# Step 5: 撰写文章
print("\n【阶段5】撰写文章")
print("-" * 40)
article_path = writer.write(topic, article_type, outline)
# 完成
print("\n" + "=" * 60)
print("工作流完成!")
print("=" * 60)
print(f"文章输出: {article_path}")
# 更新历史记录
pool.add_article_history({
"topic": topic,
"type": article_type,
"date": datetime.now().isoformat(),
"output_path": article_path,
"resources_count": len(analyzed_resources),
})
if __name__ == '__main__':
main()