This repository was archived by the owner on Jul 16, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Message Splitting to allow multiple message implementations without stacking arguments #25
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
af16dc5
Allow usage of gpt visions api
DZunke 461f8a6
Add tests
DZunke 4e37cd2
Rename content classes
DZunke 27fee03
Move content casting of user message to message object
DZunke 9989b4f
Simplify message interface with content classes
DZunke 6a1d6fe
Rename variable
DZunke bcb7e12
Fix image describer after latest interface change
DZunke 96a0ffc
Some review adjustments
DZunke File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
use PhpLlm\LlmChain\Chain; | ||
use PhpLlm\LlmChain\Message\Message; | ||
use PhpLlm\LlmChain\Message\MessageBag; | ||
use PhpLlm\LlmChain\OpenAI\Model\Gpt; | ||
use PhpLlm\LlmChain\OpenAI\Model\Gpt\Version; | ||
use PhpLlm\LlmChain\OpenAI\Runtime\OpenAI; | ||
use Symfony\Component\HttpClient\HttpClient; | ||
|
||
require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
|
||
$runtime = new OpenAI(HttpClient::create(), getenv('OPENAI_API_KEY')); | ||
$llm = new Gpt($runtime, Version::gpt4oMini()); | ||
|
||
$chain = new Chain($llm); | ||
$messages = new MessageBag( | ||
Message::forSystem('You are an image analyzer that looks to images like a comedian would like.'), | ||
Message::ofUser( | ||
'Describe the image as a comedian would do it.', | ||
'https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Webysther_20160423_-_Elephpant.svg/350px-Webysther_20160423_-_Elephpant.svg.png', | ||
'https://upload.wikimedia.org/wikipedia/commons/thumb/3/37/African_Bush_Elephant.jpg/320px-African_Bush_Elephant.jpg', | ||
DZunke marked this conversation as resolved.
Show resolved
Hide resolved
DZunke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
); | ||
$response = $chain->call($messages); | ||
|
||
echo $response.PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Message; | ||
|
||
use PhpLlm\LlmChain\Response\ToolCall; | ||
|
||
final readonly class AssistantMessage implements MessageInterface | ||
{ | ||
/** | ||
* @param ?ToolCall[] $toolCalls | ||
*/ | ||
public function __construct( | ||
public ?string $content = null, | ||
public ?array $toolCalls = null, | ||
) { | ||
} | ||
|
||
public function getRole(): Role | ||
{ | ||
return Role::Assistant; | ||
} | ||
|
||
public function hasToolCalls(): bool | ||
{ | ||
return null !== $this->toolCalls && 0 !== \count($this->toolCalls); | ||
} | ||
|
||
/** | ||
* @return array{ | ||
* role: Role::Assistant, | ||
* content: ?string, | ||
* tool_calls?: ToolCall[], | ||
* } | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
$array = [ | ||
'role' => Role::Assistant, | ||
]; | ||
|
||
if (null !== $this->content) { | ||
$array['content'] = $this->content; | ||
} | ||
|
||
if ($this->hasToolCalls()) { | ||
$array['tool_calls'] = $this->toolCalls; | ||
} | ||
|
||
return $array; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Message\Content; | ||
|
||
interface ContentInterface extends \JsonSerializable | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Message\Content; | ||
|
||
final readonly class Image implements ContentInterface | ||
{ | ||
public function __construct(public string $image) | ||
OskarStark marked this conversation as resolved.
Show resolved
Hide resolved
DZunke marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
} | ||
|
||
/** | ||
* @return array{type: 'image_url', image_url: array{url: string}} | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return ['type' => 'image_url', 'image_url' => ['url' => $this->image]]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Message\Content; | ||
|
||
final readonly class Text implements ContentInterface | ||
{ | ||
public function __construct(public string $text) | ||
{ | ||
} | ||
|
||
/** | ||
* @return array{type: 'text', text: string} | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return ['type' => 'text', 'text' => $this->text]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Message; | ||
|
||
interface MessageInterface extends \JsonSerializable | ||
{ | ||
public function getRole(): Role; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Message; | ||
|
||
final readonly class SystemMessage implements MessageInterface | ||
{ | ||
public function __construct(public string $content) | ||
{ | ||
} | ||
|
||
public function getRole(): Role | ||
{ | ||
return Role::System; | ||
} | ||
|
||
/** | ||
* @return array{ | ||
* role: Role::System, | ||
* content: string | ||
* } | ||
*/ | ||
public function jsonSerialize(): array | ||
{ | ||
return [ | ||
'role' => Role::System, | ||
'content' => $this->content, | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.