#Pass url as readStream to openai files.

1 messages · Page 1 of 1 (latest)

lavish turtle
#
        const file = await openai.files.create({
          file: createReadStream(url),
          //   file: got.stream(url).pipe(createWriteStream("file.pdf")),
          purpose: "assistants",
        });
        console.log(file.id);

Unable to pass url as readStream.

deep oreBOT
#

Hi @lavish turtle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

Based on the information provided in the extracts, it seems that you're trying to pass a URL as a ReadStream to the OpenAI API. However, the OpenAI API expects an actual file to be uploaded, not a URL.

In the issue #387 (https://github.com/openai/openai-node/issues/387) on the OpenAI Node API Library, a collaborator clarified that the file you pass to a transcription isn't the ID of a file object in the OpenAI API, but an actual file you upload. Here's the suggested code:

await openaiClient.audio.transcriptions.create({file: fs.createReadStream('[path]')})

In this case, you would replace '[path]' with the path to your local file.

If you want to use a file from a URL, you would first need to download the file from the URL to your local system or server, and then upload the file to the OpenAI API. The OpenAI API does not directly support downloading files from URLs.

Please note that the toFile helper function currently buffers the full file into memory before sending it, as discussed in issue #414 (https://github.com/openai/openai-node/issues/414). This means that large files could potentially use a lot of memory. OpenAI plans to add support for a web ReadableStream to toFile() in the near future, which would allow for more efficient handling of large files.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:

#

@lavish turtle kapa.ai is still learning and improving, please let us know how it did by reacting below

lavish turtle
#

But what if I do not have the path, I only have the url of the file? How can I pass that in Node js/React? @deep ore

deep oreBOT
#

Let me check the knowledge base for you. One moment! :books:

#

If you only have the URL of the file and you're working in a Node.js environment, you'll need to download the file first before you can pass it to the OpenAI API. This is because the OpenAI API does not directly support downloading files from URLs.

You can use libraries like axios and fs to download the file. Here's a basic example:

const axios = require('axios');
const fs = require('fs');

async function downloadFile(fileUrl, outputLocationPath) {
  const writer = fs.createWriteStream(outputLocationPath);

  return axios({
    method: 'get',
    url: fileUrl,
    responseType: 'stream',
  }).then(response => {
    return new Promise((resolve, reject) => {
      response.data.pipe(writer);
      let error = null;
      writer.on('error', err => {
        error = err;
        writer.close();
        reject(err);
      });
      writer.on('close', () => {
        if (!error) {
          resolve(true);
        }
      });
    });
  });
}

// Usage:
// downloadFile('<http://example.com/file.pdf',> 'output.pdf');

After you've downloaded the file, you can then upload it to the OpenAI API:

const openai = require('openai');

openai.setApiKey('your-api-key');

async function uploadFile(filePath) {
  const file = fs.createReadStream(filePath);
  const response = await openai.files.create({
    file: file,
    purpose: 'assistants',
  });

  console.log(response.id);
}

// Usage:
// uploadFile('output.pdf');

Please replace 'your-api-key' with your actual OpenAI API key, and '<http://example.com/file.pdf'> and 'output.pdf' with your actual file URL and desired output file path, respectively.

Remember that this approach will download the file to your local system or server, so make sure you have enough storage space and that you handle the file securely if it contains sensitive data.

#

Please note that the OpenAI API does not directly support React.js for file uploads. If you're working in a React.js environment, you would typically send the file to a server-side script (like the Node.js script above), which would then handle the file upload to the OpenAI API.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: