diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 9a785cc5..cf66678c 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -12,6 +12,8 @@ use OCA\OpenAi\Capabilities; use OCA\OpenAi\TaskProcessing\ContextWriteProvider; use OCA\OpenAi\TaskProcessing\HeadlineProvider; +use OCA\OpenAi\TaskProcessing\ReformulateProvider; +use OCA\OpenAi\TaskProcessing\ReformulateTaskType; use OCA\OpenAi\TaskProcessing\STTProvider; use OCA\OpenAi\TaskProcessing\SummaryProvider; use OCA\OpenAi\TaskProcessing\TextToImageProvider; @@ -83,6 +85,8 @@ public function register(IRegistrationContext $context): void { $context->registerTaskProcessingProvider(HeadlineProvider::class); $context->registerTaskProcessingProvider(TopicsProvider::class); $context->registerTaskProcessingProvider(ContextWriteProvider::class); + $context->registerTaskProcessingTaskType(ReformulateTaskType::class); + $context->registerTaskProcessingProvider(ReformulateProvider::class); } if ($this->config->getAppValue(Application::APP_ID, 't2i_provider_enabled', '1') === '1') { $context->registerTaskProcessingProvider(TextToImageProvider::class); diff --git a/lib/TaskProcessing/ReformulateProvider.php b/lib/TaskProcessing/ReformulateProvider.php new file mode 100644 index 00000000..011d1ce8 --- /dev/null +++ b/lib/TaskProcessing/ReformulateProvider.php @@ -0,0 +1,106 @@ +openAiAPIService->getServiceName(); + } + + public function getTaskTypeId(): string { + return ReformulateTaskType::ID; + } + + public function getExpectedRuntime(): int { + return $this->openAiAPIService->getExpTextProcessingTime(); + } + + public function getOptionalInputShape(): array { + return [ + 'temperature' => new ShapeDescriptor( + $this->l->t('Temperature'), + $this->l->t('What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.'), + EShapeType::Number + ), + 'max_tokens' => new ShapeDescriptor( + $this->l->t('Maximum tokens'), + $this->l->t('The maximum number of tokens that can be generated in the completion.'), + EShapeType::Number + ), + ]; + } + + public function getOptionalOutputShape(): array { + return []; + } + + public function process(?string $userId, array $input, callable $reportProgress): array { + $startTime = time(); + $adminModel = $this->config->getAppValue(Application::APP_ID, 'default_completion_model_id', Application::DEFAULT_COMPLETION_MODEL_ID) ?: Application::DEFAULT_COMPLETION_MODEL_ID; + + if (!isset($input['input']) || !is_string($input['input'])) { + throw new RuntimeException('Invalid prompt'); + } + $prompt = $input['input']; + $prompt = 'Reformulate the following text. Use the same language as the original text. Output only the reformulation. Here is the text:' . "\n\n" . $prompt . "\n\n" . 'Here is your reformulation in the same language:'; + + $temperature = null; + if (isset($input['temperature']) + && (is_float($input['temperature']) || is_int($input['temperature']))) { + $temperature = $input['temperature']; + } + + $maxTokens = null; + if (isset($input['max_tokens']) && is_int($input['max_tokens'])) { + $maxTokens = $input['max_tokens']; + } + + $extraParams = $temperature === null + ? null + : ['temperature' => $temperature]; + + try { + if ($this->openAiAPIService->isUsingOpenAi() || $this->openAiSettingsService->getChatEndpointEnabled()) { + $completion = $this->openAiAPIService->createChatCompletion($this->userId, $prompt, 1, $adminModel, $maxTokens, $extraParams); + } else { + $completion = $this->openAiAPIService->createCompletion($this->userId, $prompt, 1, $adminModel, $maxTokens, $extraParams); + } + } catch (Exception $e) { + throw new RuntimeException('OpenAI/LocalAI request failed: ' . $e->getMessage()); + } + if (count($completion) > 0) { + $endTime = time(); + $this->openAiAPIService->updateExpTextProcessingTime($endTime - $startTime); + return ['output' => array_pop($completion)]; + } + + throw new RuntimeException('No result in OpenAI/LocalAI response.'); + } +} diff --git a/lib/TaskProcessing/ReformulateTaskType.php b/lib/TaskProcessing/ReformulateTaskType.php new file mode 100644 index 00000000..0b67be4e --- /dev/null +++ b/lib/TaskProcessing/ReformulateTaskType.php @@ -0,0 +1,77 @@ +l->t('Reformulate'); + } + + /** + * @inheritDoc + * @since 30.0.0 + */ + public function getDescription(): string { + return $this->l->t('Formulate text in a different way.'); + } + + /** + * @return string + * @since 30.0.0 + */ + public function getId(): string { + return self::ID; + } + + /** + * @return ShapeDescriptor[] + * @since 30.0.0 + */ + public function getInputShape(): array { + return [ + 'input' => new ShapeDescriptor( + $this->l->t('Initial text'), + $this->l->t('The text you want to reformulate'), + EShapeType::Text + ), + ]; + } + + /** + * @return ShapeDescriptor[] + * @since 30.0.0 + */ + public function getOutputShape(): array { + return [ + 'output' => new ShapeDescriptor( + $this->l->t('Reformulated text'), + $this->l->t('The reformulated text generated by the assistant'), + EShapeType::Text + ), + ]; + } +}