Module: RubyLLM::Providers::OpenAI::Transcription
- Included in:
- RubyLLM::Providers::OpenAI
- Defined in:
- lib/ruby_llm/providers/openai/transcription.rb
Overview
Audio transcription methods for the OpenAI API integration
Class Method Summary collapse
- .encode_speaker_references(references) ⇒ Object
- .parse_transcription_response(response, model:) ⇒ Object
- .render_transcription_payload(file_part, model:, language:, **options) ⇒ Object
- .response_format_for(model, options) ⇒ Object
- .supports_chunking_strategy?(model, options) ⇒ Boolean
- .transcription_url ⇒ Object
Class Method Details
.encode_speaker_references(references) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/ruby_llm/providers/openai/transcription.rb', line 29 def encode_speaker_references(references) return nil unless references references.map do |ref| Attachment.new(ref).for_llm end end |
.parse_transcription_response(response, model:) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ruby_llm/providers/openai/transcription.rb', line 50 def parse_transcription_response(response, model:) data = response.body return RubyLLM::Transcription.new(text: data, model: model) if data.is_a?(String) usage = data['usage'] || {} RubyLLM::Transcription.new( text: data['text'], model: model, language: data['language'], duration: data['duration'], segments: data['segments'], input_tokens: usage['input_tokens'] || usage['prompt_tokens'], output_tokens: usage['output_tokens'] || usage['completion_tokens'] ) end |
.render_transcription_payload(file_part, model:, language:, **options) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/ruby_llm/providers/openai/transcription.rb', line 14 def render_transcription_payload(file_part, model:, language:, **) { model: model, file: file_part, language: language, chunking_strategy: ([:chunking_strategy] || 'auto' if supports_chunking_strategy?(model, )), response_format: response_format_for(model, ), prompt: [:prompt], temperature: [:temperature], timestamp_granularities: [:timestamp_granularities], known_speaker_names: [:speaker_names], known_speaker_references: encode_speaker_references([:speaker_references]) }.compact end |
.response_format_for(model, options) ⇒ Object
37 38 39 40 41 |
# File 'lib/ruby_llm/providers/openai/transcription.rb', line 37 def response_format_for(model, ) return [:response_format] if .key?(:response_format) 'diarized_json' if model.include?('diarize') end |
.supports_chunking_strategy?(model, options) ⇒ Boolean
43 44 45 46 47 48 |
# File 'lib/ruby_llm/providers/openai/transcription.rb', line 43 def supports_chunking_strategy?(model, ) return false if model.start_with?('whisper') return true if .key?(:chunking_strategy) model.include?('diarize') end |
.transcription_url ⇒ Object
10 11 12 |
# File 'lib/ruby_llm/providers/openai/transcription.rb', line 10 def transcription_url 'audio/transcriptions' end |