-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.js
More file actions
92 lines (62 loc) · 1.9 KB
/
example.js
File metadata and controls
92 lines (62 loc) · 1.9 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
require('dotenv').config();
const OpenAI = require('openai');
const fs = require('fs');
const readline = require('readline');
const openai = new OpenAI({ apiKey: process.env.API_KEY });
async function validateJSONL(file) {
const fileStream = fs.createReadStream(file);
const rl = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
let lineNumber = 0;
for await (const line of rl) {
lineNumber++;
try {
JSON.parse(line);
} catch (e) {
console.error(
`Invalid JSON object at line ${lineNumber}: ${e.message}`
);
return false;
}
}
console.log('File is a valid JSONL file');
return true;
}
async function uploadDataset() {
if (await validateJSONL('career_chat.jsonl')) {
const status = await openai.files.create({
file: fs.createReadStream('careerchat.jsonl'),
purpose: 'fine-tune',
});
console.log(status);
return status.id;
}
return null;
}
async function fineTuneModel(trainingFileId) {
const fineTune = await openai.fineTuning.jobs.create({
training_file: trainingFileId,
model: 'gpt-3.5-turbo',
});
console.log(fineTune);
// Wait for the email confirmation that the fine-tuning is complete before proceeding.
}
async function getModelCompletion(modelId) {
const completion = await openai.chat.completions.create({
messages: [
{
role: 'user',
content:
'should i include previous work experience on my resume not related to tech?',
},
],
model: modelId,
});
console.log(completion.choices[0].message.content);
}
// Uncomment the function you want to run based on the step you're at:
// uploadDataset(); // Step 1: Upload the dataset
// fineTuneModel('YOUR_FILE_ID'); // Step 2: Fine-tune the model. Replace 'YOUR_FILE_ID' with the file ID from the previous step.
// getModelCompletion('YOUR_FINE_TUNED_MODEL_ID'); // Step 3: Get a completion. Replace 'YOUR_FINE_TUNED_MODEL_ID' with the model ID from the email.