ollama-python icon indicating copy to clipboard operation
ollama-python copied to clipboard

How to use images

Open vedbobo opened this issue 11 months ago • 3 comments

How to use images for Q&A using the Moonbeam model? data= ollama.generate( model="moondream:latest", images=[image_data], prompt="What is the error message shown in the code?" ) print(data.response) # is '' Like this, but no response, where is the configuration problem.

vedbobo avatar Feb 19 '25 01:02 vedbobo

我试过像REST API那样把图像的base64码放在message列表传给模型,但是模型识别不了该图像,而是回答我传了一串符号或者公式;此外,我还试过把图片路径直接放到message列表传递,但模型回答热仍然是错误的。目前,我只在cmd下使用模型正确识别到了图片,所以我怀疑ollama-python库还没有这个功能.

wawbwb avatar Feb 23 '25 10:02 wawbwb

python实现REST API请求传递图片给大模型,亲测可行

请求的URL

url = "http://localhost:11434/api/generate"

图像路径

image_path = r'D:\用户\智学伴\demo4.png'

将图像转换为Base64编码

def image_to_base64(image_path): image = Image.open(image_path) buffered = BytesIO() image.save(buffered, format="JPEG") return base64.b64encode(buffered.getvalue()).decode()

获取图像的Base64编码

base64_string = image_to_base64(image_path)

print(base64_string)

请求的Payload数据

data = { "model": "minicpm-v", "prompt": "图片里是什么?如果是问题,请详细描述,包括题干、选项和提供的材料如图片的图例和走势等,但是不要回答问题", "stream": False, "images": [base64_string] }

发送POST请求

response = requests.post(url, json=data)

wawbwb avatar Feb 23 '25 13:02 wawbwb

感谢! 我之前没有转base64,现在转了后还是返回的是空,可能这个模型不能这样弄。不用ollama,单独调这个模型可以。 试了几个图片,发现有的图片可以识别出来,有的不行,还是模型原因。

vedbobo avatar Feb 24 '25 02:02 vedbobo