简体   繁体   中英

Getting error 400 OpenAI files endpoint Node.js

I have been trying for hours, tried CSV data, JSON data, tried it all I keep getting either a 415 error or a 400 error; if I can have any help that would be great, the documentation and the AI itself has not been at all helpful in trying to inject data to train a model

Text:

This is a text file for the OpenAI API.
It contains sample data for testing purposes.
Here is another line.
And another.

Code:

const FormData = require('form-data');
const fs = require('fs');
const API_KEY = 'MY_API_KEY';
const formData = new FormData();
formData.append('file', fs.createReadStream('./myfile.txt'), {
  filename: 'myfile.txt'
});

axios.post('https://api.openai.com/v1/files', formData, {
  headers: {
    'Content-Type': `multipart/form-data; boundary=${formData._boundary}`,
    "Authorization": "Bearer " + API_KEY
  }
})
.then(response => {
  // Handle response
})
.catch(err => {
  // Handle error
  console.log(err)
});

From the documentation , you are missing the required purpose field...

Request body

file Required
...
purpose Required

The intended purpose of the uploaded documents.

Use "fine-tune" for Fine-tuning. This allows us to validate the format of the uploaded file.

const FormData = require("form-data");
const fs = require("fs");

const API_KEY = "MY_API_KEY";

const formData = new FormData();
formData.append("purpose", "search"); // 👈 adjust accordingly
formData.append("file", fs.createReadStream("./myfile.txt"), "myfile.txt");

axios
  .post("https://api.openai.com/v1/files", formData, {
    headers: {
      ...formData.getHeaders(), // this is simpler
      authorization: `Bearer ${API_KEY}`,
    },
  })
  .then((response) => {
    // Handle response
  })
  .catch((err) => {
    // Handle error
    console.log(err.response?.data, err.toJSON());
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM