Skip to content

Commit b55d08b

Browse files
committed
feat: open contract class to be extendable
1 parent c7b13d4 commit b55d08b

File tree

19 files changed

+69
-123
lines changed

19 files changed

+69
-123
lines changed

src/Platform/Bridge/Anthropic/Contract/AnthropicSet.php renamed to src/Platform/Bridge/Anthropic/Contract/AnthropicContract.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

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

7+
use PhpLlm\LlmChain\Platform\Contract;
78
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
89

910
/**
1011
* @author Denis Zunke <[email protected]>
1112
*/
12-
final readonly class AnthropicSet
13+
final readonly class AnthropicContract extends Contract
1314
{
14-
/** @return array<NormalizerInterface> */
15-
public static function get(): array
15+
public static function create(NormalizerInterface ...$normalizer): Contract
1616
{
17-
return [
17+
return parent::create(
1818
new AssistantMessageNormalizer(),
1919
new DocumentNormalizer(),
2020
new DocumentUrlNormalizer(),
@@ -23,6 +23,7 @@ public static function get(): array
2323
new MessageBagNormalizer(),
2424
new ToolCallMessageNormalizer(),
2525
new ToolNormalizer(),
26-
];
26+
...$normalizer,
27+
);
2728
}
2829
}

src/Platform/Bridge/Anthropic/PlatformFactory.php

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

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

7-
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\AnthropicSet;
7+
use PhpLlm\LlmChain\Platform\Bridge\Anthropic\Contract\AnthropicContract;
88
use PhpLlm\LlmChain\Platform\Contract;
9-
use PhpLlm\LlmChain\Platform\ContractInterface;
109
use PhpLlm\LlmChain\Platform\Platform;
1110
use Symfony\Component\HttpClient\EventSourceHttpClient;
1211
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -21,14 +20,14 @@ public static function create(
2120
string $apiKey,
2221
string $version = '2023-06-01',
2322
?HttpClientInterface $httpClient = null,
24-
?ContractInterface $contract = null,
23+
?Contract $contract = null,
2524
): Platform {
2625
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2726

2827
return new Platform(
2928
[new ModelClient($httpClient, $apiKey, $version)],
3029
[new ResponseConverter()],
31-
$contract ?? Contract::create(...AnthropicSet::get()),
30+
$contract ?? AnthropicContract::create(),
3231
);
3332
}
3433
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

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

