Module: RubyLLM::Protocols::ElevenLabs::Transcription

Defined in:
lib/ruby_llm/protocols/elevenlabs/transcription.rb

Overview

Transcription dialect for the ElevenLabs speech-to-text API. The request is multipart with the model id in the form, and giving speaker names turns on diarization and caps the speaker count at the number of names.

Instance Method Summary collapse

Instance Method Details

#parse_transcription_response(response, model:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ruby_llm/protocols/elevenlabs/transcription.rb', line 47

def parse_transcription_response(response, model:)
  data = response.body

  RubyLLM::Transcription.new(
    text: data['text'],
    model: model,
    language: data['language_code'],
    duration: data['audio_duration_secs'],
    words: data['words']
  )
end

#render_transcription_options(timestamps:, format:, streaming:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ruby_llm/protocols/elevenlabs/transcription.rb', line 11

def render_transcription_options(timestamps:, format:, streaming:)
  return {} if timestamps.nil?

  value = timestamps.to_s
  allowed = streaming ? ['word'] : %w[none word character]
  unless allowed.include?(value) && (format.nil? || format == value)
    raise ArgumentError, "ElevenLabs timestamps must be #{allowed.join(', ')} and match format when provided"
  end

  streaming ? { include_timestamps: true } : { timestamps_granularity: value }
end

#render_transcription_payload(file_part, model:, language:, format: nil, speaker_names: nil, speaker_references: nil, provider_options: {}, prompt: nil, temperature: nil) ⇒ Object

rubocop:disable-next Lint/UnusedMethodArgument



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_llm/protocols/elevenlabs/transcription.rb', line 24

def render_transcription_payload(file_part, model:, language:, format: nil, speaker_names: nil,
                                 speaker_references: nil, provider_options: {}, prompt: nil,
                                 temperature: nil)
  payload = {
    model_id: model,
    file: file_part,
    language_code: language,
    temperature: temperature,
    timestamps_granularity: format
  }.compact

  if speaker_names
    payload[:diarize] = true
    payload[:num_speakers] = speaker_names.size
  end

  payload.merge(provider_options)
end

#transcription_urlObject



43
44
45
# File 'lib/ruby_llm/protocols/elevenlabs/transcription.rb', line 43

def transcription_url
  'v1/speech-to-text'
end