Site icon Rajeev Singh | Coder, Blogger, YouTuber

Unlock the Power of AI: Creating Your First AI Agent Using Semantic Kernel

In my previous blog, we explored the concept of an AI Agent. For more details on what an AI Agent is and how to create your first one, refer to the blog Create Your First AI Agent with Azure AI Service

We also delved into Multi-Agent systems. To learn more about this, check out the blog Kickstart Your Journey with Multi-Agent Systems: Kickstart Your Journey with Multi-Agent Systems: Build Your First Multi-Agent Using Python and Azure OpenAI

We created a Multi-Agent system and examined various options for building it without an orchestration layer, managing the agents manually.

In this post, we will dive into the Multi-Agent Orchestration Framework.

Multi-agent orchestration frameworks

Azure AI Agent Service works out-of-the-box with multi-agent orchestration frameworks that are wireline compatible with the Assistants API, such as AutoGen, a state-of-the-art research SDK for Python created by Microsoft Research, and Semantic Kernel, an enterprise AI SDK for Python, .NET, and Java.

When building a new multi-agent solution, start with building singleton agents with Azure AI Agent Service to get the most reliable, scalable, and secure agents.

You can then orchestrate these agents together AutoGen is constantly evolving to find the best collaboration patterns for agents (and humans) to work together. Features that show production value with AutoGen can then be moved into Semantic Kernel if you’re looking for production support and non-breaking changes.


If you want to explore more on this, refer to this post from Microsoft:

Azure AI Agent Service: Revolutionizing AI Agent Development and Deployment

Note: In this post, we will get started with Semantic Kernal.

What is an Orchestration layer in Developing AI solutions?

Application Design for AI Workloads on Azure – Microsoft Azure Well-Architected Framework | Microsoft Learn

Orchestration and agents

For generative AI workloads, adopting an agent-based, or agentic, approach can enhance the extensibility of your orchestration. Agents offer context-specific functionality and share many traits with microservices, performing tasks alongside an orchestrator.

The orchestrator can either advertise tasks to a pool of agents or allow agents to register their capabilities. Both methods enable the orchestrator to dynamically determine how to divide and route queries among agents.

Agentic approaches are particularly effective when you have a common user interface with multiple, evolving features that can be integrated to add more skills and grounding data over time. For complex workloads involving numerous agents, it is more efficient to let agents collaborate dynamically rather than having an orchestrator break up and assign tasks.

Communication between the orchestrator and agents should follow a topic-queue pattern, where agents subscribe to a topic and the orchestrator sends out tasks via a queue. An agentic approach works best with an orchestration pattern.

Where does this Orchestration Layer fit in with AI workload?

Now we know what these Orchestration frameworks are and when to use them, let’s understand where does it fit in with the overall GenAI solution?

Typical architecture pattern and design areas

See below, this diagram shows a typical architecture pattern and design areas and illustrates how data flows through the system from initial collection to final user interaction.

The architecture highlights the integration of different components to enable efficient data processing, model optimization, and real-time application deployment in AI-driven solutions. It includes modules such as data sources, data processing, model training, model deployment, and user interfaces.

We will be focusing on the Orchestration layer. But before that, we will cover all the components first.

To know more about this refer, AI workloads on Azure – Microsoft Azure Well-Architected Framework | Microsoft Learn

We will be covering various aspects of AI workload, design and assessment approach some other day (another blog I mean 😊)!

Orchestration Layer Flow

Let’s take an example with the above diagram to understand how the orchestration layer works in a Generative AI (GenAI) solution using Retrieval-Augmented Generation (RAG):

  1. Model Deployment: The model is published to the inference hosting platform in advance.
  2. Client Invocation: The client application invokes the orchestration layer.
  3. Data Augmentation: The orchestration layer retrieves augmentation data to enhance the model’s performance.
  4. Context Retrieval: It gathers context from the user’s interaction history.
  5. External Services: If needed, the orchestration layer calls external services.
  6. Model Invocation: The orchestration layer invokes the model via the gateway.
  7. Request Forwarding: The gateway forwards the request to the model.

