pocketflow/cookbook/pocketflow-node
Alan ALves 557a14f695 feat: add new examples from pocketflow-academy 2025-03-19 10:31:04 -03:00
..
utils feat: add new examples from pocketflow-academy 2025-03-19 10:31:04 -03:00
README.md feat: add new examples from pocketflow-academy 2025-03-19 10:31:04 -03:00
flow.py feat: add new examples from pocketflow-academy 2025-03-19 10:31:04 -03:00
main.py feat: add new examples from pocketflow-academy 2025-03-19 10:31:04 -03:00
requirements.txt feat: add new examples from pocketflow-academy 2025-03-19 10:31:04 -03:00

README.md

PocketFlow Summarize

A practical example demonstrating how to use PocketFlow to build a robust text summarization tool with error handling and retries. This example showcases core PocketFlow concepts in a real-world application.

Features

  • Text summarization using LLMs (Large Language Models)
  • Automatic retry mechanism (up to 3 attempts) on API failures
  • Graceful error handling with fallback responses
  • Clean separation of concerns using PocketFlow's Node architecture

Project Structure

.
├── docs/          # Documentation files
├── utils/         # Utility functions (LLM API wrapper)
├── flow.py        # PocketFlow implementation with Summarize Node
├── main.py        # Main application entry point
└── README.md      # Project documentation

Implementation Details

The example implements a simple but robust text summarization workflow:

  1. Summarize Node (flow.py):

    • prep(): Retrieves text from the shared store
    • exec(): Calls LLM to summarize text in 10 words
    • exec_fallback(): Provides graceful error handling
    • post(): Stores the summary back in shared store
  2. Flow Structure:

    • Single node flow for demonstration
    • Configured with 3 retries for reliability
    • Uses shared store for data passing

Setup

  1. Create a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Configure your environment:

    • Set up your LLM API key (check utils/call_llm.py for configuration)
  2. Run the example:

python main.py

Example Usage

The example comes with a sample text about PocketFlow, but you can modify main.py to summarize your own text:

shared = {"data": "Your text to summarize here..."}
flow.run(shared)
print("Summary:", shared["summary"])

What You'll Learn

This example demonstrates several key PocketFlow concepts:

  • Node Architecture: How to structure LLM tasks using prep/exec/post pattern
  • Error Handling: Implementing retry mechanisms and fallbacks
  • Shared Store: Using shared storage for data flow between steps
  • Flow Creation: Setting up a basic PocketFlow workflow

Additional Resources