|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 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\Perplexity; |
| 13 | + |
| 14 | +use Symfony\AI\Platform\Exception\ContentFilterException; |
| 15 | +use Symfony\AI\Platform\Exception\RuntimeException; |
| 16 | +use Symfony\AI\Platform\Model; |
| 17 | +use Symfony\AI\Platform\Result\ChoiceResult; |
| 18 | +use Symfony\AI\Platform\Result\RawHttpResult; |
| 19 | +use Symfony\AI\Platform\Result\RawResultInterface; |
| 20 | +use Symfony\AI\Platform\Result\ResultInterface; |
| 21 | +use Symfony\AI\Platform\Result\StreamResult; |
| 22 | +use Symfony\AI\Platform\Result\TextResult; |
| 23 | +use Symfony\AI\Platform\Result\ToolCall; |
| 24 | +use Symfony\AI\Platform\Result\ToolCallResult; |
| 25 | +use Symfony\AI\Platform\ResultConverterInterface as PlatformResponseConverter; |
| 26 | +use Symfony\Component\HttpClient\Chunk\ServerSentEvent; |
| 27 | +use Symfony\Component\HttpClient\EventSourceHttpClient; |
| 28 | +use Symfony\Component\HttpClient\Exception\JsonException; |
| 29 | +use Symfony\Contracts\HttpClient\ResponseInterface as HttpResponse; |
| 30 | + |
| 31 | +/** |
| 32 | + * @author Mathieu Santostefano <msantostefano@proton.me> |
| 33 | + */ |
| 34 | +final class ResultConverter implements PlatformResponseConverter |
| 35 | +{ |
| 36 | + public function supports(Model $model): bool |
| 37 | + { |
| 38 | + return $model instanceof Perplexity; |
| 39 | + } |
| 40 | + |
| 41 | + public function convert(RawResultInterface|RawHttpResult $result, array $options = []): ResultInterface |
| 42 | + { |
| 43 | + if ($options['stream'] ?? false) { |
| 44 | + return new StreamResult($this->convertStream($result->getObject())); |
| 45 | + } |
| 46 | + |
| 47 | + $data = $result->getData(); |
| 48 | + dump($data); |
| 49 | + |
| 50 | + if (!isset($data['choices'])) { |
| 51 | + throw new RuntimeException('Response does not contain choices.'); |
| 52 | + } |
| 53 | + |
| 54 | + $choices = array_map($this->convertChoice(...), $data['choices']); |
| 55 | + |
| 56 | + $result = 1 === \count($choices) ? $choices[0] : new ChoiceResult(...$choices); |
| 57 | + |
| 58 | + if (isset($data['citations'])) { |
| 59 | + $citations = $this->convertCitations($data['citations']); |
| 60 | + } |
| 61 | + |
| 62 | + if (isset($data['search_results'])) { |
| 63 | + $searchResults = $this->convertSearchResults($data['search_results']); |
| 64 | + } |
| 65 | + |
| 66 | + return $result; |
| 67 | + } |
| 68 | + |
| 69 | + private function convertStream(HttpResponse $result): \Generator |
| 70 | + { |
| 71 | + foreach ((new EventSourceHttpClient())->stream($result) as $chunk) { |
| 72 | + if (!$chunk instanceof ServerSentEvent || '[DONE]' === $chunk->getData()) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + try { |
| 77 | + $data = $chunk->getArrayData(); |
| 78 | + } catch (JsonException) { |
| 79 | + // try catch only needed for Symfony 6.4 |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + if (!isset($data['choices'][0]['delta']['content'])) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + yield $data['choices'][0]['delta']['content']; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param array{ |
| 93 | + * index: int, |
| 94 | + * message: array{ |
| 95 | + * role: 'assistant', |
| 96 | + * content: ?string |
| 97 | + * }, |
| 98 | + * delta: array{ |
| 99 | + * role: 'assistant', |
| 100 | + * content: string, |
| 101 | + * finish_reason: 'stop'|'length', |
| 102 | + * } $choice |
| 103 | + */ |
| 104 | + private function convertChoice(array $choice): TextResult |
| 105 | + { |
| 106 | + if (\in_array($choice['finish_reason'], ['stop', 'length'], true)) { |
| 107 | + return new TextResult($choice['message']['content']); |
| 108 | + } |
| 109 | + |
| 110 | + throw new RuntimeException(\sprintf('Unsupported finish reason "%s".', $choice['finish_reason'])); |
| 111 | + } |
| 112 | + |
| 113 | + private function convertCitations(array $citations): array |
| 114 | + { |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + private function convertSearchResults(array $searchResults): array |
| 119 | + { |
| 120 | + |
| 121 | + } |
| 122 | +} |
0 commit comments