Skip to content

Commit c0f93ad

Browse files
committed
[FineTuning] Support create job
1 parent b408733 commit c0f93ad

5 files changed

Lines changed: 95 additions & 8 deletions

File tree

src/main/java/org/devlive/sdk/openai/DefaultApi.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.devlive.sdk.openai.entity.EditEntity;
99
import org.devlive.sdk.openai.entity.EmbeddingEntity;
1010
import org.devlive.sdk.openai.entity.FileEntity;
11+
import org.devlive.sdk.openai.entity.FineTuningEntity;
1112
import org.devlive.sdk.openai.entity.ImageEntity;
1213
import org.devlive.sdk.openai.entity.ModelEntity;
1314
import org.devlive.sdk.openai.entity.ModerationEntity;
@@ -196,6 +197,18 @@ Single<FileEntity> fetchUploadFile(@Url String url,
196197
@GET
197198
Single<Object> fetchRetrieveFileContent(@Url String url);
198199

200+
/**
201+
* List your organization's fine-tuning jobs
202+
* 列出组织的微调作业
203+
*/
199204
@GET
200205
Single<FineTuningResponse> fetchFineTuningJobs(@Url String url);
206+
207+
/**
208+
* Creates a job that fine-tunes a specified model from a given dataset.
209+
* 创建一个作业,用于微调给定数据集中的指定模型。
210+
*/
211+
@POST
212+
Single<FineTuningResponse> fetchCreateFineTuningJob(@Url String url,
213+
@Body FineTuningEntity configure);
201214
}

src/main/java/org/devlive/sdk/openai/DefaultClient.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.devlive.sdk.openai.entity.EditEntity;
1717
import org.devlive.sdk.openai.entity.EmbeddingEntity;
1818
import org.devlive.sdk.openai.entity.FileEntity;
19+
import org.devlive.sdk.openai.entity.FineTuningEntity;
1920
import org.devlive.sdk.openai.entity.ImageEntity;
2021
import org.devlive.sdk.openai.entity.ModelEntity;
2122
import org.devlive.sdk.openai.entity.ModerationEntity;
@@ -219,6 +220,12 @@ public FineTuningResponse fineTuningJobs()
219220
.blockingGet();
220221
}
221222

