Пример #1
0
    async def test_remove_path_value(self):
        test = {}
        ObjectPath.set_path_value(test, "x.y.z", 15)
        ObjectPath.set_path_value(test, "x.p", "hello")
        ObjectPath.set_path_value(test, "foo", {"Bar": 15, "Blat": "yo"})
        ObjectPath.set_path_value(test, "x.a[1]", "yabba")
        ObjectPath.set_path_value(test, "x.a[0]", "dabba")

        ObjectPath.remove_path_value(test, "x.y.z")
        with self.assertRaises(KeyError):
            ObjectPath.get_path_value(test, "x.y.z")

        assert ObjectPath.get_path_value(test, "x.y.z", 99) == 99

        ObjectPath.remove_path_value(test, "x.a[1]")
        assert not ObjectPath.try_get_path_value(test, "x.a[1]")

        assert ObjectPath.try_get_path_value(test, "x.a[0]") == "dabba"
Пример #2
0
    async def begin_dialog(
        self, dialog_context: DialogContext, options: object = None
    ) -> DialogTurnResult:
        """
        Called when the dialog is started and pushed onto the dialog stack.

        .. remarks:
          If the task is successful, the result indicates whether the dialog is still
          active after the turn has been processed by the dialog.

        :param dialog_context: The :class:'botbuilder.dialogs.DialogContext' for the current turn of conversation.
        :param options: Optional, initial information to pass to the dialog.
        """

        if not dialog_context:
            raise TypeError("DialogContext is required")

        if (
            dialog_context.context
            and dialog_context.context.activity
            and dialog_context.context.activity.type != ActivityTypes.message
        ):
            return Dialog.end_of_turn

        dialog_options = QnAMakerDialogOptions(
            options=self._get_qnamaker_options(dialog_context),
            response_options=self._get_qna_response_options(dialog_context),
        )

        if options:
            dialog_options = ObjectPath.assign(dialog_options, options)

        ObjectPath.set_path_value(
            dialog_context.active_dialog.state,
            QnAMakerDialog.KEY_OPTIONS,
            dialog_options,
        )

        return await super().begin_dialog(dialog_context, dialog_options)
Пример #3
0
    async def __check_for_multiturn_prompt(self, step_context: WaterfallStepContext):
        dialog_options: QnAMakerDialogOptions = ObjectPath.get_path_value(
            step_context.active_dialog.state, QnAMakerDialog.KEY_OPTIONS
        )

        response = step_context.result
        if response and isinstance(response, List):
            answer = response[0]
            if answer.context and answer.context.prompts:
                previous_context_data = ObjectPath.get_path_value(
                    step_context.active_dialog.state,
                    QnAMakerDialog.KEY_QNA_CONTEXT_DATA,
                    {},
                )
                for prompt in answer.context.prompts:
                    previous_context_data[prompt.display_text] = prompt.qna_id

                ObjectPath.set_path_value(
                    step_context.active_dialog.state,
                    QnAMakerDialog.KEY_QNA_CONTEXT_DATA,
                    previous_context_data,
                )
                ObjectPath.set_path_value(
                    step_context.active_dialog.state,
                    QnAMakerDialog.KEY_PREVIOUS_QNA_ID,
                    answer.id,
                )
                ObjectPath.set_path_value(
                    step_context.active_dialog.state,
                    QnAMakerDialog.KEY_OPTIONS,
                    dialog_options,
                )

                # Get multi-turn prompts card activity.
                message = QnACardBuilder.get_qna_prompts_card(
                    answer, dialog_options.response_options.card_no_match_text
                )
                await step_context.context.send_activity(message)

                return DialogTurnResult(DialogTurnStatus.Waiting)

        return await step_context.next(step_context.result)
