Embeddings 功能介绍:Milvus 2.6 如何简化向量化和语义搜索
如果你曾经创建过一个向量搜索应用程序,那么你对工作流程就已经略知一二了。在存储任何数据之前,首先必须使用 Embeddings 模型将其转换为向量,然后进行清理和格式化,最后将其输入向量数据库。每个查询也要经过同样的过程:嵌入输入、运行相似性搜索,然后将得到的 ID 映射回原始文档或记录。这样做是可行的,但却会产生一个由预处理脚本、Embeddings 管道和胶水代码组成的分布式纠结系统,您必须对其进行维护。
Milvus 是一个高性能开源向量数据库,现在它朝着简化所有这些工作迈出了重要一步。Milvus 2.6引入了数据输入、数据输出功能(也称为 嵌入功能),这是一种内置嵌入功能,可直接连接 OpenAI、AWS Bedrock、Google Vertex AI 和 Hugging Face 等主要模型提供商。Milvus 现在可以为你调用这些模型,而无需管理自己的嵌入式基础架构。您还可以使用原始文本进行插入和查询--很快还可以使用其他数据类型--而 Milvus 会在写入和查询时自动处理向量。
在本篇文章的其余部分,我们将详细介绍 Data-in、Data-out 在引擎盖下的工作原理、如何配置提供程序和 Embeddings 函数,以及如何使用它来简化端到端的向量搜索工作流。
什么是数据输入、数据输出?
Milvus 2.6 中的 "数据输入,数据输出 "建立在新的功能模块之上--该框架使 Milvus 能够在内部处理数据转换和嵌入生成,而无需任何外部预处理服务。(您可以关注GitHub issue #35856 中的设计建议)有了这个模块,Milvus 可以获取原始输入数据,直接调用嵌入提供者,并自动将生成的向量写入您的 Collections。
在高层次上,功能模块将嵌入生成转化为一种本地数据库功能。Milvus 现在不再运行单独的嵌入管道、后台工作者或 Rerankers 服务,而是向配置的提供商发送请求,检索嵌入,并将它们与数据一起存储--所有这些都在摄取路径内。这消除了管理自己的 Embeddings 基础设施的操作开销。
数据输入、数据输出对 Milvus 工作流程进行了三大改进:
直接插入原始数据--您现在可以将未经处理的文本、图像或其他数据类型直接插入 Milvus。无需事先将其转换为向量。
配置一个嵌入功能--一旦你在 Milvus 中配置了嵌入模型,它就会自动管理整个嵌入过程。Milvus 与一系列模型提供商无缝集成,包括 OpenAI、AWS Bedrock、Google Vertex AI、Cohere 和 Hugging Face。
使用原始输入进行查询--您现在可以使用原始文本或其他基于内容的查询执行语义搜索。Milvus 使用相同的配置模型动态生成嵌入,执行相似性搜索,并返回相关结果。
简而言之,Milvus 现在可以自动嵌入--并可选择重新ankers--您的数据。向量化成为数据库的内置功能,无需外部嵌入服务或自定义预处理逻辑。
数据输入和输出的工作原理
下图说明了数据输入、数据输出在 Milvus 内部的操作符。
数据输入、数据输出工作流程可分为六个主要步骤:
输入数据--用户将原始数据(如文本、图像或其他内容类型)直接插入 Milvus,无需进行任何外部预处理。
生成嵌入模型--功能模块通过其第三方应用程序接口自动调用配置的嵌入模型,实时将原始输入转换为向量嵌入。
存储嵌入模型--Milvus 将生成的嵌入模型写入 Collections 中指定的向量字段,以便进行相似性搜索操作。
提交查询--与输入阶段一样,用户向 Milvus 发出原始文本或基于内容的查询。
语义搜索--Milvus使用相同的配置模型嵌入查询,在存储的向量上运行相似性搜索,并确定最接近的语义匹配。
返回结果--Milvus 将最相似的前 k 个结果(映射回其原始数据)直接返回给应用程序。
如何配置数据输入、数据输出
先决条件
安装最新版本的Milvus 2.6。
准备好支持提供商(如 OpenAI、AWS Bedrock 或 Cohere)提供的嵌入式 API 密钥。在本例中,我们将使用Cohere作为嵌入提供商。
修改milvus.yaml 配置
如果使用Docker Compose 运行 Milvus,则需要修改milvus.yaml 文件以启用 Function 模块。您可以参考官方文档以获得指导:使用 Docker Compose 配置 Milvus(也可在此处找到其他部署方法的说明)。
在配置文件中,找到credential 和function 部分。
然后,更新apikey1.apikey 和providers.cohere 字段。
...
credential:
aksk1:
access_key_id: # Your access_key_id
secret_access_key: # Your secret_access_key
apikey1:
apikey: "***********************" # Edit this section
gcp1:
credential_json: # base64 based gcp credential data
# Any configuration related to functions
function:
textEmbedding:
providers:
...
cohere: # Edit the section below
credential: apikey1 # The name in the crendential configuration item
enable: true # Whether to enable cohere model service
url: "https://api.cohere.com/v2/embed" # Your cohere embedding url, Default is the official embedding url
...
...
完成这些更改后,重启 Milvus 以应用更新的配置。
如何使用数据输入、数据输出功能
1.定义 Collections 的 Schema
要启用 Embeddings 功能,您的Collections Schema必须至少包含三个字段:
主键字段 (
id) - 唯一标识 Collections 中的每个实体。标量字段 (
document) - 存储原始数据。向量字段 (
dense) - 存储生成的向量嵌入。
from pymilvus import MilvusClient, DataType, Function, FunctionType
# Initialize Milvus client
client = MilvusClient(
uri="http://localhost:19530",
)
# Create a new schema for the collection
schema = client.create_schema()
# Add primary field "id"
schema.add_field("id", DataType.INT64, is_primary=True, auto_id=False)
# Add scalar field "document" for storing textual data
schema.add_field("document", DataType.VARCHAR, max_length=9000)
# Add vector field "dense" for storing embeddings.
# IMPORTANT: Set `dim` to match the exact output dimension of the embedding model.
# For instance, OpenAI's text-embedding-3-small model outputs 1536-dimensional vectors.
# For dense vector, data type can be FLOAT_VECTOR or INT8_VECTOR
schema.add_field("dense", DataType.FLOAT_VECTOR, dim=1536) # Set dim according to the embedding model you use.
2.定义嵌入函数
接下来,在 Schema 中定义嵌入函数。
name- 函数的唯一标识符。function_type- 文本嵌入时设置为FunctionType.TEXTEMBEDDING。Milvus 还支持其他函数类型,如FunctionType.BM25和FunctionType.RERANK。详情请参阅全文搜索和衰减排名器概述。input_field_names- 定义原始数据的输入字段 (document)。output_field_names- 定义存储向量嵌入的输出字段 (dense)。params- 包含嵌入函数的配置参数。provider和model_name的值必须与milvus.yaml配置文件中的相应条目一致。
注意:每个函数都必须有唯一的name 和output_field_names ,以区分不同的转换逻辑并防止冲突。
# Define embedding function (example: OpenAI provider)
text_embedding_function = Function(
name="cohere_embedding", # Unique identifier for this embedding function
function_type=FunctionType.TEXTEMBEDDING, # Type of embedding function
input_field_names=["document"], # Scalar field to embed
output_field_names=["dense"], # Vector field to store embeddings
params={ # Provider-specific configuration (highest priority)
"provider": "cohere", # Embedding model provider
"model_name": "embed-v4.0", # Embedding model
# "credential": "apikey1", # Optional: Credential label
# Optional parameters:
# "dim": "1536", # Optionally shorten the vector dimension
# "user": "user123" # Optional: identifier for API tracking
}
)
# Add the embedding function to your schema
schema.add_function(text_embedding_function)
3.配置索引
定义字段和函数后,为 Collections 创建索引。为简单起见,我们在此使用 AUTOINDEX 类型作为示例。
# Prepare index parameters
index_params = client.prepare_index_params()
# Add AUTOINDEX to automatically select optimal indexing method
index_params.add_index(
field_name="dense",
index_type="AUTOINDEX",
metric_type="COSINE"
)
4.创建 Collections
使用定义的 Schema 和索引创建新的 Collections。在本例中,我们将创建一个名为 Demo 的 Collection。
# Create collection named "demo"
client.create_collection(
collection_name='demo',
schema=schema,
index_params=index_params
)
5.插入数据
现在,你可以直接在 Milvus 中插入原始数据--无需手动生成 Embeddings。
# Insert sample documents
client.insert('demo', [
{'id': 1, 'document': 'Milvus simplifies semantic search through embeddings.'},
{'id': 2, 'document': 'Vector embeddings convert text into searchable numeric data.'},
{'id': 3, 'document': 'Semantic search helps users find relevant information quickly.'},
])
6.执行向量搜索
插入数据后,您可以直接使用原始文本查询执行搜索。Milvus 会自动将你的查询转换为嵌入,根据存储的向量执行相似性搜索,并返回最匹配的结果。
# Perform semantic search
results = client.search(
collection_name='demo',
data=['How does Milvus handle semantic search?'], # Use text query rather than query vector
anns_field='dense', # Use the vector field that stores embeddings
limit=1,
output_fields=['document'],
)
print(results)
# Example output:
# data: ["[{'id': 1, 'distance': 0.8821347951889038, 'entity': {'document': 'Milvus simplifies semantic search through embeddings.'}}]"]
有关向量搜索的更多详情,请参阅:基本向量搜索 和查询 API。
开始使用 Milvus 2.6
通过数据输入、数据输出,Milvus 2.6 将向量搜索的简易性提升到了一个新的水平。通过在 Milvus 中直接集成 Embeddings 和 Rerankers 功能,您不再需要管理外部预处理或维护单独的嵌入服务。
准备好试用了吗?立即安装Milvus2.6,亲身体验数据输入、数据输出的强大功能。
有问题或想深入了解任何功能?加入我们的 Discord 频道或在 GitHub 上提交问题。您还可以通过 Milvus Office Hours 预订 20 分钟的一对一课程,以获得见解、指导和问题解答。
了解有关 Milvus 2.6 功能的更多信息
Try Managed Milvus for Free
Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.
Get StartedLike the article? Spread the word



