Skip to content

Commit c71ee40

Browse files
committed
core: add more options for chat settings #38
add options to configure chat-template, use_mmap, use_mlock, n_threads add option to clear all chat messages
1 parent bf4076b commit c71ee40

File tree

18 files changed

+380
-178
lines changed

18 files changed

+380
-178
lines changed

.clang-format

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
BasedOnStyle: GNU
3+
Language: Cpp
4+
IndentWidth: 4
5+
UseTab: Never
6+
PointerAlignment: Left
7+
ReferenceAlignment: Left
8+
AlwaysBreakAfterReturnType: None
9+
SpaceBeforeParens: ControlStatements
10+
IndentPPDirectives: None
11+
BreakBeforeBraces: Attach
12+
ColumnLimit: 120
13+
AlignConsecutiveDeclarations: true
14+
AlignConsecutiveAssignments: true
15+
SpaceAfterTemplateKeyword: true
16+
SpaceBeforeAssignmentOperators: true
17+
IndentCaseLabels: false
18+
KeepEmptyLinesAtTheStartOfBlocks: false
19+
MaxEmptyLinesToKeep: 1
20+
SortIncludes: true
21+
IndentWrappedFunctionNames: false

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@
99
.cxx
1010
local.properties
1111
.kotlin
12+
ktlint
13+
ktlint.bat

