Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
Add latest Fine Tuning API (#360)
Browse files Browse the repository at this point in the history
This api supports fine tuning chat models
  • Loading branch information
TheoKanning authored Aug 25, 2023
1 parent 2a9abd0 commit 253aff4
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ as well as an example project using the service.
- [Embeddings](https://platform.openai.com/docs/api-reference/embeddings)
- [Audio](https://platform.openai.com/docs/api-reference/audio)
- [Files](https://platform.openai.com/docs/api-reference/files)
- [Fine-tunes](https://platform.openai.com/docs/api-reference/fine-tunes)
- [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tuning)
- [Images](https://platform.openai.com/docs/api-reference/images)
- [Moderations](https://platform.openai.com/docs/api-reference/moderations)

#### Deprecated by OpenAI
- [Engines](https://platform.openai.com/docs/api-reference/engines)
- [Legacy Fine-Tunes](https://platform.openai.com/docs/guides/legacy-fine-tuning)

## Importing

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.theokanning.openai.fine_tuning;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
* An object representing an event in the lifecycle of a fine-tuning job
*
* https://platform.openai.com/docs/api-reference/fine-tuning/list-events
*/
@Data
public class FineTuningEvent {
/**
* The type of object returned, should be "fine-tuneing.job.event".
*/
String object;

/**
* The creation time in epoch seconds.
*/
@JsonProperty("created_at")
Long createdAt;

/**
* The log level of this message.
*/
String level;

/**
* The event message.
*/
String message;

/**
* The type of event, i.e. "message"
*/
String type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.theokanning.openai.fine_tuning;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.util.List;

/**
* Fine-tuning job
* https://platform.openai.com/docs/api-reference/fine-tuning/object
*/
@Data
public class FineTuningJob {
/**
* The object identifier, which can be referenced in the API endpoints.
*/
String id;

/**
* The object type, which is always "fine_tuning.job".
*/
String object;

/**
* The unix timestamp for when the fine-tuning job was created.
*/
@JsonProperty("created_at")
Long createdAt;

/**
* The unix timestamp for when the fine-tuning job was finished.
*/
@JsonProperty("finished_at")
Long finishedAt;

/**
* The base model that is being fine-tuned.
*/
String model;

/**
* The name of the fine-tuned model that is being created.
* Can be null if no fine-tuned model is created yet.
*/
@JsonProperty("fine_tuned_model")
String fineTunedModel;

/**
* The organization that owns the fine-tuning job.
*/
@JsonProperty("organization_id")
String organizationId;

/**
* The current status of the fine-tuning job.
* Can be either created, pending, running, succeeded, failed, or cancelled.
*/
String status;

/**
* The hyperparameters used for the fine-tuning job.
* See the fine-tuning guide for more details.
*/
Hyperparameters hyperparameters;

/**
* The file ID used for training.
*/
@JsonProperty("training_file")
String trainingFile;

/**
* The file ID used for validation.
* Can be null if validation is not used.
*/
@JsonProperty("validation_file")
String validationFile;

/**
* The compiled results files for the fine-tuning job.
*/
@JsonProperty("result_files")
List<String> resultFiles;

/**
* The total number of billable tokens processed by this fine-tuning job.
*/
@JsonProperty("trained_tokens")
Integer trainedTokens;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.theokanning.openai.fine_tuning;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.*;


/**
* Request to create a fine tuning job
* https://platform.openai.com/docs/api-reference/fine-tuning/create
*/
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class FineTuningJobRequest {

/**
* The ID of an uploaded file that contains training data.
*/
@NonNull
@JsonProperty("training_file")
String trainingFile;

/**
* The ID of an uploaded file that contains validation data.
* Optional.
*/
@JsonProperty("validation_file")
String validationFile;

/**
* The name of the model to fine-tune.
*/
@NonNull
String model;

/**
* The hyperparameters used for the fine-tuning job.
*/
Hyperparameters hyperparameters;

/**
* A string of up to 40 characters that will be added to your fine-tuned model name.
*/
String suffix;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.theokanning.openai.fine_tuning;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;


/**
* Hyperparameters for a fine-tuning job
* https://platform.openai.com/docs/api-reference/fine-tuning/object#hyperparameters
*/
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Hyperparameters {

/**
* The number of epochs to train the model for.
* An epoch refers to one full cycle through the training dataset.
* "Auto" decides the optimal number of epochs based on the size of the dataset.
* If setting the number manually, we support any number between 1 and 50 epochs.
*/
@JsonProperty("n_epochs")
Integer nEpochs;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* https://beta.openai.com/docs/api-reference/fine-tunes
*/
@Deprecated
@Data
public class FineTuneEvent {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
* https://beta.openai.com/docs/api-reference/fine-tunes/create
*/
@Deprecated
@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
* https://beta.openai.com/docs/api-reference/fine-tunes
*/
@Deprecated
@Data
public class FineTuneResult {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* https://beta.openai.com/docs/api-reference/fine-tunes
*/
@Deprecated
@Data
public class HyperParameters {

Expand Down
23 changes: 23 additions & 0 deletions client/src/main/java/com/theokanning/openai/client/OpenAiApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import com.theokanning.openai.embedding.EmbeddingResult;
import com.theokanning.openai.engine.Engine;
import com.theokanning.openai.file.File;
import com.theokanning.openai.fine_tuning.FineTuningEvent;
import com.theokanning.openai.fine_tuning.FineTuningJob;
import com.theokanning.openai.fine_tuning.FineTuningJobRequest;
import com.theokanning.openai.finetune.FineTuneEvent;
import com.theokanning.openai.finetune.FineTuneRequest;
import com.theokanning.openai.finetune.FineTuneResult;
Expand Down Expand Up @@ -86,21 +89,41 @@ public interface OpenAiApi {
@GET("/v1/files/{file_id}")
Single<File> retrieveFile(@Path("file_id") String fileId);

@POST("/v1/fine_tuning/jobs")
Single<FineTuningJob> createFineTuningJob(@Body FineTuningJobRequest request);

@GET("/v1/fine_tuning/jobs")
Single<OpenAiResponse<FineTuningJob>> listFineTuningJobs();

@GET("/v1/fine_tuning/jobs/{fine_tuning_job_id}")
Single<FineTuningJob> retrieveFineTuningJob(@Path("fine_tuning_job_id") String fineTuningJobId);

@POST("/v1/fine_tuning/jobs/{fine_tuning_job_id}/cancel")
Single<FineTuningJob> cancelFineTuningJob(@Path("fine_tuning_job_id") String fineTuningJobId);

@GET("/v1/fine_tuning/jobs/{fine_tuning_job_id}/events")
Single<OpenAiResponse<FineTuningEvent>> listFineTuningJobEvents(@Path("fine_tuning_job_id") String fineTuningJobId);

@Deprecated
@POST("/v1/fine-tunes")
Single<FineTuneResult> createFineTune(@Body FineTuneRequest request);

@POST("/v1/completions")
Single<CompletionResult> createFineTuneCompletion(@Body CompletionRequest request);

@Deprecated
@GET("/v1/fine-tunes")
Single<OpenAiResponse<FineTuneResult>> listFineTunes();

@Deprecated
@GET("/v1/fine-tunes/{fine_tune_id}")
Single<FineTuneResult> retrieveFineTune(@Path("fine_tune_id") String fineTuneId);

@Deprecated
@POST("/v1/fine-tunes/{fine_tune_id}/cancel")
Single<FineTuneResult> cancelFineTune(@Path("fine_tune_id") String fineTuneId);

@Deprecated
@GET("/v1/fine-tunes/{fine_tune_id}/events")
Single<OpenAiResponse<FineTuneEvent>> listFineTuneEvents(@Path("fine_tune_id") String fineTuneId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import com.theokanning.openai.embedding.EmbeddingRequest;
import com.theokanning.openai.embedding.EmbeddingResult;
import com.theokanning.openai.file.File;
import com.theokanning.openai.fine_tuning.FineTuningEvent;
import com.theokanning.openai.fine_tuning.FineTuningJob;
import com.theokanning.openai.fine_tuning.FineTuningJobRequest;
import com.theokanning.openai.finetune.FineTuneEvent;
import com.theokanning.openai.finetune.FineTuneRequest;
import com.theokanning.openai.finetune.FineTuneResult;
Expand Down Expand Up @@ -170,6 +173,27 @@ public File retrieveFile(String fileId) {
return execute(api.retrieveFile(fileId));
}

public FineTuningJob createFineTuningJob(FineTuningJobRequest request) {
return execute(api.createFineTuningJob(request));
}

public List<FineTuningJob> listFineTuningJobs() {
return execute(api.listFineTuningJobs()).data;
}

public FineTuningJob retrieveFineTuningJob(String fineTuningJobId) {
return execute(api.retrieveFineTuningJob(fineTuningJobId));
}

public FineTuningJob cancelFineTuningJob(String fineTuningJobId) {
return execute(api.cancelFineTuningJob(fineTuningJobId));
}

public List<FineTuningEvent> listFineTuningJobEvents(String fineTuningJobId) {
return execute(api.listFineTuningJobEvents(fineTuningJobId)).data;
}

@Deprecated
public FineTuneResult createFineTune(FineTuneRequest request) {
return execute(api.createFineTune(request));
}
Expand All @@ -178,18 +202,22 @@ public CompletionResult createFineTuneCompletion(CompletionRequest request) {
return execute(api.createFineTuneCompletion(request));
}

@Deprecated
public List<FineTuneResult> listFineTunes() {
return execute(api.listFineTunes()).data;
}

@Deprecated
public FineTuneResult retrieveFineTune(String fineTuneId) {
return execute(api.retrieveFineTune(fineTuneId));
}

@Deprecated
public FineTuneResult cancelFineTune(String fineTuneId) {
return execute(api.cancelFineTune(fineTuneId));
}

@Deprecated
public List<FineTuneEvent> listFineTuneEvents(String fineTuneId) {
return execute(api.listFineTuneEvents(fineTuneId)).data;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import static org.junit.jupiter.api.Assertions.*;

@Deprecated
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class FineTuneTest {
static com.theokanning.openai.service.OpenAiService service;
Expand Down
Loading

1 comment on commit 253aff4

@raxitwalia
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code is really easy and good documentation easy to understand for any java developer Theo kannig

Please sign in to comment.