Пример #4
0
    async def __call_generate_answer(self, step_context: WaterfallStepContext):
        dialog_options: QnAMakerDialogOptions = ObjectPath.get_path_value(
            step_context.active_dialog.state, QnAMakerDialog.KEY_OPTIONS
        )

        # Resetting context and QnAId
        dialog_options.options.qna_id = 0
        dialog_options.options.context = QnARequestContext()

        # Storing the context info
        step_context.values[
            QnAMakerDialog.PROPERTY_CURRENT_QUERY
        ] = step_context.context.activity.text

        # -Check if previous context is present, if yes then put it with the query
        # -Check for id if query is present in reverse index.
        previous_context_data = ObjectPath.get_path_value(
            step_context.active_dialog.state, QnAMakerDialog.KEY_QNA_CONTEXT_DATA, {}
        )
        previous_qna_id = ObjectPath.get_path_value(
            step_context.active_dialog.state, QnAMakerDialog.KEY_PREVIOUS_QNA_ID, 0
        )

        if previous_qna_id > 0:
            dialog_options.options.context = QnARequestContext(
                previous_qna_id=previous_qna_id
            )

            current_qna_id = previous_context_data.get(
                step_context.context.activity.text
            )
            if current_qna_id:
                dialog_options.options.qna_id = current_qna_id

        # Calling QnAMaker to get response.
        qna_client = self._get_qnamaker_client(step_context)
        response = await qna_client.get_answers_raw(
            step_context.context, dialog_options.options
        )

        is_active_learning_enabled = response.active_learning_enabled
        step_context.values[QnAMakerDialog.PROPERTY_QNA_DATA] = response.answers

        # Resetting previous query.
        previous_qna_id = -1
        ObjectPath.set_path_value(
            step_context.active_dialog.state,
            QnAMakerDialog.KEY_PREVIOUS_QNA_ID,
            previous_qna_id,
        )

        # Check if active learning is enabled and send card
        # maximum_score_for_low_score_variation is the score above which no need to check for feedback.
        if (
            response.answers
            and response.answers[0].score <= self.maximum_score_for_low_score_variation
        ):
            # Get filtered list of the response that support low score variation criteria.
            response.answers = qna_client.get_low_score_variation(response.answers)
            if len(response.answers) > 1 and is_active_learning_enabled:
                suggested_questions = [qna.questions[0] for qna in response.answers]
                message = QnACardBuilder.get_suggestions_card(
                    suggested_questions,
                    dialog_options.response_options.active_learning_card_title,
                    dialog_options.response_options.card_no_match_text,
                )
                await step_context.context.send_activity(message)

                ObjectPath.set_path_value(
                    step_context.active_dialog.state,
                    QnAMakerDialog.KEY_OPTIONS,
                    dialog_options,
                )

                await qna_client.close()

                return DialogTurnResult(DialogTurnStatus.Waiting)

        # If card is not shown, move to next step with top qna response.
        result = [response.answers[0]] if response.answers else []
        step_context.values[QnAMakerDialog.PROPERTY_QNA_DATA] = result
        ObjectPath.set_path_value(
            step_context.active_dialog.state, QnAMakerDialog.KEY_OPTIONS, dialog_options
        )

        await qna_client.close()

        return await step_context.next(result)
Пример #5
0
    async def test_set_value(self):
        test = {}
        ObjectPath.set_path_value(test, "x.y.z", 15)
        ObjectPath.set_path_value(test, "x.p", "hello")
        ObjectPath.set_path_value(test, "foo", {"Bar": 15, "Blat": "yo"})
        ObjectPath.set_path_value(test, "x.a[1]", "yabba")
        ObjectPath.set_path_value(test, "x.a[0]", "dabba")
        ObjectPath.set_path_value(test, "null", None)

        assert ObjectPath.get_path_value(test, "x.y.z") == 15
        assert ObjectPath.get_path_value(test, "x.p") == "hello"
        assert ObjectPath.get_path_value(test, "foo.bar") == 15

        assert not ObjectPath.try_get_path_value(test, "foo.Blatxxx")
        assert ObjectPath.try_get_path_value(test, "x.a[1]") == "yabba"
        assert ObjectPath.try_get_path_value(test, "x.a[0]") == "dabba"

        assert not ObjectPath.try_get_path_value(test, "null")