SKPromptGenerator 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SKPromptGenerator --version 0.2.0                
NuGet\Install-Package SKPromptGenerator -Version 0.2.0                
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.2.0">
  <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.2.0                
#r "nuget: SKPromptGenerator, 0.2.0"                
#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.2.0

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

Semantic Kernel (SK) Prompt Generator

This project is a proof-of-concept prompt class generator using C# source generators.

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("NJ", "USA").ExecuteAsync(kernel);

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

Limitations

Your prompt must be a const string.

Installing

This generator is built for .NET 8.

To install:

dotnet add package 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:

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.
    """;
}

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 App;

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,
  };
}

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.

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)

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