### Configure Google Gemini Language Model in Ruby Source: https://vicentereig.github.io/dspy.rb/index This example demonstrates configuring DSPy.rb to use Google Gemini models, such as Gemini 2.5 Flash. It requires a Gemini API key and enables structured outputs. ```ruby DSPy.configure do |c| c.lm = DSPy::LM.new( 'gemini/gemini-2.5-flash', api_key: ENV['GEMINI_API_KEY'], structured_outputs: true ) end ``` -------------------------------- ### Configure Ollama for Local Models in Ruby Source: https://vicentereig.github.io/dspy.rb/index This example shows how to configure DSPy.rb to use local models via Ollama, such as Llama, Mistral, or Gemma. No API key is needed as it runs locally. ```ruby DSPy.configure do |c| c.lm = DSPy::LM.new('ollama/llama3.2') # Runs locally, completely free end ``` -------------------------------- ### Define Type-Safe Email Signatures with DSPy.rb Source: https://vicentereig.github.io/dspy.rb/index This snippet demonstrates how to define a type-safe signature for classifying customer support emails using DSPy.rb and Sorbet. It includes custom `T::Struct` and `T::Enum` definitions for email properties, and a `DSPy::Signature` class that specifies the input email structure and the desired output classification, priority, and summary. This approach ensures type correctness and guides the LLM effectively. ```ruby class Email < T::Struct const :subject, String const :from, String const :to, String const :body, String end class EmailCategory < T::Enum enums do Technical = new('technical') Billing = new('billing') General = new('general') end end class Priority < T::Enum enums do Low = new('low') Medium = new('medium') High = new('high') end end class ClassifyEmail < DSPy::Signature description "Classify customer support emails by analyzing content and urgency" input do const :email, Email, description: "The email to classify with all headers and content" end output do const :category, EmailCategory, description: "Main topic: technical (API, bugs), billing (payment, pricing), or general" const :priority, Priority, description: "Urgency level based on keywords like 'production', 'ASAP', 'urgent'" const :summary, String, description: "One-line summary of the issue for support dashboard" end end ``` -------------------------------- ### Implement Email Classification with DSPy.rb Chain of Thought Source: https://vicentereig.github.io/dspy.rb/index This code illustrates how to use DSPy.rb's `ChainOfThought` to classify an email based on the previously defined `ClassifyEmail` signature. It creates a `ChainOfThought` instance, constructs a typed `Email` object, and calls the classifier. The output is a typed classification object, ensuring that the returned category, priority, and summary are correctly structured and validated. Examples show accessing the results and demonstrating type safety. ```ruby classifier = DSPy::ChainOfThought.new(ClassifyEmail) # Create a properly typed email object email = Email.new( subject: "URGENT: API Key Not Working!!!", from: "john.doe@acmecorp.com", to: "support@yourcompany.com", body: "My API key stopped working after the update. I need this fixed ASAP for our production deployment!" ) classification = classifier.call(email: email) # Type-checked at runtime! # Example outputs: # irb> classification.reasoning # => "Let me analyze this email step by step:\n1. The customer mentions an API key issue - this is technical\n2. They mention it stopped working after an update - suggests a system change\n3. They emphasize 'ASAP' and 'production deployment' - this is urgent\n4. Production issues always warrant high priority" # # irb> classification.category # => # # # irb> classification.priority.serialize # Get the string value when needed # => "high" # # irb> classification.summary # => "API key authentication failure post-update affecting production" # Type safety examples: # irb> EmailCategory.values # => [#, #, #] # irb> classification.category = "invalid" # This would raise an error! ``` -------------------------------- ### Configure OpenRouter for Multiple Providers in Ruby Source: https://vicentereig.github.io/dspy.rb/index This snippet demonstrates configuring DSPy.rb to access a wide range of models from multiple providers through the OpenRouter API. It requires an OpenRouter API key. ```ruby DSPy.configure do |c| c.lm = DSPy::LM.new( 'openrouter/deepseek/deepseek-chat-v3.1:free', api_key: ENV['OPENROUTER_API_KEY'] ) end ``` -------------------------------- ### Configure Anthropic Claude Language Model in Ruby Source: https://vicentereig.github.io/dspy.rb/index This snippet illustrates how to set up DSPy.rb with Anthropic Claude models, like Claude 3.5 or Claude 4. It requires an Anthropic API key and utilizes tool-based extraction for structured outputs. ```ruby DSPy.configure do |c| c.lm = DSPy::LM.new( 'anthropic/claude-sonnet-4-5-20250929', api_key: ENV['ANTHROPIC_API_KEY'], structured_outputs: true ) end ``` -------------------------------- ### Configure OpenAI Language Model in Ruby Source: https://vicentereig.github.io/dspy.rb/index This snippet shows how to configure DSPy.rb to use OpenAI models like GPT-4o mini. It requires an OpenAI API key and enables structured outputs for better integration. ```ruby DSPy.configure do |c| c.lm = DSPy::LM.new( 'openai/gpt-4o-mini', api_key: ENV['OPENAI_API_KEY'], structured_outputs: true ) end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.