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

2

6

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.

Sturmabteilung answered 21/11, 2018 at 1:52 Comment(0)
V
4

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"}}}}}
Vocation answered 21/11, 2018 at 5:56 Comment(4)
Thank you, Vidyasasar. Now I found that maybe I should use "skills" entry of the context to handle the user-defined variables, which is change from V1, but one step beyond. Then I've checked the API v2 Reference but there's no detailed layout rules below "skills" and no practical samples. Does "main skill" part must match the exact skill name or anything will do? "user_defined" is the fixed keyword?...etc) and if I'd like to use the value of "my_result_variable" in dialog, how should I express it ? ( ex. $(my_result_variable) $(main skill.user_defined.my_result_variable)??) Thanks.Sturmabteilung
As per the V2 API documentation, user_defined is arbitrary variables that can be read and written to by a particular skill within the Assistant. For now, it's not mandatory to use the main skill. For context variable part you may have to wait for the complete documentation. For request body sample, click on the View link below the Request body in the docs.Vocation
@Sturmabteilung Updated my answer with complete cURL request and response with Context bodyVocation
Also see #53435043Chickaree
S
1

Thanks, @Vidyasagar Machupalli @data_henrik. (I created "answer" section to paste the images below .)

1) Now it worked fine. I set the variable reference in dialog editor like this. enter image description here

Then I post user-variable in context like this.

context={
    "skills": {
        "main skill": {
            "user_defined": {
                "myname": "ishida"
            }
        }
    }
}

then the response was:

{'output': {'generic': [{'response_type': 'text', 'text': 'Hi, ishida .'}], 'intents': [], 'entities': []}}

It seems that "skills"->"main skill"->"user_defined" section is a case-sensitive / FIXED keyword. From what I see, if I change one of them, I cannot read the variable on dialog editor.

2) I also found the documentation issue. The starting point of my question was : "API V2 doc says that I should use "skills" entry to handle the user variables, but no detailed info more than that .. ". @Vidyasagar Machupalli says "As per the V2 API documentation, user_defined is arbitrary variables ".. but I cannot find the paragraph in the API doc. Then I found that a) when I select "curl" tab, "user_defined" explanation appears. b) when I select other(Java/Node/Python) tab, no explanation appears.

enter image description here

I knew that the info was not there as I refered the "Python" tab. I'd appreciate this be fixed soon to avoid same confusion. And I think that the fact of "skills->main skill->user_defined section is a case-sensitive / FIXED keyword" should be noted, as it is not clear in the current description. Thanks!

Sturmabteilung answered 26/11, 2018 at 3:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.