Skip to content

Commit

Permalink
Enhancement: Add MessageBagInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark committed Dec 28, 2024
1 parent 8c6ac09 commit 8aad98d
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/Bridge/Anthropic/ModelHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, [
Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Meta/LlamaPromptConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Ollama/LlamaModelHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/Bridge/Replicate/LlamaModelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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('')),
Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Replicate/LlamaResponseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Chain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -45,7 +45,7 @@ public function __construct(
/**
* @param array<string, mixed> $options
*/
public function call(MessageBag $messages, array $options = []): ResponseInterface
public function call(MessageBagInterface $messages, array $options = []): ResponseInterface
{
$llm = $this->llm;

Expand Down
4 changes: 2 additions & 2 deletions src/Chain/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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,
) {
}
Expand Down
4 changes: 2 additions & 2 deletions src/Chain/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
) {
}
Expand Down
4 changes: 2 additions & 2 deletions src/ChainInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace PhpLlm\LlmChain;

use PhpLlm\LlmChain\Model\Message\MessageBag;
use PhpLlm\LlmChain\Model\Message\MessageBagInterface;
use PhpLlm\LlmChain\Model\Response\ResponseInterface;

interface ChainInterface
{
/**
* @param array<string, mixed> $options
*/
public function call(MessageBag $messages, array $options = []): ResponseInterface;
public function call(MessageBagInterface $messages, array $options = []): ResponseInterface;
}
8 changes: 4 additions & 4 deletions src/Model/Message/MessageBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace PhpLlm\LlmChain\Model\Message;

final class MessageBag implements \Countable, \JsonSerializable
final class MessageBag implements MessageBagInterface
{
/**
* @var MessageInterface[]
* @var list<MessageInterface>
*/
private array $messages;

Expand All @@ -22,7 +22,7 @@ public function add(MessageInterface $message): void
}

/**
* @return MessageInterface[]
* @return list<MessageInterface>
*/
public function getMessages(): array
{
Expand All @@ -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());
Expand Down
27 changes: 27 additions & 0 deletions src/Model/Message/MessageBagInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Model\Message;

interface MessageBagInterface extends \JsonSerializable, \Countable
{
public function add(MessageInterface $message): void;

/**
* @return list<MessageInterface>
*/
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;
}

0 comments on commit 8aad98d

Please sign in to comment.