Have you ever needed to send multiple files (like images or PDF) to OpenAI’s API and get a meaningful response back? For example ask open ai to give you the description of the files? This tutorial shows you how to easily do that in Node.js , using OpenAI’s SDK. This example demonstrates how you can ask Open AI to give you the description of multiple images. If you are working with PDF, change [type: “input_image”] to [type: “input_file”].
#!/usr/bin/env -S npm run tsn -T
import OpenAI from 'openai';
import fs from 'fs';
const openai = new OpenAI();
// List of local files to upload
const files: string[] = ['bath-bomb.png', 'incense-kit.png'];
/**
* Uploads multiple files to OpenAI and sends them in a prompt.
* Prints the response text from GPT-4o.
*/
async function uploadAndQueryFiles(): Promise<void> {
try {
// Upload each file
const uploadedFiles = await Promise.all(
files.map(async (file: string) => {
return await openai.files.create({
file: fs.createReadStream(file),
purpose: 'user_data',
});
}),
);
// Format the files for the OpenAI response API
const fileObjects = uploadedFiles.map((file) => ({
type: 'input_image' as const,
file_id: file.id,
}));
// Send a prompt with the uploaded files
const response = await openai.responses.create({
model: 'gpt-4o',
input: [
{
role: 'user',
content: [
{
type: 'input_text',
text: 'What are in this file?',
},
...fileObjects, // Spread the array to include each file object individually
],
},
],
});
console.log(response.output_text);
} catch (error) {
console.error('An error occurred:', error);
}
}
uploadAndQueryFiles();