Create an Agentic AI solution with Semantic Kernel

We are finally at a state in the GenAI-space where we can create agentic AI solutions with ease.
I’m most familiar with Semantic Kernel, when working with LLMs, and this library works great for creating these solutions.

In a nutshell, what you need to do is create a group chat, add your agents to it, and then let them work together to solve a problem.
Do keep in mind, at the time of this writing, version 1.58.0 of the Semantic Kernel library is used. Development is going fast in this space, so behavior might change in future versions.
For my proof of concept, I’ve created a simple solution capable of creating a Fibonacci sequence, validate if a number is part of that sequence or answer random questions you would also ask to a regular LLM-powered chatbot. If you’re interested, the full sourcecode can be found on GitHub in my console-agent repository.

Create the agents

Agents are created with the ChatCompletionAgent class. You should provide the instructions & the kernel to use.
The instructions is just a regular prompt we all know and love when working with an LLM and specifies what the agent should do. I won’t repeat it over here as it’s not relevant for this post.

new ChatCompletionAgent
{
    Name = "FibonacciGenerator",
    Instructions = FibonacciAgentInstructions.GeneratorInstructions,
    Kernel = kernel,
    Arguments = new KernelArguments()
};

When using YAML to define the agents, they can be loaded easily with the CreateAgentFromYamlAsync method.

var agentFactory = new ChatCompletionAgentFactory();
var agent = await agentFactory.CreateAgentFromYamlAsync(agentDefinition, new() { Kernel = kernel });

I’d advise using the YAML definitions, as it makes it easier to maintain and update the agents.

Add tools to your kernel

An LLM is quite capable of generating text, based on the dataset it has been trained on or has been provided to it. However, it’s not very good at math, for example. To solve this, we can add tools to the kernel that the agents can use to perform specific tasks.
Custom tools can be added to the kernel-object. With this, agents can use the tools to perform their tasks, if they’re allowed to do so.

// Add plugins for Fibonacci operations
var fibonacciPlugin = KernelPluginFactory.CreateFromType<FibonacciPlugin>();
builder.Plugins.Add(fibonacciPlugin);

For this to work, the methods in this class do need some attributes, so kernel and detect how and when to invoke them.

/// <summary>
/// Generates the first N numbers of the Fibonacci sequence
/// </summary>
/// <param name="count">Number of Fibonacci numbers to generate</param>
/// <returns>Array of Fibonacci numbers</returns>
[KernelFunction]
[Description("Generates the first N numbers of the Fibonacci sequence")]
public int[] GenerateFibonacci(
    [Description("The number of Fibonacci numbers to generate")] int count)
{
    // The implementation
}

Providing correct descriptions is the most important part over here.

The tools parameter in your agent defintion defines which tools the agent can use.

  type: chat_completion_agent
  name: FibonacciGenerator
  description: Specialist agent for generating Fibonacci sequences
  instructions: |
        ...
tools:
  - id: GenerateFibonacci
    type: function
    description: Generates the first N numbers of the Fibonacci sequence
    options:
      parameters:
        - name: count
          type: integer
          required: true
          description: The number of Fibonacci numbers to generate

Create the group chat

To use the agents, you need a group chat where all participants are added, the agents and yourself.
With the current release of Semantic Kernel, this again is quite easily done.

// Create an AgentGroupChat with all agents
var agentGroupChat = new AgentGroupChat(generatorAgent, validatorAgent, generalAgent)
{
    ExecutionSettings = new()
    {
        // Terminate after each agent responds once to give a complete answer
        TerminationStrategy = new FibonacciTerminationStrategy()
        {
            MaximumIterations = 10, // Allow up to 10 interactions between agents
            AutomaticReset = true // Reset after each user question
        }
    }
};

As you can see, the AgentGroupChat is created with the agents you want to use.

Start the conversation

Now that we have the agents and the group chat, we can start the conversation. In this example, I will ask a question in the group chat and let them collaborate to answer it.

// Add the user message to the chat
agentGroupChat.AddChatMessage(new ChatMessageContent(AuthorRole.User, question));

Console.WriteLine();

// Let the agents collaborate to answer the question
await foreach (var response in agentGroupChat.InvokeAsync())
{
    var content = response.Content ?? "";
    var agentName = response.AuthorName ?? "Agent";

    // Add emoji based on agent name for better UX
    var emoji = agentName.Contains("Generator") ? "🔢" :
                agentName.Contains("Validator") ? "✅" :
                agentName.Contains("GeneralAssistant") ? "🤖" : "💬";

    if (!string.IsNullOrEmpty(content))
    {
        Console.WriteLine($"{agentName} {emoji}: {content}");
    }
}

Of course, you can make this as advanced as you want, this is just a simple example to get started.

Bringing it all together

Running the console application with the group chat inside it will provide you with a nice chat experience with the agents working together to answer your questions.
A sample conversation I had with the agents looks like this:

You: Is 4 a number in the fibonacci sequence?

FibonacciGenerator ??: Let me check if 4 is a Fibonacci number.

If we look at the beginning of the Fibonacci sequence:
0, 1, 1, 2, 3, 5, 8, ...

You'll notice that 4 is not present in this list. So, **4 is not a Fibonacci number**.

The numbers before and after 4 in the sequence are 3 and 5. If you have other numbers in mind, feel free to ask if they're part of the Fibonacci sequence!
FibonacciValidator ?: Let me check with the rule for Fibonacci numbers:

The sequence starts: 0, 1, 1, 2, 3, 5, 8, 13, ...

As you can see, 4 does **not** appear in the Fibonacci sequence.

So, **4 is not a Fibonacci number**. If you want to check another number, just ask!
You: What are the first 4 numbers in the sequence?

GeneralAssistant ??: That's a great question about Fibonacci numbers! Let me have my Fibonacci specialists handle that for you.
FibonacciGenerator ??: The first 4 numbers in the Fibonacci sequence are:

0, 1, 1, 2

Here's how it works:
- It starts with 0 and 1.
- Each new number is the sum of the two before it.

So, after 0 and 1:
0 + 1 = 1
1 + 1 = 2

If you want more numbers, just let me know!
You: Who is the president of the USA?

FibonacciGenerator ??: That's not a Fibonacci question, but our GeneralAssistant can help you with that! Would you like me to redirect your question to the GeneralAssistant?
FibonacciValidator ?: That's not a Fibonacci question, but our GeneralAssistant can help you with that! If you'd like, I can redirect your question for an accurate and up-to-date answer.
You: Yes please

GeneralAssistant ??: As of June 2024, the President of the United States is Joe Biden. If you have more questions about U.S. leadership or politics, feel free to ask!

As you can see, the agents are working together to answer the questions.
As mentioned, the code is available on GitHub (https://github.com/Jandev/console-agent), so you can run it yourself and play around with it.


Share