微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

IBM Cloud with Watson Assistant:为 API 调用返回“未知错误,代码:422”

如何解决IBM Cloud with Watson Assistant:为 API 调用返回“未知错误,代码:422”

我正在尝试通过 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))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。