openassistant: add system prompt setting
- Commit
2626dd3af79673d13f867a2662d2a778a21c286d- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-14 15:15 UTC (3 months ago)
- Parents
3f4d986
Diff
openassistant/src/main/java/com/vayunmathur/openassistant/ui/SettingsPage.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/ui/SettingsPage.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/ui/SettingsPage.ktindex 0b8cb23cd3..0792f49ff5 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/ui/SettingsPage.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/ui/SettingsPage.kt@@ -16,25 +16,50 @@ import com.vayunmathur.library.ui.IconNavigation import com.vayunmathur.library.ui.IconDelete import com.vayunmathur.library.room.SqlCipherDbCodec import com.vayunmathur.library.util.DatabaseHelper+import com.vayunmathur.library.util.DataStoreUtils import com.vayunmathur.library.util.NavBackStack import com.vayunmathur.openassistant.R import com.vayunmathur.openassistant.Route import com.vayunmathur.openassistant.data.Memory import com.vayunmathur.openassistant.util.AssistantViewModel+import com.vayunmathur.openassistant.util.InferenceService import com.vayunmathur.openassistant.util.SettingsActions import com.vayunmathur.openassistant.util.SettingsUiState+import kotlinx.coroutines.launch /** Binds [AssistantViewModel] and the nav back stack to the stateless [SettingsScreen]. */ @Composable fun SettingsPage(backStack: NavBackStack<Route>, viewModel: AssistantViewModel) { val memories by viewModel.memories.collectAsState() val context = LocalContext.current+ val scope = rememberCoroutineScope()+ val ds = remember(context) { DataStoreUtils.getInstance(context) }++ var systemPrompt by remember {+ mutableStateOf(+ ds.getString(InferenceService.KEY_SYSTEM_PROMPT)+ ?: InferenceService.DEFAULT_SYSTEM_PROMPT+ )+ } SettingsScreen(- state = SettingsUiState(memories = memories),+ state = SettingsUiState(memories = memories, systemPrompt = systemPrompt), actions = object : SettingsActions { override fun back() = backStack.pop() override fun deleteMemory(memory: Memory) = viewModel.deleteMemory(memory)+ override fun setSystemPrompt(prompt: String) {+ systemPrompt = prompt+ scope.launch { ds.setString(InferenceService.KEY_SYSTEM_PROMPT, prompt) }+ }+ override fun resetSystemPrompt() {+ systemPrompt = InferenceService.DEFAULT_SYSTEM_PROMPT+ scope.launch {+ ds.setString(+ InferenceService.KEY_SYSTEM_PROMPT,+ InferenceService.DEFAULT_SYSTEM_PROMPT+ )+ }+ } }, // Passed in rather than built inside the screen: the backup buttons need the // database passphrase, which only exists on a real device.@@ -83,12 +108,43 @@ fun SettingsScreen( ) { item { Text(- text = stringResource(R.string.memories),+ text = stringResource(R.string.system_prompt), style = MaterialTheme.typography.titleLarge, fontWeight = FontWeight.Bold, modifier = Modifier.padding(bottom = 8.dp) ) }+ item {+ OutlinedTextField(+ value = state.systemPrompt,+ onValueChange = { actions.setSystemPrompt(it) },+ modifier = Modifier.fillMaxWidth(),+ label = { Text(stringResource(R.string.system_prompt)) },+ minLines = 5,+ maxLines = 12,+ )+ }+ item {+ Row(+ modifier = Modifier.fillMaxWidth(),+ horizontalArrangement = Arrangement.End+ ) {+ TextButton(+ onClick = { actions.resetSystemPrompt() },+ enabled = state.systemPrompt != InferenceService.DEFAULT_SYSTEM_PROMPT+ ) {+ Text(stringResource(R.string.reset_to_default))+ }+ }+ }+ item {+ Text(+ text = stringResource(R.string.memories),+ style = MaterialTheme.typography.titleLarge,+ fontWeight = FontWeight.Bold,+ modifier = Modifier.padding(top = 16.dp, bottom = 8.dp)+ )+ } if (memories.isEmpty()) { item { Text(stringResource(R.string.no_memories_yet), style = MaterialTheme.typography.bodyMedium)
openassistant/src/main/java/com/vayunmathur/openassistant/util/AssistantUiContract.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/util/AssistantUiContract.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/util/AssistantUiContract.ktindex 3626aa800a..edf30d6072 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/util/AssistantUiContract.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/util/AssistantUiContract.kt@@ -52,12 +52,18 @@ interface ChatActions { /** Everything the settings screen draws. */ data class SettingsUiState( val memories: List<Memory> = emptyList(),+ /** The chat system prompt; editable, defaults to the built-in prompt. */+ val systemPrompt: String = "", ) /** Settings callbacks. Same no-op-default arrangement as [ChatActions]. */ interface SettingsActions { fun back() {} fun deleteMemory(memory: Memory) {}+ fun setSystemPrompt(prompt: String) {}++ /** Restore the system prompt to the built-in default. */+ fun resetSystemPrompt() {} companion object { val Noop: SettingsActions = object : SettingsActions {}
openassistant/src/main/java/com/vayunmathur/openassistant/util/InferenceService.kt
diff --git a/openassistant/src/main/java/com/vayunmathur/openassistant/util/InferenceService.kt b/openassistant/src/main/java/com/vayunmathur/openassistant/util/InferenceService.ktindex 10e7d6dd2f..02f2831474 100644--- a/openassistant/src/main/java/com/vayunmathur/openassistant/util/InferenceService.kt+++ b/openassistant/src/main/java/com/vayunmathur/openassistant/util/InferenceService.kt@@ -37,6 +37,27 @@ class InferenceService : Service() { companion object { var newTitle: String? = null var halt: Boolean = false++ /** DataStore key holding the user-editable chat system prompt. */+ const val KEY_SYSTEM_PROMPT = "system_prompt"++ /** The system prompt used when the user has not set a custom one. */+ val DEFAULT_SYSTEM_PROMPT = """+ You are a helpful Android assistant.+ On the first request the user sends to you, you MUST define a title for the conversation. You may optionally change the title if the topic of conversation changes sufficiently.++ TOOL USE GUIDELINES:+ - Only use a tool when the user's request clearly and directly relates to that tool's purpose. Do NOT guess or speculatively call tools on short or ambiguous prompts.+ - If the user asks a general knowledge question (e.g. "what is...", "how does...", "tell me about..."), answer from your own knowledge. Do not invoke app tools unless the user explicitly asks to interact with an app.+ - If a tool call fails because the required app is not installed, do NOT stop. Continue helping the user by answering from your own knowledge or suggesting alternatives.++ MEMORY:+ You have a memory feature. Use it aggressively to provide a personalized and consistent experience:+ - Whenever the user shares ANY information that might conceivably be useful in future conversations (e.g., their name, preferences, family details, interests, opinions, routines, or important facts), use 'add_to_memory' to store it immediately.+ - At the start of every conversation and whenever the user asks a question or makes a request where past context could even remotely be relevant, use 'get_memories' to retrieve stored information.+ - If a stored memory is no longer accurate or requested to be forgotten, use 'remove_memory'.+ - When in doubt about whether to use memory, USE IT.+ """.trimIndent() } private sealed class InferenceJob {@@ -503,23 +524,9 @@ class InferenceService : Service() { engine = newEngine } - private fun setupConversation(id: Long, history: List<Message>) {- val systemPrompt = """- You are a helpful Android assistant.- On the first request the user sends to you, you MUST define a title for the conversation. You may optionally change the title if the topic of conversation changes sufficiently.-- TOOL USE GUIDELINES:- - Only use a tool when the user's request clearly and directly relates to that tool's purpose. Do NOT guess or speculatively call tools on short or ambiguous prompts.- - If the user asks a general knowledge question (e.g. "what is...", "how does...", "tell me about..."), answer from your own knowledge. Do not invoke app tools unless the user explicitly asks to interact with an app.- - If a tool call fails because the required app is not installed, do NOT stop. Continue helping the user by answering from your own knowledge or suggesting alternatives.-- MEMORY:- You have a memory feature. Use it aggressively to provide a personalized and consistent experience:- - Whenever the user shares ANY information that might conceivably be useful in future conversations (e.g., their name, preferences, family details, interests, opinions, routines, or important facts), use 'add_to_memory' to store it immediately.- - At the start of every conversation and whenever the user asks a question or makes a request where past context could even remotely be relevant, use 'get_memories' to retrieve stored information.- - If a stored memory is no longer accurate or requested to be forgotten, use 'remove_memory'.- - When in doubt about whether to use memory, USE IT.- """.trimIndent()+ private suspend fun setupConversation(id: Long, history: List<Message>) {+ val stored = DataStoreUtils.getInstance(applicationContext).getStringAwait(KEY_SYSTEM_PROMPT)+ val systemPrompt = if (stored.isNullOrBlank()) DEFAULT_SYSTEM_PROMPT else stored val initialMessages = history.map { msg -> when (msg.role) {
openassistant/src/main/res/values/strings.xml
diff --git a/openassistant/src/main/res/values/strings.xml b/openassistant/src/main/res/values/strings.xmlindex 844253daf1..e998a7df7b 100644--- a/openassistant/src/main/res/values/strings.xml+++ b/openassistant/src/main/res/values/strings.xml@@ -35,5 +35,9 @@ <string name="memories">Memories</string> <!-- Empty state in Settings when no long-term memories have been saved yet --> <string name="no_memories_yet">No memories yet.</string>+ <!-- Section header in Settings for the editable chat system prompt. Noun phrase -->+ <string name="system_prompt">System Prompt</string>+ <!-- Button in Settings that restores the system prompt to its built-in default value -->+ <string name="reset_to_default">Reset to default</string> <string name="inference_service_label">Access Inference Service</string> </resources>