Skip to content

Commit

Permalink
fix: make it possible to inject http client into factories
Browse files Browse the repository at this point in the history
  • Loading branch information
chr-hertel committed Dec 8, 2024
1 parent 65ba766 commit 7d8613f
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
12 changes: 9 additions & 3 deletions src/Bridge/Anthropic/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Bridge/Azure/OpenAI/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@
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
{
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);

Expand Down
12 changes: 8 additions & 4 deletions src/Bridge/Ollama/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
10 changes: 7 additions & 3 deletions src/Bridge/OpenAI/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand Down
11 changes: 9 additions & 2 deletions src/Bridge/Replicate/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand Down
13 changes: 9 additions & 4 deletions src/Bridge/Voyage/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down

0 comments on commit 7d8613f

Please sign in to comment.