Module: RubyLLM::Pollinations::Provider::Audio

Included in:
Pollinations
Defined in:
lib/ruby_llm/pollinations/provider/audio.rb

Constant Summary collapse

MUSIC_MODELS =
%w[elevenmusic music].freeze
DEFAULT_VOICE =
'alloy'
DEFAULT_FORMAT =
'mp3'
DEFAULT_SPEED =
1.0
MAX_INPUT_LENGTH =
4096
VOICE_ENUM =
%w[
  alloy echo fable onyx shimmer coral verse ballad ash sage amuch dan
  rachel bella
].freeze
FORMAT_ENUM =
%w[mp3 opus aac flac wav pcm].freeze

Class Method Summary collapse

Class Method Details

.add_music_options(payload, model, options) ⇒ Object



50
51
52
53
54
55
# File 'lib/ruby_llm/pollinations/provider/audio.rb', line 50

def add_music_options(payload, model, options)
  return unless music_model?(model)

  payload[:duration] = options[:duration] if options[:duration]
  payload[:instrumental] = options[:instrumental] if options.key?(:instrumental)
end

.build_speech_payload(input, model, voice, options) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ruby_llm/pollinations/provider/audio.rb', line 38

def build_speech_payload(input, model, voice, options)
  payload = {
    input: input,
    model: model,
    voice: voice || DEFAULT_VOICE,
    response_format: options[:response_format] || DEFAULT_FORMAT,
    speed: options[:speed] || DEFAULT_SPEED
  }
  add_music_options(payload, model, options)
  payload.compact
end

.music_model?(model) ⇒ Boolean



69
70
71
72
73
# File 'lib/ruby_llm/pollinations/provider/audio.rb', line 69

def music_model?(model)
  return false unless model

  MUSIC_MODELS.include?(model.to_s.downcase)
end

.parse_speech_response(response, model:) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ruby_llm/pollinations/provider/audio.rb', line 57

def parse_speech_response(response, model:)
  content_type = response.headers['content-type'] || ''
  mime_type = content_type.empty? ? 'audio/mpeg' : content_type.split(';').first.strip
  data = Base64.strict_encode64(response.body)

  RubyLLM::Pollinations::AudioOutput.new(
    data: data,
    mime_type: mime_type,
    model_id: model
  )
end

.render_speech_payload(input, model:, voice:, **options) ⇒ Object



26
27
28
29
# File 'lib/ruby_llm/pollinations/provider/audio.rb', line 26

def render_speech_payload(input, model:, voice:, **options)
  validate_speech_input!(input)
  build_speech_payload(input, model, voice, options)
end

.speech_url ⇒ Object



22
23
24
# File 'lib/ruby_llm/pollinations/provider/audio.rb', line 22

def speech_url
  'v1/audio/speech'
end

.validate_speech_input!(input) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
# File 'lib/ruby_llm/pollinations/provider/audio.rb', line 31

def validate_speech_input!(input)
  raise ArgumentError, 'Input text is required' if input.nil? || input.to_s.empty?
  return unless input.length > MAX_INPUT_LENGTH

  raise ArgumentError, "Input exceeds maximum length of #{MAX_INPUT_LENGTH} characters"
end