Máy khách OpenAI PHP

Để bắt đầu sử dụng gói OpenAI, bạn cần làm quen với nền tảng OpenAI. OpenAI có nhiều ứng dụng liên quan đến xử lý ngôn ngữ tự nhiên (NLP), tạo mã từ ngôn ngữ tự nhiên, v.v. Tôi khuyên bạn nên bắt đầu với tài liệu và ví dụ về API OpenAI

Kiểm tra gói này tại openai-php/client trên GitHub để bắt đầu tích hợp OpenAI với các ứng dụng PHP của bạn

Liệt kê các mô hình hiện có và cung cấp thông tin cơ bản về từng mô hình, chẳng hạn như chủ sở hữu và tính khả dụng

$response = $client->models()->list();

$response->object; // 'list'

foreach ($response->data as $result) {
    $result->id; // 'text-davinci-002'
    $result->object; // 'model'
    // ...
}

$response->toArray(); // ['object' => 'list', 'data' => [...]]

$response = $client->models()->list();

$response->object; // 'list'

foreach ($response->data as $result) {
    $result->id; // 'text-davinci-002'
    $result->object; // 'model'
    // ...
}

$response->toArray(); // ['object' => 'list', 'data' => [...]]
4

Truy xuất một phiên bản mô hình, cung cấp thông tin cơ bản về mô hình như chủ sở hữu và quyền

Liệt kê các mô hình hiện có và cung cấp thông tin cơ bản về từng mô hình, chẳng hạn như chủ sở hữu và tính khả dụng

$response = $client->models()->list();

$response->object; // 'list'

foreach ($response->data as $result) {
    $result->id; // 'text-davinci-002'
    $result->object; // 'model'
    // ...
}

$response->toArray(); // ['object' => 'list', 'data' => [...]]

$response = $client->models()->list();

$response->object; // 'list'

foreach ($response->data as $result) {
    $result->id; // 'text-davinci-002'
    $result->object; // 'model'
    // ...
}

$response->toArray(); // ['object' => 'list', 'data' => [...]]
4

Truy xuất một phiên bản mô hình, cung cấp thông tin cơ bản về mô hình như chủ sở hữu và quyền

Ứng dụng khách API REST của Tectalic OpenAI là gói cung cấp một cách thuận tiện và đơn giản để tương tác với API OpenAI từ ứng dụng PHP của bạn

Hỗ trợ các mô hình dựa trên GPT-3, Codex và DALL·E, Đối tượng truyền dữ liệu (DTO) được nhập đầy đủ cho tất cả các yêu cầu và phản hồi cũng như hỗ trợ tự động hoàn thành IDE

Thông tin thêm có sẵn từ https. //kiến tạo. com/apis/openai

Đây là gói không chính thức và không có liên kết với OpenAI

ví dụ

Việc tích hợp OpenAI vào ứng dụng của bạn giờ đây đơn giản chỉ bằng một vài dòng mã

Hoàn thành văn bản bằng GPT-3

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'text-davinci-002',

'prompt' => 'Will using a third party package save time?',

echo $response->choices[0]->text;

// Using a third party package can save time because you don't have to write the code yourself.

Tìm hiểu thêm về hoàn thành văn bản

Hoàn thành mã bằng Codex

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

Tìm hiểu thêm về hoàn thành mã

Tạo ảnh bằng DALL·E

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

Tìm hiểu thêm về tạo ảnh

Cài đặt

Cần giúp đỡ để bắt đầu? . cách xây dựng ứng dụng bằng API OpenAI

yêu cầu hệ thống

  • PHP phiên bản 7. 2. 5 hoặc mới hơn (bao gồm cả PHP 8. 0 và 8. 1)
  • Tiện ích mở rộng PHP JSON được cài đặt nếu sử dụng PHP 7. x. Kể từ PHP 8. 0, tiện ích mở rộng này đã trở thành tiện ích mở rộng PHP cốt lõi nên luôn được bật
  • Máy khách HTTP tương thích PSR-18, chẳng hạn như 'Guzzle' hoặc 'Symfony HTTP Client'

