使用 SDK for JavaScript (v3) 的 Amazon Personalize Runtime 範例 - AWS SDK for JavaScript

AWS SDK for JavaScript V3 API參考指南會詳細說明第 3 版 (V3) 的所有API操作 AWS SDK for JavaScript 。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 SDK for JavaScript (v3) 的 Amazon Personalize Runtime 範例

下列程式碼範例示範如何使用 AWS SDK for JavaScript (v3) 搭配 Amazon Personalize Runtime 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 GetPersonalizedRanking

SDK for JavaScript (v3)
注意

還有更多功能 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { GetPersonalizedRankingCommand } from "@aws-sdk/client-personalize-runtime"; import { personalizeRuntimeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeRuntimeClient = new PersonalizeRuntimeClient({ region: "REGION"}); // Set the ranking request parameters. export const getPersonalizedRankingParam = { campaignArn: "CAMPAIGN_ARN" /* required */, userId: "USER_ID" /* required */, inputList: ["ITEM_ID_1", "ITEM_ID_2", "ITEM_ID_3", "ITEM_ID_4"], }; export const run = async () => { try { const response = await personalizeRuntimeClient.send( new GetPersonalizedRankingCommand(getPersonalizedRankingParam), ); console.log("Success!", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

下列程式碼範例示範如何使用 GetRecommendations

SDK for JavaScript (v3)
注意

還有更多功能 GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

// Get service clients module and commands using ES6 syntax. import { GetRecommendationsCommand } from "@aws-sdk/client-personalize-runtime"; import { personalizeRuntimeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeRuntimeClient = new PersonalizeRuntimeClient({ region: "REGION"}); // Set the recommendation request parameters. export const getRecommendationsParam = { campaignArn: "CAMPAIGN_ARN" /* required */, userId: "USER_ID" /* required */, numResults: 15 /* optional */, }; export const run = async () => { try { const response = await personalizeRuntimeClient.send( new GetRecommendationsCommand(getRecommendationsParam), ); console.log("Success!", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

使用篩選條件取得建議 (自訂資料集群組)。

// Get service clients module and commands using ES6 syntax. import { GetRecommendationsCommand } from "@aws-sdk/client-personalize-runtime"; import { personalizeRuntimeClient } from "./libs/personalizeClients.js"; // Or, create the client here. // const personalizeRuntimeClient = new PersonalizeRuntimeClient({ region: "REGION"}); // Set the recommendation request parameters. export const getRecommendationsParam = { recommenderArn: "RECOMMENDER_ARN" /* required */, userId: "USER_ID" /* required */, numResults: 15 /* optional */, }; export const run = async () => { try { const response = await personalizeRuntimeClient.send( new GetRecommendationsCommand(getRecommendationsParam), ); console.log("Success!", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

從網域資料集群組中建立的推薦者取得篩選建議。

// Get service clients module and commands using ES6 syntax. import { GetRecommendationsCommand } from "@aws-sdk/client-personalize-runtime"; import { personalizeRuntimeClient } from "./libs/personalizeClients.js"; // Or, create the client here: // const personalizeRuntimeClient = new PersonalizeRuntimeClient({ region: "REGION"}); // Set recommendation request parameters. export const getRecommendationsParam = { campaignArn: "CAMPAIGN_ARN" /* required */, userId: "USER_ID" /* required */, numResults: 15 /* optional */, filterArn: "FILTER_ARN" /* required to filter recommendations */, filterValues: { PROPERTY: '"VALUE"' /* Only required if your filter has a placeholder parameter */, }, }; export const run = async () => { try { const response = await personalizeRuntimeClient.send( new GetRecommendationsCommand(getRecommendationsParam), ); console.log("Success!", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
  • 如需API詳細資訊,請參閱 AWS SDK for JavaScript API 參考GetRecommendations中的 。