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

Commit 1df03c4

Browse files
committed
docs: add detect intent with intent input snippet
WIP
1 parent dcd8319 commit 1df03c4

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 with specified intent."""
18+
19+
20+
# [START dialogflow_cx_v3_detect_intent_with_intent_input_async]
21+
import uuid
22+
23+
from google.cloud.dialogflowcx_v3.services.intents import IntentsClient
24+
from google.cloud.dialogflowcx_v3.services.sessions import SessionsClient
25+
from google.cloud.dialogflowcx_v3.types import session
26+
27+
28+
def run_sample():
29+
# TODO(developer): Update these values when running the function
30+
project_id = "YOUR-PROJECT-ID"
31+
location = "YOUR-LOCATION-ID"
32+
agent_id = "YOUR-AGENT-ID"
33+
intent_id = "YOUR-INTENT-ID"
34+
language_code = "en-us"
35+
36+
detect_intent_with_intent_input(
37+
project_id,
38+
location,
39+
agent_id,
40+
intent_id,
41+
language_code,
42+
)
43+
44+
45+
def detect_intent_with_intent_input(
46+
project_id,
47+
location,
48+
agent_id,
49+
intent_id,
50+
language_code,
51+
):
52+
"""Returns the result of detect intent with sentiment analysis"""
53+
54+
pass
55+
56+
client_options = None
57+
if location != "global":
58+
api_endpoint = f"{location}-dialogflow.googleapis.com:443"
59+
print(f"API Endpoint: {api_endpoint}\n")
60+
client_options = {"api_endpoint": api_endpoint}
61+
session_client = SessionsClient(client_options=client_options)
62+
session_id = str(uuid.uuid4())
63+
intents_client = IntentsClient()
64+
65+
session_path = session_client.session_path(
66+
project=project_id,
67+
location=location,
68+
agent=agent_id,
69+
session=session_id,
70+
)
71+
intent_path = intents_client.intent_path(
72+
project=project_id,
73+
location=location,
74+
agent=agent_id,
75+
intent=intent_id,
76+
)
77+
78+
intent = session.IntentInput(intent=intent_path)
79+
query_input = session.QueryInput(
80+
intent=intent,
81+
language_code=language_code
82+
)
83+
request = session.DetectIntentRequest(
84+
session=session_path,
85+
query_input=query_input,
86+
)
87+
88+
response = session_client.detect_intent(request=request)
89+
response_text = []
90+
for response_message in response.query_result.response_messages:
91+
response_text.append(response_message.text.text)
92+
print(response_message.text.text)
93+
return response_text
94+
# [END dialogflow_cx_v3_detect_intent_with_intent_input_async]
95+
96+
97+
if __name__ == "__main__":
98+
run_sample()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
PROJECT_ID = 'dialogflow-cx-demo-1-348717' # "YOUR-PROJECT-ID"
29+
AGENT_ID = '8caa6b47-5dd7-4380-b86e-ea4301d565b0' #"YOUR-AGENT-ID"
30+
31+
32+
# @pytest.mark.parametrize(
33+
# "text, expected_score_min, expected_score_max",
34+
# (['Perfect', .5, 1], ['I am not happy', -1, -.5])
35+
# )
36+
# def test_detect_intent_positive(text, expected_score_min, expected_score_max):
37+
38+
# score = detect_intent_with_sentiment_analysis(
39+
# PROJECT_ID,
40+
# 'global',
41+
# AGENT_ID,
42+
# text,
43+
# "en-us",
44+
# )
45+
# assert expected_score_min < score < expected_score_max

0 commit comments

Comments
 (0)