|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpLlm\LlmChain\Model\Language; |
| 6 | + |
| 7 | +use PhpLlm\LlmChain\Exception\RuntimeException; |
| 8 | +use PhpLlm\LlmChain\LanguageModel; |
| 9 | +use PhpLlm\LlmChain\Message\AssistantMessage; |
| 10 | +use PhpLlm\LlmChain\Message\Content\Image; |
| 11 | +use PhpLlm\LlmChain\Message\Content\Text; |
| 12 | +use PhpLlm\LlmChain\Message\MessageBag; |
| 13 | +use PhpLlm\LlmChain\Message\SystemMessage; |
| 14 | +use PhpLlm\LlmChain\Message\UserMessage; |
| 15 | +use PhpLlm\LlmChain\Platform\Ollama; |
| 16 | +use PhpLlm\LlmChain\Platform\Replicate; |
| 17 | +use PhpLlm\LlmChain\Response\TextResponse; |
| 18 | + |
| 19 | +final readonly class Llama implements LanguageModel |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private Replicate|Ollama $platform, |
| 23 | + ) { |
| 24 | + } |
| 25 | + |
| 26 | + public function call(MessageBag $messages, array $options = []): TextResponse |
| 27 | + { |
| 28 | + if ($this->platform instanceof Replicate) { |
| 29 | + $response = $this->platform->request('meta/meta-llama-3.1-405b-instruct', 'predictions', [ |
| 30 | + 'system' => self::convertMessage($messages->getSystemMessage() ?? new SystemMessage('')), |
| 31 | + 'prompt' => self::convertToPrompt($messages->withoutSystemMessage()), |
| 32 | + ]); |
| 33 | + |
| 34 | + return new TextResponse(implode('', $response['output'])); |
| 35 | + } |
| 36 | + |
| 37 | + $response = $this->platform->request('llama3.2', 'chat', ['messages' => $messages, 'stream' => false]); |
| 38 | + |
| 39 | + return new TextResponse($response['message']['content']); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @todo make method private, just for testing, or create a MessageBag to LLama convert class :thinking: |
| 44 | + */ |
| 45 | + public static function convertToPrompt(MessageBag $messageBag): string |
| 46 | + { |
| 47 | + $messages = []; |
| 48 | + |
| 49 | + /** @var UserMessage|SystemMessage|AssistantMessage $message */ |
| 50 | + foreach ($messageBag->getIterator() as $message) { |
| 51 | + $messages[] = self::convertMessage($message); |
| 52 | + } |
| 53 | + |
| 54 | + $messages = array_filter($messages, fn ($message) => '' !== $message); |
| 55 | + |
| 56 | + return trim(implode(PHP_EOL.PHP_EOL, $messages)).PHP_EOL.PHP_EOL.'<|start_header_id|>assistant<|end_header_id|>'; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @todo make method private, just for testing |
| 61 | + */ |
| 62 | + public static function convertMessage(UserMessage|SystemMessage|AssistantMessage $message): string |
| 63 | + { |
| 64 | + if ($message instanceof SystemMessage) { |
| 65 | + return trim(<<<SYSTEM |
| 66 | +<|begin_of_text|><|start_header_id|>system<|end_header_id|> |
| 67 | +
|
| 68 | +{$message->content}<|eot_id|> |
| 69 | +SYSTEM); |
| 70 | + } |
| 71 | + |
| 72 | + if ($message instanceof AssistantMessage) { |
| 73 | + if ('' === $message->content || null === $message->content) { |
| 74 | + return ''; |
| 75 | + } |
| 76 | + |
| 77 | + return trim(<<<ASSISTANT |
| 78 | +<|start_header_id|>{$message->getRole()->value}<|end_header_id|> |
| 79 | +
|
| 80 | +{$message->content}<|eot_id|> |
| 81 | +ASSISTANT); |
| 82 | + } |
| 83 | + |
| 84 | + if ($message instanceof UserMessage) { |
| 85 | + $count = count($message->content); |
| 86 | + |
| 87 | + $contentParts = []; |
| 88 | + if ($count > 1) { |
| 89 | + foreach ($message->content as $value) { |
| 90 | + if ($value instanceof Text) { |
| 91 | + $contentParts[] = $value->text; |
| 92 | + } |
| 93 | + |
| 94 | + if ($value instanceof Image) { |
| 95 | + $contentParts[] = $value->url; |
| 96 | + } |
| 97 | + } |
| 98 | + } elseif (1 === $count) { |
| 99 | + $value = $message->content[0]; |
| 100 | + if ($value instanceof Text) { |
| 101 | + $contentParts[] = $value->text; |
| 102 | + } |
| 103 | + |
| 104 | + if ($value instanceof Image) { |
| 105 | + $contentParts[] = $value->url; |
| 106 | + } |
| 107 | + } else { |
| 108 | + throw new RuntimeException('Unsupported message type.'); |
| 109 | + } |
| 110 | + |
| 111 | + $content = implode(PHP_EOL, $contentParts); |
| 112 | + |
| 113 | + return trim(<<<USER |
| 114 | +<|start_header_id|>{$message->getRole()->value}<|end_header_id|> |
| 115 | +
|
| 116 | +{$content}<|eot_id|> |
| 117 | +USER); |
| 118 | + } |
| 119 | + |
| 120 | + throw new RuntimeException('Unsupported message type.'); // @phpstan-ignore-line |
| 121 | + } |
| 122 | + |
| 123 | + public function supportsToolCalling(): bool |
| 124 | + { |
| 125 | + return false; // it does, but implementation here is still open. |
| 126 | + } |
| 127 | + |
| 128 | + public function supportsImageInput(): bool |
| 129 | + { |
| 130 | + return false; // it does, but implementation here is still open. |
| 131 | + } |
| 132 | + |
| 133 | + public function supportsStructuredOutput(): bool |
| 134 | + { |
| 135 | + return false; |
| 136 | + } |
| 137 | +} |
0 commit comments