DSPy-Go

What is DSPy-Go?
DSPy-Go is a native Go implementation of the DSPy framework, bringing systematic prompt engineering and automated reasoning capabilities to Go applications. It provides a flexible and idiomatic framework for building reliable and effective Language Model (LLM) applications through composable modules and workflows.
Key Features
- Modular Architecture: Build complex LLM applications by composing simple, reusable components
- Systematic Prompt Engineering: Optimize prompts automatically based on examples and feedback
- Flexible Workflows: Chain, branch, and orchestrate LLM operations with powerful workflow abstractions
- Multiple LLM Providers: Support for Anthropic Claude, Google Gemini (with multimodal support), OpenAI, Ollama, LlamaCPP, and OpenAI-compatible APIs (LiteLLM, LocalAI, etc.)
- Multimodal Processing: Native support for image analysis, vision Q&A, and multimodal chat with streaming capabilities
- Advanced Reasoning Patterns: Implement chain-of-thought, ReAct, refinement, and multi-chain comparison techniques
- Parallel Processing: Built-in support for concurrent execution to improve performance
- Dataset Management: Automatic downloading and management of popular datasets like GSM8K and HotPotQA
- Smart Tool Management: Intelligent tool selection, performance tracking, and auto-discovery from MCP servers
- Tool Integration: Native support for custom tools and MCP (Model Context Protocol) servers
- Tool Chaining: Sequential execution of tools in pipelines with data transformation and conditional logic
- Tool Composition: Create reusable composite tools by combining multiple tools into single units
- Advanced Parallel Execution: High-performance parallel tool execution with intelligent scheduling algorithms
- Dependency Resolution: Automatic execution planning based on tool dependencies with parallel optimization
- Quality Optimization: Advanced optimizers including GEPA (Generative Evolutionary Prompt Adaptation), MIPRO, SIMBA, and BootstrapFewShot for systematic improvement
- Compatibility Testing: Comprehensive compatibility testing framework for validating optimizer behavior against Python DSPy implementations
Installation
go get github.com/XiaoConstantine/dspy-go
π Quick Start with CLI (Recommended)
New to DSPy-Go? Try our interactive CLI tool to explore optimizers instantly:
cd cmd/dspy-cli
go build -o dspy-cli
export GEMINI_API_KEY="your-api-key-here"
./dspy-cli list
./dspy-cli try bootstrap --dataset gsm8k
./dspy-cli try mipro --dataset gsm8k --verbose
./dspy-cli recommend balanced
The CLI eliminates 60+ lines of boilerplate code and lets you test all optimizers (Bootstrap, MIPRO, SIMBA, GEPA, COPRO) with sample datasets instantly. β Full CLI Documentation
Programming Quick Start
Here's a simple example to get you started with DSPy-Go:
package main
import (
"context"
"fmt"
"log"
"github.com/XiaoConstantine/dspy-go/pkg/core"
"github.com/XiaoConstantine/dspy-go/pkg/llms"
"github.com/XiaoConstantine/dspy-go/pkg/modules"
)
func main() {
llms.EnsureFactory()
err := core.ConfigureDefaultLLM("your-api-key", core.ModelAnthropicSonnet)
if err != nil {
log.Fatalf("Failed to configure LLM: %v", err)
}
signature := core.NewSignature(
[]core.InputField{{Field: core.NewField("question", core.WithDescription("Question to answer"))}},
[]core.OutputField{{Field: core.NewField("answer", core.WithDescription("Answer to the question"))}},
)
cot := modules.NewChainOfThought(signature)
program := core.NewProgram(
map[string]core.Module{"cot": cot},
func(ctx context.Context, inputs map[string]interface{}) (map[string]interface{}, error) {
return cot.Process(ctx, inputs)
},
)
result, err := program.Execute(context.Background(), map[string]interface{}{
"question": "What is the capital of France?",
})
if err != nil {
log.Fatalf("Error executing program: %v", err)
}
fmt.Printf("Answer: %s\n", result["answer"])
}
Core Concepts
DSPy-Go is built around several key concepts that work together to create powerful LLM applications:
Signatures
Signatures define the input and output fields for modules, creating a clear contract for what a module expects and produces.
signature := core.NewSignature(
[]core.InputField{
{Field: core.NewField("document", core.WithDescription("The document to summarize"))},
},
[]core.OutputField{
{Field: core.NewField("summary", core.WithDescription("A concise summary of the document"))},
{Field: core.NewField("key_points", core.WithDescription("The main points from the document"))},
},
)
Signatures can include field descriptions that enhance prompt clarity and improve LLM performance.
Modules
Modules are the building blocks of DSPy-Go programs. They encapsulate specific functionalities and can be composed to create complex pipelines. Some key modules include:
Predict
The simplest module that makes direct predictions using an LLM.
predict := modules.NewPredict(signature)
result, err := predict.Process(ctx, map[string]interface{}{
"document": "Long document text here...",
})
ChainOfThought
Implements chain-of-thought reasoning, which guides the LLM to break down complex problems into intermediate steps.
cot := modules.NewChainOfThought(signature)
result, err := cot.Process(ctx, map[string]interface{}{
"question": "Solve 25 Γ 16 step by step.",
})
ReAct
Implements the Reasoning and Acting paradigm, allowing LLMs to use tools to solve problems.
calculator := tools.NewCalculatorTool()
searchTool := tools.NewSearchTool()
registry := tools.NewInMemoryToolRegistry()
registry.Register(calculator)
registry.Register(searchTool)
react := modules.NewReAct(signature, registry, 5)
result, err := react.Process(ctx, map[string]interface{}{
"question": "What is the population of France divided by 1000?",
})
MultiChainComparison
Compares multiple reasoning attempts and synthesizes a holistic evaluation, useful for improving decision quality through multiple perspectives.
signature := core.NewSignature(
[]core.InputField{
{Field: core.NewField("problem", core.WithDescription("The problem to solve"))},
},
[]core.OutputField{
{Field: core.NewField("solution", core.WithDescription("The recommended solution"))},
},
)
multiChain := modules.NewMultiChainComparison(signature, 3, 0.7)
completions := []map[string]interface{}{
{
"rationale": "focus on cost reduction approach",
"solution": "Implement automation to reduce operational costs",
},
{
"reasoning": "prioritize customer satisfaction strategy",
"solution": "Invest in customer service improvements",
},
{
"rationale": "balance short-term and long-term objectives",
"solution": "Gradual optimization with phased implementation",
},
}
result, err := multiChain.Process(ctx, map[string]interface{}{
"problem": "How should we address declining business performance?",
"completions": completions,
})
Refine
Improves prediction quality by running multiple attempts with different temperatures and selecting the best result based on a reward function.
rewardFn := func(inputs, outputs map[string]interface{}) float64 {
answer := outputs["answer"].(string)
return math.Min(1.0, float64(len(answer))/100.0)
}
refine := modules.NewRefine(
modules.NewPredict(signature),
modules.RefineConfig{
N: 5,
RewardFn: rewardFn,
Threshold: 0.8,
},
)
result, err := refine.Process(ctx, map[string]interface{}{
"question": "Explain quantum computing in detail.",
})
Parallel
Wraps any module to enable concurrent execution across multiple inputs, providing significant performance improvements for batch processing.
baseModule := modules.NewPredict(signature)
parallel := modules.NewParallel(baseModule,
modules.WithMaxWorkers(4),
modules.WithReturnFailures(true),
modules.WithStopOnFirstError(false),
)
batchInputs := []map[string]interface{}{
{"question": "What is 2+2?"},
{"question": "What is 3+3?"},
{"question": "What is 4+4?"},
}
result, err := parallel.Process(ctx, map[string]interface{}{
"batch_inputs": batchInputs,
})
results := result["results"].([]map[string]interface{})
for i, res := range results {
if res != nil {
fmt.Printf("Input %d: %s\n", i, res["answer"])
}
}
Programs
Programs combine modules into executable workflows. They define how inputs flow through the system and how outputs are produced.
program := core.NewProgram(
map[string]core.Module{
"retriever": retriever,
"generator": generator,
},
func(ctx context.Context, inputs map[string]interface{}) (map[string]interface{}, error) {
retrieverResult, err := retriever.Process(ctx, inputs)
if err != nil {
return nil, err
}
generatorInputs := map[string]interface{}{
"question": inputs["question"],
"documents": retrieverResult["documents"],
}
return generator.Process(ctx, generatorInputs)
},
)
Optimizers
Optimizers help improve the performance of your DSPy-Go programs by automatically tuning prompts and module parameters.
BootstrapFewShot
Automatically selects high-quality examples for few-shot learning.
dataset := datasets.NewInMemoryDataset()
dataset.AddExample(map[string]interface{}{
"question": "What is the capital of France?",
"answer": "The capital of France is Paris.",
})
optimizer := optimizers.NewBootstrapFewShot(dataset, metrics.NewExactMatchMetric("answer"))
optimizedModule, err := optimizer.Optimize(ctx, originalModule)
MIPRO (Multi-step Interactive Prompt Optimization)
Advanced optimizer that uses TPE (Tree-structured Parzen Estimator) search for systematic prompt optimization.
mipro := optimizers.NewMIPRO(
metricFunc,
optimizers.WithMode(optimizers.LightMode),
optimizers.WithNumTrials(10),
optimizers.WithTPEGamma(0.25),
)
optimizedProgram, err := mipro.Compile(ctx, program, dataset, nil)
SIMBA (Stochastic Introspective Mini-Batch Ascent)
Cutting-edge optimizer with introspective learning capabilities that analyzes its own optimization progress.
simba := optimizers.NewSIMBA(
optimizers.WithSIMBABatchSize(8),
optimizers.WithSIMBAMaxSteps(12),
optimizers.WithSIMBANumCandidates(6),
optimizers.WithSamplingTemperature(0.2),
)
optimizedProgram, err := simba.Compile(ctx, program, dataset, metricFunc)
state := simba.GetState()
fmt.Printf("Optimization completed in %d steps with score %.3f\n",
state.CurrentStep, state.BestScore)
for _, insight := range state.IntrospectionLog {
fmt.Printf("Analysis: %s\n", insight)
}
COPRO (Collaborative Prompt Optimization)
Collaborative optimizer for multi-module prompt optimization.
copro := optimizers.NewCopro(dataset, metrics.NewRougeMetric("answer"))
optimizedModule, err := copro.Optimize(ctx, originalModule)
GEPA (Generative Evolutionary Prompt Adaptation)
State-of-the-art evolutionary optimizer that combines multi-objective Pareto optimization with LLM-based self-reflection for comprehensive prompt evolution.
config := &optimizers.GEPAConfig{
PopulationSize: 20,
MaxGenerations: 10,
SelectionStrategy: "adaptive_pareto",
MutationRate: 0.3,
CrossoverRate: 0.7,
ReflectionFreq: 2,
ElitismRate: 0.1,
}
gepa, err := optimizers.NewGEPA(config)
if err != nil {
log.Fatalf("Failed to create GEPA optimizer: %v", err)
}
optimizedProgram, err := gepa.Compile(ctx, program, dataset, metricFunc)
state := gepa.GetOptimizationState()
fmt.Printf("Optimization completed in %d generations\n", state.CurrentGeneration)
fmt.Printf("Best fitness: %.3f\n", state.BestFitness)
archive := state.GetParetoArchive()
fmt.Printf("Elite solutions preserved: %d\n", len(archive))
GEPA Key Features:
- Multi-Objective Optimization: Optimizes across 7 dimensions (success rate, quality, efficiency, robustness, generalization, diversity, innovation)
- Pareto-Based Selection: Maintains diverse solutions optimized for different trade-offs
- LLM-Based Self-Reflection: Uses language models to analyze and critique prompt performance
- Semantic Diversity Metrics: Employs LLM-based similarity for true semantic diversity assessment
- Elite Archive Management: Preserves high-quality solutions across generations with crowding distance
- Real-Time System Monitoring: Context-aware performance tracking and adaptive parameter adjustment
- Advanced Genetic Operators: Semantic crossover and mutation using LLMs for meaningful prompt evolution
Agents and Workflows
DSPy-Go provides powerful abstractions for building more complex agent systems.
Memory
Different memory implementations for tracking conversation history.
memory := memory.NewBufferMemory(10)
memory.Add(context.Background(), "user", "Hello, how can you help me?")
memory.Add(context.Background(), "assistant", "I can answer questions and help with tasks. What do you need?")
history, err := memory.Get(context.Background())
Workflows
Chain Workflow
Sequential execution of steps:
workflow := workflows.NewChainWorkflow(store)
workflow.AddStep(&workflows.Step{
ID: "step1",
Module: modules.NewPredict(signature1),
})
workflow.AddStep(&workflows.Step{
ID: "step2",
Module: modules.NewPredict(signature2),
})
result, err := workflow.Execute(ctx, inputs)
Configurable Retry Logic
Each workflow step can be configured with retry logic:
step := &workflows.Step{
ID: "retry_example",
Module: myModule,
RetryConfig: &workflows.RetryConfig{
MaxAttempts: 3,
BackoffMultiplier: 2.0,
InitialBackoff: time.Second,
},
Condition: func(state map[string]interface{}) bool {
return someCondition(state)
},
}
Orchestrator
Flexible task decomposition and execution:
orchestrator := agents.NewOrchestrator()
researchTask := agents.NewTask("research", researchModule)
summarizeTask := agents.NewTask("summarize", summarizeModule)
orchestrator.AddTask(researchTask)
orchestrator.AddTask(summarizeTask)
result, err := orchestrator.Execute(ctx, map[string]interface{}{
"topic": "Climate change impacts",
})
Working with Different LLM Providers
DSPy-Go supports multiple LLM providers with flexible configuration options:
llm, err := llms.NewAnthropicLLM("api-key", core.ModelAnthropicSonnet)
llm, err := llms.NewGeminiLLM("api-key", "gemini-pro")
llm, err := llms.NewOpenAI(core.ModelOpenAIGPT4, "api-key")
llm, err := llms.NewOpenAILLM(core.ModelOpenAIGPT4,
llms.WithAPIKey("api-key"),
llms.WithOpenAIBaseURL("http://localhost:4000"))
llm, err := llms.NewOpenAILLM(core.ModelOpenAIGPT4,
llms.WithAPIKey("api-key"),
llms.WithOpenAIBaseURL("http://localhost:8080"),
llms.WithOpenAIPath("/v1/chat/completions"))
llm, err := llms.NewOpenAILLM(core.ModelOpenAIGPT4,
llms.WithAPIKey("custom-key"),
llms.WithOpenAIBaseURL("https://api.custom-provider.com"),
llms.WithHeader("Custom-Header", "value"),
llms.WithOpenAITimeout(30*time.Second))
llm, err := llms.NewOllamaLLM("ollama:llama2")
llm, err := llms.NewLlamacppLLM("http://localhost:8080")
core.SetDefaultLLM(llm)
myModule.SetLLM(llm)
OpenAI-Compatible APIs
DSPy-Go's OpenAI provider supports any OpenAI-compatible API through functional options, making it easy to work with various providers:
LiteLLM
LiteLLM provides a unified interface to 100+ LLMs:
llm, err := llms.NewOpenAILLM(core.ModelOpenAIGPT4,
llms.WithAPIKey("your-api-key"),
llms.WithOpenAIBaseURL("http://localhost:4000"))
llm, err := llms.NewOpenAILLM(core.ModelOpenAIGPT4,
llms.WithAPIKey("litellm-key"),
llms.WithOpenAIBaseURL("https://your-litellm-instance.com"),
llms.WithOpenAITimeout(60*time.Second),
llms.WithHeader("X-Custom-Header", "value"))
LocalAI
LocalAI for running local models:
llm, err := llms.NewOpenAILLM(core.ModelOpenAIGPT4,
llms.WithOpenAIBaseURL("http://localhost:8080"),
llms.WithOpenAIPath("/v1/chat/completions"))
FastChat
FastChat for serving local models:
llm, err := llms.NewOpenAILLM(core.ModelOpenAIGPT4,
llms.WithOpenAIBaseURL("http://localhost:8000"),
llms.WithOpenAIPath("/v1/chat/completions"))
Azure OpenAI
Configure for Azure OpenAI Service:
llm, err := llms.NewOpenAILLM(core.ModelOpenAIGPT4,
llms.WithAPIKey("azure-api-key"),
llms.WithOpenAIBaseURL("https://your-resource.openai.azure.com"),
llms.WithOpenAIPath("/openai/deployments/your-deployment/chat/completions"),
llms.WithHeader("api-version", "2024-02-15-preview"))
Custom Providers
Any API that follows the OpenAI chat completions format:
llm, err := llms.NewOpenAILLM(core.ModelOpenAIGPT4,
llms.WithAPIKey("custom-key"),
llms.WithOpenAIBaseURL("https://api.custom-provider.com"),
llms.WithOpenAIPath("/v1/chat/completions"),
llms.WithOpenAITimeout(30*time.Second),
llms.WithHeader("Authorization", "Bearer custom-token"),
llms.WithHeader("X-API-Version", "v1"))
Advanced Features
Tracing and Logging
DSPy-Go includes detailed tracing and structured logging for debugging and optimization:
ctx = core.WithExecutionState(context.Background())
logger := logging.NewLogger(logging.Config{
Severity: logging.DEBUG,
Outputs: []logging.Output{logging.NewConsoleOutput(true)},
})
logging.SetLogger(logger)
executionState := core.GetExecutionState(ctx)
steps := executionState.GetSteps("moduleId")
for _, step := range steps {
fmt.Printf("Step: %s, Duration: %s\n", step.Name, step.Duration)
fmt.Printf("Prompt: %s\n", step.Prompt)
fmt.Printf("Response: %s\n", step.Response)
}
Custom Tools
You can extend ReAct modules with custom tools:
type WeatherTool struct{}
func (t *WeatherTool) GetName() string {
return "weather"
}
func (t *WeatherTool) GetDescription() string {
return "Get the current weather for a location"
}
func (t *WeatherTool) CanHandle(action string) bool {
return strings.HasPrefix(action, "weather(")
}
func (t *WeatherTool) Execute(ctx context.Context, action string) (string, error) {
location := parseLocation(action)
weather, err := fetchWeather(location)
if err != nil {
return "", err
}
return fmt.Sprintf("Weather in %s: %s, %dΒ°C", location, weather.Condition, weather.Temperature), nil
}
registry := tools.NewInMemoryToolRegistry()
registry.Register(&WeatherTool{})
react := modules.NewReAct(signature, registry, 5)
Smart Tool Registry
DSPy-Go includes an intelligent tool management system that uses Bayesian inference for optimal tool selection:
import "github.com/XiaoConstantine/dspy-go/pkg/tools"
config := &tools.SmartToolRegistryConfig{
AutoDiscoveryEnabled: true,
PerformanceTrackingEnabled: true,
FallbackEnabled: true,
}
registry := tools.NewSmartToolRegistry(config)
registry.Register(mySearchTool)
registry.Register(myAnalysisTool)
tool, err := registry.SelectBest(ctx, "find user information")
if err != nil {
log.Fatal(err)
}
result, err := registry.ExecuteWithTracking(ctx, tool.Name(), params)
Key Features:
- π§ 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: Dynamic tool registration from MCP servers
- π‘οΈ Fallback Mechanisms: Intelligent fallback when tools fail
Tool Chaining and Composition
DSPy-Go provides powerful capabilities for chaining and composing tools to build complex workflows:
Tool Chaining
Create sequential pipelines with data transformation and conditional execution:
import "github.com/XiaoConstantine/dspy-go/pkg/tools"
pipeline, err := tools.NewPipelineBuilder("data_processing", registry).
Step("data_extractor").
StepWithTransformer("data_validator", tools.TransformExtractField("result")).
ConditionalStep("data_enricher",
tools.ConditionExists("validation_result"),
tools.ConditionEquals("status", "validated")).
StepWithRetries("data_transformer", 3).
FailFast().
EnableCaching().
Build()
result, err := pipeline.Execute(ctx, map[string]interface{}{
"raw_data": "input data to process",
})
Data Transformations
Transform data between pipeline steps:
transformer := tools.TransformExtractField("important_field")
transformer := tools.TransformRename(map[string]string{
"old_name": "new_name",
})
transformer := tools.TransformChain(
tools.TransformRename(map[string]string{"status": "processing_status"}),
tools.TransformAddConstant(map[string]interface{}{"pipeline_id": "001"}),
tools.TransformFilter([]string{"result", "pipeline_id", "processing_status"}),
)
Dependency Resolution
Automatic execution planning with parallel optimization:
graph := tools.NewDependencyGraph()
graph.AddNode(&tools.DependencyNode{
ToolName: "data_extractor",
Dependencies: []string{},
Outputs: []string{"raw_data"},
Priority: 1,
})
graph.AddNode(&tools.DependencyNode{
ToolName: "data_validator",
Dependencies: []string{"data_extractor"},
Inputs: []string{"raw_data"},
Outputs: []string{"validated_data"},
Priority: 2,
})
depPipeline, err := tools.NewDependencyPipeline("smart_pipeline", registry, graph, options)
result, err := depPipeline.ExecuteWithDependencies(ctx, input)
Parallel Execution
High-performance parallel tool execution with advanced scheduling:
executor := tools.NewParallelExecutor(registry, 4)
tasks := []*tools.ParallelTask{
{
ID: "task1",
ToolName: "analyzer",
Input: data1,
Priority: 1,
},
{
ID: "task2",
ToolName: "processor",
Input: data2,
Priority: 2,
},
}
results, err := executor.ExecuteParallel(ctx, tasks, &tools.PriorityScheduler{})
results, err := executor.ExecuteParallel(ctx, tasks, tools.NewFairShareScheduler())
Tool Composition
Create reusable composite tools by combining multiple tools:
type CompositeTool struct {
name string
pipeline *tools.ToolPipeline
}
func NewCompositeTool(name string, registry core.ToolRegistry,
builder func(*tools.PipelineBuilder) *tools.PipelineBuilder) (*CompositeTool, error) {
pipeline, err := builder(tools.NewPipelineBuilder(name+"_pipeline", registry)).Build()
if err != nil {
return nil, err
}
return &CompositeTool{
name: name,
pipeline: pipeline,
}, nil
}
textProcessor, err := NewCompositeTool("text_processor", registry,
func(builder *tools.PipelineBuilder) *tools.PipelineBuilder {
return builder.
Step("text_uppercase").
Step("text_reverse").
Step("text_length")
})
registry.Register(textProcessor)
result, err := textProcessor.Execute(ctx, input)
complexPipeline, err := tools.NewPipelineBuilder("complex", registry).
Step("text_processor").
Step("final_formatter").
Build()
Key Features:
- π Sequential Chaining: Build complex workflows by chaining tools together
- π Data Transformation: Transform data between steps with built-in transformers
- β‘ Conditional Execution: Execute steps based on previous results
- πΈοΈ Dependency Resolution: Automatic execution planning with topological sorting
- π Parallel Optimization: Execute independent tools concurrently for performance
- π§© Tool Composition: Create reusable composite tools as building blocks
- πΎ Result Caching: Cache intermediate results for improved performance
- π Retry Logic: Configurable retry mechanisms for reliability
- π Performance Tracking: Monitor execution metrics and worker utilization
MCP (Model Context Protocol) Integration
DSPy-Go supports integration with MCP servers for accessing external tools and services:
import (
"github.com/XiaoConstantine/dspy-go/pkg/tools"
"github.com/XiaoConstantine/mcp-go/pkg/client"
)
mcpClient, err := client.NewStdioClient("path/to/mcp-server")
if err != nil {
log.Fatal(err)
}
registry := tools.NewInMemoryToolRegistry()
err = tools.RegisterMCPTools(registry, mcpClient)
if err != nil {
log.Fatal(err)
}
react := modules.NewReAct(signature, registry, 5)
smartRegistry := tools.NewSmartToolRegistry(&tools.SmartToolRegistryConfig{
PerformanceTrackingEnabled: true,
})
err = tools.RegisterMCPTools(smartRegistry, mcpClient)
if err != nil {
log.Fatal(err)
}
selectedTool, err := smartRegistry.SelectBest(ctx, "analyze financial data")
Streaming Support
Process LLM outputs incrementally as they're generated:
handler := func(chunk string) {
fmt.Print(chunk)
}
module.SetStreamingHandler(handler)
result, err := module.Process(ctx, inputs)
Dataset Management
DSPy-Go provides built-in support for downloading and managing common datasets:
import "github.com/XiaoConstantine/dspy-go/pkg/datasets"
gsm8kPath, err := datasets.EnsureDataset("gsm8k")
if err != nil {
log.Fatal(err)
}
gsm8kDataset, err := datasets.LoadGSM8K(gsm8kPath)
if err != nil {
log.Fatal(err)
}
hotpotPath, err := datasets.EnsureDataset("hotpotqa")
if err != nil {
log.Fatal(err)
}
hotpotDataset, err := datasets.LoadHotPotQA(hotpotPath)
if err != nil {
log.Fatal(err)
}
optimizer := optimizers.NewBootstrapFewShot(gsm8kDataset, metricFunc)
optimizedModule, err := optimizer.Optimize(ctx, module)
Compatibility Testing
DSPy-Go includes a comprehensive compatibility testing framework to ensure that optimizer implementations match the behavior of Python DSPy:
cd compatibility_test
./run_experiment.sh
./run_experiment.sh --optimizer gepa --dataset-size 10
python dspy_comparison.py --optimizer gepa
go run go_comparison.go --optimizer gepa
python compare_results.py
The framework tests:
- API Compatibility: Parameter names, types, and behavior
- Performance Parity: Score differences within acceptable thresholds
- Behavioral Consistency: Similar optimization patterns and convergence
- All Four Optimizers: BootstrapFewShot, MIPRO, SIMBA, and GEPA
Results are saved as JSON files with detailed compatibility analysis and recommendations.
Examples
π― CLI Tool (Zero Code Required)
DSPy-CLI: Interactive command-line tool for exploring optimizers without writing code. Perfect for getting started, testing optimizers, and rapid experimentation.
cd cmd/dspy-cli && go build -o dspy-cli
./dspy-cli try mipro --dataset gsm8k --max-examples 5 --verbose
π Code Examples
Check the examples directory for complete implementations:
Smart Tool Registry Examples
cd examples/smart_tool_registry
go run main.go
go run advanced_example.go
The Smart Tool Registry examples demonstrate:
- Intelligent tool selection using Bayesian inference
- Performance tracking and metrics collection
- Auto-discovery from MCP servers
- Capability analysis and matching
- Fallback mechanisms and error handling
- Custom selector configuration
Tool Chaining and Composition Examples
cd examples/tool_chaining
go run main.go
cd examples/tool_composition
go run main.go
The Tool Chaining and Composition examples demonstrate:
Tool Chaining:
- Sequential pipeline execution with fluent API
- Data transformation between pipeline steps
- Conditional step execution based on previous results
- Dependency-aware execution with automatic parallelization
- Advanced parallel tool execution with intelligent scheduling
- Batch processing with fair share and priority scheduling
- Result caching and performance optimization
- Comprehensive error handling and retry mechanisms
Tool Composition:
- Creating reusable composite tools by combining multiple tools
- Nested composition (composites using other composites)
- Using composite tools as building blocks in larger pipelines
- Tool composition with data transformations
- Registry integration for seamless tool management
Multimodal Processing Example
cd examples/multimodal
export GEMINI_API_KEY="your-api-key-here"
go run main.go
The Multimodal Processing example demonstrates:
- Image Analysis: Basic image analysis with natural language questions
- Vision Question Answering: Structured visual analysis with detailed observations
- Multimodal Chat: Interactive conversations with images
- Streaming Multimodal: Real-time processing of multimodal content
- Multiple Image Analysis: Comparing and analyzing multiple images simultaneously
- Content Block System: Flexible handling of text, image, and future audio content
Documentation
For more detailed documentation:
License
DSPy-Go is released under the MIT License. See the LICENSE file for details.