Skip to content
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

fix: make it possible to inject http client into factories #155

Merged
merged 1 commit into from
Dec 8, 2024
Merged
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
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(
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not using a withHttpClient() method?

Copy link
Member Author

Choose a reason for hiding this comment

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

As a separate method? What's the benefit?

Copy link
Contributor

Choose a reason for hiding this comment

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

You can instantiate it and change it later, maybe because you want to inject a traceable http client later + you could return a clone to stay immutable, but just an idea and maybe not needed

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, that was basically the motivation here - to use the factory in the bundle but use the traceable one instantiated by the framework already

#[\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
Loading