Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use images #448

Open
vedbobo opened this issue Feb 19, 2025 · 3 comments
Open

How to use images #448

vedbobo opened this issue Feb 19, 2025 · 3 comments

Comments

@vedbobo
Copy link

vedbobo commented Feb 19, 2025

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.

@wawbwb
Copy link

wawbwb commented Feb 23, 2025

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

@wawbwb
Copy link

wawbwb commented Feb 23, 2025

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)

@vedbobo
Copy link
Author

vedbobo commented Feb 24, 2025 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants