|
|
||
|---|---|---|
| .. | ||
| README.md | ||
| flow.py | ||
| main.py | ||
| nodes.py | ||
| requirements.txt | ||
| utils.py | ||
README.md
PocketFlow Async Basic Example
This example demonstrates async operations using a simple Recipe Finder that:
- Fetches recipes from an API (async HTTP)
- Processes them with an LLM (async LLM)
- Waits for user confirmation (async input)
What this Example Does
When you run the example:
- You enter an ingredient (e.g., "chicken")
- It searches for recipes (async API call)
- It suggests a recipe (async LLM call)
- You approve or reject the suggestion
- If rejected, it tries again with a different recipe
How it Works
-
FetchRecipes (AsyncNode)
async def prep_async(self, shared): ingredient = input("Enter ingredient: ") return ingredient async def exec_async(self, ingredient): # Async API call recipes = await fetch_recipes(ingredient) return recipes -
SuggestRecipe (AsyncNode)
async def exec_async(self, recipes): # Async LLM call suggestion = await call_llm_async( f"Choose best recipe from: {recipes}" ) return suggestion -
GetApproval (AsyncNode)
async def post_async(self, shared, prep_res, suggestion): # Async user input answer = await get_user_input( f"Accept {suggestion}? (y/n): " ) return "accept" if answer == "y" else "retry"
Running the Example
pip install -r requirements.txt
python main.py
Sample Interaction
Enter ingredient: chicken
Fetching recipes...
Found 3 recipes.
Suggesting best recipe...
How about: Grilled Chicken with Herbs
Accept this recipe? (y/n): n
Suggesting another recipe...
How about: Chicken Stir Fry
Accept this recipe? (y/n): y
Great choice! Here's your recipe...
Key Concepts
-
Async Operations: Using
async/awaitfor:- API calls (non-blocking I/O)
- LLM calls (potentially slow)
- User input (waiting for response)
-
AsyncNode Methods:
prep_async: Setup and data gatheringexec_async: Main async processingpost_async: Post-processing and decisions
-
Flow Control:
- Actions ("accept"/"retry") control flow
- Retry loop for rejected suggestions