Skip to content

Commit f568981

Browse files
committed
fix clear memory
1 parent dfb84f6 commit f568981

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

tools/mtmd/mtmd-cli.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ struct mtmd_cli_context {
8080
common_chat_templates_ptr tmpls;
8181
std::vector<common_chat_msg> chat_history;
8282
bool use_jinja = false;
83+
std::string system_prompt;
8384

8485
// support for legacy templates (models not having EOT token)
8586
llama_tokens antiprompt_tokens;
@@ -110,12 +111,8 @@ struct mtmd_cli_context {
110111

111112
tmpls = common_chat_templates_init(model, params.chat_template);
112113
use_jinja = params.use_jinja;
113-
if (!params.system_prompt.empty()) {
114-
common_chat_msg sys_msg;
115-
sys_msg.role = "system";
116-
sys_msg.content = params.system_prompt;
117-
chat_history.push_back(std::move(sys_msg));
118-
}
114+
system_prompt = params.system_prompt;
115+
reset_chat_history();
119116
LOG_INF("%s: chat template example:\n%s\n", __func__, common_chat_format_example(tmpls.get(), params.use_jinja, params.default_template_kwargs).c_str());
120117

121118
init_vision_context(params);
@@ -133,6 +130,16 @@ struct mtmd_cli_context {
133130
common_sampler_free(smpl);
134131
}
135132

133+
void reset_chat_history() {
134+
chat_history.clear();
135+
if (!system_prompt.empty()) {
136+
common_chat_msg sys_msg;
137+
sys_msg.role = "system";
138+
sys_msg.content = system_prompt;
139+
chat_history.push_back(std::move(sys_msg));
140+
}
141+
}
142+
136143
void init_vision_context(common_params & params) {
137144
const char * clip_path = params.mmproj.path.c_str();
138145
mtmd_context_params mparams = mtmd_context_params_default();
@@ -363,7 +370,8 @@ int main(int argc, char ** argv) {
363370
}
364371
if (line == "/clear") {
365372
ctx.n_past = 0;
366-
llama_memory_seq_rm(llama_get_memory(ctx.lctx), 0, 1, -1); // keep BOS
373+
ctx.reset_chat_history();
374+
llama_memory_clear(llama_get_memory(ctx.lctx), true);
367375
LOG("Chat history cleared\n\n");
368376
continue;
369377
}

0 commit comments

Comments
 (0)