Skip to content

Commit 6f51f9d

Browse files
authored
Merge pull request #360 from codergeek121/main
Adds the audio Speech API
2 parents 254779d + e2ecdeb commit 6f51f9d

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,22 @@ puts response["text"]
460460
# => "Transcription of the text"
461461
```
462462

463+
#### Speech
464+
465+
The speech API takes as input the text and a voice and returns the content of an audio file you can listen to.
466+
467+
```ruby
468+
response = client.audio.speech(
469+
parameters: {
470+
model: "tts-1",
471+
input: "This is a speech test!",
472+
voice: "alloy"
473+
}
474+
)
475+
File.binwrite('demo.mp3', response)
476+
# => mp3 file that plays: "This is a speech test!"
477+
```
478+
463479
### Errors
464480

465481
HTTP errors can be caught like this:

lib/openai/audio.rb

+4
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ def transcribe(parameters: {})
1111
def translate(parameters: {})
1212
@client.multipart_post(path: "/audio/translations", parameters: parameters)
1313
end
14+
15+
def speech(parameters: {})
16+
@client.json_post(path: "/audio/speech", parameters: parameters)
17+
end
1418
end
1519
end

spec/fixtures/cassettes/speech_tts-1_test.yml

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/openai/client/audio_spec.rb

+19
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,24 @@
5555
end
5656
end
5757
end
58+
59+
describe "#speech" do
60+
context "with audio", :vcr do
61+
let(:model) { "tts-1" }
62+
63+
it "returns a working mp3 file as body" do
64+
VCR.use_cassette("speech #{model} test") do
65+
response = OpenAI::Client.new.audio.speech(
66+
parameters: {
67+
model: model,
68+
input: "This is a speech test!",
69+
voice: "alloy"
70+
}
71+
)
72+
expect(response).not_to be_empty
73+
end
74+
end
75+
end
76+
end
5877
end
5978
end

0 commit comments

Comments
 (0)