-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathfine-tuning.ts
executable file
·71 lines (54 loc) · 1.97 KB
/
fine-tuning.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env -S npm run tsn -T
/**
* Fine-tuning allows you to train models on your own data.
*
* See this guide for more information:
* - https://platform.openai.com/docs/guides/fine-tuning
*/
import fs from 'fs';
import OpenAI from 'openai';
import { FineTuningJobEvent } from 'openai/resources/fine-tuning';
// Gets the API Key from the environment variable `OPENAI_API_KEY`
const client = new OpenAI();
async function main() {
console.log(`Uploading file`);
let file = await client.files.create({
file: fs.createReadStream('./examples/fine-tuning-data.jsonl'),
purpose: 'fine-tune',
});
console.log(`Uploaded file with ID: ${file.id}`);
console.log('-----');
console.log(`Waiting for file to be processed`);
while (true) {
file = await client.files.retrieve(file.id);
console.log(`File status: ${file.status}`);
if (file.status === 'processed') {
break;
} else {
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
console.log('-----');
console.log(`Starting fine-tuning`);
let fineTune = await client.fineTuning.jobs.create({ model: 'gpt-3.5-turbo', training_file: file.id });
console.log(`Fine-tuning ID: ${fineTune.id}`);
console.log('-----');
console.log(`Track fine-tuning progress:`);
const events: Record<string, FineTuningJobEvent> = {};
while (fineTune.status == 'running' || fineTune.status == 'queued') {
fineTune = await client.fineTuning.jobs.retrieve(fineTune.id);
console.log(`${fineTune.status}`);
const { data } = await client.fineTuning.jobs.listEvents(fineTune.id, { limit: 100 });
for (const event of data.reverse()) {
if (event.id in events) continue;
events[event.id] = event;
const timestamp = new Date(event.created_at * 1000);
console.log(`- ${timestamp.toLocaleTimeString()}: ${event.message}`);
}
await new Promise((resolve) => setTimeout(resolve, 5000));
}
}
main().catch((err) => {
console.error(err);
process.exit(1);
});