Skip to content

Commit 203fc6a

Browse files
author
Michał Żarnecki
committed
tests & refactoring
1 parent b5ac450 commit 203fc6a

15 files changed

+69
-21
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,25 @@ Pick one from l2|cosine|innerProduct or support other one (see https://github.co
282282
) {
283283
```
284284

285+
## Test
286+
This project support static code analysis with PHP Linter, Rector PHP, PHPStan and testing with phpunit.
287+
Find below useful commands.
288+
289+
**Run all tests:** \
290+
`cd ./app/src && composer test`
291+
292+
Run PHP Linter \
293+
`cd ./app/src && composer lint`
294+
295+
Run Rector PHP \
296+
`cd ./app/src && composer refactor process`
297+
298+
Run PHPStan: \
299+
`cd ./app/src && composer test:types`
300+
301+
Run phpunit: \
302+
`cd ./app/src && composer test:unit`
303+
285304
## 📚 Resources
286305

287306
- Dataset: "Website Classification" by Hetul Mehta on [Kaggle](https://www.kaggle.com/datasets/hetulmehta/website-classification)

app/src/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
"test:lint": "pint --test -v",
4747
"test:refactor": "rector --dry-run ",
4848
"test:types": "phpstan analyse --ansi --memory-limit 4G",
49-
"test:unit": "cd ./test && php ../vendor/bin/phpunit",
49+
"test:unit": "cd ./test && php ../vendor/bin/phpunit .",
5050
"test": [
5151
"@test:lint",
5252
"@test:refactor",

app/src/service/DocumentLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function loadDocuments(): void
3939
}
4040

4141
foreach ($responseDocumentsChunks as $chunk) {
42-
$this->insertDocument($file, $document, $chunk);
42+
$this->insertDocument((string) $file, $document, $chunk);
4343
}
4444
$this->showProgress($index, $total, $skipFirstN);
4545
}

app/src/service/DocumentProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ public function getSimilarDocuments(
4242
/**
4343
* Sometimes its worth to use efficient algorithm for querying vector DB and more precise to rerank chosen documents in
4444
*
45-
* @return string[][]
45+
* @param string[][] $documents
46+
* @return string[]
4647
*/
4748
public function rerank(string $prompt, array $documents): array
4849
{
4950
$documentsReranked = [];
5051
foreach ($documents as $document) {
51-
$intersection = array_intersect(explode(' ', (string) $document['text']), explode(' ', $prompt));
52+
$intersection = array_intersect(explode(' ', $document['text']), explode(' ', $prompt));
5253
$index = count($intersection);
5354
while (isset($documentsReranked[$index])) {
5455
$index++;

app/src/service/PromptResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ final class PromptResolver implements StageInterface
1111
{
1212
public function getPromptFromInput(): string
1313
{
14-
$argv = [];
1514
$prompt = $_POST['prompt'] ?? null;
1615
if (! $prompt) {
16+
global $argv;
1717
$prompt = $argv[0] ?? null;
1818
}
1919
if (! $prompt) {

app/src/service/claude/AbstractClaudeAPIClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function __construct()
3030
}
3131

3232
/**
33-
* @return string[]
33+
* @return string[][][]
3434
*
3535
* @throws \JsonException
3636
*/

app/src/service/deepseek/AbstractDeepSeekAPIClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public function __construct()
2727
}
2828

2929
/**
30-
* @param string[] $messages
30+
* @param string[][] $messages
3131
* @param string[] $options
32-
* @return string[]
32+
* @return string[][][][]
3333
*
3434
* @throws \JsonException
3535
*/

app/src/service/evaluate/StringComparisonEvaluator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function calculateBLEU(string $reference, string $candidate, int $n = 1):
2222
$matches++;
2323
}
2424
}
25-
$nGramMatches[$i] = $matches / max((is_countable($candidateNGrams) ? count($candidateNGrams) : 0), 1);
25+
$nGramMatches[$i] = $matches / max(count($candidateNGrams), 1);
2626
}
2727

2828
$precision = array_product($nGramMatches);
@@ -51,8 +51,8 @@ public function calculateROUGE(string $reference, string $candidate, int $n = 1)
5151
}
5252
}
5353

54-
$recall = $matches / max(is_countable($referenceNGrams) ? count($referenceNGrams) : 0, 1);
55-
$precision = $matches / max(is_countable($candidateNGrams) ? count($candidateNGrams) : 0, 1);
54+
$recall = $matches / max(count($referenceNGrams), 1);
55+
$precision = $matches / max(count($candidateNGrams), 1);
5656
$f1Score = ($recall + $precision > 0)
5757
? 2 * ($recall * $precision) / ($recall + $precision)
5858
: 0;
@@ -65,6 +65,7 @@ public function calculateROUGE(string $reference, string $candidate, int $n = 1)
6565
}
6666

6767
/**
68+
* @param string[] $words
6869
* @return string[]
6970
*/
7071
private function getNGrams(array $words, int $n): array

app/src/service/gemini/AbstractGeminiAPIClient.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ public function __construct()
2929
]);
3030
}
3131

32+
/**
33+
* @param string[][][][] $data
34+
* @return string[][][][][][]
35+
*
36+
* @throws \JsonException
37+
*/
3238
protected function request(string $method, string $model, array $data): array
3339
{
3440
$url = "models/$model:$method?key=".$this->apiKey;

app/src/service/gemini/GeneratedTextFromGeminiProvider.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ public function generateText(string $prompt, string $sourceDocuments): string
2020
'generateContent',
2121
$this->model,
2222
[
23-
'contents' => ['parts' => [[
24-
'text' => $content,
25-
]]],
23+
'contents' => [
24+
'parts' => [
25+
['text' => $content],
26+
],
27+
],
2628
]
2729
);
2830

0 commit comments

Comments
 (0)