watson-developer-cloud / python-sdk

:snake: Client library to use the IBM Watson services in Python and available in pip as watson-developer-cloud
https://pypi.org/project/ibm-watson/
Apache License 2.0
1.45k stars 828 forks source link

How to update data register value in Dialog node response using Watson Assistant v1 API using python #603

Closed yashpatel001 closed 5 years ago

yashpatel001 commented 5 years ago

Expected behavior

{
  "output": {
    "text": {
      "values": [
        "$(data)good morning"
      ],
      "selection_policy": "sequential"
    },
    "txcom": 1
  }
}

Actual behavior

Steps to reproduce the problem

Code snippet (Note: Do not paste your credentials)

import json

import watson_developer_cloud

# Set up Assistant service.
service = watson_developer_cloud.AssistantV1(
  iam_apikey = {API KEY}, # replace with API key
  version = '2018-09-20'
)
workspace_id = {Workspace id} # replace with workspace ID

user_input = ''
context = {}

# Main input/output loop
while True:

  # Send message to Assistant service.
  response = service.message(
    workspace_id = workspace_id,
    input = {
      'text': user_input
    },
    context = context
  ).get_result()

  # If an intent was detected, print it to the console.
  if response['intents']:
    print('Detected intent: #' + response['intents'][0]['intent'])

  # Print the output from dialog, if any. Assumes a single text response.
  if response['output']['generic']:
    print(response['output']['generic'][0]['text'])

  # Update the stored context with the latest received from the dialog.
  context = response['context']

  # Prompt for next round of input.
  user_input =str(raw_input('>> '))

python sdk version

python version

yashpatel001 commented 5 years ago

How to update dynamically register value available in dialog node response .

germanattanasio commented 5 years ago

@yashpatel001 what version of the SDK are you using?

yashpatel001 commented 5 years ago

pip install --upgrade "watson-developer-cloud>=2.4.0"

ehdsouza commented 5 years ago

@yashpatel001, I am not fully sure I understand your question.

I believe you created an issue in the java sdk as well, did you find the answer in the example folder?

yashpatel001 commented 5 years ago

@ehdsouza for java i found but i didnt found for python.

i want to update register value in dialognode json response dynamically.

please help for python.

yashpatel001 commented 5 years ago

@ehdsouza

input text  = "hii"
{
  "output": {
    "text": {
      "values": [
        "$(data)good morning"
      ],
      "selection_policy": "sequential"
    },
    "txcom": 1
  }
}

through code if i will give input text ("hii") + data register value ("yash")

Response should come $(data)good morning = yash good morning

ehdsouza commented 5 years ago

@yashpatel001 could you show the java code, I could give you the equivalent python

yashpatel001 commented 5 years ago
HashMap<String,String> abc = new HashMap();
abc.put("data","How are you");

Context co = new Context();
co.put("data","How are You");

newmessageoptions = new newmessageoptions.builder()
             .workspaceid("")
             .input("First message")
             .context(context)
             .build()
messageResponse response = assistant.message(newmessageoptions).execute();
context = response.getContext();

newmessageoptions = new newmessageoptions.builder()
             .workspaceid("")
             .input("Second message")
             .context(co)  // passing data register value how are you
             .build()
yashpatel001 commented 5 years ago

@ehdsouza please provide for python same

yashpatel001 commented 5 years ago

@ehdsouza i shared sample code now give python code

germanattanasio commented 5 years ago

@yashpatel001 She is on vacation until next week. I will try to take a look but I don't have time this week :(

ehdsouza commented 5 years ago

@yashpatel001 here is the equivalent code:

response = assistant.message(
    workspace_id='',
    input={
        'text': 'First message'
    },
    context={
        'data': 'How are you'
    }
).get_result()
print(json.dumps(response, indent=2))

context = response['context']

response = assistant.message(
    workspace_id='',
    input={
        'text': 'Second message'
    },
    context=context
).get_result()
print(json.dumps(response, indent=2))

For code help, I would advice you to use: https://stackoverflow.com/questions/tagged/ibm-watson. We open issues here if there are any problems in the SDK.

yashpatel001 commented 5 years ago

@ehdsouza Thank you so much , it is working fine