Skip to content

Commit

Permalink
Add Semantic Kernel sample projects from build 2024 (#69)
Browse files Browse the repository at this point in the history
* Add hello semantic-keren project

* Add Chat history project

* Add  Adding Plugins (Function Calls)

* Add plugins(Being search)

* clean name

* Add injecting servies sample project

* Add  Inject kerenel  To WebApp.

* Rename and clean project folders

* Fix spelling/naming of project

* Being -> Bing for API Key

* Separate out the introduction of logging and cleaned up some naming

* Update naming for 06 and 07 projects.

* Update README.md

Review update

* Update Program.cs

Review fix

* review fix

* Clean

* disbaled warning from project

* Update readme to have links into youtube

* Fix ordering of projects to match video.

* Update github links

* Try escaping parenthesis

* Try dashes

* Try encoding urls

* Check-in initial navigation and readme structure

* update readme

* clean markdown syntax

* update readme of each project

* Move docs to docs folder instead of the individual projects.

* Update  exercise files

* clean 5 to 7

* Clean links

* Clean formatting

* Clean grammer and spellings

* Build based on priviuse project

* Prerequisites of OpenAI

* Clean

* clean some inconsistency

* Review fixes

* clean

* Add readme

* Add comment about redaction service.

* Update README.md

* update doc

* Add some more docs

* Update README.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update README.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/04 Add Logging.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/05 Add Plugin (Bing Search).md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/06 Modifying Kernel Behavior with Dependency Injection.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/07 Using Semantic Kernel in WebApp.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/06 Modifying Kernel Behavior with Dependency Injection.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* update version

* Clean concept notes

* Update src/build-2024/docs/Exercise/01 Hello Semantic Kernel.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/01 Hello Semantic Kernel.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/04 Add Logging.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/01 Hello Semantic Kernel.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/02 Add Chat History.md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/03 Add Plugin (Function Call).md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/03 Add Plugin (Function Call).md

Co-authored-by: Jordan Matthiesen <[email protected]>

* Update src/build-2024/docs/Exercise/02 Add Chat History.md

Co-authored-by: Jordan Matthiesen <[email protected]>

---------

Co-authored-by: Jake Radzikowski <[email protected]>
Co-authored-by: Jordan Matthiesen <[email protected]>
  • Loading branch information
3 people committed Aug 27, 2024
1 parent 0fd70fa commit 922a8cc
Show file tree
Hide file tree
Showing 35 changed files with 1,290 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ Discover how to bring AI into your .NET application! This session covers the too

[Youtube: Infusing your .NET Apps with AI: Practical Tools and Techniques](https://www.youtube.com/watch?v=jrNfKeGSuCg)

### Tutorial: Infusing your .NET Apps with AI: Practical Tools and Techniques

| # | Topic | YouTube Link and Time | GitHub Link |
|-----|--------------------------------------------------|----------------------------------------------------------------|-------------------------|
| 1 | Hello Semantic Kernel | [3m 0s](https://www.youtube.com/watch?v=jrNfKeGSuCg&t=180s) | [Hello Semantic Kernel](./src/build-2024/01%20-%20Hello%20Semantic%20Kernel) |
| 2 | Add Chat History | [5m 40s](https://www.youtube.com/watch?v=jrNfKeGSuCg&t=340s) | [Add Chat History](./src/build-2024/02%20-%20Add%20Chat%20History) |
| 3 | Add Plugin - Function Call | [7m 10s](https://www.youtube.com/watch?v=jrNfKeGSuCg&t=430s) | [Add Plugin - Function Call](./src/build-2024/03%20-%20Add%20Plugin%20%28Function%20Call%29) |
| 4 | Add Logging | [9m 24s](https://www.youtube.com/watch?v=jrNfKeGSuCg&t=565s) | [Add Logging](./src/build-2024/04%20-%20Add%20Logging) |
| 5 | Add Plugin - Bing Search | [11m 15s](https://www.youtube.com/watch?v=jrNfKeGSuCg&t=675s) | [Add Plugin - Bing Search](./src/build-2024/05%20-%20Add%20Plugin%20%28Bing%20Search%29) |
| 6 | Modify Kernel Behavior with Dependency Injection | [12m 37s](https://www.youtube.com/watch?v=jrNfKeGSuCg&t=757s) | [Modifying Kernel Behavior with Dependency Injection](./src/build-2024/06%20-%20Modifying%20Kernel%20Behavior%20with%20Dependency%20Injection) |
| 7 | Using Semantic Kernel in a Web App | [15m 57s](https://www.youtube.com/watch?v=jrNfKeGSuCg&t=957s) | [Using Semantic Kernel in a Web App](./src/build-2024/07%20-%20Using%20Semantic%20Kernel%20in%20WebApp) |

### Quickstarts using OpenAI

| # | Topic | GitHub Link |
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel" Version="1.15.0" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions src/build-2024/01 - Hello Semantic Kernel/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.SemanticKernel;

var openAIChatCompletionModelName = "gpt-3.5-turbo"; // this could be other models like "gpt-4o".
var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(openAIChatCompletionModelName, Environment.GetEnvironmentVariable("OPENAI_API_KEY")) // add the OpenAI chat completion service.
.Build();

// Basic chat
// This is zero memory or stateless chat. The AI will not remember anything from the previous messages.
while (true)
{
Console.Write("Q: ");
Console.WriteLine(await kernel.InvokePromptAsync(Console.ReadLine()!));
}
3 changes: 3 additions & 0 deletions src/build-2024/01 - Hello Semantic Kernel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Hello semantic kernel

A complete documentation for this project is [here](../docs/Exercise/01%20Hello%20Semantic%20Kernel.md)
11 changes: 11 additions & 0 deletions src/build-2024/02 - Add Chat History/02 - Add Chat History.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel" Version="1.15.0" />
</ItemGroup>
</Project>
20 changes: 20 additions & 0 deletions src/build-2024/02 - Add Chat History/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;

var openAIChatCompletionModelName = "gpt-3.5-turbo"; // this could be other models like "gpt-4o".
var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(openAIChatCompletionModelName, Environment.GetEnvironmentVariable("OPENAI_API_KEY")) // add the OpenAI chat completion service.
.Build();

var chatService = kernel.GetRequiredService<IChatCompletionService>();
ChatHistory chatHistory = [];

// Basic chat
while (true)
{
Console.Write("Q: ");
chatHistory.AddUserMessage(Console.ReadLine()); // Add user message to chat history.
var response = await chatService.GetChatMessageContentAsync(chatHistory); // Get chat response based on chat history.
Console.WriteLine(response); // Print response.
chatHistory.Add(response); // Add chat response to chat history
}
4 changes: 4 additions & 0 deletions src/build-2024/02 - Add Chat History/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# Add Chat History

A complete documentation for this project is [here](../docs/Exercise/02%20Add%20Chat%20History.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SemanticKernel" Version="1.15.0" />
</ItemGroup>
</Project>
42 changes: 42 additions & 0 deletions src/build-2024/03 - Add Plugin (Function Call)/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;

var openAIChatCompletionModelName = "gpt-3.5-turbo"; // this could be other models like "gpt-4o".

var kernel = Kernel.CreateBuilder()
.AddOpenAIChatCompletion(openAIChatCompletionModelName, Environment.GetEnvironmentVariable("OPENAI_API_KEY")) // add the OpenAI chat completion service.
.Build();

// Import the DemographicInfo class to the kernel, so it can be used in the chat completion service.
// this plugin could be from other options such as functions, prompts directory, etc.
kernel.ImportPluginFromType<DemographicInfo>();
var settings = new OpenAIPromptExecutionSettings() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };// Set the settings for the chat completion service.
var chatService = kernel.GetRequiredService<IChatCompletionService>();
ChatHistory chatHistory = [];

// Basic chat
while (true)
{
Console.Write("Q: ");
chatHistory.AddUserMessage(Console.ReadLine());// Add user message to chat history, then it can be use to get more context for the next chat response

var response = await chatService.GetChatMessageContentAsync(chatHistory, settings, kernel);// Get chat response based on chat history

Console.WriteLine(response);
chatHistory.Add(response);// Add chat response to chat history, hence it can be use to get more context for the next chat response
}

class DemographicInfo
{
[KernelFunction]
public int GetAge(string name)
{
return name switch
{
"Alice" => 25,
"Bob" => 30,
_ => 0
};
}
}
3 changes: 3 additions & 0 deletions src/build-2024/03 - Add Plugin (Function Call)/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Add Plugin (Function Call)

A complete documentation for this project is [here](../docs/Exercise/03%20Add%20Plugin%20(Function%20Call).md)
13 changes: 13 additions & 0 deletions src/build-2024/04 - Add Logging/04 - Add Logging.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0-preview.4.24266.19" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0-preview.4.24266.19" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.15.0" />
</ItemGroup>
</Project>
30 changes: 30 additions & 0 deletions src/build-2024/04 - Add Logging/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;

var openAIChatCompletionModelName = "gpt-3.5-turbo"; // this could be other models like "gpt-4o".

var builder = Kernel.CreateBuilder();

// Add logging services to the builder
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace));

var kernel = builder
.AddOpenAIChatCompletion(openAIChatCompletionModelName, Environment.GetEnvironmentVariable("OPENAI_API_KEY")) // add the OpenAI chat completion service.
.Build();

var chatService = kernel.GetRequiredService<IChatCompletionService>();
ChatHistory chatHistory = [];

// Basic chat
while (true)
{
Console.Write("Q: ");
chatHistory.AddUserMessage(Console.ReadLine());// Add user message to chat history, then it can be use to get more context for the next chat response

var response = await chatService.GetChatMessageContentAsync(chatHistory);// Get chat response based on chat history

Console.WriteLine(response);
chatHistory.Add(response);// Add chat response to chat history, hence it can be use to get more context for the next chat response
}
3 changes: 3 additions & 0 deletions src/build-2024/04 - Add Logging/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Add Logging

A complete documentation for this project is [here](../docs/Exercise/04%20Add%20Logging.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>SKEXP0050</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0-preview.4.24266.19" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0-preview.4.24266.19" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.15.0" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Web" Version="1.14.1-alpha" />
</ItemGroup>

</Project>
37 changes: 37 additions & 0 deletions src/build-2024/05 - Add Plugin (Bing Search)/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Plugins.Web;
using Microsoft.SemanticKernel.Plugins.Web.Bing;

var openAIChatCompletionModelName = "gpt-3.5-turbo"; // this could be other models like "gpt-4o".

var builder = Kernel.CreateBuilder();

// Add logging services to the builder
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace));

var kernel = builder
.AddOpenAIChatCompletion(openAIChatCompletionModelName, Environment.GetEnvironmentVariable("OPENAI_API_KEY")) // add the OpenAI chat completion service.
.Build();

kernel.ImportPluginFromObject(new WebSearchEnginePlugin(
new BingConnector(Environment.GetEnvironmentVariable("BING_API_KEY"))));

var settings = new OpenAIPromptExecutionSettings() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };// Set the settings for the chat completion service.
var chatService = kernel.GetRequiredService<IChatCompletionService>();
ChatHistory chatHistory = [];

// Basic chat
while (true)
{
Console.Write("Q: ");
chatHistory.AddUserMessage(Console.ReadLine());// Add user message to chat history, then it can be use to get more context for the next chat response

var response = await chatService.GetChatMessageContentAsync(chatHistory, settings, kernel);// Get chat response based on chat history

Console.WriteLine(response);
chatHistory.Add(response);// Add chat response to chat history, hence it can be use to get more context for the next chat response
}
3 changes: 3 additions & 0 deletions src/build-2024/05 - Add Plugin (Bing Search)/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Add Plugin (Bing Search)

A complete documentation for this project is [here](../docs/Exercise/05%20Add%20Plugin%20(Bing%20Search).md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<NoWarn>SKEXP0001,SKEXP0050</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Compliance.Redaction" Version="9.0.0-preview.4.24271.2" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0-preview.4.24266.19" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="9.0.0-preview.4.24271.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.0-preview.4.24266.19" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="9.0.0-preview.4.24266.19" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.15.0" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Web" Version="1.14.1-alpha" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Http;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Microsoft.SemanticKernel.Plugins.Web.Bing;

var openAIChatCompletionModelName = "gpt-3.5-turbo"; // this could be other models like "gpt-4o".

var builder = Kernel.CreateBuilder();

// injecting services to the kernel such as logging, http client, redaction.
builder.Services.AddLogging(b => b.AddConsole().SetMinimumLevel(LogLevel.Trace));

builder.Services.ConfigureHttpClientDefaults(b =>
{
b.AddStandardResilienceHandler();
b.RedactLoggedHeaders(["Authorization"]);
});
builder.Services.AddRedaction();// Adds the redaction service, which will redact sensitive information from logs and other outputs.

// injecting the permission filter to the kernel.
builder.Services.AddSingleton<IFunctionInvocationFilter, PermissionFilter>();

var kernel = builder
.AddOpenAIChatCompletion(openAIChatCompletionModelName, Environment.GetEnvironmentVariable("OPENAI_API_KEY")) // add the OpenAI chat completion service.
.Build();

kernel.ImportPluginFromObject(new Microsoft.SemanticKernel.Plugins.Web.WebSearchEnginePlugin(
new BingConnector(Environment.GetEnvironmentVariable("BING_API_KEY"))));

var settings = new OpenAIPromptExecutionSettings() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions };// Set the settings for the chat completion service.
var chatService = kernel.GetRequiredService<IChatCompletionService>();
ChatHistory chatHistory = [];

// Basic chat
while (true)
{
Console.Write("Q: ");
chatHistory.AddUserMessage(Console.ReadLine());// Add user message to chat history, then it can be use to get more context for the next chat response

var response = await chatService.GetChatMessageContentAsync(chatHistory, settings, kernel);// Get chat response based on chat history

Console.WriteLine(response);
chatHistory.Add(response);// Add chat response to chat history, hence it can be use to get more context for the next chat response
}

class PermissionFilter : IFunctionInvocationFilter
{
public async Task OnFunctionInvocationAsync(FunctionInvocationContext context, Func<FunctionInvocationContext, Task> next)
{
Console.WriteLine($"Allow {context.Function.Name}?");
if (Console.ReadLine() == "y")
{
await next(context);
}
else
{
throw new Exception("Permission denied");
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Modifying Kernel Behavior with Dependency Injection

A complete documentation for this project is [here](../docs/Exercise/06%20Modifying%20Kernel%20Behavior%20with%20Dependency%20Injection.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>_07___Using_Semantic_Kerenel_in_WebApp</RootNamespace>
<NoWarn>CS0618</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.15.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@_07___Using_Semantic_Kerenel_in_WebApp_HostAddress = http://localhost:5052

GET {{_07___Using_Semantic_Kerenel_in_WebApp_HostAddress}}/weatherforecast/
Accept: application/json

###
Loading

0 comments on commit 922a8cc

Please sign in to comment.