Skip to content
This repository was archived by the owner on Jul 16, 2025. It is now read-only.

LLama prompt #129

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/Model/Language/Llama.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace PhpLlm\LlmChain\Model\Language;

use PhpLlm\LlmChain\LanguageModel;
use PhpLlm\LlmChain\Message\AssistantMessage;
use PhpLlm\LlmChain\Message\MessageBag;
use PhpLlm\LlmChain\Message\MessageInterface;
use PhpLlm\LlmChain\Message\UserMessage;
use PhpLlm\LlmChain\Platform\Ollama;
use PhpLlm\LlmChain\Platform\Replicate;
use PhpLlm\LlmChain\Response\TextResponse;
Expand All @@ -24,12 +27,32 @@

$response = $this->platform->request('meta/meta-llama-3.1-405b-instruct', $endpoint, [
'system' => $systemMessage?->content,
'prompt' => $messages->withoutSystemMessage()->getIterator()->current()->content[0]->text, // @phpstan-ignore-line TODO: Multiple messages
'prompt' => self::convertToPrompt($messages->withoutSystemMessage()),
]);

return new TextResponse(implode('', $response['output']));
}

private static function convertToPrompt(MessageBag $messageBag): string
{
$messages = [];

/** @var MessageInterface $message */
foreach ($messageBag->getIterator() as $message) {
if ($message instanceof UserMessage) {
$content = $message->content[0]->text;

Check failure on line 43 in src/Model/Language/Llama.php

View workflow job for this annotation

GitHub Actions / qa

Access to an undefined property PhpLlm\LlmChain\Message\Content\Content::$text.
} elseif ($message instanceof AssistantMessage && null !== $message->content) {
$content = $message->content;
} else {
continue;
}

$messages[] = sprintf('%s: %s', ucfirst($message->getRole()->value), $content);
}

return implode(PHP_EOL, $messages);
}

public function supportsToolCalling(): bool
{
return false; // it does, but implementation here is still open.
Expand Down
17 changes: 17 additions & 0 deletions tests/Fixture/StructuredOutput/MathReasoning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\Fixture\StructuredOutput;

final class MathReasoning
{
/**
* @param Step[] $steps
*/
public function __construct(
public array $steps,
public string $finalAnswer,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\StructuredOutput\Data;
namespace PhpLlm\LlmChain\Tests\Fixture\StructuredOutput;

final class Step
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace PhpLlm\LlmChain\Tests\StructuredOutput\Data;
namespace PhpLlm\LlmChain\Tests\Fixture\StructuredOutput;

final class User
{
Expand Down
Loading