如何将CLU(Conversational Language Understanding)集成到Node.js版Bot Framework SDK中
在Node.js Bot Framework应用中集成CLU的方案
是的,有官方支持的工具可以将Azure CLU(Conversational Language Understanding)集成到你的Node.js Bot Framework应用中,替代即将停用的LUIS,具体方案如下:
1. 使用官方CLU客户端库
微软提供了专门的Node.js客户端库@azure/ai-language-conversations,用于和CLU服务交互,这是集成CLU的首选方式。
安装命令:
npm install @azure/ai-language-conversations基础使用示例:
const { ConversationAnalysisClient, AzureKeyCredential } = require('@azure/ai-language-conversations'); // 初始化CLU客户端 const cluEndpoint = '<你的CLU服务端点>'; const cluApiKey = '<你的CLU API密钥>'; const projectName = '<你的CLU项目名称>'; const deploymentName = '<你的CLU部署名称>'; const client = new ConversationAnalysisClient(cluEndpoint, new AzureKeyCredential(cluApiKey)); // 处理用户输入的函数 async function recognizeUserInput(userInput) { const result = await client.analyzeConversation({ task: { kind: 'Conversation', analysisInput: { conversationItem: { text: userInput, id: '1', participantId: 'user' } }, parameters: { projectName: projectName, deploymentName: deploymentName, verbose: true } } }); // 提取意图和实体 const topIntent = result.result.prediction.topIntent; const entities = result.result.prediction.entities; return { topIntent, entities }; }
2. 在Bot Framework中替换LUIS逻辑
你可以在原有回声机器人的对话处理逻辑中,替换掉原来LUISRecognizer的调用部分,改用上述CLU客户端来处理用户输入:
// 在机器人的turn handler中使用CLU this.onMessage(async (context, next) => { const userInput = context.activity.text; const cluResult = await recognizeUserInput(userInput); // 根据CLU返回的意图处理对话 switch(cluResult.topIntent) { case 'Greeting': await context.sendActivity('你好!有什么我可以帮你的吗?'); break; case 'WeatherQuery': // 提取实体并处理天气查询逻辑 const location = cluResult.entities.find(e => e.category === 'Location')?.text; await context.sendActivity(`你想查询${location}的天气吗?`); break; default: // 回退到回声逻辑 await context.sendActivity(`你说了:${userInput}`); } await next(); });
3. 关键注意事项
- 确保你已经在Azure门户中创建了CLU资源,并且完成了模型的训练和发布
- 初始化客户端时需要的端点、API密钥、项目名称和部署名称,都可以从Azure门户的CLU资源详情页获取
- 若需要更贴合Bot Framework的使用习惯,也可以自行封装CLU调用逻辑为一个工具类,类似原来的
LUISRecognizer
内容的提问来源于stack exchange,提问作者Narandhran Thangavel
相关产品推荐
相关产品推荐

