Help us understand the problem. What is going on with this article?

IBM Cloud Watson Conversation APIのPython SDKで過去の会話を反映したレスポンスのリクエスト

More than 3 years have passed since last update.

パッケージのリポジトリ
サンプルコード中では、過去のやり取りを用いるすべを教えてくれていないのでメモ

通常のリクエスト

response = conversation.message(workspace_id=workspace_id, input={'text': 'I\'m hungry'})
print(json.dumps(response, indent=2))

{
  "intents": [
    {
      "intent": "greetings",
      "confidence": 1
    }
  ],
  "entities": [],
  "input": {
    "text": "hey"
  },
  "output": {
    "text": [
      "Hi. It looks like a nice drive today. What would you like me to do?  "
    ],
    "nodes_visited": [
      "Start And Initialize Context"
    ],
    "log_messages": []
  },
  "context": {
    "conversation_id": "706d33fc-dc6c-48a9-bbf1-ec404aca893f",
    "system": {
      "dialog_stack": [
        {
          "dialog_node": "root"
        }
      ],
      "dialog_turn_counter": 1,
      "dialog_request_counter": 1,
      "_node_output_map": {
        "Start And Initialize Context": [
          0,
          0
        ]
      },
      "branch_exited": true,
      "branch_exited_reason": "completed"
    },
    "AConoff": "off",
    "lightonoff": "off",
    "musiconoff": "off",
    "appl_action": "",
    "heateronoff": "off",
    "volumeonoff": "off",
    "wipersonoff": "off",
    "default_counter": 0
  }
}

だと前までの会話を汲み取ってくれないです。

サンプルだと最初の挨拶をしてくるだけで、一向に会話が進まないという状態になります。

組み込み関数helpmessageメソッドのドキュメントを確認してみます。

print(help(conversation.message))

Help on method message in module watson_developer_cloud.conversation_v1:

message(workspace_id, input=None, alternate_intents=None, context=None, entities=None, intents=None, output=None) method of watson_developer_cloud.conversation_v1.ConversationV1 instance
    Get a response to a user's input.

    :param str workspace_id: Unique identifier of the workspace.
    :param InputData input: An input object that includes the input text.
    :param bool alternate_intents: Whether to return more than one intent. Set to `true` to return all matching intents.
    :param Context context: State information for the conversation. Continue a conversation by including the context object from the previous response.
    :param list[RuntimeEntity] entities: Include the entities from the previous response when they do not need to change and to prevent Watson from trying to identify them.
    :param list[RuntimeIntent] intents: An array of name-confidence pairs for the user input. Include the intents from the previous response when they do not need to change and to prevent Watson from trying to identify them.
    :param OutputData output: System output. Include the output from the request when you have several requests within the same Dialog turn to pass back in the intermediate information.
    :return: A `dict` containing the `MessageResponse` response.
    :rtype: dict

ちゃんと引数の説明を書いてくれています。
contextresponse'context'渡せばできそう。

過去の会話を参照したリクエスト

response = conversation.message(
    workspace_id=workspace_id,
    input={'text': 'I\'m hungry'},
    context=reponse['context']
)
print(json.dumps(response, indent=2))

{
  "intents": [
    {
      "intent": "locate amenity",
      "confidence": 1
    }
  ],
  "entities": [],
  "input": {
    "text": "hey"
  },
  "output": {
    "text": [
      "Of course. Do you have a specific cuisine in mind?"
    ],
    "nodes_visited": [
      "Start And Initialize Context"
    ],
    "log_messages": []
  },
  "context": {
    "conversation_id": "706d33fc-dc6c-48a9-bbf1-ec404aca893f",
    "system": {
      "dialog_stack": [
        {
          "dialog_node": "root"
        }
      ],
      "dialog_turn_counter": 1,
      "dialog_request_counter": 1,
      "_node_output_map": {
        "Start And Initialize Context": [
          0,
          0
        ]
      },
      "branch_exited": true,
      "branch_exited_reason": "completed"
    },
    "AConoff": "off",
    "lightonoff": "off",
    "musiconoff": "off",
    "appl_action": "",
    "heateronoff": "off",
    "volumeonoff": "off",
    "wipersonoff": "off",
    "default_counter": 0
  }
}

Of course. Do you have a specific cuisine in mind?

と帰ってきたので話が進みました。

increments
「エンジニアを最高に幸せにする」ために Qiita、Qiita Team、Qiita Jobs を開発・運営しています。
https://increments.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away