写入与查询自动Embedding

更新时间:
复制为 MD 格式

自动Embedding技术通过内置预训练开源模型或阿里云百炼模型,将文本、图片或视频自动转化为向量,消除了传统方案中手动定义向量字段的繁琐流程。本文介绍了如何在Lindorm系统中实现自动Embedding数据的写入与查询方法。

前提条件

  • 已开通向量引擎。如何开通,请参见开通向量引擎

  • 已开通搜索引擎,且搜索引擎为3.9.10及以上版本。如何开通,请参见开通指南。如何查看或升级当前版本,请参见搜索引擎版本说明升级小版本

    重要

    如果您的搜索引擎为3.9.10以下版本,但控制台显示已是最新版本,请联系Lindorm技术支持(钉钉号:s0s3eg3)。

  • 已开通AI引擎,如何开通,请参见开通指南。如果需要使用阿里云百炼模型,则需要在开通时选择Dashscope模型。如果没有相关选项,请联系Lindorm技术支持(钉钉号:sOs3eg3)。

    image.png

    说明
    • 由于AI引擎的功能实现依赖于宽表引擎,因此在开通AI引擎时必须同时开通宽表引擎。

  • 已将客户端IP地址添加至Lindorm白名单,具体操作请参见设置白名单

操作步骤概览

操作步骤

涉及引擎

说明

AI引擎部署Embedding模型(可选)

AI引擎

通过curl命令调用AI引擎RESTful API,部署Embedding模型,用于指定类型的数据转换为向量。

创建写入Pipeline

搜索引擎,AI引擎(需要进行参数校验)

在搜索引擎中创建写入Pipeline,用于在写入数据时,自动将写入数据转换为向量数据(Embedding)。

创建查询Pipeline

搜索引擎,AI引擎(需要进行参数校验)

在搜索引擎中创建查询Pipeline,用于在查询数据时,自动将查询数据转化为向量数据。

创建索引并指定Pipeline

向量引擎,搜索引擎

在创建或修改向量索引时,需指定写入和查询Pipeline,用于将写入与查询数据自动转换为向量数据。

数据写入

向量引擎,搜索引擎,AI引擎

向新创建的索引中写入数据,写入Pipeline将写入的数据自动转化为向量数据。

数据查询

向量引擎,搜索引擎,AI引擎

向新创建的索引中查询数据,查询Pipeline将查询的数据自动转化为向量数据。

AI引擎部署Embedding模型(可选)

AI引擎部署模型的具体操作请参见模型管理通过curl命令使用AI引擎RESTful API示例

部署BGE_VISUALIZED模型示例如下,参数详情请参见模型管理

重要

curl请求地址URL使用AI引擎的专用网络连接地址。

curl -i -k --location --header 'x-ld-ak:<username>' --header 'x-ld-sk:<password>' -X POST http://<URL>/v1/ai/models/create  -H "Content-Type: application/json" -d '{
          "model_name": "bge_visualized_model",
          "model_path": "huggingface://BAAI/bge-visualized",
          "task": "FEATURE_EXTRACTION",
          "algorithm": "BGE_VISUALIZED_M3",
          "settings": {"instance_count": "2"}
     }'

搜索引擎创建Pipeline

在搜索引擎中创建两种Pipeline,分别用于实现数据写入和查询的自动向量化处理。

重要

curl请求地址URL使用搜索引擎的专有网络连接地址。

文本Embedding

创建写入Pipeline

curl -u <username>:<password> -H "Content-Type: application/json" -XPUT "http://<URL>/_ingest/pipeline/<ingest_pipeline_name>" -d '{
  "description": "demo embedding pipeline",
  "processors": [
    {
      "text-embedding": {
        "inputFields": ["input_field"], 
        "outputFields": ["embedding_field"],
        "userName": "root",
        "password": "test****",
        "url": "http://ld-xxxx-proxy-ai-vpc.lindorm.aliyuncs.com:9002/dashscope/compatible-mode/v1/embeddings",
        "modeName": "text-embedding-v4"
      }
    }
  ]
}'

参数说明

参数

说明

processors

对写入进行Pipeline操作。

text-embedding

固定Key,用于指定processor类型,必须填写。

inputFields

需要进行向量化的字段。

outputFields

向量化后的向量字段。

userName

Lindorm AI引擎的用户名。

password

Lindorm AI引擎的密码。

url

modeName

模型名称,本文对应text-embedding-v4

dimension(可选)

