PocketFlow 快速开始指南

5分钟快速上手

1. 安装和设置

# 克隆项目
git clone https://github.com/the-pocket/PocketFlow.git
cd PocketFlow

# 安装依赖
pip install -r requirements.txt

# 设置环境变量
export OPENAI_API_KEY="your-api-key-here"

2. 第一个应用:智能文本摘要

from pocketflow import Node, Flow

def call_llm(prompt):
    from openai import OpenAI
    client = OpenAI()
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

class SummarizeNode(Node):
    def prep(self, shared):
        return shared["text"]
    
    def exec(self, text):
        return call_llm(f"请用一句话总结:{text}")
    
    def post(self, shared, prep_res, exec_res):
        shared["summary"] = exec_res

# 使用
shared = {"text": "今天是个阳光明媚的好天气,我们去公园散步..."}
node = SummarizeNode()
flow = Flow(start=node)
flow.run(shared)
print(shared["summary"])

3. 架构图

flowchart LR A[输入文本] --> B[SummarizeNode] B --> C[输出摘要] classDef nodeStyle fill:#e3f2fd,stroke:#1976d2,stroke-width:2px class B nodeStyle

4. 扩展:添加条件分支

class AnalyzeNode(Node):
    def prep(self, shared):
        return shared["text"]
    
    def exec(self, text):
        result = call_llm(f"这段文本是正面还是负面情感?只回答:positive/negative")
        return result.strip().lower()
    
    def post(self, shared, prep_res, exec_res):
        shared["sentiment"] = exec_res
        return exec_res  # 返回作为action

class PositiveResponseNode(Node):
    def exec(self, _):
        return "很棒的内容!👍"
    
    def post(self, shared, prep_res, exec_res):
        shared["response"] = exec_res

class NegativeResponseNode(Node):
    def exec(self, _):
        return "需要改进的地方 🤔"
    
    def post(self, shared, prep_res, exec_res):
        shared["response"] = exec_res

# 连接节点
analyze = AnalyzeNode()
positive = PositiveResponseNode()
negative = NegativeResponseNode()

analyze - "positive" >> positive
analyze - "negative" >> negative

flow = Flow(start=analyze)

5. 架构图(分支版本)

flowchart TD A[输入文本] --> B[AnalyzeNode] B -->|positive| C[PositiveResponseNode] B -->|negative| D[NegativeResponseNode] C --> E[正面回应] D --> F[负面回应] classDef nodeStyle fill:#e3f2fd,stroke:#1976d2,stroke-width:2px classDef branchStyle fill:#fff3e0,stroke:#ef6c00,stroke-width:2px class B branchStyle class C,D nodeStyle

下一步

常见问题

Q: 如何处理API调用失败?

class RobustNode(Node):
    def __init__(self):
        super().__init__(max_retries=3, wait=5)  # 重试3次,等待5秒
    
    def exec_fallback(self, prep_res, exc):
        return "API调用失败,返回默认结果"

Q: 如何批量处理数据?

from pocketflow import BatchNode

class BatchProcessNode(BatchNode):
    def prep(self, shared):
        return shared["data_list"]  # 返回列表
    
    def exec(self, item):
        return call_llm(f"处理:{item}")  # 处理单个项目
    
    def post(self, shared, prep_res, exec_res_list):
        shared["results"] = exec_res_list  # 获得结果列表

Q: 如何调试Flow?

# 单独测试节点
node = MyNode()
result = node.run(shared)
print(f"节点返回的action: {result}")

# 检查shared store的变化
print(f"共享数据: {shared}")
posted @ 2025-07-02 14:19  ffl  阅读(150)  评论(0)    收藏  举报