Skip to content

Commit ab675c7

Browse files
authored
Merge pull request #451 from nextcloud/fix/title-gen-multilang
Fix(ChattyLLM): Fix title generation to work with languages other than english
2 parents 4407013 + 86c4199 commit ab675c7

2 files changed

Lines changed: 4 additions & 10 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 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.';
5858
public const MAX_TEXT_INPUT_LENGTH = 64_000;
5959

6060
private IAppConfig $appConfig;

lib/Controller/ChattyLLMController.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -880,28 +880,22 @@ 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]);
904-
} catch (\OCP\DB\Exception $e) {
898+
} catch (\OCP\DB\Exception|\JsonException $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);
907901
}

0 commit comments

Comments
 (0)