Skip to content

Commit dd22ae4

Browse files
authored
Merge pull request #5 from llm-agents-php/feature/embedding-generator
Adds embedding generator
2 parents 2510748 + ae31687 commit dd22ae4

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

Diff for: composer.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"require": {
66
"php": "^8.3",
77
"openai-php/client": "^0.10.1",
8-
"llm-agents/agents": "^1.4",
8+
"llm-agents/agents": "^1.5",
99
"guzzlehttp/guzzle": "^7.0"
1010
},
1111
"require-dev": {
@@ -24,7 +24,10 @@
2424
}
2525
},
2626
"config": {
27-
"sort-packages": true
27+
"sort-packages": true,
28+
"allow-plugins": {
29+
"php-http/discovery": false
30+
}
2831
},
2932
"extra": {
3033
"laravel": {

Diff for: src/Embeddings/EmbeddingGenerator.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\OpenAI\Client\Embeddings;
6+
7+
use LLM\Agents\Embeddings\Document;
8+
use LLM\Agents\Embeddings\Embedding;
9+
use LLM\Agents\Embeddings\EmbeddingGeneratorInterface;
10+
use OpenAI\Contracts\ClientContract;
11+
12+
final readonly class EmbeddingGenerator implements EmbeddingGeneratorInterface
13+
{
14+
public function __construct(
15+
private ClientContract $client,
16+
private OpenAIEmbeddingModel $model = OpenAIEmbeddingModel::TextEmbeddingAda002,
17+
) {}
18+
19+
public function generate(Document ...$documents): array
20+
{
21+
$documents = \array_values($documents);
22+
23+
$response = $this->client->embeddings()->create([
24+
'model' => $this->model->value,
25+
'input' => \array_map(static fn(Document $doc): string => $doc->content, $documents),
26+
]);
27+
28+
foreach ($response->embeddings as $i => $embedding) {
29+
$documents[$i] = $documents[$i]->withEmbedding(new Embedding($embedding->embedding));
30+
}
31+
32+
return $documents;
33+
}
34+
}

Diff for: src/Embeddings/OpenAIEmbeddingModel.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace LLM\Agents\OpenAI\Client\Embeddings;
6+
7+
enum OpenAIEmbeddingModel: string
8+
{
9+
case TextEmbedding3Small = 'text-embedding-3-small';
10+
case TextEmbedding3Large = 'text-embedding-3-large';
11+
case TextEmbeddingAda002 = 'text-embedding-ada-002';
12+
}

Diff for: src/Integration/Laravel/OpenAIClientServiceProvider.php

+22
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66

77
use Illuminate\Contracts\Foundation\Application;
88
use Illuminate\Support\ServiceProvider;
9+
use LLM\Agents\Embeddings\EmbeddingGeneratorInterface;
910
use LLM\Agents\LLM\LLMInterface;
11+
use LLM\Agents\OpenAI\Client\Embeddings\EmbeddingGenerator;
12+
use LLM\Agents\OpenAI\Client\Embeddings\OpenAIEmbeddingModel;
1013
use LLM\Agents\OpenAI\Client\LLM;
1114
use LLM\Agents\OpenAI\Client\Parsers\ChatResponseParser;
1215
use LLM\Agents\OpenAI\Client\StreamResponseParser;
16+
use OpenAI\Contracts\ClientContract;
1317
use OpenAI\Responses\Chat\CreateStreamedResponse;
1418

1519
final class OpenAIClientServiceProvider extends ServiceProvider
@@ -21,6 +25,24 @@ public function register(): void
2125
LLM::class,
2226
);
2327

28+
$this->app->singleton(
29+
EmbeddingGeneratorInterface::class,
30+
EmbeddingGenerator::class,
31+
);
32+
33+
$this->app->singleton(
34+
EmbeddingGenerator::class,
35+
static function (
36+
ClientContract $client,
37+
): EmbeddingGenerator {
38+
return new EmbeddingGenerator(
39+
client: $client,
40+
// todo: use config
41+
model: OpenAIEmbeddingModel::TextEmbeddingAda002,
42+
);
43+
},
44+
);
45+
2446
$this->app->singleton(
2547
StreamResponseParser::class,
2648
static function (Application $app): StreamResponseParser {

Diff for: src/Integration/Spiral/OpenAIClientBootloader.php

+16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
namespace LLM\Agents\OpenAI\Client\Integration\Spiral;
66

77
use GuzzleHttp\Client as HttpClient;
8+
use LLM\Agents\Embeddings\EmbeddingGeneratorInterface;
89
use LLM\Agents\LLM\LLMInterface;
10+
use LLM\Agents\OpenAI\Client\Embeddings\EmbeddingGenerator;
11+
use LLM\Agents\OpenAI\Client\Embeddings\OpenAIEmbeddingModel;
912
use LLM\Agents\OpenAI\Client\LLM;
1013
use LLM\Agents\OpenAI\Client\Parsers\ChatResponseParser;
1114
use LLM\Agents\OpenAI\Client\StreamResponseParser;
@@ -20,6 +23,19 @@ public function defineSingletons(): array
2023
{
2124
return [
2225
LLMInterface::class => LLM::class,
26+
EmbeddingGeneratorInterface::class => EmbeddingGenerator::class,
27+
28+
EmbeddingGenerator::class => static function (
29+
ClientContract $client,
30+
EnvironmentInterface $env,
31+
): EmbeddingGenerator {
32+
return new EmbeddingGenerator(
33+
client: $client,
34+
model: OpenAIEmbeddingModel::from(
35+
$env->get('OPENAI_EMBEDDING_MODEL', OpenAIEmbeddingModel::TextEmbedding3Small->value),
36+
),
37+
);
38+
},
2339

2440
ClientContract::class => static fn(
2541
EnvironmentInterface $env,

0 commit comments

Comments
 (0)