全部产品
Search
文档中心

向量检索服务 Milvus 版:图片编辑

更新时间:Aug 03, 2026

AI_IMAGE_EDIT 函数可根据一张或多张输入图片及提示词生成编辑后的图片 URL,可用于商品白底图、背景替换、风格迁移和参考图融合。

命令格式

REST 接口

{
  "model_name": "wan2.7-image-pro",
  "texts": ["<image_url>"],
  "params": {"prompt": "<edit_instruction>", "n": 1}
}

多图输入使用 image_inputs,每个内部数组代表一条编辑任务:

{
  "model_name": "wan2.7-image-pro",
  "image_inputs": [["<base_image_url>", "<reference_image_url>"]],
  "params": {"prompt": "<edit_instruction>", "n": 2}
}

Python

schema = MilvusClient.create_schema(auto_id=True, enable_dynamic_field=False)
schema.add_field("id", DataType.INT64, is_primary=True)
schema.add_field("image_url", DataType.VARCHAR, max_length=4096)
schema.add_field("instruction", DataType.VARCHAR, max_length=512)
schema.add_field("edited_image", DataType.VARCHAR, max_length=8192)
schema.add_field("dummy_vector", DataType.FLOAT_VECTOR, dim=2)
schema.add_function(
    Function(
        name="edit_image",
        function_type=texttransform_function_type(),
        input_field_names=["image_url", "instruction"],
        output_field_names=["edited_image"],
        params={
            "provider": "aliyun_milvus",
            "model_name": "wan2.7-image-pro",
            "task": "ai_image_edit",
            "image_fields": "image_url",
            "prompt": "${instruction}",
            "n": "1",
            "size": "1024*1024",
            "watermark": "false",
            "timeout_sec": "180",
        },
    )
)

参数说明

参数

说明

model_name

必填。使用 wan2.7-image-pro(推荐)或 wan2.7-image 等可同步返回图片 URL 的模型。

prompt

必填。编辑目标描述;Schema 中可使用 ${field_name} 引用其他输入字段。

texts

单图输入的图片数组,每个元素是一条任务;不能与 image_inputs 同时传入。

image_inputs

多图输入的二维数组,内部数组是一条任务的图片列表;同一请求中各任务的图片数量必须一致。

image_fields

仅 Schema 可选。指定图片字段,默认第一个输入字段;可写为 base_image,reference_image 或 JSON 字符串数组。

n

可选,默认 1,范围 1–6。n=1 返回一张图片 URL,n>1 返回图片 URL 列表。

size / negative_prompt / seed / bbox_list / watermark

可选,按模型能力透传。

timeout_sec / max_concurrency

可选,分别控制单次超时和批量并发数。

provider / task

仅 Collection Function 必填,固定为 aliyun_milvusai_image_edit

返回值说明

n=1 时,data.output.outputs 的元素为图片 URL;n>1 时,每个元素为包含 images 数组的 JSON 字符串。data.usage 可能包含 image_tokenstotal_tokens

示例一:单图背景替换(单图 texts,n=1)

将一张人物与宠物合影的背景替换为干净白底,保留主体。默认素材是一张人物与狗的合影; Curl 示例需要 jq。

REST 接口

#!/usr/bin/env bash
set -euo pipefail

MILVUS_REST_BASE_URL="http://c-xxxx.milvus.aliyuncs.com:19530"
MILVUS_AUTH_TOKEN="<yourUsername>:<yourPassword>"

post_json() {
  local path="$1"
  local body="$2"
  curl -X POST \
    "$MILVUS_REST_BASE_URL$path" \
    -H "Authorization: Bearer $MILVUS_AUTH_TOKEN" \
    -H "Content-Type: application/json" \
    -d "$body"
}

BODY=$(cat <<JSON
{
  "model_name": "wan2.7-image-pro",
  "texts": ["https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg"],
  "params": {
    "prompt": "Keep the main subject and replace the background with a clean white background.",
    "n": 1,
    "size": "1024*1024",
    "watermark": false,
    "timeout_sec": 180
  }
}
JSON
)
RESPONSE_BODY="$(post_json "/v2/vectordb/ai/image_edit" "$BODY")"
if command -v jq >/dev/null 2>&1; then
  echo "$RESPONSE_BODY" | jq .
  [ "$(echo "$RESPONSE_BODY" | jq -r '.code // -1')" = "0" ] || exit 1
else
  echo "$RESPONSE_BODY"
fi

Python

from __future__ import annotations

from typing import Any

from pymilvus import DataType, Function, FunctionType, MilvusClient

MILVUS_URI = "http://c-xxxx.milvus.aliyuncs.com:19530"
MILVUS_TOKEN = "<yourUsername>:<yourPassword>"

DUMMY_VECTOR_DIM = 2
TEXTTRANSFORM_FUNCTION_TYPE = 9

