template.go 9.48 KB
Newer Older
Michael Yang's avatar
Michael Yang committed
1
2
3
4
5
6
7
8
package template

import (
	"bytes"
	"embed"
	"encoding/json"
	"errors"
	"io"
Michael Yang's avatar
Michael Yang committed
9
	"maps"
Michael Yang's avatar
Michael Yang committed
10
11
12
13
14
15
16
17
	"math"
	"slices"
	"strings"
	"sync"
	"text/template"
	"text/template/parse"

	"github.com/agnivade/levenshtein"
Michael Yang's avatar
lint  
Michael Yang committed
18
19

	"github.com/ollama/ollama/api"
Michael Yang's avatar
Michael Yang committed
20
21
22
23
24
25
)

//go:embed index.json
var indexBytes []byte

//go:embed *.gotmpl
26
//go:embed *.json
Michael Yang's avatar
Michael Yang committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
var templatesFS embed.FS

var templatesOnce = sync.OnceValues(func() ([]*named, error) {
	var templates []*named
	if err := json.Unmarshal(indexBytes, &templates); err != nil {
		return nil, err
	}

	for _, t := range templates {
		bts, err := templatesFS.ReadFile(t.Name + ".gotmpl")
		if err != nil {
			return nil, err
		}

		// normalize line endings
		t.Bytes = bytes.ReplaceAll(bts, []byte("\r\n"), []byte("\n"))
43
44
45
46
47
48
49
50
51

		params, err := templatesFS.ReadFile(t.Name + ".json")
		if err != nil {
			continue
		}

		if err := json.Unmarshal(params, &t.Parameters); err != nil {
			return nil, err
		}
Michael Yang's avatar
Michael Yang committed
52
53
54
55
56
57
58
59
60
	}

	return templates, nil
})

type named struct {
	Name     string `json:"name"`
	Template string `json:"template"`
	Bytes    []byte
61
62
63
64

	Parameters *struct {
		Stop []string `json:"stop"`
	}
Michael Yang's avatar
Michael Yang committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
}

func (t named) Reader() io.Reader {
	return bytes.NewReader(t.Bytes)
}

func Named(s string) (*named, error) {
	templates, err := templatesOnce()
	if err != nil {
		return nil, err
	}

	var template *named
	score := math.MaxInt
	for _, t := range templates {
		if s := levenshtein.ComputeDistance(s, t.Template); s < score {
			score = s
			template = t
		}
	}

	if score < 100 {
		return template, nil
	}

	return nil, errors.New("no matching template found")
}

Michael Yang's avatar
Michael Yang committed
93
94
var DefaultTemplate, _ = Parse("{{ .Prompt }}")

Michael Yang's avatar
Michael Yang committed
95
96
97
98
99
type Template struct {
	*template.Template
	raw string
}

Michael Yang's avatar
Michael Yang committed
100
// response is a template node that can be added to templates that don't already have one
Michael Yang's avatar
Michael Yang committed
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
var response = parse.ActionNode{
	NodeType: parse.NodeAction,
	Pipe: &parse.PipeNode{
		NodeType: parse.NodePipe,
		Cmds: []*parse.CommandNode{
			{
				NodeType: parse.NodeCommand,
				Args: []parse.Node{
					&parse.FieldNode{
						NodeType: parse.NodeField,
						Ident:    []string{"Response"},
					},
				},
			},
		},
	},
Michael Yang's avatar
Michael Yang committed
117
118
}

Michael Yang's avatar
tools  
Michael Yang committed
119
120
121
122
123
124
125
var funcs = template.FuncMap{
	"json": func(v any) string {
		b, _ := json.Marshal(v)
		return string(b)
	},
}

