2020use OCP \TaskProcessing \ISynchronousProvider ;
2121use OCP \TaskProcessing \ShapeDescriptor ;
2222use RuntimeException ;
23+ use InvalidArgumentException ;
2324
2425class ReformatParagraphsProvider implements ISynchronousProvider {
2526 private const TASK_TYPE_ID = 'core:text2text:reformatparagraphs ' ;
@@ -57,8 +58,14 @@ private function insertParagraphBreaksByAnchors(string $text, array $anchors): s
5758 }
5859
5960 $ insertAt = $ pos + $ delta ;
60- $ result = substr ($ result , 0 , $ insertAt ) . "\n\n" . substr ($ result , $ insertAt );
61- $ delta += 2 ;
61+ // Makes sure to replace newlines and whitespace that already exists at the split
62+ $ replaceFrom = $ insertAt ;
63+ while ($ replaceFrom > 0 && preg_match ('/\s/u ' , $ result [$ replaceFrom - 1 ]) === 1 ) {
64+ $ replaceFrom --;
65+ }
66+ $ result = substr ($ result , 0 , $ replaceFrom ) . "\n\n" . substr ($ result , $ insertAt );
67+ $ delta += 2 - ($ insertAt - $ replaceFrom );
68+
6269 $ searchOffset = $ pos + strlen ($ anchor );
6370 }
6471 return $ result ;
@@ -120,9 +127,7 @@ public function getOptionalInputShapeEnumValues(): array {
120127 }
121128
122129 public function getOptionalInputShapeDefaults (): array {
123- $ adminModel = $ this ->openAiAPIService ->isUsingOpenAi ()
124- ? ($ this ->appConfig ->getValueString (Application::APP_ID , 'default_completion_model_id ' , Application::DEFAULT_MODEL_ID , lazy: true ) ?: Application::DEFAULT_MODEL_ID )
125- : $ this ->appConfig ->getValueString (Application::APP_ID , 'default_completion_model_id ' , lazy: true );
130+ $ adminModel = $ this ->openAiSettingsService ->getAdminDefaultCompletionModelId ();
126131 return [
127132 'max_tokens ' => $ this ->openAiSettingsService ->getMaxTokens (),
128133 'model ' => $ adminModel ,
@@ -145,7 +150,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
145150 $ startTime = time ();
146151
147152 if (!isset ($ input ['input ' ]) || !is_string ($ input ['input ' ])) {
148- throw new RuntimeException ('Invalid prompt ' );
153+ throw new InvalidArgumentException ('Invalid prompt ' );
149154 }
150155 $ prompt = $ input ['input ' ];
151156
@@ -157,7 +162,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
157162 if (isset ($ input ['model ' ]) && is_string ($ input ['model ' ])) {
158163 $ model = $ input ['model ' ];
159164 } else {
160- $ model = $ this ->appConfig -> getValueString (Application:: APP_ID , ' default_completion_model_id ' , Application:: DEFAULT_MODEL_ID , lazy: true ) ?: Application:: DEFAULT_MODEL_ID ;
165+ $ model = $ this ->openAiSettingsService -> getAdminDefaultCompletionModelId () ;
161166 }
162167 $ chunks = $ this ->chunkService ->chunkSplitPrompt ($ prompt , false );
163168 $ result = '' ;
@@ -184,7 +189,7 @@ public function process(?string $userId, array $input, callable $reportProgress)
184189 throw new RuntimeException ('OpenAI/LocalAI request failed: ' . $ e ->getMessage ());
185190 }
186191 if (count ($ completion ) > 0 ) {
187- // The llm only needs to generate the first sentence of each paragraph, and we get the rest of the output from the orginal input.
192+ // The llm only needs to generate the first 8 to 12 words of each paragraph, and we get the rest of the output from the original input.
188193 $ raw = (string )array_pop ($ completion );
189194 $ anchors = $ this ->parseAnchorsFromModelOutput ($ raw );
190195 $ result .= $ this ->insertParagraphBreaksByAnchors ($ chunk , $ anchors );
0 commit comments