From 3788accd024cc72e77187b24ce21662013ab91aa Mon Sep 17 00:00:00 2001 From: Maciej Czarnecki Date: Thu, 28 Mar 2024 16:36:49 +0100 Subject: [PATCH] Add CompletionModel class --- README.md | 8 ++--- .../jvm/openai/CompletionModel.java | 32 +++++++++++++++++++ .../openai/CreateChatCompletionRequest.java | 10 +++++- 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 src/main/java/io/github/stefanbratanov/jvm/openai/CompletionModel.java diff --git a/README.md b/README.md index 6f396a0..66b8360 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ OpenAI openAI = OpenAI.newBuilder(System.getenv("OPENAI_API_KEY")).build(); ChatClient chatClient = openAI.chatClient(); CreateChatCompletionRequest createChatCompletionRequest = CreateChatCompletionRequest.newBuilder() - .model("gpt-3.5-turbo") + .model(CompletionModel.GPT_3_5_TURBO) .message(ChatMessage.userMessage("Who won the world series in 2020?")) .build(); ChatCompletion chatCompletion = chatClient.createChatCompletion(createChatCompletionRequest); @@ -46,7 +46,7 @@ ChatCompletion chatCompletion = chatClient.createChatCompletion(createChatComple | API | Status | |---------------------------------------------------------------------------|:------:| | [Audio](https://platform.openai.com/docs/api-reference/audio) | ✔️ | -| [Chat](https://platform.openai.com/docs/api-reference/chat) | ️ ✔️ | +| [Chat](https://platform.openai.com/docs/api-reference/chat) | ✔️ | | [Embeddings](https://platform.openai.com/docs/api-reference/embeddings) | ✔️ | | [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tuning) | ✔️ | | [Files](https://platform.openai.com/docs/api-reference/files) | ✔️ | @@ -59,7 +59,7 @@ ChatCompletion chatCompletion = chatClient.createChatCompletion(createChatComple | API | Status | |-------------------------------------------------------------------------|:------:| | [Assistants](https://platform.openai.com/docs/api-reference/assistants) | ✔️ | -| [Threads](https://platform.openai.com/docs/api-reference/threads) | ️ ✔️ | +| [Threads](https://platform.openai.com/docs/api-reference/threads) | ✔️ | | [Messages](https://platform.openai.com/docs/api-reference/messages) | ✔️ | | [Runs](https://platform.openai.com/docs/api-reference/runs) | ✔️ | @@ -92,7 +92,7 @@ OpenAI openAI = OpenAI.newBuilder(System.getenv("OPENAI_API_KEY")) ```java ChatClient chatClient = openAI.chatClient(); CreateChatCompletionRequest request = CreateChatCompletionRequest.newBuilder() - .model("gpt-3.5-turbo") + .model(CompletionModel.GPT_3_5_TURBO) .message(ChatMessage.userMessage("Who won the world series in 2020?")) .build(); CompletableFuture chatCompletion = chatClient.createChatCompletionAsync(request); diff --git a/src/main/java/io/github/stefanbratanov/jvm/openai/CompletionModel.java b/src/main/java/io/github/stefanbratanov/jvm/openai/CompletionModel.java new file mode 100644 index 0000000..4c65d64 --- /dev/null +++ b/src/main/java/io/github/stefanbratanov/jvm/openai/CompletionModel.java @@ -0,0 +1,32 @@ +package io.github.stefanbratanov.jvm.openai; + +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * Represents the latest models that can be used to generate completions. + * + *

Note that this does not correspond to a static version and may change over time. + * + * @see Continuous model + * upgrades - OpenAI API + */ +public enum CompletionModel { + + GPT_4("gpt-4"), + GPT_4_TURBO_PREVIEW("gpt-4-turbo-preview"), + GPT_4_VISION_PREVIEW("gpt-4-vision-preview"), + GPT_4_32K("gpt-4-32k"), + GPT_3_5_TURBO("gpt-3.5-turbo"), + GPT_3_5_TURBO_16K("gpt-3.5-turbo-16k"); + + private final String id; + + CompletionModel(String modelId) { + this.id = modelId; + } + + @JsonValue + public String getId() { + return this.id; + } +} diff --git a/src/main/java/io/github/stefanbratanov/jvm/openai/CreateChatCompletionRequest.java b/src/main/java/io/github/stefanbratanov/jvm/openai/CreateChatCompletionRequest.java index ec90356..fac8a92 100644 --- a/src/main/java/io/github/stefanbratanov/jvm/openai/CreateChatCompletionRequest.java +++ b/src/main/java/io/github/stefanbratanov/jvm/openai/CreateChatCompletionRequest.java @@ -41,7 +41,7 @@ public static Builder newBuilder() { public static class Builder { - private static final String DEFAULT_MODEL = "gpt-3.5-turbo"; + private static final String DEFAULT_MODEL = CompletionModel.GPT_3_5_TURBO.getId(); private final List messages = new LinkedList<>(); @@ -88,6 +88,14 @@ public Builder model(String model) { return this; } + /** + * @param model {@link CompletionModel} to use + */ + public Builder model(CompletionModel model) { + this.model = model.getId(); + return this; + } + /** * @param frequencyPenalty Number between -2.0 and 2.0. Positive values penalize new tokens * based on their existing frequency in the text so far, decreasing the model's likelihood