Michael Yang's avatar
Michael Yang committed
126
func Parse(s string) (*Template, error) {
Michael Yang's avatar
tools  
Michael Yang committed
127
	tmpl := template.New("").Option("missingkey=zero").Funcs(funcs)
Michael Yang's avatar
Michael Yang committed
128
129

	tmpl, err := tmpl.Parse(s)
Michael Yang's avatar
Michael Yang committed
130
131
132
133
	if err != nil {
		return nil, err
	}

Michael Yang's avatar
Michael Yang committed
134
135
136
137
138
139
140
141
142
143
144
	t := Template{Template: tmpl, raw: s}
	if vars := t.Vars(); !slices.Contains(vars, "messages") && !slices.Contains(vars, "response") {
		// touch up the template and append {{ .Response }}
		tmpl.Tree.Root.Nodes = append(tmpl.Tree.Root.Nodes, &response)
	}

	return &t, nil
}

func (t *Template) String() string {
	return t.raw
Michael Yang's avatar
Michael Yang committed
145
146
147
148
}

func (t *Template) Vars() []string {
	var vars []string
Michael Yang's avatar
Michael Yang committed
149
150
	for _, tt := range t.Templates() {
		for _, n := range tt.Root.Nodes {
Michael Yang's avatar
tools  
Michael Yang committed
151
			vars = append(vars, Identifiers(n)...)
Michael Yang's avatar
Michael Yang committed
152
		}
Michael Yang's avatar
Michael Yang committed
153
154
155
156
157
158
159
	}

	set := make(map[string]struct{})
	for _, n := range vars {
		set[strings.ToLower(n)] = struct{}{}
	}

Michael Yang's avatar
Michael Yang committed
160
	return slices.Sorted(maps.Keys(set))
Michael Yang's avatar
Michael Yang committed
161
162
}

Michael Yang's avatar
Michael Yang committed
163
164
type Values struct {
	Messages []api.Message
165
166
167
	api.Tools
	Prompt string
	Suffix string
168
169
170
171
	Think  bool
	// whether or not the user explicitly set the thinking flag (vs. it being
	// implicitly false). Templates can't see whether `Think` is nil
	IsThinkSet bool
172
173
174

	// forceLegacy is a flag used to test compatibility with legacy templates
	forceLegacy bool
Michael Yang's avatar
Michael Yang committed
175
176
}

Michael Yang's avatar
tools  
Michael Yang committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
func (t *Template) Subtree(fn func(parse.Node) bool) *template.Template {
	var walk func(parse.Node) parse.Node
	walk = func(n parse.Node) parse.Node {
		if fn(n) {
			return n
		}

		switch t := n.(type) {
		case *parse.ListNode:
			for _, c := range t.Nodes {
				if n := walk(c); n != nil {
					return n
				}
			}
		case *parse.BranchNode:
			for _, n := range []*parse.ListNode{t.List, t.ElseList} {
				if n != nil {
					if n := walk(n); n != nil {
						return n
					}
				}
			}
		case *parse.IfNode:
			return walk(&t.BranchNode)
		case *parse.WithNode:
			return walk(&t.BranchNode)
		case *parse.RangeNode:
			return walk(&t.BranchNode)
		}

		return nil
	}

	if n := walk(t.Tree.Root); n != nil {
		return (&template.Template{
			Tree: &parse.Tree{
				Root: &parse.ListNode{
					Nodes: []parse.Node{n},
				},
			},
		}).Funcs(funcs)
	}

	return nil
}

