Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Watson Assistant API V2 "manages the context automatically" - more details?

This is a question about Watson Assistant API V1/V2 difference. The doc says like this:

Note that if your app uses the v1 API, it communicates directly with the dialog skill, bypassing the orchestration and state-management capabilities of the assistant. This means that your application is responsible for maintaining state information. This is done using the context, an object that is passed back and forth between your application and the Watson Assistant service. Your application must maintain the context by saving the context received with each response and sending it back to the service with each new message request. An application using the v2 API can also use the context to access and store persistent information, but the context is maintained automatically (on a per-session basis) by the assistant.

It seems that in V2, "the context is maintained automatically by the assistant". What does this mean exactly ? If I'd like to pass some data to the dialog flow, I might use context on "/message". Is is allowed in V2?( yes, it seems.) Then in V1 days, I have to receive context from responses and send it back on every request. Does assistant also send the context back in V2? What should my client-app do in V2? Any detailed info is welcomed ..Thanks.

like image 899
ishida330 Avatar asked Nov 21 '18 01:11

ishida330


People also ask

How do you set the context in IBM Watson assistant?

Defining a context variableClick to open the dialog node to which you want to add a context variable. to see the menu that is associated with the response. Click Open context editor. Add the variable name and value pair to the Variable and Value fields.


1 Answers

Answering your second question first - If you check the API docs for Watson Assistant V2 here, there is a MessageContext object in the response with Global Context and skill specific context values.

enter image description here

You also have a sample Request where you can manually pass the context (both global and user-defined)

curl -u "apikey:{apikey}" -X POST -H "Content-Type:application/json" -d "{\"input\": {\"text\": \"Hello\"}, \"context\": {\"global\": {\"system\": {\"user_id\": \"my_user_id\"}}}}" "https://gateway.watsonplatform.net/conversation/api/v2/assistants/{assistant_id}/sessions/{session_id}/message?version=2018-11-08"

From the client side, you can use this code

service.message({
            assistant_id: '{assistant_id}',
            session_id: '{session_id}',
            input: {
                'message_type': 'text',
                'text': 'Hello'
            },
            context: {
                'global': {
                    'system': {
                        'user_id': 'my_user_id'
                    }
                },
                "skills": {
                    "main skill": {
                        "user_defined": {
                            "my_result_variable": "result_value"
                        }
                    }
                }
            }
        }

Reference link

Check the API Methods Summary to understand what is supported in V2 as of today.

There is a new concept in V2 called Session. A session is used to send user input to a skill and receive responses. It also maintains the state of the conversation which is Context automatically for you.

V2 API as of today supports Runtime Methods, methods that enable a client application to interact with (but not modify) an existing assistant or skill. You can use these methods to develop a user-facing client that can be deployed for production use, an application that brokers communication between an assistant and another service (such as a chat service or back-end system), or a testing application.

The complete cURL example that works for me

    curl -u "apikey:<API_KEY>" -X POST -H "Content-Type:application/json" -d "{      
                \"input\": {
                        \"text\": \"What's the time?\",
           \"options\": {
             \"alternate_intents\": true,
              \"debug\": true,\"return_context\": true
            }
                },
                \"context\": {
                        \"global\": {
                                \"system\": {


 \"user_id\": \"derp\",\"turn_count\":1
                        }
                }
        },
        \"skills\": {
                \"main_skill\":{\"user_defined\": {
                        \"chosen_service\": \"dental\"
                }}
        }
}"  "https://gateway.watsonplatform.net/assistant/api/v2/assistants/{ASSISTANT_ID}/sessions/{SESSION_ID}/message?version=2018-11-08"

for session ID, run this command

curl -u "apikey:<API_KEY>" -X POST "https://gateway.watsonplatform.net/assistant/api/v2/assistants/{ASSISTANT_ID}/sessions?version=2018-11-08"

The response includes user_defined skill context

{"output":{"generic":[{"response_type":"text","text":"Hey ! how are you today? Let me know if you need any help or get stuck looking for information."}],"debug":{"nodes_visited":[{"dialog_node":"node_6_1475611948267","title":null,"conditions":"conversation_start"}],"log_messages":[],"branch_exited":true,"branch_exited_reason":"completed"},"intents":[{"intent":"General_Greetings","confidence":0.32179955244064334},{"intent":"General_Jokes","confidence":0.296911633014679},{"intent":"goodbye","confidence":0.2852578103542328},{"intent":"General_Ending","confidence":0.2513303637504578},{"intent":"off_topic","confidence":0.24435781836509707},{"intent":"select_detail","confidence":0.24206179082393647},{"intent":"list_options","confidence":0.22829059958457948},{"intent":"still-here","confidence":0.22606439888477325},{"intent":"select_service","confidence":0.22488142400979996},{"intent":"General_Security_Assurance","confidence":0.2210852071642876}],"entities":[]},"context":{"global":{"system":{"turn_count":1,"user_id":"derp"}},"skills":{"main skill":{"user_defined":{"chosen_service":"dental"}}}}}
like image 71
Vidyasagar Machupalli Avatar answered Oct 23 '22 03:10

Vidyasagar Machupalli