Skip to content

com_cloudurable_jai_model_text

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

com.cloudurable.jai.model.text

class diagram

SerializerUtils

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)

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.

  1. It retrieves the value of the temperature attribute from the chatRequest object using the getTemperature() method.
  2. It checks if the temperature is not equal to zero.
  3. If the temperature is not zero, it adds an attribute named "temperature" with the value of the temperature to the jsonBodyBuilder object using the addAttribute() method.
  4. It then retrieves the value of the topP attribute from the chatRequest object using the getTopP() method.
  5. It checks if the topP is not equal to zero.
  6. If the topP is not zero, it adds an attribute named "top_p" with the value of the topP to the jsonBodyBuilder object using the addAttribute() method.
  7. Next, it retrieves the value of the completionCount attribute from the chatRequest object using the getCompletionCount() method.
  8. It checks if the completionCount is not equal to zero.
  9. If the completionCount is not zero, it adds an attribute named "n" with the value of the completionCount to the jsonBodyBuilder object using the addAttribute() 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. sequence diagram

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:

  1. Retrieve the frequencyPenalty value from the chatRequest object.
  2. If the frequencyPenalty is not equal to 0, add an attribute with the key "frequency_penalty" and the value of frequencyPenalty to the jsonBodyBuilder.
  3. Retrieve the presencePenalty value from the chatRequest object.
  4. If the presencePenalty is not equal to 0, add an attribute with the key "presence_penalty" and the value of presencePenalty to the jsonBodyBuilder.
  5. Commented out code block. It seems to be a temporary fix and is not currently used.
  6. Retrieve the maxTokens value from the chatRequest object.
  7. If the maxTokens is not equal to 0, add an attribute with the key "max_tokens" and the value of maxTokens to the jsonBodyBuilder.
  8. Retrieve the user value from the chatRequest object.
  9. If the user is not null and not empty, add an attribute with the key "user" and the value of user to the jsonBodyBuilder.
  10. Retrieve the stop list from the chatRequest object.
  11. If the stop list is not null and not empty, start a nested array attribute with the key "stop" in the jsonBodyBuilder.
  12. Iterate over each string str in the stop list.
  13. Add each string str as an element to the nested array in the jsonBodyBuilder.
  14. Remove the trailing comma from the nested array in the jsonBodyBuilder. sequence diagram

TextResponse

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.

Choice

The Choice class is an abstract class that represents a choice.

DeserializerUtils

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)

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();
}

Method: deserializeUsage

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)

Description

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. sequence diagram

public static FinishReason deserializeFinishReason(final String finishReason)

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;
    }
}

Method: deserializeFinishReason

This method is defined in the com.cloudurable.jai.model.text.DeserializerUtils class.

Description:

This method takes a finishReason as input and returns a FinishReason enum based on the value of finishReason.

Steps:

  1. Start by declaring a public static method named deserializeFinishReason that takes a String parameter named finishReason.

  2. Inside the method, create a switch statement using the finishReason as the expression.

  3. For each case, compare the value of finishReason with the possible values:

    a. If the value of finishReason is "stop", return FinishReason.STOP.

    b. If the value of finishReason is "content_filter", return FinishReason.CONTENT_FILTER.

    c. If the value of finishReason is "length", return FinishReason.LENGTH.

    d. If the value of finishReason is "null", return FinishReason.NULL.

    e. If none of the above conditions match, return FinishReason.OTHER.

  4. The method ends after the switch statement with a default return statement.

  5. 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. sequence diagram

TextRequest

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.

Clone this wiki locally