examples/

directory
v0.81.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2026 License: MIT

README ΒΆ

DSPy-Go Examples

This directory contains comprehensive examples demonstrating the various features and capabilities of DSPy-Go, a Go implementation of the DSPy framework for declarative, composable AI systems.

πŸ“ Example Categories

🧠 Core DSPy Patterns
πŸ”§ Tool Management
🌐 Integrations
  • mcp/ - Model Context Protocol (MCP) integrations
  • others/ - Additional integrations and utilities
πŸ€– Agent Optimization
  • rlm_oolong/ - Benchmark the RLM module on OOLONG long-context tasks
  • rlm_oolong_gepa/ - Optimize an adaptive RLM agent with GEPA on OOLONG tasks

πŸš€ Quick Start

Smart Tool Registry

The Smart Tool Registry provides intelligent tool selection using Bayesian inference, performance tracking, and automatic discovery from MCP servers.

package main

import (
    "context"
    "github.com/XiaoConstantine/dspy-go/pkg/tools"
)

func main() {
    // Create registry with intelligent features
    config := &tools.SmartToolRegistryConfig{
        AutoDiscoveryEnabled:       true,
        PerformanceTrackingEnabled: true,
        FallbackEnabled:           true,
    }
    registry := tools.NewSmartToolRegistry(config)

    // Register tools
    searchTool := &MySearchTool{}
    registry.Register(searchTool)

    // Intelligent selection based on intent
    ctx := context.Background()
    tool, err := registry.SelectBest(ctx, "find user information")
    if err != nil {
        panic(err)
    }

    // Execute with performance tracking
    result, err := registry.ExecuteWithTracking(ctx, tool.Name(), params)
}
Basic DSPy Workflow
package main

import (
    "context"
    "github.com/XiaoConstantine/dspy-go/pkg/dspy"
    "github.com/XiaoConstantine/dspy-go/pkg/core"
)

func main() {
    // Create a language model
    lm := dspy.NewOpenAI(openai.DefaultConfig("your-api-key"))

    // Create a signature for classification
    signature := dspy.Signature{
        Input:  []string{"text"},
        Output: []string{"sentiment"},
        Instructions: "Classify the sentiment of the given text as positive, negative, or neutral.",
    }

    // Create a module
    classifier := dspy.NewPredict(lm, signature)

    // Use the module
    ctx := context.Background()
    result, err := classifier.Forward(ctx, map[string]interface{}{
        "text": "I love this product!",
    })

    fmt.Printf("Sentiment: %s\n", result["sentiment"])
}

πŸ“‹ Example Index

Smart Tool Registry Examples
Example Description Key Features
main.go Basic Smart Tool Registry usage Tool registration, intelligent selection, performance tracking
advanced_example.go Advanced features demonstration Custom selectors, fallbacks, capability analysis

Features Demonstrated:

  • 🧠 Bayesian Tool Selection: Multi-factor scoring with configurable weights
  • πŸ“Š Performance Tracking: Real-time metrics and reliability scoring
  • πŸ” Capability Analysis: Automatic capability extraction and matching
  • πŸ”„ Auto-Discovery: MCP server integration for dynamic tool registration
  • πŸ›‘οΈ Fallback Mechanisms: Intelligent fallback selection when tools fail
  • βš™οΈ Custom Configuration: Configurable selection algorithms and weights
Running the Examples
# Basic Smart Tool Registry example
cd examples/smart_tool_registry
go run main.go

# Advanced features demonstration
cd examples/smart_tool_registry
# Edit advanced_example.go to uncomment main() function
go run advanced_example.go

πŸ—οΈ Integrating Smart Tool Registry with DSPy-Go

The Smart Tool Registry seamlessly integrates with DSPy-Go workflows to provide intelligent tool management:

1. In DSPy Modules
type IntelligentModule struct {
    registry *tools.SmartToolRegistry
    signature dspy.Signature
}

