mirror of
https://github.com/ollama/ollama-python.git
synced 2026-07-21 09:05:23 -04:00
ollama.generate() with multimodal llama3.2-vision does not pass images array when raw=True #164
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?
Originally created by @rylativity on GitHub (Nov 9, 2024).
Originally assigned to: @ParthSareen on GitHub.
I am using ollama.generate() to process invoice document images. I would like to set
raw=Trueso I can specify my own full prompt without it being passed into the default template.Here is what I would like to be able to do:
However, when I set

raw=Trueand pass a prompt and image to the model as shown in the screenshot below, the images array does not appear to get passed to the LLM as evidenced by the model's hallucinated response when asked to describe the image.In other instances I've even seen it explicitly state "there is no image for me to describe", further suggesting that the images are not making it to the LLM.
The next screenshot (below) shows a response when I set

raw=False(default arg value) where the model is clearly receiving the image and able to provide an appropriate, expected response.I know that additional templating and formatting is being done to the inputs when
raw=Falsebased on the model's Ollama configuration, but I would think that there should be a way to pass images to a multimodal model when passing the prompt through as raw.I also understand that there are peculiarities with multimodal models in Ollama, so Is this a known limitation, is it a bug, or are there additional steps required to pass images alongside a prompt when
raw=True?Thanks in advance for your help.
@ParthSareen commented on GitHub (Nov 11, 2024):
Might be better to use the chat interface instead in this case:
Let me know if this helps! I will check it out on the generate end in the meantime :)
@rylativity commented on GitHub (Nov 11, 2024):
Hi @ParthSareen thanks for your quick reply.
The example prompt, "What is in this image", is not the actual prompt for my use-case. It was just the simplest prompt I could use to both determine that the image was not getting passed when
raw=Trueand to demonstrate the issue here.For my actual use case, I would like to pass the prompt through as raw so that I guide the output and ensure it is parsable. And the way I generally do that is by starting the assistant's response for it and setting a reasonable stop sequence. For example, in the prompt below, I include the start/end sequence tokens for llama3 and I begin the assistant's response for it with "```csv". I would also then set a stop sequence of three backticks so that I can force the LLM to respond in a (somewhat) predictable format:
I would like to be able to pass a prompt like the one above and an image into
ollama.generate()withraw=Trueto generate predictably formatted output. If I useollama.generate()withraw=False, or if I useollama.chat(), the LLM always begins it's response with something like "Sure, here is the data you requested formatted as a table..." (which then requires post-processing to be able to parse), because my prompt is being filled into a chat prompt template.As a workaround, I've split it up into two prompts and
ollama.generate()calls - one withraw=Falseto just identify the data in the image and a second withraw=Trueto take the text-only output from the first prompt (but not the image itself) and format it in the expected format. But this isn't ideal because each page takes approximately twice as long to process since there are two LLM calls per page.Ideally, I would like to be able to pass the example prompt above and an image to
ollama.generate()withraw=True()and still have the model be able to "see" the image so that I can accomplish the task with a single LLM call.@ParthSareen commented on GitHub (Nov 12, 2024):
Dug through our API and the Python SDK - I'd recommend to use the
.chatfunctionality even in this case.I think you'd still be able to get away with one LLM call and just do post processing after you get the response from the model. If your pipeline is vision model -> structured data it would make sense why you're not getting as good of results back either. Vision LLM -> Cleanup LLM or functions -> structured data makes more sense given the Ollama's structure for now but we do have lots in the pipeline thinking along similar goals.
@rylativity commented on GitHub (Nov 12, 2024):
Thanks @ParthSareen, I appreciate the advice, and that is helpful.
But back to the root issue - images do not seem to be making it to the LLM when calling
ollama.generate()withraw=True(e.gollama.generate(prompt = "abc", images = [img_bytes], raw=True). I am trying to understand if that is a bug (but is supposed to work today), a roadmap item, or a capability that will never be implemented for one reason or another.Is there a correct way to pass images to a multimodal LLM in Ollama when calling
ollama.generate()withraw=Truetoday, or is passing images whenraw=Truenot possible today? If it is not possible today, is that something you think might be addressed down the line?@ParthSareen commented on GitHub (Nov 12, 2024):
Hey! Yeah for sure.
raw=Truewith images not possible at this time@rylativity commented on GitHub (Nov 12, 2024):
Ok, thanks for clarifying and for all the helpful info!
@jhud commented on GitHub (Dec 21, 2024):
I checked the code (https://github.com/ollama/ollama/blob/d8bab8ea4403d3fb05a9bf408e638195b72bebf9/server/routes.go#L234C14-L234C15) and I can't see a reason why images + raw shouldn't work (I don't know Go). This would be super useful to me - do you have any pointers as to why it's not possible, so that I could try forking it? Thank you!
@rylativity commented on GitHub (Dec 21, 2024):
I am also not super familiar with Go, so I could be off base here, but at first glance it seems the issue with images + raw might arise from the following:
In the file you linked, lines 205-231 preprocess the images regardless of whether raw is true or false. However, lines 260-266, which seem to add the template fields needed for the images to be passed into the completion() call later on, only run if raw is false. When raw is true, lines 235-288, which includes the code responsible for adding the image fields to the prompt, will not run.
When we finally get down to line 297 which seems to create the completion object to be passed to the API, the "prompt" passed on line does not include the template fields needed for the completion API to add the images to the prompt.
If that is indeed what is happening, in order to make images + raw prompts work correctly, I believe either the user would need to add the appropriate image template fields to their raw prompt prior to calling ollama.generate(), or ollama would need to add slightly opinionated logic to add the necessary image template fields to the raw prompt, perhaps enabled by an argument in ollama.generate() (e.g. ollama.generate(prompt=prompt, images=images, raw=True, add_images_to_prompt=True).
@rylativity commented on GitHub (Dec 21, 2024):
As I mentioned, I am not particularly familiar with Go so I'm wary of spinning my wheels on this.
But I'd be curious to hear @ParthSareen's opinion on whether the solution above might work or other things to look out for, and I'd be happy to try to implement the fix described above if it seems like a reasonable solution.
@rylativity commented on GitHub (Dec 22, 2024):
I have opened a pull request here for the implemented functionality - https://github.com/ollama/ollama/pull/8209
In the meantime, I have discovered that you can manually pass a placeholder for your image(s) inside your raw prompt and the multimodal model will then be able to see them.
Here is an example request that will work right now even without the PR being merged:
Note that the
[img-0]<image>tag can be placed anywhere in the prompt, and the0corresponds to the index of the image in theimagesarray.@jhud commented on GitHub (Dec 22, 2024):
Thank you - fantastic work! Wonderful to wake up and see that someone has already worked out a solution.
For what it's worth, here is my obvious patch, using your find:
Then the usual API call:
@rylativity commented on GitHub (Dec 22, 2024):
@jhud glad to hear that this has helped!
I have opened this related PR on the python library to support the proposed new kwarg in the python client - https://github.com/ollama/ollama-python/pull/392