def texttransform_function_type() -> Any:
    for type_name in ("TEXTTRANSFORM", "TEXT_TRANSFORM", "TextTransform"):
        function_type = getattr(FunctionType, type_name, None)
        if function_type is not None:
            return function_type
    # 阿里云 Milvus 将 TEXTTRANSFORM 作为托管扩展(函数类型值 9)提供;
    # 部分 pymilvus 版本尚未内置该枚举成员,而 Function(...) 通过 FunctionType(...) 校验。
    existing = getattr(FunctionType, "_value2member_map_", {}).get(TEXTTRANSFORM_FUNCTION_TYPE)
    if existing is not None:
        return existing
    extension = int.__new__(FunctionType, TEXTTRANSFORM_FUNCTION_TYPE)
    extension._name_ = "TEXTTRANSFORM"
    extension._value_ = TEXTTRANSFORM_FUNCTION_TYPE
    FunctionType._value2member_map_[TEXTTRANSFORM_FUNCTION_TYPE] = extension
    FunctionType._member_map_["TEXTTRANSFORM"] = extension
    return extension

def add_id(schema: Any) -> None:
    schema.add_field("id", DataType.INT64, is_primary=True)

def add_dummy_vector(schema: Any) -> None:
    schema.add_field("dummy_vector", DataType.FLOAT_VECTOR, dim=DUMMY_VECTOR_DIM)

def run_texttransform_example(*, client, collection_name, input_fields, output_field, function_name, function_params, rows) -> None:
    if client.has_collection(collection_name):
        client.drop_collection(collection_name)
    schema = MilvusClient.create_schema(auto_id=True, enable_dynamic_field=False)
    add_id(schema)
    for name, data_type, max_length in input_fields:
        field_params = {"max_length": max_length} if max_length is not None else {}
        schema.add_field(name, data_type, **field_params)
    output_name, output_data_type, output_max_length = output_field
    output_params = {"max_length": output_max_length} if output_max_length is not None else {}
    schema.add_field(output_name, output_data_type, **output_params)
    add_dummy_vector(schema)
    schema.add_function(
        Function(
            name=function_name,
            function_type=texttransform_function_type(),
            input_field_names=[name for name, _, _ in input_fields],
            output_field_names=[output_name],
            params=function_params,
        )
    )
    index_params = client.prepare_index_params()
    index_params.add_index(field_name="dummy_vector", index_type="AUTOINDEX", metric_type="COSINE")
    client.create_collection(collection_name=collection_name, schema=schema, index_params=index_params)
    client.insert(collection_name, rows)
    client.flush(collection_name)
    fields = [name for name, _, _ in input_fields] + [output_name]
    for row in client.query(collection_name, filter="", output_fields=fields, limit=len(rows)):
        print(row)

MODEL_NAME = "wan2.7-image-pro"
client = MilvusClient(uri=MILVUS_URI, token=MILVUS_TOKEN)

run_texttransform_example(
    client=client,
    collection_name="simple_ai_image_edit_schema",
    input_fields=[("image_url", DataType.VARCHAR, 4096), ("instruction", DataType.VARCHAR, 512)],
    output_field=("edited_image", DataType.VARCHAR, 8192),
    function_name="edit_image",
    function_params={"provider": "aliyun_milvus", "model_name": MODEL_NAME, "task": "ai_image_edit", "image_fields": "image_url", "prompt": "${instruction}", "n": "1", "size": "1024*1024", "watermark": "false", "timeout_sec": "180"},
    rows=[{"image_url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg", "instruction": "Keep the main subject and replace the background with a clean white background.", "dummy_vector": [0.1, 0.2]}],
)

预期结果:data.output.outputs[0] 是非空的编辑后图片 URL,可写入会员纪念素材的 edited_image 字段(VARCHAR)。生成图的具体布景具有非确定性,不应按某一固定画面断言成功。

示例二:人宠合影与服装参考图生成品牌概念图

以人物与宠物合影作为主体参考,以服装素材作为色彩和质感参考,一次生成两张社交媒体概念图供人工选片。

REST 接口

#!/usr/bin/env bash
set -euo pipefail

MILVUS_REST_BASE_URL="http://c-xxxx.milvus.aliyuncs.com:19530"
MILVUS_AUTH_TOKEN="<yourUsername>:<yourPassword>"

post_json() {
  local path="$1"
  local body="$2"
  curl -X POST \
    "$MILVUS_REST_BASE_URL$path" \
    -H "Authorization: Bearer $MILVUS_AUTH_TOKEN" \
    -H "Content-Type: application/json" \
    -d "$body"
}

BODY=$(cat <<JSON
{
  "model_name": "wan2.7-image-pro",
  "image_inputs": [["https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg", "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260415/hynnff/wan-video-edit-clothes.webp"]],
  "params": {
    "prompt": "Keep the person, dog, and seaside composition from the first image. Use the second image only as clothing color and texture reference. Generate brand-safe social media concept images.",
    "n": 2,
    "size": "1024*1024",
    "watermark": false,
    "timeout_sec": 180
  }
}
JSON
)
RESPONSE_BODY="$(post_json "/v2/vectordb/ai/image_edit" "$BODY")"
if command -v jq >/dev/null 2>&1; then
  echo "$RESPONSE_BODY" | jq .
  [ "$(echo "$RESPONSE_BODY" | jq -r '.code // -1')" = "0" ] || exit 1
