DSPy-Go

repository·main·Indexed 41 days ago

https://github.com/xiaoconstantine/dspy-go

A native Go implementation of the DSPy framework for systematic prompt engineering and automated reasoning. It includes the dspy-cli for managing optimizers, analyzing prompts, and managing agent sessions, as well as a compatibility testing framework to ensure parity with the original Python DSPy implementation across optimizers like BootstrapFewShot, MIPRO, SIMBA, COPRO, and GEPA.

Tokens
84.2K
Snippets
231
Records
327
Agent score
49%

What's inside DSPy-Go

  1. Overview of dspy-go features

    main

    Core Capabilities

    • Modular Design: Compose simple, reusable components into complex applications.
    • Multiple LLM Support: Integration with Anthropic, OpenAI, Google Gemini, Ollama, LlamaCPP, and more.
    • Intelligent Agents: ReAct patterns, ACE framework, memory management, and custom tools.
    • A2A Protocol: Multi-agent orchestration with hierarchical composition.
    • RLM Module: Large context exploration through a sandboxed Go REPL.
    • XML Adapters: Structured output parsing with security controls.
    • Tool Chaining: Sequential pipelines with data transformation and conditional logic.
    • Dependency Resolution: Automatic execution planning with parallel optimization.
    • Optimizers: Advanced algorithms like GEPA, MIPRO, SIMBA, BootstrapFewShot, and COPRO.
    • Multimodal Support: Native handling of images, vision Q&A, and multimodal chat.
  2. Command Overview for dspy-cli

    main

    The dspy-cli provides several categories of commands for interacting with the DSPy-Go ecosystem:

    Optimizer Discovery

    • list: List all registered optimizers.
    • describe <optimizer>: Show detailed information for a specific optimizer.
    • recommend: Suggest optimizers based on a --use-case (simple, balanced, advanced, or multi-module) or via --interactive mode.
    • try <optimizer>: Run an optimizer against a built-in sample dataset.

    Prompt and Log Analysis

    • analyze [prompt]: Analyze a prompt's structure. Use --optimize to expand the prompt or --export to save as YAML/JSON.
    • view <file.jsonl>: Inspect RLM or native dspy-go JSONL logs. Supports filters like --watch, --stats, --iter, and --search.

    Session Management

    • session show: Inspect a persisted session.
    • session switch: Change a session's active branch.
    • session fork: Create a new branch from a session head.

    Agents and Benchmarking

    • agent run-terminal-task: Execute a single TBLite-style task from JSON stdin.
    • benchmark tblite: Evaluate or GEPA-optimize the native benchmark agent.

    Utilities

    • interactive: Start the guided terminal UI.
    • completion: Generate shell completion scripts.
  3. Explore DSPy-Go example categories

    main

    The examples/ directory is organized into several functional categories to help you find specific implementation patterns:

    Agents

    Covers ReAct patterns, orchestration, memory, and multi-agent composition (A2A).

    • agents/: ReAct patterns and orchestration.
    • ace_basic/: Minimal ACE usage.
    • ace_react/: ACE layered onto ReAct.
    • native_agent_session/: Tool-calling with persisted sessions.
    • a2a_composition/: Multi-agent orchestration.

    Long-Context and RLM (Replay Language Model)

    Focuses on RLM modules, context policies (full, checkpointed, adaptive), and budget management.

    • rlm/: Basic live RLM run.
    • rlm_context_policy/: Comparing replay policies.
    • rlm_subrlm_budgets/: Deterministic budget demos.
    • rlm_oolong/: Benchmarking on OOLONG tasks.
    • rlm_oolong_gepa/: Optimizing and restoring adaptive RLM agents.

    Modules

    Demonstrates core DSPy-Go building blocks.

    • parallel/: Concurrent batch processing.
    • refine/: Iterative answer improvement.
    • xml_adapter/: XML structured output.
    • multimodal/: Image and text workflows.

    Tools and Integrations

    Focuses on how agents interact with external tools.

    • smart_tool_registry/: Intelligent tool selection.
    • tool_chaining/: Sequential pipelines.
    • tool_composition/: Composite tools.
    • mcp_optimizer/: MCP-backed optimization.

    Research and Miscellaneous

    Experimental or lower-level implementations.

    • others/gepa/, others/mipro/, others/simba/: Lower-level optimization examples.
    • multi_chain_comparison/: Comparing reasoning strategies.
  4. Compare and choose an optimizer

    main

    dspy-go provides several optimizers to automatically improve signatures, instructions, and examples based on datasets and metrics. Choose based on your task complexity and resource constraints:

    OptimizerBest ForSpeedQualityComplexity
    BootstrapFewShotQuick wins, simple tasks⚡⚡⚡⭐⭐⭐Low
    COPROMulti-module systems⚡⚡⭐⭐⭐⭐Medium
    MIPROSystematic optimization⚡⚡⭐⭐⭐⭐Medium
    SIMBAIntrospective learning⚡⭐⭐⭐⭐⭐High
    GEPAMulti-objective evolution⚡⭐⭐⭐⭐⭐High
  5. What is the SIMBA Optimizer?

    main

    SIMBA (Stochastic Introspective Mini-Batch Ascent) is an advanced prompt optimizer designed for efficient and insightful instruction refinement.

    Key characteristics include:

    • Introspective Learning: It analyzes its own optimization progress to provide recommendations and detect patterns.
    • Stochastic Mini-batch Processing: Uses small batches of data for faster, more efficient optimization compared to full dataset evaluation.
    • Temperature-controlled Sampling: Dynamically balances exploration (trying new instruction variants) and exploitation (refining known good ones) using temperature decay.
    • Adaptive Convergence: Automatically detects when optimization has reached a plateau or converged based on statistical analysis of improvement trends.
  6. What is the RLM Module and when to use it

    main

    The Recursive Language Model (pkg/modules/rlm) is a long-context execution loop designed for iterative reasoning. Instead of a single large prompt, it provides the model with a Go REPL-like environment to inspect context, call sub-LLMs, and iterate until a goal is met.

    Use RLM for:

    • Iterative reasoning over large context windows.
    • Code-assisted tasks like extraction, counting, filtering, or aggregation.
    • Recursive decomposition using nested sub-RLM calls.
    • Managing replay costs more effectively than raw transcript appending.
  7. What is the A2A Protocol?

    main

    The A2A (Agent-to-Agent) Protocol is a framework for multi-agent orchestration in dspy-go. It enables hierarchical composition where an Orchestrator Agent manages specialized Sub-Agents to solve complex tasks.

    Key benefits include:

    • Hierarchical Composition: Orchestrators delegate work to specialized agents.
    • Standardized Messages: Uses a consistent message format with metadata for structured communication.
    • In-Process Communication: Local coordination without HTTP overhead.
    • Capability Discovery: Agents can advertise and discover capabilities.
    • Field Metadata: Allows tagging data fields for structured passing between agents.
  8. How multimodal processing works in dspy-go

    main

    dspy-go handles mixed text and binary inputs using core.ContentBlock. The pkg/llms package converts these blocks into provider-neutral message parts, which are then formatted for specific providers (like Gemini, Anthropic, or OpenAI) by the underlying llm-go library.

    Before sending multimodal data, you should verify model capabilities using llm.Capabilities() to check for core.CapabilityVision or core.CapabilityMultimodal.

  9. Understand RLM Sub-RLM Budgeting in pkg/modules/rlm

    main

    The pkg/modules/rlm package implements two types of delegation budgets to control how many sub-RLM (Reasoning Language Model) calls can be made during a request tree execution:

    1. MaxDirectSubRLMCalls (Direct Budget): Limits the number of direct child delegations allowed per node. For example, a budget of 1 allows one direct child delegation per node.
    2. MaxTotalSubRLMCalls (Total Budget): Limits the total number of sub-RLM calls allowed across the entire request tree, regardless of depth or hierarchy.

    When a budget is exhausted, the RLM will reject further delegation attempts and return a final answer based on the guardrail firing.

  10. Optimize performance and costs in DSPy-Go

    main

    When building agentic workflows, consider the following performance and cost factors:

    Model Selection

    • gemini-2.5-flash: Recommended for the best balance of speed and quality.
    • gemini-2.5-pro: Use for high-quality, complex analysis.
    • gemini-2.5-flash-lite: Best for cost optimization and fastest responses.

    Caching

    To cache LLM responses and reduce API costs/latency, enable execution state in your context: ctx = core.WithExecutionState(ctx)

    Execution Characteristics

    • In-Process: Sub-agent calls have zero serialization overhead.
    • Parallelization: Independent agents can be called concurrently (planned feature).
  11. How GEPA prompt optimization works

    main

    GEPA (Generative Evolutionary Prompt Adaptation) is a multi-objective evolutionary approach to prompt optimization. Unlike single-objective optimizers, GEPA optimizes across 7 dimensions (success rate, quality, efficiency, robustness, generalization, diversity, and innovation) using Pareto-based selection.

    Key components include:

    • LLM-Based Self-Reflection: The model analyzes and critiques its own prompt performance, providing structured feedback (strengths, weaknesses, suggestions) to drive evolution.
    • Pareto Archive: Instead of a single 'best' prompt, GEPA maintains an archive of elite solutions that represent different trade-offs (e.g., high accuracy vs. fast execution).
    • Semantic Diversity: Uses LLM-based similarity metrics to ensure the population remains diverse beyond simple string matching.
  12. Compose modules into sophisticated workflows

    main

    Modules in dspy-go are designed to be composable. You can wrap one module inside another to create complex reasoning pipelines.

    Example Patterns:

    • Refined Reasoning: Wrap a ChainOfThought module inside a Refine module to ensure high-quality logical steps.
    • Batch Reasoning: Wrap a Refine module inside a Parallel module to process many high-quality tasks concurrently.
    // Combine ChainOfThought with Refine for high-quality reasoning
    cotModule := modules.NewChainOfThought(signature)
    refinedCot := modules.NewRefine(cotModule, refineConfig)
    
    // Wrap with Parallel for batch high-quality reasoning
    parallelRefinedCot := modules.NewParallel(refinedCot, parallelConfig)