Select resources for generative AI workloads (Scenario: RAG)

Resource selection recommendations for AI workloads on Azure – Cloud Adoption Framework | Microsoft Learn

Now we know where the Orchestration Layer fits into the overall GenAI design. We will explore resource selection recommendations for organizations running AI workloads on Azure (Azure PaaS).

Generative AI requires the combination of different resources to process and generate meaningful outputs based on input data. Proper selection ensures that generative AI applications, such as those using retrieval augmented generation (RAG), deliver accurate by grounding AI models.

#ComponentDescriptionExamples
 1UI LayerThe user interface where users interact with the AI solution.ASP.net React Angular
 2Orchestration LayerManages the workflow and coordinates between different components.Semantic Kernel AutoGen LangChain
 3Search and RetrievalRetrieves relevant data and information to support AI tasks.Azure AI Search Vector db
 4Data SourcesThe repositories where data is stored and accessed.Azure SQL Database Azure Cosmos DB Azure Blob Storage
 5GenAI AI PlatformThe platform where AI models are developed, trained, and deployed.Azure AI Foundry Azure OpenAI

What is Semantic Kernel?

Refer to this blog for all the details on What is Multi-Agent, Why do we need an Orchestration Layer? What is Semantic Kernel?  Kickstart Your Journey with Multi-Agent Systems: Build Your First Multi-Agent Using Python and Azure OpenAI

Semantic Kernel integrates LLMs like OpenAI, Azure OpenAI, and Hugging Face with conventional programming languages like C#, Python, and Java.

Using the SDK, developers can create “plugins” to interface with the LLMs and perform various tasks.

The Semantic Kernel SDK acts as a bridge between AI capabilities and traditional code, which helps simplify the process of developing AI-powered applications. Developers can easily utilize LLMs in their own applications without having to learn the intricacies the model’s API.

The kernel is the central component of the Semantic Kernel. The kernel acts as a dependency injection container that manages all of the services and plugins needed to run your AI application. This provides developers with a centralized location to configure and monitor their AI agents. For example, suppose you invoke a prompt from the kernel. The kernel will perform the following actions:

  1. Select the best AI service to run the prompt.
  2. Build the prompt using the provided prompt template.
  3. Send the prompt to the AI service.
  4. Receive and parse the response.
  5. Return the response from the LLM to your application.

Throughout this entire process, you can create events and middleware that are triggered at any of these steps. This means you can perform actions like logging, provide status updates to users, and implement responsible AI.

Understanding How Semantic Kernel works?

Let’s take an example, and suppose you invoke a prompt from the kernel. The kernel will perform the following actions:

  1. Select the best AI service to run the prompt.
  2. Build the prompt using the provided prompt template.
  3. Send the prompt to the AI service.
  4. Receive and parse the response.
  5. Return the response from the LLM to your application.

Throughout this entire process, you can create events and middleware that are triggered at any of these steps. This means you can perform actions like logging, provide status updates to users, and implement responsible AI.

What is semantic kernel – Training | Microsoft Learn

We will focus here on how to get started with SK.

Getting development started with Semantic Kernal

Semantic Kernal SDK

Semantic Kernel is an SDK that integrates Large Language Models (LLMs) like OpenAIAzure OpenAI, and Hugging Face with conventional programming languages like C#, Python, and Java. Semantic Kernel achieves this by allowing you to define plugins that can be chained together in just a few lines of code.

GitHub url to get started with SK: microsoft/semantic-kernel: Integrate cutting-edge LLM technology quickly and easily into your apps

Demo Overview

We will start with the Hello World demo and understand how to develop this program using SK. In case you want to explore another example, check Microsoft QuickStart  guide from here: How to quickly start with Semantic Kernel | Microsoft Learn

In this post, the second example we will cover our previous example used in creating Multi-Agent, Yes, the famous 😊 FoodAgent and MealSuggestionAgent.

Demo1: Hello World using Semantic Kernal

Demo2: Multi Agent (FoodAgent and MealSuggestionAgent) using Semantic Kernal

