From 2e14d7d63c3fa91a86d8153d302067f77e348cb0 Mon Sep 17 00:00:00 2001 From: Hamza Mahjoubi Date: Tue, 11 Jun 2024 15:42:16 +0200 Subject: [PATCH] fixup! Enh: Improve smart replies prompt Signed-off-by: Hamza Mahjoubi --- .../AiIntegrations/AiIntegrationsService.php | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/Service/AiIntegrations/AiIntegrationsService.php b/lib/Service/AiIntegrations/AiIntegrationsService.php index 8c7759bf9a..b996dc665f 100644 --- a/lib/Service/AiIntegrations/AiIntegrationsService.php +++ b/lib/Service/AiIntegrations/AiIntegrationsService.php @@ -170,9 +170,10 @@ public function generateEventData(Account $account, string $threadId, array $mes } /** - * @return string[] + * @return ?string[] + * @throws ServiceException */ - public function getSmartReply(Account $account, Mailbox $mailbox, Message $message, string $currentUserId): array { + public function getSmartReply(Account $account, Mailbox $mailbox, Message $message, string $currentUserId): ?array { try { $manager = $this->container->get(IManager::class); } catch (\Throwable $e) { @@ -181,7 +182,7 @@ public function getSmartReply(Account $account, Mailbox $mailbox, Message $messa if (in_array(FreePromptTaskType::class, $manager->getAvailableTaskTypes(), true)) { $cachedReplies = $this->cache->getValue('smartReplies_'.$message->getId()); if ($cachedReplies) { - return json_decode($cachedReplies, true); + return json_decode($cachedReplies, true, 512); } $client = $this->clientFactory->getClient($account); try { @@ -202,23 +203,27 @@ public function getSmartReply(Account $account, Mailbox $mailbox, Message $messa $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 = $task->getOutput(); - $this->cache->addValue('smartReplies_'.$message->getId(), $replies); - return json_decode($replies, true); - + try { + $decoded = json_decode($replies, true, 512, JSON_THROW_ON_ERROR); + $this->cache->addValue('smartReplies_'.$message->getId(), $replies); + return $decoded; + } catch (JsonException $e) { + return null; + } } else { throw new ServiceException('No language model available for smart replies'); } @@ -247,7 +252,7 @@ private function isPersonalEmail(IMAPMessage $imapMessage): bool { ]; $senderAddress = $imapMessage->getFrom()->first()?->getEmail(); - + if($senderAddress !== null) { foreach ($commonPatterns as $pattern) { if (stripos($senderAddress, $pattern) !== false) {