Documentation
¶
Overview ¶
Package prompts provides utilities for creating and managing prompts for Large Language Models (LLMs).
Basic Usage ¶
The simplest way to use this package is with prompt templates:
// Create a basic prompt template
template := prompts.NewPromptTemplate(
"Write a {{.style}} story about {{.topic}}.",
[]string{"style", "topic"},
)
// Format the template with values
result, err := template.Format(map[string]any{
"style": "funny",
"topic": "a robot learning to cook",
})
// Result: "Write a funny story about a robot learning to cook."
Template Formats ¶
Three template formats are supported:
- Go Templates (default): `{{ .variable }}` - Native Go text/template with sprig functions
- Jinja2: `{{ variable }}` - Python-style templates with filters and logic
- F-Strings: `{variable}` - Simple Python-style variable substitution
Example using different formats:
// Go template (default)
goTemplate := prompts.NewPromptTemplate(
"Hello {{ .name }}!",
[]string{"name"},
)
// Jinja2 format
jinja2Template := prompts.PromptTemplate{
Template: "Hello {{ name }}!",
InputVariables: []string{"name"},
TemplateFormat: prompts.TemplateFormatJinja2,
}
// F-string format
fstringTemplate := prompts.PromptTemplate{
Template: "Hello {name}!",
InputVariables: []string{"name"},
TemplateFormat: prompts.TemplateFormatFString,
}
Chat Prompts ¶
For conversational AI, use ChatPromptTemplate:
chatTemplate := prompts.NewChatPromptTemplate([]prompts.MessageFormatter{
prompts.NewSystemMessagePromptTemplate(
"You are a helpful assistant.",
nil,
),
prompts.NewHumanMessagePromptTemplate(
"{{.question}}",
[]string{"question"},
),
})
messages, err := chatTemplate.FormatMessages(map[string]any{
"question": "What is the capital of France?",
})
Partial Variables ¶
Pre-fill some template variables while leaving others for runtime:
template := prompts.PromptTemplate{
Template: "{{.greeting}}, {{.name}}!",
InputVariables: []string{"name"},
PartialVariables: map[string]any{
"greeting": "Welcome",
},
}
result, err := template.Format(map[string]any{
"name": "Alice",
})
// Result: "Welcome, Alice!"
Security ¶
By default, templates render without sanitization for backward compatibility. When working with untrusted user input, enable HTML escaping:
// Enable sanitization for untrusted data
result, err := prompts.RenderTemplate(
"User said: {{.input}}",
prompts.TemplateFormatGoTemplate,
map[string]any{"input": userInput},
prompts.WithSanitization(), // Escapes HTML special characters
)
Templates always block filesystem access for security. Use RenderTemplateFS for controlled template inheritance:
//go:embed templates/* var templateFS embed.FS result, err := prompts.RenderTemplateFS( templateFS, "email.j2", prompts.TemplateFormatJinja2, data, )
Advanced Features ¶
Few-Shot Learning ¶
Create prompts with examples for better model performance:
fewShot := &prompts.FewShotPrompt{
ExamplePrompt: prompts.NewPromptTemplate(
"Q: {{.question}}\nA: {{.answer}}",
[]string{"question", "answer"},
),
Examples: []map[string]string{
{"question": "What is 2+2?", "answer": "4"},
{"question": "What is 3+3?", "answer": "6"},
},
Suffix: "\nQ: {{.question}}\nA:",
InputVariables: []string{"question"},
}
Performance Considerations ¶
- Go templates are fastest and recommended for production - Template compilation is cached for repeated use - Use RenderTemplateFS with embed.FS for optimal production deployments
Package prompts provides template rendering with support for multiple formats.
Template Formats: - Jinja2: Full Jinja2 syntax with controlled filesystem access - Go Templates: Standard text/template with sprig functions - F-Strings: Python-style string formatting
Basic Usage:
// Simple template rendering
result, err := prompts.RenderTemplate(
"Hello {{ name }}!",
prompts.TemplateFormatJinja2,
map[string]any{"name": "World"},
)
Template Files:
// For templates with includes/inheritance, use RenderTemplateFS
//go:embed templates/*
var templateFS embed.FS
result, err := prompts.RenderTemplateFS(
templateFS,
"welcome.j2",
prompts.TemplateFormatJinja2,
data,
)
The RenderTemplate function is designed for inline templates and simple use cases. For template files that need to include other templates, use RenderTemplateFS with an explicit fs.FS parameter to define the template file boundary.
Example (BasicTemplateRendering) ¶
Example_basicTemplateRendering demonstrates basic template usage with automatic security.
// Basic template rendering - all formats supported
data := map[string]any{
"name": "Alice",
"role": "Developer",
"company": "Acme Corp",
}
// F-String format (fastest, simple variable substitution)
result, err := RenderTemplate(
"Hello {name}! You're a {role} at {company}.",
TemplateFormatFString,
data,
)
if err != nil {
log.Fatal(err)
}
fmt.Println("F-String:", result)
// Go Template format (recommended, supports conditionals and loops)
result, err = RenderTemplate(
"Hello {{.name}}! You're a {{.role}} at {{.company}}.",
TemplateFormatGoTemplate,
data,
)
if err != nil {
log.Fatal(err)
}
fmt.Println("Go Template:", result)
Output: F-String: Hello Alice! You're a Developer at Acme Corp. Go Template: Hello Alice! You're a Developer at Acme Corp.
Example (ErrorHandling) ¶
Example_errorHandling demonstrates proper error handling with templates.
// This will demonstrate different types of errors
// 1. Invalid template syntax
_, err := RenderTemplate("Hello {{.name", TemplateFormatGoTemplate, map[string]any{"name": "Alice"})
if err != nil {
fmt.Println("Template syntax error:", err)
}
// 2. Missing required variable
_, err = RenderTemplate("Hello {{.name}}!", TemplateFormatGoTemplate, map[string]any{})
if err != nil {
fmt.Println("Missing variable error:", err)
}
// 3. Invalid identifier format (starts with number)
_, err = RenderTemplate("{{.data}}", TemplateFormatGoTemplate, map[string]any{"123invalid": "value"}, WithSanitization())
if err != nil {
fmt.Println("Invalid variable name:", err)
}
Output: Template syntax error: template parse failure: template: template:1: unclosed action Missing variable error: template execution failure: template: template:1:8: executing "template" at <.name>: map has no entry for key "name" Invalid variable name: template execution failure: template validation failure: invalid variable name: 123invalid
Example (Migration) ¶
Example_migration demonstrates migrating to the new security model.
// OLD WAY: Templates rendered without sanitization by default
// result, _ := RenderTemplate(template, format, userInput)
// NEW WAY: Enable sanitization when needed for untrusted input
userInput := map[string]any{
"name": "<script>alert('xss')</script>",
}
template := "Hello {{.name}}!"
// Enable sanitization for untrusted data
result, err := RenderTemplate(template, TemplateFormatGoTemplate, userInput, WithSanitization())
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
Output: Hello <script>alert('xss')</script>!
Example (OptionalSecurity) ¶
Example_optionalSecurity demonstrates how to enable security for untrusted input.
// User input that contains potential security threats
userInput := map[string]any{
"username": "alice<script>alert('xss')</script>",
"bio": "I love coding! <b>Bold text</b>",
"website": "https://example.com",
}
template := `
Profile for {{.username}}
Bio: {{.bio}}
Website: {{.website}}`
// Enable sanitization for untrusted user input
result, err := RenderTemplate(template, TemplateFormatGoTemplate, userInput, WithSanitization())
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
Output: Profile for alice<script>alert('xss')</script> Bio: I love coding! <b>Bold text</b> Website: https://example.com
Example (PromptTemplate) ¶
Example_promptTemplate demonstrates using PromptTemplate for LLM integration.
// Create a prompt template for an AI assistant
template := NewPromptTemplate(
`You are a helpful assistant for {{.company}}.
User: {{.username}} ({{.role}})
Query: {{.query}}
Please provide a helpful response appropriate for their role.`,
[]string{"company", "username", "role", "query"},
)
// User input (automatically secured)
data := map[string]any{
"company": "Acme Corp",
"username": "alice",
"role": "developer",
"query": "How do I optimize database queries?",
}
prompt, err := template.FormatPrompt(data)
if err != nil {
log.Fatal(err)
}
fmt.Println(prompt.String())
Output: You are a helpful assistant for Acme Corp. User: alice (developer) Query: How do I optimize database queries? Please provide a helpful response appropriate for their role.
Example (TemplateWithIncludes) ¶
Example_templateWithIncludes demonstrates safe template composition with filesystem access.
// Using embed.FS ensures templates are bundled at compile time
// and provides a secure filesystem boundary
data := map[string]any{
"title": "Welcome Guide",
"user": "Alice",
"company": "Acme Corp",
}
// This template can safely include other templates within the embedded filesystem
result, err := RenderTemplateFS(
templateFiles,
"testdata/main.j2", // Template that includes other templates
TemplateFormatJinja2,
data,
)
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
Output: Welcome Guide Hello Alice! Welcome to Acme Corp. This is a safe template composition example.
Example (TemplateWithLogic) ¶
Example_templateWithLogic demonstrates templates with conditionals and loops.
data := map[string]any{
"user": map[string]any{
"name": "Alice",
"score": 92,
},
"items": []string{"apple", "banana", "cherry"},
}
template := `Welcome {{.user.name}}!
{{if gt .user.score 90 -}}
🌟 Excellent performance! ({{.user.score}}%)
{{- else -}}
📈 Keep up the good work! ({{.user.score}}%)
{{- end}}
Your items:
{{range .items}}• {{.}}
{{end}}`
result, err := RenderTemplate(template, TemplateFormatGoTemplate, data)
if err != nil {
log.Fatal(err)
}
fmt.Println(result)
Output: Welcome Alice! 🌟 Excellent performance! (92%) Your items: • apple • banana • cherry
Index ¶
- Variables
- func CheckValidTemplate(template string, templateFormat TemplateFormat, inputVariables []string) error
- func RenderTemplate(tmpl string, tmplFormat TemplateFormat, values map[string]any, ...) (string, error)
- func RenderTemplateFS(fsys fs.FS, name string, tmplFormat TemplateFormat, values map[string]any, ...) (string, error)
- type AIMessagePromptTemplate
- type ChatPromptTemplate
- func (p ChatPromptTemplate) Format(values map[string]any) (string, error)
- func (p ChatPromptTemplate) FormatMessages(values map[string]any) ([]llms.ChatMessage, error)
- func (p ChatPromptTemplate) FormatPrompt(values map[string]any) (llms.PromptValue, error)
- func (p ChatPromptTemplate) GetInputVariables() []string
- type ChatPromptValue
- type ExampleSelector
- type FewShotPrompt
- type FormatPrompter
- type Formatter
- type GenericMessagePromptTemplate
- type HumanMessagePromptTemplate
- type MessageFormatter
- type MessagesPlaceholder
- type PromptTemplate
- type RenderOption
- type StringPromptValue
- type SystemMessagePromptTemplate
- type TemplateFormat
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoExample is returned when none of the Examples and ExampleSelector are provided. ErrNoExample = errors.New("no example is provided") // ErrExamplesAndExampleSelectorProvided is returned when there are no Examples and ExampleSelector. ErrExamplesAndExampleSelectorProvided = errors.New("only one of 'Examples' and 'example_selector' should be" + " provided") )
var ( // ErrInvalidPartialVariableType is returned when a partial variable is not a supported type. // Valid types are string, int, float64, bool, and func() string, func() int, func() float64, func() bool. ErrInvalidPartialVariableType = errors.New("invalid partial variable type") // ErrNeedChatMessageList is returned when the variable is not a list of chat messages. ErrNeedChatMessageList = errors.New("variable should be a list of chat messages") )
var ErrInvalidTemplateFormat = errors.New("invalid template format")
ErrInvalidTemplateFormat is the error when the template format is invalid and not supported.
Functions ¶
func CheckValidTemplate ¶
func CheckValidTemplate(template string, templateFormat TemplateFormat, inputVariables []string) error
CheckValidTemplate checks if the template is valid through checking whether the given TemplateFormat is available and whether the template can be rendered.
Note: This function blocks filesystem access for security. Templates using include, extends, import, or from statements will fail. Use RenderTemplateFS for controlled filesystem access if needed.
func RenderTemplate ¶
func RenderTemplate(tmpl string, tmplFormat TemplateFormat, values map[string]any, opts ...RenderOption) (string, error)
RenderTemplate renders the template with the given values.
This function is designed for inline templates and simple use cases. It supports variable interpolation, conditionals, loops, and filters. For templates that need to include other template files, use RenderTemplateFS with an explicit fs.FS parameter to specify the template file source.
Supported features: - Variable interpolation: {{ variable }} - Conditional logic: {% if condition %}...{% endif %} - Loops: {% for item in list %}...{% endfor %} - Text filters: {{ text | filter }}
Security: By default, this function renders templates without sanitization. To enable HTML escaping for untrusted data, use the WithSanitization() option. This function always blocks filesystem access for security - templates using include, extends, or import statements will fail. Use RenderTemplateFS for controlled filesystem access.
Example:
result, err := RenderTemplate(
"Hello {{ name }}! Score: {{ score }}%",
TemplateFormatJinja2,
map[string]any{"name": "Alice", "score": 95},
)
func RenderTemplateFS ¶
func RenderTemplateFS(fsys fs.FS, name string, tmplFormat TemplateFormat, values map[string]any, opts ...RenderOption) (string, error)
RenderTemplateFS renders a template loaded from the provided filesystem. This enables templates to use include, extends, and import statements to compose templates from multiple files.
The fs.FS parameter defines which files the template can access, providing a clean boundary for template file organization.
Supported fs.FS implementations: - embed.FS: Embed templates at compile time (recommended for production) - os.DirFS: Access a specific directory tree - testing/fstest.MapFS: In-memory filesystem for testing - Custom fs.FS implementations for specialized needs
Security: Like RenderTemplate, this function optionally validates and sanitizes input data to prevent template injection attacks. The fsys parameter acts as a security boundary, limiting templates to only access files within that filesystem.
Examples:
// Production deployment with embedded templates:
//go:embed templates/*
var templateFS embed.FS
result, err := RenderTemplateFS(templateFS, "email/welcome.j2", TemplateFormatJinja2, data)
// Development with directory access:
fsys := os.DirFS("./templates")
result, err := RenderTemplateFS(fsys, "report.j2", TemplateFormatJinja2, data)
Template composition features: - Jinja2: include, extends, import, from statements - Go templates: ParseFS functionality for template inheritance - F-strings: File reading from the specified filesystem
Types ¶
type AIMessagePromptTemplate ¶
type AIMessagePromptTemplate struct {
Prompt PromptTemplate
}
AIMessagePromptTemplate is a message formatter that returns an AI message.
func NewAIMessagePromptTemplate ¶
func NewAIMessagePromptTemplate(template string, inputVariables []string) AIMessagePromptTemplate
NewAIMessagePromptTemplate creates a new AI message prompt template.
func (AIMessagePromptTemplate) FormatMessages ¶
func (p AIMessagePromptTemplate) FormatMessages(values map[string]any) ([]llms.ChatMessage, error)
FormatMessages formats the message with the values given.
func (AIMessagePromptTemplate) GetInputVariables ¶
func (p AIMessagePromptTemplate) GetInputVariables() []string
GetInputVariables returns the input variables the prompt expects.
type ChatPromptTemplate ¶
type ChatPromptTemplate struct {
// Messages is the list of the messages to be formatted.
Messages []MessageFormatter
// PartialVariables represents a map of variable names to values or functions
// that return values. If the value is a function, it will be called when the
// prompt template is rendered.
PartialVariables map[string]any
}
ChatPromptTemplate is a prompt template for chat messages.
func NewChatPromptTemplate ¶
func NewChatPromptTemplate(messages []MessageFormatter) ChatPromptTemplate
NewChatPromptTemplate creates a new chat prompt template from a list of message formatters.
func (ChatPromptTemplate) Format ¶
func (p ChatPromptTemplate) Format(values map[string]any) (string, error)
Format formats the messages with values given and returns the messages as a string.
func (ChatPromptTemplate) FormatMessages ¶
func (p ChatPromptTemplate) FormatMessages(values map[string]any) ([]llms.ChatMessage, error)
FormatMessages formats the messages with the values and returns the formatted messages.
func (ChatPromptTemplate) FormatPrompt ¶
func (p ChatPromptTemplate) FormatPrompt(values map[string]any) (llms.PromptValue, error)
FormatPrompt formats the messages into a chat prompt value.
func (ChatPromptTemplate) GetInputVariables ¶
func (p ChatPromptTemplate) GetInputVariables() []string
GetInputVariables returns the input variables the prompt expect.
type ChatPromptValue ¶
type ChatPromptValue []llms.ChatMessage
ChatPromptValue is a prompt value that is a list of chat messages.
func (ChatPromptValue) Messages ¶
func (v ChatPromptValue) Messages() []llms.ChatMessage
Messages returns the ChatMessage slice.
func (ChatPromptValue) String ¶
func (v ChatPromptValue) String() string
String returns the chat message slice as a buffer string.
type ExampleSelector ¶
type ExampleSelector interface {
AddExample(example map[string]string) string
SelectExamples(inputVariables map[string]string) []map[string]string
}
ExampleSelector is an interface for example selectors. It is equivalent to BaseExampleSelector in langchain and langchainjs.
type FewShotPrompt ¶
type FewShotPrompt struct {
// Examples to format into the prompt. Either this or ExamplePrompt should be provided.
Examples []map[string]string
// ExampleSelector to choose the examples to format into the prompt. Either this or Examples should be provided.
ExampleSelector ExampleSelector
// ExamplePrompt is used to format an individual example.
ExamplePrompt PromptTemplate
// A prompt template string to put before the examples.
Prefix string
// A prompt template string to put after the examples.
Suffix string
// A list of the names of the variables the prompt template expects.
InputVariables []string
// Represents a map of variable names to values or functions that return values. If the value is a function, it will
// be called when the prompt template is rendered.
PartialVariables map[string]any
// String separator used to join the prefix, the examples, and suffix.
ExampleSeparator string
// The format of the prompt template. Options are: 'f-string', 'jinja2'.
TemplateFormat TemplateFormat
// Whether to try validating the template.
ValidateTemplate bool
}
FewShotPrompt contains fields for a few-shot prompt.
func NewFewShotPrompt ¶
func NewFewShotPrompt(examplePrompt PromptTemplate, examples []map[string]string, exampleSelector ExampleSelector, prefix string, suffix string, input []string, partialInput map[string]interface{}, exampleSeparator string, templateFormat TemplateFormat, validateTemplate bool, ) (*FewShotPrompt, error)
NewFewShotPrompt creates a new few-shot prompt with the given input. It returns error if there is no example, both examples and exampleSelector are provided, or CheckValidTemplate returns err when ValidateTemplate is true.
func (*FewShotPrompt) AssemblePieces ¶
func (p *FewShotPrompt) AssemblePieces(exampleStrings []string) string
AssemblePieces assembles the pieces of the few-shot prompt.
func (*FewShotPrompt) Format ¶
func (p *FewShotPrompt) Format(values map[string]interface{}) (string, error)
Format assembles and formats the pieces of the prompt with the given input values and partial values.
func (*FewShotPrompt) FormatPrompt ¶
func (p *FewShotPrompt) FormatPrompt(values map[string]any) (llms.PromptValue, error)
func (*FewShotPrompt) GetInputVariables ¶
func (p *FewShotPrompt) GetInputVariables() []string
type FormatPrompter ¶
type FormatPrompter interface {
FormatPrompt(values map[string]any) (llms.PromptValue, error)
GetInputVariables() []string
}
FormatPrompter is an interface for formatting a map of values into a prompt.
type GenericMessagePromptTemplate ¶
type GenericMessagePromptTemplate struct {
Prompt PromptTemplate
Role string
}
GenericMessagePromptTemplate is a message formatter that returns message with the specified speaker.
func NewGenericMessagePromptTemplate ¶
func NewGenericMessagePromptTemplate(role, template string, inputVariables []string) GenericMessagePromptTemplate
NewGenericMessagePromptTemplate creates a new generic message prompt template.
func (GenericMessagePromptTemplate) FormatMessages ¶
func (p GenericMessagePromptTemplate) FormatMessages(values map[string]any) ([]llms.ChatMessage, error)
FormatMessages formats the message with the values given.
func (GenericMessagePromptTemplate) GetInputVariables ¶
func (p GenericMessagePromptTemplate) GetInputVariables() []string
GetInputVariables returns the input variables the prompt expects.
type HumanMessagePromptTemplate ¶
type HumanMessagePromptTemplate struct {
Prompt PromptTemplate
}
HumanMessagePromptTemplate is a message formatter that returns a human message.
func NewHumanMessagePromptTemplate ¶
func NewHumanMessagePromptTemplate(template string, inputVariables []string) HumanMessagePromptTemplate
NewHumanMessagePromptTemplate creates a new human message prompt template.
func (HumanMessagePromptTemplate) FormatMessages ¶
func (p HumanMessagePromptTemplate) FormatMessages(values map[string]any) ([]llms.ChatMessage, error)
FormatMessages formats the message with the values given.
func (HumanMessagePromptTemplate) GetInputVariables ¶
func (p HumanMessagePromptTemplate) GetInputVariables() []string
GetInputVariables returns the input variables the prompt expects.
type MessageFormatter ¶
type MessageFormatter interface {
FormatMessages(values map[string]any) ([]llms.ChatMessage, error)
GetInputVariables() []string
}
MessageFormatter is an interface for formatting a map of values into a list of messages.
type MessagesPlaceholder ¶
type MessagesPlaceholder struct {
VariableName string
}
func (MessagesPlaceholder) FormatMessages ¶
func (p MessagesPlaceholder) FormatMessages(values map[string]any) ([]llms.ChatMessage, error)
FormatMessages formats the messages from the values by variable name.
func (MessagesPlaceholder) GetInputVariables ¶
func (p MessagesPlaceholder) GetInputVariables() []string
GetInputVariables returns the input variables the prompt expect.
type PromptTemplate ¶
type PromptTemplate struct {
// Template is the prompt template.
Template string
// A list of variable names the prompt template expects.
InputVariables []string
// TemplateFormat specifies the template syntax. Defaults to [TemplateFormatGoTemplate].
// See [TemplateFormat] constants.
TemplateFormat TemplateFormat
// OutputParser is a function that parses the output of the prompt template.
OutputParser schema.OutputParser[any]
// PartialVariables pre-populates common values. Functions are called at render time.
// Valid types: string, int, float64, bool or func() string, func() int,
// func() float64, func() bool.
PartialVariables map[string]any
}
PromptTemplate is a template that can be rendered with dynamic variables. It implements Formatter and FormatPrompter interfaces.
NewPromptTemplate creates a template using Go template syntax by default. Example:
template := prompts.NewPromptTemplate(
"Summarize this {{.content}} in {{.style}} style",
[]string{"content", "style"},
)
prompt, err := template.FormatPrompt(data)
func NewPromptTemplate ¶
func NewPromptTemplate(template string, inputVars []string) PromptTemplate
NewPromptTemplate creates a new PromptTemplate using TemplateFormatGoTemplate syntax. This is the recommended constructor for Go applications. The template format defaults to Go template syntax which is the idiomatic choice for Go applications.
func (PromptTemplate) Format ¶
func (p PromptTemplate) Format(values map[string]any) (string, error)
Format formats the prompt template and returns a string value.
func (PromptTemplate) FormatPrompt ¶
func (p PromptTemplate) FormatPrompt(values map[string]any) (llms.PromptValue, error)
FormatPrompt formats the prompt template and returns a string prompt value.
func (PromptTemplate) GetInputVariables ¶
func (p PromptTemplate) GetInputVariables() []string
GetInputVariables returns the input variables the prompt expect.
type RenderOption ¶
type RenderOption func(*renderConfig)
RenderOption configures template rendering behavior.
func WithSanitization ¶
func WithSanitization() RenderOption
WithSanitization enables HTML escaping of template data values. This helps prevent XSS attacks when rendering templates with untrusted data. When enabled, HTML special characters in string values will be escaped.
type StringPromptValue ¶
type StringPromptValue string
StringPromptValue is a prompt value that is a string.
func (StringPromptValue) Messages ¶
func (v StringPromptValue) Messages() []llms.ChatMessage
Messages returns a single-element ChatMessage slice.
func (StringPromptValue) String ¶
func (v StringPromptValue) String() string
type SystemMessagePromptTemplate ¶
type SystemMessagePromptTemplate struct {
Prompt PromptTemplate
}
SystemMessagePromptTemplate is a message formatter that returns a system message.
func NewSystemMessagePromptTemplate ¶
func NewSystemMessagePromptTemplate(template string, inputVariables []string) SystemMessagePromptTemplate
NewSystemMessagePromptTemplate creates a new system message prompt template.
func (SystemMessagePromptTemplate) FormatMessages ¶
func (p SystemMessagePromptTemplate) FormatMessages(values map[string]any) ([]llms.ChatMessage, error)
FormatMessages formats the message with the values given.
func (SystemMessagePromptTemplate) GetInputVariables ¶
func (p SystemMessagePromptTemplate) GetInputVariables() []string
GetInputVariables returns the input variables the prompt expects.
type TemplateFormat ¶
type TemplateFormat string
TemplateFormat is the format of the template.
const ( // TemplateFormatGoTemplate uses Go's text/template with sprig functions. // This is the recommended format for Go applications and is the default // format used by [NewPromptTemplate]. TemplateFormatGoTemplate TemplateFormat = "go-template" // TemplateFormatJinja2 uses Jinja2-style templating with filters and inheritance. TemplateFormatJinja2 TemplateFormat = "jinja2" // TemplateFormatFString uses Python-style f-string variable substitution. TemplateFormatFString TemplateFormat = "f-string" )
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
fstring
Package fstring contains template format with f-string.
|
Package fstring contains template format with f-string. |
|
loader
Package loader provides secure filesystem access control for template engines.
|
Package loader provides secure filesystem access control for template engines. |