Package com.chatmotorapi.api.speech
Class MotorSpeechRequest
java.lang.Object
com.chatmotorapi.api.speech.MotorSpeechRequest
public class MotorSpeechRequest extends Object
Handles the creation and execution of a text to speech requests to the
ChatMotor using an user prompts. This class uses the Builder pattern to
ensure flexibility and enforce mandatory configuration. The model to be used
in requests can be specified with the .aiModel setter directly on the
MotorSpeechRequest builder. If .aiModel is not called, the default model used
is System.getenv("MOTOR_SPEECH_MODEL"). If the environment variable is not
set, the model used is
Note that standard requests are limited to 500 words in input for non-MP3 files. There are no limits for MP3 files.
Use our
MotorDefaultsModels.MOTOR_SPEECH_MODEL
.
Note that standard requests are limited to 500 words in input for non-MP3 files. There are no limits for MP3 files.
Use our
MotorAudioFileConverter
if you need to convert your audio
files to MP3 and vice versa. Usage example:
// We assume that the env var MOTOR_API_KEY is set
// Create a ChatMotor instance.
ChatMotor chatMotor = ChatMotor.builder().build();
File speechFile = new File("/path/to/audio/file.mp3"); // The audio file we want to generate.
// The content of the input
String prompt = "It was the best of times, it was the worst of times, "
+ "it was the age of wisdom, it was the age of foolishness, "
+ "it was the epoch of belief, it was the epoch of incredulity.";
MotorSpeechRequest speechRequest = MotorSpeechRequest.builder()
.chatMotor(chatMotor)
.input(prompt)
.audioFile(speechFile)
.speechResponseFormat(MotorSpeechFormat.MP3)
.speechVoice(MotorSpeechVoice.ALLOY)
.speechSpeed(1.0)
.build();
// Execute the Speech request.
MotorLargeResponse largeResponse = speechRequest.execute();
if (largeResponse.isResponseOk()) {
File responseFilePath = new File("/path/to/audio/file.mp3");
System.out.println("audioFile: " + largeResponse.getResponseFile());
try (InputStream in = largeReponse.getInputStream()) {
Files.copy(in, Paths.get(responseFilePath), StandardCopyOption.REPLACE_EXISTING);
}
} else {
// Treat errors
// See the MotorLargeResponse
class for more details.
}
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MotorSpeechRequest.Builder
Builder class for creating a MotorSpeechRequest instance. -
Method Summary
Modifier and Type Method Description String
aiModel()
Gets the AI model to be used in the request.static MotorSpeechRequest.Builder
builder()
Returns a new instance ofMotorSpeechRequest.Builder
, which is used to construct aMotorSpeechRequest
object.ChatMotor
chatMotor()
Gets the ChatMotor to be used in the request.MotorLargeResponse
execute()
Executes the chat request using the configured settings and messages.String
input()
Gets the input to be used in the request.ChunkSize
inputChunkSize()
Returns the input chunk sizeMotorSpeechFormat
motorSpeechFormat()
Gets the format to be used in the request.MotorSpeechVoice
motorSpeechVoice()
Gets the voice to be used in the request.Double
speechSpeed()
Gets the speed to be used in the request.
-
Method Details
-
chatMotor
Gets the ChatMotor to be used in the request.- Returns:
- the
-
aiModel
Gets the AI model to be used in the request.- Returns:
- the AI model to be used in the request
-
input
Gets the input to be used in the request.- Returns:
- the input to be used in the request
-
motorSpeechVoice
Gets the voice to be used in the request.- Returns:
- the voice to be used in the request
-
motorSpeechFormat
Gets the format to be used in the request.- Returns:
- the format to be used in the request
-
speechSpeed
Gets the speed to be used in the request.- Returns:
- the speed to be used in the request
-
inputChunkSize
Returns the input chunk size- Returns:
- the input chunk size
-
builder
Returns a new instance ofMotorSpeechRequest.Builder
, which is used to construct aMotorSpeechRequest
object. The builder allows for fluent configuration of aMotorSpeechRequest
instance by setting various parameters such asChatMotor
,MotorAiOptions
, List<MotorMessage>, timeout settings, and the maximum number of retries.- Returns:
- a new instance of
MotorSpeechRequest.Builder
for creatingMotorSpeechRequest
objects.
-
execute
Executes the chat request using the configured settings and messages.
This method processes the accumulated settings and messages to interact with the ChatMotor, ultimately generating a response based on the user and system prompts provided.- Returns:
- A new
MotorLargeResponse
object containing the results of the request execution.
-