> ## Documentation Index
> Fetch the complete documentation index at: https://docs.siliconflow.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 视觉语言模型

## 1. 使用场景

视觉语言模型（VLM）是一种能够同时接受视觉（图像）和语言（文本）两种模态信息输入的大语言模型。基于视觉语言模型，可以传入图像及文本信息，模型能够理解同时理解图像及上下文中的信息并跟随指令做出响应。如：

1. 视觉内容解读：要求模型对图片中包含的信息进行解读、描述，如包含的事物、文字，事物的空间关系，图像的颜色、气氛等；
2. 结合视觉内容及上下文，开展多轮会话；
3. 部分替代 OCR 等传统机器视觉模型；
4. 随着模型能力的持续提升，未来还可以用于视觉智能体、机器人等领域。

## 2. 使用方式

对于 VLM 模型，可在调用 `/chat/completions` 接口时，构造包含 `图片 url` 或 `base64 编码图片` 的 `message` 消息内容进行调用。通过 `detail` 参数控制对图像的预处理方式。

### 2.1 关于图片细节控制参数说明

SiliconFlow 提供 `low`，`high`，`auto` 三个 `detail` 参数选项。
对于目前支持的模型，`detail` 不指定或指定为 `high` 时会采用 `high`（“高分辨率”）模式，而指定为 `low` 或者 `auto` 时会采用 `low`（“低分辨率”）模式。

### 2.2 包含图像的 `message` 消息格式示例

#### 2.2.1 使用图片 url 形式

```json theme={null}
{
    "role": "user",
    "content":[
        {
            "type": "image_url",
            "image_url": {
                "url": "https://sf-maas.s3.us-east-1.amazonaws.com/images/recDq23epr.png",
                "detail":"high"
            }
        },
        {
            "type": "text",
            "text": "text-prompt here"
        }
    ]
}
```

#### 2.2.2 base64 形式

```json theme={null}

{
    "role": "user",
    "content":[
        {
            "type": "image_url",
            "image_url": {
                "url": f"data:image/jpeg;base64,{base64_image}",
                "detail":"low"
            }
        },
        {
            "type": "text",
            "text": "text-prompt here"
        }
    ]
}
```

```python 图片base64转换示例 theme={null}
from PIL import Image
import io
import base64
def convert_image_to_webp_base64(input_image_path):
    try:
        with Image.open(input_image_path) as img:
            byte_arr = io.BytesIO()
            img.save(byte_arr, format='webp')
            byte_arr = byte_arr.getvalue()
            base64_str = base64.b64encode(byte_arr).decode('utf-8')
            return base64_str
    except IOError:
        print(f"Error: Unable to open or convert the image {input_image_path}")
        return None

base64_image=convert_image_to_webp_base64(input_image_path)
```

#### 2.2.3 多图片形式，其中每个图片可以是上述两种形式之一

<Note>请注意，`DeepseekVL2` 系列模型适用于处理短上下文，建议最多传入 2 张图片。若传入超过 2 张图片，模型将自动调整图片尺寸为 384\*384，且指定的 detail 参数将无效。</Note>

```json theme={null}
{
    "role": "user",
    "content":[
        {
            "type": "image_url",
            "image_url": {
                "url": "https://sf-maas.s3.us-east-1.amazonaws.com/images/recDq23epr.png",
            }
        },
        {
            "type": "image_url",
            "image_url": {
                "url": f"data:image/jpeg;base64,{base64_image}"
            }
        },
        {
            "type": "text",
            "text": "text-prompt here"
        }
    ]
}
```

## 3. 支持模型列表

目前已支持的 VLM 模型：

* Qwen 系列：
  * Qwen/Qwen2-VL-72B-Instruct
* DeepseekVL2 系列：
  * deepseek-ai/deepseek-vl2

<Note>注意：支持的 VLM 模型可能发生调整，请在「模型广场」筛选“视觉”标签，了解支持的模型列表。</Note>

## 4. 视觉输入内容计费方式

对于图片等视觉输入内容，模型会将其转化为 tokens，与文本信息一并作为模型输出的上下文信息，因此也会一并进行计费。不同模型的视觉内容转化方式不同，以下为目前支持模型的转化方式。

### 4.1 Qwen 系列

规则：

`Qwen` 最高支持像素是 `3584 * 3584= 12845056`，最低支持像素是 `56 * 56 = 3136`，会对先对每张图片长短边均放缩至 28 的倍数 `(h * 28) * (w * 28)`。如果不在最小像素和最大像素区间内，再等比缩放至该区间。

1. `detail=low` 时将所有图片 resize 成 `448 * 448` 尺寸，最终对应 `256 tokens`；
2. `detail=high` 时等比缩放，首先将长宽按照最近的 `28` 倍数向上取整，然后再等比缩放至像素区间 `(3136, 12845056)`，并保证长宽均为 `28` 整数倍。

示例：

* `224 * 448` 和 `1024 x 1024` 和 `3172 x 4096` 的图片，选择 `detail=low` 时，均消耗 `256 tokens`；
* `224 * 448` 的图片，选择 `detail=high` 时，因为 `224 * 448` 在像素区间内，且长宽均为 `28` 倍数，消耗 `(224/28) * (448/28) = 8 * 16 = 128 tokens`；
* `1024 * 1024` 的图片，选择 `detail=high` 时，将长宽按照 `28` 的倍数向上取整至 `1036 * 1036`，该数值在像素区间内，消耗 ` (1036/28) * (1036/28) = 1369 tokens`；
* `3172 * 4096` 的图片，选择 `detail=high` 时，将长宽按照 `28` 的倍数向上取整至 `3192 * 4116`，该值超过最大像素，再将长宽等比例缩小至 `3136 * 4060`，消耗 `(3136/28) * (4060/28) = 16240 tokens`。

