Module: RubyLLM::Providers::Mistral::Transcription

Included in:
ChatCompletions
Defined in:
lib/ruby_llm/providers/mistral/transcription.rb

Overview

Transcription dialect for the Mistral audio API. Giving speaker names turns on diarization, which labels each segment with a speaker id; a prompt rides along as context biasing terms.

Constant Summary collapse

STREAM_TYPES =
{
  'transcription.text.delta' => RubyLLM::TranscriptionChunk::DELTA,
  'transcription.segment' => RubyLLM::TranscriptionChunk::SEGMENT,
  'transcription.done' => RubyLLM::TranscriptionChunk::DONE
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build_transcription_chunk(data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_llm/providers/mistral/transcription.rb', line 25

def build_transcription_chunk(data)
  type = STREAM_TYPES[data['type']]
  return super unless type

  RubyLLM::TranscriptionChunk.new(
    type: type,
    delta: (data['text'] if type == RubyLLM::TranscriptionChunk::DELTA),
    text: (data['text'] if type == RubyLLM::TranscriptionChunk::DONE),
    segment: (data.except('type') if type == RubyLLM::TranscriptionChunk::SEGMENT),
    raw: data
  )
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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_llm/providers/mistral/transcription.rb', line 43

def render_transcription_payload(file_part, model:, language:, format: nil, speaker_names: nil,
                                 speaker_references: nil, provider_options: {}, prompt: nil,
                                 temperature: nil)
  payload = {
    model: model,
    file: file_part,
    language: language,
    temperature: temperature,
    context_bias: prompt ? Array(prompt) : nil
  }.compact
  if speaker_names
    payload[:diarize] = true
    payload[:timestamp_granularities] = ['segment']
  end
  payload.merge(provider_options)
end

.transcription_duration(usage) ⇒ Object



38
39
40
# File 'lib/ruby_llm/providers/mistral/transcription.rb', line 38

def transcription_duration(usage)
  usage['prompt_audio_seconds'] || super
end

Instance Method Details

#render_transcription_options(timestamps:) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
# File 'lib/ruby_llm/providers/mistral/transcription.rb', line 16

def render_transcription_options(timestamps:, **)
  return {} if timestamps.nil?
  raise ArgumentError, 'Mistral transcription timestamps must be segment' unless timestamps == :segment

  { timestamp_granularities: ['segment'] }
end