Code interpreter in gpt-4o-mini

GPT-4o-mini works

Code - Node.js
import 'dotenv/config';
import fs from 'fs';
import OpenAI from 'openai';

const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });

async function main() {
  const assistant = await openai.beta.assistants.create({
    name: 'Chart Generator',
    instructions: `
      Write Python that builds the requested chart with matplotlib.`,
    model: 'gpt-4o-mini',
    tools: [{ type: 'code_interpreter' }],
  });

  const thread = await openai.beta.threads.create();
  await openai.beta.threads.messages.create(thread.id, {
    role: 'user',
    content: 'Create a bar chart comparing apples (10), bananas (20) and oranges (15).'
  });

  const run = await openai.beta.threads.runs.create(thread.id, {
    assistant_id: assistant.id,
  });

  // poll until finished
  let status;
  do {
    await new Promise(r => setTimeout(r, 2000));
    status = await openai.beta.threads.runs.retrieve(thread.id, run.id);
    console.log('⏳', status.status);
  } while (status.status !== 'completed');

  // fetch messages & download the file
  const msgs = await openai.beta.threads.messages.list(thread.id, { limit: 50 });
  for (const m of msgs.data) {
    for (const part of m.content) {
      if (part.type === 'image_file') {
        const resp = await openai.files.content(part.image_file.file_id);
        const ws   = fs.createWriteStream('bar_chart.png');
        resp.body.pipe(ws).on('finish', () => console.log('✅ bar_chart.png saved'));
      }
    }
  }
}

main().catch(console.error);

2 Likes