From 7d8613f412b7123040ec57fe7564fc072877b4b2 Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Sun, 8 Dec 2024 01:48:34 +0100 Subject: [PATCH] fix: make it possible to inject http client into factories --- src/Bridge/Anthropic/PlatformFactory.php | 12 +++++++++--- src/Bridge/Azure/OpenAI/PlatformFactory.php | 7 +++++-- src/Bridge/Ollama/PlatformFactory.php | 12 ++++++++---- src/Bridge/OpenAI/PlatformFactory.php | 10 +++++++--- src/Bridge/Replicate/PlatformFactory.php | 11 +++++++++-- src/Bridge/Voyage/PlatformFactory.php | 13 +++++++++---- 6 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/Bridge/Anthropic/PlatformFactory.php b/src/Bridge/Anthropic/PlatformFactory.php index d22f3bc8..644c462a 100644 --- a/src/Bridge/Anthropic/PlatformFactory.php +++ b/src/Bridge/Anthropic/PlatformFactory.php @@ -6,12 +6,18 @@ use PhpLlm\LlmChain\Platform; use Symfony\Component\HttpClient\EventSourceHttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; final readonly class PlatformFactory { - public static function create(#[\SensitiveParameter] string $apiKey, string $version = '2023-06-01'): Platform - { - $responseHandler = new ModelHandler(new EventSourceHttpClient(), $apiKey, $version); + public static function create( + #[\SensitiveParameter] + string $apiKey, + string $version = '2023-06-01', + ?HttpClientInterface $httpClient = null, + ): Platform { + $httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient); + $responseHandler = new ModelHandler($httpClient, $apiKey, $version); return new Platform([$responseHandler], [$responseHandler]); } diff --git a/src/Bridge/Azure/OpenAI/PlatformFactory.php b/src/Bridge/Azure/OpenAI/PlatformFactory.php index 1cda68f1..13215171 100644 --- a/src/Bridge/Azure/OpenAI/PlatformFactory.php +++ b/src/Bridge/Azure/OpenAI/PlatformFactory.php @@ -8,6 +8,7 @@ use PhpLlm\LlmChain\Bridge\OpenAI\GPT\ResponseConverter; use PhpLlm\LlmChain\Platform; use Symfony\Component\HttpClient\EventSourceHttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; final readonly class PlatformFactory { @@ -15,9 +16,11 @@ public static function create( string $baseUrl, string $deployment, string $apiVersion, - #[\SensitiveParameter] string $apiKey, + #[\SensitiveParameter] + string $apiKey, + ?HttpClientInterface $httpClient = null, ): Platform { - $httpClient = new EventSourceHttpClient(); + $httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient); $embeddingsResponseFactory = new EmbeddingsModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey); $GPTResponseFactory = new GPTModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey); diff --git a/src/Bridge/Ollama/PlatformFactory.php b/src/Bridge/Ollama/PlatformFactory.php index 472ef75e..00c663e4 100644 --- a/src/Bridge/Ollama/PlatformFactory.php +++ b/src/Bridge/Ollama/PlatformFactory.php @@ -5,13 +5,17 @@ namespace PhpLlm\LlmChain\Bridge\Ollama; use PhpLlm\LlmChain\Platform; -use Symfony\Component\HttpClient\HttpClient; +use Symfony\Component\HttpClient\EventSourceHttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; final class PlatformFactory { - public static function create(string $hostUrl = 'http://localhost:11434'): Platform - { - $handler = new LlamaModelHandler(HttpClient::create(), $hostUrl); + public static function create( + string $hostUrl = 'http://localhost:11434', + ?HttpClientInterface $httpClient = null, + ): Platform { + $httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient); + $handler = new LlamaModelHandler($httpClient, $hostUrl); return new Platform([$handler], [$handler]); } diff --git a/src/Bridge/OpenAI/PlatformFactory.php b/src/Bridge/OpenAI/PlatformFactory.php index 0d62fdff..5699953b 100644 --- a/src/Bridge/OpenAI/PlatformFactory.php +++ b/src/Bridge/OpenAI/PlatformFactory.php @@ -10,12 +10,16 @@ use PhpLlm\LlmChain\Bridge\OpenAI\GPT\ResponseConverter as GPTResponseConverter; use PhpLlm\LlmChain\Platform; use Symfony\Component\HttpClient\EventSourceHttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; final readonly class PlatformFactory { - public static function create(#[\SensitiveParameter] string $apiKey): Platform - { - $httpClient = new EventSourceHttpClient(); + public static function create( + #[\SensitiveParameter] + string $apiKey, + ?HttpClientInterface $httpClient = null, + ): Platform { + $httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient); return new Platform( [new GPTModelClient($httpClient, $apiKey), new EmbeddingsModelClient($httpClient, $apiKey)], diff --git a/src/Bridge/Replicate/PlatformFactory.php b/src/Bridge/Replicate/PlatformFactory.php index e5323e5c..b5e9092d 100644 --- a/src/Bridge/Replicate/PlatformFactory.php +++ b/src/Bridge/Replicate/PlatformFactory.php @@ -7,12 +7,19 @@ use PhpLlm\LlmChain\Bridge\Meta\LlamaPromptConverter; use PhpLlm\LlmChain\Platform; use Symfony\Component\Clock\Clock; +use Symfony\Component\HttpClient\EventSourceHttpClient; use Symfony\Component\HttpClient\HttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; final class PlatformFactory { - public static function create(string $apiKey): Platform - { + public static function create( + #[\SensitiveParameter] + string $apiKey, + ?HttpClientInterface $httpClient = null, + ): Platform { + $httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient); + return new Platform( [new LlamaModelClient(new Client(HttpClient::create(), new Clock(), $apiKey), new LlamaPromptConverter())], [new LlamaResponseConverter()], diff --git a/src/Bridge/Voyage/PlatformFactory.php b/src/Bridge/Voyage/PlatformFactory.php index c3ef9fb0..1f8a4641 100644 --- a/src/Bridge/Voyage/PlatformFactory.php +++ b/src/Bridge/Voyage/PlatformFactory.php @@ -5,13 +5,18 @@ namespace PhpLlm\LlmChain\Bridge\Voyage; use PhpLlm\LlmChain\Platform; -use Symfony\Component\HttpClient\HttpClient; +use Symfony\Component\HttpClient\EventSourceHttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; final class PlatformFactory { - public static function create(string $apiKey): Platform - { - $handler = new ModelHandler(HttpClient::create(), $apiKey); + public static function create( + #[\SensitiveParameter] + string $apiKey, + ?HttpClientInterface $httpClient = null, + ): Platform { + $httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient); + $handler = new ModelHandler($httpClient, $apiKey); return new Platform([$handler], [$handler]); }