Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9a05c6a

Browse filesBrowse files
authored
Add file and fine-tune api support (TheoKanning#6)
1 parent d410abe commit 9a05c6a
Copy full SHA for 9a05c6a

File tree

Expand file treeCollapse file tree

14 files changed

+565
-7
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

14 files changed

+565
-7
lines changed
Open diff view settings
Collapse file
+24Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.theokanning.openai;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* A response when deleting an object
7+
*/
8+
@Data
9+
public class DeleteResult {
10+
/**
11+
* The id of the object.
12+
*/
13+
String id;
14+
15+
/**
16+
* The type of object deleted, for example "file" or "model"
17+
*/
18+
String object;
19+
20+
/**
21+
* True if successfully deleted
22+
*/
23+
boolean deleted;
24+
}
Collapse file

‎api/src/main/java/com/theokanning/openai/completion/CompletionRequest.java‎

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/completion/CompletionRequest.java
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@
2020
@Data
2121
public class CompletionRequest {
2222

23+
/**
24+
* The name of the model to use, only used if specifying a fine tuned model.
25+
*/
26+
String model;
27+
2328
/**
2429
* An optional prompt to complete from
2530
*/
Collapse file

‎api/src/main/java/com/theokanning/openai/completion/CompletionResult.java‎

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/completion/CompletionResult.java
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class CompletionResult {
2222
String object;
2323

2424
/**
25-
* The creation time in epoch milliseconds.
25+
* The creation time in epoch seconds.
2626
*/
2727
long created;
2828

Collapse file
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.theokanning.openai.file;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* A file uploaded to OpenAi
7+
*
8+
* https://beta.openai.com/docs/api-reference/files
9+
*/
10+
@Data
11+
public class File {
12+
13+
/**
14+
* The unique id of this file.
15+
*/
16+
String id;
17+
18+
/**
19+
* The type of object returned, should be "file".
20+
*/
21+
String object;
22+
23+
/**
24+
* File size in bytes.
25+
*/
26+
Long bytes;
27+
28+
/**
29+
* The creation time in epoch seconds.
30+
*/
31+
Long createdAt;
32+
33+
/**
34+
* The name of the file.
35+
*/
36+
String filename;
37+
38+
/**
39+
* Description of the file's purpose.
40+
*/
41+
String purpose;
42+
}
Collapse file
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.theokanning.openai.finetune;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* An object representing an event in the lifecycle of a fine-tuning job
7+
*
8+
* https://beta.openai.com/docs/api-reference/fine-tunes
9+
*/
10+
@Data
11+
public class FineTuneEvent {
12+
/**
13+
* The type of object returned, should be "fine-tune-event".
14+
*/
15+
String object;
16+
17+
/**
18+
* The creation time in epoch seconds.
19+
*/
20+
Long createdAt;
21+
22+
/**
23+
* The log level of this message.
24+
*/
25+
String level;
26+
27+
/**
28+
* The event message.
29+
*/
30+
String message;
31+
}
Collapse file
+111Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package com.theokanning.openai.finetune;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.util.List;
9+
10+
/**
11+
* A request for OpenAi to create a fine-tuned model
12+
* All fields except trainingFile are nullable.
13+
*
14+
* https://beta.openai.com/docs/api-reference/fine-tunes/create
15+
*/
16+
@Builder
17+
@NoArgsConstructor
18+
@AllArgsConstructor
19+
@Data
20+
public class FineTuneRequest {
21+
22+
/**
23+
* The ID of an uploaded file that contains training data.
24+
*/
25+
String trainingFile;
26+
27+
/**
28+
* The ID of an uploaded file that contains validation data.
29+
*/
30+
String validationFile;
31+
32+
/**
33+
* The name of the base model to fine-tune. You can select one of "ada", "babbage", "curie", or "davinci".
34+
* To learn more about these models, see the Engines documentation.
35+
*/
36+
String model;
37+
38+
/**
39+
* The number of epochs to train the model for. An epoch refers to one full cycle through the training dataset.
40+
*/
41+
Integer nEpochs;
42+
43+
/**
44+
* The batch size to use for training.
45+
* The batch size is the number of training examples used to train a single forward and backward pass.
46+
*
47+
* By default, the batch size will be dynamically configured to be ~0.2% of the number of examples in the training
48+
* set, capped at 256 - in general, we've found that larger batch sizes tend to work better for larger datasets.
49+
*/
50+
Integer batchSize;
51+
52+
/**
53+
* The learning rate multiplier to use for training.
54+
* The fine-tuning learning rate is the original learning rate used for pretraining multiplied by this value.
55+
*
56+
* By default, the learning rate multiplier is the 0.05, 0.1, or 0.2 depending on final batch_size
57+
* (larger learning rates tend to perform better with larger batch sizes).
58+
* We recommend experimenting with values in the range 0.02 to 0.2 to see what produces the best results.
59+
*/
60+
Double learningRateMultiplier;
61+
62+
/**
63+
* The weight to use for loss on the prompt tokens.
64+
* This controls how much the model tries to learn to generate the prompt
65+
* (as compared to the completion which always has a weight of 1.0),
66+
* and can add a stabilizing effect to training when completions are short.
67+
*
68+
* If prompts are extremely long (relative to completions), it may make sense to reduce this weight so as to
69+
* avoid over-prioritizing learning the prompt.
70+
*/
71+
Double promptLossWeight;
72+
73+
/**
74+
* If set, we calculate classification-specific metrics such as accuracy and F-1 score using the validation set
75+
* at the end of every epoch. These metrics can be viewed in the results file.
76+
*
77+
* In order to compute classification metrics, you must provide a validation_file.
78+
* Additionally, you must specify {@link FineTuneRequest#classificationNClasses} for multiclass
79+
* classification or {@link FineTuneRequest#classificationPositiveClass} for binary classification.
80+
*/
81+
Boolean computeClassificationMetrics;
82+
83+
/**
84+
* The number of classes in a classification task.
85+
*
86+
* This parameter is required for multiclass classification.
87+
*/
88+
Integer classificationNClasses; // todo verify snake case
89+
90+
/**
91+
* The positive class in binary classification.
92+
*
93+
* This parameter is needed to generate precision, recall, and F1 metrics when doing binary classification.
94+
*/
95+
String classificationPositiveClass;
96+
97+
/**
98+
* If this is provided, we calculate F-beta scores at the specified beta values.
99+
* The F-beta score is a generalization of F-1 score. This is only used for binary classification.
100+
*
101+
* With a beta of 1 (i.e. the F-1 score), precision and recall are given the same weight.
102+
* A larger beta score puts more weight on recall and less on precision.
103+
* A smaller beta score puts more weight on precision and less on recall.
104+
*/
105+
List<Double> classificationBetas;
106+
107+
/**
108+
* A string of up to 40 characters that will be added to your fine-tuned model name.
109+
*/
110+
String suffix;
111+
}
Collapse file
+80Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.theokanning.openai.finetune;
2+
3+
import com.theokanning.openai.file.File;
4+
import lombok.Data;
5+
6+
import java.util.List;
7+
8+
/**
9+
* An object describing an fine-tuned model. Returned by multiple fine-tune requests.
10+
*
11+
* https://beta.openai.com/docs/api-reference/fine-tunes
12+
*/
13+
@Data
14+
public class FineTuneResult {
15+
/**
16+
* The ID of the fine-tuning job.
17+
*/
18+
String id;
19+
20+
/**
21+
* The type of object returned, should be "fine-tune".
22+
*/
23+
String object;
24+
25+
/**
26+
* The name of the base model.
27+
*/
28+
String model;
29+
30+
/**
31+
* The creation time in epoch seconds.
32+
*/
33+
Long createdAt;
34+
35+
/**
36+
* List of events in this job's lifecycle. Null when getting a list of fine-tune jobs.
37+
*/
38+
List<FineTuneEvent> events;
39+
40+
/**
41+
* The ID of the fine-tuned model, null if tuning job is not finished.
42+
* This is the id used to call the model.
43+
*/
44+
String fineTunedModel;
45+
46+
/**
47+
* The specified hyper-parameters for the tuning job.
48+
*/
49+
HyperParameters hyperparams;
50+
51+
/**
52+
* The ID of the organization this model belongs to.
53+
*/
54+
String organizationId;
55+
56+
/**
57+
* Result files for this fine-tune job.
58+
*/
59+
List<File> resultFiles;
60+
61+
/**
62+
* The status os the fine-tune job. "pending", "succeeded", or "cancelled"
63+
*/
64+
String status;
65+
66+
/**
67+
* Training files for this fine-tune job.
68+
*/
69+
List<File> trainingFiles;
70+
71+
/**
72+
* The last update time in epoch seconds.
73+
*/
74+
Long updatedAt;
75+
76+
/**
77+
* Validation files for this fine-tune job.
78+
*/
79+
List<File> validationFiles;
80+
}
Collapse file
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.theokanning.openai.finetune;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* Fine-tuning job hyperparameters
7+
*
8+
* https://beta.openai.com/docs/api-reference/fine-tunes
9+
*/
10+
@Data
11+
public class HyperParameters {
12+
13+
/**
14+
* The batch size to use for training.
15+
*/
16+
String batchSize;
17+
18+
/**
19+
* The learning rate multiplier to use for training.
20+
*/
21+
Double learningRateMultiplier;
22+
23+
/**
24+
* The number of epochs to train the model for.
25+
*/
26+
Integer nEpochs;
27+
28+
/**
29+
* The weight to use for loss on the prompt tokens.
30+
*/
31+
Double promptLossWeight;
32+
}
Collapse file

‎client/build.gradle‎

Copy file name to clipboardExpand all lines: client/build.gradle
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ dependencies {
77
api 'com.squareup.retrofit2:retrofit:2.9.0'
88
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
99
implementation 'com.squareup.retrofit2:converter-jackson:2.9.0'
10+
11+
testImplementation(platform('org.junit:junit-bom:5.8.2'))
12+
testImplementation('org.junit.jupiter:junit-jupiter')
13+
}
14+
15+
test {
16+
useJUnitPlatform()
17+
testLogging {
18+
events "passed", "skipped", "failed"
19+
}
1020
}
1121

1222
ext {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.