11import json
22import os
33from dotenv import load_dotenv , find_dotenv
4+ import asyncio
5+
46from watson_developer_cloud import ConversationV1
57from watson_developer_cloud import ToneAnalyzerV3
68
@@ -54,4 +56,20 @@ def invokeToneConversation (payload, maintainToneHistoryInContext):
5456 response = conversation .message (workspace_id = workspace_id , message_input = conversation_payload ['input' ], context = conversation_payload ['context' ])
5557 print (json .dumps (response , indent = 2 ))
5658
57- invokeToneConversation (payload ,maintainToneHistoryInContext );
59+ async def invokeToneConversationAsync (payload , maintainToneHistoryInContext ):
60+ tone = await tone_detection .invokeToneAsync (payload ,tone_analyzer )
61+ conversation_payload = tone_detection .updateUserTone (payload , tone , maintainToneHistoryInContext )
62+ response = conversation .message (workspace_id = workspace_id , message_input = conversation_payload ['input' ], context = conversation_payload ['context' ])
63+ print (json .dumps (response , indent = 2 ))
64+
65+
66+ # how to invoke both versions of the tone aware calls to conversation
67+
68+ # synchronous call to conversation with tone included in the context
69+ invokeToneConversation (payload ,maintainToneHistoryInContext )
70+
71+ # asynchronous call to conversation with tone included in the context
72+ loop = asyncio .get_event_loop ()
73+ loop .run_until_complete (invokeToneConversationAsync (payload ,maintainToneHistoryInContext ))
74+ loop .close ()
75+
0 commit comments