媒体搜索使用入门
您可以快速构建领先的媒体搜索应用。媒体搜索可让您的受众群体通过 Google 品质的搜索结果发现各种内容。
如需了解有关 Vertex AI Search for Media 的一般信息,请参阅媒体搜索和推荐简介。在本入门教程中,您将使用 Movielens 数据集来演示如何将媒体内容目录上传到 Vertex AI Search。Movielens 数据集包含一个影片目录(文档)。
上传影片数据后,您将创建一个搜索应用,并通过预览页面对其进行测试。
如果您已完成媒体推荐使用入门教程,但仍有数据存储区(建议名称为 quickstart-media-data-store),则可以使用该数据存储区,无需另外创建一个。在这种情况下,您应该从创建用于媒体搜索的应用这一步骤开始学习本教程。
预计完成本教程所需的时间:约 1 小时。
目标
- 了解如何导入媒体文档以创建媒体数据存储区。
- 创建、配置和测试搜索应用。
在学习本教程之前,确保您已完成准备工作中的步骤。
如需在 Google Cloud 控制台中直接遵循有关此任务的分步指导,请点击操作演示:
准备工作
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
- 
    
    
      In the Google Cloud console, on the project selector page, select or create a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Vertex AI Search (Discovery Engine), Cloud Storage, BigQuery APIs. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
    
        Make sure that you have the following role or roles on the project: Discovery Engine Admin Check for the roles- 
              In the Google Cloud console, go to the IAM page. Go to IAM
- Select the project.
- 
              In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator. 
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
 Grant the roles- 
              In the Google Cloud console, go to the IAM page. 前往 IAM
- 选择项目。
- 点击 授予访问权限。
- 
              在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。 
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
 
- 
              
- 
    
    
      In the Google Cloud console, on the project selector page, select or create a Google Cloud project. Roles required to select or create a project - Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
