-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy path01-hello-embedding.php
More file actions
64 lines (46 loc) · 2.12 KB
/
Copy path01-hello-embedding.php
File metadata and controls
64 lines (46 loc) · 2.12 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
#!/usr/bin/env php
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use FerryAI\Embedding\Embedder;
use FerryAI\OnnxBackend\OnnxBackend;
use FerryAI\Tokenizer\TokenizerFactory;
$modelDir = getenv('FERRY_AI_MODEL_DIR') ?: dirname(__DIR__) . '/models/all-MiniLM-L6-v2-onnx';
$modelPath = $modelDir . '/model.onnx';
$tokenizerPath = $modelDir . '/tokenizer.json';
if (!file_exists($modelPath)) {
echo "=== SKIP: model not found at $modelPath ===\n";
echo "Set FERRY_AI_MODEL_DIR or download: sentence-transformers/all-MiniLM-L6-v2\n";
exit(0);
}
$backend = new OnnxBackend();
$tokenizer = (new TokenizerFactory())->createFromFile($tokenizerPath);
$embedder = new Embedder($modelPath, $backend, $tokenizer, 'mean', normalize: true);
echo "=== 01 — Hello Embedding ===\n\n";
$vec = $embedder->embed('Hello world');
printf("Text: 'Hello world'\n");
printf("Dimension: %d\n", count($vec));
printf("Model: %s\n", $embedder->modelName());
printf("Vector[0]: %.4f\n", $vec[0]);
printf("Vector[1]: %.4f\n", $vec[1]);
printf("Vector[2]: %.4f\n\n", $vec[2]);
echo "--- Batch Embedding ---\n\n";
$batch = $embedder->embedBatch(['The cat sat on the mat', 'Dogs are loyal companions', 'Birds fly in the sky']);
printf("Batch size: %d\n", count($batch));
foreach ($batch as $i => $v) {
printf("[%d] dim=%d [0]=%.4f\n", $i, count($v), $v[0]);
}
echo "\n--- Similarity ---\n\n";
$sim = $embedder->cosineSimilarity($embedder->embed('cat'), $embedder->embed('kitten'));
printf("cat vs kitten: %.4f\n", $sim);
$sim = $embedder->cosineSimilarity($embedder->embed('cat'), $embedder->embed('dog'));
printf("cat vs dog: %.4f\n", $sim);
$sim = $embedder->cosineSimilarity($embedder->embed('cat'), $embedder->embed('airplane'));
printf("cat vs airplane: %.4f\n\n", $sim);
echo "--- L2 Normalization ---\n\n";
$raw = $embedder->embed('test');
$normalized = $embedder->normalize($raw);
$norm = sqrt(array_sum(array_map(fn($v) => $v * $v, $normalized)));
printf("raw norm: %.4f\n", sqrt(array_sum(array_map(fn($v) => $v * $v, $raw))));
printf("normalized norm: %.4f\n\n", $norm);
echo "=== OK ===\n";