Michael Yang's avatar
Michael Yang committed
223
func (t *Template) Execute(w io.Writer, v Values) error {
Michael Yang's avatar
Michael Yang committed
224
	system, messages := collate(v.Messages)
225
226
	if v.Prompt != "" && v.Suffix != "" {
		return t.Template.Execute(w, map[string]any{
227
228
229
230
231
			"Prompt":     v.Prompt,
			"Suffix":     v.Suffix,
			"Response":   "",
			"Think":      v.Think,
			"IsThinkSet": v.IsThinkSet,
232
233
		})
	} else if !v.forceLegacy && slices.Contains(t.Vars(), "messages") {
Michael Yang's avatar
Michael Yang committed
234
		return t.Template.Execute(w, map[string]any{
235
236
237
238
239
240
			"System":     system,
			"Messages":   messages,
			"Tools":      v.Tools,
			"Response":   "",
			"Think":      v.Think,
			"IsThinkSet": v.IsThinkSet,
Michael Yang's avatar
Michael Yang committed
241
242
243
		})
	}

Michael Yang's avatar
Michael Yang committed
244
	system = ""
Michael Yang's avatar
Michael Yang committed
245
	var b bytes.Buffer
246
	var prompt, response string
Michael Yang's avatar
Michael Yang committed
247
	for _, m := range messages {
Michael Yang's avatar
tools  
Michael Yang committed
248
		execute := func() error {
Michael Yang's avatar
Michael Yang committed
249
			if err := t.Template.Execute(&b, map[string]any{
250
251
252
253
254
				"System":     system,
				"Prompt":     prompt,
				"Response":   response,
				"Think":      v.Think,
				"IsThinkSet": v.IsThinkSet,
Michael Yang's avatar
Michael Yang committed
255
256
257
258
			}); err != nil {
				return err
			}

259
			system = ""
Michael Yang's avatar
Michael Yang committed
260
261
			prompt = ""
			response = ""
Michael Yang's avatar
Michael Yang committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
			return nil
		}

		switch m.Role {
		case "system":
			if prompt != "" || response != "" {
				if err := execute(); err != nil {
					return err
				}
			}
			system = m.Content
		case "user":
			if response != "" {
				if err := execute(); err != nil {
					return err
				}
			}
			prompt = m.Content
		case "assistant":
			response = m.Content
Michael Yang's avatar
Michael Yang committed
282
283
284
285
		}
	}

	var cut bool
286
	nodes := deleteNode(t.Template.Root.Copy(), func(n parse.Node) bool {
Michael Yang's avatar
tools  
Michael Yang committed
287
288
		if field, ok := n.(*parse.FieldNode); ok && slices.Contains(field.Ident, "Response") {
			cut = true
289
			return false
Michael Yang's avatar
Michael Yang committed
290
291
292
293
294
		}

		return cut
	})

295
296
	tree := parse.Tree{Root: nodes.(*parse.ListNode)}
	if err := template.Must(template.New("").AddParseTree("", &tree)).Execute(&b, map[string]any{
297
298
299
300
301
		"System":     system,
		"Prompt":     prompt,
		"Response":   response,
		"Think":      v.Think,
		"IsThinkSet": v.IsThinkSet,
Michael Yang's avatar
Michael Yang committed
302
303
304
305
306
307
308
309
	}); err != nil {
		return err
	}

	_, err := io.Copy(w, &b)
	return err
}

Michael Yang's avatar
Michael Yang committed
310
// collate messages based on role. consecutive messages of the same role are merged
311
312
// into a single message (except for tool messages which preserve individual metadata).
// collate also collects and returns all system messages.
313
// collate mutates message content adding image tags ([img-%d]) as needed
314
// todo(parthsareen): revisit for contextual image support
315
316
317
func collate(msgs []api.Message) (string, []*api.Message) {
	var system []string
	var collated []*api.Message
Michael Yang's avatar
Michael Yang committed
318
	for i := range msgs {
319
320
		if msgs[i].Role == "system" {
			system = append(system, msgs[i].Content)
321
322
		}

323
324
325
		// merges consecutive messages of the same role into a single message (except for tool messages)
		if len(collated) > 0 && collated[len(collated)-1].Role == msgs[i].Role && msgs[i].Role != "tool" {
			collated[len(collated)-1].Content += "\n\n" + msgs[i].Content
Michael Yang's avatar
Michael Yang committed
326
		} else {
327
			collated = append(collated, &msgs[i])
Michael Yang's avatar
Michael Yang committed
328
329
330
		}
	}

331
	return strings.Join(system, "\n\n"), collated
Michael Yang's avatar
Michael Yang committed
332
333
}

