Write a script to upload a training file and start a fine-tuning job using the OpenAI API.
Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
Implementation:
import OpenAI from 'openai';
import fs from 'fs';
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
async function startFineTuning(filePath) {
try {
console.log("Uploading training file...");
const file = await openai.files.create({
file: fs.createReadStream(filePath),
purpose: "fine-tune"
});
console.log(`File uploaded: ${file.id}`);
// Wait for file processing (in production, you'd poll or wait longer)
// For this example, we assume it's ready quickly or the API handles the queue
console.log("Starting fine-tuning job...");
const job = await openai.fineTuning.jobs.create({
training_file: file.id,
model: "gpt-3.5-turbo"
});
console.log(`Job started: ${job.id}`);
console.log(`Status: ${job.status}`);
return job;
} catch (error) {
console.error("Error starting fine-tuning:", error);
}
}
// Usage
// startFineTuning("training_data.jsonl");