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
feat: add Fabric patterns support #365
Closed
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e77de54
feat: add Fabric patterns support
OskarStark 7f4057c
fix
OskarStark 9523a7a
chore: add php-llm/fabric-pattern to require-dev and suggest
OskarStark 7fed491
refactor: simplify fabric examples with directory check
OskarStark 011e734
feat: add fabric pattern names to example outputs
OskarStark 5253ef7
style: add missing newlines at end of example files
OskarStark ed498d8
refactor: replace isset with array_key_exists
OskarStark bcf494b
refactor: make FabricRepository mandatory in FabricInputProcessor
OskarStark f1e37c8
feat: add non-empty-string type annotation for pattern
OskarStark 4064979
-
OskarStark c57da11
fix: check for Pattern class existence instead of directory structure
OskarStark 77d9a5d
refactor: update fabric package detection and remove comments
OskarStark 05709cb
chore: remove redundant comment from FabricInputProcessor
OskarStark 02be26a
chore: combine error message into single echo statement
OskarStark d128ec0
refactor: use Pattern class existence check instead of directory path
OskarStark c933b88
feat: add validation to prevent multiple system messages
OskarStark 80d4a41
refactor: use imported Pattern class instead of FQCN
OskarStark 8a39981
refactor: simplify Fabric integration by using Pattern class directly
OskarStark 8f2f5d3
-
OskarStark 7da2538
Use package-specific exceptions instead of generic PHP exceptions
OskarStark b1327e5
Fix CS: Add missing newline at end of LogicException.php
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PhpLlm\FabricPattern\Pattern; | ||
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); | ||
} | ||
|
||
if (!class_exists(Pattern::class)) { | ||
echo 'Fabric patterns are not installed. Please install them with: composer require php-llm/fabric-pattern'.\PHP_EOL; | ||
exit(1); | ||
} | ||
|
||
$platform = PlatformFactory::create($_ENV['OPENAI_API_KEY']); | ||
$model = new GPT(GPT::GPT_4O_MINI); | ||
$chain = new Chain($platform, $model); | ||
|
||
$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; | ||
|
||
$messages = new MessageBag( | ||
Message::fabric('create_summary'), | ||
Message::ofUser($article) | ||
); | ||
|
||
$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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PhpLlm\FabricPattern\Pattern; | ||
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); | ||
} | ||
|
||
if (!class_exists(Pattern::class)) { | ||
echo 'Fabric patterns are not installed. 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; |
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\Platform\Exception; | ||
|
||
class LogicException extends \LogicException implements ExceptionInterface | ||
{ | ||
} |
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace PhpLlm\LlmChain\Platform\Fabric; | ||
|
||
use PhpLlm\FabricPattern\Pattern; | ||
use PhpLlm\LlmChain\Chain\Input; | ||
use PhpLlm\LlmChain\Chain\InputProcessorInterface; | ||
use PhpLlm\LlmChain\Platform\Exception\InvalidArgumentException; | ||
use PhpLlm\LlmChain\Platform\Exception\LogicException; | ||
use PhpLlm\LlmChain\Platform\Exception\RuntimeException; | ||
use PhpLlm\LlmChain\Platform\Message\SystemMessage; | ||
|
||
/** | ||
* Requires the "php-llm/fabric-pattern" package to be installed. | ||
*/ | ||
final readonly class FabricInputProcessor implements InputProcessorInterface | ||
{ | ||
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'); | ||
} | ||
|
||
if (null !== $input->messages->getSystemMessage()) { | ||
throw new LogicException('Cannot add Fabric pattern: MessageBag already contains a system message'); | ||
} | ||
|
||
if (!class_exists(Pattern::class)) { | ||
throw new RuntimeException('Fabric patterns not found. Please install the "php-llm/fabric-pattern" package: composer require php-llm/fabric-pattern'); | ||
} | ||
|
||
$content = (new Pattern())->load($pattern); | ||
$systemMessage = new SystemMessage($content); | ||
|
||
$input->messages = $input->messages->prepend($systemMessage); | ||
|
||
unset($options['fabric_pattern']); | ||
$input->setOptions($options); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.