def test_detect_intent_field_headers(): client = SessionsClient(credentials=credentials.AnonymousCredentials(), ) # Any value that is part of the HTTP/1.1 URI should be sent as # a field header. Set these to a non-empty value. request = session.DetectIntentRequest() request.session = "session/value" # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.detect_intent), "__call__") as call: call.return_value = session.DetectIntentResponse() client.detect_intent(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] assert args[0] == request # Establish that the field header was sent. _, _, kw = call.mock_calls[0] assert ( "x-goog-request-params", "session=session/value", ) in kw["metadata"]
def detect_intent_texts(agent, session_id, text, language_code): """Returns the result of detect intent with texts as inputs. Using the same `session_id` between requests allows continuation of the conversation.""" session_path = f"{agent}/sessions/{session_id}" print(f"Session path: {session_path}\n") client_options = None agent_components = AgentsClient.parse_agent_path(agent) location_id = agent_components["location"] if location_id != "global": api_endpoint = f"{location_id}-dialogflow.googleapis.com:443" print(f"API Endpoint: {api_endpoint}\n") client_options = {"api_endpoint": api_endpoint} session_client = SessionsClient(client_options=client_options) text_input = session.TextInput(text=text) query_input = session.QueryInput(text=text_input, language_code=language_code) request = session.DetectIntentRequest(session=session_path, query_input=query_input) response = session_client.detect_intent(request=request) print("=" * 20) print(f"Query text: {response.query_result.text}") response_messages = [ " ".join(msg.text.text) for msg in response.query_result.response_messages ] print(f"Response text: {' '.join(response_messages)}\n") return response
def detect_intent_audio(agent, session_id, audio_file_path, language_code): """Returns the result of detect intent with an audio file as input. Using the same `session_id` between requests allows continuation of the conversation.""" session_client = SessionsClient() session_path = f"{agent}/sessions/{session_id}" print(f"Session path: {session_path}\n") input_audio_config = audio_config.InputAudioConfig( audio_encoding=audio_config.AudioEncoding.AUDIO_ENCODING_LINEAR_16, sample_rate_hertz=24000, ) with open(audio_file_path, "rb") as audio_file: input_audio = audio_file.read() audio_input = session.AudioInput(config=input_audio_config, audio=input_audio) query_input = session.QueryInput(audio=audio_input, language_code=language_code) request = session.DetectIntentRequest(session=session_path, query_input=query_input) response = session_client.detect_intent(request=request) print("=" * 20) print(f"Query text: {response.query_result.transcript}") response_messages = [ " ".join(msg.text.text) for msg in response.query_result.response_messages ] print(f"Response text: {' '.join(response_messages)}\n")
def discord_response(user, message_contents): """Returns the result of detect intent with texts as inputs. Using the same `session_id` between requests allows continuation of the conversation.""" session_path = f"{agent}/sessions/{session_id}" #print(f"Session path: {session_path}\n") client_options = None agent_components = AgentsClient.parse_agent_path(agent) location_id = agent_components["location"] if location_id != "global": api_endpoint = f"{location_id}-dialogflow.googleapis.com:443" print(f"API Endpoint: {api_endpoint}\n") client_options = {"api_endpoint": api_endpoint} session_client = SessionsClient(client_options=client_options) input_string = message_contents image_float = analyze_sentiment(user, input_string) text_input = session.TextInput(text=input_string) query_input = session.QueryInput(text=text_input, language_code=language_code) request = session.DetectIntentRequest( session=session_path, query_input=query_input ) response = session_client.detect_intent(request=request) #print("=" * 20) #print(f"Query text: {response.query_result.text}") response_messages = [ " ".join(msg.text.text) for msg in response.query_result.response_messages ] return ((os.getcwd() + find_emotion_gif(image_float)), f"{' '.join(response_messages)}\n")
def test_detect_intent(transport: str = "grpc", request_type=session.DetectIntentRequest): client = SessionsClient( credentials=credentials.AnonymousCredentials(), transport=transport, ) # Everything is optional in proto3 as far as the runtime is concerned, # and we are mocking out the actual API, so just send an empty request. request = request_type() # Mock the actual call within the gRPC stub, and fake the request. with mock.patch.object(type(client._transport.detect_intent), "__call__") as call: # Designate an appropriate return value for the call. call.return_value = session.DetectIntentResponse( response_id="response_id_value", output_audio=b"output_audio_blob", ) response = client.detect_intent(request) # Establish that the underlying gRPC stub method was called. assert len(call.mock_calls) == 1 _, args, _ = call.mock_calls[0] assert args[0] == session.DetectIntentRequest() # Establish that the response is the type that we expect. assert isinstance(response, session.DetectIntentResponse) assert response.response_id == "response_id_value" assert response.output_audio == b"output_audio_blob"
def detect_intent(user): #, agent, session_id, language_code """Returns the result of detect intent with texts as inputs. Using the same `session_id` between requests allows continuation of the conversation.""" session_path = f"{agent}/sessions/{session_id}" print(f"Session path: {session_path}\n") client_options = None agent_components = AgentsClient.parse_agent_path(agent) location_id = agent_components["location"] if location_id != "global": api_endpoint = f"{location_id}-dialogflow.googleapis.com:443" print(f"API Endpoint: {api_endpoint}\n") client_options = {"api_endpoint": api_endpoint} session_client = SessionsClient(client_options=client_options) input_string = input("Enter your prompt for bitberg") while(input_string != 'close'): image_float = analyze_sentiment(user, input_string) text_input = session.TextInput(text=input_string) query_input = session.QueryInput(text=text_input, language_code=language_code) request = session.DetectIntentRequest( session=session_path, query_input=query_input ) response = session_client.detect_intent(request=request) #display image somehow # # ### display_gif(find_emotion_gif(image_float)) # # if -1.0 < image_float < -0.5: __display_gif('angry') #display angry elif image_float < 0.0: __display_gif('sad') #display sad elif image_float < 0.5: __display_gif('bored') #display bored else: __display_gif('happy') #display happy print("=" * 20) print(f"Query text: {response.query_result.text}") response_messages = [ " ".join(msg.text.text) for msg in response.query_result.response_messages ] print(f"Response text: {' '.join(response_messages)}\n") input_string = input()
def detect_intent_audio(agent, session_id, audio_file_path, language_code): """Returns the result of detect intent with an audio file as input. Using the same `session_id` between requests allows continuation of the conversation.""" session_path = f"{agent}/sessions/{session_id}" print(f"Session path: {session_path}\n") client_options = None agent_components = AgentsClient.parse_agent_path(agent) location_id = agent_components["location"] if location_id != "global": api_endpoint = f"{location_id}-dialogflow.googleapis.com:443" print(f"API Endpoint: {api_endpoint}\n") client_options = {"api_endpoint": api_endpoint} session_client = SessionsClient(client_options=client_options) input_audio_config = audio_config.InputAudioConfig( audio_encoding=audio_config.AudioEncoding.AUDIO_ENCODING_LINEAR_16, sample_rate_hertz=24000, ) with open(audio_file_path, "rb") as audio_file: input_audio = audio_file.read() audio_input = session.AudioInput(config=input_audio_config, audio=input_audio) query_input = session.QueryInput(audio=audio_input, language_code=language_code) request = session.DetectIntentRequest(session=session_path, query_input=query_input) response = session_client.detect_intent(request=request) print("=" * 20) print(f"Query text: {response.query_result.transcript}") response_messages = [ " ".join(msg.text.text) for msg in response.query_result.response_messages ] print(f"Response text: {' '.join(response_messages)}\n")
def detect_intent_text(agent, session_id, text, language_code): """Returns the result of detect intent with texts as inputs. Using the same `session_id` between requests allows continuation of the conversation.""" session_path = f"{agent}/sessions/{session_id}" client_options = None agent_components = AgentsClient.parse_agent_path(agent) location_id = agent_components["location"] if location_id != "global": api_endpoint = f"{location_id}-dialogflow.googleapis.com:443" client_options = {"api_endpoint": api_endpoint} session_client = SessionsClient(client_options=client_options) text_input = session.TextInput(text=text) query_input = session.QueryInput(text=text_input, language_code=language_code) my_request = session.DetectIntentRequest( session=session_path, query_input=query_input ) response = session_client.detect_intent(request=my_request) serializable_messages = [proto.Message.to_dict(message) for message in \ response.query_result.response_messages] return json.dumps({'messages':serializable_messages})
def detect_intent_texts(agent, session_id, texts, language_code): """Returns the result of detect intent with texts as inputs. Using the same `session_id` between requests allows continuation of the conversation.""" session_client = SessionsClient() session_path = f"{agent}/sessions/{session_id}" print(f"Session path: {session_path}\n") for text in texts: text_input = session.TextInput(text=text) query_input = session.QueryInput(text=text_input, language_code=language_code) request = session.DetectIntentRequest(session=session_path, query_input=query_input) response = session_client.detect_intent(request=request) print("=" * 20) print(f"Query text: {response.query_result.text}") response_messages = [ " ".join(msg.text.text) for msg in response.query_result.response_messages ] print(f"Response text: {' '.join(response_messages)}\n")