-
Notifications
You must be signed in to change notification settings - Fork 2
com_cloudurable_jai_model_text_embedding
The EmbeddingResponse
class represents the response for an embedding operation. It implements the Response
interface.
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) {
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:
-
It takes a parameter
jsonBody
of typeString
, which represents the JSON string to be deserialized. -
It creates a new
JsonParser
object using theJsonParserBuilder.builder().build()
method. -
It parses the
jsonBody
string using theparse
method of theJsonParser
object and assigns the result to anObjectNode
variable namedobjectNode
. -
It extracts the value of the "object" field from the
objectNode
as aString
and assigns it to a variable namedobject
. -
It calls the static
deserializeUsage
method from theDeserializerUtils
class, passing theobjectNode.getObjectNode("usage")
as a parameter, to deserialize the "usage" field and assigns the result to aUsage
variable namedusage
. -
It extracts the value of the "data" field from the
objectNode
as anArrayNode
and assigns it to a variable nameddataArray
. -
It maps over each element of the
dataArray
using themapObjectNode
method and a lambda function. Inside the lambda function, it retrieves the value of the "index" field as anint
, the value of the "embedding" field as afloat[]
, and constructs anEmbedding
object using theEmbedding.builder()
method. It then adds eachEmbedding
object to aList<Embedding>
nameddata
. -
Finally, it constructs an
EmbeddingResponse
object using theEmbeddingResponse.builder()
method, passing in theobject
,data
, andusage
values. It then returns the constructedEmbeddingResponse
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.
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) {
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:
- Create a new instance of
JsonSerializer
to build the JSON body. - Begin building the JSON object by calling
startObject()
. - Check if the input list of the
EmbeddingRequest
contains only one element usingrequest.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.
- If true, add the single input element to the JSON body by calling
- Start building a nested JSON array attribute called "input" by calling
startNestedArrayAttribute("input")
. - Iterate through each input element in the
request.getInput()
list.- For each input element, add it to the JSON array by calling
addElement(in)
.
- For each input element, add it to the JSON array by calling
- Complete building the JSON array by calling
endArray()
. - Add the "model" attribute to the JSON object by calling
addAttribute("model", request.getModel())
. - Complete building the JSON object by calling
endObject()
. - Convert the JSON body to a string representation by calling
toString()
on thejsonBodyBuilder
object. - Return the JSON string representation.
This method ensures that the resulting JSON body complies with the expected structure for an OpenAI API chat request.
The Embedding
class represents an embedding.
The EmbeddingRequest
class is a public class that represents a request for an embedding operation.
- 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