OpenAI Assistant File Creation with Database Integration

I’m currently working on generating an OpenAI assistant file by integrating it with a database.

   const content = "database content for the assistant"
    const file = await openai.files.create({
        file: content,
        purpose: "assistants",
    });

However, I’ve encountered an issue when attempting to include a variable in the file field, resulting in an error.

  error: {
     message: 'The browser (or proxy) sent a request that this server could not understand.',
     type: 'server_error',
     params: null,
     code: null
   },

This is the approach I implemented. If you have an alternative or more effective solution, I would appreciate it if you could share it with me.

// Promisify the fs.writeFile function for ease of use
const writeFileAsync = util.promisify(fs.writeFile);

async function sendStringAsTextFile(content) {
    // Create a temporary file in-memory
    const tempFilePath = 'temp.txt';

    try {
        // Write the content to the temporary file
        await writeFileAsync(tempFilePath, content);

        // Send the file to the server
        const file = await openai.files.create({
            file: fs.createReadStream(tempFilePath),
            purpose: "assistants",
        });

        console.log('File successfully sent to the server:', file);

    } catch (error) {
        console.error('Error sending the file:', error);
    } finally {
        // Cleanup: Delete the temporary file
        fs.unlinkSync(tempFilePath);
    }
}

// Example usage
const content = "content for the assistant";
sendStringAsTextFile(content);
1 Like