func (m *IntelligentModule) Forward(ctx context.Context, inputs map[string]interface{}) (map[string]interface{}, error) {
    // Use registry to select best tool for current task
    intent := fmt.Sprintf("process %v", inputs["task_type"])
    tool, err := m.registry.SelectBest(ctx, intent)
    if err != nil {
        return nil, err
    }

    // Execute with performance tracking
    result, err := m.registry.ExecuteWithTracking(ctx, tool.Name(), inputs)
    if err != nil {
        return nil, err
    }

    return result.Data.(map[string]interface{}), nil
}
2. In Workflow Builders
import "github.com/XiaoConstantine/dspy-go/pkg/agents/workflows"

// Create workflow with intelligent tool selection
builder := workflows.NewWorkflowBuilder()
builder.SetToolRegistry(smartRegistry) // Use Smart Tool Registry

workflow := builder.
    AddStep("analyze", "analyze the input data").
    AddStep("process", "process the analysis results").
    AddStep("generate", "generate final output").
    Build()
3. With MCP Servers
// Auto-discover tools from MCP servers
mcpDiscovery := &tools.MCPDiscoveryService{
    ServerURL: "http://localhost:8080/mcp",
    PollInterval: 30 * time.Second,
}

config := &tools.SmartToolRegistryConfig{
    AutoDiscoveryEnabled: true,
    MCPDiscovery: mcpDiscovery,
}

registry := tools.NewSmartToolRegistry(config)
// Tools will be automatically discovered and registered

πŸ”§ Creating Custom Tools

Tools in DSPy-Go implement the core.Tool interface:

type MyCustomTool struct {
    name string
}

func (t *MyCustomTool) Name() string {
    return t.name
}

func (t *MyCustomTool) Description() string {
    return "My custom tool description"
}

func (t *MyCustomTool) Metadata() *core.ToolMetadata {
    return &core.ToolMetadata{
        Name:         t.name,
        Description:  t.Description(),
        Capabilities: []string{"custom", "processing"},
        Version:      "1.0.0",
    }
}

func (t *MyCustomTool) CanHandle(ctx context.Context, intent string) bool {
    return strings.Contains(strings.ToLower(intent), "custom")
}

func (t *MyCustomTool) Execute(ctx context.Context, params map[string]interface{}) (core.ToolResult, error) {
    // Your tool logic here
    return core.ToolResult{
        Data: map[string]interface{}{"result": "success"},
    }, nil
}

func (t *MyCustomTool) Validate(params map[string]interface{}) error {
    return nil // Implement validation logic
}

func (t *MyCustomTool) InputSchema() models.InputSchema {
    return models.InputSchema{
        Type: "object",
        Properties: map[string]models.ParameterSchema{
            "input": {
                Type:        "string",
                Description: "Input parameter",
                Required:    true,
            },
        },
    }
}

πŸ“– Additional Resources

🀝 Contributing

When adding new examples:

  1. Create a dedicated directory for your example
  2. Include a README.md with clear usage instructions
  3. Add comprehensive comments explaining key concepts
  4. Include both basic and advanced usage patterns
  5. Ensure examples are self-contained and runnable

πŸ“„ License

All examples are provided under the same license as DSPy-Go. See the main repository LICENSE file for details.

Directories ΒΆ

Path Synopsis
Package main demonstrates basic ACE (Agentic Context Engineering) usage.
Package main demonstrates basic ACE (Agentic Context Engineering) usage.
Package main demonstrates ACE (Agentic Context Engineering) integrated with the ReAct agent.
Package main demonstrates ACE (Agentic Context Engineering) integrated with the ReAct agent.
Package main provides an interactive CLI for OAuth login to AI provider subscriptions.
Package main provides an interactive CLI for OAuth login to AI provider subscriptions.
others
gepa command
html command
mcp command
mipro command
simba command
Package main demonstrates the RLM (Recursive Language Model) module in dspy-go.
Package main demonstrates the RLM (Recursive Language Model) module in dspy-go.
Package main demonstrates the RLM module on OOLONG benchmark tasks.
Package main demonstrates the RLM module on OOLONG benchmark tasks.
xml_adapter
basic_usage command
composable command
custom_config command
predict_xml command
react_xml command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL