|
11 | 11 |
|
12 | 12 | namespace Symfony\AI\Platform\Bridge\OpenRouter\Completions; |
13 | 13 |
|
14 | | -use Symfony\AI\Platform\Exception\AuthenticationException; |
15 | | -use Symfony\AI\Platform\Exception\BadRequestException; |
16 | | -use Symfony\AI\Platform\Exception\RateLimitExceededException; |
17 | | -use Symfony\AI\Platform\Exception\RuntimeException; |
| 14 | +use Symfony\AI\Platform\Bridge\OpenAi\Gpt\ResultConverter as OpenAiResponseConverter; |
18 | 15 | use Symfony\AI\Platform\Model; |
19 | 16 | use Symfony\AI\Platform\Result\RawResultInterface; |
20 | 17 | use Symfony\AI\Platform\Result\ResultInterface; |
21 | | -use Symfony\AI\Platform\Result\TextResult; |
22 | 18 | use Symfony\AI\Platform\ResultConverterInterface; |
23 | 19 |
|
24 | 20 | /** |
25 | 21 | * @author rglozman |
26 | 22 | */ |
27 | 23 | final class ResultConverter implements ResultConverterInterface |
28 | 24 | { |
| 25 | + public function __construct( |
| 26 | + private readonly OpenAiResponseConverter $gptResponseConverter = new OpenAiResponseConverter(), |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
29 | 30 | public function supports(Model $model): bool |
30 | 31 | { |
31 | 32 | return true; |
32 | 33 | } |
33 | 34 |
|
34 | 35 | public function convert(RawResultInterface $result, array $options = []): ResultInterface |
35 | 36 | { |
36 | | - $response = $result->getObject(); |
37 | | - $data = $result->getData(); |
38 | | - |
39 | | - if (401 === $response->getStatusCode()) { |
40 | | - $errorMessage = json_decode($response->getContent(false), true)['error']['message']; |
41 | | - throw new AuthenticationException($errorMessage); |
42 | | - } |
43 | | - |
44 | | - if (400 === $response->getStatusCode() || 404 === $response->getStatusCode()) { |
45 | | - $errorMessage = json_decode($response->getContent(false), true)['error']['message'] ?? 'Bad Request'; |
46 | | - throw new BadRequestException($errorMessage); |
47 | | - } |
48 | | - |
49 | | - if (429 === $response->getStatusCode()) { |
50 | | - $errorMessage = json_decode($response->getContent(false), true)['error']['message'] ?? 'Bad Request'; |
51 | | - throw new RateLimitExceededException($errorMessage); |
52 | | - } |
53 | | - |
54 | | - if (!isset($data['choices'][0]['message'])) { |
55 | | - throw new RuntimeException('Response does not contain message.'); |
56 | | - } |
57 | | - |
58 | | - if (!isset($data['choices'][0]['message']['content'])) { |
59 | | - throw new RuntimeException('Message does not contain content.'); |
60 | | - } |
61 | | - |
62 | | - return new TextResult($data['choices'][0]['message']['content']); |
| 37 | + return $this->gptResponseConverter->convert($result, $options); |
63 | 38 | } |
64 | 39 | } |
0 commit comments