1

I'm trying to use Watson Assistant from the CLI with a Python script, similar to the demo Building a custom client. The Python script is:

from ibm_watson import AssistantV2
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

# Create Assistant service object.
authenticator = IAMAuthenticator(api_key_for_wa_service) # replace with API key
assistant = AssistantV2(
    version = '2020-09-24',
    authenticator = authenticator
)
assistant.set_service_url('https://api.au-syd.assistant.watson.cloud.ibm.com')
assistant_id = '00965b15-eb3f-4d83-8983-3df0c7da9c4f'

# Start conversation with empty message:
response = assistant.message_stateless(assistant_id, 
                                        ).get_result()

I think it's connecting okay, but the request is failing with status code 422:

c:\Runnable>python create_watson_assistant_service_object.py
Method failed with status code 422: Unknown error

I've tried passing an input argument after the assistant_id argument in the request:

input = {'message_type': 'text', 'text': 'Hello'}

This gives the same result (code 422).

I don't know what to try next.

AltShift
  • 336
  • 3
  • 18
  • Could add the full error message with the call stack? Could you add error handling as documented here? https://cloud.ibm.com/apidocs/assistant/assistant-v2?code=python#error-handling – data_henrik Jul 03 '21 at 11:31
  • The new output (from new exception handling code) is now shown. – AltShift Jul 05 '21 at 19:55
  • Can you confirm that it is exactly the code above (I don't see any exception handling)? Is only that single line of error message printed? Your API key is the current one for Watson Assistant? You Assistant ID is the one matching the one shown for your assistant? Your SDK is up to date? – data_henrik Jul 06 '21 at 05:15
  • @data_henrik I changed the `create_session` call as follows: `response = assistant.create_session(assistant_id = assistant_id).get_result()` and followed it with `print(json.dumps(response, indent=2))`. Now it responds with the session_id, so that's progress. However, the response does not include the "output" field (or any other). This is not working as shown in the documentation. Is there something about the V2 Python API that has changed? – AltShift Jul 25 '21 at 03:45
  • Is there no output if the assistant is created on a Lite plan? – AltShift Jul 25 '21 at 03:45

1 Answers1

1

It is not clear, what assistant_id is being set to. my_assistant_id isn't initialised anywhere in your code snippet.

assistant_id = my_assistant_id

You also are not providing any input. From the API documentation - https://cloud.ibm.com/apidocs/assistant/assistant-v2?code=python#messagestateless

import json
from ibm_watson import AssistantV2
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator('{apikey}')
assistant = AssistantV2(
    version='2020-04-01',
    authenticator = authenticator
)

assistant.set_service_url('{url}')

response = assistant.message_stateless(
    assistant_id='{assistant_id}',
    input={
        'message_type': 'text',
        'text': 'Hello'
    }
).get_result()

print(json.dumps(response, indent=2))
chughts
  • 4,210
  • 2
  • 14
  • 27
  • Thanks. I was using `my_assistant_id` as a dummy, in the example code. Actually, it is set to '00965b15-eb3f-4d83-8983-3df0c7da9c4f' before the request. Looking at your 2nd comment now. – AltShift Jul 05 '21 at 19:35
  • Okay, so I was already basing my code on that reference. I used the same snippet as you showed, and got the same result: 422. – AltShift Jul 05 '21 at 20:04