-
Notifications
You must be signed in to change notification settings - Fork 2
com_cloudurable_jai_model_text
The SerializerUtils
class is a utility class that is used for serializing text-related requests. It provides methods and functions for converting text-based data into a serialized format.
public static void outputTextParams(TextRequest chatRequest, JsonSerializer jsonBodyBuilder) {
final float temperature = chatRequest.getTemperature();
if (temperature != 0) {
jsonBodyBuilder.addAttribute("temperature", temperature);
}
final float topP = chatRequest.getTopP();
if (topP != 0) {
jsonBodyBuilder.addAttribute("top_p", topP);
}
// add completionCount to JSON
final int completionCount = chatRequest.getCompletionCount();
if (completionCount != 0) {
jsonBodyBuilder.addAttribute("n", completionCount);
}
}
The method outputTextParams
in the class com.cloudurable.jai.model.text.SerializerUtils
takes a TextRequest
object and a JsonSerializer
object as parameters.
- It retrieves the value of the
temperature
attribute from thechatRequest
object using thegetTemperature()
method. - It checks if the temperature is not equal to zero.
- If the temperature is not zero, it adds an attribute named "temperature" with the value of the temperature to the
jsonBodyBuilder
object using theaddAttribute()
method. - It then retrieves the value of the
topP
attribute from thechatRequest
object using thegetTopP()
method. - It checks if the topP is not equal to zero.
- If the topP is not zero, it adds an attribute named "top_p" with the value of the topP to the
jsonBodyBuilder
object using theaddAttribute()
method. - Next, it retrieves the value of the
completionCount
attribute from thechatRequest
object using thegetCompletionCount()
method. - It checks if the completionCount is not equal to zero.
- If the completionCount is not zero, it adds an attribute named "n" with the value of the completionCount to the
jsonBodyBuilder
object using theaddAttribute()
method.
In summary, this method takes a TextRequest
object and a JsonSerializer
object, retrieves specific attributes from the TextRequest
object, and adds them as attributes to the JsonSerializer
object if they have non-zero values.
public static void outputCompletionParams(CommonCompletionRequest chatRequest, JsonSerializer jsonBodyBuilder)
public static void outputCompletionParams(CommonCompletionRequest chatRequest, JsonSerializer jsonBodyBuilder) {
final float frequencyPenalty = chatRequest.getFrequencyPenalty();
if (frequencyPenalty != 0) {
jsonBodyBuilder.addAttribute("frequency_penalty", frequencyPenalty);
}
float presencePenalty = chatRequest.getPresencePenalty();
if (presencePenalty != 0) {
jsonBodyBuilder.addAttribute("presence_penalty", presencePenalty);
}
//TODO FIX
// final Map<Integer, Float> logitBias = chatRequest.getLogitBias();
// if (logitBias != null && !logitBias.isEmpty()) {
// jsonBodyBuilder.startNestedListAttribute("logit_bias");
// for (Integer key : logitBias.keySet()) {
// jsonBodyBuilder.startNestedObjectElement();
// jsonBodyBuilder.addAttribute(key, logitBias.get(key));
// jsonBodyBuilder.endObject();
// }
// jsonBodyBuilder.endList();
// }
final int maxTokens = chatRequest.getMaxTokens();
if (maxTokens != 0) {
jsonBodyBuilder.addAttribute("max_tokens", maxTokens);
}
// add user to JSON
final String user = chatRequest.getUser();
if (user != null && !user.isBlank()) {
jsonBodyBuilder.addAttribute("user", user);
}
// add stop to JSON
final List<String> stop = chatRequest.getStop();
if (stop != null && !stop.isEmpty()) {
jsonBodyBuilder.startNestedArrayAttribute("stop");
for (String str : stop) {
jsonBodyBuilder.addElement(str);
}
// remove trailing comma
jsonBodyBuilder.endArray();
}
}
The outputCompletionParams
method in the SerializerUtils
class is used to generate a JSON representation of the parameters for a completion request in a chat system. Here is a step-by-step description of what this method does based on its body:
- Retrieve the
frequencyPenalty
value from thechatRequest
object. - If the
frequencyPenalty
is not equal to 0, add an attribute with the key "frequency_penalty" and the value offrequencyPenalty
to thejsonBodyBuilder
. - Retrieve the
presencePenalty
value from thechatRequest
object. - If the
presencePenalty
is not equal to 0, add an attribute with the key "presence_penalty" and the value ofpresencePenalty
to thejsonBodyBuilder
. - Commented out code block. It seems to be a temporary fix and is not currently used.
- Retrieve the
maxTokens
value from thechatRequest
object. - If the
maxTokens
is not equal to 0, add an attribute with the key "max_tokens" and the value ofmaxTokens
to thejsonBodyBuilder
. - Retrieve the
user
value from thechatRequest
object. - If the
user
is not null and not empty, add an attribute with the key "user" and the value ofuser
to thejsonBodyBuilder
. - Retrieve the
stop
list from thechatRequest
object. - If the
stop
list is not null and not empty, start a nested array attribute with the key "stop" in thejsonBodyBuilder
. - Iterate over each string
str
in thestop
list. - Add each string
str
as an element to the nested array in thejsonBodyBuilder
. - Remove the trailing comma from the nested array in the
jsonBodyBuilder
.
The TextResponse
class is an abstract class that serves as the base for text-based responses. It implements the Response
interface and provides a foundation for creating various types of text responses.
The Choice class is an abstract class that represents a choice.
The DeserializerUtils
class is a utility class for deserializing text-related data. It provides methods and functionality to assist in the deserialization process.
public static Usage deserializeUsage(final ObjectNode usageNode) {
Usage.Builder builder = Usage.builder();
if (usageNode.getNode("completion_tokens") != null) {
builder.completionTokens(usageNode.getInt("completion_tokens"));
}
builder.promptTokens(usageNode.getInt("prompt_tokens"));
builder.totalTokens(usageNode.getInt("total_tokens"));
return builder.build();
}
This method is defined in the class com.cloudurable.jai.model.text.DeserializerUtils
and has the following signature:
public static Usage deserializeUsage(final ObjectNode usageNode)
This method deserializes a JSON object into an instance of the Usage
class. It takes an ObjectNode
parameter named usageNode
, which represents the input JSON object containing the usage data.
The method begins by creating a new Usage.Builder
object.
Next, it checks if the usageNode
contains a node named "completion_tokens" using the getNode
method. If the node exists, it retrieves the integer value using the getInt
method and sets it as the completion tokens value using the completionTokens
method of the Usage.Builder
object.
After that, it retrieves the integer value of the "prompt_tokens" node using the getInt
method and sets it as the prompt tokens value using the promptTokens
method of the Usage.Builder
object.
Similarly, it retrieves the integer value of the "total_tokens" node using the getInt
method and sets it as the total tokens value using the totalTokens
method of the Usage.Builder
object.
Finally, it builds and returns the Usage
object using the build
method of the Usage.Builder
object.
Please note that the Usage
class is assumed to be defined elsewhere in the codebase.
public static FinishReason deserializeFinishReason(final String finishReason) {
switch(finishReason) {
case "stop":
return FinishReason.STOP;
case "content_filter":
return FinishReason.CONTENT_FILTER;
case "length":
return FinishReason.LENGTH;
case "null":
return FinishReason.NULL;
default:
return FinishReason.OTHER;
}
}
This method is defined in the com.cloudurable.jai.model.text.DeserializerUtils
class.
This method takes a finishReason
as input and returns a FinishReason
enum based on the value of finishReason
.
-
Start by declaring a public static method named
deserializeFinishReason
that takes aString
parameter namedfinishReason
. -
Inside the method, create a switch statement using the
finishReason
as the expression. -
For each case, compare the value of
finishReason
with the possible values:a. If the value of
finishReason
is "stop", returnFinishReason.STOP
.b. If the value of
finishReason
is "content_filter", returnFinishReason.CONTENT_FILTER
.c. If the value of
finishReason
is "length", returnFinishReason.LENGTH
.d. If the value of
finishReason
is "null", returnFinishReason.NULL
.e. If none of the above conditions match, return
FinishReason.OTHER
. -
The method ends after the switch statement with a default return statement.
-
The returned value is of type
FinishReason
, which is an enum.
Note: Make sure that the FinishReason
enum is defined and has the above values.
The TextRequest
class is an abstract class that serves as a base for text-related requests in the context of an application. It provides common functionality and properties for text-based requests and implements the Request
interface. This class encapsulates information such as the model to be used, temperature and top-p values for controlling randomness in the generated output, and the number of completions to generate for each prompt.
Developers can create concrete subclasses by extending this class and providing appropriate implementations for the abstract methods. The TextRequest
class includes methods to retrieve the model, temperature, top-p value, and completion count associated with the text request. It also provides overridden equals()
, hashCode()
, and toString()
methods for comparing and representing instances of the TextRequest
class.
Note that the TextRequest
class is abstract and cannot be instantiated directly. It is intended to be extended by subclasses that provide specific implementations based on the requirements of the application.
- 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