You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过C#代码调用LUIS意图方法并传递表单流数据

在FormFlow完成后触发LUIS意图的C#实现方案

你遇到的问题是想在FormFlow表单提交后,精准触发指定的LUIS意图(而非返回所有意图数据),下面结合你的现有代码,给出两种可行的C#实现方式:

方式一:复用Bot Framework的LUIS处理管道(推荐)

这种方式不需要直接调用LUIS API,而是把构造好的查询语句模拟成用户消息发送给Bot,让已有的LUIS识别流程自动触发对应的DocumentSearchIntent方法,完美适配你现有的代码结构。

修改你的processDocumentSearch委托代码如下:

OnCompletionAsyncDelegate<DocumentFormFlow> processDocumentSearch = async (context, Docdata) => {
    string message = "Thanks for using chat bot Please wait while we search your document , Welcome Again !!! :)";
    await context.PostAsync(message);
    
    // 构造查询语句
    string query = $"fetch me {Docdata.ClientName} {Docdata.SelectDocument} document";
    
    // 模拟用户发送这条查询消息,让Bot的LUIS管道自动处理
    var messageActivity = context.MakeMessage();
    messageActivity.Text = query;
    
    // 触发Bot的消息处理流程,会自动匹配并调用对应的LUIS意图方法
    await context.Forward(MessageReceived, null, messageActivity, CancellationToken.None);
};

核心逻辑是context.Forward方法:它会把构造好的消息转发给你的MessageReceived入口方法,而你的入口方法已经配置了LUIS识别,所以会自动解析query并触发DocumentSearchIntent方法,完全复用你已有的意图处理逻辑。

方式二:直接调用LUIS REST API并手动触发意图方法

如果需要更精细的控制(比如自定义意图筛选逻辑),可以直接调用LUIS的REST API获取意图结果,再手动调用对应的处理方法。

步骤1:安装LUIS官方NuGet包

先安装LUIS运行时客户端包:

Install-Package Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime

步骤2:修改委托代码

// 替换为你的LUIS配置信息
private static readonly string LuisAppId = "你的LUIS应用ID";
private static readonly string LuisAPIKey = "你的LUIS订阅密钥";
private static readonly string LuisAPIHostName = "你的LUIS端点(例如:westus.api.cognitive.microsoft.com)";

OnCompletionAsyncDelegate<DocumentFormFlow> processDocumentSearch = async (context, Docdata) => {
    string message = "Thanks for using chat bot Please wait while we search your document , Welcome Again !!! :)";
    await context.PostAsync(message);
    
    string query = $"fetch me {Docdata.ClientName} {Docdata.SelectDocument} document";
    
    // 初始化LUIS客户端
    var credentials = new ApiKeyServiceClientCredentials(LuisAPIKey);
    var client = new LUISRuntimeClient(credentials) { Endpoint = $"https://{LuisAPIHostName}" };
    
    // 调用LUIS API获取意图预测结果
    var predictionRequest = new PredictionRequest { Query = query };
    var predictionResponse = await client.Prediction.GetSlotPredictionAsync(
        Guid.Parse(LuisAppId),
        "production", // 或"staging",根据你的LUIS部署槽选择
        predictionRequest
    );
    
    // 获取得分最高的意图
    var topIntent = predictionResponse.Prediction.TopIntent;
    
    // 匹配到目标意图时,手动调用处理方法
    if (topIntent.Equals("DocumentSearch", StringComparison.OrdinalIgnoreCase))
    {
        // 构造适配现有方法的LuisResult对象
        var luisResult = new LuisResult
        {
            Intent = topIntent,
            Intents = predictionResponse.Prediction.Intents.ToDictionary(kv => kv.Key, kv => new LuisIntent { Score = kv.Value.Score }),
            Entities = predictionResponse.Prediction.Entities.Select(e => new EntityRecommendation
            {
                Entity = e.Entity,
                Type = e.Type
            }).ToList()
        };
        
        // 调用你的DocumentSearchIntent方法
        await DocumentSearchIntent(context, luisResult);
    }
    else
    {
        await context.PostAsync($"当前查询触发的意图是{topIntent},未匹配到DocumentSearch意图");
    }
};

关于你之前的问题:为什么JSON调用返回所有意图?

LUIS的API默认会返回所有意图的得分数据,而你需要的是触发对应意图的处理逻辑。上面两种方式都解决了这个问题:方式一依赖Bot Framework的自动路由机制,方式二通过手动判断最高意图后调用对应方法,都能精准触发你需要的DocumentSearchIntent。

内容的提问来源于stack exchange,提问作者Namrata Mahajan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:08:18