问题描述
我正在尝试通过 Python 脚本使用 CLI 中的 Watson Assistant,类似于演示 Building a custom client。 Python 脚本是:
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()
我认为连接正常,但请求失败,状态码为 422:
c:\Runnable>python create_watson_assistant_service_object.py
Method Failed with status code 422: UnkNown error
我尝试在请求中的assistant_id 参数之后传递一个输入参数:
input = {'message_type': 'text','text': 'Hello'}
这给出了相同的结果(代码 422)。
我不知道接下来该尝试什么。
解决方法
不清楚 assistant_id
被设置为什么。 my_assistant_id
未在您的代码段中的任何地方初始化。
assistant_id = my_assistant_id
您也没有提供任何意见。来自 API 文档 - 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))