Class: RubyLlmAgents::DemoGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/ruby_llm_agents/demo_generator.rb

Overview

Demo generator — scaffolds a working HelloAgent with a smoke-test script.

Usage:

rails generate ruby_llm_agents:demo

Creates:

- app/agents/hello_agent.rb        — minimal working agent
- bin/smoke_test_agent             — one-command verification script

Instance Method Summary collapse

Instance Method Details

#create_hello_agentObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/ruby_llm_agents/demo_generator.rb', line 28

def create_hello_agent
  create_file "app/agents/hello_agent.rb", <<~RUBY
    # frozen_string_literal: true

    class HelloAgent < ApplicationAgent
      system "You are a friendly assistant. Keep responses under 2 sentences."

      prompt "Say hello to {name} and tell them one fun fact."
    end
  RUBY
end

#create_smoke_testObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/generators/ruby_llm_agents/demo_generator.rb', line 40

def create_smoke_test
  create_file "bin/smoke_test_agent", <<~RUBY
    #!/usr/bin/env ruby
    # frozen_string_literal: true

    # Smoke test — verifies your RubyLLM::Agents setup end-to-end.
    #
    # Usage:
    #   bin/rails runner bin/smoke_test_agent
    #
    puts "Running RubyLLM::Agents smoke test..."
    puts ""

    # 1. Check configuration
    config = RubyLLM::Agents.configuration
    model = config.default_model
    puts "Default model: \#{model}"

    # 2. Dry-run (no API call)
    puts ""
    puts "Dry run:"
    dry = HelloAgent.call(name: "World", dry_run: true)
    puts "  System prompt: \#{dry.system_prompt[0..80]}..."
    puts "  User prompt:   \#{dry.user_prompt}"
    puts "  Model:         \#{dry.model}"
    puts "  Dry run OK!"

    # 3. Live call
    puts ""
    puts "Live call (calling \#{model})..."
    begin
      result = HelloAgent.call(name: "World")
      puts "  Response: \#{result.content}"
      puts ""
      puts "Success! Your setup is working."
    rescue => e
      puts "  Error: \#{e.class}: \#{e.message}"
      puts ""
      puts "The dry run worked but the live call failed."
      puts "This usually means your API key is missing or invalid."
      puts ""
      puts "Run 'rails ruby_llm_agents:doctor' for detailed diagnostics."
      exit 1
    end
  RUBY

  chmod "bin/smoke_test_agent", 0o755
end

#ensure_base_classObject



18
19
20
21
22
23
24
25
26
# File 'lib/generators/ruby_llm_agents/demo_generator.rb', line 18

def ensure_base_class
  agents_dir = "app/agents"
  empty_directory agents_dir

  base_class_path = "#{agents_dir}/application_agent.rb"
  unless File.exist?(File.join(destination_root, base_class_path))
    template "application_agent.rb.tt", base_class_path
  end
end

#show_next_stepsObject



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/generators/ruby_llm_agents/demo_generator.rb', line 89

def show_next_steps
  say ""
  say "Demo agent created!", :green
  say ""
  say "Try it:"
  say "  bin/rails runner bin/smoke_test_agent"
  say ""
  say "Or in the Rails console:"
  say "  HelloAgent.call(name: \"World\")"
  say "  HelloAgent.call(name: \"World\", dry_run: true)"
  say ""
end