Skip to content

Commit 21c732b

Browse files
committed
feat: add default implementation for embedding generation
1 parent f47e1c6 commit 21c732b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/Model/Embedding/Embedder.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Lugha package.
5+
*
6+
* (c) Bernard Ngandu <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Devscast\Lugha\Model\Embedding;
15+
16+
use Devscast\Lugha\Exception\ServiceIntegrationException;
17+
use Devscast\Lugha\Provider\Service\HasEmbeddingSupport;
18+
use Devscast\Lugha\Retrieval\Document;
19+
20+
/**
21+
* Class EmbeddingGenerator.
22+
*
23+
* @author bernard-ng <[email protected]>
24+
*/
25+
final readonly class Embedder implements EmbeddingInterface
26+
{
27+
public function __construct(
28+
private HasEmbeddingSupport $client,
29+
private EmbeddingConfig $config
30+
) {
31+
}
32+
33+
/**
34+
* @throws ServiceIntegrationException
35+
*/
36+
#[\Override]
37+
public function embedDocuments(iterable $documents): iterable
38+
{
39+
foreach ($documents as $document) {
40+
yield $this->embedDocument($document);
41+
}
42+
}
43+
44+
/**
45+
* @throws ServiceIntegrationException
46+
*/
47+
#[\Override]
48+
public function embedDocument(Document $document): Document
49+
{
50+
$document->embeddings = $this->client->embeddings(
51+
prompt: $document->content,
52+
config: $this->config
53+
)->embedding;
54+
55+
return $document;
56+
}
57+
58+
/**
59+
* @throws ServiceIntegrationException
60+
*/
61+
#[\Override]
62+
public function embedQuery(string $query): array
63+
{
64+
return $this->client->embeddings($query, $this->config)->embedding;
65+
}
66+
}

0 commit comments

Comments
 (0)