Skip to content

feat: open contract for more flexible normalizer configuration #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Platform/Bridge/Anthropic/Contract/AnthropicContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract;

use PhpLlm\LlmChain\Platform\Contract;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Denis Zunke <[email protected]>
*/
final readonly class AnthropicContract extends Contract
{
public static function create(NormalizerInterface ...$normalizer): Contract
{
return parent::create(
new AssistantMessageNormalizer(),
new DocumentNormalizer(),
new DocumentUrlNormalizer(),
new ImageNormalizer(),
new ImageUrlNormalizer(),
new MessageBagNormalizer(),
new ToolCallMessageNormalizer(),
new ToolNormalizer(),
...$normalizer,
);
}
}
21 changes: 3 additions & 18 deletions src/Platform/Bridge/Anthropic/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@

namespace PhpLlm\LlmChain\Platform\Bridge\Anthropic;

use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\AssistantMessageNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\DocumentNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\DocumentUrlNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\ImageNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\ImageUrlNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\MessageBagNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\ToolCallMessageNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\ToolNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\AnthropicContract;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
Expand All @@ -27,22 +20,14 @@ public static function create(
string $apiKey,
string $version = '2023-06-01',
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);

return new Platform(
[new ModelClient($httpClient, $apiKey, $version)],
[new ResponseConverter()],
Contract::create(
new AssistantMessageNormalizer(),
new DocumentNormalizer(),
new DocumentUrlNormalizer(),
new ImageNormalizer(),
new ImageUrlNormalizer(),
new MessageBagNormalizer(),
new ToolCallMessageNormalizer(),
new ToolNormalizer(),
)
$contract ?? AnthropicContract::create(),
);
}
}
4 changes: 3 additions & 1 deletion src/Platform/Bridge/Azure/Meta/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -18,9 +19,10 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
$modelClient = new LlamaHandler($httpClient ?? HttpClient::create(), $baseUrl, $apiKey);

return new Platform([$modelClient], [$modelClient]);
return new Platform([$modelClient], [$modelClient], $contract);
}
}
3 changes: 2 additions & 1 deletion src/Platform/Bridge/Azure/OpenAI/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
$embeddingsResponseFactory = new EmbeddingsModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey);
Expand All @@ -33,7 +34,7 @@ public static function create(
return new Platform(
[$GPTResponseFactory, $embeddingsResponseFactory, $whisperResponseFactory],
[new ResponseConverter(), new Embeddings\ResponseConverter(), new \PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\ResponseConverter()],
Contract::create(new AudioNormalizer()),
$contract ?? Contract::create(new AudioNormalizer()),
);
}
}
4 changes: 3 additions & 1 deletion src/Platform/Bridge/Bedrock/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpLlm\LlmChain\Platform\Bridge\Bedrock\Anthropic\ClaudeHandler;
use PhpLlm\LlmChain\Platform\Bridge\Bedrock\Meta\LlamaModelClient;
use PhpLlm\LlmChain\Platform\Bridge\Bedrock\Nova\NovaHandler;
use PhpLlm\LlmChain\Platform\Contract;

/**
* @author Björn Altmann
Expand All @@ -16,11 +17,12 @@
{
public static function create(
BedrockRuntimeClient $bedrockRuntimeClient = new BedrockRuntimeClient(),
?Contract $contract = null,
): Platform {
$modelClient[] = new ClaudeHandler($bedrockRuntimeClient);
$modelClient[] = new NovaHandler($bedrockRuntimeClient);
$modelClient[] = new LlamaModelClient($bedrockRuntimeClient);

return new Platform($modelClient);
return new Platform($modelClient, $contract);
}
}
26 changes: 26 additions & 0 deletions src/Platform/Bridge/Google/Contract/GoogleContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Platform\Bridge\Google\Contract;

use PhpLlm\LlmChain\Platform\Contract;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Denis Zunke <[email protected]>
*/
final readonly class GoogleContract extends Contract
{
public static function create(NormalizerInterface ...$normalizer): Contract
{
return parent::create(
new AssistantMessageNormalizer(),
new MessageBagNormalizer(),
new ToolNormalizer(),
new ToolCallMessageNormalizer(),
new UserMessageNormalizer(),
...$normalizer,
);
}
}
19 changes: 7 additions & 12 deletions src/Platform/Bridge/Google/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

namespace PhpLlm\LlmChain\Platform\Bridge\Google;

use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\AssistantMessageNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\MessageBagNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\ToolCallMessageNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\ToolNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\UserMessageNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\GoogleContract;
use PhpLlm\LlmChain\Platform\Bridge\Google\Embeddings\ModelClient;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\Platform;
Expand All @@ -24,17 +20,16 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
$responseHandler = new ModelHandler($httpClient, $apiKey);
$embeddings = new ModelClient($httpClient, $apiKey);

