Skip to content

Commit

Permalink
docs(samples): adds training phrases sample (#404)
Browse files Browse the repository at this point in the history
* docs(samples): adds training phrases sample

* docs(samples): fixed lint

* docs(samples): ignored auto-foratting

Co-authored-by: Anthonios Partheniou <partheniou@google.com>
  • Loading branch information
2 people authored and telpirion committed Mar 13, 2023
1 parent 57df72a commit ac8deb6
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 374 deletions.
69 changes: 22 additions & 47 deletions dialogflow/README.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

.. This file is automatically generated. Do not edit this file directly.
Dialogflow Enterprise Edition API Python Samples
Expand All @@ -16,11 +15,13 @@ This directory contains samples for Dialogflow Enterprise Edition API. The `Dia
.. _Dialogflow Enterprise Edition API: https://cloud.google.com/dialogflow-enterprise/docs/





Setup
-------------------------------------------------------------------------------



Authentication
++++++++++++++

Expand All @@ -31,9 +32,6 @@ credentials for applications.
.. _Authentication Getting Started Guide:
https://cloud.google.com/docs/authentication/getting-started




Install Dependencies
++++++++++++++++++++

Expand Down Expand Up @@ -64,15 +62,9 @@ Install Dependencies
.. _pip: https://pip.pypa.io/
.. _virtualenv: https://virtualenv.pypa.io/






Samples
-------------------------------------------------------------------------------


Detect Intent Text
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -88,7 +80,6 @@ To run this sample:
$ python detect_intent_texts.py
usage: detect_intent_texts.py [-h] --project-id PROJECT_ID
[--session-id SESSION_ID]
[--language-code LANGUAGE_CODE]
Expand Down Expand Up @@ -116,8 +107,6 @@ To run this sample:
Detect Intent Audio
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -133,7 +122,6 @@ To run this sample:
$ python detect_intent_audio.py
usage: detect_intent_audio.py [-h] --project-id PROJECT_ID
[--session-id SESSION_ID]
[--language-code LANGUAGE_CODE]
Expand Down Expand Up @@ -161,8 +149,6 @@ To run this sample:
Detect Intent Stream
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -178,7 +164,6 @@ To run this sample:
$ python detect_intent_stream.py
usage: detect_intent_stream.py [-h] --project-id PROJECT_ID
[--session-id SESSION_ID]
[--language-code LANGUAGE_CODE]
Expand Down Expand Up @@ -206,8 +191,6 @@ To run this sample:
Detect Intent Knowledge Base
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -223,7 +206,6 @@ To run this sample:
$ python detect_intent_knowledge.py
usage: detect_intent_knowledge.py [-h] --project-id PROJECT_ID
[--session-id SESSION_ID]
[--language-code LANGUAGE_CODE]
Expand Down Expand Up @@ -253,8 +235,6 @@ To run this sample:
Detect Intent with Sentiment Analysis
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -270,7 +250,6 @@ To run this sample:
$ python detect_intent_with_sentiment_analysis.py
usage: detect_intent_with_sentiment_analysis.py [-h] --project-id PROJECT_ID
[--session-id SESSION_ID]
[--language-code LANGUAGE_CODE]
Expand All @@ -297,8 +276,6 @@ To run this sample:
Detect Intent with Text to Speech Response
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -314,7 +291,6 @@ To run this sample:
$ python detect_intent_with_texttospeech_response.py
usage: detect_intent_with_texttospeech_response.py [-h] --project-id
PROJECT_ID
[--session-id SESSION_ID]
Expand Down Expand Up @@ -342,8 +318,6 @@ To run this sample:
Intent Management
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -359,7 +333,6 @@ To run this sample:
$ python intent_management.py
usage: intent_management.py [-h] --project-id PROJECT_ID
{list,create,delete} ...
Expand All @@ -385,8 +358,6 @@ To run this sample:
Knowledge Base Management
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -402,18 +373,24 @@ To run this sample:
$ python knowledge_base_management.py
usage: knowledge_base_management.py [-h] --project-id PROJECT_ID {create} ...
usage: knowledge_base_management.py [-h] --project-id PROJECT_ID
{list,create,get,delete} ...
Dialogflow API Python sample showing how to manage Knowledge bases.
Examples:
python knowledge_base_management.py -h
python knowledge_base_management.py --project-id PROJECT_ID list
python knowledge_base_management.py --project-id PROJECT_ID create --display-name DISPLAY_NAME
python knowledge_base_management.py --project-id PROJECT_ID get --knowledge-base-id knowledge_base_id
python knowledge_base_management.py --project-id PROJECT_ID delete --knowledge-base-id knowledge_base_id
positional arguments:
{create}
{list,create,get,delete}
list List all Knowledge bases that belong to the project.
create Create a new Knowledge base.
get Get a Knowledge base by its id.
delete Delete a Knowledge base by its id.
optional arguments:
-h, --help show this help message and exit
Expand All @@ -422,8 +399,6 @@ To run this sample:
Document Management
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Expand All @@ -439,21 +414,26 @@ To run this sample:
$ python document_management.py
usage: document_management.py [-h] --project-id PROJECT_ID --knowledge-base-id
KNOWLEDGE_BASE_ID
{create} ...
{list,create,get,delete} ...
Dialogflow API Python sample showing how to manage Knowledge Documents.
Examples:
python document_management.py -h
python document_management.py --project-id PROJECT_ID --knowledge-base-id knowledge_base_id list
python document_management.py --project-id PROJECT_ID --knowledge-base-id knowledge_base_id create --display-name DISPLAY_NAME --mime-type MIME_TYPE --knowledge-type KNOWLEDGE_TYPE --content-uri CONTENT_URI
python document_management.py --project-id PROJECT_ID --knowledge-base-id knowledge_base_id
python document_management.py --project-id PROJECT_ID --knowledge-base-id knowledge_base_id get --document-id DOCUMENT_ID
python document_management.py --project-id PROJECT_ID --knowledge-base-id knowledge_base_id delete --document-id DOCUMENT_ID
positional arguments:
{create}
{list,create,get,delete}
list List all Documents that belong to a certain Knowledge
base.
create Create a Document for a certain Knowledge base.
get Get a Document by its id and the Knowledge base id.
delete Delete a Document by its id and the Knowledge baseid.
optional arguments:
-h, --help show this help message and exit
Expand All @@ -467,10 +447,6 @@ To run this sample:
The client library
-------------------------------------------------------------------------------

Expand All @@ -486,5 +462,4 @@ to `browse the source`_ and `report issues`_.
https://github.com/GoogleCloudPlatform/google-cloud-python/issues



.. _Google Cloud SDK: https://cloud.google.com/sdk/
.. _Google Cloud SDK: https://cloud.google.com/sdk/
16 changes: 7 additions & 9 deletions dialogflow/answer_record_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,17 @@ def update_answer_record(project_id, answer_record_id, is_clicked):
is_clicked: whether the answer record is clicked."""

client = dialogflow.AnswerRecordsClient()
answer_record_path = client.answer_record_path(project_id,
answer_record_id)
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
}
"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))
update_mask={"paths": ["answer_feedback"]},
)
print("AnswerRecord Name: {}".format(response.name))
print("Clicked: {}".format(response.answer_feedback.clicked))
return response


Expand Down
68 changes: 38 additions & 30 deletions dialogflow/answer_record_management_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
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'
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):
Expand All @@ -34,64 +34,72 @@ def test_smart_reply(capsys):
project_id=PROJECT_ID,
display_name=CONVERSATION_PROFILE_DISPLAY_NAME,
smart_reply_allowlist_name=SMART_REPLY_ALLOWLIST,
smart_reply_model_name=SMART_REPLY_MODEL)
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()
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)
project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id
)

out, _ = capsys.readouterr()
conversation_id = out.split('conversations/')[1].rstrip()
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')
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()
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')
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()
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?')
text="Hi, how are you?",
)
out, _ = capsys.readouterr()
assert 'What would you like to know?' in out
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')
text="I am doing well, just want to check",
)
out, _ = capsys.readouterr()
assert 'Sounds good.' in out
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_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)
project_id=PROJECT_ID, answer_record_id=answer_record_id, is_clicked=True
)
out, _ = capsys.readouterr()
assert 'Clicked: True' in out
assert "Clicked: True" in out

# Complete conversation.
conversation_management.complete_conversation(
project_id=PROJECT_ID, conversation_id=conversation_id)
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)
project_id=PROJECT_ID, conversation_profile_id=conversation_profile_id
)
Loading

0 comments on commit ac8deb6

Please sign in to comment.