Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/src/main/java/helium314/keyboard/latin/ai/ChatParams.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.latin.ai

/**
* The request as chat messages, in addition to the plain prompt: a plugin from 1.0.2 on puts them into the chat format
* stored in the model file. Older plugins ignore these parameters.
*/
fun chatParams(instruction: String, text: String, thinking: Boolean): Map<String, Any> {
val (system, user) = when {
instruction.contains("{text}") -> null to instruction.replace("{text}", text)
// no text, e.g. a custom key that only uses the clipboard: the instruction is the request
text.isBlank() -> null to instruction.trim()
else -> instruction.trim() to text
}
val params = mapOf("chat_user" to user, "enable_thinking" to thinking, "show_thinking" to thinking)
return if (system == null) params else params + ("chat_system" to system)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package helium314.keyboard.latin.utils

import helium314.keyboard.latin.ai.chatParams
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.currentCoroutineContext
import helium314.keyboard.latin.ai.TextPieces
Expand Down Expand Up @@ -515,7 +516,7 @@ class ProofreadService(private val context: Context) {
"top_k" to topK,
"min_p" to minP.toDouble(),
"max_tokens" to effectiveMaxTokens
))
) + chatParams(systemPrompt, text, showThinkingVal))
}

// Schedule unload after work is done
Expand Down
22 changes: 22 additions & 0 deletions app/src/test/java/helium314/keyboard/latin/ChatParamsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: GPL-3.0-only
package helium314.keyboard.latin

import helium314.keyboard.latin.ai.chatParams
import kotlin.test.Test
import kotlin.test.assertEquals

class ChatParamsTest {
@Test fun `the instruction is the system message and the text the user message`() {
val params = chatParams(" Fix this. ", "he go", false)
assertEquals("Fix this.", params["chat_system"])
assertEquals("he go", params["chat_user"])
assertEquals(false, params["enable_thinking"])
}

@Test fun `a placeholder or a missing text leaves only a user message`() {
assertEquals("Shorten he go now", chatParams("Shorten {text} now", "he go", true)["chat_user"])
assertEquals(null, chatParams("Shorten {text} now", "he go", true)["chat_system"])
assertEquals("Tell a joke", chatParams("Tell a joke ", "", false)["chat_user"])
assertEquals(null, chatParams("Tell a joke ", "", false)["chat_system"])
}
}
Loading