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

如何为LuisRecognizerMiddleware添加Bing拼写检查功能?

在Bot Framework v4的LuisRecognizerMiddleware中配置Bing拼写检查

要在你的LUIS请求中添加Bing拼写检查参数,你需要替换掉原来使用的LuisModel类,改用更灵活的LuisRecognizerOptionsV2来配置——因为LuisModel只封装了最基础的LUIS连接信息,不支持拼写检查这类额外参数的设置。

以下是具体的修改步骤:

1. 从配置中获取Bing拼写检查密钥

首先,确保你的配置文件(比如appsettings.json)里已经添加了Bing拼写检查的订阅密钥,然后新增方法读取所有需要的配置项:

private static (string modelId, string luisSubscriptionKey, Uri luisUrl, string bingSpellCheckKey) GetLuisAndBingConfig(IConfiguration configuration)
{
    string modelId = configuration.GetSection("Luis-ModelId")?.Value;
    string luisSubscriptionKey = configuration.GetSection("Luis-SubscriptionId")?.Value;
    string luisUrl = configuration.GetSection("Luis-Url")?.Value;
    string bingSpellCheckKey = configuration.GetSection("BingSpellCheck-SubscriptionKey")?.Value;
    
    return (modelId, luisSubscriptionKey, new Uri(luisUrl), bingSpellCheckKey);
}

2. 改用LuisRecognizerOptionsV2配置LUIS识别器

在ConfigureServices方法中,替换原来的LuisModel和LuisRecognizerMiddleware初始化代码,用LuisApplication和LuisRecognizerOptionsV2来包含所有需要的参数:

public void ConfigureServices(IServiceCollection services)
{
    services.AddBot<MyBot>(options =>
    {
        options.CredentialProvider = new ConfigurationCredentialProvider(_configuration);
        options.Middleware.Add(new CatchExceptionMiddleware<Exception>(async (context, exception) =>
        {
            await context.TraceActivity("MyBotException", exception);
            await context.SendActivity("Sorry, it looks like something went wrong!");
        }));

        IStorage dataStore = new MemoryStorage();
        options.Middleware.Add(new ConversationState<MyBotConversationState>(dataStore));

        // 获取LUIS和Bing拼写检查的配置
        var (modelId, luisSubscriptionKey, luisUrl, bingSpellCheckKey) = GetLuisAndBingConfig(_configuration);
        
        // 创建LUIS应用实例
        var luisApplication = new LuisApplication(modelId, luisSubscriptionKey, luisUrl.AbsoluteUri);
        
        // 配置LUIS识别器选项,包含拼写检查参数
        var luisOptions = new LuisRecognizerOptionsV2(luisApplication)
        {
            SpellCheck = true,
            BingSpellCheckSubscriptionKey = bingSpellCheckKey,
            Verbose = true,
            TimezoneOffset = 0,
            Log = true // 保留你原来的日志设置
        };
        
        // 添加配置好的LUIS识别器中间件
        options.Middleware.Add(new LuisRecognizerMiddleware(luisOptions));
    });
}

为什么这样修改有效?

LuisRecognizerOptionsV2是专门为LUIS v2 API设计的配置类,它暴露了一系列属性来直接控制请求参数,包括:

  • SpellCheck:开启/关闭拼写检查功能
  • BingSpellCheckSubscriptionKey:你的Bing拼写检查服务密钥
  • Verbose:返回更详细的LUIS分析结果
  • TimezoneOffset:设置时区偏移值
  • Log:控制是否记录查询日志

修改后,你的LUIS请求就会自动带上这些参数,和你期望的格式完全一致:

GET https://westeurope.api.cognitive.microsoft.com/luis/v2.0/apps/?subscription-key=xxx&spellCheck=true&bing-spell-check-subscription-key=xxx&verbose=true&timezoneOffset=0&q=test234&log=True HTTP/1.1

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:25:31