-
Notifications
You must be signed in to change notification settings - Fork 2
com_cloudurable_jai_model_image
The ImageResponse
class is an implementation of the Response
interface. It represents an image response and provides information about the created date and the URLs of multiple images. The image response is returned as a JSON object with the following structure:
{
"created": 1589478378,
"data": [
{
"url": "https://..."
},
{
"url": "https://..."
}
]
}
The created
field represents the timestamp of when the image response was created. The data
field is an array of objects, with each object containing the URL of an image.
The ImageResponseDeserializer
is a class responsible for parsing and deserializing JSON data into an ImageResponse
object. It is used in the software to handle the conversion of JSON data into a Java object representation.
public static ImageResponse deserialize(final String json) {
final ImageResponse.Builder builder = ImageResponse.builder();
final JsonParser jsonParser = JsonParserBuilder.builder().build();
final ObjectNode objectNode = jsonParser.parse(json).getObjectNode();
builder.created(Instant.ofEpochSecond(objectNode.getInt("created")));
final List<ImageResponseData> imageResponseData = objectNode.getArrayNode("data").mapObjectNode(data -> new ImageResponseData(URI.create(data.getString("url"))));
builder.data(imageResponseData);
return builder.build();
}
The method deserialize
in class com.cloudurable.jai.model.image.ImageResponseDeserializer
is used to deserialize a JSON string into an ImageResponse
object.
Here is a step-by-step description of what the method is doing:
-
It takes a JSON string as input.
-
It creates an instance of
ImageResponse.Builder
using thebuilder
method. -
It creates an instance of
JsonParser
using theJsonParserBuilder
with default configurations. -
It parses the JSON string using the
jsonParser.parse(json)
method and gets the rootObjectNode
from the parsed JSON. -
It retrieves the value of the "created" field from the root
ObjectNode
usinggetInt("created")
and converts it to anInstant
usingInstant.ofEpochSecond
. -
It creates an empty list of
ImageResponseData
objects. -
It retrieves the value of the "data" field from the root
ObjectNode
as an array usinggetArrayNode("data")
. -
It maps each element of the array to an
ObjectNode
and creates a newImageResponseData
object using the value of the "url" field in theObjectNode
as a URI. The resultingImageResponseData
objects are added to the list created in step 6. -
It sets the list of
ImageResponseData
objects in theImageResponse.Builder
using thedata
method. -
It calls the
build
method on theImageResponse.Builder
to obtain the finalImageResponse
object. -
It returns the deserialized
ImageResponse
object.
Overall, this method parses a JSON string, extracts the necessary fields from the JSON, creates and populates the corresponding objects, and returns the deserialized ImageResponse
object.
The ImageResponseData
class represents the data of an image response.
The ImageRequest
class is an abstract class that represents an image request. It implements the Request
interface.
The CreateImageVariationRequest
is a subclass of the ImageRequest
class. It represents a request for creating a variation of an image.
The CreateImageRequest
class is a subclass of ImageRequest
and represents a request for creating an image. It is used to encapsulate the necessary information and parameters needed to create an image.
The EditImageRequest
class extends the ImageRequest
class and represents a request for editing an image.
The ImageRequestSerializer
class is a public class that serves as an encoder for constructing a multipart form specifically designed for an image creation request. This class is responsible for serializing the relevant information and formatting it correctly for the request.
public static MultipartEntityBuilder buildEditForm(EditImageRequest imageRequest) {
MultipartEntityBuilder form = buildForm(imageRequest);
form.addTextBody("prompt", imageRequest.getPrompt());
form.addBinaryBody("image", imageRequest.getImageBody(), "application/binary", imageRequest.getImageFileName());
if (imageRequest.getMaskBody() != null) {
form.addBinaryBody("mask", imageRequest.getMaskBody(), "application/binary", imageRequest.getMaskImageFileName());
}
return form;
}
The buildEditForm
method, defined in the com.cloudurable.jai.model.image.ImageRequestSerializer
class, is responsible for generating a multipart form entity builder based on the given EditImageRequest
object.
Here is the step-by-step description of what the buildEditForm
method is doing based on its body:
- Begin by creating a new
MultipartEntityBuilder
object namedform
using thebuildForm
method, passing in theimageRequest
object. - Add a text body field to the
form
with the key "prompt" and the value obtained fromimageRequest.getPrompt()
method. - Add a binary body field to the
form
with the key "image", the byte array obtained fromimageRequest.getImageBody()
method, the content type "application/binary", and the file name obtained fromimageRequest.getImageFileName()
method. - Check if
imageRequest.getMaskBody()
is not null.- If it is not null, add a binary body field to the
form
with the key "mask", the byte array obtained fromimageRequest.getMaskBody()
method, the content type "application/binary", and the file name obtained fromimageRequest.getMaskImageFileName()
method.
- If it is not null, add a binary body field to the
- Return the
form
object.
The buildEditForm
method essentially constructs a multipart form entity builder by adding various fields to it, such as text and binary bodies, based on the attributes of the EditImageRequest
object.
public static MultipartEntityBuilder buildForm(ImageRequest imageRequest) {
MultipartEntityBuilder form = MultipartEntityBuilder.create();
if (imageRequest.getResponseFormat() != null) {
form.addTextBody("response_format", imageRequest.getResponseFormat().toString().toLowerCase());
}
if (imageRequest.getSize() != null) {
switch(imageRequest.getSize()) {
case SIZE_1024_1024:
form.addTextBody("size", "1024x1024");
break;
case SIZE_256_BY_256:
form.addTextBody("size", "256x256");
break;
case SIZE_512_BY_512:
form.addTextBody("size", "512x512");
break;
}
}
if (imageRequest.getN() != 0 && imageRequest.getN() != 1) {
form.addTextBody("n", String.valueOf(imageRequest.getN()));
}
if (imageRequest.getUser() != null) {
form.addTextBody("user", imageRequest.getUser());
}
return form;
}
The method buildForm
in the class com.cloudurable.jai.model.image.ImageRequestSerializer
takes an ImageRequest
object as a parameter. It creates a MultipartEntityBuilder
object called form
using MultipartEntityBuilder.create()
.
The method then checks if the responseFormat
property of the imageRequest
is not null. If it is not null, it adds a text body to the form
with the key "response_format" and the lowercase string representation of the responseFormat
value.
Next, the method checks if the size
property of the imageRequest
is not null. If it is not null, it performs a switch case on the size
value. If the size
value matches SIZE_1024_1024
, it adds a text body to the form
with the key "size" and the value "1024x1024". If the size
value matches SIZE_256_BY_256
, it adds a text body to the form
with the key "size" and the value "256x256". If the size
value matches SIZE_512_BY_512
, it adds a text body to the form
with the key "size" and the value "512x512".
The method then checks if the n
property of the imageRequest
is not 0 and not 1. If it satisfies this condition, it adds a text body to the form
with the key "n" and the string representation of the n
value.
Finally, the method checks if the user
property of the imageRequest
is not null. If it is not null, it adds a text body to the form
with the key "user" and the value of the user
property.
The method returns the form
object.
public static String buildJson(CreateImageRequest imageRequest) {
JsonSerializer serializer = new JsonSerializer();
serializer.startObject();
serializer.addAttribute("prompt", imageRequest.getPrompt());
// Add response format if available
if (imageRequest.getResponseFormat() != null) {
serializer.addAttribute("response_format", imageRequest.getResponseFormat().toString().toLowerCase());
}
// Add size if available
if (imageRequest.getSize() != null) {
switch(imageRequest.getSize()) {
case SIZE_1024_1024:
serializer.addAttribute("size", "1024x1024");
break;
case SIZE_256_BY_256:
serializer.addAttribute("size", "256x256");
break;
case SIZE_512_BY_512:
serializer.addAttribute("size", "512x512");
break;
}
}
if (imageRequest.getN() != 0 && imageRequest.getN() != 1) {
serializer.addAttribute("n", imageRequest.getN());
}
if (imageRequest.getUser() != null) {
serializer.addAttribute("user", imageRequest.getUser());
}
serializer.endObject();
return serializer.toString();
}
The buildJson
method in the ImageRequestSerializer
class is used to build a JSON string based on the provided CreateImageRequest
object.
Here is a step-by-step breakdown of what the method does:
- Create a new
JsonSerializer
object to start building the JSON string. - Start the JSON object by calling the
startObject
method of theJsonSerializer
. - Add the "prompt" attribute to the JSON object by calling the
addAttribute
method of theJsonSerializer
and passing the value ofimageRequest.getPrompt()
. - Check if the
imageRequest.getResponseFormat()
is not null.- If it's not null, add the "response_format" attribute to the JSON object by calling the
addAttribute
method of theJsonSerializer
and passing the lowercase string representation ofimageRequest.getResponseFormat()
.
- If it's not null, add the "response_format" attribute to the JSON object by calling the
- Check if the
imageRequest.getSize()
is not null.- If it's not null, switch on the value of
imageRequest.getSize()
.- If it matches
SIZE_1024_1024
, add the "size" attribute with the value "1024x1024" to the JSON object. - If it matches
SIZE_256_BY_256
, add the "size" attribute with the value "256x256" to the JSON object. - If it matches
SIZE_512_BY_512
, add the "size" attribute with the value "512x512" to the JSON object.
- If it matches
- If it's not null, switch on the value of
- Check if the
imageRequest.getN()
is not 0 and not 1.- If it meets the condition, add the "n" attribute with the value of
imageRequest.getN()
to the JSON object.
- If it meets the condition, add the "n" attribute with the value of
- Check if the
imageRequest.getUser()
is not null.- If it's not null, add the "user" attribute with the value of
imageRequest.getUser()
to the JSON object.
- If it's not null, add the "user" attribute with the value of
- End the JSON object by calling the
endObject
method of theJsonSerializer
. - Convert the JSON object to a string by calling the
toString
method of theJsonSerializer
. - Return the resulting JSON string.
This method takes a CreateImageRequest
object as input and generates a corresponding JSON string based on the attributes of the object.
- Java Open AI Client
- Using ChatGpt embeddings and hyde to improve search results
- Anthropics Claude Chatbot Gets Upgrade
- Elon Musks XAi new frontier for artificial intelligence
- Using Mockito to test JAI Java Open AI Client
- Fine tuning journey with Open AI API
- Using Open AI to create callback functions, the basis for plugins
- Using Java Open AI Client Async
- Fastest Java JSON Parser
- Java Open AI API Client on Github
- Medium: Introducing Java Open AI Client
- Medium: Using ChatGPT, Embeddings, and HyDE to Improve Search Results