Class: LLM::OpenAI::Audio
- Inherits:
-
Object
- Object
- LLM::OpenAI::Audio
- Defined in:
- lib/llm/providers/openai/audio.rb
Overview
The LLM::OpenAI::Audio class provides an audio object for interacting with OpenAI’s audio API.
Instance Method Summary collapse
-
#initialize(provider) ⇒ LLM::OpenAI::Responses
constructor
Returns a new Audio object.
-
#create_speech(input:, voice: "alloy", model: "gpt-4o-mini-tts", response_format: "mp3", **params) ⇒ LLM::Response::Audio
Create an audio track.
-
#create_transcription(file:, model: "whisper-1", **params) ⇒ LLM::Response::AudioTranscription
Create an audio transcription.
-
#create_translation(file:, model: "whisper-1", **params) ⇒ LLM::Response::AudioTranslation
Create an audio translation (in English).
Constructor Details
#initialize(provider) ⇒ LLM::OpenAI::Responses
Returns a new Audio object
16 17 18 |
# File 'lib/llm/providers/openai/audio.rb', line 16 def initialize(provider) @provider = provider end |
Instance Method Details
#create_speech(input:, voice: "alloy", model: "gpt-4o-mini-tts", response_format: "mp3", **params) ⇒ LLM::Response::Audio
Create an audio track
34 35 36 37 38 39 40 |
# File 'lib/llm/providers/openai/audio.rb', line 34 def create_speech(input:, voice: "alloy", model: "gpt-4o-mini-tts", response_format: "mp3", **params) req = Net::HTTP::Post.new("/v1/audio/speech", headers) req.body = JSON.dump({input:, voice:, model:, response_format:}.merge!(params)) io = StringIO.new("".b) res = request(http, req) { _1.read_body { |chunk| io << chunk } } LLM::Response::Audio.new(res).tap { _1.audio = io } end |
#create_transcription(file:, model: "whisper-1", **params) ⇒ LLM::Response::AudioTranscription
Create an audio transcription
54 55 56 57 58 59 60 61 |
# File 'lib/llm/providers/openai/audio.rb', line 54 def create_transcription(file:, model: "whisper-1", **params) multi = LLM::Multipart.new(params.merge!(file: LLM.File(file), model:)) req = Net::HTTP::Post.new("/v1/audio/transcriptions", headers) req["content-type"] = multi.content_type set_body_stream(req, multi.body) res = request(http, req) LLM::Response::AudioTranscription.new(res).tap { _1.text = _1.body["text"] } end |
#create_translation(file:, model: "whisper-1", **params) ⇒ LLM::Response::AudioTranslation
Create an audio translation (in English)
76 77 78 79 80 81 82 83 |
# File 'lib/llm/providers/openai/audio.rb', line 76 def create_translation(file:, model: "whisper-1", **params) multi = LLM::Multipart.new(params.merge!(file: LLM.File(file), model:)) req = Net::HTTP::Post.new("/v1/audio/translations", headers) req["content-type"] = multi.content_type set_body_stream(req, multi.body) res = request(http, req) LLM::Response::AudioTranslation.new(res).tap { _1.text = _1.body["text"] } end |