7-
use PhpLlm\LlmChain\Platform\ContractInterface;
7+
use PhpLlm\LlmChain\Platform\Contract;
88
use PhpLlm\LlmChain\Platform\Platform;
99
use Symfony\Component\HttpClient\HttpClient;
1010
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -19,7 +19,7 @@ public static function create(
1919
#[\SensitiveParameter]
2020
string $apiKey,
2121
?HttpClientInterface $httpClient = null,
22-
?ContractInterface $contract = null,
22+
?Contract $contract = null,
2323
): Platform {
2424
$modelClient = new LlamaHandler($httpClient ?? HttpClient::create(), $baseUrl, $apiKey);
2525

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
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;
1211
use PhpLlm\LlmChain\Platform\Platform;
1312
use Symfony\Component\HttpClient\EventSourceHttpClient;
1413
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -25,7 +24,7 @@ public static function create(
2524
#[\SensitiveParameter]
2625
string $apiKey,
2726
?HttpClientInterface $httpClient = null,
28-
?ContractInterface $contract = null,
27+
?Contract $contract = null,
2928
): Platform {
3029
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
3130
$embeddingsResponseFactory = new EmbeddingsModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey);

src/Platform/Bridge/Bedrock/Platform.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
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;
109
use PhpLlm\LlmChain\Platform\Exception\RuntimeException;
1110
use PhpLlm\LlmChain\Platform\Model;
1211
use PhpLlm\LlmChain\Platform\PlatformInterface;
@@ -27,7 +26,7 @@ class Platform implements PlatformInterface
2726
*/
2827
public function __construct(
2928
iterable $modelClients,
30-
private ?ContractInterface $contract = null,
29+
private ?Contract $contract = null,
3130
) {
3231
$this->contract = $contract ?? Contract::create(
3332
new AnthropicContract\AssistantMessageNormalizer(),

src/Platform/Bridge/Bedrock/PlatformFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +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;
11+
use PhpLlm\LlmChain\Platform\Contract;
1212

1313
/**
1414
* @author Björn Altmann
@@ -17,7 +17,7 @@
1717
{
1818
public static function create(
1919
BedrockRuntimeClient $bedrockRuntimeClient = new BedrockRuntimeClient(),
20-
?ContractInterface $contract = null,
20+
?Contract $contract = null,
2121
): Platform {
2222
$modelClient[] = new ClaudeHandler($bedrockRuntimeClient);
2323
$modelClient[] = new NovaHandler($bedrockRuntimeClient);

src/Platform/Bridge/Google/Contract/GoogleSet.php renamed to src/Platform/Bridge/Google/Contract/GoogleContract.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,23 @@
44

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

7+
use PhpLlm\LlmChain\Platform\Contract;
78
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
89

910
/**
1011
* @author Denis Zunke <[email protected]>
1112
*/
12-
final readonly class GoogleSet
13+
final readonly class GoogleContract extends Contract
1314
{
14-
/** @return array<NormalizerInterface> */
15-
public static function get(): array
15+
public static function create(NormalizerInterface ...$normalizer): Contract
1616
{
17-
return [
17+
return parent::create(
1818
new AssistantMessageNormalizer(),
1919
new MessageBagNormalizer(),
2020
new ToolNormalizer(),
2121
new ToolCallMessageNormalizer(),
2222
new UserMessageNormalizer(),
23-
];
23+
...$normalizer,
24+
);
2425
}
2526
}

src/Platform/Bridge/Google/PlatformFactory.php

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

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

7-
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\GoogleSet;
7+
use PhpLlm\LlmChain\Platform\Bridge\Google\Contract\GoogleContract;
88
use PhpLlm\LlmChain\Platform\Bridge\Google\Embeddings\ModelClient;
99
use PhpLlm\LlmChain\Platform\Contract;
10-
use PhpLlm\LlmChain\Platform\ContractInterface;
1110
use PhpLlm\LlmChain\Platform\Platform;
1211
use Symfony\Component\HttpClient\EventSourceHttpClient;
1312
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -21,7 +20,7 @@ public static function create(
2120
#[\SensitiveParameter]
2221
string $apiKey,
2322
?HttpClientInterface $httpClient = null,
24-
?ContractInterface $contract = null,
23+
?Contract $contract = null,
2524
): Platform {
2625
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2726
$responseHandler = new ModelHandler($httpClient, $apiKey);
@@ -30,7 +29,7 @@ public static function create(
3029
return new Platform(
3130
[$responseHandler, $embeddings],
3231
[$responseHandler, $embeddings],
33-
$contract ?? Contract::create(...GoogleSet::get()),
32+
$contract ?? GoogleContract::create(),
3433
);
3534
}
3635
}

src/Platform/Bridge/HuggingFace/PlatformFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
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;
1110
use PhpLlm\LlmChain\Platform\Platform;
1211
use Symfony\Component\HttpClient\EventSourceHttpClient;
1312
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -22,7 +21,7 @@ public static function create(
2221
string $apiKey,
2322
string $provider = Provider::HF_INFERENCE,
2423
?HttpClientInterface $httpClient = null,
25-
?ContractInterface $contract = null,
24+
?Contract $contract = null,
2625
): Platform {
2726
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2827

src/Platform/Bridge/Mistral/PlatformFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
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;
1413
use PhpLlm\LlmChain\Platform\Platform;
1514
use Symfony\Component\HttpClient\EventSourceHttpClient;
1615
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -24,7 +23,7 @@ public static function create(
2423
#[\SensitiveParameter]
2524
string $apiKey,
2625
?HttpClientInterface $httpClient = null,
27-
?ContractInterface $contract = null,
26+
?Contract $contract = null,
2827
): Platform {
2928
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
3029

0 commit comments

Comments
 (0)