Skip to content

Commit 34627fa

Browse files
authored
docs(samples): add Agent Assist code samples (#267)
1 parent 1342e16 commit 34627fa

11 files changed

+1017
-90
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 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+
"""Dialogflow API Python sample showing how to manage AnswerRecord.
17+
"""
18+
19+
from google.cloud import dialogflow_v2beta1 as dialogflow
20+
21+
22+
# [START dialogflow_update_answer_record]
23+
def update_answer_record(project_id, answer_record_id, is_clicked):
24+
"""Update the answer record.
25+
26+
Args:
27+
project_id: The GCP project linked with the conversation profile.
28+
answer_record_id: The answer record id returned along with the
29+
suggestion.
30+
is_clicked: whether the answer record is clicked."""
31+
32+
client = dialogflow.AnswerRecordsClient()
33+
answer_record_path = client.answer_record_path(project_id,
34+
answer_record_id)
35+
36+
response = client.update_answer_record(
37+
answer_record={
38+
'name': answer_record_path,
39+
'answer_feedback': {
40+
'clicked': is_clicked
41+
}
42+
},
43+
update_mask={'paths': ['answer_feedback']})
44+
print('AnswerRecord Name: {}'.format(response.name))
45+
print('Clicked: {}'.format(response.answer_feedback.clicked))
46+
return response
47+
48+
49+
# [END dialogflow_update_answer_record]
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import os
16+
17+
import answer_record_management
18+
import conversation_management
19+
import conversation_profile_management
20+
import participant_management
21+
22+
PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT')
23+
SMART_REPLY_MODEL = os.getenv('SMART_REPLY_MODEL')
24+
SMART_REPLY_ALLOWLIST = os.getenv('SMART_REPLY_ALLOWLIST')
25+
CONVERSATION_PROFILE_DISPLAY_NAME = 'sample code profile for smart reply'
26+
27+
28+
def test_smart_reply(capsys):
29+
"""Test smart reply feature.
30+
"""
31+
32+
# Create conversation profile.
33+
conversation_profile_management.create_conversation_profile_smart_reply(
34+
project_id=PROJECT_ID,
35+
display_name=CONVERSATION_PROFILE_DISPLAY_NAME,
36+
smart_reply_allowlist_name=SMART_REPLY_ALLOWLIST,
37+
smart_reply_model_name=SMART_REPLY_MODEL)
38+
39+
out, _ = capsys.readouterr()
40+
assert 'Display Name: {}'.format(CONVERSATION_PROFILE_DISPLAY_NAME) in out
41+
conversation_profile_id = out.split('conversationProfiles/')[1].rstrip()
42+
43+
# Create conversation.
44+
conversation_management.create_conversation(
45+
project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id)
46+
47+
out, _ = capsys.readouterr()
48+
conversation_id = out.split('conversations/')[1].rstrip()
49+
50+
# Create end user participant.
51+
participant_management.create_participant(project_id=PROJECT_ID,
52+
conversation_id=conversation_id,
53+
role='END_USER')
54+
out, _ = capsys.readouterr()
55+
end_user_id = out.split('participants/')[1].rstrip()
56+
57+
# Create human agent participant.
58+
participant_management.create_participant(project_id=PROJECT_ID,
59+
conversation_id=conversation_id,
60+
role='HUMAN_AGENT')
61+
out, _ = capsys.readouterr()
62+
human_agent_id = out.split('participants/')[1].rstrip()
63+
64+
# AnalyzeContent
65+
participant_management.analyze_content_text(
66+
project_id=PROJECT_ID,
67+
conversation_id=conversation_id,
68+
participant_id=human_agent_id,
69+
text='Hi, how are you?')
70+
out, _ = capsys.readouterr()
71+
assert 'What would you like to know?' in out
72+
73+
response = participant_management.analyze_content_text(
74+
project_id=PROJECT_ID,
75+
conversation_id=conversation_id,
76+
participant_id=end_user_id,
77+
text='I am doing well, just want to check')
78+
out, _ = capsys.readouterr()
79+
assert 'Sounds good.' in out
80+
# Update AnswerRecord.
81+
answer_record_id = response.human_agent_suggestion_results[
82+
0].suggest_smart_replies_response.smart_reply_answers[
83+
0].answer_record.split('answerRecords/')[1].rstrip()
84+
answer_record_management.update_answer_record(
85+
project_id=PROJECT_ID,
86+
answer_record_id=answer_record_id,
87+
is_clicked=True)
88+
out, _ = capsys.readouterr()
89+
assert 'Clicked: True' in out
90+
91+
# Complete conversation.
92+
conversation_management.complete_conversation(
93+
project_id=PROJECT_ID, conversation_id=conversation_id)
94+
95+
# Delete conversation profile.
96+
conversation_profile_management.delete_conversation_profile(
97+
project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id)

dialogflow/conversation_management.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2021 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+
"""Dialogflow API Python sample showing how to manage Conversations.
17+
"""
18+
19+
from google.cloud import dialogflow_v2beta1 as dialogflow
20+
21+
22+
# [START dialogflow_create_conversation]
23+
def create_conversation(project_id, conversation_profile_id):
24+
"""Creates a conversation with given values
25+
26+
Args:
27+
project_id: The GCP project linked with the conversation.
28+
conversation_profile_id: The conversation profile id used to create
29+
conversation."""
30+
31+
client = dialogflow.ConversationsClient()
32+
conversation_profile_client = dialogflow.ConversationProfilesClient()
33+
project_path = client.common_project_path(project_id)
34+
conversation_profile_path = (
35+
conversation_profile_client.conversation_profile_path(
36+
project_id, conversation_profile_id))
37+
conversation = {'conversation_profile': conversation_profile_path}
38+
response = client.create_conversation(parent=project_path,
39+
conversation=conversation)
40+
41+
print('Life Cycle State: {}'.format(response.lifecycle_state))
42+
print('Conversation Profile Name: {}'.format(
43+
response.conversation_profile))
44+
print('Name: {}'.format(response.name))
45+
return response
46+
47+
48+
# [END dialogflow_create_conversation]
49+
50+
51+
# [START dialogflow_get_conversation]
52+
def get_conversation(project_id, conversation_id):
53+
"""Gets a specific conversation profile.
54+
55+
Args:
56+
project_id: The GCP project linked with the conversation.
57+
conversation_id: Id of the conversation."""
58+
59+
client = dialogflow.ConversationsClient()
60+
conversation_path = client.conversation_path(project_id, conversation_id)
61+
62+
response = client.get_conversation(name=conversation_path)
63+
64+
print('Life Cycle State: {}'.format(response.lifecycle_state))
65+
print('Conversation Profile Name: {}'.format(
66+
response.conversation_profile))
67+
print('Name: {}'.format(response.name))
68+
return response
69+
70+
71+
# [END dialogflow_get_conversation]
72+
73+
74+
# [START dialogflow_complete_conversation]
75+
def complete_conversation(project_id, conversation_id):
76+
"""Completes the specified conversation. Finished conversations are purged from the database after 30 days.
77+
78+
Args:
79+
project_id: The GCP project linked with the conversation.
80+
conversation_id: Id of the conversation."""
81+
82+
client = dialogflow.ConversationsClient()
83+
conversation_path = client.conversation_path(project_id, conversation_id)
84+
conversation = client.complete_conversation(name=conversation_path)
85+
print('Completed Conversation.')
86+
print('Life Cycle State: {}'.format(conversation.lifecycle_state))
87+
print('Conversation Profile Name: {}'.format(
88+
conversation.conversation_profile))
89+
print('Name: {}'.format(conversation.name))
90+
return conversation
91+
92+
93+
# [END dialogflow_complete_conversation]

0 commit comments

Comments
 (0)