-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from llm-agents-php/feature/embedding-generator
Adds embedding generator
- Loading branch information
Showing
5 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LLM\Agents\OpenAI\Client\Embeddings; | ||
|
||
use LLM\Agents\Embeddings\Document; | ||
use LLM\Agents\Embeddings\Embedding; | ||
use LLM\Agents\Embeddings\EmbeddingGeneratorInterface; | ||
use OpenAI\Contracts\ClientContract; | ||
|
||
final readonly class EmbeddingGenerator implements EmbeddingGeneratorInterface | ||
{ | ||
public function __construct( | ||
private ClientContract $client, | ||
private OpenAIEmbeddingModel $model = OpenAIEmbeddingModel::TextEmbeddingAda002, | ||
) {} | ||
|
||
public function generate(Document ...$documents): array | ||
{ | ||
$documents = \array_values($documents); | ||
|
||
$response = $this->client->embeddings()->create([ | ||
'model' => $this->model->value, | ||
'input' => \array_map(static fn(Document $doc): string => $doc->content, $documents), | ||
]); | ||
|
||
foreach ($response->embeddings as $i => $embedding) { | ||
$documents[$i] = $documents[$i]->withEmbedding(new Embedding($embedding->embedding)); | ||
} | ||
|
||
return $documents; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace LLM\Agents\OpenAI\Client\Embeddings; | ||
|
||
enum OpenAIEmbeddingModel: string | ||
{ | ||
case TextEmbedding3Small = 'text-embedding-3-small'; | ||
case TextEmbedding3Large = 'text-embedding-3-large'; | ||
case TextEmbeddingAda002 = 'text-embedding-ada-002'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters