Documentation
¶
Overview ¶
Package prompt provides reusable prompt templates with variable substitution.
Templates use Go's text/template syntax for powerful prompt construction with conditionals, loops, and custom functions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder helps construct complex prompts from multiple sections.
Example:
b := prompt.NewBuilder()
b.AddLine("You are a helpful assistant.")
b.AddLine("The user's name is {{ name }}.")
b.AddSection("Rules", "- Be concise\n- Be accurate")
result, _ := b.Build(map[string]any{"name": "Alice"})
func (*Builder) AddSection ¶
AddSection adds a named section with a header.
func (*Builder) AddTemplate ¶
AddTemplate adds a template string that will be rendered with variables.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template is a reusable prompt template with Jinja-style variable substitution.
Templates use {{ variable }} syntax for simple variable replacement, and support Go's text/template features for advanced use cases (conditionals, loops, functions).
Simple syntax (recommended):
{{ name }} → variable substitution
{{ age }} → works with any map key
Advanced syntax (Go text/template):
{{if .premium}}Premium user{{end}}
{{range .items}}* {{.}}{{end}}
{{ name | upper }} → built-in functions
Example:
t := prompt.MustNew("greeting", "Hello {{ name }}, you are a {{ role }}.")
result, _ := t.Execute(map[string]any{"name": "Alice", "role": "admin"})
// result: "Hello Alice, you are a admin."
func New ¶
New creates a new prompt template.
Variables use {{ name }} syntax. Built-in functions: join, upper, lower, trim, contains, replace, default.
func (*Template) Execute ¶
Execute renders the template with the given variables. Variables should be a map[string]any or a struct.
func (*Template) MustExecute ¶
MustExecute renders the template and panics on error.