gpt4 book ai didi

python - 如何直接在Dialogflow上获取文本响应?

转载 作者:行者123 更新时间:2023-11-30 22:25:01 25 4
gpt4 key购买 nike

我正在尝试从对话流获取直接文本响应。我确实从 github 上的示例代码中得到了答案,但这根本不方便用户使用。如何获得“仅限语音”响应?

import os.path
import sys

try:
import apiai
except ImportError:
sys.path.append(
os.path.join(os.path.dirname(os.path.realpath(__file__)), os.pardir)
)
import apiai

CLIENT_ACCESS_TOKEN = 'YOUR_ACCESS_TOKEN'


def main():
ai = apiai.ApiAI(CLIENT_ACCESS_TOKEN)

request = ai.text_request()

request.lang = 'de' # optional, default value equal 'en'

request.session_id = "<SESSION ID, UNIQUE FOR EACH USER>"

request.query = "Hello"

response = request.getresponse()

print (response.read())


if __name__ == '__main__':
main()

我只期望一个简单的结果。一个简单的问候文本。

我得到的是= b'{\n "id": "306fd06a-d9e6-4c2e-8c05-98ff7fc0ecd5",\n "timestamp": "2017-12-05T22:18:15.563Z",\n "lang": "en",\n "结果": {\n "source": "agent",\n "resolvedQuery": "hi",\n "action": "input.welcome",\n“actionIncomplete”: false,\n“参数”:{},\n“上下文”:[],\n“元数据”:{\n“intentId”:“8406ea3a-a0c9-4470-829f-aba0ce2da2e5” ,\n "webhookUsed": "false",\n "webhookForSlotFillingUsed": "false",\n "intentName": "默认欢迎意图"\n },\n "fulfillment": {\n "speech": "你好!",\n "messages": [\n {\n "type": 0,\n "speech": "你好!"\n }\n ]\n },\n "score": 1.0\n },\n "alternateResult": {\n "source": "domains",\n "resolvedQuery": "hi",\n "action": "smalltalk.greetings.hello",\n "actionIncomplete ": false,\n "参数": {},\n "上下文": [],\n "元数据": {},\n "实现": {\n "语音": "嘿!",\n "source": "agent"\n },\n "score": 1.0\n },\n "status": {\n "code": 200,\n "errorType": "success",\n "webhookTimedOut": false\n },\n "sessionId": "mr.9000"\n}'

最佳答案

只需尝试这样获取消息:

response = json.loads(request.getresponse().read().decode('utf-8'))
message = response['result']['fulfillment']['speech']
print (message)

别忘了添加

import json

在开头。如果您尚未安装,请安装它。如果你想在 python 中处理 json,你就必须这样做。告诉我是否有效

关于python - 如何直接在Dialogflow上获取文本响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47663656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com