コード例 #1
0
    def test_detect_intent(self):
        # Setup Expected Response
        response_id = "responseId1847552473"
        output_audio = b"24"
        expected_response = {
            "response_id": response_id,
            "output_audio": output_audio
        }
        expected_response = session_pb2.DetectIntentResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2.SessionsClient()

        # Setup Request
        session = "session1984987798"
        query_input = {}

        response = client.detect_intent(session, query_input)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = session_pb2.DetectIntentRequest(
            session=session, query_input=query_input)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
コード例 #2
0
    def test_detect_intent(self):
        # Setup Expected Response
        response_id = 'responseId1847552473'
        output_audio = b'24'
        expected_response = {
            'response_id': response_id,
            'output_audio': output_audio
        }
        expected_response = session_pb2.DetectIntentResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch('google.api_core.grpc_helpers.create_channel')
        with patch as create_channel:
            create_channel.return_value = channel
            client = dialogflow_v2.SessionsClient()

        # Setup Request
        session = client.session_path('[PROJECT]', '[SESSION]')
        query_input = {}

        response = client.detect_intent(session, query_input)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = session_pb2.DetectIntentRequest(
            session=session, query_input=query_input)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
コード例 #3
0
    def test_detect_intent(self):
        # Setup Expected Response
        response_id = 'responseId1847552473'
        expected_response = {'response_id': response_id}
        expected_response = session_pb2.DetectIntentResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = dialogflow_v2.SessionsClient(channel=channel)

        # Setup Request
        session = client.session_path('[PROJECT]', '[SESSION]')
        query_input = {}

        response = client.detect_intent(session, query_input)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = session_pb2.DetectIntentRequest(
            session=session, query_input=query_input)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
コード例 #4
0
    def detect_intent(self,
                      session,
                      query_input,
                      query_params=None,
                      output_audio_config=None,
                      input_audio=None,
                      retry=google.api_core.gapic_v1.method.DEFAULT,
                      timeout=google.api_core.gapic_v1.method.DEFAULT,
                      metadata=None):
        """
        Processes a natural language query and returns structured, actionable data
        as a result. This method is not idempotent, because it may cause contexts
        and session entity types to be updated, which in turn might affect
        results of future queries.

        Example:
            >>> import dialogflow_v2
            >>>
            >>> client = dialogflow_v2.SessionsClient()
            >>>
            >>> session = client.session_path('[PROJECT]', '[SESSION]')
            >>>
            >>> # TODO: Initialize `query_input`:
            >>> query_input = {}
            >>>
            >>> response = client.detect_intent(session, query_input)

        Args:
            session (str): Required. The name of the session this query is sent to. Format:
                ``projects/<Project ID>/agent/sessions/<Session ID>``. It's up to the
                API caller to choose an appropriate session ID. It can be a random
                number or some type of user identifier (preferably hashed). The length
                of the session ID must not exceed 36 bytes.
            query_input (Union[dict, ~google.cloud.dialogflow_v2.types.QueryInput]): Required. The input specification. It can be set to:

                1.  an audio config
                    which instructs the speech recognizer how to process the speech audio,

                2.  a conversational query in the form of text, or

                3.  an event that specifies which intent to trigger.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.dialogflow_v2.types.QueryInput`
            query_params (Union[dict, ~google.cloud.dialogflow_v2.types.QueryParameters]): Optional. The parameters of this query.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.dialogflow_v2.types.QueryParameters`
            output_audio_config (Union[dict, ~google.cloud.dialogflow_v2.types.OutputAudioConfig]): Optional. Instructs the speech synthesizer how to generate the output
                audio. If this field is not set and agent-level speech synthesizer is not
                configured, no output audio is generated.

                If a dict is provided, it must be of the same form as the protobuf
                message :class:`~google.cloud.dialogflow_v2.types.OutputAudioConfig`
            input_audio (bytes): Optional. The natural language speech audio to be processed. This field
                should be populated iff ``query_input`` is set to an input audio config.
                A single request can contain up to 1 minute of speech audio data.
            retry (Optional[google.api_core.retry.Retry]):  A retry object used
                to retry requests. If ``None`` is specified, requests will not
                be retried.
            timeout (Optional[float]): The amount of time, in seconds, to wait
                for the request to complete. Note that if ``retry`` is
                specified, the timeout applies to each individual attempt.
            metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
                that is provided to the method.

        Returns:
            A :class:`~google.cloud.dialogflow_v2.types.DetectIntentResponse` instance.

        Raises:
            google.api_core.exceptions.GoogleAPICallError: If the request
                    failed for any reason.
            google.api_core.exceptions.RetryError: If the request failed due
                    to a retryable error and retry attempts failed.
            ValueError: If the parameters are invalid.
        """
        # Wrap the transport method to add retry and timeout logic.
        if 'detect_intent' not in self._inner_api_calls:
            self._inner_api_calls[
                'detect_intent'] = google.api_core.gapic_v1.method.wrap_method(
                    self.transport.detect_intent,
                    default_retry=self._method_configs['DetectIntent'].retry,
                    default_timeout=self._method_configs['DetectIntent'].
                    timeout,
                    client_info=self._client_info,
                )

        request = session_pb2.DetectIntentRequest(
            session=session,
            query_input=query_input,
            query_params=query_params,
            output_audio_config=output_audio_config,
            input_audio=input_audio,
        )
        if metadata is None:
            metadata = []
        metadata = list(metadata)
        try:
            routing_header = [('session', session)]
        except AttributeError:
            pass
        else:
            routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
                routing_header)
            metadata.append(routing_metadata)

        return self._inner_api_calls['detect_intent'](request,
                                                      retry=retry,
                                                      timeout=timeout,
                                                      metadata=metadata)