LM StudioLM Studio

图像输入

用于将图像作为输入传递给模型的 API

所需 Python SDK 版本1.1.0

某些模型(称为 VLM,即视觉语言模型)可以接受图像作为输入。您可以使用 .respond() 方法将图像传递给模型。

前提条件:获取 VLM(视觉语言模型)

如果您还没有 VLM,可以使用以下命令下载 qwen2-vl-2b-instruct 等模型

lms get qwen2-vl-2b-instruct

实例化模型

连接到 LM Studio 并获取您想要使用的 VLM(视觉语言模型)句柄。

import lmstudio as lms

model = lms.llm("qwen2-vl-2b-instruct")

准备图像

使用 prepare_image() 函数或 files 命名空间方法获取随后可传递给模型的图像句柄。

import lmstudio as lms

image_path = "/path/to/image.jpg" # Replace with the path to your image
image_handle = lms.prepare_image(image_path)

如果您只有图像的原始数据,可以直接将其作为字节对象提供,而无需先将其写入磁盘。由于此特性,*不*支持二进制文件系统路径(因为它们将被视为格式错误的图像数据,而不是文件系统路径)。

二进制 IO 对象也被接受为本地文件输入。

LM Studio 服务器支持 JPEG、PNG 和 WebP 图像格式。

.respond() 中将图像传递给模型

通过在 .respond() 方法中将图像传递给模型来生成预测。

import lmstudio as lms

image_path = "/path/to/image.jpg" # Replace with the path to your image
image_handle = lms.prepare_image(image_path)
model = lms.llm("qwen2-vl-2b-instruct")
chat = lms.Chat()
chat.add_user_message("Describe this image please", images=[image_handle])
prediction = model.respond(chat)
© . This site is unofficial and not affiliated with Element Labs, Inc.