From 8aad98dcda951e03549e7b93925af43c4cf0f6ea Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Fri, 20 Dec 2024 11:11:53 +0100 Subject: [PATCH] Enhancement: Add `MessageBagInterface` --- src/Bridge/Anthropic/ModelHandler.php | 6 ++--- src/Bridge/Meta/LlamaPromptConverter.php | 4 +-- src/Bridge/Ollama/LlamaModelHandler.php | 4 +-- src/Bridge/Replicate/LlamaModelClient.php | 6 ++--- .../Replicate/LlamaResponseConverter.php | 4 +-- src/Chain.php | 4 +-- src/Chain/Input.php | 4 +-- src/Chain/Output.php | 4 +-- src/ChainInterface.php | 4 +-- src/Model/Message/MessageBag.php | 8 +++--- src/Model/Message/MessageBagInterface.php | 27 +++++++++++++++++++ 11 files changed, 51 insertions(+), 24 deletions(-) create mode 100644 src/Model/Message/MessageBagInterface.php diff --git a/src/Bridge/Anthropic/ModelHandler.php b/src/Bridge/Anthropic/ModelHandler.php index b4f9f782..f223b34e 100644 --- a/src/Bridge/Anthropic/ModelHandler.php +++ b/src/Bridge/Anthropic/ModelHandler.php @@ -5,7 +5,7 @@ namespace PhpLlm\LlmChain\Bridge\Anthropic; use PhpLlm\LlmChain\Exception\RuntimeException; -use PhpLlm\LlmChain\Model\Message\MessageBag; +use PhpLlm\LlmChain\Model\Message\MessageBagInterface; use PhpLlm\LlmChain\Model\Model; use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; use PhpLlm\LlmChain\Model\Response\StreamResponse; @@ -33,12 +33,12 @@ public function __construct( public function supports(Model $model, array|string|object $input): bool { - return $model instanceof Claude && $input instanceof MessageBag; + return $model instanceof Claude && $input instanceof MessageBagInterface; } public function request(Model $model, object|array|string $input, array $options = []): ResponseInterface { - Assert::isInstanceOf($input, MessageBag::class); + Assert::isInstanceOf($input, MessageBagInterface::class); $system = $input->getSystemMessage(); $body = array_merge($options, [ diff --git a/src/Bridge/Meta/LlamaPromptConverter.php b/src/Bridge/Meta/LlamaPromptConverter.php index 389c9d30..b872da7c 100644 --- a/src/Bridge/Meta/LlamaPromptConverter.php +++ b/src/Bridge/Meta/LlamaPromptConverter.php @@ -8,13 +8,13 @@ use PhpLlm\LlmChain\Model\Message\AssistantMessage; use PhpLlm\LlmChain\Model\Message\Content\Image; use PhpLlm\LlmChain\Model\Message\Content\Text; -use PhpLlm\LlmChain\Model\Message\MessageBag; +use PhpLlm\LlmChain\Model\Message\MessageBagInterface; use PhpLlm\LlmChain\Model\Message\SystemMessage; use PhpLlm\LlmChain\Model\Message\UserMessage; final class LlamaPromptConverter { - public function convertToPrompt(MessageBag $messageBag): string + public function convertToPrompt(MessageBagInterface $messageBag): string { $messages = []; diff --git a/src/Bridge/Ollama/LlamaModelHandler.php b/src/Bridge/Ollama/LlamaModelHandler.php index bf0995ca..ba935998 100644 --- a/src/Bridge/Ollama/LlamaModelHandler.php +++ b/src/Bridge/Ollama/LlamaModelHandler.php @@ -6,7 +6,7 @@ use PhpLlm\LlmChain\Bridge\Meta\Llama; use PhpLlm\LlmChain\Exception\RuntimeException; -use PhpLlm\LlmChain\Model\Message\MessageBag; +use PhpLlm\LlmChain\Model\Message\MessageBagInterface; use PhpLlm\LlmChain\Model\Model; use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; use PhpLlm\LlmChain\Model\Response\TextResponse; @@ -25,7 +25,7 @@ public function __construct( public function supports(Model $model, object|array|string $input): bool { - return $model instanceof Llama && $input instanceof MessageBag; + return $model instanceof Llama && $input instanceof MessageBagInterface; } public function request(Model $model, object|array|string $input, array $options = []): ResponseInterface diff --git a/src/Bridge/Replicate/LlamaModelClient.php b/src/Bridge/Replicate/LlamaModelClient.php index 4a779fa0..5747fea9 100644 --- a/src/Bridge/Replicate/LlamaModelClient.php +++ b/src/Bridge/Replicate/LlamaModelClient.php @@ -6,7 +6,7 @@ use PhpLlm\LlmChain\Bridge\Meta\Llama; use PhpLlm\LlmChain\Bridge\Meta\LlamaPromptConverter; -use PhpLlm\LlmChain\Model\Message\MessageBag; +use PhpLlm\LlmChain\Model\Message\MessageBagInterface; use PhpLlm\LlmChain\Model\Message\SystemMessage; use PhpLlm\LlmChain\Model\Model; use PhpLlm\LlmChain\Platform\ModelClient; @@ -23,13 +23,13 @@ public function __construct( public function supports(Model $model, object|array|string $input): bool { - return $model instanceof Llama && $input instanceof MessageBag; + return $model instanceof Llama && $input instanceof MessageBagInterface; } public function request(Model $model, object|array|string $input, array $options = []): ResponseInterface { Assert::isInstanceOf($model, Llama::class); - Assert::isInstanceOf($input, MessageBag::class); + Assert::isInstanceOf($input, MessageBagInterface::class); return $this->client->request(sprintf('meta/meta-%s', $model->getVersion()), 'predictions', [ 'system' => $this->promptConverter->convertMessage($input->getSystemMessage() ?? new SystemMessage('')), diff --git a/src/Bridge/Replicate/LlamaResponseConverter.php b/src/Bridge/Replicate/LlamaResponseConverter.php index 62a5ad89..b9eca166 100644 --- a/src/Bridge/Replicate/LlamaResponseConverter.php +++ b/src/Bridge/Replicate/LlamaResponseConverter.php @@ -6,7 +6,7 @@ use PhpLlm\LlmChain\Bridge\Meta\Llama; use PhpLlm\LlmChain\Exception\RuntimeException; -use PhpLlm\LlmChain\Model\Message\MessageBag; +use PhpLlm\LlmChain\Model\Message\MessageBagInterface; use PhpLlm\LlmChain\Model\Model; use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; use PhpLlm\LlmChain\Model\Response\TextResponse; @@ -17,7 +17,7 @@ { public function supports(Model $model, object|array|string $input): bool { - return $model instanceof Llama && $input instanceof MessageBag; + return $model instanceof Llama && $input instanceof MessageBagInterface; } public function convert(HttpResponse $response, array $options = []): LlmResponse diff --git a/src/Chain.php b/src/Chain.php index 367d5a57..3fc12a52 100644 --- a/src/Chain.php +++ b/src/Chain.php @@ -12,7 +12,7 @@ use PhpLlm\LlmChain\Exception\InvalidArgumentException; use PhpLlm\LlmChain\Exception\MissingModelSupport; use PhpLlm\LlmChain\Model\LanguageModel; -use PhpLlm\LlmChain\Model\Message\MessageBag; +use PhpLlm\LlmChain\Model\Message\MessageBagInterface; use PhpLlm\LlmChain\Model\Response\AsyncResponse; use PhpLlm\LlmChain\Model\Response\ResponseInterface; @@ -45,7 +45,7 @@ public function __construct( /** * @param array $options */ - public function call(MessageBag $messages, array $options = []): ResponseInterface + public function call(MessageBagInterface $messages, array $options = []): ResponseInterface { $llm = $this->llm; diff --git a/src/Chain/Input.php b/src/Chain/Input.php index ecfb73aa..a99b5071 100644 --- a/src/Chain/Input.php +++ b/src/Chain/Input.php @@ -5,7 +5,7 @@ namespace PhpLlm\LlmChain\Chain; use PhpLlm\LlmChain\Model\LanguageModel; -use PhpLlm\LlmChain\Model\Message\MessageBag; +use PhpLlm\LlmChain\Model\Message\MessageBagInterface; final class Input { @@ -14,7 +14,7 @@ final class Input */ public function __construct( public readonly LanguageModel $llm, - public readonly MessageBag $messages, + public readonly MessageBagInterface $messages, private array $options, ) { } diff --git a/src/Chain/Output.php b/src/Chain/Output.php index 438eb5f0..6ac8e823 100644 --- a/src/Chain/Output.php +++ b/src/Chain/Output.php @@ -5,7 +5,7 @@ namespace PhpLlm\LlmChain\Chain; use PhpLlm\LlmChain\Model\LanguageModel; -use PhpLlm\LlmChain\Model\Message\MessageBag; +use PhpLlm\LlmChain\Model\Message\MessageBagInterface; use PhpLlm\LlmChain\Model\Response\ResponseInterface; final class Output @@ -16,7 +16,7 @@ final class Output public function __construct( public readonly LanguageModel $llm, public ResponseInterface $response, - public readonly MessageBag $messages, + public readonly MessageBagInterface $messages, public readonly array $options, ) { } diff --git a/src/ChainInterface.php b/src/ChainInterface.php index c152fb9c..44b483a6 100644 --- a/src/ChainInterface.php +++ b/src/ChainInterface.php @@ -4,7 +4,7 @@ namespace PhpLlm\LlmChain; -use PhpLlm\LlmChain\Model\Message\MessageBag; +use PhpLlm\LlmChain\Model\Message\MessageBagInterface; use PhpLlm\LlmChain\Model\Response\ResponseInterface; interface ChainInterface @@ -12,5 +12,5 @@ interface ChainInterface /** * @param array $options */ - public function call(MessageBag $messages, array $options = []): ResponseInterface; + public function call(MessageBagInterface $messages, array $options = []): ResponseInterface; } diff --git a/src/Model/Message/MessageBag.php b/src/Model/Message/MessageBag.php index 4ff2c38d..26df4415 100644 --- a/src/Model/Message/MessageBag.php +++ b/src/Model/Message/MessageBag.php @@ -4,10 +4,10 @@ namespace PhpLlm\LlmChain\Model\Message; -final class MessageBag implements \Countable, \JsonSerializable +final class MessageBag implements MessageBagInterface { /** - * @var MessageInterface[] + * @var list */ private array $messages; @@ -22,7 +22,7 @@ public function add(MessageInterface $message): void } /** - * @return MessageInterface[] + * @return list */ public function getMessages(): array { @@ -48,7 +48,7 @@ public function with(MessageInterface $message): self return $messages; } - public function merge(MessageBag $messageBag): self + public function merge(MessageBagInterface $messageBag): self { $messages = clone $this; $messages->messages = array_merge($messages->messages, $messageBag->getMessages()); diff --git a/src/Model/Message/MessageBagInterface.php b/src/Model/Message/MessageBagInterface.php new file mode 100644 index 00000000..8f77ba4f --- /dev/null +++ b/src/Model/Message/MessageBagInterface.php @@ -0,0 +1,27 @@ + + */ + public function getMessages(): array; + + public function getSystemMessage(): ?SystemMessage; + + public function with(MessageInterface $message): self; + + public function merge(MessageBagInterface $messageBag): self; + + public function withoutSystemMessage(): self; + + public function prepend(MessageInterface $message): self; + + public function containsImage(): bool; +}