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",
},
)
)
參數說明
|
參數 |
說明 |
|
|
必填。使用 |
|
|
必填。編輯目標描述;Schema 中可使用 |
|
|
單圖輸入的圖片數組,每個元素是一條任務;不能與 |
|
|
多圖輸入的二維數組,內部數組是一條任務的圖片列表;同一請求中各任務的圖片數量必須一致。 |
|
|
僅 Schema 可選。指定圖片欄位,預設第一個輸入欄位;可寫為 |
|
|
可選,預設 |
|
|
可選,按模型能力透傳。 |
|
|
可選,分別控制單次逾時和批量並發數。 |
|
|
僅 Collection Function 必填,固定為 |
傳回值說明
n=1 時,data.output.outputs 的元素為圖片 URL;n>1 時,每個元素為包含 images 數組的 JSON 字串。data.usage 可能包含 image_tokens 和 total_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=2,data.output.outputs[0] 是含 images 數組(2 個非空圖片 URL)的 JSON 字串。
注意:ai_image_edit在n>1時,Collection 的輸出欄位必須為JSON(不能用VARCHAR),否則建立集合會報output field must be a JSON field for task [ai_image_edit] when n > 1。