Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CompletionModel class - narrower scope #5

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(OpenAIModel.GPT_3_5_TURBO)
.message(ChatMessage.userMessage("Who won the world series in 2020?"))
.build();
ChatCompletion chatCompletion = chatClient.createChatCompletion(createChatCompletionRequest);
Expand All @@ -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) | ✔️ |
Expand All @@ -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) | ✔️ |

Expand Down Expand Up @@ -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(OpenAIModel.GPT_3_5_TURBO)
.message(ChatMessage.userMessage("Who won the world series in 2020?"))
.build();
CompletableFuture<ChatCompletion> chatCompletion = chatClient.createChatCompletionAsync(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static Builder newBuilder() {

public static class Builder {

private static final String DEFAULT_MODEL = "gpt-4";
private static final String DEFAULT_MODEL = OpenAIModel.GPT_4.getId();

private String model = DEFAULT_MODEL;

Expand All @@ -41,6 +41,14 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to use
*/
public Builder model(OpenAIModel model) {
this.model = model.getId();
return this;
}

/**
* @param name The name of the assistant. The maximum length is 256 characters.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = OpenAIModel.GPT_3_5_TURBO.getId();

private final List<ChatMessage> messages = new LinkedList<>();

Expand Down Expand Up @@ -88,6 +88,14 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to use
*/
public Builder model(OpenAIModel 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to fine-tune
*/
public Builder model(OpenAIModel model) {
this.model = model.getId();
return this;
}

/**
* @param trainingFile The ID of an uploaded file that contains training data. Your dataset must
* be formatted as a JSONL file. Additionally, you must upload your file with the purpose
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to use for image generation.
*/
public Builder model(OpenAIModel model) {
this.model = Optional.of(model.getId());
return this;
}

/**
* @param n The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1
* is supported.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to use for image generation. Only {@link
* OpenAIModel#DALL_E_2} is supported at this time.
*/
public Builder model(OpenAIModel model) {
this.model = Optional.of(model.getId());
return this;
}

/**
* @param n The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1
* is supported.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to be used to execute this run. If a value is provided here,
* it will override the model associated with the assistant. If not, the model associated
* with the assistant will be used.
*/
public Builder model(OpenAIModel model) {
this.model = Optional.of(model.getId());
return this;
}

/**
* @param instructions Overrides the instructions of the assistant. This is useful for modifying
* the behavior on a per-run basis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to be used to execute this run. If a value is provided here,
* it will override the model associated with the assistant. If not, the model associated
* with the assistant will be used.
*/
public Builder model(OpenAIModel model) {
this.model = Optional.of(model.getId());
return this;
}

/**
* @param instructions Overrides the instructions of the assistant. This is useful for modifying
* the behavior on a per-run basis.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to use for image generation. Only {@link
* OpenAIModel#DALL_E_2} is supported at this time.
*/
public Builder model(OpenAIModel model) {
this.model = Optional.of(model.getId());
return this;
}

/**
* @param n The number of images to generate. Must be between 1 and 10.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to use
*/
public Builder model(OpenAIModel model) {
this.model = model.getId();
return this;
}

/**
* @param encodingFormat The format to return the embeddings in. Can be either float or base64.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public Builder model(String model) {
return this;
}

/**
* @param model Two content moderations models are available: {@link
* OpenAIModel#TEXT_MODERATION_LATEST} and {@link OpenAIModel#TEXT_MODERATION_STABLE}.
*/
public Builder model(OpenAIModel model) {
this.model = Optional.of(model.getId());
return this;
}

public ModerationRequest build() {
return new ModerationRequest(List.copyOf(input), model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to use.
*/
public Builder model(OpenAIModel model) {
this.model = Optional.of(model.getId());
return this;
}

/**
* @param name The name of the assistant. The maximum length is 256 characters.
*/
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/io/github/stefanbratanov/jvm/openai/OpenAIModel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.github.stefanbratanov.jvm.openai;

/**
* Represents the latest OpenAI models.
*
* <p>Note that this does not correspond to a static version and may change over time.
*
* <p>To see the compatibility of the models with different API endpoints, refer to <a
* href="https://platform.openai.com/docs/models/model-endpoint-compatibility">Model endpoint
* compatibility</a>.
*
* @see <a href="https://platform.openai.com/docs/models/continuous-model-upgrades">Continuous model
* upgrades - OpenAI API</a>
*/
public enum OpenAIModel {

// GPT-4 and GPT-4 Turbo (https://platform.openai.com/docs/models/gpt-3-5-turbo)
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 (https://platform.openai.com/docs/models/gpt-3-5-turbo)
GPT_3_5_TURBO("gpt-3.5-turbo"),

// DALL·E (https://platform.openai.com/docs/models/dall-e)
DALL_E_3("dall-e-3"),
DALL_E_2("dall-e-2"),

// TTS (https://platform.openai.com/docs/models/tts)
TTS_1("tts-1"),
TTS_1_HD("tts-1-hd"),

// whisper (https://platform.openai.com/docs/models/whisper)
WHISPER_1("whisper-1"),

// Embeddings (https://platform.openai.com/docs/models/embeddings)
TEXT_EMBEDDING_3_LARGE("text-embedding-3-large"),
TEXT_EMBEDDING_3_SMALL("text-embedding-3-small"),
TEXT_EMBEDDING_ADA_002("text-embedding-ada-002"),

// Moderation (https://platform.openai.com/docs/models/moderation)
TEXT_MODERATION_LATEST("text-moderation-latest"),
TEXT_MODERATION_STABLE("text-moderation-stable");

private final String id;

OpenAIModel(String modelId) {
this.id = modelId;
}

public String getId() {
return this.id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static Builder newBuilder() {

public static class Builder {

private static final String DEFAULT_MODEL = "tts-1";
private static final String DEFAULT_MODEL = OpenAIModel.TTS_1.getId();
private static final String DEFAULT_VOICE = "alloy";

private String model = DEFAULT_MODEL;
Expand All @@ -33,6 +33,15 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to use. {@link OpenAIModel#TTS_1} and {@link
* OpenAIModel#TTS_1_HD} are available.
*/
public Builder model(OpenAIModel model) {
this.model = model.getId();
return this;
}

/**
* @param input The text to generate audio for
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static Builder newBuilder() {

public static class Builder {

private static final String DEFAULT_MODEL = "whisper-1";
private static final String DEFAULT_MODEL = OpenAIModel.WHISPER_1.getId();

private Path file;
private String model = DEFAULT_MODEL;
Expand All @@ -46,6 +46,14 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to use
*/
public Builder model(OpenAIModel model) {
this.model = model.getId();
return this;
}

/**
* @param language The language of the input audio. Supplying the input language in <a
* href="https://en.wikipedia.org/wiki/List_of_ISO_639_language_codes">ISO-639-1</a> format
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static Builder newBuilder() {

public static class Builder {

private static final String DEFAULT_MODEL = "whisper-1";
private static final String DEFAULT_MODEL = OpenAIModel.WHISPER_1.getId();

private Path file;
private String model = DEFAULT_MODEL;
Expand All @@ -41,6 +41,14 @@ public Builder model(String model) {
return this;
}

/**
* @param model {@link OpenAIModel} to use
*/
public Builder model(OpenAIModel model) {
this.model = model.getId();
return this;
}

/**
* @param prompt An optional text to guide the model's style or continue a previous audio
* segment. The <a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ void testAudioClient(@TempDir Path tempDir) {

SpeechRequest speechRequest =
SpeechRequest.newBuilder()
.model("tts-1")
.model(OpenAIModel.TTS_1)
.input("The quick brown fox jumped over the lazy dog.")
.voice("alloy")
.build();
Expand All @@ -190,7 +190,7 @@ void testAudioClient(@TempDir Path tempDir) {
TranscriptionRequest transcriptionRequest =
TranscriptionRequest.newBuilder()
.file(speech)
.model("whisper-1")
.model(OpenAIModel.WHISPER_1)
.responseFormat("text")
.build();

Expand All @@ -204,7 +204,7 @@ void testAudioClient(@TempDir Path tempDir) {
TranslationRequest translationRequest =
TranslationRequest.newBuilder()
.file(greeting)
.model("whisper-1")
.model(OpenAIModel.WHISPER_1)
.responseFormat("json")
.build();

Expand Down Expand Up @@ -311,7 +311,7 @@ void testFineTuningClient() {
CreateFineTuningJobRequest createFineTuningJobRequest =
CreateFineTuningJobRequest.newBuilder()
.trainingFile("123abc")
.model("gpt-3.5-turbo")
.model(OpenAIModel.GPT_3_5_TURBO)
.build();

FineTuningJob createdFineTuningJob =
Expand Down
Loading
Loading