Skip to content

feat: add ContractInterface 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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions src/Platform/Bridge/Anthropic/Contract/AnthropicSet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

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

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Denis Zunke <[email protected]>
*/
final readonly class AnthropicSet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final readonly class AnthropicSet
final readonly class AnthropicDefaultContract
Suggested change
final readonly class AnthropicSet
final readonly class AnthropicContract

?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah it is not a contract yet, hmm 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. I have seen that there are Contract Builds that combine mixed model contract normalizers so my thought here was to give them as a "set" like rector is doing it for some tools. So you could combine the set. Maybe this is too much? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔

{
/** @return array<NormalizerInterface> */
public static function get(): array
{
return [
new AssistantMessageNormalizer(),
new DocumentNormalizer(),
new DocumentUrlNormalizer(),
new ImageNormalizer(),
new ImageUrlNormalizer(),
new MessageBagNormalizer(),
new ToolCallMessageNormalizer(),
new ToolNormalizer(),
];
}
}
22 changes: 4 additions & 18 deletions src/Platform/Bridge/Anthropic/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

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\AnthropicSet;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\ContractInterface;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -27,22 +21,14 @@ public static function create(
string $apiKey,
string $version = '2023-06-01',
?HttpClientInterface $httpClient = null,
?ContractInterface $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 ?? Contract::create(...AnthropicSet::get()),
);
}
}
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\ContractInterface;
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,
?ContractInterface $contract = null,
): Platform {
$modelClient = new LlamaHandler($httpClient ?? HttpClient::create(), $baseUrl, $apiKey);

return new Platform([$modelClient], [$modelClient]);
return new Platform([$modelClient], [$modelClient], $contract);
}
}
4 changes: 3 additions & 1 deletion src/Platform/Bridge/Azure/OpenAI/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\GPT\ResponseConverter;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\AudioNormalizer;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\ContractInterface;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -24,6 +25,7 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?ContractInterface $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
$embeddingsResponseFactory = new EmbeddingsModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey);
Expand All @@ -33,7 +35,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()),
);
}
}
3 changes: 2 additions & 1 deletion src/Platform/Bridge/Bedrock/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PhpLlm\LlmChain\Platform\Bridge\Bedrock\Nova\Contract as NovaContract;
use PhpLlm\LlmChain\Platform\Bridge\Meta\Contract as LlamaContract;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\ContractInterface;
use PhpLlm\LlmChain\Platform\Exception\RuntimeException;
use PhpLlm\LlmChain\Platform\Model;
use PhpLlm\LlmChain\Platform\PlatformInterface;
Expand All @@ -26,7 +27,7 @@ class Platform implements PlatformInterface
*/
public function __construct(
iterable $modelClients,
private ?Contract $contract = null,
private ?ContractInterface $contract = null,
) {
$this->contract = $contract ?? Contract::create(
new AnthropicContract\AssistantMessageNormalizer(),
Expand Down
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\ContractInterface;

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

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

declare(strict_types=1);

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

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* @author Denis Zunke <[email protected]>
*/
final readonly class GoogleSet
{
/** @return array<NormalizerInterface> */
public static function get(): array
{
return [
new AssistantMessageNormalizer(),
new MessageBagNormalizer(),
new ToolNormalizer(),
new ToolCallMessageNormalizer(),
new UserMessageNormalizer(),
];
}
}
20 changes: 8 additions & 12 deletions src/Platform/Bridge/Google/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@

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\GoogleSet;
use PhpLlm\LlmChain\Platform\Bridge\Google\Embeddings\ModelClient;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\ContractInterface;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -24,17 +21,16 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?ContractInterface $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 ?? Contract::create(...GoogleSet::get()),
);
}
}
4 changes: 3 additions & 1 deletion src/Platform/Bridge/HuggingFace/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpLlm\LlmChain\Platform\Bridge\HuggingFace\Contract\FileNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\HuggingFace\Contract\MessageBagNormalizer;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\ContractInterface;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -21,13 +22,14 @@ public static function create(
string $apiKey,
string $provider = Provider::HF_INFERENCE,
?HttpClientInterface $httpClient = null,
?ContractInterface $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
4 changes: 3 additions & 1 deletion src/Platform/Bridge/Mistral/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpLlm\LlmChain\Platform\Bridge\Mistral\Llm\ModelClient as MistralModelClient;
use PhpLlm\LlmChain\Platform\Bridge\Mistral\Llm\ResponseConverter as MistralResponseConverter;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\ContractInterface;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -23,13 +24,14 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?ContractInterface $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\ContractInterface;
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,
?ContractInterface $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);
}
}
4 changes: 3 additions & 1 deletion src/Platform/Bridge/OpenAI/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\ModelClient as WhisperModelClient;
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\Whisper\ResponseConverter as WhisperResponseConverter;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\ContractInterface;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -26,6 +27,7 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?ContractInterface $contract = null,
): Platform {
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);

Expand All @@ -44,7 +46,7 @@ public static function create(
$dallEModelClient,
new WhisperResponseConverter(),
],
Contract::create(new AudioNormalizer()),
$contract ?? Contract::create(new AudioNormalizer()),
);
}
}
16 changes: 11 additions & 5 deletions src/Platform/Bridge/OpenRouter/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\MessageBagNormalizer;
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\UserMessageNormalizer;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\ContractInterface;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -21,14 +22,19 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?ContractInterface $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(),
),
);
}
}
4 changes: 3 additions & 1 deletion src/Platform/Bridge/Replicate/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpLlm\LlmChain\Platform\Bridge\Replicate\Contract\LlamaMessageBagNormalizer;
use PhpLlm\LlmChain\Platform\Contract;
use PhpLlm\LlmChain\Platform\ContractInterface;
use PhpLlm\LlmChain\Platform\Platform;
use Symfony\Component\Clock\Clock;
use Symfony\Component\HttpClient\HttpClient;
Expand All @@ -20,11 +21,12 @@ public static function create(
#[\SensitiveParameter]
string $apiKey,
?HttpClientInterface $httpClient = null,
?ContractInterface $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\ContractInterface;
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,
?ContractInterface $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);
}
}
Loading