mirror of
https://github.com/ollama/ollama-python.git
synced 2026-07-21 09:05:23 -04:00
[PR #158] [MERGED] Avoid side effects if chat message contains images #388
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/ollama/ollama-python/pull/158
Author: @tillfalko
Created: 5/18/2024
Status: ✅ Merged
Merged: 6/5/2024
Merged by: @mxyng
Base:
main← Head:main📝 Commits (1)
0f8c20aAvoid side effects if chat message contains images📊 Changes
1 file changed (+5 additions, -0 deletions)
View changed files
📝
ollama/_client.py(+5 -0)📄 Description
The Issue
Generating chat responses with images has a rather nasty side effect in that, if a message has images, they will be overwritten by their Base64 encoding.
If a user passed an image path, it is undesirable to have this path overwritten by extremely verbose Base64-text.
The Cause
Most python users will pass messages as sequences of dictionaries. Dictionaries are inherently mutable object that are passed by reference, which is why the line
https://github.com/ollama/ollama-python/blob/cb81f522b0f0035acbfeeed87b7902856bda501e/ollama/_client.py#L174-L175
also affects the user's list/tuple of messages.
The Solution
Obviously this can be avoided on the user's end by simply making a deep copy themselves before passing
messages. But because this is a rather obscure issue that is never mentioned in the documentation, I recommend simply removing the possiblility of this side-effect.There are multiple possible solutions to this issue, but because the performance cost of making a deep copy is insignificant in comparison to the performance cost of LLM inference, I went with the easiest fix: simply making a deep copy of
messagesevery timeollama.chatis called, but I am looking forward to hearing the opinions of the Ollama maintainers.🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.