指定生成向量的维度。

重要

仅阿里云百炼系列模型支持指定向量维度,请参考百炼文本Embedding

说明

写入Pipeline中指定的inputFieldsoutputFields,必须与创建向量索引时填写的input_fieldembedding_field保持一致。

创建查询Pipeline

curl -u <username>:<password> -H "Content-Type: application/json" -XPUT "http://<URL>/_search/pipeline/<search_pipeline_name>" -d '{
  "request_processors": [
    {
      "text-embedding" : {
        "tag" : "auto-query-embedding",
        "description" : "Auto query embedding",
        "model_config" : {
          "inputFields": ["text_field"],
          "outputFields": ["text_field_embedding"],
          "userName": "root",
          "password": "test****",
          "url": "http://ld-xxxx-proxy-ai-vpc.lindorm.aliyuncs.com:9002/dashscope/compatible-mode/v1/embeddings",
          "modeName": "text-embedding-v4"
        }
      }
    }
  ]
}'

参数说明

参数

说明

request_processors

表示对搜索请求进行Pipeline操作。

text-embedding

固定Key,用于指定processor类型,必须填写。

inputFields

需要进行向量化的文本字段,起到占位作用。

outputFields

向量化以后的向量字段。

userName

Lindorm AI引擎的用户名。

password

Lindorm AI引擎的密码。

url

modeName

模型名称,本文对应text-embedding-v4

dimension(可选)

指定生成向量的维度。

重要

仅阿里云百炼系列模型支持指定向量维度,请参考百炼文本Embedding

说明

查询Pipeline中指定的outputFields,必须与创建向量索引时填写的embedding_field保持一致。

多模态Embedding

创建写入Pipeline

curl -u <username>:<password> -H "Content-Type: application/json" -XPUT "http://<URL>/_ingest/pipeline/<ingest_pipeline_name>" -d '{
  "description": "demo embedding pipeline",
  "processors": [
    {
      "multimodal-embedding": {
        "input_fields": ["input_field"], 
        "output_fields": ["embedding_field"], 
        "user_name": "root",
        "password": "test****",
        "url": "http://ld-xxxx-proxy-ai-vpc.lindorm.aliyuncs.com:9002/dashscope/api/v1/services/embeddings/multimodal-embedding/multimodal-embedding",
        "model_name": "tongyi-embedding-vision-plus",
        "input_type": "image"
      }
    }
  ]
}'

参数说明

参数

说明

processors

对写入进行Pipeline操作。

multimodal-embedding

固定Key,用于指定processor类型,必须填写。

input_fields

需要进行向量化的字段。

output_fields

向量化后的向量字段。

user_name

Lindorm AI引擎的用户名。

password

Lindorm AI引擎的密码。

url

model_name

模型名称,本文示例为tongyi-embedding-vision-plus

input_type

写入数据类型,目前支持text、image、video类型。

说明

写入Pipeline中指定的input_fieldsoutput_fields,必须与创建向量索引时填写的input_fieldembedding_field保持一致。

创建查询Pipeline

curl -u <username>:<password> -H "Content-Type: application/json" -XPUT "http://<URL>/_search/pipeline/<search_pipeline_name>" -d '{
  "description": "demo embedding pipeline",
  "request_processors": [
    {
      "multimodal-embedding": {
        "model_config" : {
          "input_fields": ["input_field"], 
          "output_fields": ["embedding_field"], 
          "user_name": "root",
          "password": "test****",
          "url": "http://ld-xxxx-proxy-ai-vpc.lindorm.aliyuncs.com:9002/dashscope/api/v1/services/embeddings/multimodal-embedding/multimodal-embedding",
          "model_name": "tongyi-embedding-vision-plus"
        }
      }
    }
  ]
}'

参数说明

参数

说明

request_processors

表示对搜索请求进行Pipeline操作。

multimodal-embedding

固定Key,用于指定processor类型,必须填写。

input_fields

在查询Pipeline中仅起到占位作用。

output_fields

向量化后的向量字段。

user_name

Lindorm AI引擎的用户名。

password

Lindorm AI引擎的密码。

url

model_name

模型名称,本文对应tongyi-embedding-vision-plus

说明

查询Pipeline中指定的output_fields,必须与创建向量索引时填写的embedding_field保持一致。

创建索引并指定Pipeline

在创建向量索引或修改现有向量索引设置时,请指定所需的Pipeline。

重要

curl请求地址URL使用搜索引擎的专有网络连接地址。

