Merge pull request #1244 from jmorganca/brucemacd/no-fail-template

do not fail on unsupported template variables
This commit is contained in:
Michael Yang 2023-12-06 13:23:04 -08:00 committed by GitHub
commit 1f05d77110
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -58,18 +58,21 @@ type PromptVars struct {
func (m *Model) Prompt(p PromptVars) (string, error) { func (m *Model) Prompt(p PromptVars) (string, error) {
var prompt strings.Builder var prompt strings.Builder
tmpl, err := template.New("").Parse(m.Template) // Use the "missingkey=zero" option to handle missing variables without panicking
tmpl, err := template.New("").Option("missingkey=zero").Parse(m.Template)
if err != nil { if err != nil {
return "", err return "", err
} }
if p.System == "" { vars := map[string]any{
// use the default system prompt for this model if one is not specified "System": p.System,
p.System = m.System "Prompt": p.Prompt,
"Response": p.Response,
"First": p.First,
} }
var sb strings.Builder var sb strings.Builder
if err := tmpl.Execute(&sb, p); err != nil { if err := tmpl.Execute(&sb, vars); err != nil {
return "", err return "", err
} }
prompt.WriteString(sb.String()) prompt.WriteString(sb.String())