Skip to content

Commit 41aa5fc

Browse files
authored
[Language Conversations] Publish samples (Azure#23169)
1 parent d468f9b commit 41aa5fc

31 files changed

+1751
-59
lines changed

sdk/cognitivelanguage/ai-language-conversations/samples-dev/sample_analyze_conversation_app.ts renamed to sdk/cognitivelanguage/ai-language-conversations/samples-dev/analyzeConversationApp.ts

File renamed without changes.

sdk/cognitivelanguage/ai-language-conversations/samples-dev/sample_analyze_orchestration_app_conv_response.ts renamed to sdk/cognitivelanguage/ai-language-conversations/samples-dev/analyzeOrchestrationAppConvResponse.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,27 @@ export async function main() {
5454
console.log("query: ", result.query);
5555
console.log("project kind: ", result.prediction.projectKind);
5656

57-
const top_intent = result.prediction.topIntent || "None";
58-
console.log("top intent: ", top_intent);
57+
const topIntent = result.prediction.topIntent || "None";
58+
console.log("top intent: ", topIntent);
5959

6060
const prediction = result.prediction;
6161
if (prediction.projectKind == "Orchestration") {
62-
const top_intent_object = prediction.intents[top_intent];
63-
console.log("confidence score: ", top_intent_object.confidence);
64-
console.log("project kind: ", top_intent_object.targetProjectKind);
62+
const topIntentObject = prediction.intents[topIntent];
63+
console.log("confidence score: ", topIntentObject.confidence);
64+
console.log("project kind: ", topIntentObject.targetProjectKind);
6565

66-
if (top_intent_object.targetProjectKind == "Conversation") {
66+
if (topIntentObject.targetProjectKind == "Conversation") {
6767
console.log("\nview conversation result:");
6868

69-
if (top_intent_object.result && top_intent_object.result.prediction) {
70-
console.log("\ntop intent: ", top_intent_object.result.prediction.topIntent);
69+
if (topIntentObject.result && topIntentObject.result.prediction) {
70+
console.log("\ntop intent: ", topIntentObject.result.prediction.topIntent);
7171

72-
const intent = top_intent_object.result.prediction.intents[0];
72+
const intent = topIntentObject.result.prediction.intents[0];
7373
console.log("category: ", intent.category);
7474
console.log("confidence score: ", intent.confidence);
7575

7676
console.log("\nview entities:");
77-
top_intent_object.result.prediction.entities.forEach((entity) => {
77+
topIntentObject.result.prediction.entities.forEach((entity) => {
7878
console.log("\ncategory: ", entity.category);
7979
console.log("text: ", entity.text);
8080
console.log("confidence score: %f", entity.confidence);

sdk/cognitivelanguage/ai-language-conversations/samples-dev/sample_analyze_orchestration_app_luis_response.ts renamed to sdk/cognitivelanguage/ai-language-conversations/samples-dev/analyzeOrchestrationAppLuisResponse.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,22 @@ export async function main() {
5353
console.log("query: ", result.query);
5454
console.log("project kind: ", result.prediction.projectKind);
5555

56-
const top_intent = result.prediction.topIntent || "None";
57-
console.log("top intent: ", top_intent);
56+
const topIntent = result.prediction.topIntent || "None";
57+
console.log("top intent: ", topIntent);
5858

5959
const prediction = result.prediction;
6060
if (prediction.projectKind == "Orchestration") {
61-
const top_intent_object = prediction.intents[top_intent];
62-
console.log("confidence score: ", top_intent_object.confidence);
63-
console.log("project kind: ", top_intent_object.targetProjectKind);
61+
const topIntentObject = prediction.intents[topIntent];
62+
console.log("confidence score: ", topIntentObject.confidence);
63+
console.log("project kind: ", topIntentObject.targetProjectKind);
6464

65-
if (top_intent_object.targetProjectKind == "Luis" && top_intent_object.result) {
65+
if (topIntentObject.targetProjectKind == "Luis" && topIntentObject.result) {
6666
console.log("\nluis response:");
6767

68-
const luis_response = top_intent_object.result.prediction;
69-
console.log("top intent: ", luis_response.topIntent);
68+
const luisResponse = topIntentObject.result.prediction;
69+
console.log("top intent: ", luisResponse.topIntent);
7070
console.log("\nentities:");
71-
for (const entity of luis_response.entities) {
71+
for (const entity of luisResponse.entities) {
7272
console.log("\n", entity);
7373
}
7474
}

sdk/cognitivelanguage/ai-language-conversations/samples-dev/sample_analyze_orchestration_app_qna_response.ts renamed to sdk/cognitivelanguage/ai-language-conversations/samples-dev/analyzeOrchestrationAppQnaResponse.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,20 @@ export async function main() {
5353
console.log("query: ", result.query);
5454
console.log("project kind: ", result.prediction.projectKind);
5555

56-
const top_intent = result.prediction.topIntent || "None";
57-
console.log("\ntop intent: ", top_intent);
56+
const topIntent = result.prediction.topIntent || "None";
57+
console.log("\ntop intent: ", topIntent);
5858

5959
const prediction = result.prediction;
6060
if (prediction.projectKind == "Orchestration") {
61-
const top_intent_object = prediction.intents[top_intent];
62-
console.log("confidence score: ", top_intent_object.confidence);
63-
console.log("project kind: ", top_intent_object.targetProjectKind);
61+
const topIntentObject = prediction.intents[topIntent];
62+
console.log("confidence score: ", topIntentObject.confidence);
63+
console.log("project kind: ", topIntentObject.targetProjectKind);
6464

65-
if (top_intent_object.targetProjectKind == "QuestionAnswering") {
65+
if (topIntentObject.targetProjectKind == "QuestionAnswering") {
6666
console.log("\nqna response:");
67-
const qna_response = top_intent_object.result;
68-
if (qna_response && qna_response.answers) {
69-
for (const answer of qna_response.answers) {
67+
const qnaResponse = topIntentObject.result;
68+
if (qnaResponse && qnaResponse.answers) {
69+
for (const answer of qnaResponse.answers) {
7070
console.log("\nanswer: ", answer.answer);
7171
console.log("confidence score: ", answer.confidence);
7272
}

sdk/cognitivelanguage/ai-language-conversations/samples-dev/sample_analyze_orchestration_direct_target.ts renamed to sdk/cognitivelanguage/ai-language-conversations/samples-dev/analyzeOrchestrationDirectTarget.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ const service: ConversationAnalysisClient = new ConversationAnalysisClient(
2929
);
3030

3131
const query = "How are you?";
32-
const qna_app = "ChitChat-QnA";
32+
const qnaApp = "ChitChat-QnA";
3333

3434
const body: ConversationalTask = {
3535
kind: "Conversation",
@@ -46,7 +46,7 @@ const body: ConversationalTask = {
4646
projectName: projectName,
4747
deploymentName: deploymentName,
4848
isLoggingEnabled: false,
49-
directTarget: qna_app,
49+
directTarget: qnaApp,
5050
targetProjectParameters: {
5151
"ChitChat-QnA": {
5252
targetProjectKind: "QuestionAnswering",
@@ -64,21 +64,21 @@ export async function main() {
6464
console.log("query: ", result.query);
6565
console.log("project kind: ", result.prediction.projectKind);
6666

67-
const top_intent = result.prediction.topIntent || "None";
68-
console.log("\ntop intent: ", top_intent);
67+
const topIntent = result.prediction.topIntent || "None";
68+
console.log("\ntop intent: ", topIntent);
6969

7070
const prediction = result.prediction;
7171
if (prediction.projectKind == "Orchestration") {
72-
const top_intent_object = prediction.intents[top_intent];
73-
console.log("confidence score: ", top_intent_object.confidence);
74-
console.log("project kind: ", top_intent_object.targetProjectKind);
72+
const topIntentObject = prediction.intents[topIntent];
73+
console.log("confidence score: ", topIntentObject.confidence);
74+
console.log("project kind: ", topIntentObject.targetProjectKind);
7575

76-
if (top_intent_object.targetProjectKind == "QuestionAnswering") {
76+
if (topIntentObject.targetProjectKind == "QuestionAnswering") {
7777
console.log("\nqna response:");
7878

79-
const qna_response = top_intent_object.result;
80-
if (qna_response && qna_response.answers) {
81-
for (const answer of qna_response.answers) {
79+
const qnaResponse = topIntentObject.result;
80+
if (qnaResponse && qnaResponse.answers) {
81+
for (const answer of qnaResponse.answers) {
8282
console.log("\nanswer: ", answer.answer);
8383
console.log("confidence score: ", answer.confidence);
8484
}

sdk/cognitivelanguage/ai-language-conversations/samples-dev/sample_authentication.ts renamed to sdk/cognitivelanguage/ai-language-conversations/samples-dev/authentication.ts

File renamed without changes.

sdk/cognitivelanguage/ai-language-conversations/samples-dev/sample_conv_pii_transcript_input.ts renamed to sdk/cognitivelanguage/ai-language-conversations/samples-dev/convPiiTranscriptInput.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,26 @@ export async function main() {
7979
const actionResult = await poller.pollUntilDone();
8080
if (actionResult.tasks.items === undefined) return;
8181

82-
const task_result = actionResult.tasks.items[0];
83-
if (task_result.kind == "conversationalPIIResults") {
82+
const taskResult = actionResult.tasks.items[0];
83+
if (taskResult.kind == "conversationalPIIResults") {
8484
console.log("... view task status ...");
85-
console.log("status: ", task_result.status);
86-
const conv_pii_result = task_result.results;
87-
if (conv_pii_result.errors && conv_pii_result.errors.length != 0) {
85+
console.log("status: ", taskResult.status);
86+
const convPiiResult = taskResult.results;
87+
if (convPiiResult.errors && convPiiResult.errors.length != 0) {
8888
console.log("... errors occured ...");
89-
for (const error of conv_pii_result.errors) {
89+
for (const error of convPiiResult.errors) {
9090
console.log(error);
9191
}
9292
} else {
93-
const conversation_result = conv_pii_result.conversations[0];
94-
if (conversation_result.warnings && conversation_result.warnings.length != 0) {
93+
const conversationResult = convPiiResult.conversations[0];
94+
if (conversationResult.warnings && conversationResult.warnings.length != 0) {
9595
console.log("... view warnings ...");
96-
for (const warning of conversation_result.warnings) {
96+
for (const warning of conversationResult.warnings) {
9797
console.log(warning);
9898
}
9999
} else {
100100
console.log("... view task result ...");
101-
for (const conversation of conversation_result.conversationItems) {
101+
for (const conversation of conversationResult.conversationItems) {
102102
console.log("conversation id: ", conversation.id);
103103
console.log("... entities ...");
104104
for (const entity of conversation.entities) {

sdk/cognitivelanguage/ai-language-conversations/samples-dev/sample_conv_summarization.ts renamed to sdk/cognitivelanguage/ai-language-conversations/samples-dev/convSummarization.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,25 @@ export async function main() {
7171
const actionResult = await poller.pollUntilDone();
7272
if (actionResult.tasks.items === undefined) return;
7373

74-
const task_result = actionResult.tasks.items[0];
75-
if (task_result.kind == "conversationalSummarizationResults") {
74+
const taskResult = actionResult.tasks.items[0];
75+
if (taskResult.kind == "conversationalSummarizationResults") {
7676
console.log("... view task status ...");
77-
console.log("status: %s", task_result.status);
78-
const resolution_result = task_result.results;
79-
if (resolution_result.errors && resolution_result.errors.length != 0) {
77+
console.log("status: %s", taskResult.status);
78+
const resolutionResult = taskResult.results;
79+
if (resolutionResult.errors && resolutionResult.errors.length != 0) {
8080
console.log("... errors occured ...");
81-
for (const error of resolution_result.errors) {
81+
for (const error of resolutionResult.errors) {
8282
console.log(error);
8383
}
8484
} else {
85-
const conversation_result = resolution_result.conversations[0];
86-
if (conversation_result.warnings && conversation_result.warnings.length != 0) {
85+
const conversationResult = resolutionResult.conversations[0];
86+
if (conversationResult.warnings && conversationResult.warnings.length != 0) {
8787
console.log("... view warnings ...");
88-
for (const warning of conversation_result.warnings) {
88+
for (const warning of conversationResult.warnings) {
8989
console.log(warning);
9090
}
9191
} else {
92-
const summaries = conversation_result.summaries;
92+
const summaries = conversationResult.summaries;
9393
console.log("... view task result ...");
9494
console.log("issue: %s", summaries[0].text);
9595
console.log("resolution: %s", summaries[1].text);
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
page_type: sample
3+
languages:
4+
- javascript
5+
products:
6+
- azure
7+
- azure-cognitive-services
8+
- language-service
9+
urlFragment: ai-language-conversations-javascript-beta
10+
---
11+
12+
# Azure Cognitive Language Service client library samples for JavaScript (Beta)
13+
14+
These sample programs show how to use the JavaScript client libraries for Azure Cognitive Language Service in some common scenarios.
15+
16+
| **File Name** | **Description** |
17+
| ----------------------------------------------------------------------------- | ----------------------------------------------------------------- |
18+
| [analyzeConversationApp.js][analyzeconversationapp] | Conversational query analysis for intents and entities extraction |
19+
| [analyzeOrchestrationAppConvResponse.js][analyzeorchestrationappconvresponse] | Orchestration project with conversational response |
20+
| [analyzeOrchestrationAppLuisResponse.js][analyzeorchestrationappluisresponse] | Orchestration project with LUIS response |
21+
| [analyzeOrchestrationAppQnaResponse.js][analyzeorchestrationappqnaresponse] | Orchestration project with QnA response |
22+
| [analyzeOrchestrationDirectTarget.js][analyzeorchestrationdirecttarget] | Orchestration project with direct target |
23+
| [convPiiTranscriptInput.js][convpiitranscriptinput] | PII conversational analysis |
24+
| [convSummarization.js][convsummarization] | Conversation Summarization |
25+
| [authentication.js][authentication] | authenticates a service client using an API key |
26+
27+
## Prerequisites
28+
29+
The sample programs are compatible with [LTS versions of Node.js](https://nodejs.org/about/releases/).
30+
31+
You need [an Azure subscription][freesub] and the following Azure resources to run these sample programs:
32+
33+
- [Azure Cognitive Services instance][createinstance_azurecognitiveservicesinstance]
34+
35+
Samples retrieve credentials to access the service endpoint from environment variables. Alternatively, edit the source code to include the appropriate credentials. See each individual sample for details on which environment variables/credentials it requires to function.
36+
37+
Adapting the samples to run in the browser may require some additional consideration. For details, please see the [package README][package].
38+
39+
## Setup
40+
41+
To run the samples using the published version of the package:
42+
43+
1. Install the dependencies using `npm`:
44+
45+
```bash
46+
npm install
47+
```
48+
49+
2. Edit the file `sample.env`, adding the correct credentials to access the Azure service and run the samples. Then rename the file from `sample.env` to just `.env`. The sample programs will read this file automatically.
50+
51+
3. Run whichever samples you like (note that some samples may require additional setup, see the table above):
52+
53+
```bash
54+
node analyzeConversationApp.js
55+
```
56+
57+
Alternatively, run a single sample with the correct environment variables set (setting up the `.env` file is not required if you do this), for example (cross-platform):
58+
59+
```bash
60+
npx cross-env AZURE_CONVERSATIONS_ENDPOINT="<azure conversations endpoint>" AZURE_CONVERSATIONS_KEY="<azure conversations key>" AZURE_CONVERSATIONS_PROJECT_NAME="<azure conversations project name>" AZURE_CONVERSATIONS_DEPLOYMENT_NAME="<azure conversations deployment name>" node analyzeConversationApp.js
61+
```
62+
63+
## Next Steps
64+
65+
Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.
66+
67+
[analyzeconversationapp]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-conversations/samples/v1-beta/javascript/analyzeConversationApp.js
68+
[analyzeorchestrationappconvresponse]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-conversations/samples/v1-beta/javascript/analyzeOrchestrationAppConvResponse.js
69+
[analyzeorchestrationappluisresponse]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-conversations/samples/v1-beta/javascript/analyzeOrchestrationAppLuisResponse.js
70+
[analyzeorchestrationappqnaresponse]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-conversations/samples/v1-beta/javascript/analyzeOrchestrationAppQnaResponse.js
71+
[analyzeorchestrationdirecttarget]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-conversations/samples/v1-beta/javascript/analyzeOrchestrationDirectTarget.js
72+
[convpiitranscriptinput]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-conversations/samples/v1-beta/javascript/convPiiTranscriptInput.js
73+
[convsummarization]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-conversations/samples/v1-beta/javascript/convSummarization.js
74+
[authentication]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/cognitivelanguage/ai-language-conversations/samples/v1-beta/javascript/authentication.js
75+
[apiref]: https://docs.microsoft.com/javascript/api/@azure/ai-language-conversations
76+
[freesub]: https://azure.microsoft.com/free/
77+
[createinstance_azurecognitiveservicesinstance]: https://docs.microsoft.com/azure/cognitive-services/cognitive-services-apis-create-account
78+
[package]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/cognitivelanguage/ai-language-conversations/README.md
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This sample demonstrates how to analyze user query for intents and entities using
6+
* a conversation project with a language parameter.
7+
*
8+
* @summary Conversational query analysis for intents and entities extraction
9+
*/
10+
11+
const { ConversationAnalysisClient } = require("@azure/ai-language-conversations");
12+
const { AzureKeyCredential } = require("@azure/core-auth");
13+
require("dotenv").config();
14+
15+
//Get secrets
16+
//You will have to set these environment variables for the sample to work
17+
const cluEndpoint =
18+
process.env.AZURE_CONVERSATIONS_ENDPOINT || "https://dummyendpoint.cognitiveservices.azure.com";
19+
const cluKey = process.env.AZURE_CONVERSATIONS_KEY || "<api-key>";
20+
const projectName = process.env.AZURE_CONVERSATIONS_PROJECT_NAME || "<project-name>";
21+
const deploymentName = process.env.AZURE_CONVERSATIONS_DEPLOYMENT_NAME || "<deployment-name>";
22+
23+
const service = new ConversationAnalysisClient(cluEndpoint, new AzureKeyCredential(cluKey));
24+
25+
const body = {
26+
kind: "Conversation",
27+
analysisInput: {
28+
conversationItem: {
29+
id: "id__7863",
30+
participantId: "id__7863",
31+
text: "Send an email to Carol about the tomorrow's demo",
32+
},
33+
},
34+
parameters: {
35+
projectName: projectName,
36+
deploymentName: deploymentName,
37+
},
38+
};
39+
40+
async function main() {
41+
//Analyze query
42+
const { result } = await service.analyzeConversation(body);
43+
console.log("query: ", result.query);
44+
console.log("project kind: ", result.prediction.projectKind);
45+
console.log("top intent: ", result.prediction.topIntent);
46+
47+
const prediction = result.prediction;
48+
if (prediction.projectKind == "Conversation") {
49+
console.log("category: ", prediction.intents[0].category);
50+
console.log("confidence score: ", prediction.intents[0].confidence);
51+
console.log("entities:");
52+
53+
for (const entity of prediction.entities) {
54+
console.log("\ncategory: ", entity.category);
55+
console.log("text: ", entity.text);
56+
console.log("confidence score: ", entity.confidence);
57+
58+
if (entity.resolutions) {
59+
console.log("resolutions:");
60+
for (const resolution of entity.resolutions) {
61+
console.log("kind: ", resolution.resolutionKind);
62+
if ("value" in resolution) console.log("value: ", resolution.value);
63+
}
64+
}
65+
66+
if (entity.extraInformation) {
67+
console.log("extra info:");
68+
for (const data of entity.extraInformation) {
69+
console.log("kind: ", data.extraInformationKind);
70+
if (data.extraInformationKind == "ListKey") console.log("key: ", data.key);
71+
if (data.extraInformationKind == "EntitySubtype") console.log("value: ", data.value);
72+
}
73+
}
74+
}
75+
}
76+
}
77+
78+
main().catch((err) => {
79+
console.error("The sample encountered an error:", err);
80+
});
81+
82+
module.exports = { main };

0 commit comments

Comments
 (0)