从Python中的后续意图获取Dialogflow上下文参数 [英] Get Dialogflow context parameters from a follow up intent in Python

查看:99
本文介绍了从Python中的后续意图获取Dialogflow上下文参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中创建了一个flask应用程序,用于将文本发送到Dialogflow,以便检测意图并从中返回实现文本作为响应.

I created a flask app in python to send text to Dialogflow in order to detect the intent and return the fulfillment text as response from it.

然后,我将其扩展为还沿着履行文本从查询结果中返回某个参数,该参数也可以正常工作,我得到的像这样:

Then I extended this to also return along the fulfillment text a certain parameter from the query result which also works and I get it like this:

 orderid = (response.query_result.parameters.fields["ordernumber"])
        orderid_json = json.loads(MessageToJson(orderid))

现在,当我添加跟踪意图时,我的问题就开始了,因为由于参数是上下文的一部分,因此我无法读出该参数.

Now my problems started when I added a follow up intent because then I cannot read out the parameter since it's part of the context.

针对我的意图 order.status 的RAW API响应如下:

The RAW API response for the my intent order.status looks like this:

{
  "responseId": "7ae77c38-501a-4079-b0e0-24807669c826-83ffff32",
  "queryResult": {
    "queryText": "check my order 456789",
    "action": "order.status",
    "parameters": {
      "ordernumber": "456789"
    },
    "allRequiredParamsPresent": true,
    "fulfillmentText": "Uh-oh, your order seems to be delayed and the exptected delivery date is: 29.06.2020Would you like to talk with an agent about this delay?",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "Uh-oh, your order seems to be delayed and the exptected delivery date is: 29.06.2020Would you like to talk with an agent about this delay?"
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/voicebotdemo-hdnmue/agent/sessions/5ea2f271-1e00-5041-5f23-f05c73bd1a95/contexts/orderstatus-followup",
        "lifespanCount": 2,
        "parameters": {
          "ordernumber.original": "456789",
          "ordernumber": "456789"
        }
      },
      {
        "name": "projects/voicebotdemo-hdnmue/agent/sessions/5ea2f271-1e00-5041-5f23-f05c73bd1a95/contexts/ordernumber",
        "lifespanCount": 5,
        "parameters": {
          "ordernumber": "456789",
          "ordernumber.original": "456789"
        }
      }
    ],
    "intent": {
      "name": "projects/voicebotdemo-hdnmue/agent/intents/fcb8a584-5ca1-41c7-b67e-34a35dd53602",
      "displayName": "order.status"
    },
    "intentDetectionConfidence": 0.8599001,
    "diagnosticInfo": {
      "webhook_latency_ms": 156
    },
    "languageCode": "en",
    "sentimentAnalysisResult": {
      "queryTextSentiment": {
        "score": -0.3,
        "magnitude": 0.3
      }
    }
  },
  "webhookStatus": {
    "message": "Webhook execution successful"
  }
}

以及对后续意图 order.status-yes 的响应,如下所示:

and the response for the follow-up intent order.status-yes like this:

    {
  "responseId": "8c63e43d-ff01-430b-becf-be8f8fa40839-83ffff32",
  "queryResult": {
    "queryText": "why not",
    "action": "orderstatus.orderstatus-yes",
    "parameters": {},
    "allRequiredParamsPresent": true,
    "fulfillmentText": "Routing you to an agent now!",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "Routing you to an agent now!"
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/voicebotdemo-hdnmue/agent/sessions/5ea2f271-1e00-5041-5f23-f05c73bd1a95/contexts/ordernumber",
        "lifespanCount": 5,
        "parameters": {
          "ordernumber": "456789",
          "ordernumber.original": "456789"
        }
      },
      {
        "name": "projects/voicebotdemo-hdnmue/agent/sessions/5ea2f271-1e00-5041-5f23-f05c73bd1a95/contexts/orderstatus-followup",
        "lifespanCount": 1,
        "parameters": {
          "ordernumber": "456789",
          "ordernumber.original": "456789"
        }
      }
    ],
    "intent": {
      "name": "projects/voicebotdemo-hdnmue/agent/intents/1a3a01da-cdf4-4dc6-b0d6-4c28c9a0bca7",
      "displayName": "order.status - yes"
    },
    "intentDetectionConfidence": 1,
    "languageCode": "en",
    "sentimentAnalysisResult": {
      "queryTextSentiment": {
        "score": 0.3,
        "magnitude": 0.3
      }
    }
  }
}

对后续意图的响应不包含任何参数. 因此,我的问题是如何从"outputContexts" 而不是"queryResult" 读出 ordernumber 参数?

The response for the follow-up intent does not contain any parameters. So, my question is how can I read out the ordernumber parameter from "outputContexts" instead of "queryResult" ?

这是我的完整剧本:

import json
import os
import dialogflow_v2

from flask import Flask
from flask import request
from google.api_core.exceptions import InvalidArgument
from google.protobuf.json_format import MessageToJson


proxy = 'http://10.7.111.18:8080'

os.environ['http_proxy'] = proxy
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy


os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'private_key_easteregg.json'

DIALOGFLOW_PROJECT_ID = 'voicebotdemo-hdnmue'
DIALOGFLOW_LANGUAGE_CODE = 'en'
SESSION_ID = 'me'


app = Flask(__name__)


@app.route('/call/user_input_flask', methods=['GET', 'POST', 'PUT', 'DELETE'])
def user_input_flask():
    #req_data = request.get_json(dict)
    input_text_from_web = request.form.get('input', '')
    session_client = dialogflow_v2.SessionsClient()
    session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)
    text_input = dialogflow_v2.types.TextInput(text=input_text_from_web, language_code=DIALOGFLOW_LANGUAGE_CODE)
    query_input = dialogflow_v2.types.QueryInput(text=text_input)
    query_parameter = dialogflow_v2.types.QueryParameters()
    try:
        response = session_client.detect_intent(session=session, query_input=query_input)
        orderid = (response.query_result.parameters.fields["ordernumber"])
        orderid_json = json.loads(MessageToJson(orderid))
    except InvalidArgument:
        raise
    return {
        "response": response.query_result.fulfillment_text,
        "ordernumber": orderid_json
    }


app.run(host='0.0.0.0', port=5002, debug=True)


谢谢!

推荐答案

我可以使用get()函数读取参数ordernumber,如下所示:

I thing you could read the parameter ordernumber with get() function, something like this:

outCnt = query_result.get('outputContexts') 
outCnt = outCnt[2] 
# Second element of 'OutputContexts' in query_result (a Multidict) contains parameter 'ordernumber'
ordernumber = outCnt.get('parameters').get('ordernumber')

这篇关于从Python中的后续意图获取Dialogflow上下文参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