SKPromptGenerator 0.4.2

dotnet add package SKPromptGenerator --version 0.4.2                
NuGet\Install-Package SKPromptGenerator -Version 0.4.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SKPromptGenerator" Version="0.4.2">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SKPromptGenerator --version 0.4.2                
#r "nuget: SKPromptGenerator, 0.4.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SKPromptGenerator as a Cake Addin
#addin nuget:?package=SKPromptGenerator&version=0.4.2

// Install SKPromptGenerator as a Cake Tool
#tool nuget:?package=SKPromptGenerator&version=0.4.2                

Semantic Kernel (SK) Prompt Generator

SKPromptGenerator is a C# source generator which automatically creates strongly-typed classes for your string prompt templates.

It is intended to be used with Semantic Kernel.

💡 NOTE: As of version 4, the token format is switched from {name} to {{$name}} to match Semantic Kernel.

Motivation

When working with prompts, you'll end up doing a lot of string templating and repetitive code.

Wouldn't it be nice if you could just have a strongly-typed class for each prompt automatically created using the prompt?

This library does exactly that.

public static class Prompts
{
  // Define a prompt
  [PromptTemplate]
  public const string Capitol = """
    What is the capitol of {state} {country}?
    Respond directly in a single line
    """;
}

// Execute the prompt passing in a Semantic Kernel instance.
var capitol = await new CapitolPrompt(
  state: "NJ",
  country: "USA"
).ExecuteAsync(kernel);

The tokens in the prompt string become named parameters on the class constructor 🎉

Limitations

  1. Your prompt must be a const string because the generator needs to be able to read the string in the source.
  2. Your prompts must live in some class in a namespace. If you get the error error CS1001: Identifier expected, then you are probably missing a namespace around your prompt.
  3. You must add a dependency to Microsoft.SemanticKernel since the ExecuteAsync method requires the Kernel instance.
  4. Currently only targets .NET 8; considering netstandard2.0.

Installing

This generator is built for .NET 8.

To install:

dotnet add package SKPromptGenerator

For the latest releases, see: https://www.nuget.org/packages/SKPromptGenerator

Using

This repository includes a sample project under the /app directory.

To use, create a new console app:

mkdir sk-prompt-gen-test
cd sk-prompt-gen-test
dotnet new console
dotnet add package SKPromptGenerator
dotnet add package Microsoft.SemanticKernel

In the project, create a class like so (you can call your class whatever you want):

namespace SomeNamespace;

public static class Prompts
{
  [PromptTemplate]
  public const string Capitol = """
    What is the capitol of {state} {country}?
    Respond directly in a single line
    When writing the state, always write it as the full name
    Write your output in the format: The capitol of <STATE> is: <CAPITOL>.
    For example: The capitol of California is: Sacramento.
    """;
}

💡 Note the usage of a namespace for the class. The prompt need not be in a standalone class. It can also be placed in an existing Controller (for example)

In the code above, we've created a prompt with two tokens: {state} and {country}.

The [PromptTemplate] attribute instructs the generator to create a class like so:

using System;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using SKPromptGenerator;

namespace SomeNamespace;

public partial class CapitolPrompt(
  string state, string country
) : PromptTemplateBase
{
  public override string Text => $"""
What is the capitol of {state} {country}?
Respond directly in a single line
When writing the state, always write it as the full name
Write your output in the format: The capitol of <STATE> is: <CAPITOL>.
For example: The capitol of California is: Sacramento.
""";

  public override OpenAIPromptExecutionSettings Settings => new OpenAIPromptExecutionSettings
  {
    MaxTokens = 500,
    Temperature = 0.5d,
    TopP = 0d,
  };
}

Note the two class parameters state and country which are extracted from the prompt template.

Now we can use the prompt like so:

var capitol = await new CapitolPrompt("NJ", "USA").ExecuteAsync(kernel);

Console.WriteLine($"{capitol}");
// The capitol of New Jersey is: Trenton.

capitol = await new CapitolPrompt("NY", "USA").ExecuteAsync(kernel);

Console.WriteLine($"{capitol}");
// The capitol of New York is: Albany.

Custom Base Class

If you want to customize how the prompt is executed, you can specify a custom base class when assigning the attribute.

Your base class must inherit from PromptTemplateBase:

public abstract class CustomBase : PromptTemplateBase
{
  public override async Task<string> ExecuteAsync(
    Kernel kernel,
    string? serviceId = null,
    CancellationToken cancellation = default
  )
  {
    return await Task.FromResult("response");
  }
}

And then you can specify this custom base class as a generic type:

[PromptTemplate<CustomBase>]
public const string CapitolCustom = """
  What is the capitol of {state} {country}?
  Respond directly in a single line
  When writing the state, always write it as the full name
  Write your output in the format: The capitol of <STATE> is: <CAPITOL>.
  For example: The capitol of California is: Sacramento.
  """;

This allows you to take full control of the execution of the prompt (e.g. add logging, telemetry, etc.).

💡 Note: you should use a global using statement for the namespace of your custom base class.

Prompt Execution Settings

The PromptTemplate attribute also allows specification of the prompt execution settings.

The three parameters are:

Parameter Details Default
MaxTokens The maximum number of tokens in the response 500
Temperature The temperature 0.5
TopP The TopP 0

For example:

public static class Prompts
{
  [PromptTemplate(10, 0.1)]
  public const string SampleTmpl1 = """
    What is the capitol of {state} {country}
    Respond directly on a single line.
    """;
}

(See the PromptTmpl class for details)

Using the Sample App

To use the sample app, you'll need to set up user secrets:

dotnet user-secrets init
dotnet user-secrets set "AzureOpenAIKey" "YOUR_AZURE_OPEN_AI_KEY"
dotnet user-secrets set "AzureOpenAIEndpoint" "YOUR_AZURE_OPEN_AI_ENDPOINT"

If you are using OpenAI, feel free to fork this project and simply change the service type and configuration values.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.4.2 51 7/23/2024
0.4.1 68 7/18/2024
0.4.0 67 7/16/2024
0.3.0 65 7/5/2024
0.2.1 89 6/23/2024
0.2.0 86 6/23/2024
0.1.0 86 6/23/2024