/
ChatGPT Client

ChatGPT Client

Embeddings Endpoint

The params of the OpenAiEmbeddingsRequest object match those expected by the OpenAI API. See here for more details on each parameter.

import com.pureinsights.pdp.core.client.chatgpt.ChatGPTClient;
import com.pureinsights.pdp.core.client.chatgpt.api.embeddings.OpenAiEmbeddingsRequest;

public class TestChatGPTClient {

  public void embeddingsTest() {

    // Create client
    var client = new ChatGPTClient(
        "<YourApiToken>",
        null,
        Duration.ofSeconds(60),
        Duration.ofMinutes(3)
    );

    var input = List.of("The food was delicious and the waiter..."); // You can add multiple strings in this array

    var request = OpenAiEmbeddingsRequest.builder()
        .model("text-embedding-ada-002")
        .input(input)
        .user("user45") // Optional, according to the API specification linked above
        .build();

    var response = client.createEmbeddings(request);
  }
}

The response variable will be of type OpenAiEmbeddingsResponse and will hold the resulting embeddings:

OpenAiEmbeddingsResponse[model=text-embedding-ada-002-v2, data=[OpenAiEmbedding[index=0, embedding=[0.002264385,
 -0.009305084, ..., -0.0028007287]]], usage=OpenAiTokenUsage[promptTokens=8, completionTokens=0, totalTokens=8]]

Chat Completion Request

The params of the OpenAiChatCompletionRequest object match those expected by the OpenAI API. See here for more details on each parameter. Please note that some parameters, such as those related to the function calling feature, might be missing. This is due to the client not supporting those features yet.

import com.pureinsights.pdp.core.client.chatgpt.ChatGPTClient;
import com.pureinsights.pdp.core.client.chatgpt.api.chatcompletions.OpenAiChatRole;
import com.pureinsights.pdp.core.client.chatgpt.api.chatcompletions.OpenAiChatCompletionRequest;
import com.pureinsights.pdp.core.client.chatgpt.api.chatcompletions.OpenAiChatMessage;

public class TestChatGPTClient {

  public void chatCompletionTest() {

    // Create client
    var client = new ChatGPTClient(
        "<YourApiToken>",
        null,
        Duration.ofSeconds(60),
        Duration.ofMinutes(3)
    );

    var firstMessage = OpenAiChatMessage.builder()
        .role(OpenAiChatRole.SYSTEM)
        .content("You are a helpful assistant.")
        .build();

    var secondMessage = OpenAiChatMessage.builder()
        .role(OpenAiChatRole.USER)
        .content("Hi!")
        .build();

    // The chat completion request allows many other config parameters,
    // but only the model and messages are required
    var request = OpenAiChatCompletionRequest.builder()
        .model("gpt-3.5-turbo")
        .messages(List.of(firstMessage, secondMessage))
        .build();

    var response = client.createChatCompletion(request);
  }
}

The response variable will be of type OpenAiChatCompletionResponse and will hold the resulting chat completion choices:

OpenAiChatCompletionResponse[id=chatcmpl-7yKsVVxusxwc1S2AimZIRXfEQxscA, created=1694614291, model=gpt-3.5-turbo-0613, 
choices=[OpenAiChatCompletionChoice[index=0, message=OpenAiChatMessage(role=ASSISTANT, content=Hello! How can I assist 
you today?, name=null), finishReason=STOP]], usage=OpenAiTokenUsage[promptTokens=19, completionTokens=9, 
totalTokens=28]]

Built-in backoff policy

The ChatGPT client comes with a built-in backoff functionality that can be used to control how the requests are retried after they are rejected by the circuit breaker or return a Http exception corresponding to a rate limit error on the Open AI API's side. Or in other words, exceptions with a status code of 429. The backoff policy is implemented as explained in the Retry section of the core commons library (see here).

Please note that the backoff policy does not influence the underlying circuit breaker pattern that actually blocks api calls when there has been a 429 status code error. What the backoff does is control what happens with a request after the circuit breaker rejects the call of the Open AI API returns a rate limit error.

In order to make an embeddings or chat completion request to the client with this backoff policy in place, simply pass a BackoffPolicyProperties object as second parameter to the client's respective create method. Keep in mind that a call to the client with a backoff policy will always throw an exception when its defined maximum retries are exceeded.:

import com.pureinsights.pdp.core.client.chatgpt.ChatGPTClient;
import com.pureinsights.pdp.core.client.chatgpt.api.chatcompletions.OpenAiChatRole;
import com.pureinsights.pdp.core.client.chatgpt.api.chatcompletions.OpenAiChatCompletionRequest;
import com.pureinsights.pdp.core.client.chatgpt.api.chatcompletions.OpenAiChatMessage;

public class TestChatGPTClient {

  public void backoffTest() {

    // Create client
    var client = new ChatGPTClient(
        "<YourApiToken>",
        null,
        Duration.ofSeconds(60),
        Duration.ofMinutes(3)
    );

    // In this example a chat completion request will be used, but it works the same for an embeddings one
    var firstMessage = OpenAiChatMessage.builder()
        .role(OpenAiChatRole.SYSTEM)
        .content("You are a helpful assistant.")
        .build();

    var secondMessage = OpenAiChatMessage.builder()
        .role(OpenAiChatRole.USER)
        .content("Hi!")
        .build();

    // Create the backoff policy properties
    var backoff = BackoffPolicyPropertiesImpl.builder().type(BackoffPolicyType.CONSTANT)
        .initialDelay(Duration.ofSeconds(10))
        .retries(4)
        .build();

    // Make request with backoff properties as second paramter
    var request = OpenAiChatCompletionRequest.builder()
        .model("gpt-3.5-turbo")
        .messages(List.of(firstMessage, secondMessage))
        .build();

    var response = client.createChatCompletion(request, backoff);
  }
}

©2024 Pureinsights Technology Corporation