diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 88941b05..8f4c2843 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -54,7 +54,7 @@ class Application extends App implements IBootstrap { public const ASSISTANT_DATA_FOLDER_NAME = 'Assistant'; public const CHAT_USER_INSTRUCTIONS = 'This is a conversation in a specific language between the user and you, Nextcloud Assistant. You are a kind, polite and helpful AI that helps the user to the best of its abilities. If you do not understand something, you will ask for clarification. Detect the language that the user is using. Make sure to use the same language in your response. Do not mention the language explicitly. Format your answers properly in markdown.'; - public const CHAT_USER_INSTRUCTIONS_TITLE = 'Above is a chat session in a specific language between the user and you, Nextcloud Assistant. Generate a suitable title summarizing the conversation in the same language. Output only the title in plain text, nothing else.'; + public const CHAT_USER_INSTRUCTIONS_TITLE = 'This is a conversation between the user and Nextcloud Assistant. Generate a suitable title for the conversation that summarizes it. Detect the language of the conversation. The title that you output should be in the same language as the conversation. Output only the title in plain text, nothing else. Do not mention the language explicitly. For example, if the conversation is about trees in sweden but is written in Spanish, the title could be "Àrboles en Suecia", if it was in English, the title could be "Trees in Sweden". Do not write the title in e.g. Swedish just because Sweden is mentioned in the conversation.'; public const MAX_TEXT_INPUT_LENGTH = 64_000; private IAppConfig $appConfig; diff --git a/lib/Controller/ChattyLLMController.php b/lib/Controller/ChattyLLMController.php index 91457684..99e11f2f 100644 --- a/lib/Controller/ChattyLLMController.php +++ b/lib/Controller/ChattyLLMController.php @@ -880,28 +880,22 @@ public function generateTitle(int $sessionId): JSONResponse { ) ?: Application::CHAT_USER_INSTRUCTIONS_TITLE; $userInstructions = str_replace('{user}', $user->getDisplayName(), $userInstructions); - $systemPrompt = ''; - $firstMessage = $this->messageMapper->getFirstNMessages($sessionId, 1); - if ($firstMessage->getRole() === 'system') { - $systemPrompt = $firstMessage->getContent(); - } - $history = $this->getRawLastMessages($sessionId); // history is a list of JSON strings $history = array_map(static function (Message $message) { return json_encode([ 'role' => $message->getRole(), 'content' => $message->getContent(), - ]); + ], JSON_THROW_ON_ERROR); }, $history); try { - $taskId = $this->scheduleLLMChatTask($userInstructions, $systemPrompt, $history, $sessionId, false); + $taskId = $this->scheduleLLMChatTask($userInstructions, $userInstructions, $history, $sessionId, false); } catch (\Exception $e) { return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } return new JSONResponse(['taskId' => $taskId]); - } catch (\OCP\DB\Exception $e) { + } catch (\OCP\DB\Exception|\JsonException $e) { $this->logger->warning('Failed to generate a title for the chat session', ['exception' => $e]); return new JSONResponse(['error' => $this->l10n->t('Failed to generate a title for the chat session')], Http::STATUS_INTERNAL_SERVER_ERROR); }