Skip to content

com_cloudurable_jai_model_text_embedding

Rick Hightower edited this page Jul 16, 2023 · 1 revision

com.cloudurable.jai.model.text.embedding

class diagram

EmbeddingResponse

The EmbeddingResponse class represents the response for an embedding operation. It implements the Response interface.

EmbeddingResponseDeserializer

The EmbeddingResponseDeserializer is a public class that is responsible for reading JSON formatted data and creating an EmbeddingResponse object. It provides the functionality to deserialize the JSON data and extract the necessary information to create the EmbeddingResponse.

public static EmbeddingResponse deserialize(final String jsonBody)

public static EmbeddingResponse deserialize(final String jsonBody) {
    final JsonParser parser = JsonParserBuilder.builder().build();
    final ObjectNode objectNode = parser.parse(jsonBody).asObject();
    final String object = objectNode.getString("object");
    final Usage usage = DeserializerUtils.deserializeUsage(objectNode.getObjectNode("usage"));
    final ArrayNode dataArray = objectNode.getArrayNode("data");
    final List<Embedding> data = dataArray.mapObjectNode(choiceNode -> Embedding.builder().index(choiceNode.getInt("index")).embedding(choiceNode.asObject().getArrayNode("embedding").getFloatArray()).build());
    return EmbeddingResponse.builder().object(object).data(data).usage(usage).build();
}

The method deserialize is a static method defined in the class com.cloudurable.jai.model.text.embedding.EmbeddingResponseDeserializer. Its purpose is to deserialize a JSON string representation of an EmbeddingResponse object and return the deserialized object.

Here is a step-by-step breakdown of what the method does:

  1. It takes a parameter jsonBody of type String, which represents the JSON string to be deserialized.

  2. It creates a new JsonParser object using the JsonParserBuilder.builder().build() method.

  3. It parses the jsonBody string using the parse method of the JsonParser object and assigns the result to an ObjectNode variable named objectNode.

  4. It extracts the value of the "object" field from the objectNode as a String and assigns it to a variable named object.

  5. It calls the static deserializeUsage method from the DeserializerUtils class, passing the objectNode.getObjectNode("usage") as a parameter, to deserialize the "usage" field and assigns the result to a Usage variable named usage.

  6. It extracts the value of the "data" field from the objectNode as an ArrayNode and assigns it to a variable named dataArray.

  7. It maps over each element of the dataArray using the mapObjectNode method and a lambda function. Inside the lambda function, it retrieves the value of the "index" field as an int, the value of the "embedding" field as a float[], and constructs an Embedding object using the Embedding.builder() method. It then adds each Embedding object to a List<Embedding> named data.

  8. Finally, it constructs an EmbeddingResponse object using the EmbeddingResponse.builder() method, passing in the object, data, and usage values. It then returns the constructed EmbeddingResponse object.

Note: In the above explanation, the classes Usage, Embedding, and EmbeddingResponse are assumed to be defined elsewhere and are used to represent the expected structure of the deserialized JSON object. sequence diagram

EmbeddingRequestSerializer

The EmbeddingRequestSerializer class is responsible for serializing an EmbeddingRequest object to JSON. It handles the transformation of the object into a JSON representation, allowing for easier data exchange and storage.

public static String serialize(EmbeddingRequest request)

public static String serialize(EmbeddingRequest request) {
    final JsonSerializer jsonBodyBuilder = new JsonSerializer();
    // start JSON request body for an open ai API chat request
    jsonBodyBuilder.startObject();
    if (request.getInput().size() == 1) {
        jsonBodyBuilder.addAttribute("input", request.getInput().get(0));
    } else if (request.getInput().size() > 1) {
        jsonBodyBuilder.startNestedArrayAttribute("input");
        for (String in : request.getInput()) {
            jsonBodyBuilder.addElement(in);
        }
        jsonBodyBuilder.endArray();
    }
    jsonBodyBuilder.addAttribute("model", request.getModel());
    // end JSON request body for an open ai API chat request
    jsonBodyBuilder.endObject();
    return jsonBodyBuilder.toString();
}

The serialize method is responsible for converting an EmbeddingRequest object into a JSON string representation. Here is a step-by-step description of what the method is doing:

  1. Create a new instance of JsonSerializer to build the JSON body.
  2. Begin building the JSON object by calling startObject().
  3. Check if the input list of the EmbeddingRequest contains only one element using request.getInput().size() == 1.
    • If true, add the single input element to the JSON body by calling addAttribute("input", request.getInput().get(0)).
    • If false (i.e., the input list contains more than one element), proceed to the next step.
  4. Start building a nested JSON array attribute called "input" by calling startNestedArrayAttribute("input").
  5. Iterate through each input element in the request.getInput() list.
    • For each input element, add it to the JSON array by calling addElement(in).
  6. Complete building the JSON array by calling endArray().
  7. Add the "model" attribute to the JSON object by calling addAttribute("model", request.getModel()).
  8. Complete building the JSON object by calling endObject().
  9. Convert the JSON body to a string representation by calling toString() on the jsonBodyBuilder object.
  10. Return the JSON string representation.

This method ensures that the resulting JSON body complies with the expected structure for an OpenAI API chat request. sequence diagram

Embedding

The Embedding class represents an embedding.

EmbeddingRequest

The EmbeddingRequest class is a public class that represents a request for an embedding operation.