Class MotorRequest

java.lang.Object
com.chatmotorapi.api.MotorRequest

public class MotorRequest
extends Object
Handles the creation and execution of requests to the ChatMotor using system and 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 MotorStandardRequest builder. If .aiModel is not called, the default model used is System.getenv("MOTOR_CHAT_MODEL"). If the environment variable is not set, the model used is MotorDefaultsModels.MOTOR_CHAT_MODEL.

Note that standard requests are limited to 4096 tokens in output.

If you need to send more than 4096 tokens, you can use the MotorLargeRequestLines

Usage example:

  
        // We assume that the env var MOTOR_API_KEY is set
        // Create a ChatMotor instance.
        ChatMotor chatMotor = ChatMotor.builder().build();

        String system = "You are an expert in programming.";
        String user = "Write a simple technical article about Java language";

        List<MotorMessage> motorMessages = new ArrayList<>();
        motorMessages.add(new MotorSystemMessage(system));
        motorMessages.add(new MotorUserMessage(user));

        // Make a request to the ChatMotor.
        MotorRequest motorRequest = MotorRequest.builder().chatMotor(chatMotor).messages(motorMessages).build();

        // Execute the request.
        MotorResponse motorResponse = motorRequest.execute();

        // We check if the response is ok.
        if (motorResponse.isResponseOk()) {
            String response = motorResponse.getResponse();
            System.out.println(response);
        } else {
            // We check if the response is an OpenAI error and get the OpenAI error details
            // (type and message)
            OpenAiError openAiError = motorResponse.getOpenAiError();
            OpenAiErrorType errorType = openAiError.getType();

            if (errorType != OpenAiErrorType.NO_OPENAI_ERROR) {
                System.out.println("OpenAI has returned an error : " 
                        + openAiError);

                // Take specific action depending on the error code:
                if (errorType.equals(OpenAiErrorType.SERVER_ERROR)) {
                    // ...
                } else if (errorType.equals(OpenAiErrorType.TOKENS)) {
                    // ...
                } else {
                    // Etc.
                }

            } else {
                System.out.println("throwable: " + motorResponse.getThrowable());
            }
        }

    }
 
 
  • Method Details

    • chatMotor

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

      public MotorAiOptions aiOptions()
      Gets the AI options to be used in the request.
      Returns:
      the AI options to be used in the request
    • aiModel

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

      public List<MotorMessage> messages()
      Get back the MotorMessages that will be sent to the ChatMotor.
      Returns:
      the messages that will be sent to the ChatMotor.
    • builder

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

      Using the builder pattern facilitates a more flexible and controlled construction process, allowing for optional configuration parameters to be easily managed and for mandatory parameters to be validated before the MotorRequest object is instantiated.

      Example of building a MotorRequest:

       MotorRequest request = MotorRequest.builder()
           .chatMotor(chatMotor)
           .motorAiOptions(aiOptions)
           .messages(messages)
           .build();
       
      Returns:
      a new instance of MotorRequest.Builder for creating MotorRequest objects.
    • execute

      public MotorResponse 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 MotorResponse object containing the results of the request execution.
    • executeAsStream

      public MotorStreamStatus executeAsStream​(MotorResponseListener motorResponseListener)
      Executes the configured request as a stream and returns the execution status.
      Parameters:
      motorResponseListener - The listener that will handle the streamed data chunks.
      Returns:
      An instance of MotorStreamStatus that contains information about the outcome of the streaming operation.