Пример #1
0
def test_intent_from_parse__single_parameter_single_slot():
    parse_result = {
        'input':
        'I want a dark roast espresso',
        'intent': {
            'intentName': 'AskEspresso',
            'probability': 0.56
        },
        'slots': [{
            'range': {
                'start': 9,
                'end': 13
            },
            'rawValue': 'dark',
            'value': {
                'kind': 'Custom',
                'value': 'dark'
            },
            'entity': 'CoffeeRoast',
            'slotName': 'roast'
        }]
    }
    prediction_component = _get_prediction_component()
    result = prediction_component.intent_from_parse_result(
        pf.from_dict(parse_result))
    assert result == ca.AskEspresso(roast="dark")
Пример #2
0
def test_prediction_from_parse_renders_language():
    prediction_component = _get_prediction_component()
    prediction_component.intent_from_parse_result = MagicMock(
        return_value=ca.AskEspresso(roast="dark"))
    parse_result = pf.from_dict({
        'input': 'fake message',
        'intent': {
            'intentName': 'fake intent',
            'probability': 0.677
        },
        'slots': []
    })
    result = prediction_component.prediction_from_parse_result(
        parse_result, LanguageCode.ENGLISH)
    assert result.fulfillment_text in [
        "dark roast espresso, good choice!",
        "Alright, dark roasted espresso for you"
    ]

    messages = result.fulfillment_messages.for_group(
        IntentResponseGroup.DEFAULT)
    assert messages
    assert messages[0].choices == [
        "dark roast espresso, good choice!",
        "Alright, dark roasted espresso for you"
    ]

    with pytest.warns(DeprecationWarning):
        messages = result.fulfillment_messages(IntentResponseGroup.DEFAULT)
        assert messages
        assert messages[0].choices == [
            "dark roast espresso, good choice!",
            "Alright, dark roasted espresso for you"
        ]
Пример #3
0
def test_date_mapping_from_service():
    mapping = entities.DateMapping()
    snips_date_result = {
        'input':
        'My birthday is on august 24',
        'intent': {
            'intentName': 'UserSaysBirthday',
            'probability': 1.0
        },
        'slots': [{
            'range': {
                'start': 15,
                'end': 27
            },
            'rawValue': 'on august 24',
            'value': {
                'kind': 'InstantTime',
                'value': '2021-08-24 00:00:00 +02:00',
                'grain': 'Day',
                'precision': 'Exact'
            },
            'entity': 'snips/date',
            'slotName': 'birthday_date'
        }]
    }
    parse_result = pf.from_dict(snips_date_result)
    entity = mapping.from_service(parse_result.slots[0].value)
    assert entity == Sys.Date(2021, 8, 24)
Пример #4
0
def test_from_dict_slots():
    parse_result = {
        'input': 'I want a dark roast espresso',
        'intent': {'intentName': 'AskEspresso', 'probability': 0.56},
        'slots': [
            {
                'range': {'start': 9, 'end': 13},
                'rawValue': 'dark',
                'value': {'kind': 'Custom', 'value': 'dark'},
                'entity': 'CoffeeRoast',
                'slotName': 'roast'
            }
        ]
    }
    expected = pf.ParseResult(
        input="I want a dark roast espresso",
        intent=pf.ParseResultIntent(
            intentName="AskEspresso",
            probability=0.56
        ),
        slots=[
            pf.ParseResultSlot(
                range=pf.ParseResultSlotRange(start=9, end=13),
                rawValue="dark",
                value={'kind': 'Custom', 'value': 'dark'},
                entity="CoffeeRoast",
                slotName="roast"
            )
        ]
    )
    assert pf.from_dict(parse_result) == expected
