Skip to content

Commit

Permalink
.Net: Add getting started showing how to use hooks for responsible AI (
Browse files Browse the repository at this point in the history
…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
markwallace-microsoft committed Dec 14, 2023
1 parent 73a3323 commit db251dd
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
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));
}
}
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;

public static class Step5_Pipelining
public static class Step7_Pipelining
{
/// <summary>
/// Provides an example of combining multiple functions into a single function that invokes
Expand Down

0 comments on commit db251dd

Please sign in to comment.