Site icon Experiences Unlimited

Integrate with OpenAI(ChatGPT) Chat Completion API in Java using Webclient

In my previous post I showed how you can integrate with Completion API. The completion API doesnt support the ChatpGPT model. The ChatGPT model is supported in the Chat completion API. In this post we will look at integrating with this new API using WebClient.

Below are the POJOs we will be using for parsing the response

@Data
public class ChatCompletionResponse {
String id;
String object;
Long createdOn;
String model;
List<ChatCompletionResponseChoice> choices;
ChatCompletionResponseUsage usage;
}
@Data
public class ChatCompletionResponseChoice {
ChatCompletionResponseChoiceMessage message;
Integer index;
String finishReason;
}
@Data
public class ChatCompletionResponseChoiceMessage {
String role;
String content;
}
@Data
public class ChatCompletionResponseUsage {
Integer promptTokens;
Integer completionTokens;
Integer totalTokens;
}

The input prompt to this API is provided in a different structure as shown below:

Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "gpt-3.5-turbo");
requestBody.put("messages", Arrays.asList(
Map.of("role", "user",
"content", "10 tips for time management for married working professional with kids")
));
requestBody.put("max_tokens", 500);
requestBody.put("temperature", 0.5);

We can even provide a context for the model to work on by providing the system information as part of the messages as shown below:

Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "gpt-3.5-turbo");
requestBody.put("messages", Arrays.asList(
Map.of("role", "system",
"content", "You are a helpful self help guide that provides advice on time management"),
Map.of("role", "user",
"content", "10 tips for time management for married working professional with kids")
));
requestBody.put("max_tokens", 500);
requestBody.put("temperature", 0.5);

Next is to pass the request body to the API and then process its response as shown below:

String url = "https://api.openai.com/v1/chat/completions";
ObjectMapper objectMapper = new ObjectMapper();
//System.out.println(objectMapper.writeValueAsString(requestBody));
Mono<ChatCompletionResponse> completionResponseMono = webClient.post()
.uri(url)
.headers(httpHeaders -> {
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.setBearerAuth(secret);
})
.bodyValue(objectMapper.writeValueAsString(requestBody))
.exchangeToMono(clientResponse -> {
HttpStatusCode httpStatus = clientResponse.statusCode();
if (httpStatus.is2xxSuccessful()) {
return clientResponse.bodyToMono(ChatCompletionResponse.class);
} else {
Mono<String> stringMono = clientResponse.bodyToMono(String.class);
stringMono.subscribe(s -> {
System.err.println("Response from Open AI API " + s);
});
System.err.println("Error occurred while invoking Open AI API");
return Mono.error(new Exception(
"Error occurred while generating wordage"));
}
});
ChatCompletionResponse completionResponse = completionResponseMono.block();
List<ChatCompletionResponseChoice> choices = completionResponse.getChoices();
ChatCompletionResponseChoice aChoice = choices.get(0);
System.out.println(aChoice.getMessage().getContent());
Exit mobile version