PocketFlow Documentation

repository·main·Indexed 27 days ago

https://github.com/the-pocket/pocketflow

A minimalist, 100-line LLM framework for building Agents, Workflows, and RAG applications. It provides a lightweight, dependency-free alternative to larger frameworks, featuring support for A2A protocol integration, asynchronous operations via AsyncNode, batch processing with BatchFlow and BatchNode, and graph-based architectures for research agents and agentic RAG.

Tokens
73.1K
Snippets
203
Records
388
Agent score
95%

What's inside PocketFlow

  1. Overview of pocketflow-tool-crawler features and limitations

    main

    Features

    • Domain-bounded crawling: Respects domain boundaries during crawls.
    • Content Extraction: Extracts text content and links.
    • LLM Analysis: Uses GPT-4 to generate page summaries, main topics/keywords, and content type classifications.
    • Batch Processing: Processes pages in batches for efficiency.
    • Reporting: Generates a comprehensive analysis report.

    Limitations

    • Domain Restriction: Only crawls within the same domain.
    • Media Support: Text content only (no images or media).
    • API Constraints: Subject to OpenAI API rate limits.
    • Error Handling: Implements basic error handling.
  2. Overview of Coding Agent Tools and Features

    main

    The PocketFlow Coding Agent uses a tool-based loop to interact with a codebase. It includes the following capabilities:

    Available Tools

    • list_files: Lists files in the directory.
    • grep_search: Searches for patterns within files.
    • read_file: Reads the content of a specific file.
    • patch_file: Implements code changes using a Flow-IS-Node SubFlow (Read -> Validate -> Apply) with fuzzy matching for error recovery.
    • run_command: Executes shell commands (e.g., running tests).
    • done: Signals the completion of the task.

    Key Features

    • Memory: Persists learnings from previous sessions into a .memory.md file.
    • Skills: Automatically loads project-specific rules from an AGENTS.md file if it exists in the project.
    • History Compaction: Automatically summarizes long conversation histories to prevent context window overflow.
    • Routing Architecture: Uses a clean architecture with one dedicated node per tool.
  3. Overview of PocketFlow

    main

    PocketFlow is a minimalist LLM framework consisting of approximately 100 lines of code. It is designed to be lightweight, zero-dependency, and vendor-agnostic, focusing on the core abstraction of a Graph to implement various LLM design patterns.

    Key features include:

    • Lightweight: Minimal footprint with no bloat.
    • Expressive: Supports Multi-Agent systems, Workflows, RAG, and more.
    • Multi-language support: While originally Python, versions are available in TypeScript, Java, C++, and Go.
  4. Overview of PocketFlow FastAPI WebSocket Chat features

    main

    The PocketFlow FastAPI WebSocket Chat implementation provides a real-time chat interface with the following capabilities:

    • Real-time Streaming: AI responses are streamed to the UI as they are generated by the LLM.
    • Conversation Memory: The system maintains chat history across multiple messages.
    • WebSocket Connection: Uses persistent WebSocket connections for low-latency, instant communication.
    • PocketFlow Integration: Leverages PocketFlow AsyncNode and AsyncFlow to handle streaming logic.
  5. Overview of the Podcast Generation Pipeline

    main

    The podcast generator follows a three-step linear pipeline:

    1. AnalyzeDocs: Reads source documents and extracts 2-3 interesting nuggets from each.
    2. WriteScript: Generates a conversational script (approximately 12 lines) between two hosts, Alex and Jamie.
    3. TextToSpeech: Converts the script to audio. It uses specific OpenAI TTS voices: Alex uses the alloy voice and Jamie uses the echo voice. The lines are concatenated into a single MP3 file.
  6. Overview of Pocket Flow

    main

    Pocket Flow is a minimalist LLM framework designed to be lightweight (approx. 100 lines of code) and expressive. It uses Graphs as its core abstraction, allowing you to implement various design patterns such as:

    • Agents: Single or multi-agent systems.
    • Workflows: Structured sequences of tasks.
    • RAG: Retrieval-Augmented Generation processes.

    Unlike larger frameworks (e.g., LangChain, CrewAI), Pocket Flow avoids heavy abstractions, application-specific wrappers, and vendor-specific wrappers, resulting in a significantly smaller footprint.

  7. Understand the Text Converter Flow architecture

    main

    The Text Converter Flow uses an interactive loop with branching paths between two primary nodes:

    1. TextInput Node: Responsible for collecting text input and handling menu choices.
    2. TextTransform Node: Responsible for applying the selected transformation (UPPERCASE, lowercase, Reverse, or Remove extra spaces) to the input text.

    The flow logic follows this pattern:

    • TextInput Node transitions to TextTransform Node via a transform signal.
    • TextTransform Node can transition back to TextInput Node via an input signal to process more text, or to End via an exit signal.
    • TextInput Node can also transition directly to End via an exit signal.
  8. Understand the AI Newsletter pipeline architecture

    main

    The pipeline is a linear flow consisting of four core nodes defined in nodes.py and wired together in flow.py:

    1. CurateSources: Searches the web for specified topics (e.g., AI agents, LLM benchmarks) and collects raw results.
    2. FilterStories: Uses an LLM to score and select the 4 most interesting stories based on novelty, practitioner impact, and concrete details.
    3. SummarizeStories: Generates a punchy 2-3 sentence blurb for each selected story.
    4. FormatNewsletter: Assembles the selected stories into a polished markdown digest.

    File Structure Reference:

    • main.py: Entry point; accepts optional extra topics.
    • flow.py: Defines the linear pipeline wiring.
    • nodes.py: Contains the implementation of the four core nodes.
    • utils.py: Contains helper functions for LLM calls and web searches.
  9. Understand the Lead Generation Pipeline workflow

    main

    The Lead Generation Pipeline is an automated sales workflow consisting of four sequential stages:

    1. ScrapeLeads: Loads sample lead data including name, title, and company.
    2. EnrichLeads: Adds company intelligence such as funding stage, tech stack, and team size.
    3. ScoreLeads: Uses an LLM to rate each lead on a scale of 1-10 based on product fit (considering need, seniority, and technical role).
    4. PersonalizeEmails: Generates tailored 3-sentence cold emails for leads with a score of 6 or higher.
  10. Understand the ChainOfThoughtNode orchestration

    main

    The implementation utilizes a self-looping ChainOfThoughtNode to manage structured reasoning. Instead of a single prompt, the node orchestrates a loop where the LLM performs the following in each step:

    1. Evaluate: Assess the reasoning and results of the previous thought.
    2. Execute: Perform the next pending step according to a maintained plan.
    3. Update: Mark steps as done (with results) or note issues in the plan.
    4. Refine: Adjust the plan if steps need further breakdown or error correction.
    5. Decide: Determine if further thinking (next_thought_needed) is required based on the current plan state.

    This external orchestration allows standard LLMs to perform deep reasoning by managing planning and evaluation outside of a single prompt context.

  11. Understand the PocketFlow Chat with Memory architecture

    main

    The chat application implements a memory retrieval system using four specialized nodes in a flow:

    • GetUserQuestionNode: Handles interactive user input.
    • RetrieveNode: Finds relevant past conversations using vector similarity.
    • AnswerNode: Generates responses using both the 3 most recent conversation pairs (sliding window) and the retrieved context (1 pair).
    • EmbedNode: Archives older conversations by generating embeddings for vector storage.

    Flow Logic:

    1. GetUserQuestionNode triggers RetrieveNode to find context.
    2. RetrieveNode provides context to AnswerNode to generate a response.
    3. AnswerNode sends the question to EmbedNode to archive the interaction.
    4. EmbedNode returns to GetUserQuestionNode to await the next input.
  12. Understand the Agentic RAG decision loop

    main

    The Agentic RAG system uses an iterative loop where an LLM decides which documents to read based on document summaries rather than simple similarity scores. The loop consists of three main stages:

    1. DecideAction: The LLM examines the question, available document summaries, and current context. It outputs a YAML decision: either read (specifying a document name) or answer.
    2. ReadDoc: The system retrieves the content of the chosen document, appends it to the accumulated context, and returns to the DecideAction stage.
    3. Answer: When the agent determines it has sufficient context, it generates a final answer using all gathered information.