Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
add support to audio/createSpeech API
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfariati committed Nov 8, 2023
1 parent dc51a5b commit 39814c1
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.theokanning.openai.audio;

import com.fasterxml.jackson.annotation.JsonProperty;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class CreateSpeechRequest {

/**
* The name of the model to use.
*/
@NonNull
String model;

/**
* The text to generate audio for. The maximum length is 4096 characters.
*/
@NonNull
String input;

/**
* The voice to use when generating the audio.
*/
@NonNull
String voice;

/**
* The format to audio in. Supported formats are mp3, opus, aac, and flac. Defaults to mp3.
*/
@JsonProperty("response_format")
String responseFormat;

/**
* The speed of the generated audio. Select a value from 0.25 to 4.0. Defaults to 1.0.
*/
Double speed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.theokanning.openai.DeleteResult;
import com.theokanning.openai.OpenAiResponse;
import com.theokanning.openai.audio.CreateSpeechRequest;
import com.theokanning.openai.audio.TranscriptionResult;
import com.theokanning.openai.audio.TranslationResult;
import com.theokanning.openai.billing.BillingUsage;
Expand Down Expand Up @@ -149,6 +150,9 @@ public interface OpenAiApi {
@POST("/v1/audio/translations")
Single<TranslationResult> createTranslation(@Body RequestBody requestBody);

@POST("/v1/audio/speech")
Single<ResponseBody> createSpeech(@Body CreateSpeechRequest requestBody);

@POST("/v1/moderations")
Single<ModerationResult> createModeration(@Body ModerationRequest request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.theokanning.openai.DeleteResult;
import com.theokanning.openai.OpenAiError;
import com.theokanning.openai.OpenAiHttpException;
import com.theokanning.openai.audio.CreateSpeechRequest;
import com.theokanning.openai.audio.CreateTranscriptionRequest;
import com.theokanning.openai.audio.CreateTranslationRequest;
import com.theokanning.openai.audio.TranscriptionResult;
Expand Down Expand Up @@ -345,6 +346,10 @@ public ModerationResult createModeration(ModerationRequest request) {
return execute(api.createModeration(request));
}

public ResponseBody createSpeech(CreateSpeechRequest request) {
return execute(api.createSpeech(request));
}

/**
* Calls the Open AI api, returns the response, and parses error messages if the request fails
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.theokanning.openai.service;

import com.theokanning.openai.audio.CreateSpeechRequest;
import com.theokanning.openai.audio.CreateTranscriptionRequest;
import com.theokanning.openai.audio.CreateTranslationRequest;
import com.theokanning.openai.audio.TranscriptionResult;
import com.theokanning.openai.audio.TranslationResult;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.time.Duration;

import okhttp3.MediaType;
import okhttp3.ResponseBody;

import static org.junit.jupiter.api.Assertions.*;


Expand Down Expand Up @@ -69,4 +74,18 @@ void createTranslationVerbose() {
assertTrue(result.getDuration() > 0);
assertEquals(1, result.getSegments().size());
}

@Test
void createSpeech() throws IOException {
CreateSpeechRequest createSpeechRequest = CreateSpeechRequest.builder()
.model("tts-1")
.input("Hello World.")
.voice("alloy")
.build();

final ResponseBody speech = service.createSpeech(createSpeechRequest);
assertNotNull(speech);
assertEquals(MediaType.get("audio/mpeg"), speech.contentType());
assertTrue(speech.bytes().length > 0);
}
}

0 comments on commit 39814c1

Please sign in to comment.