1111
1212namespace Symfony \AI \Platform \Bridge \OpenAi \Gpt ;
1313
14+ use Symfony \AI \Platform \Bridge \OpenAi \AbstractModelClient ;
1415use Symfony \AI \Platform \Bridge \OpenAi \Gpt ;
15- use Symfony \AI \Platform \Exception \InvalidArgumentException ;
1616use Symfony \AI \Platform \Model ;
17- use Symfony \AI \Platform \ModelClientInterface as PlatformResponseFactory ;
17+ use Symfony \AI \Platform \ModelClientInterface ;
1818use Symfony \AI \Platform \Result \RawHttpResult ;
1919use Symfony \Component \HttpClient \EventSourceHttpClient ;
2020use Symfony \Contracts \HttpClient \HttpClientInterface ;
2121
2222/**
2323 * @author Christopher Hertel <mail@christopher-hertel.de>
2424 */
25- final readonly class ModelClient implements PlatformResponseFactory
25+ final readonly class ModelClient extends AbstractModelClient implements ModelClientInterface
2626{
2727 private EventSourceHttpClient $ httpClient ;
2828
2929 public function __construct (
3030 HttpClientInterface $ httpClient ,
3131 #[\SensitiveParameter] private string $ apiKey ,
32+ private ?string $ region = null ,
3233 ) {
3334 $ this ->httpClient = $ httpClient instanceof EventSourceHttpClient ? $ httpClient : new EventSourceHttpClient ($ httpClient );
34- if ('' === $ apiKey ) {
35- throw new InvalidArgumentException ('The API key must not be empty. ' );
36- }
37- if (!str_starts_with ($ apiKey , 'sk- ' )) {
38- throw new InvalidArgumentException ('The API key must start with "sk-". ' );
39- }
35+ self ::validateApiKey ($ apiKey );
4036 }
4137
4238 public function supports (Model $ model ): bool
@@ -46,7 +42,7 @@ public function supports(Model $model): bool
4642
4743 public function request (Model $ model , array |string $ payload , array $ options = []): RawHttpResult
4844 {
49- return new RawHttpResult ($ this ->httpClient ->request ('POST ' , ' https://api.openai.com /v1/chat/completions ' , [
45+ return new RawHttpResult ($ this ->httpClient ->request ('POST ' , self :: getBaseUrl ( $ this -> region ). ' /v1/chat/completions ' , [
5046 'auth_bearer ' => $ this ->apiKey ,
5147 'json ' => array_merge ($ options , $ payload ),
5248 ]));
0 commit comments