feat: add new examples from pocketflow-academy
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
# PocketFlow Communication Example
|
||||
|
||||
This example demonstrates the [Communication](https://the-pocket.github.io/PocketFlow/communication.html) concept in PocketFlow, specifically focusing on the Shared Store pattern.
|
||||
|
||||
## Overview
|
||||
|
||||
The example implements a simple word counter that shows how nodes can communicate using a shared store. It demonstrates:
|
||||
|
||||
- How to initialize and structure a shared store
|
||||
- How nodes can read from and write to the shared store
|
||||
- How to maintain state across multiple node executions
|
||||
- Best practices for shared store usage
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
pocketflow-communication/
|
||||
├── README.md
|
||||
├── requirements.txt
|
||||
├── main.py
|
||||
├── flow.py
|
||||
└── nodes.py
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```bash
|
||||
python main.py
|
||||
```
|
||||
|
||||
Enter text when prompted. The program will:
|
||||
1. Count words in the text
|
||||
2. Store statistics in the shared store
|
||||
3. Display running statistics (total texts, total words, average)
|
||||
|
||||
Enter 'q' to quit.
|
||||
|
||||
## How it Works
|
||||
|
||||
The example uses three nodes:
|
||||
|
||||
1. `TextInput`: Reads user input and initializes the shared store
|
||||
2. `WordCounter`: Counts words and updates statistics in the shared store
|
||||
3. `ShowStats`: Displays statistics from the shared store
|
||||
|
||||
This demonstrates how nodes can share and maintain state using the shared store pattern.
|
||||
@@ -0,0 +1,21 @@
|
||||
"""Flow configuration for the communication example."""
|
||||
|
||||
from pocketflow import Flow
|
||||
from nodes import TextInput, WordCounter, ShowStats, EndNode
|
||||
|
||||
def create_flow():
|
||||
"""Create and configure the flow with all nodes."""
|
||||
# Create nodes
|
||||
text_input = TextInput()
|
||||
word_counter = WordCounter()
|
||||
show_stats = ShowStats()
|
||||
end_node = EndNode()
|
||||
|
||||
# Configure transitions
|
||||
text_input - "count" >> word_counter
|
||||
word_counter - "show" >> show_stats
|
||||
show_stats - "continue" >> text_input
|
||||
text_input - "exit" >> end_node
|
||||
|
||||
# Create and return flow
|
||||
return Flow(start=text_input)
|
||||
@@ -0,0 +1,10 @@
|
||||
from flow import create_flow
|
||||
|
||||
def main():
|
||||
"""Run the communication example."""
|
||||
flow = create_flow()
|
||||
shared = {}
|
||||
flow.run(shared)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,64 @@
|
||||
"""Node implementations for the communication example."""
|
||||
|
||||
from pocketflow import Node
|
||||
|
||||
class EndNode(Node):
|
||||
"""Node that handles flow termination."""
|
||||
pass
|
||||
|
||||
class TextInput(Node):
|
||||
"""Node that reads text input and initializes the shared store."""
|
||||
|
||||
def prep(self, shared):
|
||||
"""Get user input and ensure shared store is initialized."""
|
||||
return input("Enter text (or 'q' to quit): ")
|
||||
|
||||
def post(self, shared, prep_res, exec_res):
|
||||
"""Store text and initialize/update statistics."""
|
||||
if prep_res == 'q':
|
||||
return "exit"
|
||||
|
||||
# Store the text
|
||||
shared["text"] = prep_res
|
||||
|
||||
# Initialize statistics if they don't exist
|
||||
if "stats" not in shared:
|
||||
shared["stats"] = {
|
||||
"total_texts": 0,
|
||||
"total_words": 0
|
||||
}
|
||||
shared["stats"]["total_texts"] += 1
|
||||
|
||||
return "count"
|
||||
|
||||
class WordCounter(Node):
|
||||
"""Node that counts words in the text."""
|
||||
|
||||
def prep(self, shared):
|
||||
"""Get text from shared store."""
|
||||
return shared["text"]
|
||||
|
||||
def exec(self, text):
|
||||
"""Count words in the text."""
|
||||
return len(text.split())
|
||||
|
||||
def post(self, shared, prep_res, exec_res):
|
||||
"""Update word count statistics."""
|
||||
shared["stats"]["total_words"] += exec_res
|
||||
return "show"
|
||||
|
||||
class ShowStats(Node):
|
||||
"""Node that displays statistics from the shared store."""
|
||||
|
||||
def prep(self, shared):
|
||||
"""Get statistics from shared store."""
|
||||
return shared["stats"]
|
||||
|
||||
def post(self, shared, prep_res, exec_res):
|
||||
"""Display statistics and continue the flow."""
|
||||
stats = prep_res
|
||||
print(f"\nStatistics:")
|
||||
print(f"- Texts processed: {stats['total_texts']}")
|
||||
print(f"- Total words: {stats['total_words']}")
|
||||
print(f"- Average words per text: {stats['total_words'] / stats['total_texts']:.1f}\n")
|
||||
return "continue"
|
||||
@@ -0,0 +1 @@
|
||||
pocketflow==0.1.0
|
||||
Reference in New Issue
Block a user