Skip to content

Commit ff2334d

Browse files
authored
[Text Analytics] Add a sample for model versions (Azure#19087)
* [Text Analytics] Add a sample for model versions * address feedback * edit
1 parent c3136d6 commit ff2334d

File tree

5 files changed

+233
-0
lines changed

5 files changed

+233
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This sample shows how to choose model versions to use with pre-built Text
6+
* Analytics models.
7+
*
8+
* @summary shows how to choose model versions for pre-built models.
9+
* @azsdk-weight 40
10+
*/
11+
12+
import { TextAnalyticsClient, AzureKeyCredential } from "@azure/ai-text-analytics";
13+
14+
// Load the .env file if it exists
15+
import * as dotenv from "dotenv";
16+
dotenv.config();
17+
18+
// You will need to set these environment variables or edit the following values
19+
const endpoint = process.env["ENDPOINT"] || "<cognitive services endpoint>";
20+
const apiKey = process.env["TEXT_ANALYTICS_API_KEY"] || "<api key>";
21+
22+
const documents = ["This document is written in English."];
23+
24+
export async function main() {
25+
console.log("== Choosing Model Version Sample ==");
26+
27+
const client = new TextAnalyticsClient(endpoint, new AzureKeyCredential(apiKey));
28+
29+
const recognizeEntitiesResults = await client.recognizeEntities(documents, "en", {
30+
/**
31+
* Specify the model version by setting this property. "latest" indicates
32+
* the latest generaly availability version of the model. When not specified,
33+
* latest will be assumed. Model versions are date based, e.g "2021-06-01".
34+
* See the documentation for a list of all model versions:
35+
* https://docs.microsoft.com/en-us/azure/cognitive-services/language-service/named-entity-recognition/how-to-call#specify-the-ner-model
36+
*/
37+
modelVersion: "latest"
38+
});
39+
40+
console.log(
41+
`The results of recognizeEntities has been computed using model version: ${recognizeEntitiesResults.modelVersion}`
42+
);
43+
44+
const poller = await client.beginAnalyzeActions(
45+
documents,
46+
{
47+
recognizeEntitiesActions: [
48+
{
49+
/**
50+
* Specify the model version by setting this property. "latest" indicates
51+
* the latest generaly availability version of the model. When not specified,
52+
* latest will be assumed. Model versions are date based, e.g "2021-06-01".
53+
* See the documentation for a list of all model versions:
54+
* https://docs.microsoft.com/en-us/azure/cognitive-services/language-service/named-entity-recognition/how-to-call#specify-the-ner-model
55+
*/
56+
modelVersion: "latest"
57+
}
58+
]
59+
},
60+
"en"
61+
);
62+
63+
const beginAnalyzeActionsResults = await poller.pollUntilDone();
64+
65+
for await (const page of beginAnalyzeActionsResults) {
66+
const entitiesAction = page.recognizeEntitiesResults[0];
67+
if (!entitiesAction.error) {
68+
console.log(
69+
`The results of recognizeEntities action has been computed using model version: ${entitiesAction.results.modelVersion}`
70+
);
71+
}
72+
}
73+
}
74+
75+
main().catch((err) => {
76+
console.error("The sample encountered an error:", err);
77+
});

sdk/textanalytics/ai-text-analytics/samples/v5/javascript/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ These sample programs show how to use the JavaScript client libraries for Azure
2727
| [authenticationMethods.js][authenticationmethods] | authenticates a service client using both Azure Active Directory and an API key |
2828
| [beginAnalyzeActions.js][beginanalyzeactions] | applies multiple Text Analytics actions per document |
2929
| [customText.js][customtext] | applies multiple Custom Text Analytics actions per document |
30+
| [modelVersion.js][modelversion] | shows how to choose model versions for pre-built models. |
3031

3132
## Prerequisites
3233

@@ -80,6 +81,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
8081
[authenticationmethods]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/textanalytics/ai-text-analytics/samples/v5/javascript/authenticationMethods.js
8182
[beginanalyzeactions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/textanalytics/ai-text-analytics/samples/v5/javascript/beginAnalyzeActions.js
8283
[customtext]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/textanalytics/ai-text-analytics/samples/v5/javascript/customText.js
84+
[modelversion]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/textanalytics/ai-text-analytics/samples/v5/javascript/modelVersion.js
8385
[apiref]: https://docs.microsoft.com/javascript/api/@azure/ai-text-analytics
8486
[freesub]: https://azure.microsoft.com/free/
8587
[createinstance_azurecognitiveservicesinstance]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This sample shows how to choose model versions to use with pre-built Text
6+
* Analytics models.
7+
*
8+
* @summary shows how to choose model versions for pre-built models.
9+
*/
10+
11+
const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics");
12+
13+
// Load the .env file if it exists
14+
const dotenv = require("dotenv");
15+
dotenv.config();
16+
17+
// You will need to set these environment variables or edit the following values
18+
const endpoint = process.env["ENDPOINT"] || "<cognitive services endpoint>";
19+
const apiKey = process.env["TEXT_ANALYTICS_API_KEY"] || "<api key>";
20+
21+
const documents = ["This document is written in English."];
22+
23+
async function main() {
24+
console.log("== Choosing Model Version Sample ==");
25+
26+
const client = new TextAnalyticsClient(endpoint, new AzureKeyCredential(apiKey));
27+
28+
const recognizeEntitiesResults = await client.recognizeEntities(documents, "en", {
29+
/**
30+
* Specify the model version by setting this property. "latest" indicates
31+
* the latest generaly availability version of the model. When not specified,
32+
* latest will be assumed. Model versions are date based, e.g "2021-06-01".
33+
* See the documentation for a list of all model versions:
34+
* https://docs.microsoft.com/en-us/azure/cognitive-services/language-service/named-entity-recognition/how-to-call#specify-the-ner-model
35+
*/
36+
modelVersion: "latest"
37+
});
38+
39+
console.log(
40+
`The results of recognizeEntities has been computed using model version: ${recognizeEntitiesResults.modelVersion}`
41+
);
42+
43+
const poller = await client.beginAnalyzeActions(
44+
documents,
45+
{
46+
recognizeEntitiesActions: [
47+
{
48+
/**
49+
* Specify the model version by setting this property. "latest" indicates
50+
* the latest generaly availability version of the model. When not specified,
51+
* latest will be assumed. Model versions are date based, e.g "2021-06-01".
52+
* See the documentation for a list of all model versions:
53+
* https://docs.microsoft.com/en-us/azure/cognitive-services/language-service/named-entity-recognition/how-to-call#specify-the-ner-model
54+
*/
55+
modelVersion: "latest"
56+
}
57+
]
58+
},
59+
"en"
60+
);
61+
62+
const beginAnalyzeActionsResults = await poller.pollUntilDone();
63+
64+
for await (const page of beginAnalyzeActionsResults) {
65+
const entitiesAction = page.recognizeEntitiesResults[0];
66+
if (!entitiesAction.error) {
67+
console.log(
68+
`The results of recognizeEntities action has been computed using model version: ${entitiesAction.results.modelVersion}`
69+
);
70+
}
71+
}
72+
}
73+
74+
main().catch((err) => {
75+
console.error("The sample encountered an error:", err);
76+
});

sdk/textanalytics/ai-text-analytics/samples/v5/typescript/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ These sample programs show how to use the TypeScript client libraries for Azure
2727
| [authenticationMethods.ts][authenticationmethods] | authenticates a service client using both Azure Active Directory and an API key |
2828
| [beginAnalyzeActions.ts][beginanalyzeactions] | applies multiple Text Analytics actions per document |
2929
| [customText.ts][customtext] | applies multiple Custom Text Analytics actions per document |
30+
| [modelVersion.ts][modelversion] | shows how to choose model versions for pre-built models. |
3031

3132
## Prerequisites
3233

@@ -92,6 +93,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
9293
[authenticationmethods]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/textanalytics/ai-text-analytics/samples/v5/typescript/src/authenticationMethods.ts
9394
[beginanalyzeactions]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/textanalytics/ai-text-analytics/samples/v5/typescript/src/beginAnalyzeActions.ts
9495
[customtext]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/textanalytics/ai-text-analytics/samples/v5/typescript/src/customText.ts
96+
[modelversion]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/textanalytics/ai-text-analytics/samples/v5/typescript/src/modelVersion.ts
9597
[apiref]: https://docs.microsoft.com/javascript/api/@azure/ai-text-analytics
9698
[freesub]: https://azure.microsoft.com/free/
9799
[createinstance_azurecognitiveservicesinstance]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This sample shows how to choose model versions to use with pre-built Text
6+
* Analytics models.
7+
*
8+
* @summary shows how to choose model versions for pre-built models.
9+
*/
10+
11+
import { TextAnalyticsClient, AzureKeyCredential } from "@azure/ai-text-analytics";
12+
13+
// Load the .env file if it exists
14+
import * as dotenv from "dotenv";
15+
dotenv.config();
16+
17+
// You will need to set these environment variables or edit the following values
18+
const endpoint = process.env["ENDPOINT"] || "<cognitive services endpoint>";
19+
const apiKey = process.env["TEXT_ANALYTICS_API_KEY"] || "<api key>";
20+
21+
const documents = ["This document is written in English."];
22+
23+
export async function main() {
24+
console.log("== Choosing Model Version Sample ==");
25+
26+
const client = new TextAnalyticsClient(endpoint, new AzureKeyCredential(apiKey));
27+
28+
const recognizeEntitiesResults = await client.recognizeEntities(documents, "en", {
29+
/**
30+
* Specify the model version by setting this property. "latest" indicates
31+
* the latest generaly availability version of the model. When not specified,
32+
* latest will be assumed. Model versions are date based, e.g "2021-06-01".
33+
* See the documentation for a list of all model versions:
34+
* https://docs.microsoft.com/en-us/azure/cognitive-services/language-service/named-entity-recognition/how-to-call#specify-the-ner-model
35+
*/
36+
modelVersion: "latest"
37+
});
38+
39+
console.log(
40+
`The results of recognizeEntities has been computed using model version: ${recognizeEntitiesResults.modelVersion}`
41+
);
42+
43+
const poller = await client.beginAnalyzeActions(
44+
documents,
45+
{
46+
recognizeEntitiesActions: [
47+
{
48+
/**
49+
* Specify the model version by setting this property. "latest" indicates
50+
* the latest generaly availability version of the model. When not specified,
51+
* latest will be assumed. Model versions are date based, e.g "2021-06-01".
52+
* See the documentation for a list of all model versions:
53+
* https://docs.microsoft.com/en-us/azure/cognitive-services/language-service/named-entity-recognition/how-to-call#specify-the-ner-model
54+
*/
55+
modelVersion: "latest"
56+
}
57+
]
58+
},
59+
"en"
60+
);
61+
62+
const beginAnalyzeActionsResults = await poller.pollUntilDone();
63+
64+
for await (const page of beginAnalyzeActionsResults) {
65+
const entitiesAction = page.recognizeEntitiesResults[0];
66+
if (!entitiesAction.error) {
67+
console.log(
68+
`The results of recognizeEntities action has been computed using model version: ${entitiesAction.results.modelVersion}`
69+
);
70+
}
71+
}
72+
}
73+
74+
main().catch((err) => {
75+
console.error("The sample encountered an error:", err);
76+
});

0 commit comments

Comments
 (0)