Demo1: Hello World using Semantic Kernal

Prerequisites:

  1. VSCode
  2. Install the SDK

dotnet add package Microsoft.SemanticKernel

Semantic Kernel Tools – Visual Studio Marketplace

  1. Create a new .NET Console project using this command:

dotnet new console

Code

Below is the complete code for your First Program using SK.

This code is in C# and uses below components.

// 1. Import packages
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;

using System.ComponentModel;
using System.Text.Json.Serialization;

using System.Diagnostics;

// Populate values from your OpenAI deployment
var modelId = "gpt-4o";
var endpoint = "https://openai-sk-demo.openai.azure.com/";
var apiKey = "<Your API Key>";

// 2. Create a kernel with Azure OpenAI chat completion
var builder = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(modelId, endpoint, apiKey);

// 3. Add enterprise components (Add Telemetry)
builder.Services.AddLogging(services => services.AddConsole().SetMinimumLevel(LogLevel.Trace));

// 4. Build the kernel (Core Kernal)
Kernel kernel = builder.Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();

// 6. Add a plugin (the LightsPlugin class is defined below)
//kernel.Plugins.AddFromType<LightsPlugin>("Lights");

// 6. Add plugins for custom agents (Hello World)
kernel.Plugins.AddFromType<HelloWorldChatbotPlugin>("HelloWorldChatbot");

// 6. Add plugins for custom agents (Hello World)
//kernel.Plugins.AddFromType<FoodAgent>("FoodAgent");
//kernel.Plugins.AddFromType<MealSuggestionAgent>("MealSuggestionAgent");


// 9. Enable planning
var openAIPromptExecutionSettings = new OpenAIPromptExecutionSettings() 
{
    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};

// 10. Invoke: Create a history store the conversation
// Get the response from the AI
var history = new ChatHistory();

// 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);



public class HelloWorldChatbotPlugin
{
    [KernelFunction("greet")]
    public string Greet()
    {
        return "Hello Rajeev! How can I assist you today?";
    }

    [KernelFunction("bye")]
    public string Bye()
    {
        return "Goodbye Rajeev! Have a great day!";
    }
}

Output

Run the program and start with below prompts and see the output.

Flow of the Program

  1. Initialization: The program initializes the kernel with Azure OpenAI chat completion and adds the custom intents plugin.
  2. User Input: The program enters a loop where it prompts the user for input.
  3. Processing Input: The user input is added to the chat history, and the chat completion service generates a response based on the input and the defined intents.
  4. Generating Response: The response from the AI is printed to the console, and the conversation continues until the user exits by providing no input.

This setup allows the chatbot to respond to specific intents like “Hello” and “Bye” with custom messages, making it interactive and useful.

Explanation of the Code and Flow

1. Setting Up the Project

First, you need to create a new C# console application

dotnet new console

Add the Semantic Kernel NuGet package to your project:

dotnet add package Microsoft.SemanticKernel

2. Imports and Initialization

The necessary namespaces are imported, and the Semantic Kernel is initialized with Azure OpenAI chat completion:

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using System.ComponentModel;
using System.Text.Json.Serialization;
using System.Diagnostics;

3. Main Program

The Main method is the entry point of the application. It performs the following steps:

var modelId = "<Your model name>";
var endpoint = "<Your endpoint>"
var apiKey = "<Your API Key>";

 Create and Build the Kernel: Initialize the kernel with Azure OpenAI chat completion and build it.

// 2. Create a kernel with Azure OpenAI chat completion
var builder = Kernel.CreateBuilder().AddAzureOpenAIChatCompletion(modelId, endpoint, apiKey);

// 4. Build the kernel (Core Kernal)
Kernel kernel = builder.Build();
var chatCompletionService = kernel.GetRequiredService<IChatCompletionService>();
// 6. Add plugins for custom agents (Hello World)
kernel.Plugins.AddFromType<HelloWorldChatbotPlugin>("HelloWorldChatbot");
// 9. Enable planning
var openAIPromptExecutionSettings = new OpenAIPromptExecutionSettings() 
{
    FunctionChoiceBehavior = FunctionChoiceBehavior.Auto()
};
// 10. Invoke: Create a history store the conversation
// Get the response from the AI
var history = new ChatHistory();
// 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);

 

