Class: RubyLLM::TranscriptionChunk

Inherits:
Object
  • Object
show all
Includes:
Support::Inspectable
Defined in:
lib/ruby_llm/transcription_chunk.rb

Overview

A TranscriptionChunk is one event from a streaming transcription. RubyLLM.transcribe yields these to its block as the provider transcribes the audio, then returns the final Transcription.

RubyLLM.transcribe("meeting.wav", model: "gpt-4o-transcribe") do |chunk|
print chunk.delta if chunk.delta?
end

Constant Summary collapse

DELTA =

Text deltas, arriving as the model transcribes.

'transcript.text.delta'
PARTIAL =

A tentative transcript that may change before its segment completes.

'transcript.text.partial'
SEGMENT =

A completed segment, on models that return timed or diarized segments.

'transcript.text.segment'
DONE =

The final event, carrying the complete transcript.

'transcript.text.done'

Constants included from Support::Inspectable

Support::Inspectable::TRUNCATE_AT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Inspectable

#full_inspect, #inspect, #pretty_print

Constructor Details

#initialize(type:, delta: nil, text: nil, segment: nil, raw: nil) ⇒ TranscriptionChunk

:nodoc:



44
45
46
47
48
49
50
# File 'lib/ruby_llm/transcription_chunk.rb', line 44

def initialize(type:, delta: nil, text: nil, segment: nil, raw: nil) # :nodoc:
  @type = type
  @delta = delta
  @text = text
  @segment = segment
  @raw = raw
end

Instance Attribute Details

#deltaObject (readonly)

The text added by this event, or nil for events that add no text.



32
33
34
# File 'lib/ruby_llm/transcription_chunk.rb', line 32

def delta
  @delta
end

#rawObject (readonly)

The parsed provider event, for fields RubyLLM does not normalize.



42
43
44
# File 'lib/ruby_llm/transcription_chunk.rb', line 42

def raw
  @raw
end

#segmentObject (readonly)

The segment this event completed as a Hash, or nil. Diarization models label each segment with a speaker.



39
40
41
# File 'lib/ruby_llm/transcription_chunk.rb', line 39

def segment
  @segment
end

#textObject (readonly)

The transcript for a partial event or the complete final transcript.



35
36
37
# File 'lib/ruby_llm/transcription_chunk.rb', line 35

def text
  @text
end

#typeObject (readonly)

The normalized event type, such as "transcript.text.delta".



29
30
31
# File 'lib/ruby_llm/transcription_chunk.rb', line 29

def type
  @type
end

Instance Method Details

#delta?Boolean

Whether this event carries a text delta.

Returns:

  • (Boolean)


53
# File 'lib/ruby_llm/transcription_chunk.rb', line 53

def delta? = !delta.nil?

#done?Boolean

Whether this is the final event of the transcription.

Returns:

  • (Boolean)


62
# File 'lib/ruby_llm/transcription_chunk.rb', line 62

def done? = type == DONE

#inspect_attributesObject

:nodoc:



64
65
66
# File 'lib/ruby_llm/transcription_chunk.rb', line 64

def inspect_attributes # :nodoc:
  { type: type, delta: delta, text: text, segment: segment }
end

#partial?Boolean

Whether this is a tentative transcript, replacing the previous partial.

Returns:

  • (Boolean)


56
# File 'lib/ruby_llm/transcription_chunk.rb', line 56

def partial? = type == PARTIAL

#segment?Boolean

Whether this event completed a segment.

Returns:

  • (Boolean)


59
# File 'lib/ruby_llm/transcription_chunk.rb', line 59

def segment? = !segment.nil?