The Problem:
Upload files to OpenAI API. The API documentation specifies a file
parameter, but it is unclear how to include the actual file content when making the request.
The Solutions:
Solution 1: Using File Object in Node.js
For Node.js users, Open AI accepts a File object created using `new File()`. To upload a file using this method:
// Create a File object with the file content, name, and type
const file = new File([blob], 'example.txt', { type: 'text/plain' });
// Send the file to Open AI using the `files.create` method
const upload = await openai.files.create({
file: file,
purpose: "assistants",
});
Solution 2: Uploading Files with OpenAI API
To upload files using the OpenAI API, follow these steps:
-
Set your OpenAI API key using
openai.api_key = os.getenv("OPENAI_API_KEY")
. -
Create a file object using
openai.File.create()
. Pass the file path and purpose of the file (e.g.,'fine-tune'
). Thefile
parameter expects a file object, not a filename. Therefore, open the file in binary mode usingopen(filepath, "rb")
. -
Store the response from
openai.File.create()
in a variable, as it contains thefile_id
and other information. -
Use the
file_id
to create a fine-tuning job usingopenai.FineTuningJob.create()
. Pass thefile_id
(results.id
in the example code) and the desired model (config.BASE_MODEL
in the example) as arguments. -
Check the status of the fine-tuning job by retrieving it using
openai.FineTuningJob.retrieve(results.id)
. -
To obtain the generated model ID, monitor the status of the fine-tuning job until it completes using
python train.py --state {results.id}
.
Solution 3: File upload methods
To upload files using the OpenAI API, you have two options:
Using curl:
- Append the file information using the -F command followed by the filename/path.
Using Postman:
- In the Body tab, select "form-data" and change the "key" field from "Text" to "File". Click "Select File" to choose the file to upload.
- Alternatively, click "binary" and select the file.
Q&A
How to upload a testing file with Open AI?
Create a new File object using new File()
and assign to the file parameter in openai.files.create().
How to upload a training file with Open AI?
Use curl’s -F
command followed by the filename/path or use Postman’s form-data or binary options.
What file format should a training file be?
JSONL, where each line of the file is a JSON object.
Video Explanation:
The following video, titled "OpenAI Assistants API tutorial: Assistants, file uploads, retrieval ...", provides additional insights and in-depth exploration related to the topics discussed in this post.
Learn how to programatically upload files and assign them to assistants with the OpenAI Assistants API. This tutorial will teach you how, ...
The following video, titled "OpenAI Assistants API tutorial: Assistants, file uploads, retrieval ...", provides additional insights and in-depth exploration related to the topics discussed in this post.
Learn how to programatically upload files and assign them to assistants with the OpenAI Assistants API. This tutorial will teach you how, ...