File: test_conv_sentiment_async.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (71 lines) | stat: -rw-r--r-- 2,875 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# coding=utf-8
# ------------------------------------
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
# ------------------------------------
import pytest
from azure.ai.language.conversations.aio import ConversationAnalysisClient
from azure.core.credentials import AzureKeyCredential
from devtools_testutils import AzureRecordedTestCase


class TestConversationalSentimentTaskAsync(AzureRecordedTestCase):

    @pytest.mark.asyncio
    async def test_conversational_sentiment_task(self, recorded_test, conversation_creds):
        # analyze query
        client = ConversationAnalysisClient(
            conversation_creds["endpoint"],
            AzureKeyCredential(conversation_creds["key"])
        )
        async with client:
            poller = await client.begin_conversation_analysis(
                task={
                  "displayName": "Sentiment Analysis from a call center conversation",
                  "analysisInput": {
                    "conversations": [
                      {
                        "id": "1",
                        "language": "en",
                        "modality": "transcript",
                        "conversationItems": [
                          {
                            "participantId": "1",
                            "id": "1",
                            "text": "I like the service. I do not like the food",
                            "lexical": "i like the service i do not like the food",
                          }
                        ]
                      }
                    ]
                  },
                  "tasks": [
                    {
                      "taskName": "Conversation Sentiment Analysis",
                      "kind": "ConversationalSentimentTask",
                      "parameters": {
                        "modelVersion": "latest",
                        "predictionSource": "text"
                      }
                    }
                  ]
                }
            )

            # assert - main object
            result = await poller.result()
            assert result is not None
            assert result["status"] == "succeeded"

            # assert - task result
            task_result = result["tasks"]["items"][0]
            assert task_result["status"] == "succeeded"
            # assert task_result["kind"] == "conversationalSentimentResults" https://dev.azure.com/msazure/Cognitive%20Services/_workitems/edit/16041272

            # assert - conv result
            sentiment_result = task_result["results"]["conversations"][0]
            conversation_sentiment = sentiment_result["conversationItems"][0]
            assert conversation_sentiment["sentiment"] == "mixed"
            assert conversation_sentiment["participantId"] == "1"
            assert conversation_sentiment["confidenceScores"]