diff --git a/lib/TaskProcessing/SummaryProvider.php b/lib/TaskProcessing/SummaryProvider.php index 90335f55..ab9dce24 100644 --- a/lib/TaskProcessing/SummaryProvider.php +++ b/lib/TaskProcessing/SummaryProvider.php @@ -19,6 +19,7 @@ use OCP\TaskProcessing\Exception\UserFacingProcessingException; use OCP\TaskProcessing\ISynchronousProvider; use OCP\TaskProcessing\ShapeDescriptor; +use OCP\TaskProcessing\ShapeEnumValue; use OCP\TaskProcessing\TaskTypes\TextToTextSummary; class SummaryProvider implements ISynchronousProvider { @@ -58,6 +59,16 @@ public function getInputShapeDefaults(): array { public function getOptionalInputShape(): array { return [ + 'format' => new ShapeDescriptor( + $this->l->t('Format'), + $this->l->t('The format of the summary'), + EShapeType::Enum + ), + 'complexity' => new ShapeDescriptor( + $this->l->t('Complexity'), + $this->l->t('The complexity of the summary'), + EShapeType::Enum + ), 'max_tokens' => new ShapeDescriptor( $this->l->t('Maximum output words'), $this->l->t('The maximum number of words/tokens that can be generated in the completion.'), @@ -74,6 +85,17 @@ public function getOptionalInputShape(): array { public function getOptionalInputShapeEnumValues(): array { return [ 'model' => $this->openAiAPIService->getModelEnumValues($this->userId), + 'format' => [ + new ShapeEnumValue($this->l->t('Auto'), 'auto'), + new ShapeEnumValue($this->l->t('One Sentence'), 'sentence'), + new ShapeEnumValue($this->l->t('One Paragraph'), 'paragraph'), + new ShapeEnumValue($this->l->t('Bullet Points'), 'bullet_points') + ], + 'complexity' => [ + new ShapeEnumValue($this->l->t('Simple'), 'simple'), + new ShapeEnumValue($this->l->t('Medium'), 'medium'), + new ShapeEnumValue($this->l->t('Complex'), 'complex') + ], ]; } @@ -82,6 +104,8 @@ public function getOptionalInputShapeDefaults(): array { return [ 'max_tokens' => $this->openAiSettingsService->getMaxTokens(), 'model' => $adminModel, + 'format' => 'auto', + 'complexity' => 'medium', ]; } @@ -129,9 +153,25 @@ public function process(?string $userId, array $input, callable $reportProgress) try { $completions = []; + $summarySystemPrompt = 'You are a helpful assistant that summarizes text in the same language as the text. ' + . 'You should only return the summary without any additional information. '; + if (isset($input['format'])) { + if ($input['format'] === 'paragraph') { + $summarySystemPrompt .= 'Return the summary as a paragraph. '; + } elseif ($input['format'] === 'bullet_points') { + $summarySystemPrompt .= 'Return the summary as a list of bullet points. '; + } elseif ($input['format'] === 'sentence') { + $summarySystemPrompt .= 'Return the summary as a single sentence. Do not include more than one sentence. '; + } + } + if (isset($input['complexity'])) { + if ($input['complexity'] === 'complex') { + $summarySystemPrompt .= 'Use complex language and vocabulary appropriate for an expert in the subject. '; + } elseif ($input['complexity'] === 'simple') { + $summarySystemPrompt .= 'Use simple language and vocabulary appropriate for a 5 year old. '; + } + } if ($this->openAiAPIService->isUsingOpenAi() || $this->openAiSettingsService->getChatEndpointEnabled()) { - $summarySystemPrompt = 'You are a helpful assistant that summarizes text in the same language as the text. ' - . 'You should only return the summary without any additional information.'; foreach ($prompts as $p) { $completion = $this->openAiAPIService->createChatCompletion($userId, $model, $p, $summarySystemPrompt, null, 1, $maxTokens); @@ -143,11 +183,8 @@ public function process(?string $userId, array $input, callable $reportProgress) } } } else { - $wrapSummaryPrompt = function (string $p): string { - return 'You are a helpful assistant that summarizes text in the same language as the text. ' - . 'You should only return the summary without any additional information. ' - . 'Here is the text to summarize:\n\n' . $p . '\n'; - }; + $wrapSummaryPrompt = static fn (string $p) => $summarySystemPrompt + . 'Here is the text to summarize:\n\n' . $p . '\n'; foreach (array_map($wrapSummaryPrompt, $prompts) as $p) { $completions[] = $this->openAiAPIService->createCompletion($userId, $p, 1, $model, $maxTokens); diff --git a/tests/unit/Providers/OpenAiProviderTest.php b/tests/unit/Providers/OpenAiProviderTest.php index 0dab97aa..a42ba7b5 100644 --- a/tests/unit/Providers/OpenAiProviderTest.php +++ b/tests/unit/Providers/OpenAiProviderTest.php @@ -534,7 +534,7 @@ public function testSummaryProvider(): void { $options = ['timeout' => Application::OPENAI_DEFAULT_REQUEST_TIMEOUT, 'headers' => ['User-Agent' => Application::USER_AGENT, 'Authorization' => self::AUTHORIZATION_HEADER, 'Content-Type' => 'application/json']]; $systemPrompt = 'You are a helpful assistant that summarizes text in the same language as the text. ' - . 'You should only return the summary without any additional information.'; + . 'You should only return the summary without any additional information. '; $options['body'] = json_encode([ 'model' => Application::DEFAULT_COMPLETION_MODEL_ID, 'messages' => [