-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDecartClient.php
More file actions
91 lines (82 loc) · 3.28 KB
/
Copy pathDecartClient.php
File metadata and controls
91 lines (82 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\AI\Platform\Bridge\Decart;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\ModelClientInterface;
use Symfony\AI\Platform\Result\RawHttpResult;
use Symfony\AI\Platform\Result\RawResultInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @author Guillaume Loulier <personal@guillaumeloulier.fr>
*/
final class DecartClient implements ModelClientInterface
{
private readonly string $baseUrl;
/**
* @param string $baseUrl Base URL of a Decart-compatible endpoint, with or without a trailing slash
*/
public function __construct(
private readonly HttpClientInterface $httpClient,
#[\SensitiveParameter] private readonly string $apiKey,
string $baseUrl = 'https://api.decart.ai/v1',
) {
$this->baseUrl = rtrim($baseUrl, '/');
}
public function supports(Model $model): bool
{
return $model instanceof Decart;
}
public function request(Model $model, array|string $payload, array $options = []): RawResultInterface
{
return match (true) {
\in_array(Capability::TEXT_TO_IMAGE, $model->getCapabilities(), true),
\in_array(Capability::TEXT_TO_VIDEO, $model->getCapabilities(), true) => $this->generate($model, $payload, $options),
\in_array(Capability::IMAGE_TO_IMAGE, $model->getCapabilities(), true),
\in_array(Capability::IMAGE_TO_VIDEO, $model->getCapabilities(), true),
\in_array(Capability::VIDEO_TO_VIDEO, $model->getCapabilities(), true) => $this->edit($model, $payload, $options),
default => throw new InvalidArgumentException(\sprintf('The "%s" model is not supported.', $model->getName())),
};
}
/**
* @param array<string|int, mixed> $payload
* @param array<string, mixed> $options
*/
private function generate(Model $model, array|string $payload, array $options = []): RawResultInterface
{
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/generate/%s', $this->baseUrl, $model->getName()), [
'headers' => [
'x-api-key' => $this->apiKey,
],
'body' => [
'prompt' => \is_string($payload) ? $payload : $payload['text'],
...$options,
],
]));
}
/**
* @param array<string|int, mixed> $payload
* @param array<string, mixed> $options
*/
private function edit(Model $model, array|string $payload, array $options): RawResultInterface
{
return new RawHttpResult($this->httpClient->request('POST', \sprintf('%s/generate/%s', $this->baseUrl, $model->getName()), [
'headers' => [
'x-api-key' => $this->apiKey,
],
'body' => [
'prompt' => $options['prompt'],
'data' => fopen($payload['input_image']['path'] ?? $payload['input_video']['path'], 'r'),
...$options,
],
]));
}
}