Package com.chatmotorapi.api.image
Class MotorImageRequest
java.lang.Object
com.chatmotorapi.api.image.MotorImageRequest
public class MotorImageRequest extends Object
Handles the creation and execution of image-related requests for the
ChatMotor API, designed to interface with OpenAI's Vision API. This class
leverages the Builder pattern to provide a flexible and reliable
configuration mechanism, ensuring that all necessary parameters are set
before execution.
The model to be used for requests can be specified via the
.aiModel
setter in the builder. If .aiModel is not explicitly set, the model
defaults to the value specified by the environment variable
MOTOR_IMAGE_MODEL. If the environment variable is not set, the model
used is MotorDefaultsModels.MOTOR_IMAGE_MODEL. This allows for dynamic
configuration based on deployment settings without requiring code changes.
Note: Image requests are limited to 4096 tokens in input.
Usage example:
// We assume that the env var MOTOR_API_KEY is set
// Create a ChatMotor instance.
ChatMotor chatMotor = ChatMotor.builder()
.build();
String prompt = "hyperealistic view of the Eiffel Tower in " +
"green under a blue clear sky with sunset orange sun.";
// Make a request to the ChatMotor.
MotorImageRequest motorImageRequest = MotorImageRequest.builder()
.chatMotor(chatMotor)
.n(1).prompt(prompt)
.motorImageSize(MotorImageSize.X1024)
.responseImageFormat(ResponseImageFormat.URL)
.build();
// Execute the image request.
MotorImageResponse motorImageResponse = motorImageRequest.execute();
if (motorImageResponse.isResponseOk()) {
List<MotorImageContainer> motorImageContainers = motorImageResponse.getImageContainer();
for (MotorImageContainer motorImageContainer : motorImageContainers) {
System.out.println(motorImageContainer.getUrl());
}
System.out.println();
} else {
OpenAiError openAiError = motorImageResponse.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: " + motorImageResponse.getThrowable());
}
}
This class supports configurations specific to image processing, such as
specifying image size, format, and the number of images to generate, making
it suitable for applications requiring high-level image analysis and
manipulation within the scope of AI-enhanced environments.-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMotorImageRequest.BuilderBuilder class for creating a MotorImageRequest instance. -
Method Summary
Modifier and Type Method Description StringaiModel()Gets the AI model to be used in the request.static MotorImageRequest.Builderbuilder()Returns a new instance ofMotorImageRequest.Builder, which is used to construct aMotorImageRequestobject.ChatMotorchatMotor()Gets the ChatMotor to be used in the request.MotorImageResponseexecute()Executes the chat request using the configured settings and messages.StringgetPrompt()Gets the prompt to be used in the request.MotorImageSizemotorImageSize()Gets the image size to be used in the request.intnumber()Gets the number of images to be generated in the request.ResponseImageFormatresponseImageFormat()Gets the response image format 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
-
getPrompt
Gets the prompt to be used in the request.- Returns:
- the prompt to be used in the request
-
motorImageSize
Gets the image size to be used in the request.- Returns:
- the image size to be used in the request
-
responseImageFormat
Gets the response image format to be used in the request.- Returns:
- the response image format to be used in the request
-
number
public int number()Gets the number of images to be generated in the request.- Returns:
- the
-
builder
Returns a new instance ofMotorImageRequest.Builder, which is used to construct aMotorImageRequestobject. The builder allows for fluent configuration of aMotorImageRequestinstance by setting various parameters such asChatMotor,MotorAiOptions, List<MotorMessage>, timeout settings, and the maximum n 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
MotorImageRequestobject is instantiated.- Returns:
- a new instance of
MotorImageRequest.Builderfor creatingMotorImageRequestobjects.
-
execute
Executes the chat request using the configured settings and messages.
This method is allowed only with a total messages size < 4000 tokens.
If you need to send more than 4000 tokens, you can use theMotorImageRequestclass.
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
MotorImageResponseobject containing the results of the request execution.
-