Skip to content
This repository was archived by the owner on Jan 6, 2024. It is now read-only.

Commit dcd8319

Browse files
authored
docs: add detect intent with sentiment analysis snippet (#416)
1 parent 57a0e16 commit dcd8319

File tree

3 files changed

+137
-4
lines changed

3 files changed

+137
-4
lines changed

samples/snippets/detect_intent_audio.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
import argparse
2626
import uuid
2727

28-
from google.cloud.dialogflowcx_v3beta1.services.agents import AgentsClient
29-
from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient
30-
from google.cloud.dialogflowcx_v3beta1.types import audio_config
31-
from google.cloud.dialogflowcx_v3beta1.types import session
28+
from google.cloud.dialogflowcx_v3.services.agents import AgentsClient
29+
from google.cloud.dialogflowcx_v3.services.sessions import SessionsClient
30+
from google.cloud.dialogflowcx_v3.types import audio_config
31+
from google.cloud.dialogflowcx_v3.types import session
3232

3333

3434
# [START dialogflow_detect_intent_audio]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2022 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
"""DialogFlow Detect Intent Python sample, text input and sentiment analysis."""
18+
19+
20+
# [START dialogflow_cx_v3_detect_intent_sentiment_analysis_async]
21+
import uuid
22+
23+
from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient
24+
from google.cloud.dialogflowcx_v3beta1.types import session
25+
26+
27+
def run_sample():
28+
# TODO(developer): Update these values when running the function
29+
project_id = "YOUR-PROJECT-ID"
30+
location = "YOUR-LOCATION-ID"
31+
agent_id = "YOUR-AGENT-ID"
32+
text = "Perfect!"
33+
language_code = "en-us"
34+
35+
detect_intent_with_sentiment_analysis(
36+
project_id,
37+
location,
38+
agent_id,
39+
text,
40+
language_code,
41+
)
42+
43+
44+
def detect_intent_with_sentiment_analysis(
45+
project_id,
46+
location,
47+
agent_id,
48+
text,
49+
language_code,
50+
):
51+
"""Returns the result of detect intent with sentiment analysis"""
52+
53+
client_options = None
54+
if location != "global":
55+
api_endpoint = f"{location}-dialogflow.googleapis.com:443"
56+
print(f"API Endpoint: {api_endpoint}\n")
57+
client_options = {"api_endpoint": api_endpoint}
58+
session_client = SessionsClient(client_options=client_options)
59+
session_id = str(uuid.uuid4())
60+
61+
session_path = session_client.session_path(
62+
project=project_id,
63+
location=location,
64+
agent=agent_id,
65+
session=session_id,
66+
)
67+
68+
text_input = session.TextInput(text=text)
69+
query_input = session.QueryInput(
70+
text=text_input,
71+
language_code=language_code
72+
)
73+
query_params = session.QueryParameters(
74+
analyze_query_text_sentiment=True,
75+
)
76+
request = session.DetectIntentRequest(
77+
session=session_path,
78+
query_input=query_input,
79+
query_params=query_params,
80+
)
81+
82+
response = session_client.detect_intent(request=request)
83+
score = response.query_result.sentiment_analysis_result.score
84+
print('Sentiment Score: {score}')
85+
return score
86+
87+
# [END dialogflow_cx_v3_detect_intent_sentiment_analysis_async]
88+
89+
90+
if __name__ == "__main__":
91+
run_sample()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Copyright 2022, Google LLC
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
"""Tests for detect_intent_with_sentiment_analysis.py"""
15+
16+
from __future__ import absolute_import
17+
18+
import os
19+
20+
import pytest
21+
22+
from detect_intent_with_sentiment_analysis import detect_intent_with_sentiment_analysis
23+
24+
25+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")
26+
AGENT_ID = os.getenv("AGENT_ID")
27+
28+
29+
@pytest.mark.parametrize(
30+
"text, expected_score_min, expected_score_max",
31+
(['Perfect', .5, 1], ['I am not happy', -1, -.5])
32+
)
33+
def test_detect_intent_positive(text, expected_score_min, expected_score_max):
34+
35+
score = detect_intent_with_sentiment_analysis(
36+
PROJECT_ID,
37+
'global',
38+
AGENT_ID,
39+
text,
40+
"en-us",
41+
)
42+
assert expected_score_min < score < expected_score_max

0 commit comments

Comments
 (0)