├── MultiAIAgentSystemExample.ipynb └── README.md /MultiAIAgentSystemExample.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": { 7 | "dotnet_interactive": { 8 | "language": "csharp" 9 | }, 10 | "polyglot_notebook": { 11 | "kernelName": "csharp" 12 | } 13 | }, 14 | "outputs": [], 15 | "source": [ 16 | "#r \"nuget: Microsoft.SemanticKernel.Agents.Abstractions, 1.14.1-alpha\"\n", 17 | "#r \"nuget: Microsoft.SemanticKernel.Agents.Core, 1.14.1-alpha\"\n", 18 | "#r \"nuget: Microsoft.SemanticKernel.Agents.OpenAI, 1.14.1-alpha\"\n", 19 | "#r \"nuget: Microsoft.SemanticKernel.Connectors.OpenAI, 1.14.1-alpha\"\n" 20 | ] 21 | }, 22 | { 23 | "cell_type": "code", 24 | "execution_count": null, 25 | "metadata": { 26 | "dotnet_interactive": { 27 | "language": "csharp" 28 | }, 29 | "polyglot_notebook": { 30 | "kernelName": "csharp" 31 | } 32 | }, 33 | "outputs": [], 34 | "source": [ 35 | "using System.ComponentModel;\n", 36 | "using Microsoft.SemanticKernel;\n", 37 | "using Microsoft.SemanticKernel.Agents;\n", 38 | "using Microsoft.SemanticKernel.Agents.OpenAI;\n", 39 | "using Microsoft.SemanticKernel.ChatCompletion;\n", 40 | "using Microsoft.SemanticKernel.Connectors.OpenAI;\n", 41 | "using Microsoft.SemanticKernel.Agents.Chat;" 42 | ] 43 | }, 44 | { 45 | "cell_type": "code", 46 | "execution_count": null, 47 | "metadata": { 48 | "dotnet_interactive": { 49 | "language": "csharp" 50 | }, 51 | "polyglot_notebook": { 52 | "kernelName": "csharp" 53 | } 54 | }, 55 | "outputs": [], 56 | "source": [ 57 | "var OaiApiKey = \"\";" 58 | ] 59 | }, 60 | { 61 | "cell_type": "code", 62 | "execution_count": null, 63 | "metadata": { 64 | "dotnet_interactive": { 65 | "language": "csharp" 66 | }, 67 | "polyglot_notebook": { 68 | "kernelName": "csharp" 69 | } 70 | }, 71 | "outputs": [], 72 | "source": [ 73 | "Kernel kernel = Kernel.CreateBuilder()\n", 74 | " .AddOpenAIChatCompletion(\n", 75 | " modelId: \"gpt-4o\",\n", 76 | " apiKey: OaiApiKey)\n", 77 | " .Build();\n" 78 | ] 79 | }, 80 | { 81 | "cell_type": "code", 82 | "execution_count": null, 83 | "metadata": { 84 | "dotnet_interactive": { 85 | "language": "csharp" 86 | }, 87 | "polyglot_notebook": { 88 | "kernelName": "csharp" 89 | } 90 | }, 91 | "outputs": [], 92 | "source": [ 93 | "// Lets given the Persona to Agents\n", 94 | "\n", 95 | "string ProgamManager = \"\"\"\n", 96 | " You are a program manager which will take the requirement and create a plan for creating app. Program Manager understands the \n", 97 | " user requirements and form the detail documents with requirements and costing. \n", 98 | "\"\"\";\n", 99 | "\n", 100 | "string SoftwareEngineer = \"\"\"\n", 101 | " You are Software Engieer, and your goal is create web app using HTML and JavaScript by taking into consideration all\n", 102 | " the requirements given by Program Manager. \n", 103 | "\"\"\";\n", 104 | "\n", 105 | "string ProjectManager = \"\"\"\n", 106 | " You are manager which will review software engineer code, and make sure all client requirements are completed. \n", 107 | " You are the guardian of quality, ensuring the final product meets all specifications and receives the green light for release.\n", 108 | " Once all client requirements are completed, you can approve the request by just responding \"approve\"\n", 109 | "\"\"\";\n" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": { 116 | "dotnet_interactive": { 117 | "language": "csharp" 118 | }, 119 | "polyglot_notebook": { 120 | "kernelName": "csharp" 121 | } 122 | }, 123 | "outputs": [], 124 | "source": [ 125 | "#pragma warning disable SKEXP0110, SKEXP0001 // Rethrow to preserve stack details\n", 126 | "\n", 127 | " ChatCompletionAgent ProgaramManagerAgent =\n", 128 | " new()\n", 129 | " {\n", 130 | " Instructions = ProgamManager,\n", 131 | " Name = \"ProgaramManagerAgent\",\n", 132 | " Kernel = kernel\n", 133 | " };\n", 134 | "\n", 135 | " ChatCompletionAgent SoftwareEngineerAgent =\n", 136 | " new()\n", 137 | " {\n", 138 | " Instructions = SoftwareEngineer,\n", 139 | " Name = \"SoftwareEngineerAgent\",\n", 140 | " Kernel = kernel\n", 141 | " };\n", 142 | "\n", 143 | " ChatCompletionAgent ProjectManagerAgent =\n", 144 | " new()\n", 145 | " {\n", 146 | " Instructions = ProjectManager,\n", 147 | " Name = \"ProjectManagerAgent\",\n", 148 | " Kernel = kernel\n", 149 | " };\n", 150 | "\n" 151 | ] 152 | }, 153 | { 154 | "cell_type": "code", 155 | "execution_count": null, 156 | "metadata": { 157 | "dotnet_interactive": { 158 | "language": "csharp" 159 | }, 160 | "polyglot_notebook": { 161 | "kernelName": "csharp" 162 | } 163 | }, 164 | "outputs": [], 165 | "source": [ 166 | " #pragma warning disable SKEXP0110, SKEXP0001 // Rethrow to preserve stack details\n", 167 | " \n", 168 | " using System.Threading;\n", 169 | " \n", 170 | " private sealed class ApprovalTerminationStrategy : TerminationStrategy\n", 171 | " {\n", 172 | " // Terminate when the final message contains the term \"approve\"\n", 173 | " protected override Task ShouldAgentTerminateAsync(Agent agent, IReadOnlyList history, CancellationToken cancellationToken)\n", 174 | " => Task.FromResult(history[history.Count - 1].Content?.Contains(\"approve\", StringComparison.OrdinalIgnoreCase) ?? false);\n", 175 | " }" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": null, 181 | "metadata": { 182 | "dotnet_interactive": { 183 | "language": "csharp" 184 | }, 185 | "polyglot_notebook": { 186 | "kernelName": "csharp" 187 | } 188 | }, 189 | "outputs": [], 190 | "source": [ 191 | "#pragma warning disable SKEXP0110, SKEXP0001 \n", 192 | "\n", 193 | " AgentGroupChat chat =\n", 194 | " new(ProgaramManagerAgent, SoftwareEngineerAgent, ProjectManagerAgent)\n", 195 | " {\n", 196 | " ExecutionSettings =\n", 197 | " new()\n", 198 | " {\n", 199 | " TerminationStrategy =\n", 200 | " new ApprovalTerminationStrategy()\n", 201 | " {\n", 202 | " Agents = [ProjectManagerAgent],\n", 203 | " MaximumIterations = 6,\n", 204 | " }\n", 205 | " }\n", 206 | " };\n" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": null, 212 | "metadata": { 213 | "dotnet_interactive": { 214 | "language": "csharp" 215 | }, 216 | "polyglot_notebook": { 217 | "kernelName": "csharp" 218 | } 219 | }, 220 | "outputs": [], 221 | "source": [ 222 | " #pragma warning disable SKEXP0110, SKEXP0001 \n", 223 | " // Invoke chat and display messages.\n", 224 | " string input = \"\"\"\n", 225 | " \n", 226 | " I want to develop app which will provide me calculator. Keep it very simple. And get final approval from manager.\n", 227 | " \"\"\";\n", 228 | "\n", 229 | " chat.AddChatMessage(new ChatMessageContent(AuthorRole.User, input));\n", 230 | " Console.WriteLine($\"# {AuthorRole.User}: '{input}'\");\n", 231 | "\n", 232 | " await foreach (var content in chat.InvokeAsync())\n", 233 | " {\n", 234 | " Console.WriteLine($\"# {content.Role} - {content.AuthorName ?? \"*\"}: '{content.Content}'\");\n", 235 | " }" 236 | ] 237 | }, 238 | { 239 | "cell_type": "code", 240 | "execution_count": null, 241 | "metadata": { 242 | "dotnet_interactive": { 243 | "language": "csharp" 244 | }, 245 | "polyglot_notebook": { 246 | "kernelName": "csharp" 247 | } 248 | }, 249 | "outputs": [], 250 | "source": [] 251 | } 252 | ], 253 | "metadata": { 254 | "kernelspec": { 255 | "display_name": ".NET (C#)", 256 | "language": "C#", 257 | "name": ".net-csharp" 258 | }, 259 | "language_info": { 260 | "name": "polyglot-notebook" 261 | }, 262 | "polyglot_notebook": { 263 | "kernelInfo": { 264 | "defaultKernelName": "csharp", 265 | "items": [ 266 | { 267 | "aliases": [], 268 | "name": "csharp" 269 | } 270 | ] 271 | } 272 | } 273 | }, 274 | "nbformat": 4, 275 | "nbformat_minor": 2 276 | } 277 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AI Multi-Agent System with Microsoft Semantic Kernel and GPT-4o 2 | 3 | This repository contains the example code from the Medium blog post titled [Step-by-Step Guide to Develop AI Multi-Agent System Using Microsoft Semantic Kernel and GPT-4](https://medium.com/@akshaykokane09/step-by-step-guide-to-develop-ai-multi-agent-system-using-microsoft-semantic-kernel-and-gpt-4o-f5991af40ea6). The example demonstrates how to create an AI multi-agent system using .NET8 and Semantic Kernel SDK. 4 | --------------------------------------------------------------------------------