多模态理解 (Vision)

GLM-4V 系列多模态模型,支持图像理解和视觉问答。

API 端点

POST/chat/completions

多模态对话

请求参数

参数类型必填说明
modelstring必填模型名称:GLM-4V, GLM-4V-Plus
messagesarray必填消息列表,content 可包含文本和 image_url
temperaturenumber可选采样温度,默认 0.7
max_tokensinteger可选生成的最大 token 数量

请求示例

请求示例
{
  "model": "GLM-4V-Plus",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "这张图片里有什么?"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "https://example.com/image.jpg"
          }
        }
      ]
    }
  ]
}

响应示例

响应示例
{
  "id": "chatcmpl-456",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "GLM-4V-Plus",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "这张图片显示的是一只可爱的猫咪..."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 1200,
    "completion_tokens": 80,
    "total_tokens": 1280
  }
}

代码示例

Python

from openai import OpenAI

client = OpenAI(
    api_key="your-api-key",
    base_url="https://your-proxy-domain.com/v1"
)

response = client.chat.completions.create(
    model="GLM-4V-Plus",
    messages=[
        {
            "role": "user",
            "content": [
                {"type": "text", "text": "这张图片里有什么?"},
                {
                    "type": "image_url",
                    "image_url": {"url": "https://example.com/image.jpg"}
                }
            ]
        }
    ]
)

print(response.choices[0].message.content)

JavaScript

import OpenAI from 'openai';

const client = new OpenAI({
  apiKey: 'your-api-key',
  baseURL: 'https://your-proxy-domain.com/v1'
});

async function analyzeImage() {
  const response = await client.chat.completions.create({
    model: 'GLM-4V-Plus',
    messages: [
      {
        role: 'user',
        content: [
          { type: 'text', text: '这张图片里有什么?' },
          {
            type: 'image_url',
            image_url: { url: 'https://example.com/image.jpg' }
          }
        ]
      }
    ]
  });

  console.log(response.choices[0].message.content);
}

analyzeImage();

cURL

curl https://your-proxy-domain.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer your-api-key" \
  -d '{
    "model": "GLM-4V-Plus",
    "messages": [
      {
        "role": "user",
        "content": [
          {"type": "text", "text": "这张图片里有什么?"},
          {
            "type": "image_url",
            "image_url": {"url": "https://example.com/image.jpg"}
          }
        ]
      }
    ]
  }'