mirror of
https://github.com/vxcontrol/langchaingo.git
synced 2026-07-21 08:55:25 -04:00
450c02a722
chore: update all examples to use v0.1.14-pre.4 - Remove replace directives from 5 examples that had local path references - Update all 75 example go.mod files to use the latest pre-release version - Ensures examples work correctly for users who clone the repository
Prompt Template Example
This example demonstrates the core features of LangChain Go's prompt template system.
Features Demonstrated
- Basic Templates - Simple variable substitution using Go templates
- Template Formats - Using different template syntaxes (Go, Jinja2, F-string)
- Partial Variables - Pre-filling some template variables
- Chat Templates - Creating structured chat prompts
- Optional Sanitization - HTML escaping for untrusted data
Running the Example
go run main.go
Key Concepts
Template Formats
LangChain Go supports three template formats:
- Go Templates (default):
{{ .variable }} - Jinja2:
{{ variable }} - F-strings:
{variable}
Security
By default, templates render without sanitization for maximum compatibility. When working with untrusted user input, enable sanitization:
result, err := prompts.RenderTemplate(
template,
prompts.TemplateFormatGoTemplate,
data,
prompts.WithSanitization(), // Enables HTML escaping
)
Partial Variables
Partial variables let you pre-fill template values:
template := prompts.PromptTemplate{
Template: "{{.greeting}}, {{.name}}!",
InputVariables: []string{"name"},
PartialVariables: map[string]any{
"greeting": "Hello",
},
}