How to use Microsoft.Bot.Components.Recognizers.CustomQuestionAnsweringRecognizer in Bot Framework Composer?

Wei Lee Loh 0 Reputation points
2023-05-03T08:26:00.85+00:00

I found this custom component (Microsoft.Bot.Components.Recognizers.CustomQuestionAnsweringRecognizer) that will allow me to easily integrate my bot (via bot framework composer) with Language Studio's custom question and answer. Is it possible to create a flow that will combine Intent Recognize and QnA Intent Recognize? Example any keyword will be first match with any intent (usually regex), if no matching recognizer is found then it will pass the keyword to the QnA intent recognize trigger. I'm using the latest composer version (2.1.2) and Language Studio.

Azure AI Bot Service
Azure AI Bot Service
An Azure service that provides an integrated environment for bot development.
771 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 47,686 Reputation points
    2023-05-04T17:12:15.9033333+00:00

    Hello,

    Thanks for reaching out to us, I haven't found any official guidance about this topic but I found below steps which may work for you, please take a look and have a try, all code you need to change according to your scenario -

    You can use the Microsoft.Bot.Components.Recognizers.CustomQuestionAnsweringRecognizer in Bot Framework Composer to integrate your bot with Language Studio's custom question and answer.

    To use this recognizer, you'll need to create a custom component in your Composer project. Here are the steps:

    Create a new folder named CustomComponents in your Composer project's src folder.

    Create a new file named CustomQnAMakerRecognizerComponent.ts in the CustomComponents folder.

    Paste the following code into the CustomQnAMakerRecognizerComponent.ts file:

    typescript

    import * as path from 'path';
    import { Recognizer } from 'botbuilder-dialogs';
    import { CustomQuestionAnsweringRecognizer } from 'microsoft.bot.components.recognizers';
    const defaultLocale = 'en-us';
    const defaultConfiguration = 'default';
    export class CustomQnAMakerRecognizerComponent extends Recognizer {
        constructor() {
            super();
            this.id = 'CustomQnAMakerRecognizer';
        }
        public async onRecognize(context, options) {
            const recognizerResult = {
                $kind: 'Microsoft.CustomQnAMakerRecognizer',
                intents: {},
                entities: {},
            };
            const utterance = context.activity.text;
            const locale = context.activity.locale || defaultLocale;
            const configuration = options.configuration || defaultConfiguration;
            const recognizer = new CustomQuestionAnsweringRecognizer({
                endpointUrl: process.env.QNA_ENDPOINT_URL,
                endpointKey: process.env.QNA_ENDPOINT_KEY,
                kbId: process.env.QNA_KB_ID,
                endpointHost: process.env.QNA_ENDPOINT_HOST,
            });
            const result = await recognizer.recognize(utterance, locale, configuration);
            recognizerResult.intents = result.intents;
            recognizerResult.entities = result.entities;
            return recognizerResult;
        }
    }
    

    Save the file.

    In your Composer project, click the "Add component" button in the "Components" pane.

    In the "Add Component" dialog, select "Custom" as the component type.

    Enter "CustomQnAMakerRecognizer" as the name and "CustomComponents/CustomQnAMakerRecognizerComponent.ts" as the path.

    Click "Add" to add the component to your project.

    Now you can use the CustomQnAMakerRecognizer in your Bot Framework Composer dialog. To use it in combination with Intent Recognizer, you can add a Switch action in your dialog that checks if any matching recognizer is found, and if not, it triggers the CustomQnAMakerRecognizer. Here's an example:

    {
        "$kind": "Microsoft.AdaptiveDialog",
        "autoEndDialog": true,
        "defaultResultProperty": "dialog.result",
        "recognizer": {
            "$kind": "Microsoft.RegexRecognizer",
            "intents": [
                {
                    "intent": "Greeting",
                    "patterns": [
                        "hi",
                        "hello"
                    ]
                }
            ]
        },
        "triggers": [
            {
                "$kind": "Microsoft.OnIntent",
                "intent": "Greeting",
                "actions": [
                    {
                        "$kind": "Microsoft.SendActivity",
                        "activity": "${SendActivity('Hi there! How can I help you today?')}"
                    }
                ]
            },
            {
                "$kind": "Microsoft.OnUnknownIntent",
                "actions": [
                    {
                        "$kind": "Microsoft.Switch",
                        "condition": "count(recognizerResult.intents) > 0",
                        "default": [
                            {
                                "$kind": "Microsoft.SendActivity",
                                "activity
    
    

    I hope this helps. Thanks.

    Regards,

    Yutong

    -Please kindly accept the answer if you feel helpful to support the community, thanks a lot.

    0 comments No comments