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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ai_blog_assistant.py
|
| 2 |
+
from typing import TypedDict, Annotated, List
|
| 3 |
+
from langgraph.graph import StateGraph, END
|
| 4 |
+
from langgraph.graph.message import add_messages
|
| 5 |
+
from llm_selector import LLMSelector
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
class BlogState(TypedDict):
|
| 9 |
+
messages: Annotated[list, add_messages]
|
| 10 |
+
topic: str
|
| 11 |
+
keywords: List[str]
|
| 12 |
+
outline: str
|
| 13 |
+
research: str
|
| 14 |
+
draft: str
|
| 15 |
+
final: str
|
| 16 |
+
seo_score: int
|
| 17 |
+
|
| 18 |
+
# 使用最快的免费模型
|
| 19 |
+
llm = LLMSelector.get_groq(model="llama-3.1-8b-instant") # 最快
|
| 20 |
+
quality_llm = LLMSelector.get_groq(model="llama-3.1-70b-versatile") # 质量
|
| 21 |
+
|
| 22 |
+
def keyword_analyzer(state: BlogState) -> BlogState:
|
| 23 |
+
"""Agent 1: 关键词分析"""
|
| 24 |
+
prompt = f"""你是SEO专家。
|
| 25 |
+
|
| 26 |
+
主题: {state['topic']}
|
| 27 |
+
|
| 28 |
+
任务: 提取5-8个核心关键词
|
| 29 |
+
要求: 考虑搜索量和竞争度
|
| 30 |
+
|
| 31 |
+
只输出关键词列表(JSON格式):
|
| 32 |
+
{{"keywords": ["关键词1", "关键词2", ...]}}"""
|
| 33 |
+
|
| 34 |
+
response = llm.invoke(prompt)
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
result = json.loads(response.content)
|
| 38 |
+
state['keywords'] = result['keywords']
|
| 39 |
+
except:
|
| 40 |
+
state['keywords'] = ["人工智能", "技术", "应用"]
|
| 41 |
+
|
| 42 |
+
print(f"✅ [关键词分析] {state['keywords']}")
|
| 43 |
+
return state
|
| 44 |
+
|
| 45 |
+
def outline_creator(state: BlogState) -> BlogState:
|
| 46 |
+
"""Agent 2: 创建大纲"""
|
| 47 |
+
prompt = f"""你是内容策划师。
|
| 48 |
+
|
| 49 |
+
主题: {state['topic']}
|
| 50 |
+
关键词: {', '.join(state['keywords'])}
|
| 51 |
+
|
| 52 |
+
任务: 创建文章大纲
|
| 53 |
+
要求:
|
| 54 |
+
1. 3-5个主要章节
|
| 55 |
+
2. 每章节2-3个小点
|
| 56 |
+
3. 逻辑清晰
|
| 57 |
+
|
| 58 |
+
输出格式:
|
| 59 |
+
# 标题
|
| 60 |
+
## 章节1
|
| 61 |
+
- 要点1
|
| 62 |
+
- 要点2
|
| 63 |
+
...
|
| 64 |
+
|
| 65 |
+
直接给出大纲:"""
|
| 66 |
+
|
| 67 |
+
response = llm.invoke(prompt)
|
| 68 |
+
state['outline'] = response.content
|
| 69 |
+
print(f"✅ [大纲创建] 完成")
|
| 70 |
+
return state
|
| 71 |
+
|
| 72 |
+
def researcher(state: BlogState) -> BlogState:
|
| 73 |
+
"""Agent 3: 深度研究"""
|
| 74 |
+
prompt = f"""你是资深研究员。
|
| 75 |
+
|
| 76 |
+
主题: {state['topic']}
|
| 77 |
+
大纲: {state['outline']}
|
| 78 |
+
|
| 79 |
+
任务: 为每个章节收集资料
|
| 80 |
+
要求:
|
| 81 |
+
1. 数据支撑
|
| 82 |
+
2. 案例分析
|
| 83 |
+
3. 最新趋势
|
| 84 |
+
|
| 85 |
+
输出研究报告:"""
|
| 86 |
+
|
| 87 |
+
response = quality_llm.invoke(prompt) # 用更好的模型
|
| 88 |
+
state['research'] = response.content
|
| 89 |
+
print(f"✅ [研究] 完成")
|
| 90 |
+
return state
|
| 91 |
+
|
| 92 |
+
def writer(state: BlogState) -> BlogState:
|
| 93 |
+
"""Agent 4: 撰写文章"""
|
| 94 |
+
prompt = f"""你是专业博客作家。
|
| 95 |
+
|
| 96 |
+
主题: {state['topic']}
|
| 97 |
+
关键词: {', '.join(state['keywords'])}
|
| 98 |
+
大纲: {state['outline']}
|
| 99 |
+
研究资料: {state['research']}
|
| 100 |
+
|
| 101 |
+
任务: 撰写1500字博客文章
|
| 102 |
+
要求:
|
| 103 |
+
1. 标题吸引人(包含主关键词)
|
| 104 |
+
2. 自然融入关键词
|
| 105 |
+
3. 段落简短(2-3句)
|
| 106 |
+
4. 使用项目符号
|
| 107 |
+
5. 包含行动号召(CTA)
|
| 108 |
+
|
| 109 |
+
撰写文章:"""
|
| 110 |
+
|
| 111 |
+
response = quality_llm.invoke(prompt)
|
| 112 |
+
state['draft'] = response.content
|
| 113 |
+
print(f"✅ [写作] 完成")
|
| 114 |
+
return state
|
| 115 |
+
|
| 116 |
+
def seo_optimizer(state: BlogState) -> BlogState:
|
| 117 |
+
"""Agent 5: SEO优化"""
|
| 118 |
+
prompt = f"""你是SEO优化专家。
|
| 119 |
+
|
| 120 |
+
文章:
|
| 121 |
+
{state['draft']}
|
| 122 |
+
|
| 123 |
+
关键词: {', '.join(state['keywords'])}
|
| 124 |
+
|
| 125 |
+
任务: 优化SEO
|
| 126 |
+
1. 检查关键词密度 (2-3%)
|
| 127 |
+
2. 优化标题和副标题
|
| 128 |
+
3. 添加meta描述
|
| 129 |
+
4. 改进可读性
|
| 130 |
+
|
| 131 |
+
输出:
|
| 132 |
+
1. 优化后的文章
|
| 133 |
+
2. SEO评分 (0-100)
|
| 134 |
+
3. 改进建议
|
| 135 |
+
|
| 136 |
+
格式:
|
| 137 |
+
=== 优化文章 ===
|
| 138 |
+
[文章内容]
|
| 139 |
+
|
| 140 |
+
=== SEO评分 ===
|
| 141 |
+
评分: XX分
|
| 142 |
+
建议: ...
|
| 143 |
+
|
| 144 |
+
给出结果:"""
|
| 145 |
+
|
| 146 |
+
response = quality_llm.invoke(prompt)
|
| 147 |
+
content = response.content
|
| 148 |
+
|
| 149 |
+
# 提取评分
|
| 150 |
+
if "评分:" in content:
|
| 151 |
+
try:
|
| 152 |
+
score_line = [line for line in content.split('\n') if '评分:' in line][0]
|
| 153 |
+
score = int(''.join(filter(str.isdigit, score_line)))
|
| 154 |
+
state['seo_score'] = min(score, 100)
|
| 155 |
+
except:
|
| 156 |
+
state['seo_score'] = 75
|
| 157 |
+
|
| 158 |
+
state['final'] = content
|
| 159 |
+
print(f"✅ [SEO优化] 评分: {state['seo_score']}")
|
| 160 |
+
return state
|
| 161 |
+
|
| 162 |
+
# 构建工作流
|
| 163 |
+
def create_blog_workflow():
|
| 164 |
+
workflow = StateGraph(BlogState)
|
| 165 |
+
|
| 166 |
+
workflow.add_node("keywords", keyword_analyzer)
|
| 167 |
+
workflow.add_node("outline", outline_creator)
|
| 168 |
+
workflow.add_node("research", researcher)
|
| 169 |
+
workflow.add_node("write", writer)
|
| 170 |
+
workflow.add_node("seo", seo_optimizer)
|
| 171 |
+
|
| 172 |
+
workflow.set_entry_point("keywords")
|
| 173 |
+
workflow.add_edge("keywords", "outline")
|
| 174 |
+
workflow.add_edge("outline", "research")
|
| 175 |
+
workflow.add_edge("research", "write")
|
| 176 |
+
workflow.add_edge("write", "seo")
|
| 177 |
+
workflow.add_edge("seo", END)
|
| 178 |
+
|
| 179 |
+
return workflow.compile()
|
| 180 |
+
|
| 181 |
+
# 运行
|
| 182 |
+
def generate_blog(topic: str):
|
| 183 |
+
app = create_blog_workflow()
|
| 184 |
+
|
| 185 |
+
initial_state = {
|
| 186 |
+
"messages": [],
|
| 187 |
+
"topic": topic,
|
| 188 |
+
"keywords": [],
|
| 189 |
+
"outline": "",
|
| 190 |
+
"research": "",
|
| 191 |
+
"draft": "",
|
| 192 |
+
"final": "",
|
| 193 |
+
"seo_score": 0
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
print(f"\n{'='*70}")
|
| 197 |
+
print(f"🚀 开始生成博客: {topic}")
|
| 198 |
+
print(f"{'='*70}\n")
|
| 199 |
+
|
| 200 |
+
result = app.invoke(initial_state)
|
| 201 |
+
|
| 202 |
+
print(f"\n{'='*70}")
|
| 203 |
+
print(f"📝 最终文章 (SEO评分: {result['seo_score']})")
|
| 204 |
+
print(f"{'='*70}\n")
|
| 205 |
+
print(result['final'])
|
| 206 |
+
|
| 207 |
+
# 保存文章
|
| 208 |
+
filename = f"blog_{topic[:20].replace(' ', '_')}.md"
|
| 209 |
+
with open(filename, 'w', encoding='utf-8') as f:
|
| 210 |
+
f.write(f"# {topic}\n\n")
|
| 211 |
+
f.write(f"**关键词**: {', '.join(result['keywords'])}\n\n")
|
| 212 |
+
f.write(f"**SEO评分**: {result['seo_score']}/100\n\n")
|
| 213 |
+
f.write("---\n\n")
|
| 214 |
+
f.write(result['final'])
|
| 215 |
+
|
| 216 |
+
print(f"\n💾 文章已保存到: {filename}")
|
| 217 |
+
return result
|
| 218 |
+
|
| 219 |
+
if __name__ == "__main__":
|
| 220 |
+
generate_blog("ChatGPT在企业中的5个创新应用")
|