-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.php
143 lines (125 loc) · 4.54 KB
/
configure.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
use \OpenAI\Responses\Assistants\AssistantListResponse;
use \OpenAI\Responses\Models\ListResponse as ModelListResponse;
use \OpenAI\Testing\ClientFake;
if (isset($_SERVER['HTTP_HOST'])) {
header('Content-type: text/plain; charset=utf-8', true, 501);
die('Please run this on the command line, not through the web server.');
}
if (!file_exists(__DIR__ . '/vendor/autoload.php')
|| !file_exists(__DIR__ . '/vendor/openai-php/client/src/OpenAI.php')) {
die("Could not find the OpenAI client. Did you run 'composer install'?\n");
}
if (!file_exists('settings.json')) {
$b = @file_put_contents('settings.json', json_encode([]));
if (!$b) {
die('Could not create the settings.json file.');
} else {
print("Created an empty settings.json file.\n");
}
}
if (!is_readable('settings.json')) {
die('Could not read the settings.json file.');
}
if (!is_writable('settings.json')) {
die('Could not write the settings.json file.');
}
$_CONFIG = json_decode(file_get_contents('settings.json'), true);
require __DIR__ . '/vendor/autoload.php';
// Select the API key and retrieve the available models.
while (!isset($_CONFIG['api_key']) || !isset($_API) || empty($aModels)) {
$sInput = ($_CONFIG['api_key'] ?? '');
if (!$sInput) {
print('Enter the OpenAI API key to use: ');
$sInput = trim(fgets(STDIN));
}
if ($sInput) {
try {
if ($sInput == 'test') {
// Debugging.
$_API = new ClientFake(
[
ModelListResponse::fake(),
AssistantListResponse::fake(),
]
);
} else {
$_API = OpenAI::client($sInput);
}
$aModels = $_API->models()->list()->toArray();
if ($aModels && isset($aModels['data'])) {
$aModels = array_filter(
array_map(
function ($aModel)
{
return ($aModel['id'] ?? '');
}, $aModels['data']
)
);
} else {
print("Could not find any available models.\n");
$aModels = [];
continue;
}
} catch (Exception $e) {
print($e->getMessage() . "\n");
continue;
}
// If we get here, we successfully connected.
$_CONFIG['api_key'] = $sInput;
@file_put_contents('settings.json', json_encode($_CONFIG, JSON_PRETTY_PRINT));
}
}
// Select the model from the list of available models.
while (!isset($_CONFIG['model'])) {
if (count($aModels) == 1) {
$sInput = $aModels[0];
print("Selecting model $sInput as it's the only one available.\n");
} else {
print('Available models: ' . implode(', ', $aModels) . ".\n" .
'Select the model to use: ');
$sInput = trim(fgets(STDIN));
}
if ($sInput && in_array($sInput, $aModels)) {
$_CONFIG['model'] = $sInput;
@file_put_contents('settings.json', json_encode($_CONFIG, JSON_PRETTY_PRINT));
}
}
// Retrieve the list of assistants and select one.
while (!isset($_CONFIG['assistant'])) {
if (empty($aAssistants)) {
$aAssistants = $_API->assistants()->list()->toArray();
if ($aAssistants && isset($aAssistants['data'])) {
$aAssistants = array_filter(
array_map(
function ($aAssistant)
{
return ($aAssistant['id'] ?? '');
}, $aAssistants['data']
)
);
} else {
$aAssistants = [];
}
if (!$aAssistants) {
// FIXME: Create one?
die("Could not find any available assistants.\n");
}
}
if (count($aAssistants) == 1) {
$sInput = $aAssistants[0];
print("Selecting assistant $sInput as it's the only one available.\n");
} else {
print('Available assistants: ' . implode(', ', $aAssistants) . ".\n" .
'Select the assistant to use: ');
$sInput = trim(fgets(STDIN));
}
if ($sInput && in_array($sInput, $aAssistants)) {
$_CONFIG['assistant'] = $sInput;
@file_put_contents('settings.json', json_encode($_CONFIG, JSON_PRETTY_PRINT));
}
}
// Re-read file so we handle not having been able to write.
print("Stored the following settings:\n");
$_CONFIG = json_decode(file_get_contents('settings.json'), true);
var_dump($_CONFIG);