Skip to content

Commit

Permalink
Adds the audio Speech API
Browse files Browse the repository at this point in the history
* Adds a simple speech API testcase
* Adds a small documentation section
  • Loading branch information
codergeek121 committed Nov 14, 2023
1 parent 254779d commit e2ecdeb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,22 @@ puts response["text"]
# => "Transcription of the text"
```

#### Speech

The speech API takes as input the text and a voice and returns the content of an audio file you can listen to.

```ruby
response = client.audio.speech(
parameters: {
model: "tts-1",
input: "This is a speech test!",
voice: "alloy"
}
)
File.binwrite('demo.mp3', response)
# => mp3 file that plays: "This is a speech test!"
```

### Errors

HTTP errors can be caught like this:
Expand Down
4 changes: 4 additions & 0 deletions lib/openai/audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ def transcribe(parameters: {})
def translate(parameters: {})
@client.multipart_post(path: "/audio/translations", parameters: parameters)
end

def speech(parameters: {})
@client.json_post(path: "/audio/speech", parameters: parameters)
end
end
end
38 changes: 38 additions & 0 deletions spec/fixtures/cassettes/speech_tts-1_test.yml

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions spec/openai/client/audio_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,24 @@
end
end
end

describe "#speech" do
context "with audio", :vcr do
let(:model) { "tts-1" }

it "returns a working mp3 file as body" do
VCR.use_cassette("speech #{model} test") do
response = OpenAI::Client.new.audio.speech(
parameters: {
model: model,
input: "This is a speech test!",
voice: "alloy"
}
)
expect(response).not_to be_empty
end
end
end
end
end
end

0 comments on commit e2ecdeb

Please sign in to comment.