Skip to content

Commit c5a5d9b

Browse files
committed
feat: add ContractInterface for more flexible normalizer configuration
1 parent 58186ba commit c5a5d9b

File tree

19 files changed

+185
-78
lines changed

19 files changed

+185
-78
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract;
6+
7+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
8+
9+
/**
10+
* @author Denis Zunke <[email protected]>
11+
*/
12+
final readonly class AnthropicSet
13+
{
14+
/** @return array<NormalizerInterface> */
15+
public static function get(): array
16+
{
17+
return [
18+
new AssistantMessageNormalizer(),
19+
new DocumentNormalizer(),
20+
new DocumentUrlNormalizer(),
21+
new ImageNormalizer(),
22+
new ImageUrlNormalizer(),
23+
new MessageBagNormalizer(),
24+
new ToolCallMessageNormalizer(),
25+
new ToolNormalizer(),
26+
];
27+
}
28+
}

src/Platform/Bridge/Anthropic/PlatformFactory.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,9 @@
44

55
namespace PhpLlm\LlmChain\Platform\Bridge\Anthropic;
66

7-
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\AssistantMessageNormalizer;
8-
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\DocumentNormalizer;
9-
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\DocumentUrlNormalizer;
10-
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\ImageNormalizer;
11-
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\ImageUrlNormalizer;
12-
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\MessageBagNormalizer;
13-
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\ToolCallMessageNormalizer;
14-
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\ToolNormalizer;
7+
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\AnthropicSet;
158
use PhpLlm\LlmChain\Platform\Contract;
9+
use PhpLlm\LlmChain\Platform\ContractInterface;
1610
use PhpLlm\LlmChain\Platform\Platform;
1711
use Symfony\Component\HttpClient\EventSourceHttpClient;
1812
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -27,22 +21,14 @@ public static function create(
2721
string $apiKey,
2822
string $version = '2023-06-01',
2923
?HttpClientInterface $httpClient = null,
24+
?ContractInterface $contract = null,
3025
): Platform {
3126
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
3227

3328
return new Platform(
3429
[new ModelClient($httpClient, $apiKey, $version)],
3530
[new ResponseConverter()],
36-
Contract::create(
37-
new AssistantMessageNormalizer(),
38-
new DocumentNormalizer(),
39-
new DocumentUrlNormalizer(),
40-
new ImageNormalizer(),
41-
new ImageUrlNormalizer(),
42-
new MessageBagNormalizer(),
43-
new ToolCallMessageNormalizer(),
44-
new ToolNormalizer(),
45-
)
31+
$contract ?? Contract::create(...AnthropicSet::get()),
4632
);
4733
}
4834
}

src/Platform/Bridge/Azure/Meta/PlatformFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Platform\Bridge\Azure\Meta;
66

7+
use PhpLlm\LlmChain\Platform\ContractInterface;
78
use PhpLlm\LlmChain\Platform\Platform;
89
use Symfony\Component\HttpClient\HttpClient;
910
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -18,9 +19,10 @@ public static function create(
1819
#[\SensitiveParameter]
1920
string $apiKey,
2021
?HttpClientInterface $httpClient = null,
22+
?ContractInterface $contract = null,
2123
): Platform {
2224
$modelClient = new LlamaHandler($httpClient ?? HttpClient::create(), $baseUrl, $apiKey);
2325

24-
return new Platform([$modelClient], [$modelClient]);
26+
return new Platform([$modelClient], [$modelClient], $contract);
2527
}
2628
}

src/Platform/Bridge/Azure/OpenAI/PlatformFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\GPT\ResponseConverter;
99
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\AudioNormalizer;
1010
use PhpLlm\LlmChain\Platform\Contract;
11+
use PhpLlm\LlmChain\Platform\ContractInterface;
1112
use PhpLlm\LlmChain\Platform\Platform;
1213
use Symfony\Component\HttpClient\EventSourceHttpClient;
1314
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -24,6 +25,7 @@ public static function create(
2425
#[\SensitiveParameter]
2526
string $apiKey,
2627
?HttpClientInterface $httpClient = null,
28+
?ContractInterface $contract = null,
2729
): Platform {
2830
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2931
$embeddingsResponseFactory = new EmbeddingsModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey);
@@ -33,7 +35,7 @@ public static function create(
3335
return new Platform(
3436
[$GPTResponseFactory, $embeddingsResponseFactory, $whisperResponseFactory],
3537
[new ResponseConverter(), new Embeddings\ResponseConverter(), new \PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\ResponseConverter()],
36-
Contract::create(new AudioNormalizer()),
38+
$contract ?? Contract::create(new AudioNormalizer()),
3739
);
3840
}
3941
}

