使用 PHP 透過 OpenAI API 生成文字補全

建立專案

建立專案。

1
2
mkdir gpt-function-calling
cd gpt-function-calling

安裝 openai-php/client 套件。

1
composer require openai-php/client

新增 .gitignore 檔。

1
2
/vendor
.env

新增 .env 檔。

1
OPENAI_API_KEY=your-openai-api-key

實作

Chat Completions API

新增 index.php 檔。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php

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

$dotenv = \Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

print_r(ask('你好嗎?'));

function ask(string $question): array {
$client = OpenAI::client($_ENV['OPENAI_API_KEY']);
$response = $client->chat()->create([
'model' => 'gpt-3.5-turbo-0613',
'messages' => [
[
'role' => 'user',
'content' => $question,
],
],
'temperature' => 1,
]);

return $response['choices'][0]['message'];
}

輸出如下:

1
2
3
4
5
Array
(
[role] => assistant
[content] => 我很好,謝謝你!有什麼我可以幫你的嗎?
)

Function Calling

修改 index.php 檔。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

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

$dotenv = \Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

print_r(ask('我想知道台北的天氣如何?'));

function ask(string $question): array {
$client = OpenAI::client($_ENV['OPENAI_API_KEY']);
$response = $client->chat()->create([
'model' => 'gpt-3.5-turbo-0613',
'messages' => [
[
'role' => 'user',
'content' => $question,
],
],
'temperature' => 1,
'functions' => [
[
'name' => 'get_wether',
'description' => 'Get the current weather in a given location.',
'parameters' => [
'type' => 'object',
'properties' => [
'location' => [
'type' => 'string',
'description' => 'The city and state, e.g. San Francisco, CA',
],
'unit' => [
'type' => 'string',
'enum' => ['celsius', 'fahrenheit'],
],
],
'required' => [
'location',
],
],
],
],
]);

return $response['choices'][0]['message'];
}

輸出如下:

1
2
3
4
5
6
7
8
9
10
11
12
Array
(
[role] => assistant
[content] =>
[function_call] => Array
(
[name] => get_wether
[arguments] => {
"location": "Taipei"
}
)
)

將 Weather API 的查詢結果,發送給 AI 做回應。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

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

$dotenv = \Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

print_r(ask('我想知道台北的天氣如何?'));

function ask(string $question): array {
$client = OpenAI::client($_ENV['OPENAI_API_KEY']);
$response = $client->chat()->create([
'model' => 'gpt-3.5-turbo-0613',
'messages' => [
[
'role' => 'user',
'content' => $question,
],
[
'role' => 'assistant',
'content' => null,
'function_call' => [
'name' => 'get_current_weather',
'arguments' => 'Taipei',
],
],
[
'role' => 'function',
'name' => 'get_current_weather',
'content' => json_encode([
'temperature' => 22,
'unit' => 'celsius',
'description' => 'Sunny',
]),
],
],
'temperature' => 1,
'functions' => [
[
'name' => 'get_wether',
'description' => 'Get the current weather in a given location.',
'parameters' => [
'type' => 'object',
'properties' => [
'location' => [
'type' => 'string',
'description' => 'The city and state, e.g. San Francisco, CA',
],
'unit' => [
'type' => 'string',
'enum' => ['celsius', 'fahrenheit'],
],
],
'required' => [
'location',
],
],
],
],
]);

return $response['choices'][0]['message'];
}

輸出如下:

1
2
3
4
5
Array
(
[role] => assistant
[content] => 台北的天氣目前是晴天,溫度約為攝氏22度。
)

參考資料