4. Custom Intents Plugin

The ChatbotPlugin class defines two methods: Greet and Bye, which handle the “Hello” and “Bye” intents, respectively. These methods are annotated with [KernelFunction] to indicate that they are functions that can be invoked by the kernel.

public class HelloWorldChatbotPlugin
{
    [KernelFunction("greet")]
    public string Greet()
    {
        return "Hello Rajeev! How can I assist you today?";
    }

    [KernelFunction("bye")]
    public string Bye()
    {
        return "Goodbye Rajeev! Have a great day!";
    }

 

Demo2: Multi Agent (FoodAgent and MealSuggestionAgent) using Semantic Kernal

Great job on completing your first code with Semantic Kernel (SK)!

Now, let’s dive into another example to explore how SK can be beneficial in enhancing your AI agent’s capabilities.

This will help you understand the practical applications and advantages of using SK in your projects.

Ready to get started?

Code

All the code remains the same, and the only change you do is below:

Add Plugins for custom agents

// Add plugins for custom agents
kernel.Plugins.AddFromType<FoodAgent>("FoodAgent");
kernel.Plugins.AddFromType<MealSuggestionAgent>("MealSuggestionAgent");

Agent Code

public class FoodAgent
{
    [KernelFunction("get_food_info")]
    public string GetFoodInfo(string food)
    {
        // Simulate fetching food information
        return $"The food {food} is rich in vitamins and minerals.";
    }

    [KernelFunction("get_calories")]
    public string GetCalories(string food)
    {
        // Simulate fetching calorie information
        return $"The food {food} contains approximately 200 calories per serving.";
    }
}

public class MealSuggestionAgent
{
    [KernelFunction("suggest_meal")]
    public string SuggestMeal(string preference)
    {
        // Simulate suggesting a meal based on preference
        if (preference.Contains("vegetarian", StringComparison.OrdinalIgnoreCase))
        {
            return "How about a delicious vegetarian stir-fry with tofu and vegetables?";
        }
        else if (preference.Contains("low-carb", StringComparison.OrdinalIgnoreCase))
        {
            return "How about a grilled chicken salad with a variety of fresh greens?";
        }
        else
        {
            return "How about a classic spaghetti Bolognese with a side of garlic bread?";
        }
    }

    [KernelFunction("suggest_snack")]
    public string SuggestSnack()
    {
        // Simulate suggesting a snack
        return "How about some fresh fruit or a handful of nuts for a healthy snack?";
    }
}

Explanation of the Multi-Agent System

1. FoodAgent

The FoodAgent class handles tasks related to food information and calorie content. It has two functions:

2. MealSuggestionAgent

The MealSuggestionAgent class handles tasks related to meal and snack suggestions. It has two functions:

3. Main Program

The main program remains largely the same, but now it includes the addition of the FoodAgent and MealSuggestionAgent plugins:

kernel.Plugins.AddFromType<FoodAgent>(“FoodAgent”);

kernel.Plugins.AddFromType<MealSuggestionAgent>(“MealSuggestionAgent”);

Behind the screen:

When you run the program, it will behave as a multi-agent chatbot that can handle different tasks related to food information and meal suggestions. Here’s what you can expect:

