Skip to content
This repository was archived by the owner on Aug 26, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 47 additions & 125 deletions sdk/cognitiveservices/cognitiveservices-personalizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,159 +17,81 @@ npm install @azure/cognitiveservices-personalizer

#### nodejs - Authentication, client creation and reward events as an example written in TypeScript.

##### Install @azure/ms-rest-azure-js
##### Install @azure/ms-rest-nodeauth

- Please install minimum version of `"@azure/ms-rest-nodeauth": "^3.0.0"`.
```bash
npm install @azure/ms-rest-azure-js
npm install @azure/ms-rest-nodeauth@"^3.0.0"
```

##### Sample code
The following sample ranks a personalized request object. To know more, refer to the [Azure Documentation on Personalizer](https://docs.microsoft.com/en-us/azure/cognitive-services/personalizer/)

```typescript
import {
PersonalizerClient,
PersonalizerModels
} from "@azure/cognitiveservices-personalizer";
import { CognitiveServicesCredentials } from "@azure/ms-rest-azure-js";

async function main(): Promise<void> {
const personalizerKey = process.env["personalizerKey"] || "<personalizerKey>";
const personalizerEndPoint =
process.env["personalizerEndPoint"] || "<personalizerEndPoint>";
const cognitiveServiceCredentials = new CognitiveServicesCredentials(
personalizerKey
);

const client = new PersonalizerClient(
cognitiveServiceCredentials,
personalizerEndPoint
);

const rankRequest: PersonalizerModels.RankRequest = {
contextFeatures: [
{
timeOfDay: "Morning"
}
],
actions: [
{
id: "NewsArticle",
features: [
{
type: "News"
}
]
},
{
id: "SportsArticle",
features: [
{
type: "Sports"
}
]
},
{
id: "EntertainmentArticle",
features: [
{
type: "Entertainment"
}
]
}
],
excludedActions: ["SportsArticle"],
eventId: "75269AD0-BFEE-4598-8196-C57383D38E10",
deferActivation: false
import * as msRest from "@azure/ms-rest-js";
import * as msRestNodeAuth from "@azure/ms-rest-nodeauth";
import { PersonalizerClient, PersonalizerModels, PersonalizerMappers } from "@azure/cognitiveservices-personalizer";
const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];

msRestNodeAuth.interactiveLogin().then((creds) => {
const client = new PersonalizerClient(creds, subscriptionId);
const eventId = "testeventId";
const rewardParameter: PersonalizerModels.RewardRequest = {
value: 1.01
};

client
.rank(rankRequest)
.then(result => {
console.log("The result is: ");
console.log(result);
})
.catch(err => {
console.log("An error occurred:");
console.error(err);
});
}

main();
client.events.reward(eventId, rewardParameter).then((result) => {
console.log("The result is:");
console.log(result);
});
}).catch((err) => {
console.error(err);
});
```

#### browser - Authentication, client creation and reward events as an example written in JavaScript.

##### Install @azure/ms-rest-browserauth

```bash
npm install @azure/ms-rest-browserauth
```

##### Sample code

See https://github.com/Azure/ms-rest-browserauth to learn how to authenticate to Azure in the browser.

- index.html
```html
<!DOCTYPE html>
<html lang="en">
<head>
<title>@azure/cognitiveservices-personalizer sample</title>
<script src="node_modules/@azure/ms-rest-js/dist/msRest.browser.js"></script>
<script src="node_modules/@azure/ms-rest-browserauth/dist/msAuth.js"></script>
<script src="node_modules/@azure/cognitiveservices-personalizer/dist/cognitiveservices-personalizer.js"></script>
<script type="text/javascript">
const personalizerKey = "<YOUR_PERSONALIZER_KEY>";
const personalizerEndPoint = "<YOUR_PERSONALIZER_ENDPOINT>";
const cognitiveServiceCredentials = new msRest.ApiKeyCredentials({
inHeader: {
"Ocp-Apim-Subscription-Key": personalizerKey
}
const subscriptionId = "<Subscription_Id>";
const authManager = new msAuth.AuthManager({
clientId: "<client id for your Azure AD app>",
tenant: "<optional tenant for your organization>"
});
const client = new Azure.CognitiveservicesPersonalizer.PersonalizerClient(
cognitiveServiceCredentials,
personalizerEndPoint
);

const rankRequest = {
contextFeatures: [
{
timeOfDay: "Morning"
}
],
actions: [
{
id: "NewsArticle",
features: [
{
type: "News"
}
]
},
{
id: "SportsArticle",
features: [
{
type: "Sports"
}
]
},
{
id: "EntertainmentArticle",
features: [
{
type: "Entertainment"
}
]
}
],
excludedActions: ["SportsArticle"],
eventId: "75269AD0-BFEE-4598-8196-C57383D38E10",
deferActivation: false
};

client
.rank(rankRequest)
.then(result => {
console.log("The result is: ");
authManager.finalizeLogin().then((res) => {
if (!res.isLoggedIn) {
// may cause redirects
authManager.login();
}
const client = new Azure.CognitiveservicesPersonalizer.PersonalizerClient(res.creds, subscriptionId);
const eventId = "testeventId";
const rewardParameter = {
value: 1.01
};
client.events.reward(eventId, rewardParameter).then((result) => {
console.log("The result is:");
console.log(result);
})
.catch(err => {
}).catch((err) => {
console.log("An error occurred:");
console.error(err);
});
});
</script>
</head>
<body></body>
Expand Down
3 changes: 2 additions & 1 deletion swagger_to_sdk_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"typescript": "",
"license-header": "MICROSOFT_MIT_NO_VERSION",
"sdkrel:typescript-sdks-folder": ".",
"use": "@microsoft.azure/autorest.typescript@4.1.1"
"typescript.clear-output-folder": "",
"use": "@microsoft.azure/autorest.typescript@4.2.2"
},
"advanced_options": {
"clone_dir": "./azure-sdk-for-js",
Expand Down