- 
      Create a project: To create a project, you need the Project Creator
      (roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
 
- 
  
    Verify that billing is enabled for your Google Cloud project. 
- 
  
  
    
      Enable the Vertex AI Search (Discovery Engine), Cloud Storage, BigQuery APIs. Roles required to enable APIs To enable APIs, you need the Service Usage Admin IAM role ( roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
- 
    
        Make sure that you have the following role or roles on the project: Discovery Engine Admin Check for the roles- 
              In the Google Cloud console, go to the IAM page. Go to IAM
- Select the project.
- 
              In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator. 
- For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.
 Grant the roles- 
              In the Google Cloud console, go to the IAM page. 前往 IAM
- 选择项目。
- 点击 授予访问权限。
- 
              在新的主账号字段中,输入您的用户标识符。 这通常是 Google 账号的电子邮件地址。 
- 在选择角色列表中,选择一个角色。
- 如需授予其他角色,请点击 添加其他角色,然后添加其他各个角色。
- 点击 Save(保存)。
 
- 
              
- 打开Google Cloud 控制台。
- 选择您的 Google Cloud 项目。
- 记下信息中心页面上的项目信息卡片中的项目 ID。您需要项目 ID 才能执行以下程序。
- 点击控制台顶部的激活 Cloud Shell 按钮。一个 Cloud Shell 会话随即会在Google Cloud 控制台底部的新框架内打开,并显示命令行提示符。 如需了解启动 Cloud Shell 的其他方法,请参阅启动 Cloud Shell。   
- 使用您的项目 ID 运行以下命令,为命令行设置默认项目。 - gcloud config set project PROJECT_ID
- 创建 BigQuery 数据集: - bq mk movielens
- 将 - movies.csv加载到新的- moviesBigQuery 表中:- bq load --skip_leading_rows=1 movielens.movies \ gs://cloud-samples-data/gen-app-builder/media-recommendations/movies.csv \ movieId:integer,title,genres
- 将 - ratings.csv加载到新的- ratingsBigQuery 表中:- bq load --skip_leading_rows=1 movielens.ratings \ gs://cloud-samples-data/gen-app-builder/media-recommendations/ratings.csv \ userId:integer,movieId:integer,rating:float,time:timestamp
- 创建一个将影片表转换为 - Document架构的视图:- bq mk --project_id=PROJECT_ID \ --use_legacy_sql=false \ --view ' WITH t AS ( SELECT CAST(movieId AS string) AS id, SUBSTR(title, 0, 128) AS title, SPLIT(genres, "|") AS categories FROM `PROJECT_ID.movielens.movies`) SELECT id, "default_schema" as schemaId, null as parentDocumentId, TO_JSON_STRING(STRUCT(title as title, categories as categories, CONCAT("http://mytestdomain.movie/content/", id) as uri, "2023-01-01T00:00:00Z" as available_time, "2033-01-01T00:00:00Z" as expire_time, "movie" as media_type)) as jsonData FROM t;' \ movielens.movies_view- 现在,新视图具有 Discovery Engine API 所需的架构。 
- 前往 Google Cloud 控制台的 BigQuery 页面。 
- 在探索器窗格中,展开项目名称,点击数据集,选择 - movielens数据集,然后点击- movies_view以打开此视图的架构页面。- 或者,在探索器窗格中,选择经典探索器标签页,展开项目名称,展开 - movielens数据集,然后点击- movies_view以打开此视图的架构页面。  
- 前往表探索器标签页。   
- 在生成的查询窗格中,点击复制到查询按钮。系统随即会打开查询编辑器。 
- 点击运行以在您创建的视图中查看电影数据。 
- 运行以下 Cloud Shell 命令,以根据影片评分创建虚构的用户事件: - bq mk --project_id=PROJECT_ID \ --use_legacy_sql=false \ --view ' WITH t AS ( SELECT MIN(UNIX_SECONDS(time)) AS old_start, MAX(UNIX_SECONDS(time)) AS old_end, UNIX_SECONDS(TIMESTAMP_SUB( CURRENT_TIMESTAMP(), INTERVAL 90 DAY)) AS new_start, UNIX_SECONDS(CURRENT_TIMESTAMP()) AS new_end FROM `PROJECT_ID.movielens.ratings`) SELECT CAST(userId AS STRING) AS userPseudoId, "view-item" AS eventType, FORMAT_TIMESTAMP("%Y-%m-%dT%X%Ez", TIMESTAMP_SECONDS(CAST( (t.new_start + (UNIX_SECONDS(time) - t.old_start) * (t.new_end - t.new_start) / (t.old_end - t.old_start)) AS int64))) AS eventTime, [STRUCT(movieId AS id, null AS name)] AS documents, FROM `PROJECT_ID.movielens.ratings`, t WHERE rating >= 4;' \ movielens.user_events
- 在 Google Cloud 控制台中,前往 AI Applications 页面。 
- 可选:点击允许 Google 有选择性地对模型输入和回答进行采样。 
- 点击继续并激活 API。 
- 在 Google Cloud 控制台中,前往 AI Applications 页面。 
- 点击 - 创建应用 。
- 在创建应用页面的 Media catalog search(媒体目录搜索)下方,点击创建。 
- 在您的应用名称字段中,输入应用的名称,例如 - quickstart-media-search。您的应用 ID 会显示在引擎名称下方。
- 点击继续。 
- 如果您已完成媒体推荐使用入门教程,并且仍有数据存储区(建议名称为 - quickstart-media-data-store),请选择该存储区,点击创建,然后跳至预览搜索。
- 如果您没有包含 Movielens 数据集的数据存储区,请创建一个新的数据存储区并将其选中: - 在数据存储区页面上,点击创建数据存储区。 
- 为数据存储区输入一个显示名称,例如 - quickstart-media-data-store,然后点击创建。
- 选择您刚刚创建的数据存储区,然后点击创建以创建您的应用。系统会将您重定向至选择数据源页面。 
 
- 如果系统未自动将您重定向到选择数据源页面,请执行以下操作: - 打开文档标签页。
- 点击导入数据。
 
- 在选择数据源页面上,选择 BigQuery。 
- 输入您创建的 - moviesBigQuery 视图的名称,然后点击导入。- PROJECT_ID.movielens.movies_view
- 等待所有文档导入完毕,此过程大约需要 15 分钟。完成后,应该有 86537 个文档。 - 您可以查看活动标签页,了解导入操作状态。导入完成后,导入操作状态会更改为已完成。 
- 打开事件标签页。 
- 点击导入活动。 
- 选择 BigQuery。 
- 输入您创建的 - user_eventsBigQuery 视图的名称,然后点击导入。- PROJECT_ID.movielens.user_events
- 在导入事件之前,您可以继续执行下一步操作,但搜索结果尚未包含完整的数据集。 - 您可以查看活动标签页,了解操作状态。由于您正在导入数百万行,因此这个过程大约需要一个小时才能完成。 
- 在导航菜单中,点击 - 配置 。
- 在在此处搜索框中,输入电影名称,例如《指环王》。 
- 请注意,搜索结果与输入的影片名称相关。 
- 在此页面上,您可以自定义搜索微件显示搜索结果信息的方式。如需了解详情,请参阅配置搜索微件的结果。 - 对于媒体搜索应用,您可以: - 完成更改后,点击保存并发布以更新微件。 
- 在导航菜单中,点击集成。 
- 确保微件标签页处于选中状态。 
- 选择基于 JWT 或 OAuth 作为 widget 授权类型。 
- 在网域字段中,输入将放置该微件的网页的域名。例如,如果您要将该微件复制到网页 - example.com/ai.html,请输入- example.com作为网域。
- 点击添加,然后单击保存。 
- 复制将以下代码复制到您的 Web 应用部分中提供的代码段。 
- 在您的代码库中,生成授权令牌。 
- 如需将授权令牌传递给微件,请使用将以下代码复制到您的 Web 应用部分中提供的“设置授权令牌”代码段,并将文本 - <JWT or OAuth token provided by you backend>替换为授权令牌。
- 如需有关将搜索应用集成到 Web 应用的帮助,请参阅获取搜索结果中的代码示例。 
- 若不再需要该项目,请使用Google Cloud console 将其删除,以避免产生不必要的 Google Cloud 费用。
- 如果您为了解 Vertex AI Search 创建了一个新项目,但现在不再需要该项目,请删除项目。
- 如果您使用的是现有 Google Cloud 项目,请删除您创建的资源,以避免您的账号产生费用。如需了解详情,请参阅删除应用、完全清除数据存储区中的数据和删除数据存储区。
- 按照关闭 Vertex AI Search 中的步骤操作。
- 如果您创建了 BigQuery 数据集,请在 Cloud Shell 中将其删除: - bq rm --recursive --dataset movielens
准备数据集
注意:如果您已完成媒体推荐使用入门教程并且仍然有数据存储区(建议名称为quickstart-media-data-store),请跳到创建用于媒体搜索的应用这一步。
您可以使用 Cloud Shell 导入 Movielens 数据集并为媒体重构 Vertex AI Search 的数据集。
打开 Cloud Shell
导入数据集
为了便于导入,Movielens 数据集位于公共 Cloud Storage 存储桶中。
创建 BigQuery 视图
在此步骤中,您将重构 Movielens 数据集,使其遵循媒体数据存储区的预期格式。
在本指南中,您将根据正面评分 (>= 4) 创建过去 90 天内的虚假 view-item 用户事件。
激活 Vertex AI Search
创建用于媒体搜索的应用
本部分中的步骤将指导您创建和部署媒体搜索应用。
导入数据
接下来,导入之前设置了格式的影片和用户事件数据。
导入文档
导入用户事件
预览并配置搜索
部署搜索微件
清理
为避免因本页中使用的资源导致您的 Google Cloud 账号产生费用,请按照以下步骤操作。
您可以重复使用在媒体推荐使用入门教程中为媒体推荐创建的数据存储区。在执行此清理过程之前,请先尝试参考该教程。