Skip to content

Commit

Permalink
refactor: introduce PlatformInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
chr-hertel committed Nov 23, 2024
1 parent 2687a43 commit 37ce995
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/Bridge/OpenAI/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace PhpLlm\LlmChain\Bridge\OpenAI;

use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings\ModelClient;
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings\ResponseConverter;
use PhpLlm\LlmChain\Bridge\OpenAI\GPT\ModelClient as GPTResponseFactory;
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings\ModelClient as EmbeddingsModelClient;
use PhpLlm\LlmChain\Bridge\OpenAI\Embeddings\ResponseConverter as EmbeddingsResponseConverter;
use PhpLlm\LlmChain\Bridge\OpenAI\GPT\ModelClient as GPTModelClient;
use PhpLlm\LlmChain\Bridge\OpenAI\GPT\ResponseConverter as GPTResponseConverter;
use PhpLlm\LlmChain\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
Expand All @@ -18,8 +18,8 @@ public static function create(#[\SensitiveParameter] string $apiKey): Platform
$httpClient = new EventSourceHttpClient();

return new Platform(
[new GPTResponseFactory($httpClient, $apiKey), new ModelClient($httpClient, $apiKey)],
[new GPTResponseConverter(), new ResponseConverter()],
[new GPTModelClient($httpClient, $apiKey), new EmbeddingsModelClient($httpClient, $apiKey)],
[new GPTResponseConverter(), new EmbeddingsResponseConverter()],
);
}
}
2 changes: 1 addition & 1 deletion src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
* @param OutputProcessor[] $outputProcessor
*/
public function __construct(
private Platform $platform,
private PlatformInterface $platform,
private LanguageModel $llm,
iterable $inputProcessor = [],
iterable $outputProcessor = [],
Expand Down
4 changes: 2 additions & 2 deletions src/Chain/ToolBox/Tool/SimilaritySearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use PhpLlm\LlmChain\Document\Vector;
use PhpLlm\LlmChain\Document\VectorDocument;
use PhpLlm\LlmChain\Model\EmbeddingsModel;
use PhpLlm\LlmChain\Platform;
use PhpLlm\LlmChain\PlatformInterface;
use PhpLlm\LlmChain\Store\VectorStoreInterface;

#[AsTool('similarity_search', description: 'Searches for documents similar to a query or sentence.')]
Expand All @@ -20,7 +20,7 @@ final class SimilaritySearch
public array $usedDocuments = [];

public function __construct(
private readonly Platform $platform,
private readonly PlatformInterface $platform,
private readonly EmbeddingsModel $embeddings,
private readonly VectorStoreInterface $vectorStore,
) {
Expand Down
2 changes: 1 addition & 1 deletion src/Embedder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
private ClockInterface $clock;

public function __construct(
private Platform $platform,
private PlatformInterface $platform,
private EmbeddingsModel $embeddings,
private StoreInterface $store,
?ClockInterface $clock = null,
Expand Down
6 changes: 1 addition & 5 deletions src/Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse;

final readonly class Platform
final readonly class Platform implements PlatformInterface
{
/**
* @var ModelClient[]
Expand All @@ -37,10 +37,6 @@ public function __construct(iterable $modelClients, iterable $responseConverter)
$this->responseConverter = $responseConverter instanceof \Traversable ? iterator_to_array($responseConverter) : $responseConverter;
}

/**
* @param array<mixed>|string|object $input
* @param array<string, mixed> $options
*/
public function request(Model $model, array|string|object $input, array $options = []): ResponseInterface
{
$options = array_merge($model->getOptions(), $options);
Expand Down
17 changes: 17 additions & 0 deletions src/PlatformInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain;

use PhpLlm\LlmChain\Model\Model;
use PhpLlm\LlmChain\Model\Response\ResponseInterface;

interface PlatformInterface
{
/**
* @param array<mixed>|string|object $input
* @param array<string, mixed> $options
*/
public function request(Model $model, array|string|object $input, array $options = []): ResponseInterface;
}

0 comments on commit 37ce995

Please sign in to comment.