Skip to content

Commit

Permalink
docs(samples): add Agent Assist code samples (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogers140 authored Mar 25, 2021
1 parent 1342e16 commit 34627fa
Show file tree
Hide file tree
Showing 11 changed files with 1,017 additions and 90 deletions.
49 changes: 49 additions & 0 deletions dialogflow/answer_record_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Dialogflow API Python sample showing how to manage AnswerRecord.
"""

from google.cloud import dialogflow_v2beta1 as dialogflow


# [START dialogflow_update_answer_record]
def update_answer_record(project_id, answer_record_id, is_clicked):
"""Update the answer record.
Args:
project_id: The GCP project linked with the conversation profile.
answer_record_id: The answer record id returned along with the
suggestion.
is_clicked: whether the answer record is clicked."""

client = dialogflow.AnswerRecordsClient()
answer_record_path = client.answer_record_path(project_id,
answer_record_id)

response = client.update_answer_record(
answer_record={
'name': answer_record_path,
'answer_feedback': {
'clicked': is_clicked
}
},
update_mask={'paths': ['answer_feedback']})
print('AnswerRecord Name: {}'.format(response.name))
print('Clicked: {}'.format(response.answer_feedback.clicked))
return response


# [END dialogflow_update_answer_record]
97 changes: 97 additions & 0 deletions dialogflow/answer_record_management_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import os

import answer_record_management
import conversation_management
import conversation_profile_management
import participant_management

PROJECT_ID = os.getenv('GOOGLE_CLOUD_PROJECT')
SMART_REPLY_MODEL = os.getenv('SMART_REPLY_MODEL')
SMART_REPLY_ALLOWLIST = os.getenv('SMART_REPLY_ALLOWLIST')
CONVERSATION_PROFILE_DISPLAY_NAME = 'sample code profile for smart reply'


def test_smart_reply(capsys):
"""Test smart reply feature.
"""

# Create conversation profile.
conversation_profile_management.create_conversation_profile_smart_reply(
project_id=PROJECT_ID,
display_name=CONVERSATION_PROFILE_DISPLAY_NAME,
smart_reply_allowlist_name=SMART_REPLY_ALLOWLIST,
smart_reply_model_name=SMART_REPLY_MODEL)

out, _ = capsys.readouterr()
assert 'Display Name: {}'.format(CONVERSATION_PROFILE_DISPLAY_NAME) in out
conversation_profile_id = out.split('conversationProfiles/')[1].rstrip()

# Create conversation.
conversation_management.create_conversation(
project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id)

out, _ = capsys.readouterr()
conversation_id = out.split('conversations/')[1].rstrip()

# Create end user participant.
participant_management.create_participant(project_id=PROJECT_ID,
conversation_id=conversation_id,
role='END_USER')
out, _ = capsys.readouterr()
end_user_id = out.split('participants/')[1].rstrip()

# Create human agent participant.
participant_management.create_participant(project_id=PROJECT_ID,
conversation_id=conversation_id,
role='HUMAN_AGENT')
out, _ = capsys.readouterr()
human_agent_id = out.split('participants/')[1].rstrip()

# AnalyzeContent
participant_management.analyze_content_text(
project_id=PROJECT_ID,
conversation_id=conversation_id,
participant_id=human_agent_id,
text='Hi, how are you?')
out, _ = capsys.readouterr()
assert 'What would you like to know?' in out

response = participant_management.analyze_content_text(
project_id=PROJECT_ID,
conversation_id=conversation_id,
participant_id=end_user_id,
text='I am doing well, just want to check')
out, _ = capsys.readouterr()
assert 'Sounds good.' in out
# Update AnswerRecord.
answer_record_id = response.human_agent_suggestion_results[
0].suggest_smart_replies_response.smart_reply_answers[
0].answer_record.split('answerRecords/')[1].rstrip()
answer_record_management.update_answer_record(
project_id=PROJECT_ID,
answer_record_id=answer_record_id,
is_clicked=True)
out, _ = capsys.readouterr()
assert 'Clicked: True' in out

# Complete conversation.
conversation_management.complete_conversation(
project_id=PROJECT_ID, conversation_id=conversation_id)

# Delete conversation profile.
conversation_profile_management.delete_conversation_profile(
project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id)
93 changes: 93 additions & 0 deletions dialogflow/conversation_management.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env python

# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Dialogflow API Python sample showing how to manage Conversations.
"""

from google.cloud import dialogflow_v2beta1 as dialogflow


# [START dialogflow_create_conversation]
def create_conversation(project_id, conversation_profile_id):
"""Creates a conversation with given values
Args:
project_id: The GCP project linked with the conversation.
conversation_profile_id: The conversation profile id used to create
conversation."""

client = dialogflow.ConversationsClient()
conversation_profile_client = dialogflow.ConversationProfilesClient()
project_path = client.common_project_path(project_id)
conversation_profile_path = (
conversation_profile_client.conversation_profile_path(
project_id, conversation_profile_id))
conversation = {'conversation_profile': conversation_profile_path}
response = client.create_conversation(parent=project_path,
conversation=conversation)

print('Life Cycle State: {}'.format(response.lifecycle_state))
print('Conversation Profile Name: {}'.format(
response.conversation_profile))
print('Name: {}'.format(response.name))
return response


# [END dialogflow_create_conversation]


# [START dialogflow_get_conversation]
def get_conversation(project_id, conversation_id):
"""Gets a specific conversation profile.
Args:
project_id: The GCP project linked with the conversation.
conversation_id: Id of the conversation."""

client = dialogflow.ConversationsClient()
conversation_path = client.conversation_path(project_id, conversation_id)

response = client.get_conversation(name=conversation_path)

print('Life Cycle State: {}'.format(response.lifecycle_state))
print('Conversation Profile Name: {}'.format(
response.conversation_profile))
print('Name: {}'.format(response.name))
return response


# [END dialogflow_get_conversation]


# [START dialogflow_complete_conversation]
def complete_conversation(project_id, conversation_id):
"""Completes the specified conversation. Finished conversations are purged from the database after 30 days.
Args:
project_id: The GCP project linked with the conversation.
conversation_id: Id of the conversation."""

client = dialogflow.ConversationsClient()
conversation_path = client.conversation_path(project_id, conversation_id)
conversation = client.complete_conversation(name=conversation_path)
print('Completed Conversation.')
print('Life Cycle State: {}'.format(conversation.lifecycle_state))
print('Conversation Profile Name: {}'.format(
conversation.conversation_profile))
print('Name: {}'.format(conversation.name))
return conversation


# [END dialogflow_complete_conversation]
Loading

0 comments on commit 34627fa

Please sign in to comment.