|
16 | 16 | from collections import OrderedDict |
17 | 17 | import functools |
18 | 18 | import re |
19 | | -from typing import Dict, Mapping, Optional, Sequence, Tuple, Type, Union |
| 19 | +from typing import ( |
| 20 | + Dict, |
| 21 | + Mapping, |
| 22 | + Optional, |
| 23 | + AsyncIterable, |
| 24 | + Awaitable, |
| 25 | + AsyncIterator, |
| 26 | + Sequence, |
| 27 | + Tuple, |
| 28 | + Type, |
| 29 | + Union, |
| 30 | +) |
20 | 31 | import pkg_resources |
21 | 32 | import warnings |
22 | 33 |
|
@@ -761,6 +772,161 @@ async def sample_analyze_content(): |
761 | 772 | # Done; return the response. |
762 | 773 | return response |
763 | 774 |
|
| 775 | + def streaming_analyze_content( |
| 776 | + self, |
| 777 | + requests: AsyncIterator[participant.StreamingAnalyzeContentRequest] = None, |
| 778 | + *, |
| 779 | + retry: OptionalRetry = gapic_v1.method.DEFAULT, |
| 780 | + timeout: float = None, |
| 781 | + metadata: Sequence[Tuple[str, str]] = (), |
| 782 | + ) -> Awaitable[AsyncIterable[participant.StreamingAnalyzeContentResponse]]: |
| 783 | + r"""Adds a text (e.g., chat) or audio (e.g., phone recording) |
| 784 | + message from a participant into the conversation. Note: This |
| 785 | + method is only available through the gRPC API (not REST). |
| 786 | +
|
| 787 | + The top-level message sent to the client by the server is |
| 788 | + ``StreamingAnalyzeContentResponse``. Multiple response messages |
| 789 | + can be returned in order. The first one or more messages contain |
| 790 | + the ``recognition_result`` field. Each result represents a more |
| 791 | + complete transcript of what the user said. The next message |
| 792 | + contains the ``reply_text`` field, and potentially the |
| 793 | + ``reply_audio`` and/or the ``automated_agent_reply`` fields. |
| 794 | +
|
| 795 | + Note: Always use agent versions for production traffic sent to |
| 796 | + virtual agents. See `Versions and |
| 797 | + environments <https://cloud.google.com/dialogflow/es/docs/agents-versions>`__. |
| 798 | +
|
| 799 | + .. code-block:: python |
| 800 | +
|
| 801 | + from google.cloud import dialogflow_v2beta1 |
| 802 | +
|
| 803 | + async def sample_streaming_analyze_content(): |
| 804 | + # Create a client |
| 805 | + client = dialogflow_v2beta1.ParticipantsAsyncClient() |
| 806 | +
|
| 807 | + # Initialize request argument(s) |
| 808 | + request = dialogflow_v2beta1.StreamingAnalyzeContentRequest( |
| 809 | + input_audio=b'input_audio_blob', |
| 810 | + participant="participant_value", |
| 811 | + ) |
| 812 | +
|
| 813 | + # This method expects an iterator which contains |
| 814 | + # 'dialogflow_v2beta1.StreamingAnalyzeContentRequest' objects |
| 815 | + # Here we create a generator that yields a single `request` for |
| 816 | + # demonstrative purposes. |
| 817 | + requests = [request] |
| 818 | +
|
| 819 | + def request_generator(): |
| 820 | + for request in requests: |
| 821 | + yield request |
| 822 | +
|
| 823 | + # Make the request |
| 824 | + stream = await client.streaming_analyze_content(requests=request_generator()) |
| 825 | +
|
| 826 | + # Handle the response |
| 827 | + async for response in stream: |
| 828 | + print(response) |
| 829 | +
|
| 830 | + Args: |
| 831 | + requests (AsyncIterator[`google.cloud.dialogflow_v2beta1.types.StreamingAnalyzeContentRequest`]): |
| 832 | + The request object AsyncIterator. The top-level message sent by the |
| 833 | + client to the |
| 834 | + [Participants.StreamingAnalyzeContent][google.cloud.dialogflow.v2beta1.Participants.StreamingAnalyzeContent] |
| 835 | + method. |
| 836 | + Multiple request messages should be sent in order: |
| 837 | +
|
| 838 | + 1. The first message must contain |
| 839 | + [participant][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.participant], |
| 840 | + [config][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.config] |
| 841 | + and optionally |
| 842 | + [query_params][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.query_params]. |
| 843 | + If you want to receive an audio response, it should |
| 844 | + also contain |
| 845 | + [reply_audio_config][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.reply_audio_config]. |
| 846 | + The message must not contain |
| 847 | + [input][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.input]. |
| 848 | + 2. If |
| 849 | + [config][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.config] |
| 850 | + in the first message was set to |
| 851 | + [audio_config][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.audio_config], |
| 852 | + all subsequent messages must contain |
| 853 | + [input_audio][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.input_audio] |
| 854 | + to continue with Speech recognition. |
| 855 | + If you decide to rather analyze text input after you |
| 856 | + already started Speech recognition, please send a |
| 857 | + message with |
| 858 | + [StreamingAnalyzeContentRequest.input_text][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.input_text]. |
| 859 | + However, note that: |
| 860 | +
|
| 861 | + * Dialogflow will bill you for the audio so far. |
| 862 | + * Dialogflow discards all Speech recognition results |
| 863 | + in favor of the text input. |
| 864 | +
|
| 865 | + 3. If |
| 866 | + [StreamingAnalyzeContentRequest.config][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.config] |
| 867 | + in the first message was set to |
| 868 | + [StreamingAnalyzeContentRequest.text_config][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.text_config], |
| 869 | + then the second message must contain only |
| 870 | + [input_text][google.cloud.dialogflow.v2beta1.StreamingAnalyzeContentRequest.input_text]. |
| 871 | + Moreover, you must not send more than two messages. |
| 872 | + After you sent all input, you must half-close or abort |
| 873 | + the request stream. |
| 874 | + retry (google.api_core.retry.Retry): Designation of what errors, if any, |
| 875 | + should be retried. |
| 876 | + timeout (float): The timeout for this request. |
| 877 | + metadata (Sequence[Tuple[str, str]]): Strings which should be |
| 878 | + sent along with the request as metadata. |
| 879 | +
|
| 880 | + Returns: |
| 881 | + AsyncIterable[google.cloud.dialogflow_v2beta1.types.StreamingAnalyzeContentResponse]: |
| 882 | + The top-level message returned from the |
| 883 | + StreamingAnalyzeContent method. |
| 884 | +
|
| 885 | + Multiple response messages can be returned in order: |
| 886 | +
|
| 887 | + 1. If the input was set to streaming audio, the first |
| 888 | + one or more messages contain recognition_result. |
| 889 | + Each recognition_result represents a more complete |
| 890 | + transcript of what the user said. The last |
| 891 | + recognition_result has is_final set to true. |
| 892 | + 2. In virtual agent stage: if |
| 893 | + enable_partial_automated_agent_reply is true, the |
| 894 | + following N (currently 1 <= N <= 4) messages |
| 895 | + contain automated_agent_reply and optionally |
| 896 | + reply_audio returned by the virtual agent. The |
| 897 | + first (N-1) automated_agent_replys will have |
| 898 | + automated_agent_reply_type set to PARTIAL. The |
| 899 | + last automated_agent_reply has |
| 900 | + automated_agent_reply_type set to FINAL. If |
| 901 | + enable_partial_automated_agent_reply is not |
| 902 | + enabled, response stream only contains the final |
| 903 | + reply. |
| 904 | +
|
| 905 | + In human assist stage: the following N (N >= 1) |
| 906 | + messages contain human_agent_suggestion_results, |
| 907 | + end_user_suggestion_results or message. |
| 908 | +
|
| 909 | + """ |
| 910 | + |
| 911 | + # Wrap the RPC method; this adds retry and timeout information, |
| 912 | + # and friendly error handling. |
| 913 | + rpc = gapic_v1.method_async.wrap_method( |
| 914 | + self._client._transport.streaming_analyze_content, |
| 915 | + default_timeout=220.0, |
| 916 | + client_info=DEFAULT_CLIENT_INFO, |
| 917 | + ) |
| 918 | + |
| 919 | + # Send the request. |
| 920 | + response = rpc( |
| 921 | + requests, |
| 922 | + retry=retry, |
| 923 | + timeout=timeout, |
| 924 | + metadata=metadata, |
| 925 | + ) |
| 926 | + |
| 927 | + # Done; return the response. |
| 928 | + return response |
| 929 | + |
764 | 930 | async def suggest_articles( |
765 | 931 | self, |
766 | 932 | request: Union[participant.SuggestArticlesRequest, dict] = None, |
|
0 commit comments