Skip to content

Commit be5d914

Browse files
authored
refactor: simplify transformers implementation by adding specific Platform (#314)
Similar to #312
1 parent 8157e7d commit be5d914

File tree

4 files changed

+43
-120
lines changed

4 files changed

+43
-120
lines changed

src/Bridge/TransformersPHP/Handler.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/Bridge/TransformersPHP/PipelineResponse.php

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpLlm\LlmChain\Bridge\TransformersPHP;
6+
7+
use Codewithkyrian\Transformers\Pipelines\Task;
8+
use PhpLlm\LlmChain\Exception\InvalidArgumentException;
9+
use PhpLlm\LlmChain\Model\Model;
10+
use PhpLlm\LlmChain\Model\Response\ResponseInterface;
11+
use PhpLlm\LlmChain\Model\Response\StructuredResponse;
12+
use PhpLlm\LlmChain\Model\Response\TextResponse;
13+
use PhpLlm\LlmChain\PlatformInterface;
14+
15+
use function Codewithkyrian\Transformers\Pipelines\pipeline;
16+
17+
final class Platform implements PlatformInterface
18+
{
19+
public function request(Model $model, object|array|string $input, array $options = []): ResponseInterface
20+
{
21+
if (null === $task = $options['task'] ?? null) {
22+
throw new InvalidArgumentException('The task option is required.');
23+
}
24+
25+
$pipeline = pipeline(
26+
$options['task'],
27+
$model->getName(),
28+
$options['quantized'] ?? true,
29+
$options['config'] ?? null,
30+
$options['cacheDir'] ?? null,
31+
$options['revision'] ?? 'main',
32+
$options['modelFilename'] ?? null,
33+
);
34+
35+
$data = $pipeline($input);
36+
37+
return match ($task) {
38+
Task::Text2TextGeneration => new TextResponse($data[0]['generated_text']),
39+
default => new StructuredResponse($data),
40+
};
41+
}
42+
}

src/Bridge/TransformersPHP/PlatformFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Codewithkyrian\Transformers\Transformers;
88
use PhpLlm\LlmChain\Exception\RuntimeException;
9-
use PhpLlm\LlmChain\Platform;
109

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

19-
return new Platform([$handler = new Handler()], [$handler]);
18+
return new Platform();
2019
}
2120
}

0 commit comments

Comments
 (0)