29 lines
893 B
Go
29 lines
893 B
Go
package prompt
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// RenderUserSystem renders the system and user prompt pair for promptID.
|
|
func RenderUserSystem(promptID string, data any) (system string, user string, metadata Metadata, err error) {
|
|
trimmedID := strings.TrimSpace(promptID)
|
|
compiled, ok := promptRegistry[trimmedID]
|
|
if !ok {
|
|
return "", "", Metadata{}, fmt.Errorf("unknown prompt id %q", promptID)
|
|
}
|
|
|
|
var systemBuf bytes.Buffer
|
|
if err := compiled.systemTmpl.Execute(&systemBuf, data); err != nil {
|
|
return "", "", Metadata{}, fmt.Errorf("render system prompt %q: %w", trimmedID, err)
|
|
}
|
|
|
|
var userBuf bytes.Buffer
|
|
if err := compiled.userTmpl.Execute(&userBuf, data); err != nil {
|
|
return "", "", Metadata{}, fmt.Errorf("render user prompt %q: %w", trimmedID, err)
|
|
}
|
|
|
|
return strings.TrimSpace(systemBuf.String()), strings.TrimSpace(userBuf.String()), compiled.metadata, nil
|
|
}
|