forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
.Net: Add getting started showing how to use hooks for responsible AI (…
…microsoft#4273) ### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄
- Loading branch information
1 parent
73a3323
commit db251dd
Showing
3 changed files
with
75 additions
and
1 deletion.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
dotnet/samples/KernelSyntaxExamples/Getting_Started/Step5_Chat_Prompt.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.SemanticKernel; | ||
|
||
public static class Step5_Chat_Prompt | ||
{ | ||
/// <summary> | ||
/// Show how to construct a chat prompt and invoke it. | ||
/// </summary> | ||
public static async Task RunAsync() | ||
{ | ||
// Create a kernel with OpenAI chat completion | ||
Kernel kernel = Kernel.CreateBuilder() | ||
.AddOpenAIChatCompletion( | ||
modelId: TestConfiguration.OpenAI.ChatModelId, | ||
apiKey: TestConfiguration.OpenAI.ApiKey) | ||
.Build(); | ||
|
||
// Invoke the kernel with a chat prompt and display the result | ||
string chatPrompt = @" | ||
<message role=""user"">What is Seattle?</message> | ||
<message role=""system"">Respond with JSON.</message> | ||
"; | ||
Console.WriteLine(await kernel.InvokePromptAsync(chatPrompt)); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
dotnet/samples/KernelSyntaxExamples/Getting_Started/Step6_Responsible_AI.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.SemanticKernel; | ||
|
||
// This example shows how to use rendering event hooks to ensure that prompts are rendered in a responsible manner. | ||
public static class Step6_Responsible_AI | ||
{ | ||
/// <summary> | ||
/// Show how to use rendering event hooks to ensure that prompts are rendered in a responsible manner. | ||
/// </summary> | ||
public static async Task RunAsync() | ||
{ | ||
// Create a kernel with OpenAI chat completion | ||
Kernel kernel = Kernel.CreateBuilder() | ||
.AddOpenAIChatCompletion( | ||
modelId: TestConfiguration.OpenAI.ChatModelId, | ||
apiKey: TestConfiguration.OpenAI.ApiKey) | ||
.Build(); | ||
|
||
// Handler which is called before a prompt is rendered | ||
void MyRenderingHandler(object? sender, PromptRenderingEventArgs e) | ||
{ | ||
if (e.Arguments.ContainsName("card_number")) | ||
{ | ||
e.Arguments["card_number"] = "**** **** **** ****"; | ||
} | ||
} | ||
|
||
// Handler which is called after a prompt is rendered | ||
void MyRenderedHandler(object? sender, PromptRenderedEventArgs e) | ||
{ | ||
e.RenderedPrompt += " NO SEXISM, RACISM OR OTHER BIAS/BIGOTRY"; | ||
|
||
Console.WriteLine(e.RenderedPrompt); | ||
} | ||
|
||
// Add the handlers to the kernel | ||
kernel.PromptRendering += MyRenderingHandler; | ||
kernel.PromptRendered += MyRenderedHandler; | ||
|
||
KernelArguments arguments = new() { { "card_number", "4444 3333 2222 1111" } }; | ||
Console.WriteLine(await kernel.InvokePromptAsync("Tell me some useful information about this credit card number {{$card_number}}?", arguments)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters