prompt

package
v0.5.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 5 Imported by: 0

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 NewBuilder

func NewBuilder() *Builder

NewBuilder creates a new prompt builder.

func (*Builder) AddLine

func (b *Builder) AddLine(line string) *Builder

AddLine adds a line of text to the prompt.

func (*Builder) AddSection

func (b *Builder) AddSection(header, content string) *Builder

AddSection adds a named section with a header.

func (*Builder) AddTemplate

func (b *Builder) AddTemplate(text string) *Builder

AddTemplate adds a template string that will be rendered with variables.

func (*Builder) Build

func (b *Builder) Build(vars any) (string, error)

Build renders the complete prompt with the given variables.

func (*Builder) String

func (b *Builder) String() string

String returns the raw template text without variable substitution.

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 MustNew

func MustNew(name, text string) *Template

MustNew creates a new prompt template and panics on error.

func New

func New(name, text string) (*Template, error)

New creates a new prompt template.

Variables use {{ name }} syntax. Built-in functions: join, upper, lower, trim, contains, replace, default.

func (*Template) Execute

func (t *Template) Execute(vars any) (string, error)

Execute renders the template with the given variables. Variables should be a map[string]any or a struct.

func (*Template) MustExecute

func (t *Template) MustExecute(vars any) string

MustExecute renders the template and panics on error.

func (*Template) Name

func (t *Template) Name() string

Name returns the template name.

func (*Template) Raw

func (t *Template) Raw() string

Raw returns the original template text before parsing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL