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

Support all audio response formats #358

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ public interface OpenAiApi {
Single<ImageResult> createImageVariation(@Body RequestBody requestBody);

@POST("/v1/audio/transcriptions")
Single<TranscriptionResult> createTranscription(@Body RequestBody requestBody);
Single<ResponseBody> createTranscription(@Body RequestBody requestBody);

@POST("/v1/audio/translations")
Single<TranslationResult> createTranslation(@Body RequestBody requestBody);
Single<ResponseBody> createTranslation(@Body RequestBody requestBody);

@POST("/v1/audio/speech")
Single<ResponseBody> createSpeech(@Body CreateSpeechRequest requestBody);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,25 @@ public TranscriptionResult createTranscription(CreateTranscriptionRequest reques
builder.addFormDataPart("language", request.getLanguage());
}

return execute(api.createTranscription(builder.build()));
try (ResponseBody responseBody = execute(api.createTranscription(builder.build()))) {
final String responseFormat = request.getResponseFormat() == null ? "json" : request.getResponseFormat();
switch (responseFormat) {
case "json":
case "verbose_json":
final ObjectMapper objectMapper = defaultObjectMapper();
return objectMapper.readValue(responseBody.string(), TranscriptionResult.class);
case "text":
case "vtt":
case "srt":
final TranscriptionResult result = new TranscriptionResult();
result.setText(responseBody.string());
return result;
default:
throw new IllegalArgumentException("Unknown response format: " + responseFormat);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

public TranslationResult createTranslation(CreateTranslationRequest request, String audioPath) {
Expand All @@ -356,7 +374,25 @@ public TranslationResult createTranslation(CreateTranslationRequest request, jav
builder.addFormDataPart("temperature", request.getTemperature().toString());
}

return execute(api.createTranslation(builder.build()));
try (ResponseBody responseBody = execute(api.createTranslation(builder.build()))) {
final String responseFormat = request.getResponseFormat() == null ? "json" : request.getResponseFormat();
switch (responseFormat) {
case "json":
case "verbose_json":
final ObjectMapper objectMapper = defaultObjectMapper();
return objectMapper.readValue(responseBody.string(), TranslationResult.class);
case "text":
case "vtt":
case "srt":
final TranslationResult result = new TranslationResult();
result.setText(responseBody.string());
return result;
default:
throw new IllegalArgumentException("Unknown response format: " + responseFormat);
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

public ModerationResult createModeration(ModerationRequest request) {
Expand Down