cài đặt nhà soạn nhạc

Cài đặt gói vào dự án của bạn

composer require tectalic/openai

Cách sử dụng

Sau khi cài đặt gói Máy khách API REST của Tectalic OpenAI vào dự án của bạn, hãy đảm bảo rằng bạn cũng có một máy khách HTTP PSR-18 tương thích, chẳng hạn như ‘Guzzle’ hoặc ‘Máy khách HTTP’ của Symfony

Bạn có thể sử dụng mẫu mã sau và tùy chỉnh nó cho phù hợp với ứng dụng của mình

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

use Symfony\Component\HttpClient\Psr18Client;

use Tectalic\OpenAi\Authentication;

use Tectalic\OpenAi\Client;

use Tectalic\OpenAi\Manager;

// Build a Tectalic OpenAI REST API Client globally.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

Manager::build($httpClient, $auth);

// Build a Tectalic OpenAI REST API Client manually.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

$client = new Client($httpClient, $auth, Manager::BASE_URI);

xác thực

Để xác thực các yêu cầu API của bạn, bạn sẽ cần cung cấp một đối tượng

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

3 (

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

4) khi gọi

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

5

Xác thực API OpenAI bằng xác thực HTTP Bearer

Vui lòng xem tài liệu API OpenAI để biết thêm chi tiết về cách lấy thông tin đăng nhập xác thực của bạn

Trong mã Sử dụng ở trên, hãy tùy chỉnh hàm tạo

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

3 theo nhu cầu của bạn. Ví dụ: có thể sẽ cần thêm biến môi trường

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

7 vào hệ thống của bạn

Lớp khách hàng

Lớp chính mà bạn sẽ tương tác là lớp

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

8 (

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

9)

Lớp

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

8 này cũng chứa các phương thức của trình trợ giúp cho phép bạn nhanh chóng truy cập 13 Trình xử lý API

Vui lòng xem bên dưới để biết danh sách đầy đủ các trình xử lý và phương thức được hỗ trợ

Các phương thức và trình xử lý API được hỗ trợ

Gói này hỗ trợ 20 Phương thức API, được nhóm thành 13 Trình xử lý API

Xem bảng bên dưới để biết danh sách đầy đủ các Phương thức và Trình xử lý API

Tên phương thức và lớp trình xử lý API Mô tả Động từ API và URL

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

1Tạo phần hoàn thành cho dấu nhắc và tham số được cung cấp

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

2

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

3

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

4Tạo bản chỉnh sửa mới cho đầu vào, hướng dẫn và tham số được cung cấp

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

2

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

6

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

7Tạo một vectơ nhúng đại diện cho văn bản đầu vào.

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

2

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

9

composer require tectalic/openai

0Trả về danh sách các tệp thuộc về tổ chức của người dùng.

composer require tectalic/openai

1

composer require tectalic/openai

2

composer require tectalic/openai

3Tải lên tệp chứa (các) tài liệu sẽ được sử dụng trên nhiều điểm cuối/tính năng khác nhau. Hiện tại, kích thước của tất cả các tệp do một tổ chức tải lên có thể lên tới 1 GB. Vui lòng liên hệ với chúng tôi nếu bạn cần tăng hạn mức lưu trữ.

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

2

composer require tectalic/openai

2

composer require tectalic/openai

6Trả về thông tin về một tệp cụ thể.

composer require tectalic/openai

1

composer require tectalic/openai

8

composer require tectalic/openai

9Xóa tệp.

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

0

composer require tectalic/openai

8

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

2Trả về nội dung của tệp được chỉ định

composer require tectalic/openai

1

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

4

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

5Liệt kê các công việc tinh chỉnh của tổ chức bạn

composer require tectalic/openai

1

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

7

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

8Tạo một công việc tinh chỉnh một mô hình cụ thể từ một tập dữ liệu nhất định
Phản hồi bao gồm các chi tiết của công việc được xử lý bao gồm trạng thái công việc và tên của các mô hình tinh chỉnh sau khi hoàn thành
Tìm hiểu thêm về Tinh chỉnh

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

