Milvus
Zilliz
  • Home
  • Blog
  • 介紹嵌入功能:Milvus 2.6 如何簡化向量化和語意搜尋

介紹嵌入功能:Milvus 2.6 如何簡化向量化和語意搜尋

  • Engineering
December 03, 2025
Xuqi Yang

如果您曾經建立過向量搜尋應用程式,您就已經對工作流程有點了解了。在儲存任何資料之前,必須先使用嵌入模型將資料轉換成向量,然後進行清理和格式化,最後才輸入向量資料庫。每個查詢也要經過相同的程序:嵌入輸入、執行相似性搜尋,然後將結果 ID 對映回原始文件或記錄。這樣做是可行的,但卻會產生分散式的預處理腳本、嵌入管道以及您必須維護的膠水程式碼。

Milvus 是一個高效能的開放原始碼向量資料庫,現在朝向簡化這一切邁進了一大步。Milvus 2.6引入了Data-in, Data-out 功能 (也稱為 嵌入功能),這是一種內建的嵌入功能,可直接連接到 OpenAI、AWS Bedrock、Google Vertex AI 和 Hugging Face 等主要模型提供者。Milvus 現在可以為您呼叫這些模型,而無需管理您自己的嵌入基礎架構。您也可以使用原始文字進行插入與查詢 - 很快也會使用其他資料類型 - 而 Milvus 會在寫入與查詢時自動處理向量化。

在這篇文章的其餘部分,我們將進一步瞭解 Data-in, Data-out 在引擎蓋下的運作方式、如何設定提供者和嵌入函式,以及您如何使用它來簡化向量搜尋端對端的工作流程。

什麼是 Data-in、Data-out?

Milvus 2.6 中的 Data-in, Data-out 是建構在新的 Function 模組上 - 一個能讓 Milvus 在內部處理資料轉換和嵌入產生的框架,不需要任何外部的預處理服務。(您可以參考GitHub issue #35856 中的設計提案。)有了這個模組,Milvus 可以取得原始輸入資料,直接呼叫嵌入提供者,並自動將產生的向量寫入您的集合中。

從高層次來看,Function模組將嵌入產生轉換為原生資料庫功能。Milvus 現在不需要執行獨立的嵌入管道、後台工作人員或 reranker 服務,而是將請求傳送至您設定的提供者、擷取嵌入,並將它們與您的資料一起儲存 - 所有這些都在攝取路徑中。這消除了管理您自己的嵌入式基礎架構的作業開銷。

Data-in, Data-out 為 Milvus 工作流程引入了三項重大改進:

  • 直接插入原始資料- 您現在可以直接將未處理的文字、影像或其他資料類型插入 Milvus。不需要事先將它們轉換成向量。

  • 配置一個嵌入功能- 一旦您在 Milvus 中配置了嵌入模型,它就會自動管理整個嵌入過程。Milvus 可與一系列模型提供者無縫整合,包括 OpenAI、AWS Bedrock、Google Vertex AI、Cohere 和 Hugging Face。

  • 使用原始輸入進行查詢- 您現在可以使用原始文字或其他基於內容的查詢來執行語意搜尋。Milvus 會使用相同的配置模型來即時產生嵌入、執行相似性搜尋,並傳回相關結果。

簡而言之,Milvus 現在會自動嵌入 - 並選擇性地重整 - 您的資料。矢量化成為內建的資料庫功能,無需外部嵌入服務或客製化預處理邏輯。

資料輸入、資料輸出如何運作

下圖說明了 Data-in、Data-out 如何在 Milvus 中運作。

資料輸入、資料輸出的工作流程可分為六個主要步驟:

  1. 輸入資料- 使用者直接將原始資料,例如文字、圖片或其他內容類型,插入 Milvus,而不需執行任何外部預處理。

  2. 產生嵌入- 功能模組透過其第三方 API 自動調用設定的嵌入模型,即時將原始輸入轉換為向量嵌入。

  3. 儲存嵌入資料 - Milvus 會將產生的嵌入資料寫入資料集中指定的向量欄位,以便進行相似性搜尋作業。

  4. 提交查詢- 使用者向 Milvus 發出原始文字或基於內容的查詢,就像輸入階段一樣。

  5. 語意搜尋- Milvus 使用相同的配置模型嵌入查詢,在儲存的向量上執行相似性搜尋,並決定最接近的語意匹配。

  6. 回傳結果- Milvus 會將最相似的前 k 個結果(映射回原始資料)直接回傳到應用程式。

如何設定資料輸入、資料輸出

先決條件

  • 安裝最新版本的Milvus 2.6

  • 從支援的提供者 (例如 OpenAI、AWS Bedrock 或 Cohere) 準備您的嵌入式 API 金鑰。在本範例中,我們將使用Cohere作為嵌入提供者。

修改milvus.yaml 設定

如果您使用Docker Compose 執行 Milvus,您需要修改milvus.yaml 檔案以啟用 Function 模組。您可以參考官方文件的指引:使用 Docker Compose 設定 Milvus(其他部署方法的說明也可以在這裡找到)。

在設定檔中,找到credentialfunction 兩個部分。

然後更新欄位apikey1.apikeyproviders.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.定義資料集的模式

要啟用嵌入功能,您的集合模式必須包含至少三個欄位:

  • 主鍵欄位 (id ) - 獨一無二地識別集合中的每個實體。

  • Scalar 欄位 (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.定義嵌入函數

接下來,在模式中定義嵌入函數。

  • name - 功能的唯一識別碼。

  • function_type - 對於文字嵌入,設定為FunctionType.TEXTEMBEDDING 。Milvus 也支援其他函數類型,例如FunctionType.BM25FunctionType.RERANK 。詳情請參閱全文檢索Decay Ranker 總覽

  • input_field_names - 定義原始資料的輸入欄位 (document)。

  • output_field_names - 定義向量嵌入的輸出欄位 (dense)。

  • params - 包含嵌入函數的設定參數。providermodel_name 的值必須與milvus.yaml 設定檔中的相應項目相符。

注意:每個函式必須有唯一的nameoutput_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.設定索引

定義欄位和函式後,為集合建立索引。為了簡單起見,我們在此以 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.建立集合

使用已定義的模式和索引來建立新的集合。在本範例中,我們將建立一個名為 Demo 的集合。

# Create collection named "demo"
client.create_collection(
    collection_name='demo', 
    schema=schema, 
    index_params=index_params
)

5.插入資料

現在您可以直接插入原始資料到 Milvus - 不需要手動產生嵌入。

# 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.'},
])

插入資料後,您可以直接使用原始文字查詢執行搜尋。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 內直接整合嵌入和重排功能,您不再需要管理外部預處理或維護單獨的嵌入服務。

準備好試用了嗎?立即安裝Milvus2.6,親身體驗 Data-in, Data-out 的強大功能。

對任何功能有問題或想要深入瞭解?加入我們的 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 Started

    Like the article? Spread the word

    繼續閱讀