src/Platform/Bridge/Bedrock/Platform.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PhpLlm\LlmChain\Platform\Bridge\Bedrock\Nova\Contract as NovaContract;
77
use PhpLlm\LlmChain\Platform\Bridge\Meta\Contract as LlamaContract;
88
use PhpLlm\LlmChain\Platform\Contract;
9+
use PhpLlm\LlmChain\Platform\ContractInterface;
910
use PhpLlm\LlmChain\Platform\Exception\RuntimeException;
1011
use PhpLlm\LlmChain\Platform\Model;
1112
use PhpLlm\LlmChain\Platform\PlatformInterface;
@@ -26,7 +27,7 @@ class Platform implements PlatformInterface
2627
*/
2728
public function __construct(
2829
iterable $modelClients,
29-
private ?Contract $contract = null,
30+
private ?ContractInterface $contract = null,
3031
) {
3132
$this->contract = $contract ?? Contract::create(
3233
new AnthropicContract\AssistantMessageNormalizer(),

src/Platform/Bridge/Bedrock/PlatformFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpLlm\LlmChain\Platform\Bridge\Bedrock\Anthropic\ClaudeHandler;
99
use PhpLlm\LlmChain\Platform\Bridge\Bedrock\Meta\LlamaModelClient;
1010
use PhpLlm\LlmChain\Platform\Bridge\Bedrock\Nova\NovaHandler;
11+
use PhpLlm\LlmChain\Platform\ContractInterface;
1112

1213
/**
1314
* @author Björn Altmann
@@ -16,11 +17,12 @@
1617
{
1718
public static function create(
1819
BedrockRuntimeClient $bedrockRuntimeClient = new BedrockRuntimeClient(),
20+
?ContractInterface $contract = null,
1921
): Platform {
2022
$modelClient[] = new ClaudeHandler($bedrockRuntimeClient);
2123
$modelClient[] = new NovaHandler($bedrockRuntimeClient);
2224
$modelClient[] = new LlamaModelClient($bedrockRuntimeClient);
2325

24-
return new Platform($modelClient);
26+
return new Platform($modelClient, $contract);
2527
}
2628
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Platform\Bridge\Google\Contract;
6+
7+
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
8+
9+
/**
10+
* @author Denis Zunke <[email protected]>
11+
*/
12+
final readonly class GoogleSet
13+
{
14+
/** @return array<NormalizerInterface> */
15+
public static function get(): array
16+
{
17+
return [
18+
new AssistantMessageNormalizer(),
19+
new MessageBagNormalizer(),
20+
new ToolNormalizer(),
21+
new ToolCallMessageNormalizer(),
22+
new UserMessageNormalizer(),
23+
];
24+
}
25+
}

src/Platform/Bridge/Google/PlatformFactory.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
namespace PhpLlm\LlmChain\Platform\Bridge\Google;
66

7-
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\AssistantMessageNormalizer;
8-
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\MessageBagNormalizer;
9-
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\ToolCallMessageNormalizer;
10-
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\ToolNormalizer;
11-
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\UserMessageNormalizer;
7+
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\GoogleSet;
128
use PhpLlm\LlmChain\Platform\Bridge\Google\Embeddings\ModelClient;
139
use PhpLlm\LlmChain\Platform\Contract;
10+
use PhpLlm\LlmChain\Platform\ContractInterface;
1411
use PhpLlm\LlmChain\Platform\Platform;
1512
use Symfony\Component\HttpClient\EventSourceHttpClient;
1613
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -24,17 +21,16 @@ public static function create(
2421
#[\SensitiveParameter]
2522
string $apiKey,
2623
?HttpClientInterface $httpClient = null,
24+
?ContractInterface $contract = null,
2725
): Platform {
2826
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2927
$responseHandler = new ModelHandler($httpClient, $apiKey);
3028
$embeddings = new ModelClient($httpClient, $apiKey);
3129

32-
return new Platform([$responseHandler, $embeddings], [$responseHandler, $embeddings], Contract::create(
33-
new AssistantMessageNormalizer(),
34-
new MessageBagNormalizer(),
35-
new ToolNormalizer(),
36-
new ToolCallMessageNormalizer(),
37-
new UserMessageNormalizer(),
38-
));
30+
return new Platform(
31+
[$responseHandler, $embeddings],
32+
[$responseHandler, $embeddings],
33+
$contract ?? Contract::create(...GoogleSet::get()),
34+
);
3935
}
4036
}

src/Platform/Bridge/HuggingFace/PlatformFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpLlm\LlmChain\Platform\Bridge\HuggingFace\Contract\FileNormalizer;
88
use PhpLlm\LlmChain\Platform\Bridge\HuggingFace\Contract\MessageBagNormalizer;
99
use PhpLlm\LlmChain\Platform\Contract;
10+
use PhpLlm\LlmChain\Platform\ContractInterface;
1011
use PhpLlm\LlmChain\Platform\Platform;
1112
use Symfony\Component\HttpClient\EventSourceHttpClient;
1213
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -21,13 +22,14 @@ public static function create(
2122
string $apiKey,
2223
string $provider = Provider::HF_INFERENCE,
2324
?HttpClientInterface $httpClient = null,
25+
?ContractInterface $contract = null,
2426
): Platform {
2527
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2628

2729
return new Platform(
2830
[new ModelClient($httpClient, $provider, $apiKey)],
2931
[new ResponseConverter()],
30-
Contract::create(
32+
$contract ?? Contract::create(
3133
new FileNormalizer(),
3234
new MessageBagNormalizer(),
3335
),

src/Platform/Bridge/Mistral/PlatformFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PhpLlm\LlmChain\Platform\Bridge\Mistral\Llm\ModelClient as MistralModelClient;
1111
use PhpLlm\LlmChain\Platform\Bridge\Mistral\Llm\ResponseConverter as MistralResponseConverter;
1212
use PhpLlm\LlmChain\Platform\Contract;
13+
use PhpLlm\LlmChain\Platform\ContractInterface;
1314
use PhpLlm\LlmChain\Platform\Platform;
1415
use Symfony\Component\HttpClient\EventSourceHttpClient;
1516
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -23,13 +24,14 @@ public static function create(
2324
#[\SensitiveParameter]
2425
string $apiKey,
2526
?HttpClientInterface $httpClient = null,
27+
?ContractInterface $contract = null,
2628
): Platform {
2729
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2830

2931
return new Platform(
3032
[new EmbeddingsModelClient($httpClient, $apiKey), new MistralModelClient($httpClient, $apiKey)],
3133
[new EmbeddingsResponseConverter(), new MistralResponseConverter()],
32-
Contract::create(new ToolNormalizer()),
34+
$contract ?? Contract::create(new ToolNormalizer()),
3335
);
3436
}
3537
}

src/Platform/Bridge/Ollama/PlatformFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Platform\Bridge\Ollama;
66

7+
use PhpLlm\LlmChain\Platform\ContractInterface;
78
use PhpLlm\LlmChain\Platform\Platform;
89
use Symfony\Component\HttpClient\EventSourceHttpClient;
910
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -16,10 +17,11 @@ final class PlatformFactory
1617
public static function create(
1718
string $hostUrl = 'http://localhost:11434',
1819
?HttpClientInterface $httpClient = null,
20+
?ContractInterface $contract = null,
1921
): Platform {
2022
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2123
$handler = new LlamaModelHandler($httpClient, $hostUrl);
2224

23-
return new Platform([$handler], [$handler]);
25+
return new Platform([$handler], [$handler], $contract);
2426
}
2527
}

src/Platform/Bridge/OpenAI/PlatformFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\ModelClient as WhisperModelClient;
1414
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\ResponseConverter as WhisperResponseConverter;
1515
use PhpLlm\LlmChain\Platform\Contract;
16+
use PhpLlm\LlmChain\Platform\ContractInterface;
1617
use PhpLlm\LlmChain\Platform\Platform;
1718
use Symfony\Component\HttpClient\EventSourceHttpClient;
1819
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -26,6 +27,7 @@ public static function create(
2627
#[\SensitiveParameter]
2728
string $apiKey,
2829
?HttpClientInterface $httpClient = null,
30+
?ContractInterface $contract = null,
2931
): Platform {
3032
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
3133

@@ -44,7 +46,7 @@ public static function create(
4446
$dallEModelClient,
4547
new WhisperResponseConverter(),
4648
],
47-
Contract::create(new AudioNormalizer()),
49+
$contract ?? Contract::create(new AudioNormalizer()),
4850
);
4951
}
5052
}

