From 0b8e3b8360abe9dff5aeda12abf4b6e9108e063a Mon Sep 17 00:00:00 2001 From: "AFRICA\\mshaban" Date: Fri, 1 Oct 2021 12:23:25 +0200 Subject: [PATCH] adding remaining samples --- .../samples/sample_analyze_workflow_app.py | 5 +- .../sample_analyze_workflow_app_direct.py | 81 +++++++++++++++++++ .../sample_analyze_workflow_app_with_parms.py | 5 +- .../samples/sample_authentication.py | 4 +- 4 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app_direct.py diff --git a/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app.py b/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app.py index 6a53f87a8cff..887cc713c624 100644 --- a/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app.py +++ b/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app.py @@ -19,11 +19,10 @@ Set the environment variables with your own values before running the sample: 1) AZURE_CONVERSATIONS_ENDPOINT - the endpoint to your CLU resource. 2) AZURE_CONVERSATIONS_KEY - your CLU API key. - 3) AZURE_CONVERSATIONS_PROJECT - the name of your CLU conversations project. - 4) AZURE_CONVERSATIONS_WORKFLOW_PROJECT - the name of your CLU workflow project. + 3) AZURE_CONVERSATIONS_WORKFLOW_PROJECT - the name of your CLU workflow project. """ -def sample_analyze_workflow_app(self, conv_account, conv_key, workflow_project): +def sample_analyze_workflow_app(): # [START analyze_workflow_app] # import libraries import os diff --git a/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app_direct.py b/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app_direct.py new file mode 100644 index 000000000000..cb417bc5dddd --- /dev/null +++ b/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app_direct.py @@ -0,0 +1,81 @@ +# coding=utf-8 +# ------------------------------------ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. +# ------------------------------------ + +""" +FILE: sample_analyze_workflow_app_direct.py + +DESCRIPTION: + This sample demonstrates how to analyze user query using an orchestration/workflow project. + In this sample, we direct the orchestrator project to use a specifc subproject using the "direct_target" parameter. + The "direct_target" in our case will be a Qna project. + + For more info about how to setup a CLU workflow project, see the README. + +USAGE: + python sample_analyze_workflow_app_direct.py + + Set the environment variables with your own values before running the sample: + 1) AZURE_CONVERSATIONS_ENDPOINT - the endpoint to your CLU resource. + 2) AZURE_CONVERSATIONS_KEY - your CLU API key. + 3) AZURE_CONVERSATIONS_WORKFLOW_PROJECT - the name of your CLU workflow project. +""" + +def sample_analyze_workflow_app_direct(): + # [START analyze_workflow_app_direct] + # import libraries + import os + from azure.core.credentials import AzureKeyCredential + + from azure.ai.language.conversations import ConversationAnalysisClient + from azure.ai.language.conversations.models import AnalyzeConversationOptions + + # get secrets + conv_endpoint = os.environ.get("AZURE_CONVERSATIONS_ENDPOINT"), + conv_key = os.environ.get("AZURE_CONVERSATIONS_KEY"), + workflow_project = os.environ.get("AZURE_CONVERSATIONS_WORKFLOW_PROJECT") + + # prepare data + query = "How do you make sushi rice?", + target_intent = "SushiMaking" + input = AnalyzeConversationOptions( + query=query, + direct_target=target_intent, + parameters={ + "SushiMaking": QuestionAnsweringParameters( + calling_options={ + "question": query, + "top": 1, + "confidenceScoreThreshold": 0.1 + } + ) + } + ) + + # analyze query + client = ConversationAnalysisClient(conv_endpoint, AzureKeyCredential(conv_key)) + with client: + result = client.analyze_conversations( + input, + project_name=workflow_project, + deployment_name='production', + ) + + # view result + print("query: {}".format(result.query)) + print("project kind: {}\n".format(result.prediction.project_kind)) + + print("view top intent:") + print("top intent: {}".format(result.prediction.top_intent)) + print("\tcategory: {}".format(result.prediction.intents[0].category)) + print("\tconfidence score: {}\n".format(result.prediction.intents[0].confidence_score)) + + print("view qna result:") + print("\tresult: {}\n".format(result.prediction.intents[0].result)) + # [START analyze_workflow_app_direct] + + +if __name__ == '__main__': + sample_analyze_workflow_app_direct() \ No newline at end of file diff --git a/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app_with_parms.py b/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app_with_parms.py index f428811ebf73..6dd0ff50cd6f 100644 --- a/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app_with_parms.py +++ b/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_analyze_workflow_app_with_parms.py @@ -19,11 +19,10 @@ Set the environment variables with your own values before running the sample: 1) AZURE_CONVERSATIONS_ENDPOINT - the endpoint to your CLU resource. 2) AZURE_CONVERSATIONS_KEY - your CLU API key. - 3) AZURE_CONVERSATIONS_PROJECT - the name of your CLU conversations project. - 4) AZURE_CONVERSATIONS_WORKFLOW_PROJECT - the name of your CLU workflow project. + 3) AZURE_CONVERSATIONS_WORKFLOW_PROJECT - the name of your CLU workflow project. """ -def sample_analyze_workflow_app_with_parms(self, conv_account, conv_key, workflow_project): +def sample_analyze_workflow_app_with_parms(): # [START analyze_workflow_app_with_parms] # import libraries import os diff --git a/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_authentication.py b/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_authentication.py index a31ec6c9466f..c56212ae987e 100644 --- a/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_authentication.py +++ b/sdk/cognitivelanguage/azure-ai-language-conversations/samples/sample_authentication.py @@ -50,7 +50,7 @@ def sample_authentication_api_key(): def sample_authentication_with_azure_active_directory(): - # [START create_dt_client_with_aad] + # [START create_clu_client_with_aad] """DefaultAzureCredential will use the values from these environment variables: AZURE_CLIENT_ID, AZURE_TENANT_ID, AZURE_CLIENT_SECRET """ @@ -61,7 +61,7 @@ def sample_authentication_with_azure_active_directory(): credential = DefaultAzureCredential() clu_client = ConversationAnalysisClient(endpoint, credential) - # [END create_dt_client_with_aad] + # [END create_clu_client_with_aad] if __name__ == '__main__':