Add in simple to use iterative chat (chat with history) #30

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

Originally created by @connor-makowski on GitHub (Feb 13, 2024).

For example a function like:

from ollama import historic_chat

message = {
  'role': 'user',
  'content': 'Tell me a Joke in less than 30 words.',
}
response = historic_chat('mistral', message=message)
print(response['message']['content'])

message = {
  'role': 'user',
  'content': 'Another please.',
}
response = historic_chat('mistral', message=message)
print(response['message']['content'])

Should return two jokes.

Originally created by @connor-makowski on GitHub (Feb 13, 2024). For example a function like: ``` from ollama import historic_chat message = { 'role': 'user', 'content': 'Tell me a Joke in less than 30 words.', } response = historic_chat('mistral', message=message) print(response['message']['content']) message = { 'role': 'user', 'content': 'Another please.', } response = historic_chat('mistral', message=message) print(response['message']['content']) ``` Should return two jokes.
yindo closed this issue 2026-02-15 16:27:34 -05:00
Author
Owner

@connor-makowski commented on GitHub (Feb 15, 2024):

@mxyng tagging you to get some initial thoughts on this issue and PR.

@connor-makowski commented on GitHub (Feb 15, 2024): @mxyng tagging you to get some initial thoughts on this issue and PR.
Author
Owner

@mxyng commented on GitHub (Feb 21, 2024):

Thanks for the interest in ollama-python. The current intention for this library is to mirror the Ollama API and is minimal as a result. Implementing memory as part of the library is out of scope.

Memory can be implemented easily by manipulating the messages keyword argument. Here's a quick example:

from ollama import chat


messages = [
  {'role': 'user', 'content': 'Tell me a Joke in less than 30 words.'},
]

response = chat('mistral', messages=messages)
message = response['message']
print(message['content'])
messages.append(message)

messages.append({'role': 'user', 'content': 'Another please.'})
response = chat('mistral', messages=messages)
message = response['message']
print(message['content'])
messages.append(message)

This example will print two jokes. Here's what mistral gave me

$ python run example.py
 Why don't scientists trust atoms? Because they make up everything!
 Why did the scarecrow win an award? Because he was outstanding in his field!

This full example provides a working version of the above with a few extra bells and whistles. It's effectively a trimmed down version of ollama run (with --speak for fun).

@mxyng commented on GitHub (Feb 21, 2024): Thanks for the interest in ollama-python. The current intention for this library is to mirror the Ollama API and is minimal as a result. Implementing memory as part of the library is out of scope. Memory can be implemented easily by manipulating the `messages` keyword argument. Here's a quick example: ```python from ollama import chat messages = [ {'role': 'user', 'content': 'Tell me a Joke in less than 30 words.'}, ] response = chat('mistral', messages=messages) message = response['message'] print(message['content']) messages.append(message) messages.append({'role': 'user', 'content': 'Another please.'}) response = chat('mistral', messages=messages) message = response['message'] print(message['content']) messages.append(message) ``` This example will print two jokes. Here's what `mistral` gave me ``` $ python run example.py Why don't scientists trust atoms? Because they make up everything! Why did the scarecrow win an award? Because he was outstanding in his field! ``` This full [example](https://github.com/ollama/ollama-python/blob/main/examples/async-chat-stream/main.py) provides a working version of the above with a few extra bells and whistles. It's effectively a trimmed down version of `ollama run` (with `--speak` for fun).
Author
Owner

@connor-makowski commented on GitHub (Feb 21, 2024):

I am not sure if you saw the PR submitted to solve this. Its very simple and seems worthwhile to add to scope.https://github.com/ollama/ollama-python/pull/64

@connor-makowski commented on GitHub (Feb 21, 2024): I am not sure if you saw the PR submitted to solve this. Its very simple and seems worthwhile to add to scope.https://github.com/ollama/ollama-python/pull/64
Author
Owner

@mxyng commented on GitHub (Feb 21, 2024):

I did see the PR. The PR changes the synchronous client to subclass a new client with internal state and adds a method iterative_chat which uses the internal state in place of messages.

A stateful client is problematic since now there now needs to be functions to manage the internal state such as trimming or resetting state. This adds additional scope and complexity to library for features that are user specific.

Additionally, using a single client for two sets of history is impossible since this client maintains a single history list.

The optional history keyword argument functions roughly the same as the current messages keyword argument by manipulating the internal state. However it's error prone since sending the same history twice will add duplicate entries.

Overall, it's a lot of effort and code to implement something the user can implement easily and with more precision

@mxyng commented on GitHub (Feb 21, 2024): I did see the PR. The PR changes the synchronous client to subclass a new client with internal state and adds a method iterative_chat which uses the internal state in place of messages. A stateful client is problematic since now there now needs to be functions to manage the internal state such as trimming or resetting state. This adds additional scope and complexity to library for features that are user specific. Additionally, using a single client for two sets of history is impossible since this client maintains a single history list. The optional `history` keyword argument functions roughly the same as the current `messages` keyword argument by manipulating the internal state. However it's error prone since sending the same history twice will add duplicate entries. Overall, it's a lot of effort and code to implement something the user can implement easily and with more precision
Author
Owner

@connor-makowski commented on GitHub (Feb 21, 2024):

Fair points. I still see a large need for this. I will probably just end up creating an external library that streamlines this process.

@connor-makowski commented on GitHub (Feb 21, 2024): Fair points. I still see a large need for this. I will probably just end up creating an external library that streamlines this process.
Author
Owner

@mxyng commented on GitHub (Feb 21, 2024):

That would be great! This library is intended to be a building block on which others such as yourself can build more featureful projects

@mxyng commented on GitHub (Feb 21, 2024): That would be great! This library is intended to be a building block on which others such as yourself can build more featureful projects
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: ollama/ollama-python#30