Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0ec5a9e

Browse filesBrowse files
add support to audio/createSpeech API (TheoKanning#392)
1 parent f1e587d commit 0ec5a9e
Copy full SHA for 0ec5a9e

File tree

Expand file treeCollapse file tree

4 files changed

+73
-0
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+73
-0
lines changed
+45Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.theokanning.openai.audio;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
import lombok.NonNull;
10+
11+
@Builder
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
@Data
15+
public class CreateSpeechRequest {
16+
17+
/**
18+
* The name of the model to use.
19+
*/
20+
@NonNull
21+
String model;
22+
23+
/**
24+
* The text to generate audio for. The maximum length is 4096 characters.
25+
*/
26+
@NonNull
27+
String input;
28+
29+
/**
30+
* The voice to use when generating the audio.
31+
*/
32+
@NonNull
33+
String voice;
34+
35+
/**
36+
* The format to audio in. Supported formats are mp3, opus, aac, and flac. Defaults to mp3.
37+
*/
38+
@JsonProperty("response_format")
39+
String responseFormat;
40+
41+
/**
42+
* The speed of the generated audio. Select a value from 0.25 to 4.0. Defaults to 1.0.
43+
*/
44+
Double speed;
45+
}

‎client/src/main/java/com/theokanning/openai/client/OpenAiApi.java

Copy file name to clipboardExpand all lines: client/src/main/java/com/theokanning/openai/client/OpenAiApi.java
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.theokanning.openai.DeleteResult;
44
import com.theokanning.openai.OpenAiResponse;
5+
import com.theokanning.openai.audio.CreateSpeechRequest;
56
import com.theokanning.openai.audio.TranscriptionResult;
67
import com.theokanning.openai.audio.TranslationResult;
78
import com.theokanning.openai.billing.BillingUsage;
@@ -149,6 +150,9 @@ public interface OpenAiApi {
149150
@POST("/v1/audio/translations")
150151
Single<TranslationResult> createTranslation(@Body RequestBody requestBody);
151152

153+
@POST("/v1/audio/speech")
154+
Single<ResponseBody> createSpeech(@Body CreateSpeechRequest requestBody);
155+
152156
@POST("/v1/moderations")
153157
Single<ModerationResult> createModeration(@Body ModerationRequest request);
154158

‎service/src/main/java/com/theokanning/openai/service/OpenAiService.java

Copy file name to clipboardExpand all lines: service/src/main/java/com/theokanning/openai/service/OpenAiService.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.theokanning.openai.DeleteResult;
99
import com.theokanning.openai.OpenAiError;
1010
import com.theokanning.openai.OpenAiHttpException;
11+
import com.theokanning.openai.audio.CreateSpeechRequest;
1112
import com.theokanning.openai.audio.CreateTranscriptionRequest;
1213
import com.theokanning.openai.audio.CreateTranslationRequest;
1314
import com.theokanning.openai.audio.TranscriptionResult;
@@ -347,6 +348,10 @@ public ModerationResult createModeration(ModerationRequest request) {
347348
return execute(api.createModeration(request));
348349
}
349350

351+
public ResponseBody createSpeech(CreateSpeechRequest request) {
352+
return execute(api.createSpeech(request));
353+
}
354+
350355
/**
351356
* Calls the Open AI api, returns the response, and parses error messages if the request fails
352357
*/

‎service/src/test/java/com/theokanning/openai/service/AudioTest.java

Copy file name to clipboardExpand all lines: service/src/test/java/com/theokanning/openai/service/AudioTest.java
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.theokanning.openai.service;
22

3+
import com.theokanning.openai.audio.CreateSpeechRequest;
34
import com.theokanning.openai.audio.CreateTranscriptionRequest;
45
import com.theokanning.openai.audio.CreateTranslationRequest;
56
import com.theokanning.openai.audio.TranscriptionResult;
67
import com.theokanning.openai.audio.TranslationResult;
78
import org.junit.jupiter.api.Test;
89

10+
import java.io.IOException;
911
import java.time.Duration;
1012

13+
import okhttp3.MediaType;
14+
import okhttp3.ResponseBody;
15+
1116
import static org.junit.jupiter.api.Assertions.*;
1217

1318

@@ -69,4 +74,18 @@ void createTranslationVerbose() {
6974
assertTrue(result.getDuration() > 0);
7075
assertEquals(1, result.getSegments().size());
7176
}
77+
78+
@Test
79+
void createSpeech() throws IOException {
80+
CreateSpeechRequest createSpeechRequest = CreateSpeechRequest.builder()
81+
.model("tts-1")
82+
.input("Hello World.")
83+
.voice("alloy")
84+
.build();
85+
86+
final ResponseBody speech = service.createSpeech(createSpeechRequest);
87+
assertNotNull(speech);
88+
assertEquals(MediaType.get("audio/mpeg"), speech.contentType());
89+
assertTrue(speech.bytes().length > 0);
90+
}
7291
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.