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 23
feat: add support for TransformersPHP for model runtime in PHP #280
Merged
Merged
Changes from all commits
Commits
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 |
|---|---|---|
|
|
@@ -4,3 +4,4 @@ vendor | |
| .phpunit.cache | ||
| coverage | ||
| .env.local | ||
| .transformers-cache | ||
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,26 @@ | ||
| <?php | ||
|
|
||
| use Codewithkyrian\Transformers\Pipelines\Task; | ||
| use PhpLlm\LlmChain\Bridge\TransformersPHP\Model; | ||
| use PhpLlm\LlmChain\Bridge\TransformersPHP\PlatformFactory; | ||
|
|
||
| require_once dirname(__DIR__).'/vendor/autoload.php'; | ||
|
|
||
| if (!extension_loaded('ffi') || '1' !== ini_get('ffi.enable')) { | ||
| echo 'FFI extension is not loaded or enabled. Please enable it in your php.ini file.'.PHP_EOL; | ||
| echo 'See https://github.com/CodeWithKyrian/transformers-php for setup instructions.'.PHP_EOL; | ||
| exit(1); | ||
| } | ||
|
|
||
| if (!is_dir(dirname(__DIR__).'/.transformers-cache/Xenova/LaMini-Flan-T5-783M')) { | ||
| echo 'Model "Xenova/LaMini-Flan-T5-783M" not found. Downloading it will be part of the first run. This may take a while...'.PHP_EOL; | ||
| } | ||
|
|
||
| $platform = PlatformFactory::create(); | ||
| $model = new Model('Xenova/LaMini-Flan-T5-783M'); | ||
|
|
||
| $response = $platform->request($model, 'How many continents are there in the world?', [ | ||
| 'task' => Task::Text2TextGeneration, | ||
| ]); | ||
|
|
||
| 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,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpLlm\LlmChain\Bridge\TransformersPHP; | ||
|
|
||
| use Codewithkyrian\Transformers\Pipelines\Task; | ||
| use PhpLlm\LlmChain\Model\Model as BaseModel; | ||
| use PhpLlm\LlmChain\Model\Response\ResponseInterface as LlmResponse; | ||
| use PhpLlm\LlmChain\Model\Response\StructuredResponse; | ||
| use PhpLlm\LlmChain\Model\Response\TextResponse; | ||
| use PhpLlm\LlmChain\Platform\ModelClient; | ||
| use PhpLlm\LlmChain\Platform\ResponseConverter; | ||
| use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
|
||
| use function Codewithkyrian\Transformers\Pipelines\pipeline; | ||
|
|
||
| final readonly class Handler implements ModelClient, ResponseConverter | ||
| { | ||
| public function supports(BaseModel $model, object|array|string $input): bool | ||
| { | ||
| return $model instanceof Model; | ||
| } | ||
|
|
||
| public function request(BaseModel $model, object|array|string $input, array $options = []): ResponseInterface | ||
| { | ||
| if (!isset($options['task'])) { | ||
| throw new \InvalidArgumentException('The task option is required.'); | ||
| } | ||
|
|
||
| $pipeline = pipeline( | ||
| $options['task'], | ||
| $model->getName(), | ||
| $options['quantized'] ?? true, | ||
| $options['config'] ?? null, | ||
| $options['cacheDir'] ?? null, | ||
| $options['revision'] ?? 'main', | ||
| $options['modelFilename'] ?? null, | ||
| ); | ||
|
|
||
| return new PipelineResponse($pipeline, $input); | ||
| } | ||
|
|
||
| public function convert(ResponseInterface $response, array $options = []): LlmResponse | ||
| { | ||
| if (!$response instanceof PipelineResponse) { | ||
| throw new \InvalidArgumentException('The response is not a valid TransformersPHP response.'); | ||
| } | ||
|
|
||
| $task = $options['task']; | ||
| $data = $response->toArray(); | ||
|
|
||
| return match ($task) { | ||
| Task::Text2TextGeneration => new TextResponse($data[0]['generated_text']), | ||
| default => new StructuredResponse($response->toArray()), | ||
| }; | ||
| } | ||
| } |
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\Bridge\TransformersPHP; | ||
|
|
||
| use PhpLlm\LlmChain\Model\Model as BaseModel; | ||
|
|
||
| final readonly class Model implements BaseModel | ||
| { | ||
| /** | ||
| * @param string $name the name of the model is optional with TransformersPHP | ||
| * @param array<string, mixed> $options | ||
| */ | ||
| public function __construct( | ||
| private ?string $name = null, | ||
| private array $options = [], | ||
| ) { | ||
| } | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return $this->name ?? ''; | ||
| } | ||
|
|
||
| public function getOptions(): array | ||
| { | ||
| return $this->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,58 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace PhpLlm\LlmChain\Bridge\TransformersPHP; | ||
|
|
||
| use Codewithkyrian\Transformers\Pipelines\Pipeline; | ||
| use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
|
||
| final class PipelineResponse implements ResponseInterface | ||
| { | ||
| /** | ||
| * @var array<mixed> | ||
| */ | ||
| private array $result; | ||
|
|
||
| /** | ||
| * @param object|array<mixed>|string $input | ||
| */ | ||
| public function __construct( | ||
| private readonly Pipeline $pipeline, | ||
| private readonly object|array|string $input, | ||
| ) { | ||
| } | ||
|
|
||
| public function getStatusCode(): int | ||
| { | ||
| return 200; | ||
| } | ||
|
|
||
| public function getHeaders(bool $throw = true): array | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| public function getContent(bool $throw = true): string | ||
| { | ||
| throw new \RuntimeException('Not implemented'); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<mixed> | ||
| */ | ||
| public function toArray(bool $throw = true): array | ||
| { | ||
| return $this->result ?? $this->result = ($this->pipeline)($this->input); | ||
| } | ||
|
|
||
| public function cancel(): void | ||
| { | ||
| throw new \RuntimeException('Not implemented'); | ||
| } | ||
|
|
||
| public function getInfo(?string $type = null): mixed | ||
| { | ||
| throw new \RuntimeException('Not implemented'); | ||
| } | ||
| } |
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\Bridge\TransformersPHP; | ||
|
|
||
| use Codewithkyrian\Transformers\Transformers; | ||
| use PhpLlm\LlmChain\Platform; | ||
|
|
||
| final readonly class PlatformFactory | ||
| { | ||
| public static function create(): Platform | ||
| { | ||
| if (!class_exists(Transformers::class)) { | ||
| throw new \RuntimeException('TransformersPHP is not installed. Please install it using "composer require codewithkyrian/transformers".'); | ||
| } | ||
|
|
||
| return new Platform([$handler = new Handler()], [$handler]); | ||
| } | ||
| } |
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.