|
13 | 13 |
|
14 | 14 | namespace Devscast\Lugha\Model\Embeddings; |
15 | 15 |
|
16 | | -use Devscast\Lugha\Provider\Service\HasEmbeddingSupport; |
| 16 | +use Devscast\Lugha\Provider\Service\HasEmbeddingsSupport; |
17 | 17 | use Devscast\Lugha\Retrieval\Document; |
18 | 18 |
|
19 | 19 | /** |
|
24 | 24 | final readonly class EmbeddingsGenerator implements EmbeddingsGeneratorInterface |
25 | 25 | { |
26 | 26 | public function __construct( |
27 | | - private HasEmbeddingSupport $client, |
28 | | - private EmbeddingsConfig $config |
| 27 | + private HasEmbeddingsSupport $client, |
| 28 | + private EmbeddingsConfig $config, |
| 29 | + private DimensionReducer $dimensionReducer = new DimensionReducer() |
29 | 30 | ) { |
30 | 31 | } |
31 | 32 |
|
32 | 33 | #[\Override] |
33 | | - public function embedDocuments(iterable $documents): iterable |
| 34 | + public function embedDocuments(iterable $documents, ?int $dimensions = null): iterable |
34 | 35 | { |
35 | 36 | foreach ($documents as $document) { |
36 | | - yield $this->embedDocument($document); |
| 37 | + yield $this->embedDocument($document, $dimensions); |
37 | 38 | } |
38 | 39 | } |
39 | 40 |
|
40 | 41 | #[\Override] |
41 | | - public function embedDocument(Document $document): Document |
| 42 | + public function embedDocument(Document $document, ?int $dimensions = null): Document |
42 | 43 | { |
43 | | - $values = $this->client->embeddings($document->content, $this->config)->embedding; |
44 | | - $document->embeddings = Vector::from($values); |
| 44 | + $values = $this->client->embeddings($document->content, $this->config)->embeddings; |
| 45 | + $vector = Vector::from($values); |
| 46 | + |
| 47 | + $document->embeddings = $dimensions !== null |
| 48 | + ? $this->dimensionReducer->reduce($vector, $dimensions) |
| 49 | + : $vector; |
45 | 50 |
|
46 | 51 | return $document; |
47 | 52 | } |
48 | 53 |
|
49 | 54 | #[\Override] |
50 | | - public function embedQuery(string $query): Vector |
| 55 | + public function embedQuery(string $query, ?int $dimensions = null): Vector |
51 | 56 | { |
52 | | - $values = $this->client->embeddings($query, $this->config)->embedding; |
| 57 | + $values = $this->client->embeddings($query, $this->config)->embeddings; |
| 58 | + $vector = Vector::from($values); |
53 | 59 |
|
54 | | - return Vector::from($values); |
| 60 | + return $dimensions !== null |
| 61 | + ? $this->dimensionReducer->reduce($vector, $dimensions) |
| 62 | + : $vector; |
55 | 63 | } |
56 | 64 | } |
0 commit comments