-
Notifications
You must be signed in to change notification settings - Fork 6
/
tune.ts
70 lines (60 loc) · 1.71 KB
/
tune.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
import { blob } from 'node:stream/consumers';
import { createReadStream } from 'node:fs';
import { Client } from '../src/index.js';
const client = new Client({
apiKey: process.env.GENAI_API_KEY,
});
{
// List all completed tunes
let totalCount = Infinity;
const limit = 100;
for (let offset = 0; offset < totalCount; offset += limit) {
const { results, total_count } = await client.tune.list({
limit,
offset,
status: 'completed',
});
for (const file of results) {
console.log(file);
}
totalCount = total_count;
}
}
{
// List available tune methods
const { results: tuneTypes } = await client.tune.types({});
console.log(tuneTypes);
// Upload file for tuning
const { result: file } = await client.file.create({
purpose: 'tune',
file: {
name: 'tune_input.jsonl',
content: (await blob(
createReadStream('examples/assets/tune_input.jsonl'),
)) as any,
},
});
// Create a tune
const { result: createdTune } = await client.tune.create({
name: 'Awesome Tune',
tuning_type: 'prompt_tuning',
model_id: 'google/flan-t5-xl',
task_id: 'generation',
training_file_ids: [file.id],
});
console.log(createdTune);
// Show details of the tune
const { result: retrievedTune } = await client.tune.retrieve({
id: createdTune.id,
});
console.log(retrievedTune);
// Download tune's assets when completed
if (retrievedTune.status === 'completed') {
const logs = await client.tune.read({ id: retrievedTune.id, type: 'logs' });
console.log(await logs.text());
}
// Delete the tune
await client.tune.delete({ id: createdTune.id });
// Detele the file
await client.file.delete({ id: file.id });
}