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 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.
     }

 
 
  • Method Details

    • chatMotor

      public ChatMotor chatMotor()
      Gets the ChatMotor to be used in the request.
      Returns:
      the
    • aiModel

      public String aiModel()
      Gets the AI model to be used in the request.
      Returns:
      the AI model to be used in the request
    • input

      public String input()
      Gets the input to be used in the request.
      Returns:
      the input to be used in the request
    • motorSpeechVoice

      public MotorSpeechVoice motorSpeechVoice()
      Gets the voice to be used in the request.
      Returns:
      the voice to be used in the request
    • motorSpeechFormat

      public MotorSpeechFormat motorSpeechFormat()
      Gets the format to be used in the request.
      Returns:
      the format to be used in the request
    • speechSpeed

      public Double speechSpeed()
      Gets the speed to be used in the request.
      Returns:
      the speed to be used in the request
    • inputChunkSize

      public ChunkSize inputChunkSize()
      Returns the input chunk size
      Returns:
      the input chunk size
    • builder

      public static MotorSpeechRequest.Builder builder()
      Returns a new instance of MotorSpeechRequest.Builder, which is used to construct a MotorSpeechRequest object. The builder allows for fluent configuration of a MotorSpeechRequest instance by setting various parameters such as ChatMotor, MotorAiOptions, List<MotorMessage>, timeout settings, and the maximum number of retries.
      Returns:
      a new instance of MotorSpeechRequest.Builder for creating MotorSpeechRequest objects.
    • execute

      public MotorLargeResponse 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.