-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOpenAI.php
352 lines (297 loc) · 10.4 KB
/
OpenAI.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
declare(strict_types=1);
/**
* Gazelle\OpenAI
*
* Client for the OpenAI API.
* Example request for a common use case:
*
* curl -X POST https://api.openai.com/v1/completions \
* -H 'Authorization: Bearer {secretKey}' \
* -H 'OpenAI-Organization: {organizationId}' \
* -H 'Content-Type: application/vnd.api+json' \
* -d '{
* "model": "text-davinci-003",
* "prompt": "Summarize in 100 words: {torrent group description}",
* "max_tokens": 1000
* }'
*
* @see https://beta.openai.com/docs/introduction
* @see https://github.com/openai-php/client
*
* ========================================
*
* Database table schema:
*
CREATE TABLE `openai` (
`id` INT NOT NULL AUTO_INCREMENT,
`jobId` VARCHAR(128) NOT NULL,
`groupId` INT NOT NULL,
`object` VARCHAR(32),
`created` DATETIME DEFAULT NOW(),
`updated` DATETIME DEFAULT NOW() ON UPDATE CURRENT_TIMESTAMP,
`model` VARCHAR(32),
`text` TEXT,
`index` TINYINT,
`logprobs` TINYINT,
`finishReason` VARCHAR(16),
`promptTokens` SMALLINT,
`completionTokens` SMALLINT,
`totalTokens` SMALLINT,
`failCount` TINYINT DEFAULT 0,
`json` JSON,
`type` VARCHAR(16),
PRIMARY KEY (`id`,`jobId`,`groupId`)
);
*/
namespace Gazelle;
class OpenAI
{
# client and params
public $client = null;
private int $maxTokens = 2500; # $0.05
private string $model = "text-davinci-003";
# cache settings
private string $cachePrefix = "openai:";
private string $cacheDuration = "1 day";
/**
* __construct
*
* Load the OpenAI client.
*
* @param array $options
* @return void
*/
public function __construct(array $options = [])
{
$app = App::go();
if (!$app->env->enableOpenAi) {
throw new Exception("OpenAI support is disabled in the app config");
}
$openAiApi = $app->env->private("openAiApi");
$this->client = \OpenAI::client($openAiApi->secretKey);
}
/**
* test
*
* Test the OpenAI API.
*
* @param string $prompt
* @return OpenAI\Responses\Completions\CreateResponse
*/
public function test(string $prompt = "hello"): \OpenAI\Responses\Completions\CreateResponse
{
$response = $this->client->completions()->create([
"model" => $this->model,
"prompt" => $prompt,
"max_tokens" => $this->maxTokens,
]);
return $response;
}
/**
* summarize
*
* Generate a summary of a torrent group description.
* Store the summary in the database for later use.
*
* @see https://beta.openai.com/docs/api-reference/completions
*
* @param int $groupId
* @return array
*/
public function summarize(int $groupId): array
{
$app = App::go();
$app->debug["time"]->startMeasure("summarize", "openai: summarize groupId {$groupId}");
# return cached if available
$cacheKey = "{$this->cachePrefix}_summary_{$groupId}";
$cacheHit = $app->cache->get($cacheKey);
if ($cacheHit) {
return $cacheHit;
}
# get the torrent group description
$query = "select description from torrents_group where id = ?";
$description = $app->dbNew->single($query, [$groupId]);
if (!$description || empty($description)) {
throw new Exception("groupId {$groupId} not found or description empty");
}
# process the description
$description = $this->processDescription($description);
# query the openai api
try {
$response = $this->client->completions()->create([
"model" => $this->model,
"prompt" => "Summarize in 100 words: {$description}",
"max_tokens" => $this->maxTokens,
]);
!d($response);
# cast to an array and save to the database
$response = $response->toArray();
$this->insertResponse($groupId, "summary", $response);
} catch (\Throwable $e) {
throw new Exception($e->getMessage());
}
$app->cache->set($cacheKey, $response, $this->cacheDuration);
return $response;
}
/**
* keywords
*
* Generate a list of keywords from a summary (good) or a torrent group description (bad).
* Store the keywords in the database for later use and add them to `torrents_tags`.
*
* @see https://beta.openai.com/docs/api-reference/completions
*
* @param int $groupId
* @return array
*/
public function keywords(int $groupId): array
{
$app = App::go();
$app->debug["time"]->startMeasure("keywords", "openai: keywords for groupId {$groupId}");
# return cached if available
$cacheKey = "{$this->cachePrefix}_keywords_{$groupId}";
$cacheHit = $app->cache->get($cacheKey);
if ($cacheHit) {
return $cacheHit;
}
# try to get a tl;dr summary
$query = "select text from openai where groupId = ? and type = ?";
$description = $app->dbNew->single($query, [$groupId, "summary"]);
# get a description if no summary exists
if (!$description || empty($description)) {
$query = "select description from torrents_group where id = ?";
$description = $app->dbNew->single($query, [$groupId]);
}
if (!$description || empty($description)) {
throw new Exception("groupId {$groupId} not found or description empty");
}
# process the description
$description = $this->processDescription($description);
# query the openai api
try {
$response = $this->client->completions()->create([
"model" => $this->model,
"prompt" => "List 10 keywords in the format [\"one\", \"two\", \"three\"]: {$description}",
"max_tokens" => $this->maxTokens,
]);
!d($response);
# cast to an array and save to the database
$response = $response->toArray();
$this->insertResponse($groupId, "keywords", $response);
} catch (\Throwable $e) {
throw new Exception($e->getMessage());
}
# process response into an array
$keywords = json_decode(Text::oneLine($response["choices"][0]["text"]), true);
if (!$keywords || !is_array($keywords)) {
throw new Exception("openai fucked up jobId {$response["id"]}");
}
# convert to gazelle tags
$keywords = array_unique($keywords);
foreach ($keywords as $key => $value) {
$value = \Illuminate\Support\Str::slug($value, ".");
$keywords[$key] = $value;
}
# insert into the database
# sections/upload/upload_handle.php
foreach ($keywords as $keyword) {
$keyword = \Misc::get_alias_tag($keyword);
# inset into tags
$query = "
insert into tags (name, tagType, userId) values (?, ?, ?)
on duplicate key update uses = uses + 1
";
$app->dbNew->do($query, [$keyword, "openai", 0]);
# get tagId
$tagId = $app->dbNew->source->lastInsertId();
# insert into torrents_tags
$query = "
insert into torrents_tags (tagId, groupId, userId) values (?, ?, ?)
on duplicate key update tagId = tagId
";
$app->dbNew->do($query, [$tagId, $groupId, 0]);
}
$app->cache->set($cacheKey, $response, $this->cacheDuration);
return $response;
}
/**
* processDescription
*
* Remove garbage from a text prompt.
*
* @param string $description
* @return string
*/
private function processDescription(string $description): string
{
$description = Text::parse($description);
$description = strip_tags($description);
$description = Text::oneLine($description);
return $description;
}
/**
* insertResponse
*
* Write an OpenAI API response to the database.
*
* @param int $groupId
* @param string $type
* @param array $response
* @return void
*/
private function insertResponse(int $groupId, string $type, array $response): void
{
$app = App::go();
$allowedTypes = ["summary", "keywords"];
if (!in_array($type, $allowedTypes)) {
throw new Exception("type must be one of " . implode(", ", $allowedTypes) . ", {$type} given");
}
# format the data
$data = [
"jobId" => $response["id"],
"groupId" => $groupId,
"object" => $response["object"],
"created" => \Carbon\Carbon::createFromTimestamp($response["created"])->toDateTimeString(),
"model" => $response["model"],
"text" => Text::oneLine($response["choices"][0]["text"]),
"index" => $response["choices"][0]["index"],
"logprobs" => $response["choices"][0]["logprobs"],
"finishReason" => $response["choices"][0]["finish_reason"],
"promptTokens" => $response["usage"]["prompt_tokens"],
"completionTokens" => $response["usage"]["completion_tokens"],
"totalTokens" => $response["usage"]["total_tokens"],
"json" => json_encode($response),
"type" => $type,
];
# get the failCount
$query = "select failCount from openai where groupId = ? and type = ?";
$failCount = $app->dbNew->single($query, [$groupId, $type]) ?? 0;
$data["failCount"] = $failCount;
# increment on an error
if (empty($data["text"]) || $data["finishReason"] !== "stop") {
$data["failCount"]++;
}
# debug
!d($data);
# the query itself
$query = "
insert into openai (
jobId, groupId,
object, created, model,
text, `index`, logprobs, finishReason,
promptTokens, completionTokens, totalTokens,
failCount, json, type
)
values (
:jobId, :groupId,
:object, :created, :model,
:text, :index, :logprobs, :finishReason,
:promptTokens, :completionTokens, :totalTokens,
:failCount, :json, :type
)
";
# do it
$app->dbNew->do($query, $data);
}
} # class