forked from The-Pocket/PocketFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnodes.py
More file actions
502 lines (407 loc) · 16.1 KB
/
nodes.py
File metadata and controls
502 lines (407 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
from pocketflow import Node
from utils.call_llm import call_llm, analyze_with_llm, generate_with_llm
from utils.document_processor import parse_document, apply_styles, generate_html_from_markdown
from utils.image_processor import process_image_with_effects, batch_process_images
import json
import yaml
import os
import logging
# 配置日志
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class ParseRequirementNode(Node):
"""解析用户需求节点"""
def prep(self, shared):
"""准备用户指令数据"""
return shared.get("user_instruction", "")
def exec(self, prep_res):
"""使用LLM解析用户需求"""
if not prep_res:
return {"error": "没有提供用户指令"}
prompt = f"""
作为专业的文档设计师,请分析用户的以下指令,并提取出具体的格式要求:
用户指令:"{prep_res}"
请分析并返回以下信息(JSON格式):
{{
"style": "用户希望的整体风格(如商务、学术、创意等)",
"format": "输出格式(如PDF、HTML、Word等)",
"layout": {{
"font_size": "字体大小偏好",
"color_scheme": "配色方案",
"spacing": "间距要求"
}},
"image_style": {{
"unified_size": "是否统一图片尺寸",
"effects": "图片效果要求(如边框、阴影、圆角等)",
"alignment": "图片对齐方式"
}},
"special_requirements": "其他特殊要求"
}}
如果用户指令比较简单,请根据常见的设计原则补充合理的默认设置。
"""
result = call_llm(prompt)
try:
# 尝试解析JSON
if "```json" in result:
json_str = result.split("```json")[1].split("```")[0].strip()
else:
json_str = result
parsed_requirements = json.loads(json_str)
return parsed_requirements
except json.JSONDecodeError:
# 如果JSON解析失败,返回基本的默认设置
logger.warning("无法解析LLM返回的JSON,使用默认设置")
return {
"style": "现代简约",
"format": "HTML",
"layout": {
"font_size": "适中",
"color_scheme": "蓝白配色",
"spacing": "标准"
},
"image_style": {
"unified_size": True,
"effects": "圆角边框",
"alignment": "居中"
},
"special_requirements": prep_res
}
def post(self, shared, prep_res, exec_res):
"""保存解析结果"""
shared["requirements"] = exec_res
logger.info(f"需求解析完成: {exec_res.get('style', 'Unknown')}风格")
return "default"
class AnalyzeDocumentNode(Node):
"""文档分析节点"""
def prep(self, shared):
"""准备文档内容"""
return {
"content": shared.get("original_document", ""),
"file_type": shared.get("file_type", "markdown")
}
def exec(self, prep_res):
"""分析文档结构"""
content = prep_res["content"]
file_type = prep_res["file_type"]
if not content:
return {"error": "没有提供文档内容"}
# 使用文档处理器解析结构
document_structure = parse_document(content, file_type)
# 使用LLM进行更深入的分析
analysis_prompt = f"""
请分析以下文档结构,并提供优化建议:
文档类型:{file_type}
标题数量:{len(document_structure.get('titles', []))}
段落数量:{len(document_structure.get('paragraphs', []))}
图片数量:{len(document_structure.get('images', []))}
文档内容摘要:
{content[:500]}...
请分析文档的:
1. 结构层次是否清晰
2. 内容组织是否合理
3. 建议的排版改进点
返回JSON格式的分析结果。
"""
llm_analysis = analyze_with_llm(content[:1000], "分析文档结构和排版建议")
# 合并结构信息和LLM分析
document_structure["llm_analysis"] = llm_analysis
return document_structure
def post(self, shared, prep_res, exec_res):
"""保存文档分析结果"""
shared["document_structure"] = exec_res
titles_count = len(exec_res.get("titles", []))
images_count = len(exec_res.get("images", []))
logger.info(f"文档分析完成: {titles_count}个标题, {images_count}张图片")
return "default"
class DesignLayoutNode(Node):
"""排版设计节点"""
def prep(self, shared):
"""准备需求和文档结构数据"""
return {
"requirements": shared.get("requirements", {}),
"document_structure": shared.get("document_structure", {})
}
def exec(self, prep_res):
"""设计具体的排版方案"""
requirements = prep_res["requirements"]
doc_structure = prep_res["document_structure"]
design_prompt = f"""
作为专业的UI/UX设计师,根据以下信息设计文档排版方案:
用户需求:
- 风格:{requirements.get('style', '现代简约')}
- 格式:{requirements.get('format', 'HTML')}
- 特殊要求:{requirements.get('special_requirements', '无')}
文档结构:
- 标题层级:{len(doc_structure.get('titles', []))}个标题
- 图片数量:{len(doc_structure.get('images', []))}张图片
- 段落数量:{len(doc_structure.get('paragraphs', []))}个段落
请设计详细的排版方案,包括:
{{
"typography": {{
"primary_font": "主要字体",
"heading_sizes": {{"h1": "2.5em", "h2": "2em", "h3": "1.5em"}},
"line_height": "行高",
"letter_spacing": "字间距"
}},
"colors": {{
"primary": "主色调",
"secondary": "辅助色",
"text": "文字颜色",
"background": "背景色"
}},
"spacing": {{
"section_margin": "段落间距",
"paragraph_margin": "段落内间距",
"title_margin": "标题间距"
}},
"image_design": {{
"max_width": "图片最大宽度",
"border_radius": "圆角大小",
"shadow": "阴影效果",
"border": "边框样式"
}}
}}
请返回JSON格式的完整设计方案。
"""
result = call_llm(design_prompt)
try:
if "```json" in result:
json_str = result.split("```json")[1].split("```")[0].strip()
else:
json_str = result
layout_design = json.loads(json_str)
return layout_design
except json.JSONDecodeError:
# 返回默认设计方案
logger.warning("无法解析设计方案,使用默认设计")
return {
"typography": {
"primary_font": "'Segoe UI', sans-serif",
"heading_sizes": {"h1": "2.5em", "h2": "2em", "h3": "1.5em"},
"line_height": "1.6",
"letter_spacing": "normal"
},
"colors": {
"primary": "#2196F3",
"secondary": "#FFC107",
"text": "#333333",
"background": "#FFFFFF"
},
"spacing": {
"section_margin": "2em",
"paragraph_margin": "1em",
"title_margin": "1.5em"
},
"image_design": {
"max_width": "100%",
"border_radius": "8px",
"shadow": "0 4px 8px rgba(0,0,0,0.1)",
"border": "none"
}
}
def post(self, shared, prep_res, exec_res):
"""保存设计方案"""
shared["layout_design"] = exec_res
primary_color = exec_res.get("colors", {}).get("primary", "Unknown")
logger.info(f"排版设计完成: 主色调 {primary_color}")
return "default"
class ProcessTextNode(Node):
"""文本处理节点"""
def prep(self, shared):
"""准备文档内容和设计方案"""
return {
"original_content": shared.get("original_document", ""),
"layout_design": shared.get("layout_design", {}),
"requirements": shared.get("requirements", {})
}
def exec(self, prep_res):
"""处理文本格式"""
content = prep_res["original_content"]
layout_design = prep_res["layout_design"]
if not content:
return {"error": "没有文档内容可处理"}
# 应用基本样式
styles = {
"title_style": {
"color": layout_design.get("colors", {}).get("primary", "#2196F3"),
"prefix": "",
"suffix": ""
},
"paragraph_style": layout_design.get("spacing", {})
}
processed_content = apply_styles(content, styles)
# 使用LLM优化文本结构
optimization_prompt = f"""
请优化以下文档的文本结构和格式:
原始内容:
{content}
优化要求:
1. 确保标题层次清晰
2. 段落结构合理
3. 添加必要的分隔和强调
4. 保持原始内容的完整性
请返回优化后的Markdown格式文档。
"""
optimized_content = call_llm(optimization_prompt)
return {
"processed_content": processed_content,
"optimized_content": optimized_content,
"applied_styles": styles
}
def post(self, shared, prep_res, exec_res):
"""保存处理后的文本"""
shared["processed_text"] = exec_res
logger.info("文本处理完成")
return "default"
class UnifyImagesNode(Node):
"""图片统一处理节点"""
def prep(self, shared):
"""准备图片信息和设计方案"""
return {
"document_structure": shared.get("document_structure", {}),
"layout_design": shared.get("layout_design", {}),
"requirements": shared.get("requirements", {})
}
def exec(self, prep_res):
"""统一处理图片"""
doc_structure = prep_res["document_structure"]
layout_design = prep_res["layout_design"]
images = doc_structure.get("images", [])
if not images:
return {"message": "没有发现图片,跳过图片处理"}
# 构建图片处理效果配置
image_design = layout_design.get("image_design", {})
effects = {
"resize": {
"size": (800, 600),
"maintain_aspect": True
},
"enhance": {
"brightness": 1.0,
"contrast": 1.1,
"saturation": 1.0
}
}
# 添加边框和圆角
if image_design.get("border_radius"):
effects["rounded_corners"] = {
"radius": int(image_design["border_radius"].replace("px", ""))
}
if image_design.get("shadow"):
effects["shadow"] = {
"offset": (5, 5),
"blur_radius": 8,
"color": "#00000020"
}
# 模拟图片处理(实际应用中需要真实的图片文件)
processed_images = []
for img in images:
processed_img = {
"original_url": img["url"],
"alt_text": img["alt_text"],
"effects_applied": effects,
"new_url": f"processed_{img['url']}",
"section": img.get("section", "")
}
processed_images.append(processed_img)
return {
"processed_images": processed_images,
"effects_config": effects,
"total_processed": len(processed_images)
}
def post(self, shared, prep_res, exec_res):
"""保存图片处理结果"""
shared["processed_images"] = exec_res
count = exec_res.get("total_processed", 0)
logger.info(f"图片处理完成: {count}张图片已统一")
return "default"
class GenerateDocumentNode(Node):
"""文档生成节点"""
def prep(self, shared):
"""准备所有处理结果"""
return {
"processed_text": shared.get("processed_text", {}),
"processed_images": shared.get("processed_images", {}),
"layout_design": shared.get("layout_design", {}),
"requirements": shared.get("requirements", {})
}
def exec(self, prep_res):
"""生成最终文档"""
processed_text = prep_res["processed_text"]
layout_design = prep_res["layout_design"]
requirements = prep_res["requirements"]
# 获取优化后的内容
content = processed_text.get("optimized_content", processed_text.get("processed_content", ""))
if not content:
return {"error": "没有可用的文档内容"}
# 根据要求的格式生成文档
output_format = requirements.get("format", "HTML").upper()
if output_format == "HTML":
# 生成HTML文档
styles = {
"title_style": layout_design.get("colors", {}),
"image_style": layout_design.get("image_design", {})
}
html_content = generate_html_from_markdown(content, styles)
return {
"format": "HTML",
"content": html_content,
"styles_applied": styles
}
elif output_format == "MARKDOWN":
# 返回优化的Markdown
return {
"format": "MARKDOWN",
"content": content,
"styles_applied": layout_design
}
else:
# 其他格式的基本支持
return {
"format": output_format,
"content": content,
"note": f"基础{output_format}格式输出"
}
def post(self, shared, prep_res, exec_res):
"""保存最终文档"""
shared["final_document"] = exec_res
doc_format = exec_res.get("format", "Unknown")
content_length = len(exec_res.get("content", ""))
logger.info(f"文档生成完成: {doc_format}格式, {content_length}字符")
# 保存到文件
self._save_document(exec_res)
return "default"
def _save_document(self, document):
"""保存文档到文件"""
try:
format_type = document.get("format", "HTML").lower()
content = document.get("content", "")
# 确保输出目录存在
os.makedirs("output", exist_ok=True)
if format_type == "html":
filename = "output/formatted_document.html"
elif format_type == "markdown":
filename = "output/formatted_document.md"
else:
filename = f"output/formatted_document.{format_type.lower()}"
with open(filename, "w", encoding="utf-8") as f:
f.write(content)
logger.info(f"文档已保存到: {filename}")
except Exception as e:
logger.error(f"保存文档失败: {e}")
# 错误处理节点
class ErrorHandlingNode(Node):
"""错误处理节点"""
def prep(self, shared):
"""准备错误信息"""
return shared.get("error", "")
def exec(self, prep_res):
"""处理错误"""
if prep_res:
logger.error(f"处理过程中发生错误: {prep_res}")
return {"handled": True, "error": prep_res}
return {"handled": False}
def post(self, shared, prep_res, exec_res):
"""记录错误处理结果"""
shared["error_handled"] = exec_res
return "default"