DEV Community

Cover image for Build a Smarter FAQ Bot with Azure AI: A Step-by-Step Guide to Question Answering
Olalekan Oladiran
Olalekan Oladiran

Posted on

Build a Smarter FAQ Bot with Azure AI: A Step-by-Step Guide to Question Answering

Introduction

One of the most common conversational scenarios is providing support through a knowledge base of frequently asked questions (FAQs). Many organizations publish FAQs as documents or web pages, which works well for a small set of question and answer pairs, but large documents can be difficult and time-consuming to search.

Azure AI Language includes a question answering capability that enables you to create a knowledge base of question and answer pairs that can be queried using natural language input, and is most commonly used as a resource that a bot can use to look up answers to questions submitted by users.

Provision an Azure AI Language resource

Click Build Your First Text Analytics App with Azure AI in Under 30 Minutes

Create a question answering project

To create a knowledge base for question answering in your Azure AI Language resource, you can use the Language Studio portal to create a question answering project. In this case, you’ll create a knowledge base containing questions and answers about Microsoft Learn.

  • In a new browser tab, go to the Language Studio portal at https://language.cognitive.azure.com/ and sign in using the Microsoft account associated with your Azure subscription. Image description
  • If you’re prompted to choose a Language resource, select the following settings:
    • Azure Directory: The Azure directory containing your subscription.
    • Azure subscription: Your Azure subscription.
    • Resource type: Language
    • Resource name: The Azure AI Language resource you created previously. Image description If you are not prompted to choose a language resource, it may be because you have multiple Language resources in your subscription; in which case:
    • On the bar at the top if the page, select the Settings (⚙) button.
    • On the Settings page, view the Resources tab.
    • Select the language resource you just created, and click Switch resource.
    • At the top of the page, click Language Studio to return to the Language Studio home page.
  • Click create new and select Custom question answering. Image description
  • In the Create a project wizard, on the Choose language setting page, select the option to Select the language for all projects, and select English as the language. Then select Next. Image description
  • On the Enter basic information page, enter the following details:
    • Name LearnFAQ
    • Description: FAQ for Microsoft Learn
    • Default answer when no answer is returned: Sorry, I don't understand the question Image description
  • Select Next.
  • On the Review and finish page, select Create project. Image description

Add sources to the knowledge base

You can create a knowledge base from scratch, but it’s common to start by importing questions and answers from an existing FAQ page or document. In this case, you’ll import data from an existing FAQ web page for Microsoft Learn, and you’ll also import some pre-defined “chit chat” questions and answers to support common conversational exchanges.

  • On the Manage sources page for your question answering project, in the + Add source list, select URLs. Then in the Add URLs dialog box, select + Add url and set the following name and URL before you select Add all to add it to the knowledge base:

Image description
Image description
Image description
Image description

  • On the Manage sources page for your question answering project, in the + Add source list, select Chitchat. The in the Add chit chat dialog box, select Friendly and select Add chit chat. Image description Image description

Edit the knowledge base

Your knowledge base has been populated with question and answer pairs from the Microsoft Learn FAQ, supplemented with a set of conversational chit-chat question and answer pairs. You can extend the knowledge base by adding additional question and answer pairs.

  • In your LearnFAQ project in Language Studio, select the Edit knowledge base page to see the existing question and answer pairs (if some tips are displayed, read them and choose Got it to dismiss them, or select Skip all) Image description
  • In the knowledge base, on the Question answer pairs tab, select +, and create a new question answer pair with the following settings:
  • Select Done. Image description
  • In the page for the What are Microsoft credentials? question that is created, expand Alternate questions. Then add the alternate question How can I demonstrate my Microsoft technology skills?. Image description
  • Under the answer you entered for the certification question, expand Follow-up prompts and add the following follow-up prompt:
    • Text displayed in the prompt to the user: Learn more about credentials.
    • Select the Create link to new pair tab, and enter this text: You can learn more about credentials on the Microsoft credentials page.
    • Select Show in contextual flow only. This option ensures that the answer is only ever returned in the context of a follow-up question from the original certification question.
  • Select Add prompt. Image description Image description

Train and test the knowledge base

Now that you have a knowledge base, you can test it in Language Studio.

  • Save the changes to your knowledge base by selecting the Save button under the Question answer pairs tab on the left. Image description
  • After the changes have been saved, select the Test button to open the test pane. Image description
  • In the test pane, at the top, deselect Include short answer response (if not already unselected). Then at the bottom enter the message Hello. A suitable response should be returned.
  • In the test pane, at the bottom enter the message What is Microsoft Learn?. An appropriate response from the FAQ should be returned.
  • Enter the message Thanks! An appropriate chit-chat response should be returned.
  • Enter the message Tell me about Microsoft credentials. The answer you created should be returned along with a follow-up prompt link.
  • Select the Learn more about credentials follow-up link. The follow-up answer with a link to the certification page should be returned.
  • When you’re done testing the knowledge base, close the test pane. Image description

