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

Commit 253aff4

Browse files
authored
Add latest Fine Tuning API (#360)
This api supports fine tuning chat models
1 parent 2a9abd0 commit 253aff4

File tree

14 files changed

+356
-1
lines changed

14 files changed

+356
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ as well as an example project using the service.
2121
- [Embeddings](https://platform.openai.com/docs/api-reference/embeddings)
2222
- [Audio](https://platform.openai.com/docs/api-reference/audio)
2323
- [Files](https://platform.openai.com/docs/api-reference/files)
24-
- [Fine-tunes](https://platform.openai.com/docs/api-reference/fine-tunes)
24+
- [Fine-tuning](https://platform.openai.com/docs/api-reference/fine-tuning)
2525
- [Images](https://platform.openai.com/docs/api-reference/images)
2626
- [Moderations](https://platform.openai.com/docs/api-reference/moderations)
2727

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

3132
## Importing
3233

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.theokanning.openai.fine_tuning;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.Data;
5+
6+
/**
7+
* An object representing an event in the lifecycle of a fine-tuning job
8+
*
9+
* https://platform.openai.com/docs/api-reference/fine-tuning/list-events
10+
*/
11+
@Data
12+
public class FineTuningEvent {
13+
/**
14+
* The type of object returned, should be "fine-tuneing.job.event".
15+
*/
16+
String object;
17+
18+
/**
19+
* The creation time in epoch seconds.
20+
*/
21+
@JsonProperty("created_at")
22+
Long createdAt;
23+
24+
/**
25+
* The log level of this message.
26+
*/
27+
String level;
28+
29+
/**
30+
* The event message.
31+
*/
32+
String message;
33+
34+
/**
35+
* The type of event, i.e. "message"
36+
*/
37+
String type;
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.theokanning.openai.fine_tuning;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.Data;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Fine-tuning job
10+
* https://platform.openai.com/docs/api-reference/fine-tuning/object
11+
*/
12+
@Data
13+
public class FineTuningJob {
14+
/**
15+
* The object identifier, which can be referenced in the API endpoints.
16+
*/
17+
String id;
18+
19+
/**
20+
* The object type, which is always "fine_tuning.job".
21+
*/
22+
String object;
23+
24+
/**
25+
* The unix timestamp for when the fine-tuning job was created.
26+
*/
27+
@JsonProperty("created_at")
28+
Long createdAt;
29+
30+
/**
31+
* The unix timestamp for when the fine-tuning job was finished.
32+
*/
33+
@JsonProperty("finished_at")
34+
Long finishedAt;
35+
36+
/**
37+
* The base model that is being fine-tuned.
38+
*/
39+
String model;
40+
41+
/**
42+
* The name of the fine-tuned model that is being created.
43+
* Can be null if no fine-tuned model is created yet.
44+
*/
45+
@JsonProperty("fine_tuned_model")
46+
String fineTunedModel;
47+
48+
/**
49+
* The organization that owns the fine-tuning job.
50+
*/
51+
@JsonProperty("organization_id")
52+
String organizationId;
53+
54+
/**
55+
* The current status of the fine-tuning job.
56+
* Can be either created, pending, running, succeeded, failed, or cancelled.
57+
*/
58+
String status;
59+
60+
/**
61+
* The hyperparameters used for the fine-tuning job.
62+
* See the fine-tuning guide for more details.
63+
*/
64+
Hyperparameters hyperparameters;
65+
66+
/**
67+
* The file ID used for training.
68+
*/
69+
@JsonProperty("training_file")
70+
String trainingFile;
71+
72+
/**
73+
* The file ID used for validation.
74+
* Can be null if validation is not used.
75+
*/
76+
@JsonProperty("validation_file")
77+
String validationFile;
78+
79+
/**
80+
* The compiled results files for the fine-tuning job.
81+
*/
82+
@JsonProperty("result_files")
83+
List<String> resultFiles;
84+
85+
/**
86+
* The total number of billable tokens processed by this fine-tuning job.
87+
*/
88+
@JsonProperty("trained_tokens")
89+
Integer trainedTokens;
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.theokanning.openai.fine_tuning;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.*;
5+
6+
7+
/**
8+
* Request to create a fine tuning job
9+
* https://platform.openai.com/docs/api-reference/fine-tuning/create
10+
*/
11+
@Builder
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
@Data
15+
public class FineTuningJobRequest {
16+
17+
/**
18+
* The ID of an uploaded file that contains training data.
19+
*/
20+
@NonNull
21+
@JsonProperty("training_file")
22+
String trainingFile;
23+
24+
/**
25+
* The ID of an uploaded file that contains validation data.
26+
* Optional.
27+
*/
28+
@JsonProperty("validation_file")
29+
String validationFile;
30+
31+
/**
32+
* The name of the model to fine-tune.
33+
*/
34+
@NonNull
35+
String model;
36+
37+
/**
38+
* The hyperparameters used for the fine-tuning job.
39+
*/
40+
Hyperparameters hyperparameters;
41+
42+
/**
43+
* A string of up to 40 characters that will be added to your fine-tuned model name.
44+
*/
45+
String suffix;
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.theokanning.openai.fine_tuning;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
6+
import lombok.Data;
7+
import lombok.NoArgsConstructor;
8+
9+
10+
/**
11+
* Hyperparameters for a fine-tuning job
12+
* https://platform.openai.com/docs/api-reference/fine-tuning/object#hyperparameters
13+
*/
14+
@Builder
15+
@NoArgsConstructor
16+
@AllArgsConstructor
17+
@Data
18+
public class Hyperparameters {
19+
20+
/**
21+
* The number of epochs to train the model for.
22+
* An epoch refers to one full cycle through the training dataset.
23+
* "Auto" decides the optimal number of epochs based on the size of the dataset.
24+
* If setting the number manually, we support any number between 1 and 50 epochs.
25+
*/
26+
@JsonProperty("n_epochs")
27+
Integer nEpochs;
28+
}

api/src/main/java/com/theokanning/openai/finetune/FineTuneEvent.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
* https://beta.openai.com/docs/api-reference/fine-tunes
1010
*/
11+
@Deprecated
1112
@Data
1213
public class FineTuneEvent {
1314
/**

api/src/main/java/com/theokanning/openai/finetune/FineTuneRequest.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* https://beta.openai.com/docs/api-reference/fine-tunes/create
1313
*/
14+
@Deprecated
1415
@Builder
1516
@NoArgsConstructor
1617
@AllArgsConstructor

api/src/main/java/com/theokanning/openai/finetune/FineTuneResult.java

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* https://beta.openai.com/docs/api-reference/fine-tunes
1313
*/
14+
@Deprecated
1415
@Data
1516
public class FineTuneResult {
1617
/**

api/src/main/java/com/theokanning/openai/finetune/HyperParameters.java

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*
99
* https://beta.openai.com/docs/api-reference/fine-tunes
1010
*/
11+
@Deprecated
1112
@Data
1213
public class HyperParameters {
1314

client/src/main/java/com/theokanning/openai/client/OpenAiApi.java

+23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
import com.theokanning.openai.embedding.EmbeddingResult;
1717
import com.theokanning.openai.engine.Engine;
1818
import com.theokanning.openai.file.File;
19+
import com.theokanning.openai.fine_tuning.FineTuningEvent;
20+
import com.theokanning.openai.fine_tuning.FineTuningJob;
21+
import com.theokanning.openai.fine_tuning.FineTuningJobRequest;
1922
import com.theokanning.openai.finetune.FineTuneEvent;
2023
import com.theokanning.openai.finetune.FineTuneRequest;
2124
import com.theokanning.openai.finetune.FineTuneResult;
@@ -86,21 +89,41 @@ public interface OpenAiApi {
8689
@GET("/v1/files/{file_id}")
8790
Single<File> retrieveFile(@Path("file_id") String fileId);
8891

92+
@POST("/v1/fine_tuning/jobs")
93+
Single<FineTuningJob> createFineTuningJob(@Body FineTuningJobRequest request);
94+
95+
@GET("/v1/fine_tuning/jobs")
96+
Single<OpenAiResponse<FineTuningJob>> listFineTuningJobs();
97+
98+
@GET("/v1/fine_tuning/jobs/{fine_tuning_job_id}")
99+
Single<FineTuningJob> retrieveFineTuningJob(@Path("fine_tuning_job_id") String fineTuningJobId);
100+
101+
@POST("/v1/fine_tuning/jobs/{fine_tuning_job_id}/cancel")
102+
Single<FineTuningJob> cancelFineTuningJob(@Path("fine_tuning_job_id") String fineTuningJobId);
103+
104+
@GET("/v1/fine_tuning/jobs/{fine_tuning_job_id}/events")
105+
Single<OpenAiResponse<FineTuningEvent>> listFineTuningJobEvents(@Path("fine_tuning_job_id") String fineTuningJobId);
106+
107+
@Deprecated
89108
@POST("/v1/fine-tunes")
90109
Single<FineTuneResult> createFineTune(@Body FineTuneRequest request);
91110

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

114+
@Deprecated
95115
@GET("/v1/fine-tunes")
96116
Single<OpenAiResponse<FineTuneResult>> listFineTunes();
97117

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

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

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

service/src/main/java/com/theokanning/openai/service/OpenAiService.java

+28
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import com.theokanning.openai.embedding.EmbeddingRequest;
2525
import com.theokanning.openai.embedding.EmbeddingResult;
2626
import com.theokanning.openai.file.File;
27+
import com.theokanning.openai.fine_tuning.FineTuningEvent;
28+
import com.theokanning.openai.fine_tuning.FineTuningJob;
29+
import com.theokanning.openai.fine_tuning.FineTuningJobRequest;
2730
import com.theokanning.openai.finetune.FineTuneEvent;
2831
import com.theokanning.openai.finetune.FineTuneRequest;
2932
import com.theokanning.openai.finetune.FineTuneResult;
@@ -170,6 +173,27 @@ public File retrieveFile(String fileId) {
170173
return execute(api.retrieveFile(fileId));
171174
}
172175

176+
public FineTuningJob createFineTuningJob(FineTuningJobRequest request) {
177+
return execute(api.createFineTuningJob(request));
178+
}
179+
180+
public List<FineTuningJob> listFineTuningJobs() {
181+
return execute(api.listFineTuningJobs()).data;
182+
}
183+
184+
public FineTuningJob retrieveFineTuningJob(String fineTuningJobId) {
185+
return execute(api.retrieveFineTuningJob(fineTuningJobId));
186+
}
187+
188+
public FineTuningJob cancelFineTuningJob(String fineTuningJobId) {
189+
return execute(api.cancelFineTuningJob(fineTuningJobId));
190+
}
191+
192+
public List<FineTuningEvent> listFineTuningJobEvents(String fineTuningJobId) {
193+
return execute(api.listFineTuningJobEvents(fineTuningJobId)).data;
194+
}
195+
196+
@Deprecated
173197
public FineTuneResult createFineTune(FineTuneRequest request) {
174198
return execute(api.createFineTune(request));
175199
}
@@ -178,18 +202,22 @@ public CompletionResult createFineTuneCompletion(CompletionRequest request) {
178202
return execute(api.createFineTuneCompletion(request));
179203
}
180204

205+
@Deprecated
181206
public List<FineTuneResult> listFineTunes() {
182207
return execute(api.listFineTunes()).data;
183208
}
184209

210+
@Deprecated
185211
public FineTuneResult retrieveFineTune(String fineTuneId) {
186212
return execute(api.retrieveFineTune(fineTuneId));
187213
}
188214

215+
@Deprecated
189216
public FineTuneResult cancelFineTune(String fineTuneId) {
190217
return execute(api.cancelFineTune(fineTuneId));
191218
}
192219

220+
@Deprecated
193221
public List<FineTuneEvent> listFineTuneEvents(String fineTuneId) {
194222
return execute(api.listFineTuneEvents(fineTuneId)).data;
195223
}

service/src/test/java/com/theokanning/openai/service/FineTuneTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

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

13+
@Deprecated
1314
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
1415
public class FineTuneTest {
1516
static com.theokanning.openai.service.OpenAiService service;

0 commit comments

Comments
 (0)