mirror of
https://github.com/symfony/ai-scaleway-platform.git
synced 2026-03-23 23:32:07 +01:00
53 lines
1.7 KiB
PHP
53 lines
1.7 KiB
PHP
<?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\Scaleway\Llm;
|
|
|
|
use Symfony\AI\Platform\Bridge\Scaleway\Scaleway;
|
|
use Symfony\AI\Platform\Exception\InvalidArgumentException;
|
|
use Symfony\AI\Platform\Model;
|
|
use Symfony\AI\Platform\ModelClientInterface;
|
|
use Symfony\AI\Platform\Result\RawHttpResult;
|
|
use Symfony\Component\HttpClient\EventSourceHttpClient;
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
/**
|
|
* @author Marcus Stöhr <marcus@fischteich.net>
|
|
*/
|
|
final class ModelClient implements ModelClientInterface
|
|
{
|
|
private readonly EventSourceHttpClient $httpClient;
|
|
|
|
public function __construct(
|
|
HttpClientInterface $httpClient,
|
|
#[\SensitiveParameter] private readonly string $apiKey,
|
|
) {
|
|
$this->httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);
|
|
}
|
|
|
|
public function supports(Model $model): bool
|
|
{
|
|
return $model instanceof Scaleway;
|
|
}
|
|
|
|
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));
|
|
}
|
|
|
|
return new RawHttpResult($this->httpClient->request('POST', 'https://api.scaleway.ai/v1/chat/completions', [
|
|
'auth_bearer' => $this->apiKey,
|
|
'json' => array_merge($options, $payload),
|
|
]));
|
|
}
|
|
}
|