While researching local DeepSeek deployment, I searched for “how to set separate user histories in Ollama.” A few minutes later I searched for “how to build a conversation with memory using the OpenAI API,” and eventually narrowed the question to “Ollama API multi-turn conversation.” A tiny example ties them together. Lin tells the assistant, “My package code name is Birch.” Chen says theirs is “Sea Salt.” Both then ask, “What was the code name again?” How does the same model know which answer to give?
What's different about the second request?
In Ollama v0.5.7, POST /api/chat accepts a list called messages. On the first turn, send only Lin's message. After receiving the answer, the application saves both question and answer. When Lin follows up, the application sends the following three messages in order. The model can now find “Birch” in the first one. The role field identifies the speaker: user is the user and assistant is the assistant.12
| Order | role | content |
|---|---|---|
| 1 | user | My package code name is Birch. |
| 2 | assistant | Got it. I'll remember that. |
| 3 | user | What was the code name again? |
Send only the third message, and the model sees a question with no referent. Paishen ran into the same issue in an earlier API chatbot and put previous questions and answers back into the prompt. Hoper-J's later DeepSeek API example makes the steps especially clear: first append the previous assistant reply to messages, then append the user's follow-up. The follow-up asks “刚刚针对哪两个数值进行了比较?” (“Which two values did we just compare?”), a convenient test of whether the previous turn was included.34
If two people share one model, where does the history go?
Have the application store messages separately for each user and conversation. Lin could open a “package” chat and a “travel” chat at the same time. Keying by username alone would merge Lin's topics. Keying by window name alone could mix up two different users' “package” chats.
Once you have the local Ollama service from the deployment tutorial and deepseek-r1:1.5b, save the following example as chat_history.py and run python3 chat_history.py. It uses only the Python standard library, simulates two users in sequence, and clears the history when the program exits.
import json
from urllib.request import Request, urlopen
histories = {}
def ask(user, conversation, question):
key = (user, conversation)
messages = histories.get(key, []) + [{"role": "user", "content": question}]
payload = {"model": "deepseek-r1:1.5b", "messages": messages, "stream": False}
request = Request("http://127.0.0.1:11434/api/chat",
data=json.dumps(payload).encode(),
headers={"Content-Type": "application/json"})
with urlopen(request) as response:
answer = json.load(response)["message"]["content"]
histories[key] = messages + [{"role": "assistant", "content": answer}]
return answer
ask("小林", "包裹", "我的包裹代号是白桦。")
ask("小陈", "包裹", "我的包裹代号是海盐。")
for user in ["小林", "小陈"]:
print(user, ask(user, "包裹", "刚才的包裹代号是什么?只回答代号。"))
Check two things. First, do the answers give “白桦” (Birch) and “海盐” (Sea Salt) respectively? If the conversations get crossed, print messages before sending it. Lin's request should contain only messages from this particular conversation with Lin. To resume after closing the webpage, store messages in a database using the same keys and read them back in sending order when the user returns. On a multi-user site, obtain the user ID from the authenticated identity and check that the conversation belongs to that user.
Is reopening the same chat the same as being remembered in a new chat?
Reopening an existing conversation only requires retrieving its saved messages. If it's long, keep the latest few turns or summarize older messages to leave room in the model's context window for its answer. The trade-off is straightforward: if the summary leaves out the package code name, the model won't see it on this turn even though the application has the complete record.
To carry information over to a new conversation, give it its own storage scope. Suppose Lin says, “Answer in Chinese from now on.” The application can store that preference under Lin's user ID and include it when a new chat starts, while keeping the package code name in the original conversation. When ChatGPT tested its memory feature in February 2024, users could delete a chat and still manage separately saved memories in settings.5 For an example of how an application updates and retrieves long-term records, see the role-playing example in the prompt engineering article.
References
Footnotes
-
Ollama contributors. (2025). API documentation (v0.5.7). GitHub. Generate a chat completion. ↩
-
Datawhale. (2025, February). Ollama API 使用指南 [Ollama API usage guide]. GitHub. Example of a request with history, version before February 24. During the same search I also read the CSDN Library (n.d.) Q&A “How to set up multi-turn conversations in Ollama”. ↩
-
-派神-. (n.d.). 我让API版的ChatGPT长了记性! [I gave the API version of ChatGPT a memory!]. CSDN. Single-turn calls and assembling context. ↩
-
Hoper-J. (2025, February). DeepSeek API 多轮对话 - OpenAI SDK [Multi-turn conversations with the DeepSeek API and OpenAI SDK]. GitHub. The quotation is from the second question in the two-turn example, version before February 24. ↩
-
李孟聊AI. (2024, February 14, edited). OpenAI ChatGPT 记忆功能怎么实现? [How does OpenAI ChatGPT's memory work?]. Zhihu. See the discussion of deleting conversations and managing memories in the usage section. ↩