File: test_conversation_prediction_with_language.py

package info (click to toggle)
python-azure 20251104%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 770,224 kB
  • sloc: python: 6,357,217; ansic: 804; javascript: 287; makefile: 198; sh: 193; xml: 109
file content (107 lines) | stat: -rw-r--r-- 3,837 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import functools
import pytest

from devtools_testutils import AzureRecordedTestCase, EnvironmentVariableLoader, recorded_by_proxy
from azure.ai.language.conversations import ConversationAnalysisClient
from azure.ai.language.conversations.models import (
    AnalyzeConversationOperationInput,
    ConversationActionContent,
    ConversationAnalysisInput,
    TextConversationItem,
    ConversationActionResult,
    ConversationPrediction,
    ConversationIntent,
    ConversationEntity,
    StringIndexType,
    ResolutionBase,
    DateTimeResolution,
    AnalyzeConversationActionResult,
    ConversationLanguageUnderstandingInput,
)
from typing import cast

from azure.core.credentials import AzureKeyCredential

ConversationsPreparer = functools.partial(
    EnvironmentVariableLoader,
    "conversations",
    conversations_endpoint="https://Sanitized.cognitiveservices.azure.com/",
    conversations_key="fake_key",
)


class TestConversations(AzureRecordedTestCase):

    # Start with any helper functions you might need, for example a client creation method:
    def create_client(self, endpoint, key):
        credential = AzureKeyCredential(key)
        client = ConversationAnalysisClient(endpoint, credential)
        return client

    ...


class TestConversationsCase(TestConversations):
    @ConversationsPreparer()
    @recorded_by_proxy
    def test_conversation_prediction_with_language(self, conversations_endpoint, conversations_key):
        client = self.create_client(conversations_endpoint, conversations_key)

        project_name = "EmailApp"
        deployment_name = "production"

        # Build request using strongly-typed models; set language to Spanish ("es")
        data = ConversationLanguageUnderstandingInput(
            conversation_input=ConversationAnalysisInput(
                conversation_item=TextConversationItem(
                    id="1",
                    participant_id="participant1",
                    text="Enviar un email a Carol acerca de la presentación de mañana",
                    language="es",
                )
            ),
            action_content=ConversationActionContent(
                project_name=project_name,
                deployment_name=deployment_name,
                string_index_type=StringIndexType.UTF16_CODE_UNIT,
                verbose=True,
            ),
        )

        # Call API
        response: AnalyzeConversationActionResult = client.analyze_conversation(data)

        # Cast to discriminator subtype (C#: `as ConversationActionResult`)
        conversation_result = cast(ConversationActionResult, response)
        prediction = conversation_result.result.prediction
        assert isinstance(prediction, ConversationPrediction)

        print(f"Top intent: {prediction.top_intent}")

        # Intents
        print("Intents:")
        for intent in prediction.intents or []:
            print(f"Category: {intent.category}")
            print(f"Confidence: {intent.confidence}")
            print()

        # Entities
        print("Entities:")
        for entity in prediction.entities or []:
            print(f"Category: {entity.category}")
            print(f"Text: {entity.text}")
            print(f"Offset: {entity.offset}")
            print(f"Length: {entity.length}")
            print(f"Confidence: {entity.confidence}")
            print()

            if entity.resolutions:
                for res in entity.resolutions:
                    if isinstance(res, DateTimeResolution):
                        print(f"Datetime Sub Kind: {getattr(res, 'date_time_sub_kind', None)}")
                        print(f"Timex: {res.timex}")
                        print(f"Value: {res.value}")
                        print()

        # Final assertion (mirror C#)
        assert prediction.top_intent == "SendEmail"