return new Platform([$responseHandler, $embeddings], [$responseHandler, $embeddings], Contract::create(
new AssistantMessageNormalizer(),
new MessageBagNormalizer(),
new ToolNormalizer(),
new ToolCallMessageNormalizer(),
new UserMessageNormalizer(),
));
return new Platform(
[$responseHandler, $embeddings],
[$responseHandler, $embeddings],
$contract ?? GoogleContract::create(),
);
}
}
3 changes: 2 additions & 1 deletion src/Platform/Bridge/HuggingFace/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ public static function create(
string $apiKey,
string $provider = Provider::HF_INFERENCE,
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);

return new Platform(
[new ModelClient($httpClient, $provider, $apiKey)],
[new ResponseConverter()],
Contract::create(
$contract ?? Contract::create(
new FileNormalizer(),
new MessageBagNormalizer(),
),
Expand Down
3 changes: 2 additions & 1 deletion src/Platform/Bridge/Mistral/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);

return new Platform(
[new EmbeddingsModelClient($httpClient, $apiKey), new MistralModelClient($httpClient, $apiKey)],
[new EmbeddingsResponseConverter(), new MistralResponseConverter()],
Contract::create(new ToolNormalizer()),
$contract ?? Contract::create(new ToolNormalizer()),
);
}
}
4 changes: 3 additions & 1 deletion src/Platform/Bridge/Ollama/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpLlm\LlmChain\Platform\Bridge\Ollama;

use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -16,10 +17,11 @@ final class PlatformFactory
public static function create(
string $hostUrl = 'http://localhost:11434',
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
$handler = new LlamaModelHandler($httpClient, $hostUrl);

return new Platform([$handler], [$handler]);
return new Platform([$handler], [$handler], $contract);
}
}
3 changes: 2 additions & 1 deletion src/Platform/Bridge/OpenAI/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);

Expand All @@ -44,7 +45,7 @@ public static function create(
$dallEModelClient,
new WhisperResponseConverter(),
],
Contract::create(new AudioNormalizer()),
$contract ?? Contract::create(new AudioNormalizer()),
);
}
}
15 changes: 10 additions & 5 deletions src/Platform/Bridge/OpenRouter/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
$handler = new Client($httpClient, $apiKey);

return new Platform([$handler], [$handler], Contract::create(
new AssistantMessageNormalizer(),
new MessageBagNormalizer(),
new UserMessageNormalizer(),
));
return new Platform(
[$handler],
[$handler],
$contract ?? Contract::create(
new AssistantMessageNormalizer(),
new MessageBagNormalizer(),
new UserMessageNormalizer(),
),
);
}
}
3 changes: 2 additions & 1 deletion src/Platform/Bridge/Replicate/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
return new Platform(
[new LlamaModelClient(new Client($httpClient ?? HttpClient::create(), new Clock(), $apiKey))],
[new LlamaResponseConverter()],
Contract::create(new LlamaMessageBagNormalizer()),
$contract ?? Contract::create(new LlamaMessageBagNormalizer()),
);
}
}
4 changes: 3 additions & 1 deletion src/Platform/Bridge/Voyage/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpLlm\LlmChain\Platform\Bridge\Voyage;

use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -17,10 +18,11 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?Contract $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
$handler = new ModelHandler($httpClient, $apiKey);

return new Platform([$handler], [$handler]);
return new Platform([$handler], [$handler], $contract);
}
}
10 changes: 5 additions & 5 deletions src/Platform/Contract.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
/**
* @author Christopher Hertel <[email protected]>
*/
final readonly class Contract
readonly class Contract
{
public const CONTEXT_MODEL = 'model';

public function __construct(
private NormalizerInterface $normalizer,
final public function __construct(
protected NormalizerInterface $normalizer,
) {
}

Expand Down Expand Up @@ -63,7 +63,7 @@ public static function create(NormalizerInterface ...$normalizer): self
*
* @return array<string, mixed>|string
*/
public function createRequestPayload(Model $model, object|array|string $input): string|array
final public function createRequestPayload(Model $model, object|array|string $input): string|array
{
return $this->normalizer->normalize($input, context: [self::CONTEXT_MODEL => $model]);
}
Expand All @@ -73,7 +73,7 @@ public function createRequestPayload(Model $model, object|array|string $input):
*
* @return array<string, mixed>
*/
public function createToolOption(array $tools, Model $model): array
final public function createToolOption(array $tools, Model $model): array
{
return $this->normalizer->normalize($tools, context: [self::CONTEXT_MODEL => $model, AbstractObjectNormalizer::PRESERVE_EMPTY_OBJECTS => true]);
}
Expand Down