Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Show HN: Convert text prompts into classes with C# source generators (github.com/charliedigital)
2 points by CharlieDigital on June 23, 2024 | hide | past | favorite
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!


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: