|
| 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) |
0 commit comments