From 37ce995da994f32e8e210fecfa0b522d7e2df9da Mon Sep 17 00:00:00 2001 From: Christopher Hertel Date: Sat, 23 Nov 2024 17:16:52 +0100 Subject: [PATCH] refactor: introduce PlatformInterface --- src/Bridge/OpenAI/PlatformFactory.php | 10 +++++----- src/Chain.php | 2 +- src/Chain/ToolBox/Tool/SimilaritySearch.php | 4 ++-- src/Embedder.php | 2 +- src/Platform.php | 6 +----- src/PlatformInterface.php | 17 +++++++++++++++++ 6 files changed, 27 insertions(+), 14 deletions(-) create mode 100644 src/PlatformInterface.php diff --git a/src/Bridge/OpenAI/PlatformFactory.php b/src/Bridge/OpenAI/PlatformFactory.php index d09e1f11..0d62fdff 100644 --- a/src/Bridge/OpenAI/PlatformFactory.php +++ b/src/Bridge/OpenAI/PlatformFactory.php @@ -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; @@ -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()], ); } } diff --git a/src/Chain.php b/src/Chain.php index 0b353ffc..f649218e 100644 --- a/src/Chain.php +++ b/src/Chain.php @@ -33,7 +33,7 @@ * @param OutputProcessor[] $outputProcessor */ public function __construct( - private Platform $platform, + private PlatformInterface $platform, private LanguageModel $llm, iterable $inputProcessor = [], iterable $outputProcessor = [], diff --git a/src/Chain/ToolBox/Tool/SimilaritySearch.php b/src/Chain/ToolBox/Tool/SimilaritySearch.php index 880b9b9d..2a673f5d 100644 --- a/src/Chain/ToolBox/Tool/SimilaritySearch.php +++ b/src/Chain/ToolBox/Tool/SimilaritySearch.php @@ -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.')] @@ -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, ) { diff --git a/src/Embedder.php b/src/Embedder.php index 814299c2..6e95fa58 100644 --- a/src/Embedder.php +++ b/src/Embedder.php @@ -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, diff --git a/src/Platform.php b/src/Platform.php index 68ae14cd..0b3fe996 100644 --- a/src/Platform.php +++ b/src/Platform.php @@ -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[] @@ -37,10 +37,6 @@ public function __construct(iterable $modelClients, iterable $responseConverter) $this->responseConverter = $responseConverter instanceof \Traversable ? iterator_to_array($responseConverter) : $responseConverter; } - /** - * @param array|string|object $input - * @param array $options - */ public function request(Model $model, array|string|object $input, array $options = []): ResponseInterface { $options = array_merge($model->getOptions(), $options); diff --git a/src/PlatformInterface.php b/src/PlatformInterface.php new file mode 100644 index 00000000..e99ee73f --- /dev/null +++ b/src/PlatformInterface.php @@ -0,0 +1,17 @@ +|string|object $input + * @param array $options + */ + public function request(Model $model, array|string|object $input, array $options = []): ResponseInterface; +}