AI  

Step-by-Step Guide to Building AI Agents with Semantic Kernel

In this article, we’ll explore how to build intelligent AI agents using Azure OpenAI and semantic kernels (Microsoft C# SDK). You can combine it with OpenAI, Azure OpenAI, Hugging Face, or any other model. We’ll cover the fundamentals, dive into implementation details, and provide practical code examples in C#. Whether you’re a beginner or an experienced developer, this guide will help you harness the power of AI for your applications.

What is Semantic Kernel?

In Kevin Scott's talk on "The era of the AI copilot," he showcased how Microsoft's Copilot system uses a mix of AI models and plugins to enhance user experiences. At the core of this setup is an AI orchestration layer, which allows Microsoft to combine these AI components to create innovative features for users. For developers looking to develop their own copilot-like experiences using AI plugins, Microsoft has introduced the Semantic kernel.

Semantic Kernel is an open-source framework that enables developers to build intelligent agents by providing a standard interface for various AI models and algorithms. The Semantic Kernel SDK allows you to integrate the power of large language models (LLMs) in your own applications. The Semantic Kernel SDK enables developers to incorporate prompts with LLMs in their applications, potentially creating their own copilot-like experiences. It allows developers to focus on building intelligent applications without worrying about the underlying complexities of AI models. Semantic Kernel is built on top of the .NET ecosystem and provides a robust and scalable platform for building intelligent apps/agents.

Copilot

Key Features of Semantic Kernel

  • Modular architecture: The Semantic Kernel features a modular architecture that enables developers to integrate new AI models and algorithms easily.
  • Knowledge graph: Semantic Kernel provides a built-in knowledge graph that enables developers to store and query complex relationships between entities.
  • Machine learning: Semantic Kernel supports various machine learning algorithms, including classification, regression, and clustering.
  • Natural language processing: Semantic Kernel provides natural language processing capabilities, including text analysis and sentiment analysis.
  • Integration with external services: Semantic Kernel allows developers to integrate with external services, such as databases and web services.

Let's dive deep into writing some intelligent code using the Semantic kernel C# SDK. I will write them in steps so it will be easy to follow along.

Step 1. Setting up the Environment

Let's set up our environment. You will need to install the following to follow along.

  • .NET 8 or later
  • Semantic Kernel SDK (available on NuGet)
  • Your preferred IDE (Visual Studio, Visual Studio Code, etc.)
  • Azure OpenAI access

Step 2. Creating a New Project in VS

Open Visual Studio and create a blank, empty .NET 8 console Application.

New project

Step 3. Install NuGet References

  • Right-click on the project --> click on Manage NuGet reference section to install the following two latest NuGet packages.
  • Microsoft.SemanticKernel
  • Microsoft.Extensions.Configuration.json

Note. To avoid hardcoding the Azure OpenAI key and endpoint, I store them as key-value pairs in appsettings.json. Using the #2 package, I can easily retrieve them based on the key.

Step 4. Create and Deploy an Azure OpenAI Model

Once you have obtained access to the Azure OpenAI service, log in to the Azure portal or Azure OpenAI studio to create an Azure OpenAI resource. The screenshots below are from the Azure portal.

OpenAI

Azure

You can also create an Azure OpenAI service resource using Azure CLI by running the following command.

az cognitiveservices account create \
  -n <nameoftheresource> \
  -g <Resourcegroupname> \
  -l <location> \
  --kind OpenAI \
  --sku s0 \
  --subscription <subscriptionID>

You can see your resource from Azure OpenAI studio as well by navigating to this page and selecting the resource that was created from.

OpenAI Studio

AI Studio

Deploy a Model

Azure OpenAI includes several types of base models, as shown in the studio when you navigate to the Deployments tab. You can also create your own custom models by using existing base models as per your requirements.

Model

Let's use the deployed GPT-35-turbo model and see how to consume it in the Azure OpenAI studio. Fill in the details and click Create.

Create

Once the model is deployed, grab the Azure OpenAI key and endpoint to paste them inside the appsettings.json file as shown below.

Appsettings

Step 5. Create a Kernel in the Code

Create kernel

Step 6. Create a Plugin to Call the Azure OpenAI Model

Plugin

Step 7. Use Kernel To Invoke the LLM Models

LLM Models

Once you run the program by pressing F5, you will see the response generated from the Azure OpenAI model.

Complete Code

using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;

var config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .Build();

var builder = Kernel.CreateBuilder();

builder.Services.AddAzureOpenAIChatCompletion(
    deploymentName: config["AzureOpenAI:DeploymentModel"] ?? string.Empty,
    endpoint: config["AzureOpenAI:Endpoint"] ?? string.Empty,
    apiKey: config["AzureOpenAI:ApiKey"] ?? string.Empty
);

var semanticKernel = builder.Build();

Console.WriteLine(
    await semanticKernel.InvokePromptAsync("Give me shopping list for cooking Sushi")
);

Conclusion

By combining AI LLM models with semantic kernels, you’ll create intelligent applications that go beyond simple keyword matching. Experiment, iterate, and keep learning to build remarkable apps that truly understand and serve your needs.