文档
所需 Python SDK 版本: 1.1.0
某些模型(称为 VLM,即视觉语言模型)可以接受图像作为输入。您可以使用 .respond() 方法将图像传递给模型。
如果您还没有 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)
如果您只有图像的原始数据,可以直接将其作为字节对象(bytes object)提供,而无需先将其写入磁盘。由于此功能的存在,不支持二进制文件系统路径(因为它们会被处理为格式错误的图像数据,而不是文件系统路径)。
二进制 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)
此页面的源代码可在 GitHub 上获取