Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

Commit 8b88d5d

Browse files
authored
fix: make it possible to inject http client into factories (#155)
1 parent 65ba766 commit 8b88d5d

File tree

6 files changed

+47
-18
lines changed

6 files changed

+47
-18
lines changed

src/Bridge/Anthropic/PlatformFactory.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,18 @@
66

77
use PhpLlm\LlmChain\Platform;
88
use Symfony\Component\HttpClient\EventSourceHttpClient;
9+
use Symfony\Contracts\HttpClient\HttpClientInterface;
910

1011
final readonly class PlatformFactory
1112
{
12-
public static function create(#[\SensitiveParameter] string $apiKey, string $version = '2023-06-01'): Platform
13-
{
14-
$responseHandler = new ModelHandler(new EventSourceHttpClient(), $apiKey, $version);
13+
public static function create(
14+
#[\SensitiveParameter]
15+
string $apiKey,
16+
string $version = '2023-06-01',
17+
?HttpClientInterface $httpClient = null,
18+
): Platform {
19+
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
20+
$responseHandler = new ModelHandler($httpClient, $apiKey, $version);
1521

1622
return new Platform([$responseHandler], [$responseHandler]);
1723
}

src/Bridge/Azure/OpenAI/PlatformFactory.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
use PhpLlm\LlmChain\Bridge\OpenAI\GPT\ResponseConverter;
99
use PhpLlm\LlmChain\Platform;
1010
use Symfony\Component\HttpClient\EventSourceHttpClient;
11+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1112

1213
final readonly class PlatformFactory
1314
{
1415
public static function create(
1516
string $baseUrl,
1617
string $deployment,
1718
string $apiVersion,
18-
#[\SensitiveParameter] string $apiKey,
19+
#[\SensitiveParameter]
20+
string $apiKey,
21+
?HttpClientInterface $httpClient = null,
1922
): Platform {
20-
$httpClient = new EventSourceHttpClient();
23+
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
2124
$embeddingsResponseFactory = new EmbeddingsModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey);
2225
$GPTResponseFactory = new GPTModelClient($httpClient, $baseUrl, $deployment, $apiVersion, $apiKey);
2326

src/Bridge/Ollama/PlatformFactory.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@
55
namespace PhpLlm\LlmChain\Bridge\Ollama;
66

77
use PhpLlm\LlmChain\Platform;
8-
use Symfony\Component\HttpClient\HttpClient;
8+
use Symfony\Component\HttpClient\EventSourceHttpClient;
9+
use Symfony\Contracts\HttpClient\HttpClientInterface;
910

1011
final class PlatformFactory
1112
{
12-
public static function create(string $hostUrl = 'http://localhost:11434'): Platform
13-
{
14-
$handler = new LlamaModelHandler(HttpClient::create(), $hostUrl);
13+
public static function create(
14+
string $hostUrl = 'http://localhost:11434',
15+
?HttpClientInterface $httpClient = null,
16+
): Platform {
17+
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
18+
$handler = new LlamaModelHandler($httpClient, $hostUrl);
1519

1620
return new Platform([$handler], [$handler]);
1721
}

src/Bridge/OpenAI/PlatformFactory.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
use PhpLlm\LlmChain\Bridge\OpenAI\GPT\ResponseConverter as GPTResponseConverter;
1111
use PhpLlm\LlmChain\Platform;
1212
use Symfony\Component\HttpClient\EventSourceHttpClient;
13+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1314

1415
final readonly class PlatformFactory
1516
{
16-
public static function create(#[\SensitiveParameter] string $apiKey): Platform
17-
{
18-
$httpClient = new EventSourceHttpClient();
17+
public static function create(
18+
#[\SensitiveParameter]
19+
string $apiKey,
20+
?HttpClientInterface $httpClient = null,
21+
): Platform {
22+
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
1923

2024
return new Platform(
2125
[new GPTModelClient($httpClient, $apiKey), new EmbeddingsModelClient($httpClient, $apiKey)],

src/Bridge/Replicate/PlatformFactory.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
use PhpLlm\LlmChain\Bridge\Meta\LlamaPromptConverter;
88
use PhpLlm\LlmChain\Platform;
99
use Symfony\Component\Clock\Clock;
10+
use Symfony\Component\HttpClient\EventSourceHttpClient;
1011
use Symfony\Component\HttpClient\HttpClient;
12+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1113

1214
final class PlatformFactory
1315
{
14-
public static function create(string $apiKey): Platform
15-
{
16+
public static function create(
17+
#[\SensitiveParameter]
18+
string $apiKey,
19+
?HttpClientInterface $httpClient = null,
20+
): Platform {
21+
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
22+
1623
return new Platform(
1724
[new LlamaModelClient(new Client(HttpClient::create(), new Clock(), $apiKey), new LlamaPromptConverter())],
1825
[new LlamaResponseConverter()],

src/Bridge/Voyage/PlatformFactory.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@
55
namespace PhpLlm\LlmChain\Bridge\Voyage;
66

77
use PhpLlm\LlmChain\Platform;
8-
use Symfony\Component\HttpClient\HttpClient;
8+
use Symfony\Component\HttpClient\EventSourceHttpClient;
9+
use Symfony\Contracts\HttpClient\HttpClientInterface;
910

1011
final class PlatformFactory
1112
{
12-
public static function create(string $apiKey): Platform
13-
{
14-
$handler = new ModelHandler(HttpClient::create(), $apiKey);
13+
public static function create(
14+
#[\SensitiveParameter]
15+
string $apiKey,
16+
?HttpClientInterface $httpClient = null,
17+
): Platform {
18+
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
19+
$handler = new ModelHandler($httpClient, $apiKey);
1520

1621
return new Platform([$handler], [$handler]);
1722
}

0 commit comments

Comments
 (0)