-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat-with-function.php
More file actions
92 lines (74 loc) · 2.26 KB
/
chat-with-function.php
File metadata and controls
92 lines (74 loc) · 2.26 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
/*
* This file is part of OpenAi PHP Client.
*
* (c) Thomas Joußen <tjoussen91@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Thojou\OpenAi\OpenAi;
$API_KEY = $argv[1];
require_once __DIR__ . '/../vendor/autoload.php';
function hasEmployeeBirthday(string $employee): bool
{
return in_array($employee, ["Alice", "Bob", "Marry"], true);
}
// Describe available functions
$functions = [
[
'name' => 'hasEmployeeBirthday',
'description' => 'Check if employee has birthday today',
'parameters' => [
'type' => 'object',
'properties' => [
'employee' => ['type' => 'string']
],
'required' => [
'employee'
]
]
]
];
$messages = [
['role' => 'user', 'content' => 'Hello, is today Alice\'s birthday?']
];
// Pass the available functions into the chat completion
$openAi = new OpenAi($API_KEY);
$result = $openAi->chat()->completion([
'model' => 'gpt-3.5-turbo',
'messages' => $messages,
'functions' => $functions
]);
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";
if(!is_array($result['choices'])) {
return;
}
// if finish_reason is function_call, openai asked for a function call
if ($result['choices'][0]['finish_reason'] !== 'function_call') {
echo "No function call found\n";
return;
}
// Evaluate the function call
$functionCall = $result['choices'][0]['message']['function_call'];
$functionName = $functionCall['name'];
$functionArgs = (array)json_decode($functionCall['arguments'], true);
if (!function_exists($functionName)) {
echo "Function $functionName not found\n";
return;
}
// Call the function
$functionResult = call_user_func_array($functionName, $functionArgs);
// Insert the function result into the messages array. You don't have to add the function call answer from the assistant
$messages[] = [
'role' => 'function',
'content' => $functionResult ? 'Yes' : 'No',
'name' => $functionName,
];
$result = $openAi->chat()->completion([
'model' => 'gpt-3.5-turbo',
'messages' => $messages,
'functions' => $functions
]);
echo json_encode($result, JSON_PRETTY_PRINT) . "\n";