From 90c1691dfc15627748a173fb54a15d9453e8e21b Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 3 Feb 2026 13:33:06 +0100 Subject: [PATCH 1/2] Fix(ChattyLLM): Fix title generation to work with languages other than english tested with Llama 3.1 8B and Mistral Small 24B Signed-off-by: Marcel Klehr --- lib/AppInfo/Application.php | 2 +- lib/Controller/ChattyLLMController.php | 13 +++++-------- 2 files changed, 6 insertions(+), 9 deletions(-) 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..778d190a 100644 --- a/lib/Controller/ChattyLLMController.php +++ b/lib/Controller/ChattyLLMController.php @@ -880,23 +880,17 @@ 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); } @@ -904,6 +898,9 @@ public function generateTitle(int $sessionId): JSONResponse { } catch (\OCP\DB\Exception $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); + } catch (\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); } } From 86c419966db94b8488f662be1ee938015142eb77 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 3 Feb 2026 13:45:23 +0100 Subject: [PATCH 2/2] fix: Address review comments Signed-off-by: Marcel Klehr --- lib/Controller/ChattyLLMController.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/Controller/ChattyLLMController.php b/lib/Controller/ChattyLLMController.php index 778d190a..99e11f2f 100644 --- a/lib/Controller/ChattyLLMController.php +++ b/lib/Controller/ChattyLLMController.php @@ -895,10 +895,7 @@ public function generateTitle(int $sessionId): JSONResponse { return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST); } return new JSONResponse(['taskId' => $taskId]); - } catch (\OCP\DB\Exception $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); - } catch (\JsonException $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); }