Skip to content

Instantly share code, notes, and snippets.

@luisquintanilla
Created March 11, 2025 19:45
Show Gist options
  • Save luisquintanilla/e8a48d47abfa7f427e8b7ef0413501a6 to your computer and use it in GitHub Desktop.
Save luisquintanilla/e8a48d47abfa7f427e8b7ef0413501a6 to your computer and use it in GitHub Desktop.
Using Microsoft.Extensions.AI and Semantic Kernel Agents with GitHub Models
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div><div></div><div></div><div><strong>Installed Packages</strong><ul><li><span>Microsoft.Extensions.AI, 9.3.0-preview.1.25114.11</span></li><li><span>Microsoft.Extensions.AI.AzureAIInference, 9.3.0-preview.1.25114.11</span></li><li><span>Microsoft.SemanticKernel.Agents.Core, 1.40.1-preview</span></li></ul></div></div>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"#r \"nuget: Microsoft.Extensions.AI.AzureAIInference, 9.3.0-preview.1.25114.11\"\n",
"#r \"nuget: Microsoft.Extensions.AI, 9.3.0-preview.1.25114.11\"\n",
"#r \"nuget: Microsoft.SemanticKernel.Agents.Core, 1.40.1-preview\""
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"using Azure;\n",
"using Azure.AI.Inference;\n",
"using Microsoft.Extensions.AI;\n",
"using Microsoft.SemanticKernel;\n",
"using Microsoft.SemanticKernel.Agents;\n",
"using Microsoft.SemanticKernel.ChatCompletion;\n",
"using Microsoft.Extensions.DependencyInjection;\n",
"using ChatRole = Microsoft.Extensions.AI.ChatRole;"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"IChatClient chatClient = \n",
" new ChatCompletionsClient(\n",
" new Uri(\"https://models.inference.ai.azure.com\"), \n",
" new AzureKeyCredential(Environment.GetEnvironmentVariable(\"GH_TOKEN\")))\n",
" .AsChatClient(\"gpt-4o-mini\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Extensions.AI"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"List<ChatMessage> chatHistory = [\n",
" new ChatMessage(ChatRole.System, \"You are an AI assistant that helps users summarize text.\")\n",
"];"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"var userInput = \n",
" \"\"\"\n",
" An AI agent is a software entity designed to perform tasks autonomously or semi-autonomously by recieving input, processing information, and taking actions to achieve specific goals.\n",
"\n",
" Agents can send and receive messages, generating responses using a combination of models, tools, human inputs, or other customizable components.\n",
"\n",
" Agents are designed to work collaboratively, enabling complex workflows by interacting with each other. The Agent Framework allows for the creation of both simple and sophisticated agents, enhancing modularity and ease of maintenance\n",
" \"\"\";"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"chatHistory.Add(new ChatMessage(ChatRole.User, userInput));"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"var response = await chatClient.GetResponseAsync(chatHistory);"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/plain": [
"An AI agent is a software entity that performs tasks autonomously or semi-autonomously by processing input and taking actions to achieve specific goals. These agents can communicate with each other and generate responses using various models and tools. They are designed to collaborate, facilitating complex workflows, and the Agent Framework supports the development of both simple and advanced agents, improving modularity and maintenance."
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"response.Message.Text"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Semantic Kernel"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#pragma warning disable SKEXP0001 //\n",
"var builder = Kernel.CreateBuilder();\n",
"builder.Services.AddTransient<IChatCompletionService>(_ => chatClient.AsChatCompletionService());\n",
"#pragma warning enable SKEXP0001 //"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"var kernel = builder.Build();"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"ChatCompletionAgent agent = new ()\n",
"{\n",
" Name = \"SummarizerAgent\",\n",
" Description = \"Summarize user input\",\n",
" Kernel = kernel\n",
"};"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"ChatHistory chatHistory = [\n",
" new ChatMessageContent(AuthorRole.User, userInput)\n",
"];"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Your description provides a clear overview of AI agents, highlighting their capabilities and functions. Here are some key points that could enhance your explanation:\n",
"\n",
"1. **Types of AI Agents**: You might want to categorize agents based on their capabilities. For example, you could mention reactive agents that respond to immediate inputs, deliberative agents that plan their actions based on goals, and hybrid agents that combine both approaches.\n",
"\n",
"2. **Communication Protocols**: Elaborate on how agents communicate with each other. This might include message formats, standards, or protocols that ensure seamless interaction.\n",
"\n",
"3. **Decision-Making Process**: You could discuss how agents make decisions based on input data. For instance, do they use rules, heuristics, machine learning models, or a combination of these methods?\n",
"\n",
"4. **Training and Learning**: Mention how agents can improve their performance over time through learning from interactions and experiences, such as reinforcement learning or supervised learning techniques.\n",
"\n",
"5. **Applications**: Providing examples of specific applications of AI agents in different industries (e.g., customer service bots, autonomous vehicles, personal assistants, etc.) can illustrate their practical utility.\n",
"\n",
"6. **Ethical Considerations**: Addressing the ethical implications of using AI agents, such as transparency, accountability, and potential biases, could enhance the discussion of their deployment and impact.\n",
"\n",
"7. **Future Trends**: Briefly touch on potential future developments in AI agents, such as advancements in natural language processing, decision-making capabilities, or integration into IoT ecosystems.\n",
"\n",
"By incorporating these elements, your explanation will offer a more comprehensive understanding of AI agents and their significance in various contexts."
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"await foreach(ChatMessageContent message in agent.InvokeAsync(chatHistory))\n",
"{\n",
" message.Content.Display();\n",
"}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [],
"languageName": "csharp",
"name": "csharp"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment