Skip to content

refactor: simplify transformers implementation by adding specific Platform #314

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

Merged
merged 1 commit into from
May 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions src/Bridge/TransformersPHP/Handler.php

This file was deleted.

59 changes: 0 additions & 59 deletions src/Bridge/TransformersPHP/PipelineResponse.php

This file was deleted.

42 changes: 42 additions & 0 deletions src/Bridge/TransformersPHP/Platform.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace PhpLlm\LlmChain\Bridge\TransformersPHP;

use Codewithkyrian\Transformers\Pipelines\Task;
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
use PhpLlm\LlmChain\Model\Model;
use PhpLlm\LlmChain\Model\Response\ResponseInterface;
use PhpLlm\LlmChain\Model\Response\StructuredResponse;
use PhpLlm\LlmChain\Model\Response\TextResponse;
use PhpLlm\LlmChain\PlatformInterface;

use function Codewithkyrian\Transformers\Pipelines\pipeline;

final class Platform implements PlatformInterface
{
public function request(Model $model, object|array|string $input, array $options = []): ResponseInterface
{
if (null === $task = $options['task'] ?? null) {
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,
);

$data = $pipeline($input);

return match ($task) {
Task::Text2TextGeneration => new TextResponse($data[0]['generated_text']),
default => new StructuredResponse($data),
};
}
}
3 changes: 1 addition & 2 deletions src/Bridge/TransformersPHP/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Codewithkyrian\Transformers\Transformers;
use PhpLlm\LlmChain\Exception\RuntimeException;
use PhpLlm\LlmChain\Platform;

final readonly class PlatformFactory
{
Expand All @@ -16,6 +15,6 @@ public static function create(): Platform
throw new RuntimeException('TransformersPHP is not installed. Please install it using "composer require codewithkyrian/transformers".');
}

return new Platform([$handler = new Handler()], [$handler]);
return new Platform();
}
}