add map reduce tutorial

This commit is contained in:
zachary62
2025-03-22 12:44:33 -04:00
parent 7411a9127b
commit eb1c721e00
15 changed files with 420 additions and 52 deletions
+29 -33
View File
@@ -5,7 +5,6 @@ A PocketFlow example that demonstrates an article writing workflow using a seque
## Features
- Generate a simple outline with up to 3 main sections using YAML structured output
- Process each section independently using batch processing
- Write concise (100 words max) content for each section in simple terms
- Apply a conversational, engaging style to the final article
@@ -41,14 +40,14 @@ The workflow consists of three sequential nodes:
```mermaid
graph LR
Outline[Generate Outline] --> Write[Batch Write Content]
Outline[Generate Outline] --> Write[Write Content]
Write --> Style[Apply Style]
```
Here's what each node does:
1. **Generate Outline**: Creates a simple outline with up to 3 main sections using YAML structured output
2. **Write Simple Content**: Processes each section in parallel (as a BatchNode), writing a concise 100-word explanation for each
2. **Write Simple Content**: Writes a concise 100-word explanation for each section
3. **Apply Style**: Rewrites the combined content in a conversational, engaging style
## Files
@@ -68,44 +67,51 @@ Here's what each node does:
===== OUTLINE (YAML) =====
sections:
- Understanding AI Safety
- Challenges in Ensuring AI Safety
- Strategies for Mitigating AI Risks
- Introduction to AI Safety
- Key Challenges in AI Safety
- Strategies for Ensuring AI Safety
===== PARSED OUTLINE =====
1. Understanding AI Safety
2. Challenges in Ensuring AI Safety
3. Strategies for Mitigating AI Risks
1. Introduction to AI Safety
2. Key Challenges in AI Safety
3. Strategies for Ensuring AI Safety
=========================
Parsed 3 sections: ['Understanding AI Safety', 'Challenges in Ensuring AI Safety', 'Strategies for Mitigating AI Risks']
===== SECTION CONTENTS =====
--- Understanding AI Safety ---
Understanding AI safety is about ensuring that artificial intelligence systems work safely and as intended. Just like you wouldn't want a car to suddenly speed up on its own, we want AI to be predictable and reliable. For example, if an AI were to help cook, we need to make sure it identifies ingredients correctly and doesn't start a fire. By focusing on AI safety, we aim to prevent accidents and ensure these systems help rather than harm us.
--- Introduction to AI Safety ---
AI Safety is about making sure that artificial intelligence (AI) systems are helpful and not harmful. Imagine teaching a robot to help with chores. AI Safety is like setting ground rules for the robot so it doesn't accidentally cause trouble, like mistaking a pet for a toy. By ensuring AI systems understand their tasks and limitations, we can trust them to act safely. It's about creating guidelines and checks to ensure AI assists us without unintended consequences.
--- Challenges in Ensuring AI Safety ---
Making sure AI is safe involves several challenges. Imagine teaching a robot to understand commands correctly; if it misinterprets instructions, things could go wrong. It's like teaching a toddler to cross the street safely—they need to understand when and where it's safe to walk. Similarly, AI must be programmed to make safe decisions. Ensuring AI doesn't act unpredictably and behaves as intended, even in new situations, is crucial. Balancing innovation and safety is key, just like making sure a car is fast but also has reliable brakes to prevent accidents.
--- Key Challenges in AI Safety ---
AI safety is about ensuring that artificial intelligence systems operate in ways that are beneficial and not harmful. One key challenge is making sure AI makes decisions that align with human values. Imagine teaching a robot to fetch coffee, but it ends up knocking things over because it doesn't understand the mess it creates. Similarly, if AI systems don't fully grasp human intentions, they might act in unexpected ways. The task is to make AI smart enough to achieve goals without causing problems, much like training a puppy to follow rules without chewing on your shoes.
--- Strategies for Mitigating AI Risks ---
Mitigating AI risks is about making sure AI technologies help us without causing harm. It's like having seat belts in cars: they allow us to drive safely by minimizing dangers. To manage AI risks, we can use guidelines and rules to ensure AI behaves as expected. Training AI with diverse data is crucial so it doesn't develop biases, much like teaching children to respect different cultures. Additionally, we can create "off switches" for AI systems, similar to remote controls, to turn them off if they start acting unexpectedly. These steps help us safely enjoy the benefits AI offers.
--- Strategies for Ensuring AI Safety ---
Ensuring AI safety is about making sure artificial intelligence behaves as expected and doesnt cause harm. Imagine AI as a new driver on the road; we need rules and safeguards to prevent accidents. By testing AI systems under different conditions, setting clear rules for their behavior, and keeping human oversight, we can manage risks. For instance, just as cars have brakes to ensure safety, AI systems need to have fail-safes. This helps in building trust and avoiding unexpected issues, keeping both humans and AI on the right track.
===========================
===== FINAL ARTICLE =====
Hey there! Have you ever wondered about the safety of artificial intelligence and how it fits into our world? It's a bit like making sure a pet behaves itself—you want your dog to fetch the ball, not run off with your slippers! At its heart, understanding AI safety means ensuring these high-tech systems do what they're supposed to without causing a ruckus. Just as you wouldn't want your car to suddenly speed up without warning, we hope for AI to be as reliable as your morning coffee brewing on schedule. Imagine an AI assistant in your kitchen—it should know the difference between sugar and salt, and definitely not turn your peaceful cooking session into a fire drill. So, by focusing on AI safety, we're aiming for a world where these systems help us, without creating chaos.
# Welcome to the World of AI Safety
Now, navigating the challenges of AI safety? That's quite the adventure! Picture this: you're trying to teach a robot your way of doing things. It's like teaching a toddler to cross a busy street. The little one needs to know when to stop, when to go, and how to manage all the things happening around them. Similarly, our AI pals need to be programmed to make safe decisions, even if they're seeing the world for the first time through their digital eyes. It's this delicate dance between innovation and safety—like crafting a sports car that's both exhilaratingly fast and equipped with top-notch brakes. We don't want surprises when it comes to AI behavior, right?
Have you ever wondered what it would be like to have your very own robot helping you around the house? Sounds like a dream, right? But lets hit pause for a moment. What if this robot mistook your fluffy cat for a toy? Thats exactly where AI Safety comes in. Think of AI Safety as setting some friendly ground rules for your household helper, ensuring that it knows the difference between doing chores and causing a bit of chaos. Its all about making sure our AI allies play by the rules, making life easier without those pesky accidental hiccups.
So, how do we juggle these AI risks and keep things safe? Imagine AI guidelines and protocols like the seat belts in your car—designed to keep you secure while letting you enjoy the ride. By setting rules, we ensure AI behaves as expected, kind of like a teacher maintaining order in a classroom. And just like we educate kids to appreciate the diverse world around them, we train AI with a wide array of data to avoid any unfair biases. Plus, isn't it reassuring to know we can install an "off switch" on these systems? Think of it like having a remote control to power down the device if it starts acting up. These strategies are our way of making sure we can relish the wonders of AI, all while knowing we've got everything under control.
# Navigating the Maze of AI Challenges
In a nutshell, AI safety is about bridging the gap between groundbreaking technology and everyday peace of mind. It's this journey of making technology a trustworthy companion rather than a wild card. After all, it's all about enjoying the benefits without the hiccups—who wouldn't want that kind of harmony in their tech-driven life?
Picture this: you've asked your trusty robot to grab you a cup of coffee. But instead, it sends mugs flying and spills coffee because it doesnt quite get the concept of a mess. Frustrating, isnt it? One of the biggest hurdles in AI Safety is aligning AI decisions with our human values and intentions. Its like training a puppy not to gnaw on your favorite pair of shoes. Our job is to teach AI how to reach its goals without stepping on our toes, all while being as reliable and lovable as a well-trained pup.
# Steering AI Toward Safe Horizons
Now, how do we keep our AI friends on the straight and narrow? Imagine AI as a new driver learning to navigate the roads of life. Just like we teach new drivers the rules of the road and equip cars with brakes for safety, we provide AI with guidelines and fail-safes to prevent any unintended mishaps. Testing AI systems in various scenarios and keeping a watchful human eye on them ensures they dont veer off track. Its all about building trust and creating a partnership where both humans and AI are cruising smoothly together.
# Wrapping It Up
At the end of the day, AI Safety is about creating a harmonious relationship between humans and machines, where we trust our metal companions to support us without the fear of unexpected surprises. By setting boundaries and ensuring understanding, were not just building smarter machines—were crafting a future where AI and humanity can thrive together. So, next time youre imagining that helpful robot assistant, rest easy knowing that AI Safety is making sure it's ready to lend a hand without dropping the ball—or your coffee mug!
========================
@@ -113,17 +119,7 @@ In a nutshell, AI safety is about bridging the gap between groundbreaking techno
=== Workflow Completed ===
Topic: AI Safety
Outline Length: 100 characters
Draft Length: 1707 characters
Final Article Length: 2531 characters
Outline Length: 96 characters
Draft Length: 1690 characters
Final Article Length: 2266 characters
```
## Extending the Example
You can easily extend this example by:
1. Adding more processing nodes to the workflow
2. Modifying the prompts in the node classes
3. Implementing branching logic based on the content generated
4. Adding user interaction between workflow steps
5. Using different structured output formats (JSON, XML, etc.)
+17 -18
View File
@@ -43,17 +43,19 @@ sections:
print(formatted_outline)
print("\n=========================\n")
print(f"Parsed {len(sections)} sections: {sections}")
return "default"
class WriteSimpleContent(BatchNode):
class WriteSimpleContent(Node):
def prep(self, shared):
# Return the list of sections to process
# Get the list of sections to process
return shared.get("sections", [])
def exec(self, section):
prompt = f"""
def exec(self, sections):
all_sections_content = []
section_contents = {}
for section in sections:
prompt = f"""
Write a short paragraph (MAXIMUM 100 WORDS) about this section:
{section}
@@ -64,21 +66,18 @@ Requirements:
- Keep it very concise (no more than 100 words)
- Include one brief example or analogy
"""
return section, call_llm(prompt)
def post(self, shared, prep_res, exec_res_list):
# Create a dictionary of section: content
section_contents = {}
all_content = []
for section, content in exec_res_list:
content = call_llm(prompt)
section_contents[section] = content
all_content.append(f"## {section}\n\n{content}\n")
all_sections_content.append(f"## {section}\n\n{content}\n")
return sections, section_contents, "\n".join(all_sections_content)
def post(self, shared, prep_res, exec_res):
sections, section_contents, draft = exec_res
# Store the section contents and draft
shared["section_contents"] = section_contents
# Combine all content into a single draft
shared["draft"] = "\n".join(all_content)
shared["draft"] = draft
print("\n===== SECTION CONTENTS =====\n")
for section, content in section_contents.items():
@@ -1,3 +1,3 @@
pocketflow>=0.1.0
pocketflow>=0.0.1
openai>=1.0.0
pyyaml>=6.0