feat(prompts): support Sprig in templates - #337
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR enhances the template system by integrating the Sprig template function library and refactoring template handling. The main goal is to add support for Sprig template functions across the agent's prompt processing system.
- Switched from
html/templatetotext/templatepackage - Added Sprig v3 template function support via new helper functions
- Applied template processing to message content and system prompts
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| core/agent/templates.go | Added templateBase and templateExecute helper functions with Sprig support, updated import from html/template to text/template, refactored renderTemplate to use new helpers |
| core/agent/agent.go | Applied template processing to message content and system prompts using the new template helpers |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| content, err := templateExecute(promptTemplate, struct{}{}) | ||
| if err != nil { | ||
| xlog.Error("Error executing template", "error", err) | ||
| } | ||
| if !conversation.Exist(content) { |
There was a problem hiding this comment.
When template execution fails (line 375-378), the error is logged but content remains empty. The code then proceeds to check conversation.Exist(content) with an empty string on line 379, which may cause unintended behavior. Consider returning early or handling the error to prevent using an empty content value.
| promptTemplate, err := templateBase("template", content) | ||
| if err != nil { | ||
| xlog.Error("Error rendering template", "error", err) | ||
| continue | ||
| } | ||
|
|
||
| content, err = templateExecute(promptTemplate, struct{}{}) | ||
| if err != nil { | ||
| xlog.Error("Error executing template", "error", err) | ||
| continue |
There was a problem hiding this comment.
Template processing is applied to all message content regardless of whether it contains template directives. Consider checking if the content contains template markers (e.g., '{{') before creating and executing templates to avoid unnecessary parsing overhead for non-templated content.
| promptTemplate, err := templateBase("template", content) | |
| if err != nil { | |
| xlog.Error("Error rendering template", "error", err) | |
| continue | |
| } | |
| content, err = templateExecute(promptTemplate, struct{}{}) | |
| if err != nil { | |
| xlog.Error("Error executing template", "error", err) | |
| continue | |
| if strings.Contains(content, "{{") { | |
| promptTemplate, err := templateBase("template", content) | |
| if err != nil { | |
| xlog.Error("Error rendering template", "error", err) | |
| continue | |
| } | |
| content, err = templateExecute(promptTemplate, struct{}{}) | |
| if err != nil { | |
| xlog.Error("Error executing template", "error", err) | |
| continue | |
| } |
| continue | ||
| } | ||
|
|
||
| content, err = templateExecute(promptTemplate, struct{}{}) |
There was a problem hiding this comment.
An empty struct struct{}{} is passed as template data, which means templates cannot access any dynamic values. Consider defining a proper data structure with relevant context (e.g., agent state, character info, timestamps) to make the template functionality more useful.
Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
22affa1 to
e1516dd
Compare
This PR adds support to Sprig into prompt templates