MCPcopy
hub / github.com/mudler/LocalAI / loadTemplateIfExists

Method loadTemplateIfExists

core/templates/cache.go:43–81  ·  view source on GitHub ↗
(templateType TemplateType, templateName string)

Source from the content-addressed store, hash-verified

41 return utils.ExistsInPath(tc.templatesPath, s)
42}
43func (tc *templateCache) loadTemplateIfExists(templateType TemplateType, templateName string) error {
44
45 // Check if the template was already loaded
46 if _, ok := tc.templates[templateType][templateName]; ok {
47 return nil
48 }
49
50 // Check if the model path exists
51 // skip any error here - we run anyway if a template does not exist
52 modelTemplateFile := fmt.Sprintf("%s.tmpl", templateName)
53
54 dat := ""
55 file := filepath.Join(tc.templatesPath, modelTemplateFile)
56
57 // Security check
58 if err := utils.VerifyPath(modelTemplateFile, tc.templatesPath); err != nil {
59 return fmt.Errorf("template file outside path: %s", file)
60 }
61
62 // can either be a file in the system or a string with the template
63 if tc.existsInModelPath(modelTemplateFile) {
64 d, err := os.ReadFile(file)
65 if err != nil {
66 return err
67 }
68 dat = string(d)
69 } else {
70 dat = templateName
71 }
72
73 // Parse the template
74 tmpl, err := template.New("prompt").Funcs(sprig.FuncMap()).Parse(dat)
75 if err != nil {
76 return err
77 }
78 tc.templates[templateType][templateName] = tmpl
79
80 return nil
81}
82
83func (tc *templateCache) evaluateTemplate(templateType TemplateType, templateNameOrContent string, in any) (string, error) {
84 tc.mu.Lock()

Callers 1

evaluateTemplateMethod · 0.95

Calls 3

existsInModelPathMethod · 0.95
VerifyPathFunction · 0.92
ParseMethod · 0.80

Tested by

no test coverage detected