共计 2834 个字符,预计需要花费 8 分钟才能阅读完成。
概念解析:Skill 的本质特性
Skill 是一种事件驱动的交互式服务,与普通 API/ 插件的核心区别在于:

- 会话上下文感知:能记住多轮对话状态(比如用户问 ” 天气怎么样 ” 后追问 ” 明天呢 ”)
- 自然语言理解:通过 NLU 引擎将用户口语转化为结构化意图(如 ” 查天气 ” 对应 WeatherQueryIntent)
- 多模态响应:除了语音 /text,还能返回卡片、视频等富媒体(Alexa 的 APL 模板)
举个具体例子:当用户说 ” 问问天气预报今天北京会下雨吗 ”,
1. 技能平台会先解析出意图(WeatherCheckIntent)
2. 提取实体参数(地点 = 北京,日期 = 今天)
3. 将结构化参数传给开发者配置的接口
4. 最终返回语音 + 图文卡片组合结果
开发准备:平台选型与环境配置
主流平台对比:
- Alexa Skills Kit (ASK)
- 优势:文档最全,调试工具完善(Alexa 开发者控制台)
- 劣势:认证流程严格,仅支持 Lambda/ 自托管
-
必备工具:ASK CLI、AWS 账号
-
Google Actions
- 优势:直接对接 Android 生态,支持更多语言
- 劣势:多轮对话设计复杂
- 必备工具:gactions CLI、Firebase 项目
通用环境清单:
- Node.js 14+ 或 Python 3.8+
- ngrok(本地调试反向代理)
- 对应平台的开发者账号
- IDE 插件(如 VS Code 的 Alexa Toolkit)
核心实现:天气查询技能实战
步骤 1:定义意图与话语
以 Alexa 为例,在交互模型 JSON 中定义:
{
"intents": [
{
"name": "WeatherQueryIntent",
"slots": [{"name": "city", "type": "AMAZON.US_CITY"},
{"name": "date", "type": "AMAZON.DATE"}
],
"samples": ["{city}天气怎么样",
"查查 {date} 的{city}天气预报"
]
}
]
}
步骤 2:处理会话逻辑(Node.js 示例)
const weatherHandler = {canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'WeatherQueryIntent';
},
async handle(handlerInput) {
try {const { city, date} = handlerInput.requestEnvelope.request.intent.slots;
// 调用天气 API(实际项目建议封装为独立服务)const data = await getWeather(city.value, date.value);
// 保持会话上下文
const sessionAttributes = handlerInput.attributesManager.getSessionAttributes();
sessionAttributes.lastQuery = {city: city.value, date: date.value};
// 构建语音 + 卡片响应
return handlerInput.responseBuilder
.speak(`${city.value}${date.value}的天气是 ${data.summary}`)
.withStandardCard(`${city.value}天气预报 `,
` 日期: ${date.value}\n 状态: ${data.summary}\n 温度: ${data.temp}℃`,
data.imageUrl
)
.getResponse();} catch (error) {console.error('天气查询失败:', error);
return handlerInput.responseBuilder
.speak('抱歉,获取天气信息时出错了,请稍后再试')
.reprompt('您想查询其他城市吗?')
.getResponse();}
}
};
生产级优化关键点
对话超时处理
- 设置合理的 sessionTimeoutInSeconds(通常 60-300 秒)
- 使用持久化存储(如 DynamoDB)保存超过超时时间的上下文
敏感信息加密
推荐方案:
- 使用平台提供的加密服务(如 Alexa 的 key encryption)
- OAuth token 存储示例:
# 使用 AWS KMS 加密
import boto3
kms = boto3.client('kms')
def encrypt_token(token):
response = kms.encrypt(
KeyId='alias/your-key-name',
Plaintext=token.encode())
return response['CiphertextBlob']
性能压测指标
- 冷启动延迟:控制在 1500ms 内(可用 Lambda Provisioned Concurrency 优化)
- 并发会话:按峰值流量的 1.5 倍配置(Alexa 单个技能默认限制 1000TPS)
典型故障与解决方案
意图匹配失败
调试方法:
- 检查平台端的 NLU 日志(如 Alexa 的 Utterance Profiler)
- 添加未识别意图的兜底处理
const FallbackHandler = {canHandle(handlerInput) {
return handlerInput.requestEnvelope.request.type === 'IntentRequest'
&& handlerInput.requestEnvelope.request.intent.name === 'AMAZON.FallbackIntent';
},
handle(handlerInput) {
// 记录失败请求用于优化模型
logUnhandledIntent(handlerInput.requestEnvelope);
return handlerInput.responseBuilder
.speak('没听懂您的请求,请换种说法试试?')
.reprompt('您可以说" 帮助 "查看支持的功能')
.getResponse();}
};
多轮对话状态丢失
预防措施:
- 每次响应前保存关键数据到 sessionAttributes
- 重要流程使用持久化存储 + 唯一 sessionId 关联
证书自动续期
推荐方案:
- 使用 ACM(AWS Certificate Manager)自动续期
- 通过 CloudWatch Event 触发 Lambda 更新技能配置
思考:当 Skill 遇到 LLM
传统技能需要明确定义意图和话语模板,而像 ChatGPT 这类大语言模型可以动态理解用户请求。未来是否会演变为:
- 开发者只需提供 API 能力,由 LLM 负责意图解析?
- 多轮对话状态管理是否会被上下文窗口(context window)替代?
这些问题值得在技术选型时持续关注。
正文完
