共用方式為


Azure AI 延伸模組中的語意運算子 (預覽)

Azure AI 延伸模組引進語意運算子,這是一項突破性功能,可將進階的生成式 AI (GenAI) 功能直接整合到 PostgreSQL SQL 中。 這些運算子由聊天完成和其他 Azure AI 部署等模型提供支援,可讓開發人員直接在其資料庫內建置 GenAI 驅動的應用程式。 這會解除鎖定理解文字、推理和產生結構化輸出的新功能。

主要功能

語意運算子為使用者提供四個使用產生 AI 功能的核心 SQL 函式:

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

每個函式都是透過使用 函 azure_ai.set_setting 式註冊的 AI Foundry 端點運作,以確保順暢的整合和使用者控制。

瞭解語意運算符

在 Azure AI 擴充功能中,語義運算子被設計用來直接在你的 PostgreSQL 資料庫裡簡化複雜的 AI 驅動工作。 這些運算符可讓用戶順暢地將產生 AI 功能整合到其 SQL 工作流程中,以啟用進階文字產生、真實評估、實體擷取和檔排名。 每個操作員都經過優化,以方便使用和具彈性,讓開發人員以最少的努力建置智慧應用程式。

azure_ai.generate()

此運算子會使用 LLM 來產生文字或結構化輸出。

它支援下列輸入參數:

論點 類型 說明
prompt text 要傳送至 LLM 的使用者提示。
json_schema (選用) JsonB DEFAULT '' 您想要 LLM 回應遵守之結構化輸出的 JSON 結構描述。 必須遵循結構化輸出的 Open AI 標記法
model (選用) text DEFAULT "gpt-4.1" Azure AI Foundry 中模型部署的名稱。
system_prompt (選用) text DEFAULT "You are a helpful assistant." 要傳送至 LLM 的系統提示。

根據預設,運算子會傳回包含所產生回應的 text 值。 如果提供 json_schema 引數,輸出會作為符合指定結構描述的結構化 JsonB 物件傳回。

使用方式範例:

SELECT azure_ai.generate(
  'Rewrite the following comment to be more polite: ' comment_text
) AS polite_comment
FROM user_comments;

SELECT review, azure_ai.generate(
    prompt        => 'Rewrite the following comment to be more polite and return the number of products mentioned:' || review,
    json_schema   => '{ 
                        "name": "generate_response", 
                        "description": "Generate a response to the user",
                        "strict": true, 
                        "schema": { 
                          "type": "object", 
                          "properties": { 
                            "comment": { "type": "string" },
                            "num_products": { "type": "integer" } 
                          }, 
                          "required": ["comment", "num_products"],
                          "additionalProperties": false 
                          } 
                        }',
     model  => 'gpt-4.1-mini'
) as polite_comment_with_count 
FROM 
    Reviews;

azure_ai.is_true()

這個運算子會評估指定陳述式為 true、傳回 boolean 值,或結果不明時傳回 NULL

它支援下列輸入參數:

論點 類型 說明
statement text 要評估為 true 或 false 的陳述式。
model (選用) text DEFAULT "gpt-4.1" Azure AI Foundry 中模型部署的名稱。

使用方式範例:

SELECT azure_ai.is_true(
  'The review talks about the product: '
  product_name
  ' Review: '
  review_text
) AS is_relevant_review
FROM product_reviews;

azure_ai.extract()

此運算子會根據使用者定義的標籤,從文字擷取結構化功能或實體。

它支援下列輸入參數:

論點 類型 說明
document text 包含實體和功能的文件。
data array[text] 標籤或功能名稱的陣列,其中每個項目都代表要從輸入文字擷取的不同實體類型。
model (選用) text DEFAULT "gpt-4.1" Azure AI Foundry 中模型部署的名稱。

運算子會傳回 JsonB 物件,其中包含對應至其對應標籤的已擷取實體。

使用方式範例

SELECT azure_ai.extract(
   'The headphones are not great. They have a good design, but the sound quality is poor and the battery life is short.',
   ARRAY[ 'product', 'sentiment']
);

-- Output: {"product": "headphones", "sentiment": "negative"}

SELECT azure_ai.extract(
    'The music quality is good, though the call quality could have been better. The design is sleek, but still slightly heavy for convenient travel.',
    ARRAY[
        'design: string - comma separated list of design features of the product',
        'sound: string - sound quality (e.g., music, call, noise cancellation) of the product',
        'sentiment: number - sentiment score of the review; 1 (lowest) to 5 (highest)'
    ]
);

-- Output: {"sound": "music quality is good, call quality could have been better", "design": "sleek, slightly heavy", "sentiment": 3}

azure_ai.rank()

此運算子會根據其與指定查詢的相關性來重新排名文件。 它支援跨編碼器和 GPT 模型。

它支援下列輸入參數:

論點 類型 說明
query text 用來評估和排名每個文件的相關性的搜尋字串。
document_contents array[text] 要重新建立的文件陣列。
document_ids (選用) array 對應至輸入文件的文件識別碼陣列。
model (選用) text DEFAULT "cohere-rerank-v3.5" Azure AI Foundry 中模型部署的名稱。 支援跨編碼器和 GPT 型模型。

運算子會傳回 table,其中包含文件識別碼、其排名,以及相關聯的相關性分數。

使用方式範例:

SELECT azure_ai.rank(
    'Best headphones for travel',
    ARRAY[
        'The headphones are lightweight and foldable, making them easy to carry.',
        'Bad battery life, not so great for long trips.',
        'The sound quality is excellent, with good noise isolation.'
    ]
)