app/objectbox-models/default.json

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"entities": [
66
{
77
"id": "1:2836016800019494484",
8-
"lastPropertyId": "11:2362849161259116908",
8+
"lastPropertyId": "15:5655950154838757763",
99
"name": "Chat",
1010
"properties": [
1111
{
@@ -63,6 +63,26 @@
6363
"id": "11:2362849161259116908",
6464
"name": "contextSizeConsumed",
6565
"type": 5
66+
},
67+
{
68+
"id": "12:454958936887824250",
69+
"name": "chatTemplate",
70+
"type": 9
71+
},
72+
{
73+
"id": "13:4856054665752880536",
74+
"name": "nThreads",
75+
"type": 5
76+
},
77+
{
78+
"id": "14:3913930867701003089",
79+
"name": "useMmap",
80+
"type": 1
81+
},
82+
{
83+
"id": "15:5655950154838757763",
84+
"name": "useMlock",
85+
"type": 1
6686
}
6787
],
6888
"relations": []
@@ -98,7 +118,7 @@
98118
},
99119
{
100120
"id": "3:3976296884919346124",
101-
"lastPropertyId": "5:5106603935737186708",
121+
"lastPropertyId": "6:9128691076308457471",
102122
"name": "LLMModel",
103123
"properties": [
104124
{
@@ -126,6 +146,11 @@
126146
"id": "5:5106603935737186708",
127147
"name": "contextSize",
128148
"type": 5
149+
},
150+
{
151+
"id": "6:9128691076308457471",
152+
"name": "chatTemplate",
153+
"type": 9
129154
}
130155
],
131156
"relations": []

app/src/main/java/io/shubham0204/smollmandroid/data/ChatsDB.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ data class Chat(
4343
var isTask: Boolean = false,
4444
var contextSize: Int = 0,
4545
var contextSizeConsumed: Int = 0,
46+
var chatTemplate: String = "",
47+
var nThreads: Int = 4,
48+
var useMmap: Boolean = true,
49+
var useMlock: Boolean = false,
4650
)
4751

4852
@Single

app/src/main/java/io/shubham0204/smollmandroid/data/ModelsDB.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ data class LLMModel(
3131
var url: String = "",
3232
var path: String = "",
3333
var contextSize: Int = 0,
34+
var chatTemplate: String = "",
3435
)
3536

3637
@Single
@@ -42,11 +43,25 @@ class ModelsDB {
4243
url: String,
4344
path: String,
4445
contextSize: Int,
46+
chatTemplate: String,
4547
) {
46-
modelsBox.put(LLMModel(name = name, url = url, path = path, contextSize = contextSize))
48+
modelsBox.put(
49+
LLMModel(
50+
name = name,
51+
url = url,
52+
path = path,
53+
contextSize = contextSize,
54+
chatTemplate = chatTemplate,
55+
),
56+
)
4757
}
4858

49-
fun getModel(id: Long): LLMModel? = modelsBox.get(id)
59+
fun getModel(id: Long): LLMModel? =
60+
try {
61+
modelsBox.get(id)
62+
} catch (e: IllegalArgumentException) {
63+
null
64+
}
5065

5166
fun getModels(): Flow<List<LLMModel>> =
5267
modelsBox

app/src/main/java/io/shubham0204/smollmandroid/llm/SmolLMManager.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class SmolLMManager(
4040
private var responseGenerationJob: Job? = null
4141
private var modelInitJob: Job? = null
4242
private var chat: Chat? = null
43-
var isInstanceLoaded = false
43+
private var isInstanceLoaded = false
4444
var isInferenceOn = false
4545

4646
data class SmolLMInitParams(
@@ -50,6 +50,10 @@ class SmolLMManager(
5050
val temperature: Float,
5151
val storeChats: Boolean,
5252
val contextSize: Long,
53+
val chatTemplate: String,
54+
val nThreads: Int,
55+
val useMmap: Boolean,
56+
val useMlock: Boolean,
5357
)
5458

5559
data class SmolLMResponse(
@@ -77,6 +81,10 @@ class SmolLMManager(
7781
initParams.temperature,
7882
initParams.storeChats,
7983
initParams.contextSize,
84+
initParams.chatTemplate,
85+
initParams.nThreads,
86+
initParams.useMmap,
87+
initParams.useMlock,
8088
)
8189
LOGD("Model loaded")
8290
if (initParams.chat.systemPrompt.isNotEmpty()) {

app/src/main/java/io/shubham0204/smollmandroid/ui/screens/chat/ChatActivity.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,6 @@ class ChatActivity : ComponentActivity() {
155155
override fun onStop() {
156156
super.onStop()
157157
modelUnloaded = viewModel.unloadModel()
158-
if (modelUnloaded) {
159-
Toast.makeText(this, "Model unloaded from memory", Toast.LENGTH_SHORT).show()
160-
}
161158
LOGD("onStop() called - model unloaded result: $modelUnloaded")
162159
}
163160
}
@@ -644,7 +641,7 @@ private fun SelectModelsList(viewModel: ChatScreenViewModel) {
644641
onDismissRequest = { viewModel.hideSelectModelListDialog() },
645642
modelsList,
646643
onModelListItemClick = { model ->
647-
viewModel.updateChatLLM(model.id)
644+
viewModel.updateChatLLMParams(model.id, model.chatTemplate)
648645
viewModel.loadModel()
649646
viewModel.hideSelectModelListDialog()
650647
},

app/src/main/java/io/shubham0204/smollmandroid/ui/screens/chat/ChatMoreOptionsPopup.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,30 @@ fun ChatMoreOptionsPopup(
9494
viewModel.hideMoreOptionsPopup()
9595
},
9696
)
97+
DropdownMenuItem(
98+
leadingIcon = { Icon(Icons.Default.Delete, contentDescription = "Clear Chat Messages") },
99+
text = { Text(stringResource(R.string.chat_options_clear_messages)) },
100+
onClick = {
101+
viewModel.currChatState.value?.let { chat ->
102+
createAlertDialog(
103+
dialogTitle = context.getString(R.string.chat_options_clear_messages),
104+
dialogText = context.getString(R.string.chat_options_clear_messages_text),
105+
dialogPositiveButtonText = context.getString(R.string.dialog_pos_clear),
106+
dialogNegativeButtonText = context.getString(R.string.dialog_neg_cancel),
107+
onPositiveButtonClick = {
108+
viewModel.deleteChatMessages(chat)
109+
Toast
110+
.makeText(
111+
viewModel.context,
112+
"Chat '${chat.name}' cleared",
113+
Toast.LENGTH_LONG,
114+
).show()
115+
},
116+
onNegativeButtonClick = {},
117+
)
118+
}
119+
viewModel.hideMoreOptionsPopup()
120+
},
121+
)
97122
}
98123
}

app/src/main/java/io/shubham0204/smollmandroid/ui/screens/chat/ChatScreenViewModel.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,11 @@ class ChatScreenViewModel(
147147

148148
fun getChatMessages(chatId: Long): Flow<List<ChatMessage>> = messagesDB.getMessages(chatId)
149149

150-
fun updateChatLLM(modelId: Long) {
151-
_currChatState.value = _currChatState.value?.copy(llmModelId = modelId)
150+
fun updateChatLLMParams(
151+
modelId: Long,
152+
chatTemplate: String,
153+
) {
154+
_currChatState.value = _currChatState.value?.copy(llmModelId = modelId, chatTemplate = chatTemplate)
152155
chatsDB.updateChat(_currChatState.value!!)
153156
}
154157

@@ -223,6 +226,11 @@ class ChatScreenViewModel(
223226
_currChatState.value = null
224227
}
225228

229+
fun deleteChatMessages(chat: Chat) {
230+
stopGeneration()
231+
messagesDB.deleteMessages(chat.id)
232+
}
233+
226234
fun deleteModel(modelId: Long) {
227235
modelsRepository.deleteModel(modelId)
228236
if (_currChatState.value?.llmModelId == modelId) {
@@ -252,6 +260,10 @@ class ChatScreenViewModel(
252260
chat.temperature,
253261
!chat.isTask,
254262
chat.contextSize.toLong(),
263+
chat.chatTemplate,
264+
chat.nThreads,
265+
chat.useMmap,
266+
chat.useMlock,
255267
),
256268
onError = { e ->
257269
_modelLoadState.value = ModelLoadingState.FAILURE

app/src/main/java/io/shubham0204/smollmandroid/ui/screens/chat/EditChatSettingsScreen.kt

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,16 @@ fun EditChatSettingsScreen(
7373
var minP by remember { mutableFloatStateOf(chat.minP) }
7474
var temperature by remember { mutableFloatStateOf(chat.temperature) }
7575
var contextSize by remember { mutableIntStateOf(chat.contextSize) }
76+
var nThreads by remember { mutableIntStateOf(chat.nThreads) }
7677
var takeContextSizeFromModel by remember { mutableStateOf(false) }
78+
var chatTemplate by remember { mutableStateOf(chat.chatTemplate) }
79+
var useMmap by remember { mutableStateOf(chat.useMmap) }
80+
var useMlock by remember { mutableStateOf(chat.useMlock) }
7781
val context = LocalContext.current
7882
val llmModel = viewModel.modelsRepository.getModelFromId(chat.llmModelId)
83+
84+
val totalThreads = Runtime.getRuntime().availableProcessors()
85+
7986
SmolLMAndroidTheme {
8087
Scaffold(
8188
topBar = {
@@ -99,6 +106,10 @@ fun EditChatSettingsScreen(
99106
minP = minP,
100107
temperature = temperature,
101108
contextSize = contextSize,
109+
chatTemplate = chatTemplate,
110+
nThreads = nThreads,
111+
useMmap = useMmap,
112+
useMlock = useMlock,
102113
)
103114
if (chat != updatedChat) {
104115
viewModel.updateChat(updatedChat)
@@ -152,6 +163,21 @@ fun EditChatSettingsScreen(
152163
KeyboardOptions.Default.copy(
153164
capitalization = KeyboardCapitalization.Sentences,
154165
),
166+
maxLines = 5,
167+
)
168+
169+
Spacer(modifier = Modifier.height(16.dp))
170+
171+
TextField(
172+
modifier = Modifier.fillMaxWidth(),
173+
value = chatTemplate,
174+
onValueChange = { chatTemplate = it },
175+
label = { Text("Chat template") },
176+
keyboardOptions =
177+
KeyboardOptions.Default.copy(
178+
capitalization = KeyboardCapitalization.Sentences,
179+
),
180+
maxLines = 5,
155181
)
156182

157183
Spacer(modifier = Modifier.height(8.dp))
@@ -266,6 +292,64 @@ fun EditChatSettingsScreen(
266292
style = MaterialTheme.typography.labelSmall,
267293
)
268294
}
295+
Spacer(modifier = Modifier.height(24.dp))
296+
297+
Text(
298+
text = "Number of Threads",
299+
style = MaterialTheme.typography.titleMedium,
300+
)
301+
Text(
302+
text = "num threads desc",
303+
style = MaterialTheme.typography.labelSmall,
304+
)
305+
Slider(
306+
value = nThreads.toFloat(),
307+
onValueChange = { nThreads = it.toInt() },
308+
valueRange = 0.0f..(totalThreads).toFloat(),
309+
steps = totalThreads,
310+
)
311+
Text(
312+
text = "$nThreads",
313+
style = MaterialTheme.typography.labelSmall,
314+
)
315+
316+
Spacer(modifier = Modifier.height(24.dp))
317+
318+
Row(modifier = Modifier.fillMaxWidth()) {
319+
Checkbox(
320+
checked = useMmap,
321+
onCheckedChange = { useMmap = it },
322+
)
323+
Column {
324+
Text(
325+
text = "Use mmap",
326+
style = MaterialTheme.typography.titleMedium,
327+
)
328+
Text(
329+
text = "Disable memory mapping (mmap) for potentially fewer pageouts and better performance on low-memory systems, but with slower load times and a risk of preventing loading large models.",
330+
style = MaterialTheme.typography.labelSmall,
331+
)
332+
}
333+
}
334+
Spacer(modifier = Modifier.height(24.dp))
335+
336+
Row(modifier = Modifier.fillMaxWidth()) {
337+
Checkbox(
338+
checked = useMlock,
339+
onCheckedChange = { useMlock = it },
340+
)
341+
Column {
342+
Text(
343+
text = "Use mlock",
344+
style = MaterialTheme.typography.titleMedium,
345+
)
346+
Text(
347+
text = "Keep the model loaded in RAM for faster performance, but uses more memory and may load slower.",
348+
style = MaterialTheme.typography.labelSmall,
349+
)
350+
}
351+
}
352+
Spacer(modifier = Modifier.height(24.dp))
269353
}
270354
}
271355
}

0 commit comments

Comments
 (0)