This repository was archived by the owner on Feb 11, 2025. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 57
[FEATURE] Support Speech OpenAI Audio API #24
Open
KaisNeffati wants to merge 2 commits into ai-for-java:main Choose a base branch from KaisNeffati:feature/audio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits Select commit Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions 48 src/main/java/dev/ai4j/openai4j/BytesConverterFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package dev.ai4j.openai4j; | ||
| | ||
| import dev.ai4j.openai4j.audio.GenerateSpeechResponse; | ||
| import okhttp3.ResponseBody; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import retrofit2.Converter; | ||
| import retrofit2.Retrofit; | ||
| | ||
| import java.io.IOException; | ||
| import java.lang.annotation.Annotation; | ||
| import java.lang.reflect.Type; | ||
| | ||
| /** | ||
| * A converter factory to handle the conversion of Retrofit response bodies into | ||
| * ByteArrayObjectWrapper instances. | ||
| */ | ||
| public class BytesConverterFactory extends Converter.Factory { | ||
| | ||
| private static final Logger logger = LoggerFactory.getLogger(BytesConverterFactory.class); | ||
| | ||
| public BytesConverterFactory() { | ||
| // Constructor can be utilized for initializing if needed | ||
| } | ||
| | ||
| @Override | ||
| public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) { | ||
| | ||
| logger.debug("Requesting conversion for type: {}", type.getTypeName()); | ||
| if (GenerateSpeechResponse.class.equals(type)) { | ||
| return responseBody -> { | ||
| try { | ||
| logger.debug("Converting response body to GenerateSpeechResponse"); | ||
| return GenerateSpeechResponse.builder() | ||
| .data(responseBody.bytes()) | ||
| .build(); | ||
| } catch (IOException e) { | ||
| logger.error("Failed to read bytes from response body", e); | ||
| throw new RuntimeException("Error reading response body", e); | ||
| } finally { | ||
| responseBody.close(); | ||
| } | ||
| }; | ||
| } | ||
| logger.debug("No converter found for type: {}", type.getTypeName()); | ||
| return null; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions 109 src/main/java/dev/ai4j/openai4j/audio/GenerateSpeechRequest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package dev.ai4j.openai4j.audio; | ||
| | ||
| import java.util.Objects; | ||
| | ||
| /** | ||
| * This class represents a request to generate audio speech using specific parameters. | ||
| */ | ||
| public class GenerateSpeechRequest { | ||
| private final String model; | ||
| private final String input; | ||
| private final String voice; | ||
| private final String responseFormat; | ||
| private final double speed; | ||
| | ||
| private GenerateSpeechRequest(Builder builder) { | ||
| this.model = Objects.requireNonNull(builder.model, "Model cannot be null"); | ||
| this.input = Objects.requireNonNull(builder.input, "Input cannot be null"); | ||
| this.voice = Objects.requireNonNull(builder.voice, "Voice cannot be null"); | ||
| this.responseFormat = builder.responseFormat; | ||
| this.speed = builder.speed; | ||
| } | ||
| | ||
| // Implementing equals method to ensure correct behavior in collections and other use cases. | ||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) return true; | ||
| if (o == null || getClass() != o.getClass()) return false; | ||
| GenerateSpeechRequest that = (GenerateSpeechRequest) o; | ||
| return Double.compare(that.speed, speed) == 0 && | ||
| Objects.equals(model, that.model) && | ||
| Objects.equals(input, that.input) && | ||
| Objects.equals(voice, that.voice) && | ||
| Objects.equals(responseFormat, that.responseFormat); | ||
| } | ||
| | ||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(model, input, voice, responseFormat, speed); | ||
| } | ||
| | ||
| @Override | ||
| public String toString() { | ||
| return String.format( | ||
| "GenerateAudioRequest{model='%s', input='%s', voice='%s', responseFormat='%s', speed=%.1f}", | ||
| model, input, voice, responseFormat, speed | ||
| ); | ||
| } | ||
| | ||
| // Static factory method for the builder, improving code readability. | ||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
| | ||
| // Builder class for GenerateAudioRequest. | ||
| public static class Builder { | ||
| private String model; | ||
| private String input; | ||
| private String voice; | ||
| private String responseFormat = SpeechModel.ResponseFormat.MP3.toString(); // Default response format | ||
| private double speed = 1.0; // Default speed | ||
| | ||
| public Builder model(String model) { | ||
| this.model = model; | ||
| return this; | ||
| } | ||
| | ||
| public Builder model(SpeechModel model) { | ||
| this.model = model.toString(); | ||
| return this; | ||
| } | ||
| | ||
| public Builder input(String input) { | ||
| this.input = input; | ||
| return this; | ||
| } | ||
| | ||
| public Builder voice(String voice) { | ||
| this.voice = voice; | ||
| return this; | ||
| } | ||
| | ||
| public Builder voice(SpeechModel.Voice voice) { | ||
| this.voice = voice.toString(); | ||
| return this; | ||
| } | ||
| | ||
| public Builder responseFormat(String responseFormat) { | ||
| this.responseFormat = responseFormat; | ||
| return this; | ||
| } | ||
| | ||
| public Builder responseFormat(SpeechModel.ResponseFormat responseFormat) { | ||
| this.responseFormat = responseFormat.toString(); | ||
| return this; | ||
| } | ||
| | ||
| public Builder speed(double speed) { | ||
| if (speed <= 0) { | ||
| throw new IllegalArgumentException("Speed must be positive"); | ||
| } | ||
| this.speed = speed; | ||
| return this; | ||
| } | ||
| | ||
| public GenerateSpeechRequest build() { | ||
| return new GenerateSpeechRequest(this); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious about the builder approach. Why not use lombok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why adding another dependency? It's not much that would be saved here in terms of lines of code or complexity. I'd keep as is. @langchain4j @LizeRaes I'd love to use it here: langchain4j/langchain4j#255