ollama-python equivalent of /clear and context summarization in a chat #100

Closed
opened 2026-02-15 16:27:59 -05:00 by yindo · 3 comments
Owner

Originally created by @JiahuiKChen on GitHub (Jun 17, 2024).

When using ollama run <model>, there's a /clear command to "clear session context".
How can this be done in the ollama-python library? I can't figure out if it's possible when looking at client.py.

My use case is that I want to chat with the same model in a script but clear the context occasionally.

I'm also interested in how to summarize older conversations to enable longer history with less context, mentioned in this example.
Would I have to explicitly ask the model to summarize its context so far, clear the context, then prompt the model with the saved summary -- or is there a built-in way for the model to summarize its context so far and retain the summary, while clearing the rest of its context?

Originally created by @JiahuiKChen on GitHub (Jun 17, 2024). When using `ollama run <model>`, there's a `/clear` command to "clear session context". How can this be done in the ollama-python library? I can't figure out if it's possible when looking at `client.py`. My use case is that I want to chat with the same model in a script but clear the context occasionally. I'm also interested in how to summarize older conversations to enable longer history with less context, mentioned in [this example](https://github.com/ollama/ollama/tree/main/examples/python-simplechat#next-steps). Would I have to explicitly ask the model to summarize its context so far, clear the context, then prompt the model with the saved summary -- or is there a built-in way for the model to summarize its context so far and retain the summary, while clearing the rest of its context?
yindo closed this issue 2026-02-15 16:27:59 -05:00
Author
Owner

@JICA98 commented on GitHub (Jun 20, 2024):

+1

@JICA98 commented on GitHub (Jun 20, 2024): +1
Author
Owner

@militu commented on GitHub (Jun 23, 2024):

For the first question, it seems natively handled, look here https://github.com/ollama/ollama/issues/4564

@militu commented on GitHub (Jun 23, 2024): For the first question, it seems natively handled, look here https://github.com/ollama/ollama/issues/4564
Author
Owner

@pdevine commented on GitHub (Jun 26, 2024):

@JiahuiKChen You have to handle this yourself in the client. You can just empty the messages list, which is effectively what /clear does. You'll have to repopulate the system message again (although we have been looking at doing this automatically in the future).

You definitely can ask the LLM to summarize the current conversation and then substitute that into the messages if you want the LLM to not "lose its brain". We haven't added that capability yet into Ollama.

I'll go ahead and close the issue.

@pdevine commented on GitHub (Jun 26, 2024): @JiahuiKChen You have to handle this yourself in the client. You can just empty the `messages` list, which is effectively what `/clear` does. You'll have to repopulate the system message again (although we have been looking at doing this automatically in the future). You definitely can ask the LLM to summarize the current conversation and then substitute that into the messages if you want the LLM to not "lose its brain". We haven't added that capability yet into Ollama. I'll go ahead and close the issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#100