-
Notifications
You must be signed in to change notification settings - Fork 24
feat: add Fabric patterns support #365
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
OskarStark
wants to merge
10
commits into
main
Choose a base branch
from
feat/fabric-patterns
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3d2eeeb
feat: add Fabric patterns support
OskarStark 4960e82
fix
OskarStark d7127e5
chore: add php-llm/fabric-pattern to require-dev and suggest
OskarStark 006b704
refactor: simplify fabric examples with directory check
OskarStark d46f357
feat: add fabric pattern names to example outputs
OskarStark b89571e
style: add missing newlines at end of example files
OskarStark 54e4a6d
refactor: replace isset with array_key_exists
OskarStark 87956d2
refactor: make FabricRepository mandatory in FabricInputProcessor
OskarStark 33deeda
feat: add non-empty-string type annotation for pattern
OskarStark 9d0476a
-
OskarStark 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
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PhpLlm\LlmChain\Chain\Chain; | ||
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\GPT; | ||
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory; | ||
use PhpLlm\LlmChain\Platform\Message\Message; | ||
use PhpLlm\LlmChain\Platform\Message\MessageBag; | ||
|
||
require_once dirname(__DIR__).'/../vendor/autoload.php'; | ||
|
||
if (empty($_ENV['OPENAI_API_KEY'])) { | ||
echo 'Please set the OPENAI_API_KEY environment variable.'.\PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
// Check if Fabric patterns package is installed | ||
if (!is_dir(dirname(__DIR__, 2).'/vendor/php-llm/fabric-pattern')) { | ||
echo 'Fabric patterns are not installed.'.\PHP_EOL; | ||
echo 'Please install them with: composer require php-llm/fabric-pattern'.\PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
// Initialize platform and model | ||
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); | ||
$model = new GPT(GPT::GPT_4O_MINI); | ||
$chain = new Chain($platform, $model); | ||
|
||
// Example article to summarize | ||
$article = <<<'ARTICLE' | ||
The field of artificial intelligence has undergone dramatic transformations in recent years, | ||
with large language models (LLMs) emerging as one of the most significant breakthroughs. | ||
These models, trained on vast amounts of text data, have demonstrated remarkable capabilities | ||
in understanding and generating human-like text. The implications for software development, | ||
content creation, and human-computer interaction are profound. | ||
|
||
However, with these advances come important considerations regarding ethics, bias, and the | ||
responsible deployment of AI systems. Researchers and practitioners must work together to | ||
ensure that these powerful tools are used in ways that benefit society while minimizing | ||
potential harms. | ||
ARTICLE; | ||
|
||
// Create messages using Fabric pattern | ||
$messages = new MessageBag( | ||
Message::fabric('create_summary'), | ||
Message::ofUser($article) | ||
); | ||
|
||
// Call the chain | ||
$response = $chain->call($messages); | ||
|
||
echo 'Summary using Fabric pattern "create_summary":'.\PHP_EOL; | ||
echo '=============================================='.\PHP_EOL; | ||
echo $response->getContent().\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,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PhpLlm\LlmChain\Chain\Chain; | ||
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\GPT; | ||
use PhpLlm\LlmChain\Platform\Bridge\OpenAI\PlatformFactory; | ||
use PhpLlm\LlmChain\Platform\Fabric\FabricInputProcessor; | ||
use PhpLlm\LlmChain\Platform\Message\Message; | ||
use PhpLlm\LlmChain\Platform\Message\MessageBag; | ||
|
||
require_once dirname(__DIR__).'/../vendor/autoload.php'; | ||
|
||
if (empty($_ENV['OPENAI_API_KEY'])) { | ||
echo 'Please set the OPENAI_API_KEY environment variable.'.\PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
// Check if Fabric patterns package is installed | ||
if (!is_dir(dirname(__DIR__, 2).'/vendor/php-llm/fabric-pattern')) { | ||
echo 'Fabric patterns are not installed.'.\PHP_EOL; | ||
echo 'Please install them with: composer require php-llm/fabric-pattern'.\PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
// Initialize platform and model | ||
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); | ||
$model = new GPT(GPT::GPT_4O_MINI); | ||
|
||
// Create chain with Fabric processor | ||
$processor = new FabricInputProcessor(); | ||
$chain = new Chain($platform, $model, [$processor]); | ||
|
||
// Example code to analyze | ||
$code = <<<'CODE' | ||
function processUserData($data) { | ||
$sql = "SELECT * FROM users WHERE id = " . $data['id']; | ||
$result = mysql_query($sql); | ||
|
||
while ($row = mysql_fetch_array($result)) { | ||
echo $row['name'] . " - " . $row['email']; | ||
} | ||
} | ||
CODE; | ||
|
||
// Create messages | ||
$messages = new MessageBag( | ||
Message::ofUser("Analyze this PHP code for security issues:\n\n".$code) | ||
); | ||
|
||
// Call with Fabric pattern | ||
$response = $chain->call($messages, ['fabric_pattern' => 'analyze_code']); | ||
|
||
echo 'Code Analysis using Fabric pattern "analyze_code":'.\PHP_EOL; | ||
echo '=================================================='.\PHP_EOL; | ||
echo $response->getContent().\PHP_EOL; |
12 changes: 12 additions & 0 deletions
12
src/Platform/Fabric/Exception/PatternNotFoundException.php
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,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Platform\Fabric\Exception; | ||
|
||
/** | ||
* Exception thrown when a Fabric pattern is not found. | ||
*/ | ||
final class PatternNotFoundException extends \RuntimeException | ||
{ | ||
} |
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Platform\Fabric; | ||
|
||
use PhpLlm\LlmChain\Chain\Input; | ||
use PhpLlm\LlmChain\Chain\InputProcessorInterface; | ||
use PhpLlm\LlmChain\Platform\Message\SystemMessage; | ||
|
||
/** | ||
* Input processor for Fabric patterns. | ||
* | ||
* Requires the "php-llm/fabric-pattern" package to be installed. | ||
* | ||
* This processor allows adding Fabric patterns through options: | ||
* - fabric_pattern: string - The pattern name to load | ||
*/ | ||
final readonly class FabricInputProcessor implements InputProcessorInterface | ||
{ | ||
public function __construct( | ||
private FabricRepository $repository = new FabricRepository(), | ||
) { | ||
} | ||
|
||
public function processInput(Input $input): void | ||
{ | ||
$options = $input->getOptions(); | ||
|
||
if (!\array_key_exists('fabric_pattern', $options)) { | ||
return; | ||
} | ||
|
||
$pattern = $options['fabric_pattern']; | ||
if (!\is_string($pattern)) { | ||
throw new \InvalidArgumentException('The "fabric_pattern" option must be a string'); | ||
} | ||
|
||
// Load the pattern and prepend as system message | ||
$fabricPrompt = $this->repository->load($pattern); | ||
$systemMessage = new SystemMessage($fabricPrompt->getContent()); | ||
|
||
// Prepend the system message | ||
$input->messages = $input->messages->prepend($systemMessage); | ||
|
||
// Remove the fabric option from the chain options | ||
unset($options['fabric_pattern']); | ||
$input->setOptions($options); | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Platform\Fabric; | ||
|
||
/** | ||
* Represents a Fabric prompt pattern. | ||
*/ | ||
final readonly class FabricPrompt implements FabricPromptInterface | ||
{ | ||
/** | ||
* @param non-empty-string $pattern | ||
* @param array<string, mixed> $metadata | ||
*/ | ||
public function __construct( | ||
private string $pattern, | ||
private string $content, | ||
private array $metadata = [], | ||
) { | ||
} | ||
|
||
/** | ||
* @return non-empty-string | ||
*/ | ||
public function getPattern(): string | ||
{ | ||
return $this->pattern; | ||
} | ||
|
||
public function getContent(): string | ||
{ | ||
return $this->content; | ||
} | ||
|
||
public function getMetadata(): array | ||
{ | ||
return $this->metadata; | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Platform\Fabric; | ||
|
||
/** | ||
* Interface for Fabric prompt patterns. | ||
*/ | ||
interface FabricPromptInterface | ||
{ | ||
/** | ||
* Get the pattern name (e.g., 'create_summary'). | ||
* | ||
* @return non-empty-string | ||
*/ | ||
public function getPattern(): string; | ||
|
||
/** | ||
* Get the system prompt content. | ||
*/ | ||
public function getContent(): string; | ||
|
||
/** | ||
* Get metadata about the pattern. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function getMetadata(): array; | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is happening if the MessageBag already have a System Message? I think most models can handle having multiple system messages. I have read some models do not care about the role of a message and take everything the same. But are we clear about possible problems when adding a system message to a bag that is already containing a system message? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, same with the SystemInputProcessor, only one SystemMessage is supported by llm-chain