视觉#

学习如何使用 LLM 处理图像。

介绍#

通过 vision 能力,您可以让模型接收图像并回答有关它们的问题。在 Xinference 中,这表示某些模型在通过 Chat API 进行对话时能够处理图像输入。

支持的模型列表#

在 Xinference 中支持 vision 功能的模型如下:

  • qwen-vl-chat

快速入门#

模型可以通过两种主要方式获取图像:通过传递图像的链接或直接在请求中传递 base64 编码的图像。

使用 OpenAI 客户端的示例#

import openai

client = openai.Client(
    api_key="cannot be empty",
    base_url=f"http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
response = client.chat.completions.create(
    model="<MODEL_UID>",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What’s in this image?"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "http://i.epochtimes.com/assets/uploads/2020/07/shutterstock_675595789-600x400.jpg",
                    },
                },
            ],
        }
    ],
)
print(response.choices[0])

上传 Base64 编码的图片#

import openai
import base64

# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
    return base64.b64encode(image_file.read()).decode('utf-8')

# Path to your image
image_path = "path_to_your_image.jpg"

# Getting the base64 string
b64_img = encode_image(image_path)

client = openai.Client(
    api_key="cannot be empty",
    base_url=f"http://<XINFERENCE_HOST>:<XINFERENCE_PORT>/v1"
)
response = client.chat.completions.create(
    model="<MODEL_UID>",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "What’s in this image?"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{b64_img}",
                    },
                },
            ],
        }
    ],
)
print(response.choices[0])

你可以在教程笔记本中找到更多关于 vision 能力的示例。

Qwen VL Chat

通过使用 qwen-vl-chat 的示例来学习使用 LLM 的视觉能力