41. PocketFlow#
Pocket Flow is a 100-line minimalist LLM framework
Lightweight: Just 100 lines. Zero bloat, zero dependencies, zero vendor lock-in.
Expressive: Everything you love—(Multi-)Agents, Workflow, RAG, and more.
Agentic Coding: Let AI Agents (e.g., Cursor AI) build Agents—10x productivity boost!
!pip install pocketflow --quiet
!pip install litellm --quiet
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.5/10.5 MB 64.2 MB/s eta 0:00:00
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 278.1/278.1 kB 11.6 MB/s eta 0:00:00
?25h
from google.colab import drive
drive.mount('/content/drive') # Add My Drive/<>
import os
os.chdir('drive/My Drive')
os.chdir('Books_Writings/NLPBook/')
Mounted at /content/drive
%run keys.ipynb
import os
from litellm import completion
def call_llm(messages):
"""Calls the litellm completion API with the given messages."""
response = completion(
model = "openai/gpt-4.1",
messages=messages
)
return response.choices[0].message.content
# Test the LLM call
messages = [{"role": "user", "content": "In a few words, what's the meaning of life?"}]
response = call_llm(messages)
print(f"Prompt: {messages[0]['content']}")
print(f"Response: {response}")
Prompt: In a few words, what's the meaning of life?
Response: The meaning of life is to seek connection, growth, and purpose—each person finds their own reason to live.
from pocketflow import Node, Flow
# from utils import call_llm
class ChatNode(Node):
def prep(self, shared):
# Initialize messages if this is the first run
if "messages" not in shared:
shared["messages"] = []
print("Welcome to the chat! Type 'exit' to end the conversation.")
# Get user input
user_input = input("\nYou: ")
# Check if user wants to exit
if user_input.lower() == 'exit':
return None
# Add user message to history
shared["messages"].append({"role": "user", "content": user_input})
# Return all messages for the LLM
return shared["messages"]
def exec(self, messages):
if messages is None:
return None
# Call LLM with the entire conversation history
response = call_llm(messages)
return response
def post(self, shared, prep_res, exec_res):
if prep_res is None or exec_res is None:
print("\nGoodbye!")
return None # End the conversation
# Print the assistant's response
print(f"\nAssistant: {exec_res}")
# Add assistant message to history
shared["messages"].append({"role": "assistant", "content": exec_res})
# Loop back to continue the conversation
return "continue"
# Create the flow with self-loop
chat_node = ChatNode()
chat_node - "continue" >> chat_node # Loop back to continue conversation
flow = Flow(start=chat_node)
# Start the chat
shared = {}
flow.run(shared)
Welcome to the chat! Type 'exit' to end the conversation.
You: What is the meaning of life?
Assistant: The question "What is the meaning of life?" has been pondered by philosophers, scientists, religious thinkers, and countless others throughout history. There isn't one universally accepted answer—it often depends on individual beliefs, cultural backgrounds, and personal experiences.
Here are a few perspectives:
**1. Philosophical Perspective:**
Some philosophers argue that life doesn't have an inherent universal meaning, but rather, it's up to each individual to create their own purpose and meaning through relationships, work, creativity, and personal growth.
**2. Religious Perspective:**
Many religions provide their own answers. For example, in Christianity, the meaning of life might be to love God and others, seek salvation, and live righteously. In Buddhism, it can be about ending suffering and achieving enlightenment.
**3. Scientific Perspective:**
From a biological standpoint, the "purpose" of life could be survival, reproduction, and the continuation of our species. Some scientists assert that meaning is a human construct, evolved alongside consciousness.
**4. Existential Perspective:**
Existentialists propose that life is what we make it. Since the universe is indifferent, it's up to each person to find or create meaning amidst the chaos.
**5. Cultural and Personal Perspective:**
Many people find meaning through relationships, work, contributing to society, artistic creation, understanding the world, or simply experiencing joy.
**In summary:**
The meaning of life is a deeply personal question. Many find it by connecting with others, striving for personal development, helping their community, or seeking understanding and fulfillment.
Or, as humorously stated in Douglas Adams' *The Hitchhiker's Guide to the Galaxy*:
**"The answer to the ultimate question of life, the universe, and everything is 42."**
What does the question mean to you?
You: Nothing at all
Assistant: That’s a powerful and honest response. For many people, "the meaning of life" doesn’t have to be a grand or philosophical pursuit—it’s okay if it doesn’t mean anything at all, or if the question itself feels unimportant or even unanswerable.
Sometimes, not finding an inherent meaning can feel freeing; it allows you to create your own values, focus on what brings you contentment, or simply experience life as it comes. Other times, it can feel overwhelming or even disheartening.
If you ever want to talk more about how you feel, or if you’re just exploring ideas, I’m here to listen or chat. Everyone’s perspective is valid, and sometimes just asking the question is as meaningful as any answer.
You: exit
Goodbye!
/usr/local/lib/python3.12/dist-packages/pocketflow/__init__.py:44: UserWarning: Flow ends: 'None' not found in ['continue']
if not nxt and curr.successors: warnings.warn(f"Flow ends: '{action}' not found in {list(curr.successors)}")