Deploy the knowledge base

The knowledge base provides a back-end service that client applications can use to answer questions. Now you are ready to publish your knowledge base and access its REST interface from a client.

  • In the LearnFAQ project in Language Studio, select the Deploy knowledge base page from the navigation menu on the left.
  • At the top of the page, select Deploy. Then select Deploy to confirm you want to deploy the knowledge base. Image description Image description
  • When deployment is complete, select Get prediction URL to view the REST endpoint for your knowledge base and note that the sample request includes parameters for:
    • projectName: The name of your project (which should be LearnFAQ)
    • deploymentName: The name of your deployment (which should be production) Image description Image description
  • Close the prediction URL dialog box.

Prepare to develop an app in VS Code

  • Open VS Code and enter the following commands to clone the GitHub repo for this exercise: git clone https://github.com/microsoftlearning/mslearn-ai-language mslearn-ai-language

Image description

  • After the repo has been cloned, navigate to the folder containing the application code files:

cd mslearn-ai-language/Labfiles/02-qna

Image description

Configure your application

Applications for both C# and Python have been provided, as well as a sample text file you’ll use to test the summarization. Both apps feature the same functionality. We will be using C# for this project. First, you’ll complete some key parts of the application to enable it to use your Azure AI Language resource.

  • Run the command cd C-Sharp/qna-app on your language preference. Each folder contains the language-specific files for an app into which you’re going to integrate Azure AI Language question answering functionality.

dotnet add package Azure.AI.Language.QuestionAnswering

Image description

  • Open appsettings.json file.
  • In the code file, update the configuration values it contains to reflect the endpoint and an authentication key for the Azure Language resource you created (available on the Keys and Endpoint page for your Azure AI Language resource in the Azure portal). The project name and deployment name for your deployed knowledge base should also be in this file. Image description
  • After you’ve replaced the placeholders, within the code editor, use the CTRL+S command or Right-click > Save to save your changes.

Add code to the application

Now you’re ready to add the code necessary to import the required SDK libraries, establish an authenticated connection to your deployed project, and submit questions.

  • Note that the qna-app folder contains a code file for the client application: C#: Program.cs
  • Open the code file and at the top, under the existing namespace references, find the comment Import namespaces. Then, under this comment, add the following language-specific code to import the namespaces you will need to use the Text Analytics SDK:
 // import namespaces
 using Azure;
 using Azure.AI.Language.QuestionAnswering;
Enter fullscreen mode Exit fullscreen mode

Image description

  • In the Main function, note that code to load the Azure AI Language service endpoint and key from the configuration file has already been provided. Then find the comment Create client using endpoint and key, and add the following code to create a client for the Text Analysis API:
// Create client using endpoint and key
AzureKeyCredential credentials = new AzureKeyCredential(aiSvcKey);
Uri endpoint = new Uri(aiSvcEndpoint);
QuestionAnsweringClient aiClient = new    QuestionAnsweringClient(endpoint, credentials); 
Enter fullscreen mode Exit fullscreen mode

Image description

  • In the Main function, find the comment Submit a question and display the answer, and add the following code to repeatedly read questions from the command line, submit them to the service, and display details of the answers:
 // Submit a question and display the answer
 string user_question = "";
 while (true)
     {
         Console.Write("Question: ");
         user_question = Console.ReadLine();
         if (user_question.ToLower() == "quit")
             break;
         QuestionAnsweringProject project = new QuestionAnsweringProject(projectName, deploymentName);
         Response<AnswersResult> response = aiClient.GetAnswers(user_question, project);
         foreach (KnowledgeBaseAnswer answer in response.Value.Answers)
         {
             Console.WriteLine(answer.Answer);
             Console.WriteLine($"Confidence: {answer.Confidence:P2}");
             Console.WriteLine($"Source: {answer.Source}");
             Console.WriteLine();
         }
     }
Enter fullscreen mode Exit fullscreen mode

Image description

  • Save your changes, then enter the following command to run the program:

    dotnet run

  • When prompted, enter a question to be submitted to your question answering project; for example What is a learning path?.

  • Review the answer that is returned.
    Image description

  • Ask more questions. When you’re done, enter quit.

You’ve just transformed static FAQ documents into an intelligent, AI-powered question-answering system with Azure AI Language. By leveraging natural language understanding, your knowledge base can now deliver instant, accurate responses—freeing up time for more complex support tasks and improving user experiences.

But don’t stop here! Experiment with adding more data sources, fine-tuning answers, or integrating your knowledge base into a chatbot for seamless customer interactions. The future of automated support starts with tools like these, and you’re already ahead of the curve.

Ready to take the next step? Dive deeper into Azure AI documentation or try connecting your Q&A system to Microsoft Bot Framework. Happy building!

Thanks for staying till the end

Top comments (0)