適用於 PostgreSQL 的 Azure 資料庫彈性伺服器中的 azure_ai 擴充功能

適用於 PostgreSQL 的 Azure 資料庫 中的 azure_ai 擴充功能使大型語言模型(LLMs)能在資料庫中使用來建構生成式 AI 應用。 它允許資料庫呼叫 Microsoft Foundry 模型中的 Azure OpenAIAzure OpenAI 服務Azure 認知服務(Foundry 工具中的 Azure 語言) 以及 Azure 機器學習服務,透過與這些服務的無縫整合來簡化開發。

啟用 azure_ai 延伸模組

在你能使用 azure_ai 擴充功能之前,你需要先將它列入允許名單,然後安裝到你的適用於 PostgreSQL 的 Azure 資料庫實例中。 請依照以下步驟開始。

將擴充功能加入允許清單

請將 azure_ai 加入你的 適用於 PostgreSQL 的 Azure 資料庫 彈性伺服器的允許名單,詳見 如何使用 PostgreSQL 擴充功能。 透過執行 SHOW azure.extensions; 查詢來確認是否正確新增。

安裝擴充套件

連接到你的資料庫並執行以下 CREATE EXTENSION 指令:

CREATE EXTENSION IF NOT EXISTS azure_ai;

在每個需要該擴充功能的資料庫中重複此指令。

安裝 azure_ai後,會產生以下結構結構:

  • azure_ai: 配置及相關功能的主結構
  • azure_openai:Foundry 模型中的 Azure OpenAI 功能與類型,及 Microsoft Foundry 中的 OpenAI 模型
  • azure_cognitive:Azure 認知服務的功能與類型(Foundry Tools 中的 Azure Language)
  • azure_ml:Azure Machine Learning 服務的功能與類型

小提示

您也可能想要啟用在適用於 PostgreSQL 的 Azure 資料庫中啟用並使用 pgvector,因為它通常和azure_ai搭配使用。

備註

要從目前資料庫中移除該副檔名,請執行 DROP EXTENSION azure_ai;

設定 azure_ai 延伸模組

要設定擴充功能,請提供你想使用的 Azure AI + 機器學習服務的端點與認證細節(API 金鑰或管理身份)。 使用 azure_ai.set_setting 配置函式搭配各種配置鍵來儲存這些數值。

權限

該擴充定義了一個角色 azure_ai_settings_manager ,允許透過 azure_ai.set_gettingazure_ai.set_setting 函式來讀取與寫入設定設定。 只有超級使用者及此角色的成員能呼叫這些函式。 在適用於 PostgreSQL 的 Azure 資料庫 Flexible Server中,azure_pg_admin角色預設賦予此角色。

組態功能

azure_ai_set_setting

設定 AI + 機器學習的配置。

使用方式:

-- Syntax
azure_ai.set_setting(key TEXT, value TEXT)

-- Usage example: Set the Endpoint and an API Key for Azure OpenAI
select azure_ai.set_setting('azure_openai.endpoint','https://<endpoint>.openai.azure.com');
select azure_ai.set_setting('azure_openai.subscription_key', '<API Key>');

支援的配置鍵與值列表:

key value
azure_openai.endpoint 支援 OpenAI 端點(例如, https://example.openai.azure.com)。
azure_openai.auth_type subscription-keymanaged-identity
azure_openai.subscription_key 一個 OpenAI 資源的訂閱金鑰。
azure_cognitive.endpoint 支援的認知服務端點(例如, https://example.cognitiveservices.azure.com
azure_cognitive.auth_type subscription-keymanaged-identity
azure_cognitive.subscription_key 認知服務資源的訂用帳戶金鑰。
azure_ml.scoring_endpoint 支援的機器學習線上終結點 URI。
azure_ml.auth_type subscription-keymanaged-identity
azure_ml.endpoint_key 供 Azure 機器學習端點使用的端點金鑰。

azure_ai.get_setting

取得指定 key 的目前設定值(參見上方支援的鍵值列表)。 回傳 TEXT 代表所選設定的當前值。

使用方式:

-- Syntax
azure_ai.get_setting(key TEXT)

-- Usage example: Get the Endpoint and API Key for Azure OpenAI
select azure_ai.get_setting('azure_openai.endpoint');
select azure_ai.get_setting('azure_openai.subscription_key');

azure_ai.version

回傳 TEXT 代表目前擴充功能 azure_ai 版本。

使用方式:

SELECT azure_ai.version()

啟用管理身份驗證

適用於 PostgreSQL 的 Azure 資料庫 的 azure_ai 擴充功能支援系統指派管理身份(SAMI),提供更強的安全效益。 透過使用 Microsoft Entra ID,您可以無需存取金鑰進行驗證,降低未經授權存取的風險並簡化憑證管理。

要啟用受管身份驗證,請參閱 這份操作指南

azure_ai 擴充功能的特點

AI功能

azure_ai 擴充功能可透過以下 AI 函式(預覽版),在資料庫中呼叫託管於 Foundry 及 Azure OpenAI 的模型:

  • 在 適用於 PostgreSQL 的 Azure 資料庫:為給定輸入文字建立向量嵌入。
  • azure_ai.generate():使用大型語言模型(LLM)產生文字或結構化輸出。
  • azure_ai.is_true():評估指定陳述式為 true 的可能性。
  • azure_ai.extract():從文字中擷取結構化功能或實體。
  • azure_ai.rank():根據與指定查詢的相關性,重新排名文件清單。

其他功能

擴充功能也支援叫用

升級 AI + 機器學習擴充功能

要檢查已安裝版本及可用升級,請執行:

SELECT * FROM pg_available_extensions
WHERE name = 'azure_ai'

要將擴充功能更新到最新支援版本,請執行:

ALTER EXTENSION azure_ai UPDATE;