src/pocketflow

Search:
Group by:

PocketFlow - A minimalist flow-based agent framework for Nim

PocketFlow provides a flexible and powerful framework for building LLM-powered workflows, RAG systems, and agent pipelines.

Core Features:

  • Node-based workflow orchestration
  • Batch and parallel processing
  • Retry logic and fallback handling
  • Multiple LLM provider support (OpenAI, Anthropic, Google, Ollama)
  • Streaming responses
  • Caching and cost tracking
  • RAG capabilities (chunking, embeddings, retrieval)
  • Advanced node types (conditional, loop, timeout)
  • Observability (logging, metrics, tracing)
  • State persistence and recovery
  • Performance benchmarking

Quick Example:

import pocketflow

let ctx = newPfContext()
let llm = newLlmClient(provider = OpenAI, apiKey = "sk-...")

let node = newNode(
  exec = proc(ctx: PfContext, params: JsonNode, prepRes: JsonNode): Future[JsonNode] {.async.} =
    let response = await llm.generate("Tell me a joke")
    return %response
)

let flow = newFlow(node)
waitFor flow.internalRun(ctx)

Exports

newPfContext, PfContext, hasKey, []=, [], DefaultAction, next, BatchPostCallback, FallbackCallback, initBaseNode, ExecCallback, internalRun, addSuccessor, getNextNode, internalRun, newParallelBatchNode, internalRun, next, setParams, ParallelBatchNode, Node, BatchPrepCallback, -, run, ActionLink, BatchItemFallbackCallback, PostCallback, >>, BatchExecItemCallback, internalRun, newNode, BaseNode, PrepCallback, >>, newBatchNode, BatchNode, internalRun, ParallelBatchFlow, setPrepBatch, newFlow, internalRun, newParallelBatchFlow, setPostBatch, Flow, newBatchFlow, setPrepBatch, BatchFlow, setPostBatch, internalRun, LlmClient, newLlmClient, LlmProvider, LlmOptions, close, embeddings, chat, StreamCallback, generate, chatWithOptions, NodeExecutionError, newPersistenceError, newRAGError, LLMError, newLLMError, FlowExecutionError, TimeoutError, newTimeoutError, PocketFlowError, RateLimitError, newNodeExecutionError, newPocketFlowError, ValidationError, CacheError, newCacheError, newRateLimitError, evictExpired, getCached, has, size, setCached, get, CacheEntry, set, newCache, globalCache, clear, computeKey, Cache, newCostTracker, estimateTokens, CostTracker, TokenCounter, globalCostTracker, reset, getSummary, trackUsage, LogLevel, Metric, globalObserver, Observer, getMetricsSummary, getDurationMs, withSpan, recordMetric, newSpan, finish, Span, logStructured, chunkByParagraphs, cosineSimilarity, chunkDocument, rerankChunks, Chunk, ChunkingStrategy, ChunkingOptions, newChunkingOptions, findTopK, chunkByFixedSize, chunkBySentences, internalRun, newMapNode, internalRun, LoopNode, TimeoutNode, internalRun, ConditionalNode, internalRun, newLoopNode, newConditionalNode, newTimeoutNode, MapNode, loadState, saveState, FlowState, listStates, globalStateStore, deleteState, newStateStore, restoreContext, StateStore, captureState, compare, benchmarkNode, toJson, benchmark, BenchmarkSuite, newBenchmarkSuite, BenchmarkResult, printSummary, benchmarkFlow