All Products
Search
Document Center

Vector Retrieval Service for Milvus:Keyword extraction

Last Updated:Sep 14, 2026

AI_KEYWORDS is an AI Function in Alibaba Cloud Milvus that extracts representative keywords from text, images, or videos. Use it for search indexing, article tagging, and cross-language asset retrieval.

Command syntax

AI_KEYWORDS supports two invocation modes:

  • REST API — Call the endpoint directly for on-demand keyword extraction from text, images, or videos.

  • Collection Function — Embed keyword extraction in a collection schema so that keywords are automatically extracted when data is inserted.

REST API

REST API

Send a POST request to the AI keywords endpoint:

POST /v2/vectordb/ai/keywords
Content-Type: application/json

{
  "model_name": "<model_name>",
  "texts": ["<text_or_media_url>"],
  "params": {"max_keywords": 10, "target_language": "zh"}
}

Collection Function (Python SDK)

Collection Function (Python SDK)

Define keyword extraction as a Collection Function in the schema. The function triggers automatically on insert and writes results to the specified output field:

schema = MilvusClient.create_schema(auto_id=True, enable_dynamic_field=False)
schema.add_field("id", DataType.INT64, is_primary=True)
schema.add_field("content", DataType.VARCHAR, max_length=8192)
schema.add_field("keywords", DataType.JSON)
schema.add_field("dummy_vector", DataType.FLOAT_VECTOR, dim=2)
schema.add_function(
    Function(
        name="extract_keywords",
        function_type=texttransform_function_type(),
        input_field_names=["content"],
        output_field_names=["keywords"],
        params={
            "provider": "aliyun_milvus",
            "model_name": "<model_name>",
            "task": "ai_keywords",
            "max_keywords": "5",
            "target_language": "en",
            "temperature": "0",
        },
    )
)

Parameters

ParameterDescription
model_nameRequired. Model name. For images or videos, use a configured multi-modal model (for example, qwen3.7-plus).
textsRequired for REST API. Text content to process, or a URL pointing to an image or video.
max_keywordsOptional. Maximum number of keywords to extract. Default: 10. Valid range: 1–50.
target_languageOptional. Language for the output keywords. Defaults to the input language. Supported values: zh, en, es, fr, de, ja, ko, ru, ar, and pt.
promptOptional. Additional extraction rules, up to 5,000 characters. Does not support ${...} template variable interpolation.
media_typeOptional. Valid values: image or video.
temperature/max_concurrency/timeout_secOptional. Model stability, concurrency, and timeout settings.
provider/taskRequired for Collection Function only. Fixed to aliyun_milvus and ai_keywords respectively.

Return values

Each output item is a JSON string in the format {"keywords":["keyword 1","keyword 2"]}. When AI_KEYWORDS is used as a Collection Function, the schema automatically parses this result and writes it to the JSON output field.

Example 1: Auto-tag technical articles with keywords (text)

A knowledge base needs to write article keywords to an index field to support filtering and aggregation.

REST API

REST API

#!/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": "qwen3.7-max",
  "texts": ["Milvus is an open-source vector database for high-performance approximate nearest neighbor search in RAG, recommendation, and image search."],
  "params": {"max_keywords": 5, "target_language": "en", "temperature": 0}
}
JSON
)
RESPONSE_BODY="$(post_json "/v2/vectordb/ai/keywords" "$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 (Collection Function)

Python (Collection Function)

The following example uses shared utility functions (texttransform_function_type and run_texttransform_example) that handle schema creation, function registration, data insertion, and result querying. Examples 2 and 3 reuse these same utilities.

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
    # Alibaba Cloud Milvus provides TEXTTRANSFORM as a managed extension (function type value 9);
    # some pymilvus versions do not have this enum member built in, while Function(...) validates through 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)

client = MilvusClient(uri=MILVUS_URI, token=MILVUS_TOKEN)

run_texttransform_example(
    client=client,
    collection_name="simple_ai_keywords_text",
    input_fields=[("content", DataType.VARCHAR, 8192)],
    output_field=("keywords", DataType.JSON, None),
    function_name="extract_keywords",
    function_params={"provider": "aliyun_milvus", "model_name": "qwen3.7-max", "task": "ai_keywords", "max_keywords": "5", "target_language": "en", "temperature": "0"},
    rows=[{"content": "Milvus is an open-source vector database for high-performance approximate nearest neighbor search in RAG, recommendation, and image search.", "dummy_vector": [0.1, 0.2]}],
)

Expected result: The keywords field returns {"keywords":["Milvus","vector database","approximate nearest neighbor search","RAG","open-source"]} (English keywords when target_language=en).

Example 2: Extract keywords from product images (image)

A media team needs to generate English keywords from images to support cross-language asset search.

REST API

REST API

#!/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":"qwen3.7-plus","texts":["https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260415/hynnff/wan-video-edit-clothes.webp"],"params":{"media_type":"image","max_keywords":5,"target_language":"en","temperature":0,"timeout_sec":120,"enable_thinking":false}}
JSON
)
RESPONSE_BODY="$(post_json "/v2/vectordb/ai/keywords" "$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 (Collection Function)

Python (Collection Function)

This example uses the same utility functions defined in Example 1. Only the unique invocation is shown below.

client = MilvusClient(uri=MILVUS_URI, token=MILVUS_TOKEN)

run_texttransform_example(
    client=client,
    collection_name="simple_ai_keywords_image",
    input_fields=[("image_url", DataType.VARCHAR, 4096)],
    output_field=("keywords", DataType.JSON, None),
    function_name="extract_image_keywords",
    function_params={"provider": "aliyun_milvus", "model_name": "qwen3.7-plus", "task": "ai_keywords", "media_type": "image", "max_keywords": "5", "target_language": "en", "temperature": "0"},
    rows=[{"image_url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260415/hynnff/wan-video-edit-clothes.webp", "dummy_vector": [0.1, 0.2]}],
)

Expected result: The keywords field returns up to 5 English keywords extracted from the image in the format {"keywords":[...]}.

Example 3: Extract keywords from video assets (video)

A video library needs to auto-tag videos to enable retrieval by topic and scene.

REST API

REST API

#!/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":"qwen3.7-plus","texts":["https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260409/dozxak/Wan_Video_Edit_33_1.mp4"],"params":{"media_type":"video","max_keywords":5,"target_language":"en","temperature":0}}
JSON
)
RESPONSE_BODY="$(post_json "/v2/vectordb/ai/keywords" "$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 (Collection Function)

Python (Collection Function)

This example uses the same utility functions defined in Example 1. Only the unique invocation is shown below.

client = MilvusClient(uri=MILVUS_URI, token=MILVUS_TOKEN)

run_texttransform_example(
    client=client,
    collection_name="simple_ai_keywords_video",
    input_fields=[("video_url", DataType.VARCHAR, 4096)],
    output_field=("keywords", DataType.JSON, None),
    function_name="extract_video_keywords",
    function_params={"provider": "aliyun_milvus", "model_name": "qwen3.7-plus", "task": "ai_keywords", "media_type": "video", "max_keywords": "5", "target_language": "en", "temperature": "0"},
    rows=[{"video_url": "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20260409/dozxak/Wan_Video_Edit_33_1.mp4", "dummy_vector": [0.1, 0.2]}],
)

Expected result: The keywords field returns up to 5 English keywords related to the video topic, which can be written to video retrieval fields.