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

feat: add Fabric patterns support #365

Closed
wants to merge 21 commits into from
Closed

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

@chr-hertel
Copy link
Member

Two things I struggle here with:

  1. i'm not sure about this options way of using the prompts - I def favor the Message::fabric one over the option
  2. i don't think we need the entire repository+prompt classes, since the lib itself provides already the logic to use the markdown files (new Pattern)->load('extract_wisdom')

@OskarStark OskarStark force-pushed the feat/fabric-patterns branch from 9d0476a to 862d3ae Compare July 14, 2025 09:59
@OskarStark
Copy link
Contributor Author

any comments @chr-hertel ?

Copy link
Member

@chr-hertel chr-hertel left a comment

Choose a reason for hiding this comment

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

only minor stuff :) 👍

OskarStark and others added 20 commits July 15, 2025 12:41

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark
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 <noreply@anthropic.com>
fix

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark
- Remove try/catch blocks for cleaner code
- Use simple directory existence check
- Update composer.json to only allow fabric-pattern ^0.1

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark
- Repository is now always initialized (either provided or created)
- Simplified code by removing null coalescing in processInput

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark
- FabricPromptInterface::getPattern() now returns non-empty-string
- FabricPrompt constructor expects non-empty-string for pattern
- Improves type safety and PHPStan analysis
-

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark
- Remove FabricRepository, FabricPrompt, FabricPromptInterface
- Remove PatternNotFoundException
- Remove support for custom pattern paths
- Use php-llm/fabric-pattern's Pattern class directly
- Update tests to match new implementation
- Update README to remove custom pattern location docs
-

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark
@OskarStark OskarStark force-pushed the feat/fabric-patterns branch from 8b356f9 to 7da2538 Compare July 15, 2025 10:49
@OskarStark
Copy link
Contributor Author

Problem 1
- Root composer.json requires php-llm/fabric-pattern ^0.1 -> satisfiable by php-llm/fabric-pattern[0.1.4.51].
- php-llm/fabric-pattern 0.1.4.51 requires danielmiessler/fabric ^1.4 -> could not be found in any version, there may be a typo in the package name.

lets release a 1.0? 🤷

Verified

This commit was signed with the committer’s verified signature.
OskarStark Oskar Stark
@chr-hertel
Copy link
Member

Oooooh, no, if course ... my trick to install that python thingy doesn't work for transitive dependencies. php-llm/fabric-pattern 0.1.4.51 requires danielmiessler/fabric will always result in an error since that thing is not a real PHP package.

okay, we need a different approach anyways

@chr-hertel
Copy link
Member

Let's first bring in a new approach to fabric into symfony/ai and then rebuild this please

@chr-hertel chr-hertel closed this Jul 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fabric Support for System Messages
3 participants