Skip to content

Commit 297ed81

Browse files
committed
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 <mklehr@gmx.net>
1 parent 4407013 commit 297ed81

2 files changed

Lines changed: 6 additions & 9 deletions

File tree

lib/AppInfo/Application.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class Application extends App implements IBootstrap {
5454
public const ASSISTANT_DATA_FOLDER_NAME = 'Assistant';
5555

5656
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.';
57-
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.';
57+
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 and is in Spanish, the title could be "Àrboles", if it was in English, the title could be "Trees"';
5858
public const MAX_TEXT_INPUT_LENGTH = 64_000;
5959

6060
private IAppConfig $appConfig;

lib/Controller/ChattyLLMController.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -880,30 +880,27 @@ public function generateTitle(int $sessionId): JSONResponse {
880880
) ?: Application::CHAT_USER_INSTRUCTIONS_TITLE;
881881
$userInstructions = str_replace('{user}', $user->getDisplayName(), $userInstructions);
882882

883-
$systemPrompt = '';
884-
$firstMessage = $this->messageMapper->getFirstNMessages($sessionId, 1);
885-
if ($firstMessage->getRole() === 'system') {
886-
$systemPrompt = $firstMessage->getContent();
887-
}
888-
889883
$history = $this->getRawLastMessages($sessionId);
890884
// history is a list of JSON strings
891885
$history = array_map(static function (Message $message) {
892886
return json_encode([
893887
'role' => $message->getRole(),
894888
'content' => $message->getContent(),
895-
]);
889+
], JSON_THROW_ON_ERROR);
896890
}, $history);
897891

898892
try {
899-
$taskId = $this->scheduleLLMChatTask($userInstructions, $systemPrompt, $history, $sessionId, false);
893+
$taskId = $this->scheduleLLMChatTask($userInstructions, $userInstructions, $history, $sessionId, false);
900894
} catch (\Exception $e) {
901895
return new JSONResponse(['error' => $e->getMessage()], Http::STATUS_BAD_REQUEST);
902896
}
903897
return new JSONResponse(['taskId' => $taskId]);
904898
} catch (\OCP\DB\Exception $e) {
905899
$this->logger->warning('Failed to generate a title for the chat session', ['exception' => $e]);
906900
return new JSONResponse(['error' => $this->l10n->t('Failed to generate a title for the chat session')], Http::STATUS_INTERNAL_SERVER_ERROR);
901+
} catch (\JsonException $e) {
902+
$this->logger->warning('Failed to generate a title for the chat session', ['exception' => $e]);
903+
return new JSONResponse(['error' => $this->l10n->t('Failed to generate a title for the chat session')], Http::STATUS_INTERNAL_SERVER_ERROR);
907904
}
908905
}
909906

0 commit comments

Comments
 (0)