python - Dialogflow 上下文/会话:获得后续响应?
问题描述
我有一个基于网络实现的对话流代理,我正在使用 Dialogflow v2 API 进行查询。但是,目前每个查询的响应都是独立的。我如何收到后续回复(以及适当的流畅对话)?
示例(我想要的): 我:“你好,清理我的包” 机器人:“你确定要清理你的包吗” 我:“是” 机器人:“清理了包”
实际: 我:“你好,清理我的包” 机器人:“你确定要清理你的包” 我:“是” 机器人:“抱歉,不确定你的意思”
Sample code:
"""DialogFlow API Detect Intent Python sample with text inputs.
Examples:
python detect_intent_texts.py -h
python detect_intent_texts.py --project-id PROJECT_ID \
--session-id SESSION_ID \
"hello" "book a meeting room" "Mountain View"
python detect_intent_texts.py --project-id PROJECT_ID \
--session-id SESSION_ID \
"tomorrow" "10 AM" "2 hours" "10 people" "A" "yes"
"""
import argparse
import uuid
import json
# [START dialogflow_detect_intent_text]
def detect_intent_texts(project_id, session_id, texts, language_code):
"""Returns the result of detect intent with texts as inputs.
Using the same `session_id` between requests allows continuation
of the conversaion."""
import dialogflow_v2 as dialogflow
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session))
for text in texts:
text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(session=session, query_input=query_input)
print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
print('Detected intent: {} (confidence: {})\n'.format(response.query_result.intent.display_name,response.query_result.intent_detection_confidence))
print('Fulfillment text: {}\n'.format(response))
#test = str(response)
#test = str(response.query_result.webhook_payload)
#print(test)
# [END dialogflow_detect_intent_text]
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'--project-id',
help='Project/agent id. Required.',
required=True)
parser.add_argument(
'--session-id',
help='Identifier of the DetectIntent session. '
'Defaults to a random UUID.',
default=str(uuid.uuid4()))
parser.add_argument(
'--language-code',
help='Language code of the query. Defaults to "en-US".',
default='en-US')
parser.add_argument(
'texts',
nargs='+',
type=str,
help='Text inputs.')
args = parser.parse_args()
detect_intent_texts(
args.project_id, args.session_id, args.texts, args.language_code)'
解决方案
您需要为是/否设置嵌套意图,然后在您采取相应措施之后。
推荐阅读
- amazon-web-services - 由于配置错误,执行失败:Api Gataway 中路径中的非法字符
- r - R中的更短的顺序表达式
- ios - iOS 应用程序在启动时崩溃并出现 EXC_CORPSE_NOTIFY 异常
- javascript - 获取路径参数并在下拉列表中以 2/4/5 角度显示默认值,并带有翻译(ngx-translate)
- reactjs - Reactjs:将 Jquery 应用程序重写为 Reactjs 以删除内容
- python - 模拟另一个函数使用的python函数
- angular - 如何获取输入值并将其传递给角度组件?
- javascript - 单击地图控件或关闭弹出窗口时,整个页面进行回发,导致地图位置和点丢失
- php - MYSQL 错误:用户 'root'@'localhost' 的访问被拒绝 (1045)
- python - 查找数字^(数字位置)==原始数字即(89 = 8^1 + 9^2)的数字,但代码不会返回高于1306的任何数字?