src/Platform/Bridge/OpenRouter/PlatformFactory.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\MessageBagNormalizer;
99
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\UserMessageNormalizer;
1010
use PhpLlm\LlmChain\Platform\Contract;
11+
use PhpLlm\LlmChain\Platform\ContractInterface;
1112
use PhpLlm\LlmChain\Platform\Platform;
1213
use Symfony\Component\HttpClient\EventSourceHttpClient;
1314
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -21,14 +22,19 @@ public static function create(
2122
#[\SensitiveParameter]
2223
string $apiKey,
2324
?HttpClientInterface $httpClient = null,
25+
?ContractInterface $contract = null,
2426
): Platform {
2527
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2628
$handler = new Client($httpClient, $apiKey);
2729

28-
return new Platform([$handler], [$handler], Contract::create(
29-
new AssistantMessageNormalizer(),
30-
new MessageBagNormalizer(),
31-
new UserMessageNormalizer(),
32-
));
30+
return new Platform(
31+
[$handler],
32+
[$handler],
33+
$contract ?? Contract::create(
34+
new AssistantMessageNormalizer(),
35+
new MessageBagNormalizer(),
36+
new UserMessageNormalizer(),
37+
),
38+
);
3339
}
3440
}

src/Platform/Bridge/Replicate/PlatformFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpLlm\LlmChain\Platform\Bridge\Replicate\Contract\LlamaMessageBagNormalizer;
88
use PhpLlm\LlmChain\Platform\Contract;
9+
use PhpLlm\LlmChain\Platform\ContractInterface;
910
use PhpLlm\LlmChain\Platform\Platform;
1011
use Symfony\Component\Clock\Clock;
1112
use Symfony\Component\HttpClient\HttpClient;
@@ -20,11 +21,12 @@ public static function create(
2021
#[\SensitiveParameter]
2122
string $apiKey,
2223
?HttpClientInterface $httpClient = null,
24+
?ContractInterface $contract = null,
2325
): Platform {
2426
return new Platform(
2527
[new LlamaModelClient(new Client($httpClient ?? HttpClient::create(), new Clock(), $apiKey))],
2628
[new LlamaResponseConverter()],
27-
Contract::create(new LlamaMessageBagNormalizer()),
29+
$contract ?? Contract::create(new LlamaMessageBagNormalizer()),
2830
);
2931
}
3032
}

src/Platform/Bridge/Voyage/PlatformFactory.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PhpLlm\LlmChain\Platform\Bridge\Voyage;
66

7+
use PhpLlm\LlmChain\Platform\ContractInterface;
78
use PhpLlm\LlmChain\Platform\Platform;
89
use Symfony\Component\HttpClient\EventSourceHttpClient;
910
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -17,10 +18,11 @@ public static function create(
1718
#[\SensitiveParameter]
1819
string $apiKey,
1920
?HttpClientInterface $httpClient = null,
21+
?ContractInterface $contract = null,
2022
): Platform {
2123
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2224
$handler = new ModelHandler($httpClient, $apiKey);
2325

24-
return new Platform([$handler], [$handler]);
26+
return new Platform([$handler], [$handler], $contract);
2527
}
2628
}

0 commit comments

Comments
 (0)