Featured image of post How to Create Your First Agent with the Agentic Framework in Semantic Kernel and .NET

How to Create Your First Agent with the Agentic Framework in Semantic Kernel and .NET

In this step-by-step tutorial, you'll learn how to create our first AI Agent using Microsoft Semantic Kernel's Agentic Framework and .NET.

This tutorial shows you how to create a summarization agent using the latest Agentic Framework in Microsoft Semantic Kernel with just a few lines of code.


๐Ÿง  What You’ll Learn

  • Set up your first summarization agent
  • Use OpenAI’s GPT-4o via Semantic Kernel
  • Input user content and return summarized output

๐Ÿ“ฆ Required NuGet Packages

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

๐Ÿ› ๏ธ Full Working Example

Below is a complete code snippet to create a text summarization agent using ChatCompletionAgent. This example is optimized for developers looking to get started quickly with the Semantic Kernel Agentic Framework in C#.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;

// Step 1: Create a kernel builder instance
IKernelBuilder builder = Kernel.CreateBuilder();

// Step 2: Register OpenAI GPT-4o as your chat completion model
builder.AddOpenAIChatCompletion(
    modelId: "gpt-4o",
    apiKey: "<your-api-key>",
    serviceId: "OpenAI"
);

// Step 3: Build the kernel
Kernel kernel = builder.Build();

// Step 4: Create the ChatCompletionAgent
ChatCompletionAgent agent = new()
{
    Name = "SummarizationAgent", // The agent's identifier
    Instructions = "Summarize user input", // Prompt instructions
    Kernel = kernel, // Assign the built kernel
    Arguments = new KernelArguments(
        new OpenAIPromptExecutionSettings
        {
            ServiceId = "OpenAI" // Tell the agent which model to use
        })
};

// Step 5: Interact with the agent
Console.WriteLine("Enter text to summarize:");
string input = Console.ReadLine();

Console.WriteLine("Summarizing...");

// Step 6: Stream the summarized response from the LLM
await foreach (ChatMessageContent response in agent.InvokeAsync(new ChatMessageContent(AuthorRole.User, input)))
{
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine(response.Content);
}

Console.ResetColor();
Console.ReadLine();

Console.WriteLine("Summarizing...");

await foreach (ChatMessageContent response in agent.InvokeAsync(new ChatMessageContent(AuthorRole.User, input)))
{
    Console.ForegroundColor = ConsoleColor.Green;
    Console.WriteLine(response.Content);
}

Console.ResetColor();
Console.ReadLine();

โœ… Example Output

Input:

1
Semantic Kernel is an SDK from Microsoft that lets you build AI-first apps using LLMs, plugins, and planners.

Output:

1
Semantic Kernel is Microsoft's SDK for building AI-first applications using large language models and tools like planners and plugins.

๐Ÿ“š Summary

Step Description
1๏ธโƒฃ Set up the kernel and register OpenAI completion model
2๏ธโƒฃ Instantiate ChatCompletionAgent with name and instructions
3๏ธโƒฃ Accept user input and stream summarized response

โ“ Frequently Asked Questions (FAQs)

๐Ÿค” What is a ChatCompletionAgent in Semantic Kernel?

A ChatCompletionAgent is a built-in class in the Semantic Kernel Agentic Framework that simplifies the creation of agents without needing to implement the IAgent interface manually. You just specify the name, instructions, kernel, and LLM settings, and it takes care of the rest.

โš™๏ธ Do I need to implement IAgent manually to build agents?

No. If your use case is simple and fits a prompt-based model like summarization or Q&A, ChatCompletionAgent is perfect. However, for multi-step logic, memory management, or agent collaboration, a custom IAgent implementation is more suitable.

๐Ÿ’ก Can I use Azure OpenAI instead of OpenAI?

Yes. Simply use the AddAzureOpenAIChatCompletion method instead of AddOpenAIChatCompletion, and provide Azure-specific parameters like deployment name and endpoint.

๐Ÿ“Œ How is this different from plugins?

Plugins are reusable, declarative functions exposed to the AI model for reasoning and execution. Agents can call these plugins to perform actions. In this post, we focus on using the chat model directly.

๐Ÿ” How secure is this setup?

Security depends on how you store and manage your OpenAI keys. Use UserSecrets or Azure Key Vault for secure storage instead of hardcoding keys in code.

๐Ÿš€ How do I deploy this?

You can containerize the app and deploy it using Azure App Services, Kubernetes, or any serverless host like Azure Functions or AWS Lambda.

๐Ÿ”— Learn More

comments powered by Disqus