Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: vtt transcribe not working issue #540

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/openai/audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ def initialize(client:)
end

def transcribe(parameters: {})
@client.multipart_post(path: "/audio/transcriptions", parameters: parameters)
plain_text_response = parameters[:response_format] == "vtt"
@client.multipart_post(path: "/audio/transcriptions", parameters: parameters,
plain_text_response: plain_text_response)
end

def translate(parameters: {})
Expand Down
8 changes: 4 additions & 4 deletions lib/openai/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def json_post(path:, parameters:, query_parameters: {})
end&.body
end

def multipart_post(path:, parameters: nil)
conn(multipart: true).post(uri(path: path)) do |req|
def multipart_post(path:, parameters: nil, plain_text_response: false)
conn(multipart: true, plain_text: plain_text_response).post(uri(path: path)) do |req|
req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
req.body = multipart_parameters(parameters)
end&.body
Expand Down Expand Up @@ -71,13 +71,13 @@ def to_json_stream(user_proc:)
end
end

def conn(multipart: false)
def conn(multipart: false, plain_text: false)
connection = Faraday.new do |f|
f.options[:timeout] = @request_timeout
f.request(:multipart) if multipart
f.use MiddlewareErrors if @log_errors
f.response :raise_error
f.response :json
f.response :json unless plain_text
end

@faraday_middleware&.call(connection)
Expand Down

Large diffs are not rendered by default.

33 changes: 27 additions & 6 deletions spec/openai/client/audio_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,44 @@

let(:response) do
OpenAI::Client.new.audio.transcribe(
parameters: {
model: model,
file: File.open(audio, "rb")
}
parameters: parameters
)
end
let(:content) { response["text"] }
let(:cassette) { "audio #{model} transcribe".downcase }
let(:parameters) do
{
model: model,
file: File.open(audio, "rb")
}
end

context "with model: whisper-1" do
let(:model) { "whisper-1" }
let(:cassette) { "audio #{model} transcribe".downcase }
let(:content) { response["text"] }

it "succeeds" do
VCR.use_cassette(cassette) do
expect(content.empty?).to eq(false)
end
end

context "with response_format: 'vtt'" do
let(:parameters) do
{
model: model,
file: File.open(audio, "rb"),
response_format: response_format
}
end
let(:response_format) { "vtt" }
let(:cassette) { "audio #{model} transcribe with #{response_format} format".downcase }

it "succeeds" do
VCR.use_cassette(cassette) do
expect(response).to include("WEBVTT")
end
end
end
end
end
end
Expand Down