😸

OpenAI APIをPHPから使う

2023/03/08に公開

概要

  • OpenAIのAPIをPHPから使うサンプルコードを紹介します
  • 事前にOpenAIのAPI KEYの取得が必要になります

GPT-3 (text-davinci-003)を使う

<?php
// OpenAI API endpoint URL
$url = 'https://api.openai.com/v1/completions';

// Your OpenAI API key
$api_key = 'sk-APIKEY';

// Request parameters
$data = array(
    'prompt' => '現在の日本の総理大臣は?',
    'model' => 'text-davinci-003',
    'max_tokens' => 32,
);

// Set headers
$headers = array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $api_key
);

// Create cURL request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Send request
$response = curl_exec($ch);

// Check for errors
if ($response === false) {
    die(curl_error($ch));
}

// Decode response
$result = json_decode($response, true);

// Print response
echo('<pre>');
var_dump($result);
echo('</pre>');
?>

こんな感じのレスポンスが返ってきます。
答え間違ってますが

array(6) {
  ["id"]=>
  string(34) "cmpl-"
  ["object"]=>
  string(15) "text_completion"
  ["created"]=>
  int(1678201737)
  ["model"]=>
  string(16) "text-davinci-003"
  ["choices"]=>
  array(1) {
    [0]=>
    array(4) {
      ["text"]=>
      string(48) "

安倍晋三(あべ しんぞう)です。"
      ["index"]=>
      int(0)
      ["logprobs"]=>
      NULL
      ["finish_reason"]=>
      string(4) "stop"
    }
  }
  ["usage"]=>
  array(3) {
    ["prompt_tokens"]=>
    int(23)
    ["completion_tokens"]=>
    int(29)
    ["total_tokens"]=>
    int(52)
  }
}

画像生成

<?php
// OpenAI API endpoint URL
$url = 'https://api.openai.com/v1/images/generations';

// Your OpenAI API key
$api_key = 'sk-APIKEY';

// Request parameters
$data = array(
    'prompt' => 'a white siamese cat',
    'num_images' => 1,
    'size' => '256x256'
);

// Set headers
$headers = array(
    'Content-Type: application/json',
    'Authorization: Bearer ' . $api_key
);

// Create cURL request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// Send request
$response = curl_exec($ch);

// Check for errors
if ($response === false) {
    die(curl_error($ch));
}

// Decode response
$result = json_decode($response, true);

// Print response
echo('<pre>');
var_dump($result);
echo('</pre>');
?>

こんな感じのレスポンスが返ってきます
URLの部分をブラウザで開くと画像が取得できます

array(2) {
  ["created"]=>
  int(1678202021)
  ["data"]=>
  array(1) {
    [0]=>
    array(1) {
      ["url"]=>
      string(472) "https://oaidalleapiprodscus.blob.core.windows.net/private/org-YXI0SB53REuHkTYgK1RaW374/user-RiZHVJGtjKZPl1wMqYMh9rK7/img-1pvqh7xiUZpxS3YdbJiyf7tq.png"
    }
  }
}

参考

APIリファレンス
https://platform.openai.com/docs/api-reference/introduction
https://platform.openai.com/docs/api-reference/images

Discussion