Skip to content

Commit 30b9cbc

Browse files
authored
🤖 Automatic code style fixes
1 parent 5e6dd16 commit 30b9cbc

File tree

11 files changed

+56
-88
lines changed

11 files changed

+56
-88
lines changed

‎AIChat.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
class AIChat
99
{
1010
/** @var int preferUIlanguage config: guess language use, all sources */
11-
public const LANG_AUTO_ALL = 0;
11+
final public const LANG_AUTO_ALL = 0;
1212
/** @var int preferUIlanguage config: use UI language, all sources */
13-
public const LANG_UI_ALL = 1;
13+
final public const LANG_UI_ALL = 1;
1414
/** @var int preferUIlanguage config: use UI language, limit sources */
15-
public const LANG_UI_LIMITED = 2;
15+
final public const LANG_UI_LIMITED = 2;
1616
}

‎Chunk.php‎

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,10 @@
22

33
namespace dokuwiki\plugin\aichat;
44

5-
class Chunk implements \JsonSerializable
5+
class Chunk implements \JsonSerializable, \Stringable
66
{
7-
/** @var string */
8-
protected $page;
9-
/** @var int */
10-
protected $id;
11-
/** @var string */
12-
protected $text;
13-
/** @var float[] */
14-
protected $embedding;
157
/** @var int */
168
protected $created;
17-
/** @var int */
18-
protected $score;
199
/** @var string */
2010
protected $language;
2111

@@ -25,19 +15,15 @@ class Chunk implements \JsonSerializable
2515
* @param string $text
2616
* @param float[] $embedding
2717
* @param int $created
18+
* @param int $score
2819
*/
29-
public function __construct($page, $id, $text, $embedding, $lang = '', $created = '', $score = 0)
20+
public function __construct(protected $page, protected $id, protected $text, protected $embedding, $lang = '', $created = '', protected $score = 0)
3021
{
31-
$this->page = $page;
32-
$this->id = $id;
33-
$this->text = $text;
34-
$this->embedding = $embedding;
3522
$this->language = $lang ?: $this->determineLanguage();
3623
$this->created = $created ?: time();
37-
$this->score = $score;
3824
}
3925

40-
public function __toString()
26+
public function __toString(): string
4127
{
4228
return $this->page . '#' . $this->id;
4329
}
@@ -181,7 +167,7 @@ protected function determineLanguage()
181167
*/
182168
public static function fromJSON($json)
183169
{
184-
$data = json_decode($json, true);
170+
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
185171
return new self(
186172
$data['page'],
187173
$data['id'],

‎Embeddings.php‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class Embeddings
2020
{
2121
/** @var int maximum overlap between chunks in tokens */
22-
public const MAX_OVERLAP_LEN = 200;
22+
final public const MAX_OVERLAP_LEN = 200;
2323

2424
/** @var AbstractModel */
2525
protected $model;
@@ -34,9 +34,6 @@ class Embeddings
3434
/** @var array remember sentences when chunking */
3535
private $sentenceQueue = [];
3636

37-
/**
38-
* @param AbstractModel $model
39-
*/
4037
public function __construct(AbstractModel $model, AbstractStorage $storage)
4138
{
4239
$this->model = $model;
@@ -56,7 +53,6 @@ public function getStorage()
5653
/**
5754
* Add a logger instance
5855
*
59-
* @param CLI $logger
6056
* @return void
6157
*/
6258
public function setLogger(CLI $logger)
@@ -98,7 +94,7 @@ public function createNewIndex($skipRE = '', $clear = false)
9894
!page_exists($page) ||
9995
isHiddenPage($page) ||
10096
filesize(wikiFN($page)) < 150 || // skip very small pages
101-
($skipRE && preg_match($skipRE, $page))
97+
($skipRE && preg_match($skipRE, (string) $page))
10298
) {
10399
// this page should not be in the index (anymore)
104100
$this->storage->deletePageChunks($page, $chunkID);
@@ -145,7 +141,7 @@ protected function createPageChunks($page, $firstChunkID)
145141

146142
$parts = $this->splitIntoChunks($text);
147143
foreach ($parts as $part) {
148-
if (trim($part) == '') continue; // skip empty chunks
144+
if (trim((string) $part) == '') continue; // skip empty chunks
149145

150146
try {
151147
$embedding = $this->model->getEmbedding($part);

‎Model/OpenAI/GPT35Turbo.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class GPT35Turbo extends AbstractModel
2929
];
3030

3131
/** @var int How often to retry a request if it fails */
32-
public const MAX_RETRIES = 3;
32+
final public const MAX_RETRIES = 3;
3333

3434
/** @var DokuHTTPClient */
3535
protected $http;
@@ -143,7 +143,7 @@ protected function request($endpoint, $data, $retry = 0)
143143
$url = 'https://api.openai.com/v1/' . $endpoint;
144144

145145
/** @noinspection PhpParamsInspection */
146-
$this->http->post($url, json_encode($data));
146+
$this->http->post($url, json_encode($data, JSON_THROW_ON_ERROR));
147147
$response = $this->http->resp_body;
148148
if ($response === false || $this->http->error) {
149149
if ($retry < self::MAX_RETRIES) {
@@ -154,7 +154,7 @@ protected function request($endpoint, $data, $retry = 0)
154154
throw new \Exception('OpenAI API returned no response. ' . $this->http->error);
155155
}
156156

157-
$result = json_decode($response, true);
157+
$result = json_decode((string) $response, true, 512, JSON_THROW_ON_ERROR);
158158
if (!$result) {
159159
$this->requestStart = 0;
160160
throw new \Exception('OpenAI API returned invalid JSON: ' . $response);

‎Storage/ChromaStorage.php‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct()
3939
$this->http->keep_alive = false;
4040
$this->http->timeout = 30;
4141

42-
if($helper->getConf('chroma_apikey')) {
42+
if ($helper->getConf('chroma_apikey')) {
4343
$this->http->headers['Authorization'] = 'Bearer ' . $helper->getConf('chroma_apikey');
4444
}
4545
}
@@ -53,14 +53,14 @@ public function __construct()
5353
* @return mixed
5454
* @throws \Exception
5555
*/
56-
protected function runQuery($endpoint, $data, $method = 'POST')
56+
protected function runQuery($endpoint, mixed $data, $method = 'POST')
5757
{
5858
$url = $this->baseurl . '/api/v1' . $endpoint . '?tenant=' . $this->tenant . '&database=' . $this->database;
5959

6060
if (is_array($data) && $data === []) {
6161
$json = '{}';
6262
} else {
63-
$json = json_encode($data);
63+
$json = json_encode($data, JSON_THROW_ON_ERROR);
6464
}
6565

6666
$this->http->sendRequest($url, $json, $method);
@@ -71,19 +71,19 @@ protected function runQuery($endpoint, $data, $method = 'POST')
7171
}
7272

7373
try {
74-
$result = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
75-
} catch (\Exception $e) {
74+
$result = json_decode((string) $response, true, 512, JSON_THROW_ON_ERROR);
75+
} catch (\Exception) {
7676
throw new \Exception('Chroma API returned invalid JSON. ' . $response);
7777
}
7878

7979
if ((int)$this->http->status !== 200) {
8080
if (isset($result['detail'][0]['msg'])) {
8181
$error = $result['detail'][0]['msg'];
82-
} else if (isset($result['detail']['msg'])) {
82+
} elseif (isset($result['detail']['msg'])) {
8383
$error = $result['detail']['msg'];
84-
} else if (isset($result['detail']) && is_string($result['detail'])) {
84+
} elseif (isset($result['detail']) && is_string($result['detail'])) {
8585
$error = $result['detail'];
86-
} else if (isset($result['error'])) {
86+
} elseif (isset($result['error'])) {
8787
$error = $result['error'];
8888
} else {
8989
$error = $this->http->error;
@@ -164,9 +164,7 @@ public function deletePageChunks($page, $firstChunkID)
164164
{
165165
// delete all possible chunk IDs
166166
$ids = range($firstChunkID, $firstChunkID + 99, 1);
167-
$ids = array_map(function ($id) {
168-
return (string)$id;
169-
}, $ids);
167+
$ids = array_map(static fn($id) => (string)$id, $ids);
170168
$this->runQuery(
171169
'/collections/' . $this->getCollectionID() . '/delete',
172170
[
@@ -192,7 +190,6 @@ public function addPageChunks($chunks)
192190
'language' => $chunk->getLanguage()
193191
];
194192
$documents[] = $chunk->getText();
195-
196193
}
197194

198195
$this->runQuery(
@@ -222,9 +219,7 @@ public function runMaintenance()
222219
public function getPageChunks($page, $firstChunkID)
223220
{
224221
$ids = range($firstChunkID, $firstChunkID + 99, 1);
225-
$ids = array_map(function ($id) {
226-
return (string)$id;
227-
}, $ids);
222+
$ids = array_map(static fn($id) => (string)$id, $ids);
228223

229224
$data = $this->runQuery(
230225
'/collections/' . $this->getCollectionID() . '/get',

‎Storage/PineconeStorage.php‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ public function __construct()
4141
* @return mixed
4242
* @throws \Exception
4343
*/
44-
protected function runQuery($endpoint, $data, $method = 'POST')
44+
protected function runQuery($endpoint, mixed $data, $method = 'POST')
4545
{
4646
$url = $this->baseurl . $endpoint;
4747

4848
if (is_array($data) && $data === []) {
4949
$json = '{}';
5050
} else {
51-
$json = json_encode($data);
51+
$json = json_encode($data, JSON_THROW_ON_ERROR);
5252
}
5353

5454
$this->http->sendRequest($url, $json, $method);
@@ -57,7 +57,7 @@ protected function runQuery($endpoint, $data, $method = 'POST')
5757
throw new \Exception('Pinecone API returned no response. ' . $this->http->error);
5858
}
5959

60-
$result = json_decode($response, true);
60+
$result = json_decode((string) $response, true, 512, JSON_THROW_ON_ERROR);
6161
if ($result === null) {
6262
throw new \Exception('Pinecone API returned invalid JSON. ' . $response);
6363
}
@@ -104,7 +104,7 @@ public function startCreation($clear = false)
104104
if ($clear) {
105105
try {
106106
$this->runQuery('/vectors/delete', ['delete_all' => 'True']);
107-
} catch (\Exception $e) {
107+
} catch (\Exception) {
108108
// delete all seems not supported -> starter edition
109109
$this->overwrite = true;
110110
}
@@ -122,9 +122,7 @@ public function deletePageChunks($page, $firstChunkID)
122122
{
123123
// delete all possible chunk IDs
124124
$ids = range($firstChunkID, $firstChunkID + 99, 1);
125-
$ids = array_map(function ($id) {
126-
return (string)$id;
127-
}, $ids);
125+
$ids = array_map(static fn($id) => (string)$id, $ids);
128126
$this->runQuery('/vectors/delete', ['ids' => $ids]);
129127
}
130128

@@ -169,9 +167,7 @@ public function runMaintenance()
169167
public function getPageChunks($page, $firstChunkID)
170168
{
171169
$ids = range($firstChunkID, $firstChunkID + 99, 1);
172-
$ids = array_reduce($ids, function ($carry, $item) {
173-
return $carry . '&ids=' . $item;
174-
});
170+
$ids = array_reduce($ids, static fn($carry, $item) => $carry . '&ids=' . $item);
175171

176172
$data = $this->runQuery(
177173
'/vectors/fetch?' . $ids,

0 commit comments

Comments
 (0)