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);