Skip to content

Commit df87faa

Browse files
authored
feat: fine-tuning implementation (#242)
1 parent d34d6fd commit df87faa

File tree

24 files changed

+802
-129
lines changed

24 files changed

+802
-129
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@ Use your `OpenAI` instance to make API requests. [Learn more](guides/GettingStar
7878
- [Images](guides/GettingStarted.md#images)
7979
- [Embeddings](guides/GettingStarted.md#embeddings)
8080
- [Files](guides/GettingStarted.md#files)
81-
- [Fine-tunes](guides/GettingStarted.md#fine-tunes)
81+
- [Fine-tuning](guides/GettingStarted.md#fine-tuning)
8282
- [Moderations](guides/GettingStarted.md#moderations)
8383
- [Audio](guides/GettingStarted.md#audio)
8484

8585
#### Legacy
8686
- [Completions](guides/GettingStarted.md#completions)
8787

8888
#### Deprecated
89+
- [Fine-tunes](guides/GettingStarted.md#fine-tunes)
8990
- [Edits](guides/GettingStarted.md#edits)
9091

9192
## 📚 Guides

guides/GettingStarted.md

Lines changed: 129 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ Use your `OpenAI` instance to make API requests.
2121
- [Create image variation](#create-image-variation)
2222
- [Embeddings](#embeddings)
2323
- [Create embeddings](#create-embeddings)
24+
- [Fine-tuning](#fine-tuning)
25+
- [Create fine-tuning job](#create-fine-tuning-job)
26+
- [List fine-tuning jobs](#list-fine-tuning-jobs)
27+
- [Retrieve fine-tuning job](#retrieve-fine-tuning-job)
28+
- [Cancel fine-tuning](#cancel-fine-tuning)
29+
- [List fine-tuning events](#list-fine-tuning-events)
2430
- [Audio](#audio)
2531
- [Create transcription](#create-transcription)
2632
- [Create translation](#create-translation)
@@ -30,13 +36,6 @@ Use your `OpenAI` instance to make API requests.
3036
- [Delete file](#delete-file)
3137
- [Retrieve file](#retrieve-file)
3238
- [Retrieve file content](#retrieve-file-content)
33-
- [Fine-tunes](#fine-tunes)
34-
- [Create fine-tune](#create-fine-tune)
35-
- [List fine-tunes](#list-fine-tunes)
36-
- [Retrieve fine-tune](#retrieve-fine-tune)
37-
- [Cancel fine-tune](#cancel-fine-tune)
38-
- [List fine-tune events](#list-fine-tune-events)
39-
- [Delete fine-tune model](#delete-fine-tune-model)
4039
- [Moderations](#moderations)
4140
- [Create moderation](#create-moderation)
4241

@@ -45,6 +44,13 @@ Use your `OpenAI` instance to make API requests.
4544
- [Create completion](#create-completion-legacy)
4645

4746
#### Deprecated
47+
- [Fine-tunes](#fine-tunes)
48+
- [Create fine-tune](#create-fine-tune)
49+
- [List fine-tunes](#list-fine-tunes)
50+
- [Retrieve fine-tune](#retrieve-fine-tune)
51+
- [Cancel fine-tune](#cancel-fine-tune)
52+
- [List fine-tune events](#list-fine-tune-events)
53+
- [Delete fine-tune model](#delete-fine-tune-model)
4854
- [Edits](#edits)
4955
- [Create edits](#create-edits-deprecated)
5056

@@ -162,6 +168,83 @@ val embeddings = openAI.embeddings(
162168
)
163169
````
164170

171+
## Fine-tuning
172+
173+
Manage fine-tuning jobs to tailor a model to your specific training data.
174+
175+
### Create fine-tuning job
176+
177+
Creates a job that fine-tunes a specified model from a given dataset.
178+
179+
Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
180+
181+
#### No Hyperparameters
182+
183+
```kotlin
184+
val request = FineTuningRequest(
185+
trainingFile = FileId("file-abc123"),
186+
model = ModelId("gpt-3.5-turbo"),
187+
)
188+
val fineTuningJob = client.fineTuningJob(request)
189+
```
190+
191+
#### Hyperparameters
192+
193+
```kotlin
194+
val request = FineTuningRequest(
195+
trainingFile = FileId("file-abc123"),
196+
model = ModelId("gpt-3.5-turbo"),
197+
hyperparameters = Hyperparameters(nEpochs = 2),
198+
)
199+
val fineTuningJob = client.fineTuningJob(request)
200+
```
201+
202+
#### Validation File
203+
204+
```kotlin
205+
val request = FineTuningRequest(
206+
trainingFile = FileId("file-abc123"),
207+
validation_file = FileId("file-def345"),
208+
model = ModelId("gpt-3.5-turbo"),
209+
)
210+
val fineTuningJob = client.fineTuningJob(request)
211+
```
212+
213+
### List fine-tuning jobs
214+
215+
List your organization's fine-tuning jobs
216+
217+
```kotlin
218+
val fineTuningJobs = client.fineTuningJobs(limit = 2)
219+
```
220+
221+
### Retrieve fine-tuning job
222+
223+
Get info about a fine-tuning job.
224+
225+
```kotlin
226+
val id = FineTuningId("ft-AF1WoRqd3aJAHsqc9NY7iL8F")
227+
val fineTuningJob = client.fineTuningJob(id)
228+
```
229+
230+
### Cancel fine-tuning
231+
232+
Immediately cancel a fine-tune job.
233+
234+
```kotlin
235+
val id = FineTuningId("ftjob-abc12")
236+
client.cancel(id)
237+
```
238+
239+
### List fine-tuning events
240+
241+
Get status updates for a fine-tuning job.
242+
243+
```kotlin
244+
val id = FineTuningId("ftjob-abc12")
245+
val fineTuningEvents = client.fineTuningEvents(id)
246+
```
247+
165248
## Audio
166249

167250
Learn how to turn audio into text.
@@ -240,6 +323,45 @@ Returns the contents of the specified file
240323
val bytes = openAI.download(fileId)
241324
````
242325

326+
## Moderations
327+
328+
Given an input text, outputs if the model classifies it as violating OpenAI's content policy.
329+
330+
### Create moderation
331+
332+
Classifies if text violates OpenAI's Content Policy
333+
334+
````kotlin
335+
val moderation = openAI.moderations(
336+
request = ModerationRequest(
337+
input = "I want to kill them."
338+
)
339+
)
340+
````
341+
342+
---
343+
344+
## Completions
345+
346+
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
347+
348+
### Create Completion `legacy`
349+
350+
Creates a completion for the provided prompt and parameters
351+
352+
```kotlin
353+
val completionRequest = CompletionRequest(
354+
model = ModelId("text-ada-001"),
355+
prompt = "Somebody once told me the world is gonna roll me",
356+
echo = true
357+
)
358+
val completion: TextCompletion = openAI.completion(completionRequest)
359+
// or, as flow
360+
val completions: Flow<TextCompletion> = openAI.completions(completionRequest)
361+
```
362+
363+
---
364+
243365
## Fine-tunes
244366

245367
Manage fine-tuning jobs to tailor a model to your specific training data.
@@ -301,45 +423,6 @@ Delete a fine-tuned model. You must have the Owner role in your organization.
301423
openAI.delete(fileId)
302424
```
303425

304-
## Moderations
305-
306-
Given an input text, outputs if the model classifies it as violating OpenAI's content policy.
307-
308-
### Create moderation
309-
310-
Classifies if text violates OpenAI's Content Policy
311-
312-
````kotlin
313-
val moderation = openAI.moderations(
314-
request = ModerationRequest(
315-
input = "I want to kill them."
316-
)
317-
)
318-
````
319-
320-
---
321-
322-
## Completions
323-
324-
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
325-
326-
### Create Completion `legacy`
327-
328-
Creates a completion for the provided prompt and parameters
329-
330-
```kotlin
331-
val completionRequest = CompletionRequest(
332-
model = ModelId("text-ada-001"),
333-
prompt = "Somebody once told me the world is gonna roll me",
334-
echo = true
335-
)
336-
val completion: TextCompletion = openAI.completion(completionRequest)
337-
// or, as flow
338-
val completions: Flow<TextCompletion> = openAI.completions(completionRequest)
339-
```
340-
341-
---
342-
343426
## Edits
344427

345428
Given a prompt and an instruction, the model will return an edited version of the prompt.

openai-client/src/commonMain/kotlin/com.aallam.openai.client/FineTunes.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,42 @@ public interface FineTunes {
1818
* Response includes details of the enqueued job including job status and the name of the fine-tuned models once
1919
* complete.
2020
*/
21+
@Deprecated("Use FineTuning instead.")
2122
public suspend fun fineTune(request: FineTuneRequest): FineTune
2223

2324
/**
2425
* List your organization's fine-tuning jobs.
2526
*/
27+
@Deprecated("Use FineTuning instead.")
2628
public suspend fun fineTunes(): List<FineTune>
2729

2830
/**
2931
* Gets info about to fine-tune job.
3032
*/
33+
@Deprecated("Use FineTuning instead.")
3134
public suspend fun fineTune(fineTuneId: FineTuneId): FineTune?
3235

3336
/**
3437
* Immediately cancel a fine-tune job.
3538
*/
39+
@Deprecated("Use FineTuning instead.")
3640
public suspend fun cancel(fineTuneId: FineTuneId): FineTune?
3741

3842
/**
3943
* Get fine-grained status updates for fine-tune job.
4044
*/
45+
@Deprecated("Use FineTuning instead.")
4146
public suspend fun fineTuneEvents(fineTuneId: FineTuneId): List<FineTuneEvent>
4247

4348
/**
4449
* Get fine-grained status updates for fine-tune job.
4550
*/
51+
@Deprecated("Use FineTuning instead.")
4652
public fun fineTuneEventsFlow(fineTuneId: FineTuneId): Flow<FineTuneEvent>
4753

4854
/**
4955
* Delete a fine-tuned model. You must have the Owner role in your organization.
5056
*/
57+
@Deprecated("Use FineTuning instead.")
5158
public suspend fun delete(fineTuneModel: ModelId): Boolean
5259
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.aallam.openai.client
2+
3+
import com.aallam.openai.api.core.PaginatedList
4+
import com.aallam.openai.api.finetuning.*
5+
6+
/**
7+
* Manage fine-tuning jobs to tailor a model to your specific training data.
8+
*/
9+
public interface FineTuning {
10+
11+
/**
12+
* Creates a job that fine-tunes a specified model from a given dataset.
13+
*
14+
* Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
15+
*/
16+
public suspend fun fineTuningJob(request: FineTuningRequest): FineTuningJob
17+
18+
/**
19+
* List your organization's fine-tuning jobs.
20+
*
21+
* @param after Identifier for the last job from the previous pagination request.
22+
* @param limit Number of fine-tuning jobs to retrieve.
23+
*/
24+
public suspend fun fineTuningJobs(after: String? = null, limit: Int? = null): List<FineTuningJob>
25+
26+
/**
27+
* Get info about a fine-tuning job.
28+
*
29+
* @param id The ID of the fine-tuning job.
30+
*/
31+
public suspend fun fineTuningJob(id: FineTuningId): FineTuningJob?
32+
33+
/**
34+
* Immediately cancel a fine-tune job.
35+
*
36+
* @param id The ID of the fine-tuning job to cancel.
37+
*/
38+
public suspend fun cancel(id: FineTuningId): FineTuningJob?
39+
40+
/**
41+
* Get status updates for a fine-tuning job.
42+
*
43+
* @param id The ID of the fine-tuning job to get events for.
44+
* @param after Identifier for the last event from the previous pagination request.
45+
* @param limit Number of events to retrieve.
46+
*/
47+
public suspend fun fineTuningEvents(
48+
id: FineTuningId,
49+
after: String? = null,
50+
limit: Int? = null
51+
): PaginatedList<FineTuningJobEvent>
52+
}

openai-client/src/commonMain/kotlin/com.aallam.openai.client/OpenAI.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import kotlin.time.Duration.Companion.seconds
1111
* OpenAI API.
1212
*/
1313
public interface OpenAI : Completions, Files, Edits, Embeddings, Models, Moderations, FineTunes, Images, Chat, Audio,
14+
FineTuning,
1415
Closeable
1516

1617
/**

openai-client/src/commonMain/kotlin/com.aallam.openai.client/internal/OpenAIApi.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ internal class OpenAIApi(
2222
Images by ImagesApi(requester),
2323
Chat by ChatApi(requester),
2424
Audio by AudioApi(requester),
25+
FineTuning by FineTuningApi(requester),
2526
Closeable by requester

openai-client/src/commonMain/kotlin/com.aallam.openai.client/internal/api/ApiPath.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ internal object ApiPath {
1717
const val ImagesVariants = "images/variations"
1818
const val Models = "models"
1919
const val Moderations = "moderations"
20+
const val FineTuningJobs = "fine_tuning/jobs"
2021
}

0 commit comments

Comments
 (0)