Batch API
#383
Replies: 3 comments
-
What do you mean, help? Here you go - need a library for that? <?php
class OpenAI_Batch_Processor {
private $db;
public function __construct() {
$this->db = new PDO('sqlite:batch_data.sqlite');
$this->db->exec("CREATE TABLE IF NOT EXISTS batch_responses (id TEXT PRIMARY KEY, json_response TEXT)");
}
public function addJsonLine($jsonLine) {
file_put_contents('batchinput.jsonl', $jsonLine . PHP_EOL, FILE_APPEND);
}
public function sendFile() {
$command = "curl https://api.openai.com/v1/files -H 'Authorization: Bearer " . getenv('OPENAI_API_KEY') . "' -F purpose='batch' -F file=@batchinput.jsonl";
return shell_exec($command);
}
public function createBatch($inputFileId) {
$data = [
'input_file_id' => $inputFileId,
'endpoint' => '/v1/chat/completions',
'completion_window' => '24h'
];
$command = "curl https://api.openai.com/v1/batches -H 'Authorization: Bearer " . getenv('OPENAI_API_KEY') . "' -H 'Content-Type: application/json' -d '" . json_encode($data) . "'";
$response = shell_exec($command);
$json = json_decode($response, true);
$this->db->exec("INSERT INTO batch_responses (id, json_response) VALUES ('{$json['id']}', '" . json_encode($json) . "')");
return $response;
}
public function checkBatchStatus($batchId) {
$command = "curl https://api.openai.com/v1/batches/$batchId -H 'Authorization: Bearer " . getenv('OPENAI_API_KEY') . "' -H 'Content-Type: application/json'";
return shell_exec($command);
}
public function retrieveBatchOutput($fileId) {
$command = "curl https://api.openai.com/v1/files/$fileId/content -H 'Authorization: Bearer " . getenv('OPENAI_API_KEY') . "' > batch_output.jsonl";
shell_exec($command);
}
public function listBatches() {
$command = "curl https://api.openai.com/v1/batches?limit=10 -H 'Authorization: Bearer " . getenv('OPENAI_API_KEY') . "' -H 'Content-Type: application/json'";
return shell_exec($command);
}
public function cancelBatch($batchId) {
$command = "curl https://api.openai.com/v1/batches/$batchId/cancel -H 'Authorization: Bearer " . getenv('OPENAI_API_KEY') . "' -H 'Content-Type: application/json' -X POST";
return shell_exec($command);
}
}
?>
|
Beta Was this translation helpful? Give feedback.
0 replies
-
if you got any bad feelings about the shell_exec you can use curl ext - need help implementing that in what? <?php
class OpenAI_Batch_Processor {
private $db;
public function __construct() {
$this->db = new PDO('sqlite:batch_data.sqlite');
$this->db->exec("CREATE TABLE IF NOT EXISTS batch_responses (id TEXT PRIMARY KEY, json_response TEXT)");
}
public function addJsonLine($jsonLine) {
file_put_contents('batchinput.jsonl', $jsonLine . PHP_EOL, FILE_APPEND);
}
private function executeCurl($url, $headers, $postData = null, $isPost = false) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($isPost) {
curl_setopt($ch, CURLOPT_POST, true);
if ($postData) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
}
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function sendFile() {
$url = "https://api.openai.com/v1/files";
$headers = [
"Authorization: Bearer " . getenv('OPENAI_API_KEY'),
"Content-Type: multipart/form-data"
];
$postData = [
'purpose' => 'batch',
'file' => new CURLFile('batchinput.jsonl')
];
return $this->executeCurl($url, $headers, $postData, true);
}
public function createBatch($inputFileId) {
$url = "https://api.openai.com/v1/batches";
$headers = [
"Authorization: Bearer " . getenv('OPENAI_API_KEY'),
"Content-Type: application/json"
];
$data = [
'input_file_id' => $inputFileId,
'endpoint' => '/v1/chat/completions',
'completion_window' => '24h'
];
$response = $this->executeCurl($url, $headers, json_encode($data), true);
$json = json_decode($response, true);
$this->db->exec("INSERT INTO batch_responses (id, json_response) VALUES ('{$json['id']}', '" . json_encode($json) . "')");
return $response;
}
public function checkBatchStatus($batchId) {
$url = "https://api.openai.com/v1/batches/$batchId";
$headers = [
"Authorization: Bearer " . getenv('OPENAI_API_KEY'),
"Content-Type: application/json"
];
return $this->executeCurl($url, $headers);
}
public function retrieveBatchOutput($fileId) {
$url = "https://api.openai.com/v1/files/$fileId/content";
$headers = [
"Authorization: Bearer " . getenv('OPENAI_API_KEY')
];
$outputPath = 'batch_output.jsonl';
$response = $this->executeCurl($url, $headers);
file_put_contents($outputPath, $response);
}
public function listBatches() {
$url = "https://api.openai.com/v1/batches?limit=10";
$headers = [
"Authorization: Bearer " . getenv('OPENAI_API_KEY'),
"Content-Type: application/json"
];
return $this->executeCurl($url, $headers);
}
public function cancelBatch($batchId) {
$url = "https://api.openai.com/v1/batches/$batchId/cancel";
$headers = [
"Authorization: Bearer " . getenv('OPENAI_API_KEY'),
"Content-Type: application/json"
];
return $this->executeCurl($url, $headers, null, true);
}
}
?> |
Beta Was this translation helpful? Give feedback.
0 replies
-
The package now supports the batches API: https://github.com/openai-php/client/releases/tag/v0.9.1 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there!
I am wondering if you can help us integrate the new Batch API. With the Batch API, you can upload a bulk request file and receive results within 24 hours. Plus, you'll get a 50% discount on API prices. Check out https://platform.openai.com/docs/guides/batch for more information.
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions