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

教程:使用 Azure Database for PostgreSQL 和 Azure OpenAI 创建建议系统

适用于: Azure Database for PostgreSQL 灵活服务器

本动手教程演示如何使用 Azure Database for PostgreSQL 和 Azure OpenAI 生成推荐器应用程序。 推荐系统在不同领域中有应用。 服务提供商倾向于根据先前从客户和环境收集的历史记录和上下文信息提供产品和服务的建议。

可通过多种方式对建议系统进行建模。 本教程探讨了最简单的形式:基于一个产品推荐,这个产品与之前的购买(例如)相对应。 本教程使用了用于语义搜索的教程中使用的食谱数据集。 对食谱的建议基于客户喜欢或之前搜索过的食谱。

Prerequisites

  1. 创建 OpenAI 帐户并请求访问 Azure OpenAI
  2. 在所需订阅中授予对 Azure OpenAI 的访问权限。
  3. 授予创建 Azure OpenAI 资源和部署模型的权限。
  4. 创建和部署 Azure OpenAI 资源和模型。 部署嵌入模型 文本嵌入-ada-002。 复制部署名称,因为需要使用它来创建嵌入。

启用 azure_ai 和 pgvector 扩展

在 Azure Database for PostgreSQL 灵活服务器上启用 azure_aipgvector 之前,需要先将它们加入允许列表。 确保通过运行 SHOW azure.extensions; 来正确添加它们。

然后,可以通过连接到目标数据库并运行 CREATE EXTENSION 命令来安装扩展。 对希望扩展可用的每个数据库单独重复该命令。

CREATE EXTENSION azure_ai;
CREATE EXTENSION vector;

配置 OpenAI 终结点和密钥

在 Azure AI 服务中的 “资源管理>密钥和终结点”下,可以找到 Azure AI 资源的终结点和密钥。 使用终结点和其中一个密钥来启用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>');

下载数据

Kaggle 下载数据。

创建表

连接到服务器并创建 test 数据库。 在该数据库中,使用以下命令创建要在其中导入数据的表:

CREATE TABLE public.recipes(
    rid integer NOT NULL,
    recipe_name text,
    prep_time text,
    cook_time text,
    total_time text,
    servings integer,
    yield text,
    ingredients text,
    directions text,
    rating real,
    url text,
    cuisine_path text,
    nutrition text,
    timing text,
    img_src text,
    PRIMARY KEY (rid)
);

导入数据

在客户端窗口中设置以下环境变量,将编码设置为 UTF-8。 此步骤是必需的,因为此特定数据集使用 Windows-1252 编码。

Rem on Windows
Set PGCLIENTENCODING=utf-8;
# on Unix based operating systems
export PGCLIENTENCODING=utf-8

将数据导入到创建的表中。 请注意,此数据集包含标题行。

psql -d <database> -h <host> -U <user> -c "\copy recipes FROM <local recipe data file> DELIMITER ',' CSV HEADER"

添加一个列来存储嵌入

向表添加嵌入列:

ALTER TABLE recipes ADD COLUMN embedding vector(1536);

生成嵌入内容

使用 azure_ai 扩展为数据生成嵌入。 以下示例向量化几个字段并连接。

WITH ro AS (
    SELECT ro.rid
    FROM
        recipes ro
    WHERE
        ro.embedding is null
        LIMIT 500
)
UPDATE
    recipes r
SET
    embedding = azure_openai.create_embeddings('text-embedding-ada-002', r.recipe_name||' '||r.cuisine_path||' '||r.ingredients||' '||r.nutrition||' '||r.directions)
FROM
    ro
WHERE
    r.rid = ro.rid;

重复该命令,直到没有更多行要处理。

提示

尝试调整LIMIT值。 如果使用的值较高,该语句可能因 Azure OpenAI 施加的限制而中途失败。 如果语句失败,请等待至少一分钟,然后再次运行该命令。

为方便起见,请在数据库中创建搜索函数:

create function
    recommend_recipe(sampleRecipeId int, numResults int)
returns table(
            out_recipeName text,
            out_nutrition text,
            out_similarityScore real)
as $$
declare
    queryEmbedding vector(1536);
    sampleRecipeText text;
begin
    sampleRecipeText := (select
                            recipe_name||' '||cuisine_path||' '||ingredients||' '||nutrition||' '||directions
                        from
                            recipes where rid = sampleRecipeId);

    queryEmbedding := (azure_openai.create_embeddings('text-embedding-ada-002',sampleRecipeText));

    return query
    select
        distinct r.recipe_name,
        r.nutrition,
        (r.embedding <=> queryEmbedding)::real as score
    from
        recipes r
    order by score asc limit numResults; -- cosine distance
end $$
language plpgsql;

现在只需调用函数来搜索建议:

select out_recipename, out_similarityscore from recommend_recipe(1, 20); -- search for 20 recipe recommendations that closest to recipeId 1

浏览结果:

            out_recipename             | out_similarityscore
---------------------------------------+---------------------
 Apple Pie by Grandma Ople             |                   0
 Easy Apple Pie                        |          0.05137232
 Grandma's Iron Skillet Apple Pie      |         0.054287136
 Old Fashioned Apple Pie               |         0.058492836
 Apple Hand Pies                       |          0.06449003
 Apple Crumb Pie                       |          0.07290977
 Old-Fashioned Apple Dumplings         |         0.078374185
 Fried Apple Pies                      |          0.07918481
 Apple Pie Filling                     |         0.084320426
 Apple Turnovers                       |          0.08576391
 Dutch Apple Pie with Oatmeal Streusel |          0.08779895
 Apple Crisp - Perfect and Easy        |          0.09170883
 Delicious Cinnamon Baked Apples       |          0.09384012
 Easy Apple Crisp with Pie Filling     |          0.09477234
 Jump Rope Pie                         |          0.09503954
 Easy Apple Strudel                    |         0.095167875
 Apricot Pie                           |          0.09634114
 Easy Apple Crisp with Oat Topping     |          0.09708358
 Baked Apples                          |          0.09826993
 Pear Pie                              |         0.099974394
(20 rows)