共用方式為


資料來源 - Azure 機器學習 索引 (預覽)

使用 Azure OpenAI On Your Data 時,Azure 機器學習 索引的可設定選項。 API 版本 2024-02-15-preview支援此資料來源。

名稱 類型​​ 必要 描述
parameters 參數 True 設定 Azure 機器學習 索引時要使用的參數。
type string True 必須是 azure_ml_index

參數

姓名 類型​​ 必要 描述
project_resource_id string True Azure 機器學習 項目的資源標識符。
name string True Azure 機器學習 索引名稱。
version string True Azure 機器學習 索引的版本。
authentication 其中一個 AccessTokenAuthenticationOptionsSystemAssignedManagedIdentityAuthenticationOptionsUserAssignedManagedIdentityAuthenticationOptions True 存取已定義數據源時要使用的驗證方法。
in_scope boolean False 查詢是否應該限制為使用索引數據。 預設值為 True
role_information string False 提供模型關於其行為方式的指示,以及產生回應時應該參考的任何內容。 您可以描述助理的個性,並告知其如何格式化回應。
strictness 整數 False 已設定的搜尋相關性篩選嚴格性。 嚴格程度越高,精確度越高,但對答案的召回率較低。 預設值為 3
top_n_documents 整數 False 設定的檔數目已設定為已設定查詢的功能。 預設值為 5
filter string False 搜尋篩選條件。 只有在 Azure 機器學習 索引的類型為 Azure 搜尋服務時,才支援。

存取令牌驗證選項

使用存取令牌時,Azure OpenAI On Your Data 的驗證選項。

名稱 類型​​ 必要 描述
access_token string True 要用於驗證的存取令牌。
type string True 必須是 access_token

系統指派的受控識別驗證選項

使用系統指派的受控識別時,Azure OpenAI On Your Data 的驗證選項。

名稱 類型​​ 必要 描述
type string True 必須是 system_assigned_managed_identity

使用者指派的受控識別驗證選項

使用使用者指派的受控識別時,Azure OpenAI On Data 的驗證選項。

名稱 類型​​ 必要 描述
managed_identity_resource_id string True 要用於驗證的使用者指派受控識別資源標識碼。
type string True 必須是 user_assigned_managed_identity

範例

先決條件:

  • 設定從 Azure OpenAI 系統指派受控識別到 Azure 機器學習 工作區資源的角色指派。 必要角色: AzureML Data Scientist
  • 設定從使用者到 Azure OpenAI 資源的角色指派。 必要角色: Cognitive Services OpenAI User
  • 安裝 Az CLI 並執行 az login
  • 定義下列環境變數:AzureOpenAIEndpoint、、、ChatCompletionsDeploymentNameProjectResourceIdIndexNameIndexVersion
  • 如果您使用 MINGW 請執行 export MSYS_NO_PATHCONV=1
export AzureOpenAIEndpoint=https://example.openai.azure.com/
export ChatCompletionsDeploymentName=turbo
export ProjectResourceId='/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.MachineLearningServices/workspaces/{workspace-id}'
export IndexName=testamlindex
export IndexVersion=2

安裝最新的 pip 套件 openaiazure-identity

import os
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider

endpoint = os.environ.get("AzureOpenAIEndpoint")
deployment = os.environ.get("ChatCompletionsDeploymentName")
project_resource_id = os.environ.get("ProjectResourceId")
index_name = os.environ.get("IndexName")
index_version = os.environ.get("IndexVersion")

token_provider = get_bearer_token_provider(
    DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")

client = AzureOpenAI(
    azure_endpoint=endpoint,
    azure_ad_token_provider=token_provider,
    api_version="2024-02-15-preview",
)

completion = client.chat.completions.create(
    model=deployment,
    messages=[
        {
            "role": "user",
            "content": "Who is DRI?",
        },
    ],
    extra_body={
        "data_sources": [
            {
                "type": "azure_ml_index",
                "parameters": {
                    "project_resource_id": project_resource_id,
                    "name": index_name,
                    "version": index_version,
                    "authentication": {
                        "type": "system_assigned_managed_identity"
                    },
                }
            }
        ]
    }
)

print(completion.model_dump_json(indent=2))