Package com.chatmotorapi.api
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
Note that standard requests are limited to 4096 tokens in output.
If you need to send more than 4096 tokens, you can use the
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());
}
}
}
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MotorRequest.Builder
Builder class for creating a MotorRequest instance. -
Method Summary
Modifier and Type Method Description String
aiModel()
Gets the AI model to be used in the request.MotorAiOptions
aiOptions()
Gets the AI options to be used in the request.static MotorRequest.Builder
builder()
Returns a new instance ofMotorRequest.Builder
, which is used to construct aMotorRequest
object.ChatMotor
chatMotor()
Gets the ChatMotor to be used in the request.MotorResponse
execute()
Executes the chat request using the configured settings and messages.MotorStreamStatus
executeAsStream(MotorResponseListener motorResponseListener)
Executes the configured request as a stream and returns the execution status.List<MotorMessage>
messages()
Get back the MotorMessages that will be sent to the ChatMotor.
-
Method Details
-
chatMotor
Gets the ChatMotor to be used in the request.- Returns:
- the
-
aiOptions
Gets the AI options to be used in the request.- Returns:
- the AI options to be used in the request
-
aiModel
Gets the AI model to be used in the request.- Returns:
- the AI model to be used in the request
-
messages
Get back the MotorMessages that will be sent to the ChatMotor.- Returns:
- the messages that will be sent to the ChatMotor.
-
builder
Returns a new instance ofMotorRequest.Builder
, which is used to construct aMotorRequest
object. The builder allows for fluent configuration of aMotorRequest
instance by setting various parameters such asChatMotor
,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 creatingMotorRequest
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
MotorResponse
object containing the results of the request execution.
-
executeAsStream
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.
-