2

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

7

use Symfony\Component\HttpClient\Psr18Client;

use Tectalic\OpenAi\Authentication;

use Tectalic\OpenAi\Client;

use Tectalic\OpenAi\Manager;

// Build a Tectalic OpenAI REST API Client globally.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

Manager::build($httpClient, $auth);

// Build a Tectalic OpenAI REST API Client manually.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

$client = new Client($httpClient, $auth, Manager::BASE_URI);

1Nhận thông tin về công việc tinh chỉnh
Tìm hiểu thêm về Tinh chỉnh

composer require tectalic/openai

1

use Symfony\Component\HttpClient\Psr18Client;

use Tectalic\OpenAi\Authentication;

use Tectalic\OpenAi\Client;

use Tectalic\OpenAi\Manager;

// Build a Tectalic OpenAI REST API Client globally.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

Manager::build($httpClient, $auth);

// Build a Tectalic OpenAI REST API Client manually.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

$client = new Client($httpClient, $auth, Manager::BASE_URI);

3

use Symfony\Component\HttpClient\Psr18Client;

use Tectalic\OpenAi\Authentication;

use Tectalic\OpenAi\Client;

use Tectalic\OpenAi\Manager;

// Build a Tectalic OpenAI REST API Client globally.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

Manager::build($httpClient, $auth);

// Build a Tectalic OpenAI REST API Client manually.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

$client = new Client($httpClient, $auth, Manager::BASE_URI);

4Hủy ngay một công việc tinh chỉnh.

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

2

use Symfony\Component\HttpClient\Psr18Client;

use Tectalic\OpenAi\Authentication;

use Tectalic\OpenAi\Client;

use Tectalic\OpenAi\Manager;

// Build a Tectalic OpenAI REST API Client globally.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

Manager::build($httpClient, $auth);

// Build a Tectalic OpenAI REST API Client manually.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

$client = new Client($httpClient, $auth, Manager::BASE_URI);

6

use Symfony\Component\HttpClient\Psr18Client;

use Tectalic\OpenAi\Authentication;

use Tectalic\OpenAi\Client;

use Tectalic\OpenAi\Manager;

// Build a Tectalic OpenAI REST API Client globally.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

Manager::build($httpClient, $auth);

// Build a Tectalic OpenAI REST API Client manually.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

$client = new Client($httpClient, $auth, Manager::BASE_URI);

7Nhận thông tin cập nhật trạng thái chi tiết để hoàn thành công việc.

composer require tectalic/openai

1

use Symfony\Component\HttpClient\Psr18Client;

use Tectalic\OpenAi\Authentication;

use Tectalic\OpenAi\Client;

use Tectalic\OpenAi\Manager;

// Build a Tectalic OpenAI REST API Client globally.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

Manager::build($httpClient, $auth);

// Build a Tectalic OpenAI REST API Client manually.

$auth = new Authentication(getenv('OPENAI_API_KEY'));

$httpClient = new Psr18Client();

$client = new Client($httpClient, $auth, Manager::BASE_URI);

9_______570Tạo một hình ảnh được chỉnh sửa hoặc mở rộng với một hình ảnh gốc và lời nhắc.

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

2

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

72

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

73Tạo một hình ảnh khi có lời nhắc.

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

2

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

75

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

76Tạo một biến thể của một hình ảnh nhất định.

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

2

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

78

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

79Liệt kê các mẫu hiện có và cung cấp thông tin cơ bản về từng mẫu chẳng hạn như chủ sở hữu và tình trạng sẵn có.

composer require tectalic/openai

1

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

81____682Truy xuất một phiên bản mô hình, cung cấp thông tin cơ bản về mô hình như chủ sở hữu và quyền.

composer require tectalic/openai

1

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

84

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

85Xóa một mô hình tinh chỉnh. Bạn phải có vai trò Chủ sở hữu trong tổ chức của mình.

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