else
  echo "$RESPONSE_BODY"
fi

Python

from __future__ import annotations

from typing import Any

from pymilvus import DataType, Function, FunctionType, MilvusClient

MILVUS_URI = "http://c-xxxx.milvus.aliyuncs.com:19530"
MILVUS_TOKEN = "<yourUsername>:<yourPassword>"

DUMMY_VECTOR_DIM = 2
TEXTTRANSFORM_FUNCTION_TYPE = 9

def texttransform_function_type() -> Any:
    for type_name in ("TEXTTRANSFORM", "TEXT_TRANSFORM", "TextTransform"):
        function_type = getattr(FunctionType, type_name, None)
        if function_type is not None:
            return function_type
    # 阿里云 Milvus 将 TEXTTRANSFORM 作为托管扩展(函数类型值 9)提供;
    # 部分 pymilvus 版本尚未内置该枚举成员,而 Function(...) 通过 FunctionType(...) 校验。
    existing = getattr(FunctionType, "_value2member_map_", {}).get(TEXTTRANSFORM_FUNCTION_TYPE)
    if existing is not None:
        return existing
    extension = int.__new__(FunctionType, TEXTTRANSFORM_FUNCTION_TYPE)
    extension._name_ = "TEXTTRANSFORM"
    extension._value_ = TEXTTRANSFORM_FUNCTION_TYPE
    FunctionType._value2member_map_[TEXTTRANSFORM_FUNCTION_TYPE] = extension
    FunctionType._member_map_["TEXTTRANSFORM"] = extension
    return extension

def add_id(schema: Any) -> None:
    schema.add_field("id", DataType.INT64, is_primary=True)

def add_dummy_vector(schema: Any) -> None:
    schema.add_field("dummy_vector", DataType.FLOAT_VECTOR, dim=DUMMY_VECTOR_DIM)

def run_texttransform_example(*, client, collection_name, input_fields, output_field, function_name, function_params, rows) -> None:
    if client.has_collection(collection_name):
        client.drop_collection(collection_name)
    schema = MilvusClient.create_schema(auto_id=True, enable_dynamic_field=False)
    add_id(schema)
    for name, data_type, max_length in input_fields:
        field_params = {"max_length": max_length} if max_length is not None else {}
        schema.add_field(name, data_type, **field_params)
    output_name, output_data_type, output_max_length = output_field
    output_params = {"max_length": output_max_length} if output_max_length is not None else {}
    schema.add_field(output_name, output_data_type, **output_params)
    add_dummy_vector(schema)
    schema.add_function(
        Function(
            name=function_name,
            function_type=texttransform_function_type(),
            input_field_names=[name for name, _, _ in input_fields],
            output_field_names=[output_name],
            params=function_params,
        )
    )
    index_params = client.prepare_index_params()
    index_params.add_index(field_name="dummy_vector", index_type="AUTOINDEX", metric_type="COSINE")
    client.create_collection(collection_name=collection_name, schema=schema, index_params=index_params)
    client.insert(collection_name, rows)
    client.flush(collection_name)
    fields = [name for name, _, _ in input_fields] + [output_name]
    for row in client.query(collection_name, filter="", output_fields=fields, limit=len(rows)):
        print(row)

MODEL_NAME = "wan2.7-image-pro"
client = MilvusClient(uri=MILVUS_URI, token=MILVUS_TOKEN)

# 每项 image_inputs 包含基础图和参考图;n=2 返回两个候选结果。
# 注意:ai_image_edit 在 n > 1 时,输出字段必须为 DataType.JSON(不能用 VARCHAR)。
run_texttransform_example(
    client=client,
    collection_name="simple_ai_image_edit_multi_schema",
    input_fields=[
        ("base_image_url", DataType.VARCHAR, 4096),
        ("reference_image_url", DataType.VARCHAR, 4096),
        ("instruction", DataType.VARCHAR, 1024),
    ],
    output_field=("edited_images", DataType.JSON, None),
    function_name="edit_images_with_reference",
    function_params={
        "provider": "aliyun_milvus",
        "model_name": MODEL_NAME,
        "task": "ai_image_edit",
        "image_fields": '["base_image_url","reference_image_url"]',
        "prompt": "${instruction}",
        "n": "2",
        "size": "1024*1024",
        "watermark": "false",
        "timeout_sec": "180",
    },
    rows=[
        {
            "base_image_url": "https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg",
            "reference_image_url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260415/hynnff/wan-video-edit-clothes.webp",
            "instruction": "Keep the person, dog, and seaside composition from the base image. Use the reference image only as clothing color and texture guidance. Generate brand-safe social media concept images.",
            "dummy_vector": [0.1, 0.2],
        }
    ],
)

预期结果:因为 n=2data.output.outputs[0] 是含 images 数组(2 个非空图片 URL)的 JSON 字符串。

注意:ai_image_editn>1 时,Collection 的输出字段必须为 JSON(不能用 VARCHAR),否则创建集合会报 output field must be a JSON field for task [ai_image_edit] when n > 1