创建向量索引

curl -u <username>:<password> -H 'Content-Type: application/json' -XPUT "http://<URL>/<index_name>"  -d '
{
 "settings" : {
    "index": {
      "number_of_shards": 2,
      "knn": true,
      "default_pipeline": <ingest_pipeline_name>,
      "search.default_pipeline": <search_pipeline_name>
    }
  },
  "mappings": {
    "_source": {
      "excludes": ["embedding_field"]
    },
    "properties": {
      "input_field": {
        "type": "text",
        "analyzer": "ik_max_word" 
      },
      "embedding_field": {
        "type": "knn_vector",
        "dimension": 1024,
        "method": {
          "engine": "lvector",
          "name": "hnsw", 
          "space_type": "cosinesimil",
          "parameters": {
            "m": 24,
            "ef_construction": 500
         }
       }
      },
      "tag": {
          "type": "keyword"
      },
      "brand": {
          "type": "keyword"
      },
      "merit" : {
        "type": "text",
        "analyzer": "ik_max_word"  
      }
    }
  }
}'

参数说明

参数

说明

default_pipeline

设置写入Pipeline。

search.default_pipeline

设置查询时自动EmbeddingPipeline。

type

Keyword对应等值,是text分词。需要指定分词器analyzer,建议设置为ik_max_word

dimension

向量的维度,需要与模型输出的向量维度匹配。

其余参数说明请参见参数说明

说明

写入和查询Pipeline中指定的inputFieldsoutputFields,必须与创建向量索引时填写的input_fieldembedding_field保持一致。

修改现有向量索引设置

如果您已经创建了向量索引,可以通过以下方式修改其配置,指定写入和查询时使用的Pipeline,以满足特定的业务需求。

curl -u <username>:<password> -H 'Content-Type: application/json' -XPUT "http://<URL>/<index_name>/_settings"  -d '
{
  "index": {
       "default_pipeline": <ingest_pipeline_name>,
       "search.default_pipeline": <search_pipeline_name>
    }
}
'

数据写入

由于指定了写入的Pipeline,因此,在写入过程中,除了将标量字段input_field写入外,还会根据该Pipelineinput_field编码成向量形式,并将其作为embedding_field一并写入。

重要

curl请求地址URL使用搜索引擎的专有网络连接地址。

curl -u <username>:<password> -H 'Content-Type: application/json' -XPOST "http://<URL>/_bulk?pretty"  -d '
{ "index" : { "_index" : "<index_name>", "_id" : "3982" } }
{ "input_field" : "品牌A 时尚节能无线鼠标(草绿)(眩光.悦动.时尚炫舞鼠标 12个月免换电池 高精度光学寻迹引擎 超细微接收器10米传输距离)", "tag": ["鼠标", "电子产品"], "brand":"品牌A", "merit":"好用、外观漂亮"}
{ "index" : { "_index" : "<index_name>", "_id" : "323519" } }
{ "input_field" : "品牌B 光学鼠标(经典黑)(智能自动对码/1000DPI高精度光学引擎)", "tag": ["鼠标", "电子产品"], "brand":"品牌B", "merit":"质量好、到货速度快、外观漂亮、好用"}
{ "index" : { "_index" : "<index_name>", "_id" : "300265" } }
{ "input_field" : "品牌C 耳塞式耳机 白色(经典时尚)", "tag": ["耳机", "电子产品"], "brand":"品牌C", "merit":"外观漂亮、质量好"}
{ "index" : { "_index" : "<index_name>", "_id" : "6797" } }
{ "input_field" : "品牌D 两刀头充电式电动剃须刀", "tag": ["家用电器", "电动剃须刀"], "brand":"品牌D", "merit":"好用、外观漂亮"}
{ "index" : { "_index" : "<index_name>", "_id" : "8195" } }
{ "input_field" : "品牌E Class4 32G TF卡(micro SD)手机存储卡", "tag": ["存储设备", "存储卡", "SD卡"], "brand":"品牌E", "merit":"容量挺大的、速度快、好用、质量好"}
{ "index" : { "_index" : "<index_name>", "_id" : "13316" } }
{ "input_field" : "品牌E 101 G2 32GB 优盘", "tag": ["存储设备","U盘", "优盘"], "brand":"品牌E", "merit":"好用、容量挺大的、速度快"}
{ "index" : { "_index" : "<index_name>", "_id" : "14103" } }
{ "input_field" : "品牌B 64GB至尊高速移动存储卡 UHS-1制式 读写速度最高可达30MB", "tag": ["存储设备", "存储卡", "SD卡"], "brand":"品牌B", "merit":"容量挺大的、速度快、好用"}
'
说明