0

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

84

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

88Phân loại nếu văn bản vi phạm Chính sách nội dung của OpenAI

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

2

composer require tectalic/openai

70

Yêu cầu

Có hai cách để yêu cầu Trình xử lý API được chỉ định và Phương thức API

Nếu bạn đã xây dựng ứng dụng khách có thể truy cập được trên toàn cầu, bạn có thể sử dụng trực tiếp Lớp trình xử lý API có liên quan

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

7

Ngoài ra, bạn có thể truy cập tất cả Trình xử lý API từ lớp máy khách bằng cách sử dụng lớp Máy khách

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->imagesGenerations()->create(

'prompt' => 'A cute baby sea otter wearing a hat',

foreach ($response->data as $item) {

8

Truy xuất phản hồi

Khi bạn đã đưa ra yêu cầu bằng một trong hai phương pháp được nêu ở trên, bước tiếp theo là truy cập phản hồi

Bạn có thể truy cập phản hồi theo nhiều cách khác nhau. Vui lòng chọn một cái ưa thích của bạn

Phản hồi mô hình

Các phản hồi của mô hình là các lớp PHP kiểu Đối tượng truyền dữ liệu (DTO), với các thuộc tính công khai cho từng thuộc tính API

Họ cung cấp một cách có cấu trúc để truy xuất phản hồi từ yêu cầu API

Tất cả các Mô hình Phản hồi là một phiên bản của

composer require tectalic/openai

71 hoặc

composer require tectalic/openai

72

Sau đó, hãy sử dụng phương thức thông thạo

composer require tectalic/openai

73 cho Phương thức API

composer require tectalic/openai

7

Mỗi lệnh gọi

composer require tectalic/openai

74 của Phương thức API sẽ trả về loại lớp Mô hình thích hợp cho Phương thức API mà bạn vừa gọi

Phản hồi mảng kết hợp

Sau khi thực hiện yêu cầu, hãy sử dụng phương thức thông thạo

composer require tectalic/openai

75 cho Phương thức API

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

6

Trong mảng kết hợp thu được, các khóa của mảng sẽ khớp với tên của các thuộc tính công khai trong lớp Mô hình có liên quan

Đối tượng phản hồi PSR 7

Nếu bạn cần truy cập phản hồi thô hoặc kiểm tra các tiêu đề HTTP, hãy sử dụng phương thức thông thạo

composer require tectalic/openai

76 cho Phương thức API. Nó sẽ trả về một

composer require tectalic/openai

77

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

0

lỗi

Khi thực hiện các yêu cầu với Máy khách API REST của Tectalic OpenAI, các tình huống cụ thể sẽ khiến một

composer require tectalic/openai

78 bị ném. Xin vui lòng xem chi tiết bên dưới

Cách sử dụng không hợp lệ của lớp

composer require tectalic/openai

79

Một

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

60 sẽ bị ném ra nếu hàm

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

5 được gọi nhiều lần hoặc nếu hàm

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

62 được gọi trước khi gọi

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

5

Mã phản hồi HTTP không thành công

Ứng dụng khách API REST của Tectalic OpenAI phụ thuộc vào ứng dụng khách HTTP tương thích PSR-18 và ứng dụng khách HTTP đó không nên đưa ra ngoại lệ cho

Mã phản hồi không thành công được phân loại là mã không nằm trong phạm vi

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

64-

// Load your project's composer autoloader (if you aren't already doing so).

require_once(__DIR__ . '/vendor/autoload.php');

65 (bao gồm). Ví dụ về mã phản hồi không thành công bao gồm

  • Phản hồi thông tin (

    // Load your project's composer autoloader (if you aren't already doing so).

    require_once(__DIR__ . '/vendor/autoload.php');

    66-

    // Load your project's composer autoloader (if you aren't already doing so).

    require_once(__DIR__ . '/vendor/autoload.php');

    67)
  • Phản hồi chuyển hướng (

    // Load your project's composer autoloader (if you aren't already doing so).

    require_once(__DIR__ . '/vendor/autoload.php');

    68-

    // Load your project's composer autoloader (if you aren't already doing so).

    require_once(__DIR__ . '/vendor/autoload.php');

    69)
  • Phản hồi về lỗi của khách hàng (

    $openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

    $response = $openaiClient->completions()->create(

    'model' => 'code-davinci-002',

    'prompt' => "// PHP 8\n// A variable that saves the current date and time",

    echo $response->choices[0]->text;

    // $now = date("Y-m-d G:i:s")

    00-

    $openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

    $response = $openaiClient->completions()->create(

    'model' => 'code-davinci-002',

    'prompt' => "// PHP 8\n// A variable that saves the current date and time",

    echo $response->choices[0]->text;

    // $now = date("Y-m-d G:i:s")

    01)
  • Phản hồi lỗi máy chủ (

    $openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

    $response = $openaiClient->completions()->create(

    'model' => 'code-davinci-002',

    'prompt' => "// PHP 8\n// A variable that saves the current date and time",

    echo $response->choices[0]->text;

    // $now = date("Y-m-d G:i:s")

    02-

    $openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

    $response = $openaiClient->completions()->create(

    'model' => 'code-davinci-002',

    'prompt' => "// PHP 8\n// A variable that saves the current date and time",

    echo $response->choices[0]->text;

    // $now = date("Y-m-d G:i:s")

    03)

Nếu xảy ra mã phản hồi không thành công

  • Máy khách HTTP của bạn sẽ không đưa ra Ngoại lệ
  • phương pháp

    composer require tectalic/openai

    74 của Trình xử lý API sẽ tạo ra một

    $openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

    $response = $openaiClient->completions()->create(

    'model' => 'code-davinci-002',

    'prompt' => "// PHP 8\n// A variable that saves the current date and time",

    echo $response->choices[0]->text;

    // $now = date("Y-m-d G:i:s")

    05
  • phương pháp

    $openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

    $response = $openaiClient->completions()->create(

    'model' => 'code-davinci-002',

    'prompt' => "// PHP 8\n// A variable that saves the current date and time",

    echo $response->choices[0]->text;

    // $now = date("Y-m-d G:i:s")

    06 của Trình xử lý API sẽ trả về nội dung phản hồi và không ném một

    $openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

    $response = $openaiClient->completions()->create(

    'model' => 'code-davinci-002',

    'prompt' => "// PHP 8\n// A variable that saves the current date and time",

    echo $response->choices[0]->text;

    // $now = date("Y-m-d G:i:s")

    05
  • Phương thức

    $openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

    $response = $openaiClient->completions()->create(

    'model' => 'code-davinci-002',

    'prompt' => "// PHP 8\n// A variable that saves the current date and time",

    echo $response->choices[0]->text;

    // $now = date("Y-m-d G:i:s")

    08 của Trình xử lý API sẽ trả về phản hồi thô và không đưa ra một

    $openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

    $response = $openaiClient->completions()->create(

    'model' => 'code-davinci-002',

    'prompt' => "// PHP 8\n// A variable that saves the current date and time",

    echo $response->choices[0]->text;

    // $now = date("Y-m-d G:i:s")

    05

Dưới đây là một ví dụ về cách bạn có thể muốn sử dụng khối

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

10/

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

11 khi thực hiện một yêu cầu để bạn có thể phát hiện và xử lý các lỗi không mong muốn

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

1

Ngoại lệ máy khách HTTP

Nếu ứng dụng khách HTTP mà bạn chọn đưa ra một ngoại lệ không phải là

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

05, thì Ứng dụng khách API REST của OpenAI Tectalic

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

8 và các lớp Trình xử lý API của nó sẽ cho phép các ngoại lệ này xuất hiện

Tham khảo tài liệu về máy khách HTTP của bạn để biết thêm chi tiết về xử lý ngoại lệ

bài kiểm tra

Gói Máy khách API REST của Tectalic OpenAI bao gồm một số loại thử nghiệm PHPUnit tự động để xác minh hoạt động chính xác

  • bài kiểm tra đơn vị
  • Kiểm tra tích hợp

Để chạy các thử nghiệm này, bạn cần phải cài đặt gói Máy khách API REST của Tectalic OpenAI với các phần phụ thuộc dành cho nhà phát triển của nó (i. e. không sử dụng cờ

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

14 khi chạy trình soạn thảo)

bài kiểm tra đơn vị

Các bài kiểm tra PHPUnit này được thiết kế để

  • xác nhận rằng mỗi Phương thức API tập hợp một yêu cầu hợp lệ phù hợp với thông số kỹ thuật OpenAPI của API OpenAI
  • xác minh hành vi của các phần khác của gói, chẳng hạn như các lớp

    $openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

    $response = $openaiClient->completions()->create(

    'model' => 'code-davinci-002',

    'prompt' => "// PHP 8\n// A variable that saves the current date and time",

    echo $response->choices[0]->text;

    // $now = date("Y-m-d G:i:s")

    8 và

    composer require tectalic/openai

    79

Các bài kiểm tra đơn vị có thể được chạy bằng lệnh sau, lệnh này cần được chạy từ thư mục gốc của gói này

Các bài kiểm tra đơn vị không thực hiện bất kỳ yêu cầu thực nào đối với API OpenAI

Các bài kiểm tra đơn vị được đặt trong thư mục

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

17

Kiểm tra tích hợp

Các bài kiểm tra tích hợp nằm trong thư mục

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

18

Các bài kiểm tra PHPUnit này được thiết kế để xác nhận rằng mỗi Phương thức API phân tích một phản hồi hợp lệ, theo đặc tả OpenAPI của API OpenAI. Ngay lập tức, các bài kiểm tra tích hợp được thiết kế để hoạt động với Prism Mock Server

Sử dụng lăng kính làm mục tiêu

Đảm bảo Prism đã được cài đặt. Vui lòng xem tài liệu Prism để biết chi tiết về cách cài đặt Prism

Khi Prism được cài đặt, bạn có thể chạy prism và kiểm tra tích hợp song song trong các cửa sổ đầu cuối riêng biệt hoặc sử dụng lệnh sau, lệnh này cần được chạy từ thư mục gốc của gói này

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

2

Các lệnh đó sẽ khởi động máy chủ mô phỏng Prism, sau đó chạy thử nghiệm tích hợp, sau đó dừng máy chủ mô phỏng Prism khi quá trình kiểm tra hoàn tất

Trong trường hợp này, các bài kiểm tra tích hợp không thực hiện bất kỳ yêu cầu thực tế nào đối với API OpenAI

Sử dụng một mục tiêu khác

Bằng cách đặt biến môi trường

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

19, bạn có thể đặt mục tiêu điểm cuối API khác cho các thử nghiệm tích hợp

Ví dụ: thay vì sử dụng Prism, bạn có thể sử dụng một máy chủ mô phỏng/dàn dựng/kiểm tra khác mà bạn chọn hoặc bạn có thể sử dụng các điểm cuối trực tiếp của API OpenAI

Đừng quên đặt thông tin xác thực phù hợp trong biến môi trường

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

20

$openaiClient = Manager::build(new \GuzzleHttp\Client(), new Authentication(getenv('OPENAI_API_KEY')));

$response = $openaiClient->completions()->create(

'model' => 'code-davinci-002',

'prompt' => "// PHP 8\n// A variable that saves the current date and time",

echo $response->choices[0]->text;

// $now = date("Y-m-d G:i:s")

21

Sau khi thiết lập của bạn hoàn tất, chỉ cần chạy lệnh sau

Chúng tôi khuyên bạn không nên chạy thử nghiệm tích hợp đối với các điểm cuối API OpenAI trực tiếp. Điều này là do các thử nghiệm sẽ gửi dữ liệu mẫu đến tất cả các điểm cuối, điều này có thể dẫn đến dữ liệu mới được tạo hoặc dữ liệu hiện có bị xóa