From 9819a1aa4f1e053d971b835666f5eda7b4753462 Mon Sep 17 00:00:00 2001 From: Hamza Mahjoubi Date: Thu, 29 Feb 2024 15:28:27 +0100 Subject: [PATCH] Enh: Improve smart replies prompt Signed-off-by: Hamza Mahjoubi --- lib/Controller/MessagesController.php | 2 +- .../AiIntegrations/AiIntegrationsService.php | 25 ++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/Controller/MessagesController.php b/lib/Controller/MessagesController.php index a6f7b44d96..c16989a0d1 100755 --- a/lib/Controller/MessagesController.php +++ b/lib/Controller/MessagesController.php @@ -908,7 +908,7 @@ public function smartReply(int $messageId):JSONResponse { return new JSONResponse([], Http::STATUS_FORBIDDEN); } try { - $replies = $this->aiIntegrationService->getSmartReply($account, $mailbox, $message, $this->currentUserId); + $replies = array_values($this->aiIntegrationService->getSmartReply($account, $mailbox, $message, $this->currentUserId)); } catch (ServiceException $e) { $this->logger->error('Smart reply failed: ' . $e->getMessage(), [ 'exception' => $e, diff --git a/lib/Service/AiIntegrations/AiIntegrationsService.php b/lib/Service/AiIntegrations/AiIntegrationsService.php index 722c1ca411..e745af5288 100644 --- a/lib/Service/AiIntegrations/AiIntegrationsService.php +++ b/lib/Service/AiIntegrations/AiIntegrationsService.php @@ -176,9 +176,9 @@ public function getSmartReply(Account $account, Mailbox $mailbox, Message $messa throw new ServiceException('Text processing is not available in your current Nextcloud version', 0, $e); } if (in_array(FreePromptTaskType::class, $manager->getAvailableTaskTypes(), true)) { - $cachedReplies = $this->cache->getValue('smartReplies_'.$message->getId()); + $cachedReplies = $this->cache->getValue('smartReplies_'.$message->getUid()); if ($cachedReplies) { - return explode("|", $cachedReplies); + return json_decode($cachedReplies, true); } $client = $this->clientFactory->getClient($account); try { @@ -196,12 +196,25 @@ public function getSmartReply(Account $account, Mailbox $mailbox, Message $messa } finally { $client->logout(); } - $prompt = "Suggest 2 replies to the following email. Each reply should be 25 characters max. Separate the replies with \"| \", like for example \"Yes! | No, I'm not available \". Do not print anything else. The email contents are : ".$messageBody.""; + $prompt = "You are tasked with formulating helpful replies or reply templates to e-mails provided that have been sent to me. If you don't know some relevant information for answering the e-mails (like my schedule) leave blanks in the text that can later be filled by me. You must write the replies from my point of view as replies to the original sender of the provided e-mail! + + Formulate two extremely succinct reply suggestions to the provided ***E-MAIL***. Please, do not invent any context for the replies but, rather, leave blanks for me to fill in with relevant information where necessary. Provide the output formatted as valid JSON with the keys 'reply1' and 'reply2' for the reply suggestions. + + Each suggestion must be of 25 characters or less. + + Here is the ***E-MAIL*** for which you must suggest the replies to: + + ***START_OF_E-MAIL***".$messageBody." + + ***END_OF_E-MAIL*** + + Please, output *ONLY* a valid JSON string with the keys 'reply1' and 'reply2' for the reply suggestions. Leave out any other text besides the JSON! Be extremely succinct and write the replies from my point of view. + "; $task = new Task(FreePromptTaskType::class, $prompt, 'mail,', $currentUserId); $manager->runTask($task); - $replies = array_slice(explode("|", $task->getOutput()), 0, 2); - $this->cache->addValue('smartReplies_'.$message->getUid(), implode("|", $replies)); - return $replies; + $replies = $task->getOutput(); + $this->cache->addValue('smartReplies_'.$message->getUid(), $replies); + return json_decode($replies, true); } else { throw new ServiceException('No language model available for smart replies');