Show HN: Convert text prompts into classes with C# source generators

https://github.com/CharlieDigital/SKPromptGenerator
by CharlieDigital • 2 years ago
2 0 2 years ago

If you work with prompts in code, a minor nuisance is managing your placeholder tokens and basic templating. There's also a lot of repetitive code you'll end up writing to set up and execute the prompt.

C# source generators with .NET's Roslyn analyzer allow reading the source and generating strongly-typed class-per-prompt which hoists the tokens into constructor parameters so it's easier to consume the prompt.

So a prompt string like this:

    // Define a prompt
    [PromptTemplate]
    public const string Capitol = """
      What is the capitol of {state} {country}?
      Respond directly in a single line
      """;
Can be invoked like this:
    // Execute the prompt passing in a Semantic Kernel instance.
    var capitol = await new CapitolPrompt("NJ", "USA")
      .ExecuteAsync(kernel);      
Nice!

Related Stories

Loading related stories...

Source preview

github.com