Table of Contents

Class PromptTemplate

Namespace
LMKit.TextGeneration.Prompts
Assembly
LM-Kit.NET.dll

A compiled prompt template with variable substitution, conditionals, loops, filters, and custom helpers.

public sealed class PromptTemplate
Inheritance
PromptTemplate
Inherited Members

Examples

Simple variable substitution:

using LMKit.TextGeneration.Prompts;

var template = PromptTemplate.Parse(
    "You are {{role}}. Help the user with {{topic}}."
);

string prompt = template.Render(new PromptTemplateContext
{
    ["role"] = "a senior C# developer",
    ["topic"] = "async programming"
});
// "You are a senior C# developer. Help the user with async programming."

Conditionals and loops:

using LMKit.TextGeneration.Prompts;

var template = PromptTemplate.Parse(@"
You are a helpful assistant.
{{#if tools}}
You have access to the following tools:
{{#each tools}}
- {{this.name}}: {{this.description}}
{{/each}}
{{#else}}
You do not have access to any tools.
{{/if}}
{{#if language}}Respond in {{language}}.{{/if}}
");

var context = new PromptTemplateContext
{
    ["tools"] = new[]
    {
        new { name = "calculator", description = "Perform arithmetic" },
        new { name = "web_search", description = "Search the web" }
    },
    ["language"] = "French"
};

string prompt = template.Render(context);

Filters and defaults:

using LMKit.TextGeneration.Prompts;

var template = PromptTemplate.Parse(
    "Welcome, {{name|trim|capitalize}}! Your role: {{role:user}}."
);

string result = template.Render(new PromptTemplateContext
{
    ["name"] = "  alice  "
    // "role" not set: defaults to "user"
});
// "Welcome, Alice! Your role: user."

Using with AgentBuilder:

using LMKit.Model;
using LMKit.Agents;
using LMKit.TextGeneration.Prompts;

var instructionTemplate = PromptTemplate.Parse(@"
You are an expert in {{domain}}.
{{#if constraints}}
Constraints:
{{#each constraints}}
- {{this}}
{{/each}}
{{/if}}
Always respond in {{language:English}}.
");

string instruction = instructionTemplate.Render(new PromptTemplateContext
{
    ["domain"] = "machine learning",
    ["constraints"] = new[] { "Be concise", "Cite sources", "Use examples" }
});

using var model = new LM("path/to/model.gguf");

var agent = new AgentBuilder()
    .WithModel(model)
    .WithPersona("ML Expert")
    .WithInstruction(instruction)
    .Build();

Remarks

Templates are parsed once via Parse(string, PromptTemplateOptions) and rendered many times with different PromptTemplateContext instances, making them efficient for repeated prompt generation. The default syntax uses Mustache-style {{variable}} placeholders.

Supported constructs (Mustache syntax):

  • Variables: {{name}}, {{user.address.city}}
  • Filters: {{name|upper}}, {{text|trim|truncate:100}}
  • Defaults: {{name:Anonymous}}
  • Conditionals: {{#if premium}}...{{#else}}...{{/if}}
  • Negation: {{#unless banned}}...{{/unless}}
  • Loops: {{#each items}}...{{this.name}}...{{/each}}
  • Scoping: {{#with user}}{{name}} ({{email}}){{/with}}
  • Helpers: {{uppercase name}} (custom registered functions)

Dollar (${var}) and Percent (%var%) syntaxes support variable substitution with filters but not block constructs.

Properties

Source

Gets the original template source string.

Variables

Gets the names of all variables referenced in the template.

Methods

Parse(string, PromptTemplateOptions)

Parses a template string into a compiled PromptTemplate.

Render(PromptTemplateContext)

Renders the template with the given context.

Render(IDictionary<string, object>)

Renders the template with a dictionary of variable values.

ToString()

Returns the original template source string.

TryParse(string, out PromptTemplate, out IReadOnlyList<string>, PromptTemplateOptions)

Tries to parse a template string, returning false on syntax errors instead of throwing an exception.