如何在Dialogflow中传递Google Speech-to-Text的class token
Dialogflow 传递Speech-to-Text Class Token实现方案
以下示例适配对接App Engine Fulfillment的Dialogflow代理场景,直接在Fulfillment返回的响应中携带语音识别配置即可完成class token传递。
核心实现逻辑
Dialogflow会读取Fulfillment响应中speechRecognitionConfig字段下的自定义配置,你可以将提前定义好的class token放在该字段的语音上下文配置中,同步传递给底层Speech-to-Text服务生效。
Node.js版App Engine Fulfillment示例代码
const express = require('express'); const { WebhookClient } = require('dialogflow-fulfillment'); const app = express(); app.post('/dialogflow-webhook', express.json(), (req, res) => { const agent = new WebhookClient({ request: req, response: res }); function orderQueryHandler(agent) { // 此处为你的业务逻辑处理代码 agent.add('请告知你要查询的订单编号'); // 注入class token识别配置 agent.response_.payload = { google: { speechRecognitionConfig: { speechContexts: [{ phrases: [ "$ORDER_NUM_CLASS" // 替换为你实际定义的class token名称 ], boost: 16 // 可调整权重,范围0-20,值越高对应格式内容识别优先级越高 }] } } }; } let intentMap = new Map(); intentMap.set('订单查询意图', orderQueryHandler); // 替换为你实际的意图名称 agent.handleRequest(intentMap); }); app.listen(process.env.PORT || 8080);
注意事项
- class token需要提前在Google Cloud控制台Speech-to-Text模块完成类规则定义,再将对应的token名称填写到上述配置的
phrases数组中 - 配置仅对当前会话的下一轮语音识别请求生效,若需要全会话生效可在每个意图的响应中都携带该配置
- 你可以根据业务需求在
speechContexts数组中同时添加多个不同的class token
内容的提问来源于stack exchange,提问作者Gavin Siu
相关产品推荐
相关产品推荐

