Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2019 Microsoft
Copyright (c) 2021 Microsoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
169 changes: 43 additions & 126 deletions sdk/cognitiveservices/cognitiveservices-personalizer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,158 +15,75 @@ npm install @azure/cognitiveservices-personalizer

### How to use

#### nodejs - Authentication, client creation and reward events as an example written in TypeScript.
#### nodejs - client creation and get serviceConfiguration 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/azure/cognitive-services/personalizer/)

```javascript
While the below sample uses the interactive login, other authentication options can be found in the [README.md file of @azure/ms-rest-nodeauth](https://www.npmjs.com/package/@azure/ms-rest-nodeauth) package
```typescript
const msRestNodeAuth = require("@azure/ms-rest-nodeauth");
const { PersonalizerClient } = require("@azure/cognitiveservices-personalizer");
const { CognitiveServicesCredentials } = require("@azure/ms-rest-azure-js");

async function main() {
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 = {
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: ");
console.log(result);
})
.catch(err => {
console.log("An error occurred:");
console.error(err);
});
}

main();
const subscriptionId = process.env["AZURE_SUBSCRIPTION_ID"];

msRestNodeAuth.interactiveLogin().then((creds) => {
const client = new PersonalizerClient(creds, subscriptionId);
client.serviceConfiguration.get().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.
#### browser - Authentication, client creation and get serviceConfiguration 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);
client.serviceConfiguration.get().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 All @@ -177,4 +94,4 @@ main();

- [Microsoft Azure SDK for Javascript](https://github.com/Azure/azure-sdk-for-js)

![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2Fcognitiveservices%2Fcognitiveservices-personalizer%2FREADME.png)
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js/sdk/cognitiveservices/cognitiveservices-personalizer/README.png)
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const config = {
"@azure/ms-rest-azure-js": "msRestAzure"
},
banner: `/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
*/

export {
ErrorResponse,
Evaluation,
EvaluationContract,
EvaluationsCreateHeaders,
InternalError,
PersonalizerError,
PolicyContract,
PolicyResult,
PolicyResultSummary,
PolicyResultTotalSummary
} from "../models/mappers";
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT License.
*
* Code generated by Microsoft (R) AutoRest Code Generator.
* Changes may cause incorrect behavior and will be lost if the code is regenerated.
Expand Down
Loading