  1. Prompt for Input: The program will continuously prompt you to enter a message by displaying “User > ” in the console.
  2. Process Input: After you type a message and press Enter, the program will process your input to determine the appropriate response based on the defined functions in the FoodAgent and MealSuggestionAgent.
  3. Invoke Functions: Depending on your input, the program will invoke the relevant function from the appropriate agent. For example:
    • If you ask for food information, it will use the GetFoodInfo function from the FoodAgent.
    • If you ask for a meal suggestion, it will use the SuggestMeal function from the MealSuggestionAgent.
  4. Respond: The chatbot will respond with the output from the invoked function. For example:
    • If you type “Tell me about apples”, it might respond with “The food apple is rich in vitamins and minerals.”
    • If you type “Suggest a vegetarian meal”, it might respond with “How about a delicious vegetarian stir-fry with tofu and vegetables?”
  5. Loop: This process will repeat, allowing you to continue the conversation and ask different questions until you enter an empty message (just press Enter without typing anything), which will break the loop and end the program.

Sample Interaction

Here’s a sample interaction to illustrate the behavior:

User > Tell me about apples

Assistant > The food apple is rich in vitamins and minerals.

User > How many calories are in a banana?

Assistant > The food banana contains approximately 200 calories per serving.

User > Suggest a vegetarian meal

Assistant > How about a delicious vegetarian stir-fry with tofu and vegetables?

User > Suggest a snack

Assistant > How about some fresh fruit or a handful of nuts for a healthy snack?

User >

In this interaction, the chatbot uses the FoodAgent to provide information about apples and bananas, and the MealSuggestionAgent to suggest a vegetarian meal and a snack.

Feel free to try different inputs and see how the chatbot responds based on the defined functions. If you have any other questions or need further assistance, just let me know!

Understand specific user query:

If you ask the chatbot “What should I eat right now?”, the program will process your input and determine which function to invoke based on the available agents and their functions.

Since this input is related to meal suggestions, the chatbot will likely use the MealSuggestionAgent to provide a response.

Here’s how the program might handle this input:

  1. User Input: “What should I eat right now?”
  2. Determine Intent: The program recognizes that this input is related to meal suggestions.
  3. Invoke Function: The program invokes the SuggestMeal function from the MealSuggestionAgent.
  4. Generate Response: The SuggestMeal function provides a meal suggestion based on the default logic or any specific preferences mentioned in the input.

Sample Response

Given the current implementation, the response might be something like:

User > What should I eat right now?

Assistant > How about a classic spaghetti Bolognese with a side of garlic bread?

If you want the chatbot to provide more personalized suggestions based on specific preferences, you can enhance the SuggestMeal function to handle different scenarios.

For example, you could add logic to check for keywords like “vegetarian” or “low-carb” in the user’s input and provide appropriate suggestions.

Conclusion

Creating AI agents with the Semantic Kernel SDK is a powerful way to leverage the capabilities of large language models in your applications. By following the steps outlined in this document, you can get started with SK and you can build reliable, scalable, and secure AI agents that can handle various tasks and provide valuable insights.

The integration of LLMs with traditional programming languages simplifies the development process, making it accessible to developers with different levels of expertise.

Whether you’re building a simple chatbot or a complex multi-agent system, the Semantic Kernel provides the tools and flexibility needed to bring your AI solutions to life.

Call to Action: 

Connect with me on LinkedIn Rajeev Singh | LinkedIn and be sure to like, comment, and share this post to help spread the knowledge!

What’s Next:

Feel free to explore the additional readings and labs listed below. I will be publishing more articles on this topic in the future.

Guided project – Create an AI travel agent – Training | Microsoft Learn

Semantic Kernel Samples:

semantic-kernel/python/samples at main · microsoft/semantic-kernel

Semantic Kernel Demo Applications:

semantic-kernel/python/samples/demos at main · microsoft/semantic-kernel

Guest Blog: Building Multi-Agent Systems with Multi-Models in Semantic Kernel – Part 1 | Semantic Kernel

References:

https://learn.microsoft.com/en-us/azure/well-architected/ai/get-started

https://learn.microsoft.com/en-us/azure/well-architected/ai/application-design

https://learn.microsoft.com/en-us/azure/cloud-adoption-framework/scenarios/ai/platform/resource-selection

https://techcommunity.microsoft.com/blog/azure-ai-services-blog/introducing-azure-ai-agent-service/4298357

https://learn.microsoft.com/en-us/training/modules/build-your-kernel/2-what-semantic-kernel

https://learn.microsoft.com/en-us/semantic-kernel/get-started/quick-start-guide?pivots=programming-language-csharp

Exit mobile version