pocketflow/cookbook/pocketflow-fastapi-background
zachary62 aaf69731ee update fastapi 2025-05-26 18:43:21 -04:00
..
docs update fastapi 2025-05-26 18:43:21 -04:00
static update fastapi 2025-05-26 18:43:21 -04:00
utils update fastapi 2025-05-26 18:43:21 -04:00
README.md update fastapi 2025-05-26 18:43:21 -04:00
flow.py update fastapi 2025-05-26 18:43:21 -04:00
main.py update fastapi 2025-05-26 18:43:21 -04:00
nodes.py update fastapi 2025-05-26 18:43:21 -04:00
requirements.txt update fastapi 2025-05-26 18:43:21 -04:00

README.md

PocketFlow FastAPI Background Job

A minimal example of running PocketFlow workflows as background jobs with real-time progress updates via Server-Sent Events (SSE).

Features

  • Start article generation jobs via REST API
  • Real-time granular progress updates via SSE (shows progress for each section)
  • Background processing with FastAPI
  • Simple three-step workflow: Outline → Content → Style
  • Web interface for easy job submission and monitoring

Getting Started

  1. Install dependencies:
pip install -r requirements.txt
  1. Set your OpenAI API key:
export OPENAI_API_KEY=your_api_key_here
  1. Run the server:
python main.py

Usage

  1. Open your browser and go to http://localhost:8000
  2. Enter an article topic (e.g., "AI Safety", "Climate Change")
  3. Click "Generate Article"
  4. You'll be redirected to a progress page showing real-time updates
  5. The final article will appear when generation is complete

API Usage

Start a Job

curl -X POST "http://localhost:8000/start-job" -d "topic=AI Safety" -H "Content-Type: application/x-www-form-urlencoded"

Response:

{"job_id": "123e4567-e89b-12d3-a456-426614174000", "topic": "AI Safety", "status": "started"}

Monitor Progress

curl "http://localhost:8000/progress/123e4567-e89b-12d3-a456-426614174000"

SSE Stream:

data: {"step": "outline", "progress": 33, "data": {"sections": ["Introduction", "Challenges", "Solutions"]}}
data: {"step": "content", "progress": 44, "data": {"section": "Introduction", "completed_sections": 1, "total_sections": 3}}
data: {"step": "content", "progress": 55, "data": {"section": "Challenges", "completed_sections": 2, "total_sections": 3}}
data: {"step": "content", "progress": 66, "data": {"section": "Solutions", "completed_sections": 3, "total_sections": 3}}
data: {"step": "content", "progress": 66, "data": {"draft_length": 1234, "status": "complete"}}
data: {"step": "complete", "progress": 100, "data": {"final_article": "..."}}

Files

  • main.py - FastAPI app with background jobs and SSE
  • flow.py - PocketFlow workflow definition
  • nodes.py - Workflow nodes (Outline, Content, Style)
  • utils/call_llm.py - LLM utility function
  • static/index.html - Main page for starting jobs
  • static/progress.html - Progress monitoring page with real-time updates