|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Platform\Bridge\Scaleway\Llm; |
| 13 | + |
| 14 | +use Symfony\AI\Platform\Bridge\Scaleway\Scaleway; |
| 15 | +use Symfony\AI\Platform\Exception\ContentFilterException; |
| 16 | +use Symfony\AI\Platform\Exception\RuntimeException; |
| 17 | +use Symfony\AI\Platform\Model; |
| 18 | +use Symfony\AI\Platform\Result\ChoiceResult; |
| 19 | +use Symfony\AI\Platform\Result\RawHttpResult; |
| 20 | +use Symfony\AI\Platform\Result\RawResultInterface; |
| 21 | +use Symfony\AI\Platform\Result\ResultInterface; |
| 22 | +use Symfony\AI\Platform\Result\StreamResult; |
| 23 | +use Symfony\AI\Platform\Result\TextResult; |
| 24 | +use Symfony\AI\Platform\Result\ToolCall; |
| 25 | +use Symfony\AI\Platform\Result\ToolCallResult; |
| 26 | +use Symfony\AI\Platform\ResultConverterInterface as PlatformResultConverter; |
| 27 | +use Symfony\Component\HttpClient\Chunk\ServerSentEvent; |
| 28 | +use Symfony\Component\HttpClient\EventSourceHttpClient; |
| 29 | +use Symfony\Component\HttpClient\Exception\JsonException; |
| 30 | +use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author Marcus Stöhr <[email protected]> |
| 34 | + */ |
| 35 | +final class ResultConverter implements PlatformResultConverter |
| 36 | +{ |
| 37 | + public function supports(Model $model): bool |
| 38 | + { |
| 39 | + return $model instanceof Scaleway; |
| 40 | + } |
| 41 | + |
| 42 | + public function convert(RawResultInterface|RawHttpResult $result, array $options = []): ResultInterface |
| 43 | + { |
| 44 | + if ($options['stream'] ?? false) { |
| 45 | + return new StreamResult($this->convertStream($result->getObject())); |
| 46 | + } |
| 47 | + $data = $result->getData(); |
| 48 | + |
| 49 | + if (isset($data['error']['code']) && 'content_filter' === $data['error']['code']) { |
| 50 | + throw new ContentFilterException($data['error']['message']); |
| 51 | + } |
| 52 | + |
| 53 | + if (!isset($data['choices'])) { |
| 54 | + throw new RuntimeException('Result does not contain choices.'); |
| 55 | + } |
| 56 | + |
| 57 | + $choices = array_map($this->convertChoice(...), $data['choices']); |
| 58 | + |
| 59 | + return 1 === \count($choices) ? $choices[0] : new ChoiceResult(...$choices); |
| 60 | + } |
| 61 | + |
| 62 | + private function convertStream(HttpResponse $result): \Generator |
| 63 | + { |
| 64 | + $toolCalls = []; |
| 65 | + foreach ((new EventSourceHttpClient())->stream($result) as $chunk) { |
| 66 | + if (!$chunk instanceof ServerSentEvent || '[DONE]' === $chunk->getData()) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + $data = $chunk->getArrayData(); |
| 72 | + } catch (JsonException) { |
| 73 | + // try catch only needed for Symfony 6.4 |
| 74 | + continue; |
| 75 | + } |
| 76 | + |
| 77 | + if ($this->streamIsToolCall($data)) { |
| 78 | + $toolCalls = $this->convertStreamToToolCalls($toolCalls, $data); |
| 79 | + } |
| 80 | + |
| 81 | + if ([] !== $toolCalls && $this->isToolCallsStreamFinished($data)) { |
| 82 | + yield new ToolCallResult(...array_map($this->convertToolCall(...), $toolCalls)); |
| 83 | + } |
| 84 | + |
| 85 | + if (!isset($data['choices'][0]['delta']['content'])) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + yield $data['choices'][0]['delta']['content']; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param array<string, mixed> $toolCalls |
| 95 | + * @param array<string, mixed> $data |
| 96 | + * |
| 97 | + * @return array<string, mixed> |
| 98 | + */ |
| 99 | + private function convertStreamToToolCalls(array $toolCalls, array $data): array |
| 100 | + { |
| 101 | + if (!isset($data['choices'][0]['delta']['tool_calls'])) { |
| 102 | + return $toolCalls; |
| 103 | + } |
| 104 | + |
| 105 | + foreach ($data['choices'][0]['delta']['tool_calls'] as $i => $toolCall) { |
| 106 | + if (isset($toolCall['id'])) { |
| 107 | + // initialize tool call |
| 108 | + $toolCalls[$i] = [ |
| 109 | + 'id' => $toolCall['id'], |
| 110 | + 'function' => $toolCall['function'], |
| 111 | + ]; |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + // add arguments delta to tool call |
| 116 | + $toolCalls[$i]['function']['arguments'] .= $toolCall['function']['arguments']; |
| 117 | + } |
| 118 | + |
| 119 | + return $toolCalls; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @param array<string, mixed> $data |
| 124 | + */ |
| 125 | + private function streamIsToolCall(array $data): bool |
| 126 | + { |
| 127 | + return isset($data['choices'][0]['delta']['tool_calls']); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param array<string, mixed> $data |
| 132 | + */ |
| 133 | + private function isToolCallsStreamFinished(array $data): bool |
| 134 | + { |
| 135 | + return isset($data['choices'][0]['finish_reason']) && 'tool_calls' === $data['choices'][0]['finish_reason']; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @param array{ |
| 140 | + * index: int, |
| 141 | + * message: array{ |
| 142 | + * role: 'assistant', |
| 143 | + * content: ?string, |
| 144 | + * tool_calls: array{ |
| 145 | + * id: string, |
| 146 | + * type: 'function', |
| 147 | + * function: array{ |
| 148 | + * name: string, |
| 149 | + * arguments: string |
| 150 | + * }, |
| 151 | + * }, |
| 152 | + * refusal: ?mixed |
| 153 | + * }, |
| 154 | + * logprobs: string, |
| 155 | + * finish_reason: 'stop'|'length'|'tool_calls'|'content_filter', |
| 156 | + * } $choice |
| 157 | + */ |
| 158 | + private function convertChoice(array $choice): ToolCallResult|TextResult |
| 159 | + { |
| 160 | + if ('tool_calls' === $choice['finish_reason']) { |
| 161 | + return new ToolCallResult(...array_map([$this, 'convertToolCall'], $choice['message']['tool_calls'])); |
| 162 | + } |
| 163 | + |
| 164 | + if (\in_array($choice['finish_reason'], ['stop', 'length'], true)) { |
| 165 | + return new TextResult($choice['message']['content']); |
| 166 | + } |
| 167 | + |
| 168 | + throw new RuntimeException(\sprintf('Unsupported finish reason "%s".', $choice['finish_reason'])); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * @param array{ |
| 173 | + * id: string, |
| 174 | + * type: 'function', |
| 175 | + * function: array{ |
| 176 | + * name: string, |
| 177 | + * arguments: string |
| 178 | + * } |
| 179 | + * } $toolCall |
| 180 | + */ |
| 181 | + private function convertToolCall(array $toolCall): ToolCall |
| 182 | + { |
| 183 | + $arguments = json_decode($toolCall['function']['arguments'], true, \JSON_THROW_ON_ERROR); |
| 184 | + |
| 185 | + return new ToolCall($toolCall['id'], $toolCall['function']['name'], $arguments); |
| 186 | + } |
| 187 | +} |
0 commit comments