Michael Yang's avatar
tools  
Michael Yang committed
334
335
// Identifiers walks the node tree returning any identifiers it finds along the way
func Identifiers(n parse.Node) []string {
Michael Yang's avatar
Michael Yang committed
336
	switch n := n.(type) {
Michael Yang's avatar
tools  
Michael Yang committed
337
338
339
340
	case *parse.ListNode:
		var names []string
		for _, n := range n.Nodes {
			names = append(names, Identifiers(n)...)
Michael Yang's avatar
Michael Yang committed
341
		}
Michael Yang's avatar
tools  
Michael Yang committed
342

Michael Yang's avatar
Michael Yang committed
343
		return names
Michael Yang's avatar
tools  
Michael Yang committed
344
345
346
347
348
349
350
351
352
353
	case *parse.TemplateNode:
		return Identifiers(n.Pipe)
	case *parse.ActionNode:
		return Identifiers(n.Pipe)
	case *parse.BranchNode:
		names := Identifiers(n.Pipe)
		for _, n := range []*parse.ListNode{n.List, n.ElseList} {
			if n != nil {
				names = append(names, Identifiers(n)...)
			}
Michael Yang's avatar
Michael Yang committed
354
355
		}
		return names
Michael Yang's avatar
tools  
Michael Yang committed
356
357
358
359
	case *parse.IfNode:
		return Identifiers(&n.BranchNode)
	case *parse.RangeNode:
		return Identifiers(&n.BranchNode)
Michael Yang's avatar
Michael Yang committed
360
	case *parse.WithNode:
Michael Yang's avatar
tools  
Michael Yang committed
361
		return Identifiers(&n.BranchNode)
Michael Yang's avatar
Michael Yang committed
362
363
364
365
	case *parse.PipeNode:
		var names []string
		for _, c := range n.Cmds {
			for _, a := range c.Args {
Michael Yang's avatar
tools  
Michael Yang committed
366
				names = append(names, Identifiers(a)...)
Michael Yang's avatar
Michael Yang committed
367
368
369
370
371
			}
		}
		return names
	case *parse.FieldNode:
		return n.Ident
Michael Yang's avatar
tools  
Michael Yang committed
372
373
	case *parse.VariableNode:
		return n.Ident
Michael Yang's avatar
Michael Yang committed
374
375
376
377
	}

	return nil
}
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446

// deleteNode walks the node list and deletes nodes that match the predicate
// this is currently to remove the {{ .Response }} node from templates
func deleteNode(n parse.Node, fn func(parse.Node) bool) parse.Node {
	var walk func(n parse.Node) parse.Node
	walk = func(n parse.Node) parse.Node {
		if fn(n) {
			return nil
		}

		switch t := n.(type) {
		case *parse.ListNode:
			var nodes []parse.Node
			for _, c := range t.Nodes {
				if n := walk(c); n != nil {
					nodes = append(nodes, n)
				}
			}

			t.Nodes = nodes
			return t
		case *parse.IfNode:
			t.BranchNode = *(walk(&t.BranchNode).(*parse.BranchNode))
		case *parse.WithNode:
			t.BranchNode = *(walk(&t.BranchNode).(*parse.BranchNode))
		case *parse.RangeNode:
			t.BranchNode = *(walk(&t.BranchNode).(*parse.BranchNode))
		case *parse.BranchNode:
			t.List = walk(t.List).(*parse.ListNode)
			if t.ElseList != nil {
				t.ElseList = walk(t.ElseList).(*parse.ListNode)
			}
		case *parse.ActionNode:
			n := walk(t.Pipe)
			if n == nil {
				return nil
			}

			t.Pipe = n.(*parse.PipeNode)
		case *parse.PipeNode:
			var commands []*parse.CommandNode
			for _, c := range t.Cmds {
				var args []parse.Node
				for _, a := range c.Args {
					if n := walk(a); n != nil {
						args = append(args, n)
					}
				}

				if len(args) == 0 {
					return nil
				}

				c.Args = args
				commands = append(commands, c)
			}

			if len(commands) == 0 {
				return nil
			}

			t.Cmds = commands
		}

		return n
	}

	return walk(n)
}