223+
public FineTuningResponse createFineTuningJob(FineTuningEntity configure)
224+
{
225+
return this.api.fetchCreateFineTuningJob(ProviderUtils.getUrl(provider, UrlModel.FETCH_FINE_TUNING_JOBS), configure)
226+
.blockingGet();
227+
}
228+
222229
private ObjectMapper createObjectMapper()
223230
{
224231
ObjectMapper objectMapper = new ObjectMapper();

src/main/java/org/devlive/sdk/openai/entity/FineTuningEntity.java

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import lombok.NoArgsConstructor;
1010
import lombok.ToString;
1111
import org.devlive.sdk.openai.exception.ParamException;
12+
import org.devlive.sdk.openai.model.CompletionModel;
1213

1314
@Data
1415
@Builder
@@ -32,6 +33,20 @@ public class FineTuningEntity
3233
@JsonProperty(value = "limit")
3334
private Integer limit;
3435

36+
/**
37+
* The name of the model to fine-tune.
38+
* 要微调的模型的名称。
39+
*/
40+
@JsonProperty(value = "model")
41+
private String model;
42+
43+
/**
44+
* The ID of an uploaded file that contains training data.
45+
* 包含训练数据的已上传文件的 ID。
46+
*/
47+
@JsonProperty(value = "training_file")
48+
private String file;
49+
3550
@JsonProperty(value = "object")
3651
private String object;
3752

@@ -57,23 +72,33 @@ public class FineTuningEntity
5772
private FineTuningEntity(FineTuningEntityBuilder builder)
5873
{
5974
if (builder.limit == null) {
60-
builder.limit(20);
75+
builder.limit(null);
6176
}
6277
this.limit = builder.limit;
6378
this.after = builder.after;
79+
80+
if (builder.model == null) {
81+
builder.model(CompletionModel.GPT_35_TURBO);
82+
}
83+
this.model = builder.model;
84+
85+
if (builder.file == null) {
86+
builder.file(null);
87+
}
88+
this.file = builder.file;
6489
}
6590

6691
public static class FineTuningEntityBuilder
6792
{
6893
public FineTuningEntityBuilder limit(Integer limit)
6994
{
70-
if (limit == null) {
71-
limit = 20;
72-
}
73-
74-
if (limit < 1) {
75-
throw new ParamException("Invalid limit must not be less than 1");
76-
}
95+
// if (limit == null) {
96+
// limit = 20;
97+
// }
98+
//
99+
// if (limit < 1) {
100+
// throw new ParamException("Invalid limit must not be less than 1");
101+
// }
77102
this.limit = limit;
78103
return this;
79104
}
@@ -84,6 +109,35 @@ public FineTuningEntityBuilder after(String after)
84109
return this;
85110
}
86111

112+
public FineTuningEntityBuilder model(CompletionModel model)
113+
{
114+
if (model == null) {
115+
model = CompletionModel.GPT_35_TURBO;
116+
}
117+
118+
switch (model) {
119+
case GPT_35_TURBO:
120+
case GPT_35_TURBO_0613:
121+
case BABBAGE_002:
122+
case DAVINCI_002:
123+
case GPT_4_0613:
124+
this.model = model.getName();
125+
break;
126+
default:
127+
throw new ParamException(String.format("Not support completion model %s", model));
128+
}
129+
return this;
130+
}
131+
132+
public FineTuningEntityBuilder file(String file)
133+
{
134+
if (file == null) {
135+
throw new ParamException("Invalid file name must not be empty");
136+
}
137+
this.file = file;
138+
return this;
139+
}
140+
87141
public FineTuningEntity build()
88142
{
89143
return new FineTuningEntity(this);

src/main/java/org/devlive/sdk/openai/model/CompletionModel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public enum CompletionModel
1616
"Capable of straightforward tasks, very fast, and lower cost.",
1717
null,
1818
2049),
19+
BABBAGE_002("babbage-002", null, null, 2049),
1920
BABBAGE_CODE_SEARCH_CODE("babbage-code-search-code", null, null, 4096),
2021
BABBAGE_CODE_SEARCH_TEXT("babbage-code-search-text", null, null, 4096),
2122
BABBAGE_SEARCH_DOCUMENT("babbage-search-document", null, null, 4096),
@@ -38,6 +39,7 @@ public enum CompletionModel
3839
"Most capable GPT-3 model. Can do any task the other models can do, often with higher quality.",
3940
null,
4041
2049),
42+
DAVINCI_002("davinci-002", null, null, 2049),
4143
DAVINCI_INSTRUCT_BETA("davinci-instruct-beta", null, null, 4096),
4244
DAVINCI_SEARCH_DOCUMENT("davinci-search-document", null, null, 4096),
4345
DAVINCI_SEARCH_QUERY("davinci-search-query", null, null, 4096),

src/test/java/org/devlive/sdk/openai/FineTuningTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.devlive.sdk.openai;
22

3+
import org.devlive.sdk.openai.entity.FineTuningEntity;
4+
import org.devlive.sdk.openai.exception.RequestException;
35
import org.junit.Assert;
46
import org.junit.Before;
57
import org.junit.Test;
@@ -21,4 +23,13 @@ public void testFineTuningJobs()
2123
{
2224
Assert.assertNotNull(client.fineTuningJobs());
2325
}
26+
27+
@Test
28+
public void testCreateFineTuningJob()
29+
{
30+
FineTuningEntity entity = FineTuningEntity.builder()
31+
.file("file-Rcxv6RrQXzTuJ2i2LY9kuChL")
32+
.build();
33+
Assert.assertThrows(RequestException.class, () -> client.createFineTuningJob(entity));
34+
}
2435
}

0 commit comments

Comments
 (0)