【问题标题】:Passing the answer of Watson Assistant to a variable Python将 Watson Assistant 的答案传递给变量 Python
【发布时间】:2018-12-14 08:58:55
【问题描述】:

我正在尝试将 Watson Assistant 的输出转换为变量。因此,据我搜索,我需要获取 json 的“输出”和“文本”部分(起初它是一个 dict,但后来我们将其解析为 json)。但我似乎无法理解:

这2个问题我已经搜索过了:This one for watsonThis one for parsing the json

代码非常简单:访问我的机器人,然后输入“trips”。我已经取出了 api 和工作区,但我有它们(显然)。

if __name__ == '__main__':
    assistant = watson_developer_cloud.AssistantV1(

        iam_apikey='{YOUR API HERE}',
        version='2018-09-20',
        url='https://gateway-syd.watsonplatform.net/assistant/api'
    )

    response = assistant.message(
        workspace_id='{YOUR WORKSPACE HERE}',
        input={
            'text': 'trips'
        }
    ).get_result()
    fullResponse=json.dumps(response, indent=2)
    print(fullResponse)
    print("testing to print the output: ")
    respuesta=json.dumps(response, indent=2)
    #print(respuesta['output'][0]['text'])
    print(respuesta['output']['text'])

然后输出:

Traceback (most recent call last):
  "intents": [
  File "C:/Users/.PyCharmCE2018.3/config/scratches/pruebaMain.py", line 105, in <module>
    {
    print(respuesta['output']['text'])
      "intent": "trips",
TypeError: string indices must be integers
      "confidence": 1
    }
  ],
  "entities": [],
  "input": {
    "text": "trips"
  },
  "output": {
    "generic": [
      {
        "response_type": "text",
        "text": "We got trips to different countries! Type continents to know more!"
      }
    ],
    "text": [
      "We got trips to different countries! Type continents to know more!"
    ],
    "nodes_visited": [
      "node_2_1544696932582"
    ],
    "log_messages": []
  },
  "context": {
    "conversation_id": "{took it out for privacy}",
    "system": {
      "initialized": true,
      "dialog_stack": [
        {
          "dialog_node": "root"
        }
      ],
      "dialog_turn_counter": 1,
      "dialog_request_counter": 1,
      "_node_output_map": {
        "node_2_1544696932582": {
          "0": [
            0
          ]
        }
      },
      "branch_exited": true,
      "branch_exited_reason": "completed"
    }
  }
}
testing to print the output: 

Process finished with exit code 1

所以我想得到“我们去不同国家旅行!输入大陆以了解更多信息!”的答案。我已经阅读了 python API 的文档和更多信息(https://github.com/IBM-Cloud/watson-conversation-variables),但似乎找不到任何东西。我还尝试使用$ 访问 json 变量,但没有成功。

【问题讨论】:

    标签: python ibm-cloud watson-conversation


    【解决方案1】:

    这里不用json.dumps,直接使用服务返回的响应JSON如下面代码sn-p所示

    import watson_developer_cloud
    
    if __name__ == '__main__':
        assistant = watson_developer_cloud.AssistantV1(
    
            iam_apikey='APIKEY',
            version='2018-09-20',
            url='https://gateway.watsonplatform.net/assistant/api'
        )
    
        response = assistant.message(
            workspace_id='WORKSPACE_ID',
            input={
                'text': 'trips'
            }
        ).get_result()
    
        print(response)
        print(response['output']['text'][0])
    

    【讨论】:

    • 谢谢!所以 json.dumps 将字典“加载”到一个 json 中,所以我有一个字符串。我对您的回答很满意,但是使用 dumps 怎么样。可以做什么?
    • 是的,json.dumps 返回一个字符串。响应对象已经是一个字典,并且在 Python 中遍历字典非常容易。加上这段代码,你就会明白对象的类型print(type(response)) jsondumps = json.dumps(response) print(type(jsondumps))
    猜你喜欢
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2021-04-16
    相关资源
    最近更新 更多