### 4.2 DeepseekVL2 系列

规则：

`DeepseekVL2`对于每张图片，会处理`global_view`和`local_view`两部分。`global_view`将原图片统一 resize 成`384*384`像素大小，local\_view 会将每张图片划分成若干`384*384`的块大小。图片中间会根据宽度增加额外 token 来衔接。

1. `detail=low`时将所有图片 resize 成`384*384`尺寸
2. `detail=high`时会根据长宽比例，将图片 resize 成长宽均为`384(OpenAI是512)`的倍数， `(h*384) * (w * 384)`, 且`1 <= h*w <=9`。

* 放缩的长宽`h * w`按照如下规则选择：

  * `h`和`w`均为整数，在满足`1 <= h*w <=9`约束下，按照`(h, w)`组合遍历。
  * 将图片 resize 成`(h*384, w*384)`像素时，和原图片的像素比较，取新图片像素和原图片像素的最小值作为有效像素值，取原图片像素值与有效像素值之差作为无效像素值。如果有效像素值超过之前判定的有效像素值，或者当有效像素值和之前持平，但是无效像素值更小时，选择当前`(h*384, w*384)`组合

* token 消耗按照如下规则：
  * `(h*w + 1) * 196 + (w+1) * 14 + 1  token`

示例：

* `224 x 448` 和 `1024 x 1024` 和 `2048 x 4096` 的图片，选择`detail=low`时，均消耗`421token`.
* `384 x 768`的图片，选择`detail=high`时，长宽比为`1:1`, 会缩放至`384 x 768`,  此时`h=1, w=2`, 消耗
  `(1*2 +1)*196+(2+1)*14+1=631 token`.
* `1024 x 1024`的图片，选择`detail=high`时，会缩放至`1152*1152(h=w=3)`, 消耗`(3*3 + 1) * 196 + (3+1)*14+1 = 2017 token`.
* `2048 x 4096`的图片，选择`detail=high`时，长宽比例为`1:2`,  按照规则缩放至 `768*1536(h=2,w=4)`, 消耗 `(2*4 + 1) * 196 + (4+1)*14+1 = 1835 token`.

### 4.3 GLM-4.1V-9B-Thinking

规则：

`GLM-4.1V` 最小支持像素`28 * 28`，以`28`像素为单位，对图片按照比例将长宽缩放至最接近的`28`整数倍像素。
如果缩放后像素小于`112 * 112`，或者大于 `4816894`，那么按照`28`像素的倍数，等比例缩放长宽至区间。

1. `detail=low` 时将所有图片`resize 成`448\*448`尺寸, 最终对应 `256 token\`。
2. `detail=high`时等比缩放，首先将长宽按照最近的`28`倍数取整，然后再等比缩放至像素区间`(12544, 4816894)`，并保证长宽均为`28`整数倍。

示例:

* `224 x 448` 和 `1024 x 1024` 和 `3172 x 4096` 的图片，选择`detail=low`时，均消耗`256token`.
* `224 x 448`的图片, 选择`detail=high`时,  因为`224x448`在像素区间内，且长宽均为`28`倍数，消耗 `(224//28) * (448//28) = 8 * 16 = 128 token`.
* `1024 x 1024`的图片, 选择`detail=high`时,  将长宽按照`28`的倍数取整为`1036*1036`, 该数值在像素区间内, 消耗 `(1036//28) * (1036//28) = 1369 token`.
* `3172 x 4096`的图片, 选择`detail=high`时, 将长宽按照28的倍数取整至`3192 x 4088`, 该值超过最大像素,  再将长宽等比例缩小至 `1932 x 2464`, 消耗`(1932//28) * (2464//28) = 6072 token`.

## 5. 使用示例

### 5.1. 示例 1 图片理解

```python theme={null}
import json  
from openai import OpenAI

client = OpenAI(
    api_key="您的 API KEY", # 从 https://cloud.siliconflow.com/account/ak获取
    base_url="https://api.siliconflow.com/v1"
)

response = client.chat.completions.create(
        model="Qwen/Qwen2-VL-72B-Instruct",
        messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://sf-maas.s3.us-east-1.amazonaws.com/images/recu6XreBFQ0st.png"
                    }
                },
                {
                    "type": "text",
                    "text": "Describe the image."
                }
            ]
        }],
        stream=True
)

for chunk in response:
    chunk_message = chunk.choices[0].delta.content
    print(chunk_message, end='', flush=True)
```

### 5.2. 示例 2 多图理解

```python theme={null}
import json  
from openai import OpenAI

client = OpenAI(
    api_key="您的 API KEY", # 从 https://cloud.siliconflow.com/account/ak 获取
    base_url="https://api.siliconflow.c/v1"
)

response = client.chat.completions.create(
        model="Qwen/Qwen2-VL-72B-Instruct",
        messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://sf-maas.s3.us-east-1.amazonaws.com/images/recu6XreBFQ0st.png"
                    }
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://sf-maas.s3.us-east-1.amazonaws.com/images/recu6Xrf2Cd0cn.png"
                    }
                },
                {
                    "type": "text",
                    "text": "Identify the similarities between these images."
                }
            ]
        }],
        stream=True
)

for chunk in response:
    chunk_message = chunk.choices[0].delta.content
    print(chunk_message, end='', flush=True)

```
