ruscoe/openai-php

OpenAI API的PHP库

1.2.0 2023-05-11 03:09 UTC

This package is auto-updated.

Last update: 2024-09-09 04:18:37 UTC


README

OpenAI API的非官方库。

release

要求

快速设置

git clone git@github.com:ruscoe/openai-php.git

cd openai-php

composer install

使用示例

以下示例假设您将API密钥存储在环境变量中。要在Linux或MacOS系统上执行此操作,请运行

export OPENAI_API_KEY=sk-XA0yN...

请确保在OPENAI_API_KEY=之后替换自己的API密钥。

完成

此示例要求gpt-3.5-turbo模型描述键盘。它指示OpenAI API使用超过默认数量的标记,以便返回合理的长度描述。

<?php

require __DIR__ . '/vendor/autoload.php';

// @see https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAICompletions($api_key);

$parameters = [
    'max_tokens' => 128,
];

$response = $api->create('gpt-3.5-turbo', 'Describe a keyboard', 1, $parameters);

var_dump($response);

响应

object(stdClass)#34 (6) {
  ["id"]=>
  string(34) "cmpl-7EqnVAO0xpxtWkDepi2s64AG7UAbU"
  ["object"]=>
  string(15) "text_completion"
  ["created"]=>
  int(1683773901)
  ["model"]=>
  string(16) "gpt-3.5-turbo"
  ["choices"]=>
  array(1) {
    [0]=>
    object(stdClass)#32 (4) {
      ["text"]=>
      string(663) "

A keyboard is a computer peripheral device used for entering data and commands into a computer or other device. It consists of a keypad, usually a set of standard-sized keys corresponding to letters, numbers, symbols, and/or functionality, which can be powered by various means including electricity, batteries, or solar cells. It generally has several special-function keys that can be used to control program functions, such as volume or brightness levels. Modern keyboards often include multimedia hotkeys, and are typically back-lit in order to make typing in the dark easier. Some keyboards may include a palm rest to help reduce fatigue caused by extended"
      ["index"]=>
      int(0)
      ["logprobs"]=>
      NULL
      ["finish_reason"]=>
      string(6) "length"
    }
  }
  ["usage"]=>
  object(stdClass)#18 (3) {
    ["prompt_tokens"]=>
    int(4)
    ["completion_tokens"]=>
    int(128)
    ["total_tokens"]=>
    int(132)
  }
}

聊天

此示例发送了一条简单的聊天消息。

<?php

require __DIR__ . '/vendor/autoload.php';

// @see https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAIChat($api_key);

$messages = [
    (object) ['role' => 'user', 'content' => 'Hello, friend!'],
];

$response = $api->create('gpt-3.5-turbo', $messages);

var_dump($response);

响应

object(stdClass)#35 (6) {
  ["id"]=>
  string(38) "chatcmpl-7EqphlIJngrqxGAiDdtV9JMu89NhQ"
  ["object"]=>
  string(15) "chat.completion"
  ["created"]=>
  int(1683774037)
  ["model"]=>
  string(18) "gpt-3.5-turbo-0301"
  ["usage"]=>
  object(stdClass)#33 (3) {
    ["prompt_tokens"]=>
    int(12)
    ["completion_tokens"]=>
    int(10)
    ["total_tokens"]=>
    int(22)
  }
  ["choices"]=>
  array(1) {
    [0]=>
    object(stdClass)#29 (3) {
      ["message"]=>
      object(stdClass)#19 (2) {
        ["role"]=>
        string(9) "assistant"
        ["content"]=>
        string(40) "Hello there, how may I assist you today?"
      }
      ["finish_reason"]=>
      string(4) "stop"
      ["index"]=>
      int(0)
    }
  }
}

图像

创建图像

此示例要求提供两张红色六面骰子在水中的图像。

<?php

require __DIR__ . '/vendor/autoload.php';

// @see https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAIImages($api_key);

$response = $api->createAsURL('A red six-sided dice in water', 2, '256x256');

响应

img-OFmHrZWgvmnGmizogrqP5SGv img-9O0Nv8GCurUr86pzzkv4dlJE

创建图像变体

<?php

require __DIR__ . '/vendor/autoload.php';

// @see https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAIImages($api_key);

$response = $api->createVariationAsURL('variation.png', 1, '256x256');

源和响应

variation variation1

创建图像编辑

<?php

require __DIR__ . '/vendor/autoload.php';

// @see https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAIImages($api_key);

$response = $api->createEditAsURL('edit.png', 'a duck driving a car', 'edit_mask.png', 1, '256x256');

源和响应

edit edit_mask edit1

微调模型

首先,请查看官方微调文档

创建并上传您的训练文件。示例

training.jsonl

{"prompt": "bird =", "completion": " animal"}
{"prompt": "dog =", "completion": " animal"}
{"prompt": "cat =", "completion": " animal"}
{"prompt": "limestone =", "completion": " rock"}
{"prompt": "shale =", "completion": " rock"}
{"prompt": "marble =", "completion": " rock"}
<?php

require __DIR__ . '/vendor/autoload.php';

// @see https://platform.openai.com/docs/api-reference/authentication
$api_key = getenv('OPENAI_API_KEY');

$api = new OpenAI\OpenAIFiles($api_key);

$file = $api->uploadFile('training.jsonl');

列出您的文件以获取文件ID

$api = new OpenAI\OpenAIFiles($api_key);

$files = $api->getFiles();

var_dump($files);

使用您获得的文件ID创建新的训练作业

$api = new OpenAI\OpenAIFineTunes($api_key);

$api->create('file-1CO...');

列出模型以获取您新微调模型的名字

$api = new OpenAI\OpenAIModels($api_key);

$models = $api->getModels();

var_dump($models);

模型名字将以curie:ft开头,并以当前日期和时间结尾。

使用您的微调模型创建新的完成。

$api = new OpenAI\OpenAICompletions($api_key);

$response = $api->create('curie:ft...', 'dog =', 1, $parameters);

响应应包括"animal"。

$api = new OpenAI\OpenAICompletions($api_key);

$response = $api->create('curie:ft...', 'marble =', 1, $parameters);

响应应包括"rock"。

可用函数

模型

完成

聊天

编辑

图像

嵌入

音频

文件

微调

审查

许可

MIT