你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

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 生成文本或结构化输出。

它支持以下输入参数:

论点 类型 DESCRIPTION
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 结果不确定的可能性。

它支持以下输入参数:

论点 类型 DESCRIPTION
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()

此运算符基于用户定义的标签从文本中提取结构化特征或实体。

它支持以下输入参数:

论点 类型 DESCRIPTION
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 模型。

它支持以下输入参数:

论点 类型 DESCRIPTION
query text 用于计算和排名每个文档相关性的搜索字符串。
document_contents array[text] 要重新调整的文档数组。
document_ids(可选) array 对应于输入文档的文档标识符数组。
model(可选) text DEFAULT "cohere-rerank-v3.5" Azure AI Foundry 中模型部署的名称。 支持跨编码器和基于 GPT 的模型。

运算符返回包含文档 ID、其排名和关联相关性分数的一个 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. azure_ai Azure Database for PostgreSQL 灵活服务器上启用扩展。

  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>');
    

    如果希望改用托管标识,请参阅 本文 以执行以下步骤:

    • 为 Azure Database for PostgreSQL 灵活服务器启用系统分配的托管标识并重启服务器。
    • 将“认知服务 OpenAI 用户”角色分配给托管标识以与 Azure OpenAI 资源交互。
    • 设置为 azure_openai.auth_type “managed-identity”。
    • 使用 azure_openai.endpoint 终结点 URL 设置。
  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. azure_ai Azure Database for PostgreSQL 灵活服务器上启用扩展。

  2. 导航到 Azure AI Foundry 并使用无服务器 API 购买选项 部署 Cohere-rerank-v3.5 模型

  3. 请注意模型的终结点密钥和 Reranker 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>');
    

    如果希望改用托管标识,请参阅 本文 以执行以下步骤:

    • 为 Azure Database for PostgreSQL 灵活服务器启用系统分配的托管标识并重启服务器。
    • 将“Azure 机器学习数据科学家”角色分配给托管标识以与 Cohere 模型交互。
    • 设置为 azure_ml.auth_type “managed-identity”。
    • 使用 azure_ml.serverless_ranking_endpoint Cohere 重新ranker API 设置。
  5. 现在,你已全部设置为使用 Cohere reranker 模型调用 .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;