Azure OpenAICompatible with Milvus 2.6.x
本主題描述如何在 Milvus 中配置和使用 Azure OpenAI 嵌入功能。
選擇嵌入模型
Milvus 支援 Azure OpenAI 提供的所有嵌入模型。以下是目前可用的 Azure OpenAI 嵌入模型,以供快速參考:
模型 |
尺寸 |
最大代幣 |
說明 |
|---|---|---|---|
文字嵌入-3-小 |
預設:1,536 (可截取至 1536 以下的尺寸) |
8,191 |
最適合對成本敏感且可擴充的語意搜尋-以較低的價格提供強大的效能。 |
文字嵌入-3-大 |
預設:3,072 (可分割為 3072 以下的尺寸) |
8,191 |
最適合需要增強檢索準確性和更豐富語意表示的應用程式。 |
文字嵌入-ADA-002 |
固定:1,536 (不支援截斷) |
8,191 |
前一代模型適合傳統管道或需要向後相容性的情況。 |
第三代嵌入模型(text-embedding-3) 支援透過dim 參數減少嵌入的大小。從運算、記憶體和儲存的角度來看,較大的嵌入通常較昂貴。能夠調整維度的數量,就能更有效地控制整體成本和效能。如需各種模型的詳細資訊,請參閱Embeddings。
配置憑證
Milvus 必須知道您的 Azure OpenAI API 金鑰,才能請求嵌入。Milvus 提供兩種配置憑證的方法:
設定檔案 (建議使用):將 API 金鑰儲存在
milvus.yaml,以便每次重新啟動和節點都會自動取得。環境變數:在部署時注入金鑰 - 最適合 Docker Compose。
在以下兩種方法中選擇一種--配置檔案在裸機和虛擬機器上較容易維護,而 env-var 路線則適合容器工作流程。
如果相同提供者的 API 金鑰同時出現在組態檔案和環境變數中,Milvus 會始終使用milvus.yaml 中的值,而忽略環境變數。
選項 1:組態檔案 (建議使用且優先順序較高)
將您的 API 金鑰保留在milvus.yaml ;Milvus 會在啟動時讀取它們,並覆寫相同提供者的任何環境變數。
**在下列位置宣告您的金鑰
credential:您可以列出一個或多個 API 金鑰 - 給每個金鑰一個您自創的標籤,稍後可以參考。
# milvus.yaml credential: apikey_dev: # dev environment apikey: <YOUR_DEV_KEY> apikey_prod: # production environment apikey: <YOUR_PROD_KEY>將 API 金鑰放在這裡可以讓它們在重新啟動時保持不變,並讓您只需更改標籤就可以切換金鑰。
告訴 Milvus Azure OpenAI 呼叫使用哪個金鑰
在同一個檔案中,將 Azure OpenAI 提供者指向您希望它使用的標籤。
function: textEmbedding: providers: azure_openai: credential: apikey_dev # ← choose any label you defined above resource_name: # Your azure openai resource name # url: # Your azure openai embedding url這將特定的金鑰綁定到 Milvus 傳送給 Azure OpenAI embeddings endpoint 的每個請求。
選項 2:環境變數
當您使用 Docker Compose 執行 Milvus,並希望不在檔案和影像中洩露秘密時,請使用此方法。
只有在milvus.yaml 中找不到提供者的金鑰時,Milvus 才會回退到環境變數。
變數 |
需要 |
說明 |
|---|---|---|
|
是 |
在每個 Milvus 容器中提供 Azure OpenAI 金鑰(當 |
|
是 |
當您建立 Azure OpenAI 服務資源時所定義的 Azure OpenAI 資源名稱。 |
在你的docker-compose.yaml檔案中,設定環境變數。
# docker-compose.yaml (standalone service section)
standalone:
# ... other configurations ...
environment:
# ... other environment variables ...
# Set the environment variable pointing to the Azure OpenAI API key inside the container
MILVUSAI_AZURE_OPENAI_API_KEY: <MILVUSAI_AZURE_OPENAI_API_KEY>
MILVUSAI_AZURE_OPENAI_RESOURCE_NAME: <MILVUSAI_AZURE_OPENAI_RESOURCE_NAME>
environment: 區塊只會將金鑰注入 Milvus 容器,而不會碰觸您的主機作業系統。詳情請參考使用 Docker Compose 設定 Milvus。
使用嵌入功能
一旦配置了憑證,請按照以下步驟定義和使用嵌入函數。
步驟 1:定義模式欄位
若要使用嵌入函式,請建立具有特定模式的集合。此模式必須包含至少三個必要欄位:
唯一識別集合中每個實體的主要欄位。
儲存要嵌入的原始資料的標量欄位。
預留向量欄位,用來儲存函式將為標量欄位產生的向量嵌入。
以下範例定義了一個模式,其中一個標量欄位"document" 用來儲存文字資料,另一個向量欄位"dense" 用來儲存函式模組要產生的嵌入資料。切記設定向量維度 (dim) 以符合您所選擇的嵌入模型輸出。
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.
schema.add_field("dense", DataType.FLOAT_VECTOR, dim=1536)
步驟 2:在模式中加入嵌入函數
定義好嵌入函數後,將其加入集合模式。這會指示 Milvus 使用指定的嵌入函數來處理和儲存文字資料的嵌入。
# Define embedding function specifically for Azure OpenAI provider
text_embedding_function = Function(
name="azopenai", # Unique identifier for this embedding function
function_type=FunctionType.TEXTEMBEDDING, # Indicates a text embedding function
input_field_names=["document"], # Scalar field(s) containing text data to embed
output_field_names=["dense"], # Vector field(s) for storing embeddings
params={ # Provider-specific embedding parameters
"provider": "azure_openai", # Embedding provider name (must be "azure_openai")
"model_name": "zilliz-text-embedding-3-small", # Model should be set to the deployment name you chose when you deployed the embedding model
# Optional parameters (only specify if necessary):
# "url": "https://{resource_name}.openai.azure.com/" # Optional: Your Azure OpenAI service endpoint
# "credential": "apikey_dev", # Optional: Credential label specified in milvus.yaml
# "dim": "1536", # Optional: Shorten the output vector dimension
# "user": "user123", # Optional: identifier for API tracking
}
)
# Add the configured embedding function to your existing collection schema
schema.add_function(text_embedding_function)
下一步
設定好嵌入函式後,請參閱函式概述,以取得索引設定、資料插入範例和語意搜尋作業的其他指引。