Class: RubyLlmAgents::SpeakerGenerator

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

Overview

Speaker generator for creating new text-to-speech speakers

Usage:

rails generate ruby_llm_agents:speaker Narrator
rails generate ruby_llm_agents:speaker Narrator --provider elevenlabs
rails generate ruby_llm_agents:speaker Narrator --voice alloy --speed 1.25

This will create:

- app/agents/audio/narrator_speaker.rb

Instance Method Summary collapse

Instance Method Details

#create_speaker_fileObject



51
52
53
54
55
# File 'lib/generators/ruby_llm_agents/speaker_generator.rb', line 51

def create_speaker_file
  # Support nested paths: "article/narrator" -> "app/agents/audio/article/narrator_speaker.rb"
  speaker_path = name.underscore
  template "speaker.rb.tt", "app/agents/audio/#{speaker_path}_speaker.rb"
end

#ensure_base_class_and_skill_fileObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/generators/ruby_llm_agents/speaker_generator.rb', line 32

def ensure_base_class_and_skill_file
  audio_dir = "app/agents/audio"

  # Create directory if needed
  empty_directory audio_dir

  # Create base class if it doesn't exist
  base_class_path = "#{audio_dir}/application_speaker.rb"
  unless File.exist?(File.join(destination_root, base_class_path))
    template "application_speaker.rb.tt", base_class_path
  end

  # Create skill file if it doesn't exist
  skill_file_path = "#{audio_dir}/SPEAKERS.md"
  unless File.exist?(File.join(destination_root, skill_file_path))
    template "skills/SPEAKERS.md.tt", skill_file_path
  end
end

#show_usageObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/ruby_llm_agents/speaker_generator.rb', line 57

def show_usage
  # Build full class name from path
  speaker_class_name = name.split("/").map(&:camelize).join("::")
  full_class_name = "Audio::#{speaker_class_name}Speaker"
  say ""
  say "Speaker #{full_class_name} created!", :green
  say ""
  say "Usage:"
  say "  # Generate speech"
  say "  result = #{full_class_name}.call(text: \"Hello world\")"
  say "  result.audio  # => Binary audio data"
  say ""
  say "  # Save to file"
  say "  result.save_to(\"output.mp3\")"
  say ""
  say "  # Stream audio"
  say "  #{full_class_name}.stream(text: \"Long article...\") do |chunk|"
  say "    audio_player.play(chunk.audio)"
  say "  end"
  say ""
end