This skill should be used when working with DSPy.rb, a Ruby framework for building type-safe, composable LLM applications. Use this when implementing predictable AI features, creating LLM signatures and modules, configuring language model providers (OpenAI, Anthropic, Gemini, Ollama), building agent systems with tools, optimizing prompts, or testing LLM-powered functionality in Ruby applications.
This skill should be used when working with DSPy.rb, a Ruby framework for building type-safe, composable LLM applications. Use this when implementing predictable AI features, creating LLM signatures and modules, configuring language model providers (OpenAI, Anthropic, Gemini, Ollama), building agent systems with tools, optimizing prompts, or testing LLM-powered functionality in Ruby applications.
DSPy.rb Expert
Overview
DSPy.rb is a Ruby framework that enables developers to program LLMs, not prompt them. Instead of manually crafting prompts, define application requirements through type-safe, composable modules that can be tested, optimized, and version-controlled like regular code.
This skill provides comprehensive guidance on:
Creating type-safe signatures for LLM operations
Building composable modules and workflows
Configuring multiple LLM providers
Implementing agents with tools
Testing and optimizing LLM applications
Production deployment patterns
Core Capabilities
1. Type-Safe Signatures
Create input/output contracts for LLM operations with runtime type checking.
When to use: Defining any LLM task, from simple classification to complex analysis.
ReAct: Tasks requiring external tools (search, calculation, API calls)
CodeAct: Tasks best solved with generated code
Full documentation: See references/core-concepts.md section on Predictors.
4. LLM Provider Configuration
Support for OpenAI, Anthropic Claude, Google Gemini, Ollama, and OpenRouter.
Quick configuration examples:
# OpenAIDSPy.configure do |c|
c.lm = DSPy::LM.new('openai/gpt-4o-mini',
api_key:ENV['OPENAI_API_KEY'])
end# Anthropic ClaudeDSPy.configure do |c|
c.lm = DSPy::LM.new('anthropic/claude-3-5-sonnet-20241022',
api_key:ENV['ANTHROPIC_API_KEY'])
end# Google GeminiDSPy.configure do |c|
c.lm = DSPy::LM.new('gemini/gemini-1.5-pro',
api_key:ENV['GOOGLE_API_KEY'])
end# Local Ollama (free, private)DSPy.configure do |c|
c.lm = DSPy::LM.new('ollama/llama3.1')
end
Templates: See assets/config-template.rb for comprehensive examples including:
Environment-based configuration
Multi-model setups for different tasks
Configuration with observability (OpenTelemetry, Langfuse)
Retry logic and fallback strategies
Budget tracking
Rails initializer patterns
Provider compatibility matrix:
Feature
OpenAI
Anthropic
Gemini
Ollama
Structured Output
✅
✅
✅
✅
Vision (Images)
✅
✅
✅
⚠️ Limited
Image URLs
✅
❌
❌
❌
Tool Calling
✅
✅
✅
Varies
Cost optimization strategy:
Development: Ollama (free) or gpt-4o-mini (cheap)
Testing: gpt-4o-mini with temperature=0.0
Production simple tasks: gpt-4o-mini, claude-3-haiku, gemini-1.5-flash
Production complex tasks: gpt-4o, claude-3-5-sonnet, gemini-1.5-pro
Full documentation: See references/providers.md for all configuration options, provider-specific features, and troubleshooting.
5. Multimodal & Vision Support
Process images alongside text using the unified DSPy::Image interface.
Quick reference:
classVisionSignature < DSPy::Signature
description "Analyze image and answer questions"
input do
const :image, DSPy::Image
const :question, Stringend
output do
const :answer, Stringendend
predictor = DSPy::Predict.new(VisionSignature)
result = predictor.forward(
image:DSPy::Image.from_file("path/to/image.jpg"),
question:"What objects are visible?"
)
Image loading methods:
# From fileDSPy::Image.from_file("path/to/image.jpg")
# From URL (OpenAI only)DSPy::Image.from_url("https://example.com/image.jpg")
# From base64DSPy::Image.from_base64(base64_data, mime_type:"image/jpeg")
Provider support:
OpenAI: Full support including URLs
Anthropic, Gemini: Base64 or file loading only
Ollama: Limited multimodal depending on model
Full documentation: See references/core-concepts.md section on Multimodal Support.
6. Testing LLM Applications
Write standard RSpec tests for LLM logic.
Quick reference:
RSpec.describe EmailClassifierdo
before doDSPy.configure do |c|
c.lm = DSPy::LM.new('openai/gpt-4o-mini',
api_key:ENV['OPENAI_API_KEY'])
endend
it 'classifies technical emails correctly'do
classifier = EmailClassifier.new
result = classifier.forward(
email_subject:"Can't log in",
email_body:"Unable to access account"
)
expect(result[:category]).to eq('Technical')
expect(result[:priority]).to be_in(['High', 'Medium', 'Low'])
endend
Testing patterns:
Mock LLM responses for unit tests
Use VCR for deterministic API testing
Test type safety and validation
Test edge cases (empty inputs, special characters, long texts)
Integration test complete workflows
Full documentation: See references/optimization.md section on Testing.
7. Optimization & Improvement
Automatically improve prompts and modules using optimization techniques.