Skip to content

Move audios related methods to Audios.rb from Client.rb #269

New issue

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

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

Already on GitHub? # to your account

Merged
merged 8 commits into from
Aug 14, 2023
Merged
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [5.0.0] - 2023-08-14

### Added

- Support multi-tenant use of the gem! Each client now holds its own config, so you can create unlimited clients in the same project, for example to Azure and OpenAI, or for different headers, access keys, etc.
- [BREAKING-ish] This change should only break your usage of ruby-openai if you are directly calling class methods like `OpenAI::Client.get` for some reason, as they are now instance methods. Normal usage of the gem should be unaffected, just you can make new clients and they'll keep their own config if you want, overriding the global config.
- Huge thanks to [@petergoldstein](https://github.com/petergoldstein) for his original work on this, [@cthulhu](https://github.com/cthulhu) for testing and many others for reviews and suggestions.

### Changed

- [BREAKING] Move audio related method to Audio model from Client model. You will need to update your code to handle this change, changing `client.translate` to `client.audio.translate` and `client.transcribe` to `client.audio.transcribe`.

## [4.3.2] - 2023-08-14

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
ruby-openai (4.3.2)
ruby-openai (5.0.0)
faraday (>= 1)
faraday-multipart (>= 1)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ Whisper is a speech to text model that can be used to generate text based on aud
The translations API takes as input the audio file in any of the supported languages and transcribes the audio into English.

```ruby
response = client.translate(
response = client.audio.translate(
parameters: {
model: "whisper-1",
file: File.open("path_to_file", "rb"),
Expand All @@ -431,7 +431,7 @@ puts response["text"]
The transcriptions API takes as input the audio file you want to transcribe and returns the text in the desired output file format.

```ruby
response = client.transcribe(
response = client.audio.transcribe(
parameters: {
model: "whisper-1",
file: File.open("path_to_file", "rb"),
Expand Down
1 change: 1 addition & 0 deletions lib/openai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative "openai/finetunes"
require_relative "openai/images"
require_relative "openai/models"
require_relative "openai/audio"
require_relative "openai/version"

module OpenAI
Expand Down
15 changes: 15 additions & 0 deletions lib/openai/audio.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module OpenAI
class Audio
def initialize(client:)
@client = client
end

def transcribe(parameters: {})
@client.multipart_post(path: "/audio/transcriptions", parameters: parameters)
end

def translate(parameters: {})
@client.multipart_post(path: "/audio/translations", parameters: parameters)
end
end
end
12 changes: 4 additions & 8 deletions lib/openai/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def embeddings(parameters: {})
json_post(path: "/embeddings", parameters: parameters)
end

def audio
@audio ||= OpenAI::Audio.new(client: self)
end

def files
@files ||= OpenAI::Files.new(client: self)
end
Expand All @@ -57,14 +61,6 @@ def moderations(parameters: {})
json_post(path: "/moderations", parameters: parameters)
end

def transcribe(parameters: {})
multipart_post(path: "/audio/transcriptions", parameters: parameters)
end

def translate(parameters: {})
multipart_post(path: "/audio/translations", parameters: parameters)
end

def azure?
@api_type&.to_sym == :azure
end
Expand Down
2 changes: 1 addition & 1 deletion lib/openai/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module OpenAI
VERSION = "4.3.2".freeze
VERSION = "5.0.0".freeze
end

Large diffs are not rendered by default.

Large diffs are not rendered by default.

80 changes: 41 additions & 39 deletions spec/openai/client/audio_spec.rb
Original file line number Diff line number Diff line change
@@ -1,54 +1,56 @@
RSpec.describe OpenAI::Client do
describe "#transcribe" do
context "with audio", :vcr do
let(:filename) { "audio_sample.mp3" }
let(:audio) { File.join(RSPEC_ROOT, "fixtures/files", filename) }
describe "#audio" do
describe "#transcribe" do
context "with audio", :vcr do
let(:filename) { "audio_sample.mp3" }
let(:audio) { File.join(RSPEC_ROOT, "fixtures/files", filename) }

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

context "with model: whisper-1" do
let(:model) { "whisper-1" }
context "with model: whisper-1" do
let(:model) { "whisper-1" }

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

describe "#translate" do
context "with audio", :vcr do
let(:filename) { "audio_sample.mp3" }
let(:audio) { File.join(RSPEC_ROOT, "fixtures/files", filename) }
describe "#translate" do
context "with audio", :vcr do
let(:filename) { "audio_sample.mp3" }
let(:audio) { File.join(RSPEC_ROOT, "fixtures/files", filename) }

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

context "with model: whisper-1" do
let(:model) { "whisper-1" }
context "with model: whisper-1" do
let(:model) { "whisper-1" }

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