AI Receptionist with Microsoft Semantic Kernel and OpenAI

Artificial Intelligence (AI) is transforming the way businesses interact with their customers, and AI-driven virtual assistants or receptionists are becoming increasingly popular. These AI receptionists can handle routine inquiries, manage appointments, and provide essential information, all while delivering a seamless user experience. In this article, we will explore how to build an AI receptionist using Microsoft Semantic Kernel and Azure OpenAI.

Introduction

Microsoft Semantic Kernel is a powerful framework that leverages AI models to perform complex tasks such as language understanding, text generation, and more. By integrating Semantic Kernel with OpenAI's models, developers can create sophisticated AI-driven applications. In this article, we will guide you through building an AI receptionist capable of handling customer interactions in a software house.

Prerequisites

Before diving into the implementation, ensure that you have the following prerequisites.

  1. Azure Subscription: To access OpenAI services.
  2. .NET SDK: Installed on your development environment.
  3. Microsoft.SemanticKernel NuGet Package: For integrating Semantic Kernel.
  4. Basic Knowledge of C#: Familiarity with C# programming and asynchronous programming patterns.

Project Setup

Start by creating a new console application in your preferred development environment, such as Visual Studio or Visual Studio Code.

dotnet new console -n LearnSemanticKernel.AIReceptionist

Once your project is created, install the necessary NuGet package by running.

dotnet add package Microsoft.SemanticKernel
dotnet add package Microsoft.SemanticKernel.Connectors.OpenAI

These packages provide the required classes and methods to integrate Semantic Kernel with OpenAI.

Code Implementation

Below is the complete code for building an AI receptionist using Microsoft Semantic Kernel and OpenAI.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
namespace LearnSemanticKernel.AIReceptionist
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            // Create a kernel with Azure OpenAI chat completion
            var builder = Kernel.CreateBuilder()
                .AddAzureOpenAIChatCompletion("modelid", "endpoint", "apiKey");
            // Build the kernel
            Kernel kernel = builder.Build();
            var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
            // Enable planning
            OpenAIPromptExecutionSettings openAIPromptExecutionSettings = new()
            {
                ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions
            };
            var businessInfoText = File.ReadAllText(Path.Combine(Directory.GetCurrentDirectory(),"Data/BusinessInfo.txt"));
            // Create a history store for the conversation
            var history = new ChatHistory(businessInfoText);
            // Initiate a back-and-forth chat
            string? userInput;
            do
            {
                // Collect user input
                Console.Write("User > ");
                userInput = Console.ReadLine();
                // Add user input
                history.AddUserMessage(userInput);
                // Get the response from the AI
                var result = await chatCompletionService.GetChatMessageContentAsync(
                    history,
                    executionSettings: openAIPromptExecutionSettings,
                    kernel: kernel);
                // Print the results
                Console.WriteLine("Assistant > " + result);
                // Add the message from the agent to the chat history
                history.AddMessage(result.Role, result.Content ?? string.Empty);
            } while (userInput is not null);
        }
    }
}

Breaking Down the Code

  1. Setting Up the Kernel: The Kernel. The CreateBuilder() method is used to create a kernel builder, which is configured to use Azure OpenAI for chat completion. The connection to the OpenAI model is established using the AddAzureOpenAIChatCompletion() method. This method requires the model name, the endpoint URL, and the API key.
  2. Building the Kernel and Chat Service: The Kernel. The build () method finalizes the kernel creation. Afterward, the IChatCompletionService is retrieved to interact with the chat model. This service is responsible for processing the user’s input and generating responses.
  3. Enabling Planning: The OpenAIPromptExecutionSettings class allows for the customization of prompt execution. By setting ToolCallBehavior to AutoInvokeKernelFunctions, the kernel will automatically invoke functions as needed during the conversation.
  4. Loading Business Information: The business information is loaded from a file named BusinessInfo.txt, which is stored in the Data directory. This file contains essential details about the software house, such as services offered and contact information.
    This is an AI Receptionist who will entertain customers to understand our business and help them to connect with us. Welcome customer with prfessional way.
    
    Software House Name: Finchship
    
    Services Offered:
    
    Custom Software Development:
    Tailored software solutions designed to meet specific business needs.
    Full-cycle development from ideation to deployment.
    
    Mobile App Development:
    Native and cross-platform mobile app development for iOS and Android.
    UI/UX design, testing, and ongoing maintenance.
    
    Web Development:
    Responsive website design and development.
    E-commerce solutions, CMS, and web applications.
    
    Cloud Solutions:
    Cloud migration, management, and optimization.
    Expertise in AWS, Azure, and Google Cloud.
    
    DevOps Services:
    Continuous integration and continuous deployment (CI/CD) pipelines.
    Infrastructure as code, automated testing, and monitoring.
    
    AI and Machine Learning:
    AI-powered solutions for data analysis, prediction, and automation.
    Development of custom machine learning models.
    
    Cybersecurity Services:
    Security assessments, penetration testing, and threat mitigation.
    Implementation of secure coding practices and compliance.
    
    IT Consulting:
    Strategic IT consulting to align technology with business goals.
    Digital transformation and technology roadmap planning.
    
    Contact Information:
    
    Office Address:
    Finchship
    123 Innovation Drive, Suite 456
    Tech City, TC 78910
    
    Phone Number:
    +1 (800) 555-1234
    
    Email Address:
    [email protected]
    
    Website:
    www.finchship.com
    
    Customer Support:
    [email protected]
    +1 (800) 555-5678
    
    Business Hours:
    Monday to Friday: 9:00 AM - 6:00 PM (Tech City Time)
    Saturday: 10:00 AM - 4:00 PM
    Sunday: Closed
    
  5. Managing Chat History: A ChatHistory object is created to store the conversation history between the user and the AI receptionist. This history is essential for maintaining context during the interaction, enabling the AI to provide more accurate and relevant responses.
  6. Handling User Input: The code enters a loop where it continuously prompts the user for input. The user’s messages are added to the chat history, and the AI’s response is generated and displayed. The loop continues until the user exits the conversation.

Demo Video

Demo

Conclusion

Building an AI receptionist using Microsoft Semantic Kernel and OpenAI is a powerful way to automate customer interactions while maintaining a high level of service. By following the steps outlined in this article, you can create a virtual assistant that not only responds to inquiries but also learns from interactions, adapts to different scenarios, and seamlessly integrates with your existing business systems.

As AI technology continues to evolve, so too will the capabilities of AI receptionists. The combination of Microsoft Semantic Kernel and OpenAI provides a solid foundation for developing intelligent, responsive, and adaptable virtual assistants that can transform the way businesses operate.

Source Code

You can access the source code from my GitHub repo https://github.com/habib-developer/LearnSemanticKernel


Finchship
We Provide Web, Desktop and Mobile Apps Solution