-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelCatalog.php
More file actions
106 lines (89 loc) · 3.75 KB
/
Copy pathModelCatalog.php
File metadata and controls
106 lines (89 loc) · 3.75 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?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\Deepgram;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\Exception\ModelNotFoundException;
use Symfony\AI\Platform\Exception\RuntimeException;
use Symfony\AI\Platform\ModelCatalog\ModelCatalogInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\HttpExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Resolves Deepgram models from the live `/v1/models` endpoint.
*
* @author Guillaume Loulier <personal@guillaumeloulier.fr>
*/
final class ModelCatalog implements ModelCatalogInterface
{
/**
* @var array<string, array{class: class-string<Deepgram>, capabilities: list<Capability>}>|null
*/
private ?array $models = null;
public function __construct(
private readonly HttpClientInterface $httpClient,
) {
}
public function getModel(string $modelName): Deepgram
{
if ('' === $modelName) {
throw new InvalidArgumentException('Model name cannot be empty.');
}
$models = $this->getModels();
if (!isset($models[$modelName])) {
throw new ModelNotFoundException(\sprintf('Model "%s" does not exist.', $modelName));
}
return new Deepgram($modelName, $models[$modelName]['capabilities']);
}
public function getModels(): array
{
if (null !== $this->models) {
return $this->models;
}
try {
$payload = $this->httpClient->request('GET', 'models')->toArray();
} catch (HttpExceptionInterface $exception) {
throw new RuntimeException(\sprintf('Deepgram returned status "%d" while listing models.', $exception->getResponse()->getStatusCode()), 0, $exception);
} catch (DecodingExceptionInterface $exception) {
throw new RuntimeException('Deepgram returned a malformed JSON payload while listing models.', 0, $exception);
} catch (TransportExceptionInterface $exception) {
throw new RuntimeException('Could not reach the Deepgram API to fetch the model catalog.', 0, $exception);
}
$capabilities = [
'tts' => [Capability::INPUT_TEXT, Capability::TEXT_TO_SPEECH, Capability::OUTPUT_AUDIO],
'stt' => [Capability::INPUT_AUDIO, Capability::SPEECH_TO_TEXT, Capability::OUTPUT_TEXT],
];
$models = [];
foreach ($capabilities as $type => $typeCapabilities) {
$entries = $payload[$type] ?? [];
if (!\is_array($entries)) {
continue;
}
foreach ($entries as $entry) {
if (!\is_array($entry)) {
continue;
}
$identifiers = [$entry['name'] ?? null, $entry['canonical_name'] ?? null];
if ('stt' === $type) {
// /v1/listen accepts the architecture (e.g. "nova-3") as a model alias,
// while /v1/speak requires a full voice name
$identifiers[] = $entry['architecture'] ?? null;
}
foreach ($identifiers as $name) {
if (\is_string($name) && '' !== $name) {
$models[$name] = ['class' => Deepgram::class, 'capabilities' => $typeCapabilities];
}
}
}
}
return $this->models = $models;
}
}