This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
generated from spatie/package-skeleton-php
-
Notifications
You must be signed in to change notification settings - Fork 5
Files
Greg Priday edited this page Jul 7, 2021
·
2 revisions
You can store your training data on GPT3 for faster more efficient use of the Answers, Search and Classifications endpoints. We give you 3 ways to create files. Either an array of arrays, a file resource or a string.
See the OpenAI docs on Files.
use SiteOrigin\OpenAI\Client;
$client = new Client($_ENV['OPENAI_API_KEY']);
$data = [
[ 'text' => 'This is the future of AI' ],
[ 'text' => 'Buy Bitcoin' ],
];
$created = $client->files()->create('data.json', $data, 'search');
$fileId = $created->id;
This gives us a value for $created
of
(object) array(
'id' => 'file-9puMz1JxrKrlXRBGHHQJrnq2',
'object' => 'file',
'bytes' => 59,
'created_at' => 1625654046,
'filename' => 'data.json',
'purpose' => 'search',
'status' => 'uploaded',
'status_details' => NULL,
);
In addition to using data arrays, you can also use a string or file resource to upload a file.
$client = new Client($_ENV['OPENAI_API_KEY']);
$data = file_get_contents('data.json.txt');
$created = $client->files()->create('data.json', $data, 'search');
$created = $client->files()->create('data.json', fopen('data.json.txt', 'r'), 'search');
use SiteOrigin\OpenAI\Client;
$client = new Client($_ENV['OPENAI_API_KEY']);
$list = $client->files()->list();
This gives us information about all uploaded files.
(object) array(
'object' => 'list',
'data' =>
array (
0 =>
(object) array(
'id' => 'file-8puMz1KxrKrlXRBGHHOJrnq1',
'object' => 'file',
'bytes' => 59,
'created_at' => 1625654046,
'filename' => '_temp_5003307d211c.json',
'purpose' => 'search',
'status' => 'uploaded',
'status_details' => NULL,
),
),
);
You can retrieve information about a file either from its ID, or filename. Note that it's possible for 2 files to have the same filename. In this case, we'll fetch the first one created first.
use SiteOrigin\OpenAI\Client;
$client = new Client($_ENV['OPENAI_API_KEY']);
// By ID
$file = $client->files()->retrieve('file-8puMz1KxrKrlXRBGHHOJrnq1');
// Or by filename, which first lists the files, then chooses one. Try use the base retrieve() function where possible.
$file = $client->files()->retrieveByFilename('data.json');
The value of file would be:
(object) array(
'id' => 'file-8puMz1KxrKrlXRBGHHOJrnq1',
'object' => 'file',
'bytes' => 59,
'created_at' => 1625656909,
'filename' => '_temp_9653a371e569.json',
'purpose' => 'search',
'status' => 'uploaded',
'status_details' => NULL,
)
Finally, if you want to delete files, you can delete them by ID.
use SiteOrigin\OpenAI\Client;
$client = new Client($_ENV['OPENAI_API_KEY']);
$client->files()->delete('file-8puMz1KxrKrlXRBGHHOJrnq1');
// Or delete by filename
$client->files()->deleteByFilename('data.json');