-
Notifications
You must be signed in to change notification settings - Fork 145
Slide deck generation task provider #4390
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| <?php | ||
| /** | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Richdocuments\Service; | ||
|
|
||
| use OCA\Richdocuments\AppInfo\Application; | ||
| use OCA\Richdocuments\TemplateManager; | ||
| use OCP\IConfig; | ||
| use OCP\TaskProcessing\Exception\Exception; | ||
| use OCP\TaskProcessing\Exception\NotFoundException; | ||
| use OCP\TaskProcessing\Exception\PreConditionNotMetException; | ||
| use OCP\TaskProcessing\Exception\UnauthorizedException; | ||
| use OCP\TaskProcessing\Exception\ValidationException; | ||
| use OCP\TaskProcessing\IManager; | ||
| use OCP\TaskProcessing\Task; | ||
| use OCP\TaskProcessing\TaskTypes\TextToText; | ||
| use RuntimeException; | ||
|
|
||
| class SlideDeckService { | ||
| public const PROMPT = <<<EOF | ||
| Draft a presentation slide deck with headlines and a maximum of 5 bullet points per headline. Use the following JSON structure for your whole output and output only the JSON array, no introductory text: | ||
|
|
||
| ``` | ||
| [{"headline": "Headline 1", points: ["Bullet point 1", "Bullet point 2"]}, {"headline": "Headline 2", points: ["Bullet point 1", "Bullet point 2"]}] | ||
| ``` | ||
|
|
||
| Here is the presentation text: | ||
| EOF; | ||
|
|
||
| public function __construct( | ||
| private IManager $taskProcessingManager, | ||
| private TemplateManager $templateManager, | ||
| private RemoteService $remoteService, | ||
| private IConfig $config, | ||
| ) { | ||
| } | ||
|
|
||
| public function generateSlideDeck(?string $userId, string $presentationText) { | ||
| $rawModelOutput = $this->runLLMQuery($userId, $presentationText); | ||
|
|
||
| $ooxml = $this->config->getAppValue(Application::APPNAME, 'doc_format', '') === 'ooxml'; | ||
| $format = $ooxml ? 'pptx' : 'odp'; | ||
| $emptyPresentation = $this->getBlankPresentation($format); | ||
|
|
||
| try { | ||
| $parsedStructure = $this->parseModelJSON($rawModelOutput); | ||
| } catch (\JsonException) { | ||
| throw new RuntimeException('LLM generated faulty JSON data'); | ||
| } | ||
|
|
||
| try { | ||
| $transformedPresentation = $this->remoteService->transformDocumentStructure( | ||
| 'presentation.' . $format, | ||
| $emptyPresentation, | ||
| $parsedStructure | ||
| ); | ||
|
|
||
| return $transformedPresentation; | ||
| } catch (\Exception) { | ||
| throw new RuntimeException('Unable to apply transformations to presentation file'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Parses the JSON output from the LLM into | ||
| * JSON that Collabora expects | ||
| * | ||
| * @param string $jsonString | ||
| * @return array | ||
| */ | ||
| private function parseModelJSON(string $jsonString): array { | ||
| $modelJSON = json_decode( | ||
| $jsonString, | ||
| associative: true, | ||
| flags: JSON_THROW_ON_ERROR | ||
| ); | ||
|
|
||
| $slideCommands = []; | ||
| foreach ($modelJSON as $index => $slide) { | ||
| if (count($slideCommands) > 0) { | ||
| $slideCommands[] = [ 'JumpToSlide' => 'last' ]; | ||
| $slideCommands[] = [ 'InsertMasterSlide' => 0 ]; | ||
| } else { | ||
| $slideCommands[] = [ 'JumpToSlide' => $index]; | ||
| } | ||
|
|
||
| $slideCommands[] = [ 'ChangeLayoutByName' => 'AUTOLAYOUT_TITLE_CONTENT' ]; | ||
| $slideCommands[] = [ 'SetText.0' => $slide['headline'] ]; | ||
|
|
||
| $editTextObjectCommands = [ | ||
| [ 'SelectParagraph' => 0 ], | ||
| [ 'InsertText' => implode(PHP_EOL, $slide['points']) ], | ||
| ]; | ||
|
|
||
| $slideCommands[] = [ 'EditTextObject.1' => $editTextObjectCommands ]; | ||
| } | ||
|
|
||
| return [ 'SlideCommands' => $slideCommands ]; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a blank presentation file in memory | ||
| * | ||
| * @param string $format | ||
| * @return resource | ||
| */ | ||
| private function getBlankPresentation(string $format) { | ||
| $emptyPresentationContent = $this->templateManager->getEmptyFileContent($format); | ||
| $memoryStream = fopen('php://memory', 'r+'); | ||
|
|
||
| if (!$memoryStream) { | ||
| throw new RuntimeException('Unable to open file stream'); | ||
| } | ||
|
|
||
| fwrite($memoryStream, $emptyPresentationContent); | ||
| rewind($memoryStream); | ||
|
|
||
| return $memoryStream; | ||
| } | ||
|
|
||
| private function runLLMQuery(?string $userId, string $presentationText) { | ||
| $prompt = self::PROMPT; | ||
| $task = new Task( | ||
| TextToText::ID, | ||
| ['input' => $prompt . "\n\n" . $presentationText], | ||
| Application::APPNAME, | ||
| $userId | ||
| ); | ||
|
|
||
| try { | ||
| $this->taskProcessingManager->scheduleTask($task); | ||
| } catch (PreConditionNotMetException|UnauthorizedException|ValidationException|Exception $e) { | ||
| throw new RuntimeException($e->getMessage(), $e->getCode(), $e); | ||
| } | ||
|
|
||
| while (true) { | ||
| try { | ||
| $task = $this->taskProcessingManager->getTask($task->getId()); | ||
| } catch (NotFoundException|Exception $e) { | ||
| throw new RuntimeException($e->getMessage(), $e->getCode(), $e); | ||
| } | ||
| if (in_array($task->getStatus(), [Task::STATUS_SUCCESSFUL, Task::STATUS_FAILED, Task::STATUS_CANCELLED])) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if ($task->getStatus() !== Task::STATUS_SUCCESSFUL) { | ||
| throw new RuntimeException('LLM backend Task with id ' . $task->getId() . ' failed or was cancelled'); | ||
| } | ||
|
|
||
| return $task->getOutput()['output']; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just remembered, Daphne raised to me that this likely should support different languages as well (especially German). Is that feasible? Just using different prompts then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably add an instruction sentence to make sure the model outputs in the same language as the "presentation text" coming from the user.
The "system" prompt can stay in english. We do it for summary, headlines, topics etc... and it works pretty well with most tested models.