multimodal-embedding类型的processor在写入时需要写入与input_type对应格式的数据。

input_type

示例

text

"input_field" : "多模态向量模型"

image

"input_field" : "https://img.alicdn.com/imgextra/i3/O1CN01rdstgY1uiZWt8gqSL_!!6000000006071-0-tps-1970-356.jpg"

video

"input_field" : "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250107/lbcemt/new+video.mp4"

数据查询

重要

curl请求地址URL使用搜索引擎的专有网络连接地址。

纯向量

文字搜索

curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "http://ld-xx-proxy-search-vpc.lindorm.aliyuncs.com:30070/<index_name>/_search?pretty"  -d '
{
  "size": 10,
  "_source": true,
  "query": {
    "knn": {
      "embedding_filed": {
        "query_text": "存储卡",
        "k": 10
      }
    }
  },
  "ext": {
    "lvector": {
      "ef_search": "200"
    }
  }
}'

图片搜索

curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "http://ld-xx-proxy-search-vpc.lindorm.aliyuncs.com:30070/<index_name>/_search?pretty"  -d '
{
  "size": 10,
  "_source": true,
  "query": {
    "knn": {
      "embedding_filed": {
        "query_image": "https://img.alicdn.com/imgextra/i2/O1CN019eO00F1HDdlU4Syj5_!!6000000000724-2-tps-2476-1158.png",
        "k": 10
      }
    }
  },
  "ext": {
    "lvector": {
      "ef_search": "200"
    }
  }
}'
说明
  • text-embedding类型

    • text-embedding类型的request_processor只对KNN查询生效,且KNN查询中指定的向量列必须包含在Pipeline配置文件里指定的outputFields列表中。

    • 在构造KNN查询时,请始终通过query_text参数来提供待Embedding的内容。

      字段

      说明

      query_text

      查询文本,会用这个值作embedding,query_text为固定key。

  • multimodal-embedding类型

    • multimodal-embedding类型的request_processor只对KNN查询生效,且KNN指定的向量列需要存在于pipeline配置中指定的output_fields列表。

    • 在构造KNN查询时,请通过以下参数来提供待Embedding的内容。

      输入格式

      查询自动使用的key

      text

      query_text

      image

      query_image

      video

      query_video

其余参数说明请参见参数说明

向量+属性过滤

curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "http://ld-xx-proxy-search-vpc.lindorm.aliyuncs.com:30070/<index_name>/_search?pretty"  -d '
{
  "size": 10,
  "_source": true,
  "query": {
    "knn": {
      "embedding_field": {
        "query_text": "存储卡",
        "k": 10,
        "filter": {
          "bool": {
            "filter": [{
              "match": {
                "merit": "质量好"
              }
            },
            {
              "term": {
                "brand": "品牌E"
              }
            },
            {
              "terms": {
                "tag": ["SD卡", "存储卡"]
              }
            }]
          }
        }
      }
    }
  },
  "ext": {
    "lvector": {
      "filter_type": "efficient_filter",
      "ef_search": "200"
    }
  }
}'

向量全文融合检索 + 属性过滤

curl -u <username>:<password> -H 'Content-Type: application/json' -XGET "http://ld-xx-proxy-search-vpc.lindorm.aliyuncs.com:30070/<index_name>/_search?pretty"  -d '
{
  "size": 10,
  "_source": true,
  "query": {
    "knn": {
      "embedding_filed": {
        "query_text": "存储卡",
        "filter": {
          "bool": {
            "must": [{
              "bool": {
                "must": [{
                  "match": {
                    "input_field": {
                      "query": "存储卡"
                    }
                  }
                }]
              }
            },
            {
              "bool": {
                "filter": [{
                  "match": {
                    "merit": "质量好"
                  }
                },
                {
                  "term": {
                    "brand": "品牌E"
                  }
                },
                {
                  "terms": {
                    "tag": ["SD卡", "存储卡"]
                  }
                }]
              }
            }]
          }
        },
        "k": 10
      }
    }
  },
  "ext": {
    "lvector": {
      "filter_type": "efficient_filter",
      "hybrid_search_type": "filter_rrf",
      "rrf_rank_constant": "1",
      "ef_search": "200"
    }
  }
}'