Skip to content

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
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

OskarStark
Copy link
Contributor

Summary

This PR implements support for Fabric patterns in LLM Chain, providing access to a comprehensive collection of pre-built, tested system prompts for common AI tasks.

Implementation Details

Core Components

  1. Message::fabric() Factory Method

    • Simple API for using Fabric patterns: Message::fabric('create_summary')
    • Supports custom pattern locations
    • Automatically loads patterns from the php-llm/fabric-pattern package
  2. FabricInputProcessor

    • Enables dynamic pattern loading via chain options
    • Usage: $chain->call($messages, ['fabric_pattern' => 'analyze_code'])
    • Can be configured with custom pattern repositories
  3. FabricRepository

    • Manages pattern loading and caching
    • Validates pattern existence
    • Supports metadata loading (README.md, metadata.json)
    • Checks for php-llm/fabric-pattern package installation
  4. Exception Handling

    • Clear error messages when the patterns package is not installed
    • PatternNotFoundException for missing patterns

Design Decisions

  1. Separate Package Requirement

    • Fabric patterns are provided by the php-llm/fabric-pattern package
    • Keeps the main library lightweight
    • Users can opt-in to Fabric functionality
    • Clear error messages guide users to install the package
  2. Multiple Usage Patterns

    • Factory method for simple, static usage
    • Input processor for dynamic, runtime pattern selection
    • Both approaches support custom pattern locations
  3. Immutable Design

    • Follows the library's immutable patterns (e.g., MessageBag)
    • Repository uses internal caching for performance

Usage Examples

Via Factory Method

$messages = new MessageBag(
    Message::fabric('create_summary'),
    Message::ofUser($articleContent)
);
$response = $chain->call($messages);

Via Input Processor

$processor = new FabricInputProcessor();
$chain = new Chain($platform, $model, [$processor]);

$response = $chain->call($messages, ['fabric_pattern' => 'analyze_threat_report']);

Custom Pattern Location

$repository = new FabricRepository('/path/to/custom/patterns');
$processor = new FabricInputProcessor($repository);

Documentation

  • Added comprehensive section to README.md
  • Created example files in examples/fabric/
  • Included PHPDoc comments with usage instructions

Testing

  • Full test coverage for all new classes
  • Tests handle the case when the patterns package is not installed
  • Mock-friendly design (removed final from FabricRepository)

Available Patterns

Some popular patterns include:

  • create_summary - Create comprehensive summaries
  • analyze_claims - Fact-check and analyze claims
  • extract_wisdom - Extract key insights
  • improve_writing - Enhance writing quality
  • analyze_code - Security and quality analysis

Closes #31

🤖 Generated with Claude Code

OskarStark and others added 10 commits June 29, 2025 23:54
Implements integration with Fabric patterns from danielmiessler/fabric, providing
a collection of pre-built, tested system prompts for common AI tasks.

Key features:
- Message::fabric() factory method for easy pattern usage
- FabricInputProcessor for dynamic pattern loading via chain options
- FabricRepository for pattern management with caching
- Support for custom pattern locations
- Comprehensive test coverage

Usage:
1. Install the patterns: composer require php-llm/fabric-pattern
2. Use via factory: Message::fabric('create_summary')
3. Or via processor: $chain->call($messages, ['fabric_pattern' => 'analyze_code'])

The implementation requires the separate php-llm/fabric-pattern package to be
installed, keeping the main library lightweight while allowing users to opt-in
to Fabric patterns functionality.

Closes #31

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
- Remove try/catch blocks for cleaner code
- Use simple directory existence check
- Update composer.json to only allow fabric-pattern ^0.1
- Repository is now always initialized (either provided or created)
- Simplified code by removing null coalescing in processInput
- FabricPromptInterface::getPattern() now returns non-empty-string
- FabricPrompt constructor expects non-empty-string for pattern
- Improves type safety and PHPStan analysis
$systemMessage = new SystemMessage($fabricPrompt->getContent());

// Prepend the system message
$input->messages = $input->messages->prepend($systemMessage);
Copy link
Contributor

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? 🤔

Copy link
Contributor Author

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

{
if (null === $patternsPath) {
// Check if fabric-pattern package is installed
$fabricPatternPath = \dirname(__DIR__, 4).'/fabric-pattern/patterns';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this maybe be improved by checking for the existence of the pattern class from this package? For me checking directory hierarchie feels a bit unstable. And utilizing the pattern path btw. the loader from this class the library is delivering?

https://github.com/php-llm/fabric-pattern/blob/main/src/Pattern.php

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say yes, also not super happy right now with this extra package, maybe it's better to get all the data and check it in into the fabric-pattern repository by mentioning the copyright etc. or having a command to download the fabrics upfront and use this path. not sure yet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fabric Support for System Messages
2 participants