-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
220 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
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
139 changes: 139 additions & 0 deletions
139
samples/dotnet/kernel-syntax-examples/Example28_ChatWithPrompts.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,139 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Globalization; | ||
using System.Threading.Tasks; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.AI.ChatCompletion; | ||
using Microsoft.SemanticKernel.CoreSkills; | ||
using Microsoft.SemanticKernel.TemplateEngine; | ||
using RepoUtils; | ||
using Resources; | ||
|
||
/** | ||
* Scenario: | ||
* - the user is reading a wikipedia page, they select a piece of text and they ask AI to extract some information. | ||
* - the app explicitly uses the Chat model to get a result from gpt-3.5-turbo. | ||
* | ||
* The following example shows how to: | ||
* | ||
* - Use the prompt template engine to render prompts, without executing them. | ||
* This can be used to leverage the template engine (which executes functions internally) | ||
* to generate prompts and use them programmatically, without executing them like semantic functions. | ||
* | ||
* - Use rendered prompts to create the context of System and User messages sent to Chat models | ||
* like "gpt-3.5-turbo" | ||
* | ||
* Note: normally you would work with Semantic Functions to automatically send a prompt to a model | ||
* and get a response. In this case we use the Chat model, sending a chat history object, which | ||
* includes some instructions, some context (the text selected), and the user query. | ||
* | ||
* We use the prompt template engine to craft the strings with all of this information. | ||
* | ||
* Out of scope and not in the example: if needed, one could go further and use a semantic | ||
* function (with extra cost) asking AI to generate the text to send to the Chat model. | ||
* | ||
* TLDR: how to render a prompt: | ||
* | ||
* var kernel = new KernelBuilder().WithLogger(ConsoleLogger.Log).Build(); | ||
* ... import skills and functions ... | ||
* var context = kernel.CreateNewContext(); | ||
* ... set variables ... | ||
* | ||
* var promptRenderer = new PromptTemplateEngine(); | ||
* string renderedPrompt = await promptRenderer.RenderAsync("...prompt template...", context); | ||
*/ | ||
|
||
// ReSharper disable CommentTypo | ||
// ReSharper disable once InconsistentNaming | ||
public static class Example28_ChatWithPrompts | ||
{ | ||
public static async Task RunAsync() | ||
{ | ||
Console.WriteLine("======== Chat with prompts ========"); | ||
|
||
/* Load 3 files: | ||
* - 28-system-prompt.txt: the system prompt, used to initialize the chat session. | ||
* - 28-user-context.txt: the user context, e.g. a piece of a document the user selected and is asking to process. | ||
* - 28-user-prompt.txt: the user prompt, just for demo purpose showing that one can leverage the same approach also to augment user messages. | ||
*/ | ||
|
||
var systemPromptTemplate = EmbeddedResource.Read("28-system-prompt.txt"); | ||
var selectedText = EmbeddedResource.Read("28-user-context.txt"); | ||
var userPromptTemplate = EmbeddedResource.Read("28-user-prompt.txt"); | ||
|
||
// Usual kernel initialization, with GPT 3.5 Turbo | ||
IKernel kernel = new KernelBuilder().WithLogger(ConsoleLogger.Log).Build(); | ||
kernel.Config.AddOpenAIChatCompletionService("chat", "gpt-3.5-turbo", Env.Var("OPENAI_API_KEY")); | ||
|
||
// As an example, we import the time skill, which is used in system prompt to read the current date. | ||
// We could also use a variable, this is just to show that the prompt can invoke functions. | ||
kernel.ImportSkill(new TimeSkill(), "time"); | ||
|
||
// We need a kernel context to store some information to pass to the prompts and the list | ||
// of available skills needed to render prompt templates. | ||
var context = kernel.CreateNewContext(); | ||
|
||
// Put the selected document into the variable used by the system prompt (see 28-system-prompt.txt). | ||
context["selectedText"] = selectedText; | ||
|
||
// Demo another variable, e.g. when the chat started, used by the system prompt (see 28-system-prompt.txt). | ||
context["startTime"] = DateTimeOffset.Now.ToString("hh:mm:ss tt zz", CultureInfo.CurrentCulture); | ||
|
||
// This is the user message, store it in the variable used by 28-user-prompt.txt | ||
context["userMessage"] = "extract locations as a bullet point list"; | ||
|
||
// Instantiate the prompt renderer, which we will use to turn prompt templates | ||
// into strings, that we will store into a Chat history object, which is then sent | ||
// to the Chat Model. | ||
var promptRenderer = new PromptTemplateEngine(); | ||
|
||
// Render the system prompt. This string is used to configure the chat. | ||
// This contains the context, ie a piece of a wikipedia page selected by the user. | ||
string systemMessage = await promptRenderer.RenderAsync(systemPromptTemplate, context); | ||
Console.WriteLine($"------------------------------------\n{systemMessage}"); | ||
|
||
// Render the user prompt. This string is the query sent by the user | ||
// This contains the user request, ie "extract locations as a bullet point list" | ||
string userMessage = await promptRenderer.RenderAsync(userPromptTemplate, context); | ||
Console.WriteLine($"------------------------------------\n{userMessage}"); | ||
|
||
// Client used to request answers to gpt-3.5-turbo | ||
var chatGPT = kernel.GetService<IChatCompletion>(); | ||
|
||
// The full chat history. Depending on your scenario, you can pass the full chat if useful, | ||
// or create a new one every time, assuming that the "system message" contains all the | ||
// information needed. | ||
var chatHistory = chatGPT.CreateNewChat(systemMessage); | ||
|
||
// Add the user query to the chat history | ||
chatHistory.AddMessage(ChatHistory.AuthorRoles.User, userMessage); | ||
|
||
// Finally, get the response from AI | ||
string answer = await chatGPT.GenerateMessageAsync(chatHistory); | ||
Console.WriteLine($"------------------------------------\n{answer}"); | ||
|
||
/* | ||
Output: | ||
------------------------------------ | ||
You are an AI assistant that helps people find information. | ||
The chat started at: 09:52:12 PM -07 | ||
The current time is: Thursday, April 27, 2023 9:52 PM | ||
Text selected: | ||
The central Sahara is hyperarid, with sparse vegetation. The northern and southern reaches of the desert, along with the highlands, have areas of sparse grassland and desert shrub, with trees and taller shrubs in wadis, where moisture collects. In the central, hyperarid region, there are many subdivisions of the great desert: Tanezrouft, the Ténéré, the Libyan Desert, the Eastern Desert, the Nubian Desert and others. These extremely arid areas often receive no rain for years. | ||
------------------------------------ | ||
Thursday, April 27, 2023 2:34 PM: extract locations as a bullet point list | ||
------------------------------------ | ||
Sure, here are the locations mentioned in the text: | ||
- Tanezrouft | ||
- Ténéré | ||
- Libyan Desert | ||
- Eastern Desert | ||
- Nubian Desert | ||
*/ | ||
} | ||
} |
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
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
20 changes: 20 additions & 0 deletions
20
samples/dotnet/kernel-syntax-examples/RepoUtils/ConfigurationException.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,20 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
|
||
namespace RepoUtils; | ||
|
||
public class ConfigurationException : Exception | ||
{ | ||
public ConfigurationException() | ||
{ | ||
} | ||
|
||
public ConfigurationException(string message) : base(message) | ||
{ | ||
} | ||
|
||
public ConfigurationException(string message, Exception innerException) : base(message, innerException) | ||
{ | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
samples/dotnet/kernel-syntax-examples/Resources/28-system-prompt.txt
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,5 @@ | ||
You are an AI assistant that helps people find information. | ||
The chat started at: {{ $startTime }} | ||
The current time is: {{ time.now }} | ||
Text selected: | ||
{{ $selectedText }} |
1 change: 1 addition & 0 deletions
1
samples/dotnet/kernel-syntax-examples/Resources/28-user-context.txt
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 @@ | ||
The central Sahara is hyperarid, with sparse vegetation. The northern and southern reaches of the desert, along with the highlands, have areas of sparse grassland and desert shrub, with trees and taller shrubs in wadis, where moisture collects. In the central, hyperarid region, there are many subdivisions of the great desert: Tanezrouft, the Ténéré, the Libyan Desert, the Eastern Desert, the Nubian Desert and others. These extremely arid areas often receive no rain for years. |
1 change: 1 addition & 0 deletions
1
samples/dotnet/kernel-syntax-examples/Resources/28-user-prompt.txt
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 @@ | ||
{{ time.now }}: {{ $userMessage }} |
38 changes: 38 additions & 0 deletions
38
samples/dotnet/kernel-syntax-examples/Resources/EmbeddedResource.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,38 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System.IO; | ||
using System.Reflection; | ||
using RepoUtils; | ||
|
||
namespace Resources; | ||
|
||
/// <summary> | ||
/// Resource helper to load resources embedded in the assembly. By default we embed only | ||
/// text files, so the helper is limited to returning text. | ||
/// | ||
/// You can find information about embedded resources here: | ||
/// * https://learn.microsoft.com/dotnet/core/extensions/create-resource-files | ||
/// * https://learn.microsoft.com/dotnet/api/system.reflection.assembly.getmanifestresourcestream?view=net-7.0 | ||
/// | ||
/// To know which resources are embedded, check the csproj file. | ||
/// </summary> | ||
internal static class EmbeddedResource | ||
{ | ||
private static readonly string? s_namespace = typeof(EmbeddedResource).Namespace; | ||
|
||
internal static string Read(string fileName) | ||
{ | ||
// Get the current assembly. Note: this class is in the same assembly where the embedded resources are stored. | ||
Assembly? assembly = typeof(EmbeddedResource).GetTypeInfo().Assembly; | ||
if (assembly == null) { throw new ConfigurationException($"[{s_namespace}] {fileName} assembly not found"); } | ||
|
||
// Resources are mapped like types, using the namespace and appending "." (dot) and the file name | ||
var resourceName = $"{s_namespace}." + fileName; | ||
using Stream? resource = assembly.GetManifestResourceStream(resourceName); | ||
if (resource == null) { throw new ConfigurationException($"{resourceName} resource not found"); } | ||
|
||
// Return the resource content, in text format. | ||
using var reader = new StreamReader(resource); | ||
return reader.ReadToEnd(); | ||
} | ||
} |