Пример #5
0
def test_intent_from_parse__list_parameter_list_slot():
    parse_result = {
        'input':
        'My colors are green and red',
        'intent': {
            'intentName': 'UserSaysManyColors',
            'probability': 0.66
        },
        'slots': [{
            'range': {
                'start': 14,
                'end': 19
            },
            'rawValue': 'green',
            'value': {
                'kind': 'Custom',
                'value': 'Green'
            },
            'entity': 'I_IntentsColor',
            'slotName': 'user_color_list'
        }, {
            'range': {
                'start': 24,
                'end': 27
            },
            'rawValue': 'red',
            'value': {
                'kind': 'Custom',
                'value': 'Red'
            },
            'entity': 'I_IntentsColor',
            'slotName': 'user_color_list'
        }]
    }

    @dataclass
    class UserSaysManyColors(Intent):
        name = "UserSaysManyColors"
        user_color_list: List[Sys.Color]

    UserSaysManyColors.__intent_language_data__ = ca.mock_language_data(
        UserSaysManyColors, [
            "My colors are $user_color_list{red}, $user_color_list{green} and $user_color_list{blue}",
            "I give you some colors $user_color_list{yellow}, $user_color_list{orange} and also $user_color_list{purple}"
        ], ["$user_color it is"], LanguageCode.ENGLISH)

    class MyAgent(Agent):
        languages = ['en']

    MyAgent.register(UserSaysManyColors)

    prediction_component = prediction.SnipsPredictionComponent(
        MyAgent, entities.ENTITY_MAPPINGS)

    parse_result = pf.from_dict(parse_result)
    print("NAME", parse_result.intent.intentName)
    result = prediction_component.intent_from_parse_result(parse_result)
    assert result == UserSaysManyColors(user_color_list=['Green', 'Red'])
Пример #6
0
def test_intent_from_parse__default_parameter():
    parse_result = {
        'input': 'I want an espresso',
        'intent': {
            'intentName': 'AskEspresso',
            'probability': 0.677
        },
        'slots': []
    }
    prediction_component = _get_prediction_component()
    result = prediction_component.intent_from_parse_result(
        pf.from_dict(parse_result))
    assert result == ca.AskEspresso()
Пример #7
0
def test_from_dict_no_slots():
    parse_result = {
        'input': 'I want a coffee',
        'intent': {'intentName': 'AskCoffee', 'probability': 0.65},
        'slots': []
    }
    expected = pf.ParseResult(
        input="I want a coffee",
        intent=pf.ParseResultIntent(
            intentName="AskCoffee",
            probability=0.65
        ),
        slots=[]
    )
    assert pf.from_dict(parse_result) == expected
Пример #8
0
    def predict(self,
                message: str,
                session: str = None,
                language: Union[LanguageCode, str] = None) -> SnipsPrediction:
        """
        Predict the given User message in the given session using the given
        language. When `session` or `language` are None, `predict` will use the
        default values that are specified in :meth:`__init__`.

        *predict* will return an instance of :class:`Prediction`, representing
        the service response.

        >>> from intents.connectors._experimental.snips import SnipsConnector
        >>> from example_agent import ExampleAgent
        >>> snips = SnipsConnector(ExampleAgent)
        >>> snips.upload() # This trains the models
        >>> prediction = snips.predict("Hi, my name is Guido")
        >>> prediction.intent
        UserNameGive(user_name='Guido')
        >>> prediction.intent.user_name
        "Guido"
        >>> prediction.fulfillment_text
        "Hi Guido, I'm Bot"
        >>> prediction.confidence
        0.62

        Note that the Italian version of :class:`~example_agent.ExampleAgent`
        won't be trained, as :class:`Sys.Date` is not available for the Italian
        language in Snips.

        Args:
            message: The User message to predict
            session: Any string identifying a conversation
            language: A LanguageCode object, or a ISO 639-1 string (e.g. "en")
        """
        if not language:
            language = self.default_language
        language = ensure_language_code(language)
        parse_result_dict = self.nlu_engines[language].parse(message)
        parse_result = prediction_format.from_dict(parse_result_dict)
        prediction = self.prediction_component.prediction_from_parse_result(
            parse_result, language)
        return self.prediction_component.fulfill_local(prediction, language)