SELECT azure_ai.rank(
  query => 'Clear calling capability that blocks out background noise',
  document_contents => ARRAY[
                        'The product has a great battery life, good design, and decent sound quality.',
                        'These headphones are perfect for long calls and music.',
                        'Best headphones for music lovers. Call quality could have been better.',
                        'The product has a good design, but it is a bit heavy. Not recommended for travel.'
                      ],
  document_ids => ARRAY['Review1', 'Review2', 'Review3', 'Review4'],
  model => 'gpt-4.1'
) AS ranked_reviews;

如何開始使用

若要在 PostgreSQL 資料庫中使用語意運算子,請遵循下列步驟:

設定 .generate().extract().is_true() 運算子

這些運算子支援聊天完成模型,預設為 gpt-4.1

  1. 在您的適用於 PostgreSQL 的 Azure 資料庫彈性伺服器實例上啟用azure_ai延伸模組

  2. 建立 Azure OpenAI 服務資源,並部署聊天完成模型 (例如 gpt-4.1)。 或者,您可以透過 Azure AI Foundry 所提供的直覺式體驗來部署和管理模型。

  3. 記下 Azure OpenAI 端點 URLAPI 金鑰

  4. 設定存取權

    若要讓 azure_ai 延伸模組使用訂用帳戶金鑰驗證叫用此模型,請執行下列 SQL 命令:

    SELECT azure_ai.set_setting('azure_openai.endpoint', 'https://<endpoint>.openai.azure.com/'); 
    SELECT azure_ai.set_setting('azure_openai.subscription_key', '<API Key>');
    

    如果您想要改用受控識別,請參閱這篇文章來執行下列步驟:

    • 針對適用於 PostgreSQL 的 Azure 資料庫彈性伺服器執行個體啟用系統指派的受控識別,然後重新啟動伺服器。
    • 將「認知服務 OpenAI 使用者」角色指派給受控識別,以與 Azure OpenAI 資源互動。
    • azure_openai.auth_type 設定為「managed-identity」。
    • 使用端點 URL 設定 azure_openai.endpoint
  5. 您現在全都設定為叫用 .generate().is_true().extract() 運算子。

    範例使用方式與 gpt-4.1 (預設值):

    SELECT name, azure_ai.generate(
      'Generate a description for the product: ' || name
    ) AS description
    FROM products;
    

    其他模型的範例使用方式:

    SELECT name, azure_ai.generate(
      'Generate a description for the product: ' || name , 'gpt-4.1-mini'
    ) AS description
    FROM products;
    

.rank() 運算子的設定

.rank() 運算子同時支援跨編碼器和聊天完成模型,預設為跨編碼器 Cohere-rerank-v3.5

使用 Cohere-rerank-v3.5 跨編碼器:

  1. 在適用於 PostgreSQL 的 Azure 資料庫執行個體上啟用azure_ai延伸模組

  2. 瀏覽至 Azure AI Foundry,並使用無伺服器 API 購買選項來部署 Cohere-rerank-v3.5 模型

  3. 請注意模型的端點金鑰和重新排名器 API 路由,其看起來應該像這樣:https://<deployment name>.<region>.models.ai.azure.com/<v1 or v2>/rerank

  4. 設定存取權

    若要讓 azure_ai 延伸模組使用訂用帳戶金鑰驗證叫用此模型,請執行下列 SQL 命令:

    SELECT azure_ai.set_setting('azure_ml.serverless_ranking_endpoint', '<Cohere reranker API>');
    SELECT azure_ai.set_setting('azure_ml.serverless_ranking_endpoint_key', '<API Key>');
    

    如果您想要改用受控識別,請參閱這篇文章來執行下列步驟:

    • 針對適用於 PostgreSQL 的 Azure 資料庫彈性伺服器執行個體啟用系統指派的受控識別,然後重新啟動伺服器。
    • 將「Azure Machine Learning 資料科學家」角色指派給受控識別,以與 Cohere 模型互動。
    • azure_ml.auth_type 設定為「managed-identity」。
    • 使用 Cohere 重新排名器 API 來設定 azure_ml.serverless_ranking_endpoint
  5. 您現在全都已設定為使用 Cohere 重新排名器模型來叫用 .rank() 運算子。

    SELECT azure_ai.rank(
      'Best headphones for travel',
      ARRAY[
          'The headphones are lightweight and foldable, making them easy to carry.',
          'Bad battery life, not so great for long trips.',
          'The sound quality is excellent, with good noise isolation.'
      ]
    ) AS ranked_reviews;
    

若要搭配 .rank() 之類的聊天完成模型使用 gpt-4.1 運算子,請在 Azure OpenAI 上部署所需的模型、使用模型的端點詳細資料設定 azure_ai 延伸模組,並在叫用運算子時指定模型名稱。

SELECT azure_ai.set_setting('azure_openai.endpoint', 'https://<endpoint>.openai.azure.com/'); 
SELECT azure_ai.set_setting('azure_openai.subscription_key', '<API Key>');

SELECT azure_ai.rank(
 'Best headphones for travel',
  ARRAY[
      'The headphones are lightweight and foldable, making them easy to carry.',
      'Bad battery life, not so great for long trips.',
      'The sound quality is excellent, with good noise isolation.'
  ],
  'gpt-4.1'
) AS ranked_reviews;