Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 #4

Merged
merged 3 commits into from
Oct 25, 2024
Merged

V2 #4

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 15 additions & 44 deletions Classes/Controller/SummarizeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
namespace Ayacoo\NewsTldr\Controller;

use Ayacoo\NewsTldr\Event\ModifyChatGptContentEvent;
use OpenAI;
use Psr\EventDispatcher\EventDispatcherInterface;
use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Http\JsonResponse;
use TYPO3\CMS\Core\Http\RequestFactory;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

class SummarizeController
{
private const EXTENSION_KEY = 'news_tldr';

public function __construct(
private readonly RequestFactory $requestFactory,
private readonly ExtensionConfiguration $extensionConfiguration,
private readonly EventDispatcherInterface $eventDispatcher
)
Expand Down Expand Up @@ -46,49 +45,21 @@ public function updateTeaserAction(): JsonResponse
new ModifyChatGptContentEvent($row, $content)
);

$payload = [];
$payload['model'] = $extConf['model'] ?? '';
$messages = new \stdClass();
$messages->role = 'user';
$messages->content = $modifyChatGptContentEvent->getContent();
$payload['messages'] = [$messages];

$additionalOptions = [
'body' => json_encode($payload),
'headers' => [
'Authorization' => 'Bearer ' . $token,
'Cache-Control' => 'no-cache',
'Content-Type' => 'application/json'
$client = OpenAI::client($token);
$response = $client->chat()->create([
'model' => $extConf['model'],
'messages' => [
[
'role' => 'user',
'content' => $modifyChatGptContentEvent->getContent()
],
],
];
$response = $this->requestFactory->request(
'https://api.openai.com/v1/chat/completions',
'POST',
$additionalOptions
);
]);

if ($response->getStatusCode() !== 200) {
throw new \RuntimeException(
LocalizationUtility::translate('status_code_is', self::EXTENSION_KEY) . $response->getStatusCode()
);
}
if ($response->getHeaderLine('Content-Type') !== 'application/json') {
throw new \RuntimeException(
LocalizationUtility::translate('no_valid_json', self::EXTENSION_KEY)
);
}
$content = $response->getBody()->getContents();
try {
$result = json_decode($content, true, flags: JSON_THROW_ON_ERROR);
return new JsonResponse([
'text' => (string)$result['choices'][0]['message']['content'] ?? '',
'success' => true
]);
} catch (\JsonException) {
return new JsonResponse([
'text' => LocalizationUtility::translate('no_valid_token', self::EXTENSION_KEY),
'success' => false
]);
}
$result = $response->toArray();
return new JsonResponse([
'text' => (string)$result['choices'][0]['message']['content'] ?? '',
'success' => true
]);
}
}
15 changes: 12 additions & 3 deletions Classes/Form/Element/SummarizeFieldElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
namespace Ayacoo\NewsTldr\Form\Element;

use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Backend\Form\NodeFactory;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Imaging\IconSize;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\StringUtility;

class SummarizeFieldElement extends AbstractFormElement
{
Expand All @@ -22,18 +25,24 @@ public function render(): array

$row = $this->data['databaseRow'];

$nodeFactory = GeneralUtility::makeInstance(NodeFactory::class);
$this->injectNodeFactory($nodeFactory);
$fieldInformationResult = $this->renderFieldInformation();
$resultArray = $this->mergeChildReturnIntoExistingResult($this->initializeResultArray(), $fieldInformationResult, false);

$fieldId = StringUtility::getUniqueId('formengine-textarea-');

$html = [];
$html[] = $this->renderLabel($fieldId);
$html[] = '<div class="formengine-field-item t3js-formengine-field-item">';
$html[] = '<div class="form-wizards-wrap">';
$html[] = '<div class="form-wizards-element">';
$html[] = '<div class="form-control-wrap">';

$icon = $this->iconFactory->getIcon(
$iconFactory = GeneralUtility::makeInstance(IconFactory::class);
$icon = $iconFactory->getIcon(
'actions-dice',
Icon::SIZE_SMALL,
IconSize::SMALL,
'overlay-identifier'
);

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ The extension makes it possible to create short news summaries using ChatGPT.

To use this extension, you need the following requirements:

- PHP version 8.1 or higher
- TYPO3 version 12
- PHP version 8.2 or higher
- TYPO3 version 13
- [News][3] Extension 11 or higher
- [ChatGPT API Token][2] (Please note the number of tokens and costs)

Expand Down Expand Up @@ -68,7 +68,7 @@ class ContentListener
$row = $event->getRow()
$text = strip_tags($row['bodytext'] ?? '');

$content = 'Fasse mir diesen Text in 100 Zeichen zusammen: ' . $text;
$content = 'Summarise this text for me in 100 characters: ' . $text;
$event->setContent($content);

return $event;
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ayacoo/news-tldr",
"version": "1.0.1",
"version": "2.0.0",
"type": "typo3-cms-extension",
"description": "Creates a short summary for news via ChatGPT",
"homepage": "https://www.ayacoo.de",
Expand All @@ -14,9 +14,10 @@
"GPL-2.0-or-later"
],
"require": {
"php": ">=8.1",
"typo3/cms-core": "^12.4",
"georgringer/news": "^11.0"
"php": ">=8.2 < 8.4",
"typo3/cms-core": "^13.4",
"georgringer/news": "^12.0",
"openai-php/client": "*"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
'category' => 'plugin',
'constraints' => [
'depends' => [
'typo3' => '12.3.0-12.9.99',
'news' => '11.0.0-11.9.99',
'typo3' => '13.0.0-13.4.99',
'news' => '12.0.0-12.9.99',
],
'conflicts' => [],
'suggests' => [],
Expand All @@ -17,5 +17,5 @@
'author' => 'Guido Schmechel',
'author_email' => 'info@ayacoo.de',
'author_company' => 'ayacoo',
'version' => '1.0.1',
'version' => '2.0.0',
];