-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelClient.php
More file actions
68 lines (56 loc) · 2.38 KB
/
ModelClient.php
File metadata and controls
68 lines (56 loc) · 2.38 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
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\AI\Platform\Bridge\OpenResponses;
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\StructuredOutput\PlatformSubscriber;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @author Christopher Hertel <[email protected]>
*/
class ModelClient implements ModelClientInterface
{
private readonly EventSourceHttpClient $httpClient;
public function __construct(
HttpClientInterface $httpClient,
private readonly string $baseUrl,
#[\SensitiveParameter] private readonly ?string $apiKey = null,
private readonly string $path = '/v1/responses',
) {
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
}
public function supports(Model $model): bool
{
return $model instanceof ResponsesModel;
}
public function request(Model $model, array|string $payload, array $options = []): RawHttpResult
{
if (\is_string($payload)) {
throw new InvalidArgumentException(\sprintf('Payload must be an array, but a string was given to "%s".', self::class));
}
if (isset($options[PlatformSubscriber::RESPONSE_FORMAT]['json_schema']['schema'])) {
$schema = $options[PlatformSubscriber::RESPONSE_FORMAT]['json_schema'];
$options['text']['format'] = $schema;
$options['text']['format']['name'] = $schema['name'];
$options['text']['format']['type'] = $options[PlatformSubscriber::RESPONSE_FORMAT]['type'];
unset($options[PlatformSubscriber::RESPONSE_FORMAT]);
}
$requestOptions = [
'json' => array_merge($options, ['model' => $model->getName()], $payload),
];
if (null !== $this->apiKey) {
$requestOptions['auth_bearer'] = $this->apiKey;
}
return new RawHttpResult($this->httpClient->request('POST', $this->baseUrl.$this->path, $requestOptions));
}
}