|
3 | 3 | namespace Ayacoo\NewsTldr\Controller; |
4 | 4 |
|
5 | 5 | use Ayacoo\NewsTldr\Event\ModifyChatGptContentEvent; |
| 6 | +use OpenAI; |
6 | 7 | use Psr\EventDispatcher\EventDispatcherInterface; |
7 | 8 | use TYPO3\CMS\Backend\Utility\BackendUtility; |
8 | 9 | use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; |
9 | 10 | use TYPO3\CMS\Core\Http\JsonResponse; |
10 | | -use TYPO3\CMS\Core\Http\RequestFactory; |
11 | 11 | use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
12 | 12 |
|
13 | 13 | class SummarizeController |
14 | 14 | { |
15 | 15 | private const EXTENSION_KEY = 'news_tldr'; |
16 | 16 |
|
17 | 17 | public function __construct( |
18 | | - private readonly RequestFactory $requestFactory, |
19 | 18 | private readonly ExtensionConfiguration $extensionConfiguration, |
20 | 19 | private readonly EventDispatcherInterface $eventDispatcher |
21 | 20 | ) |
@@ -46,49 +45,21 @@ public function updateTeaserAction(): JsonResponse |
46 | 45 | new ModifyChatGptContentEvent($row, $content) |
47 | 46 | ); |
48 | 47 |
|
49 | | - $payload = []; |
50 | | - $payload['model'] = $extConf['model'] ?? ''; |
51 | | - $messages = new \stdClass(); |
52 | | - $messages->role = 'user'; |
53 | | - $messages->content = $modifyChatGptContentEvent->getContent(); |
54 | | - $payload['messages'] = [$messages]; |
55 | | - |
56 | | - $additionalOptions = [ |
57 | | - 'body' => json_encode($payload), |
58 | | - 'headers' => [ |
59 | | - 'Authorization' => 'Bearer ' . $token, |
60 | | - 'Cache-Control' => 'no-cache', |
61 | | - 'Content-Type' => 'application/json' |
| 48 | + $client = OpenAI::client($token); |
| 49 | + $response = $client->chat()->create([ |
| 50 | + 'model' => $extConf['model'], |
| 51 | + 'messages' => [ |
| 52 | + [ |
| 53 | + 'role' => 'user', |
| 54 | + 'content' => $modifyChatGptContentEvent->getContent() |
| 55 | + ], |
62 | 56 | ], |
63 | | - ]; |
64 | | - $response = $this->requestFactory->request( |
65 | | - 'https://api.openai.com/v1/chat/completions', |
66 | | - 'POST', |
67 | | - $additionalOptions |
68 | | - ); |
| 57 | + ]); |
69 | 58 |
|
70 | | - if ($response->getStatusCode() !== 200) { |
71 | | - throw new \RuntimeException( |
72 | | - LocalizationUtility::translate('status_code_is', self::EXTENSION_KEY) . $response->getStatusCode() |
73 | | - ); |
74 | | - } |
75 | | - if ($response->getHeaderLine('Content-Type') !== 'application/json') { |
76 | | - throw new \RuntimeException( |
77 | | - LocalizationUtility::translate('no_valid_json', self::EXTENSION_KEY) |
78 | | - ); |
79 | | - } |
80 | | - $content = $response->getBody()->getContents(); |
81 | | - try { |
82 | | - $result = json_decode($content, true, flags: JSON_THROW_ON_ERROR); |
83 | | - return new JsonResponse([ |
84 | | - 'text' => (string)$result['choices'][0]['message']['content'] ?? '', |
85 | | - 'success' => true |
86 | | - ]); |
87 | | - } catch (\JsonException) { |
88 | | - return new JsonResponse([ |
89 | | - 'text' => LocalizationUtility::translate('no_valid_token', self::EXTENSION_KEY), |
90 | | - 'success' => false |
91 | | - ]); |
92 | | - } |
| 59 | + $result = $response->toArray(); |
| 60 | + return new JsonResponse([ |
| 61 | + 'text' => (string)$result['choices'][0]['message']['content'] ?? '', |
| 62 | + 'success' => true |
| 63 | + ]); |
93 | 64 | } |
94 | 65 | } |
0 commit comments