parser.go 14.4 KB
Newer Older
1
package parser
2
3
4

import (
	"bufio"
5
	"bytes"
6
	"crypto/sha256"
7
	"errors"
8
	"fmt"
9
	"io"
10
11
12
13
	"net/http"
	"os"
	"os/user"
	"path/filepath"
Michael Yang's avatar
Michael Yang committed
14
	"runtime"
15
	"slices"
Michael Yang's avatar
Michael Yang committed
16
17
	"strconv"
	"strings"
Michael Yang's avatar
Michael Yang committed
18
	"sync"
Michael Yang's avatar
Michael Yang committed
19

Michael Yang's avatar
Michael Yang committed
20
	"golang.org/x/sync/errgroup"
Michael Yang's avatar
Michael Yang committed
21
22
	"golang.org/x/text/encoding/unicode"
	"golang.org/x/text/transform"
23
24

	"github.com/ollama/ollama/api"
25
26
)

27
28
29
var ErrModelNotFound = errors.New("no Modelfile or safetensors files found")

type Modelfile struct {
Michael Yang's avatar
Michael Yang committed
30
31
32
	Commands []Command
}

33
func (f Modelfile) String() string {
Michael Yang's avatar
Michael Yang committed
34
35
36
37
38
39
40
41
	var sb strings.Builder
	for _, cmd := range f.Commands {
		fmt.Fprintln(&sb, cmd.String())
	}

	return sb.String()
}

42
43
44
45
46
47
48
var deprecatedParameters = []string{
	"penalize_newline",
	"low_vram",
	"f16_kv",
	"logits_all",
	"vocab_only",
	"use_mlock",
49
50
51
	"mirostat",
	"mirostat_tau",
	"mirostat_eta",
52
}
53

54
// CreateRequest creates a new *api.CreateRequest from an existing Modelfile
55
func (f Modelfile) CreateRequest(relativeDir string) (*api.CreateRequest, error) {
56
57
58
59
60
61
62
63
64
	req := &api.CreateRequest{}

	var messages []api.Message
	var licenses []string
	params := make(map[string]any)

	for _, c := range f.Commands {
		switch c.Name {
		case "model":
65
			path, err := expandPath(c.Args, relativeDir)
66
67
68
69
70
71
72
73
74
75
76
77
			if err != nil {
				return nil, err
			}

			digestMap, err := fileDigestMap(path)
			if errors.Is(err, os.ErrNotExist) {
				req.From = c.Args
				continue
			} else if err != nil {
				return nil, err
			}

78
79
80
81
82
83
84
			if req.Files == nil {
				req.Files = digestMap
			} else {
				for k, v := range digestMap {
					req.Files[k] = v
				}
			}
85
		case "adapter":
86
			path, err := expandPath(c.Args, relativeDir)
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
			if err != nil {
				return nil, err
			}

			digestMap, err := fileDigestMap(path)
			if err != nil {
				return nil, err
			}

			req.Adapters = digestMap
		case "template":
			req.Template = c.Args
		case "system":
			req.System = c.Args
		case "license":
			licenses = append(licenses, c.Args)
		case "message":
			role, msg, _ := strings.Cut(c.Args, ": ")
			messages = append(messages, api.Message{Role: role, Content: msg})
		default:
107
108
109
110
111
			if slices.Contains(deprecatedParameters, c.Name) {
				fmt.Printf("warning: parameter %s is deprecated\n", c.Name)
				break
			}

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
			ps, err := api.FormatParams(map[string][]string{c.Name: {c.Args}})
			if err != nil {
				return nil, err
			}

			for k, v := range ps {
				if ks, ok := params[k].([]string); ok {
					params[k] = append(ks, v.([]string)...)
				} else if vs, ok := v.([]string); ok {
					params[k] = vs
				} else {
					params[k] = v
				}
			}
		}
	}

	if len(params) > 0 {
		req.Parameters = params
	}
	if len(messages) > 0 {
		req.Messages = messages
	}
	if len(licenses) > 0 {
		req.License = licenses
	}

	return req, nil
}

func fileDigestMap(path string) (map[string]string, error) {
	fl := make(map[string]string)

	fi, err := os.Stat(path)
	if err != nil {
		return nil, err
	}

	var files []string
	if fi.IsDir() {
152
		fs, err := filesForModel(path)
153
154
155
		if err != nil {
			return nil, err
		}
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

		for _, f := range fs {
			f, err := filepath.EvalSymlinks(f)
			if err != nil {
				return nil, err
			}

			rel, err := filepath.Rel(path, f)
			if err != nil {
				return nil, err
			}

			if !filepath.IsLocal(rel) {
				return nil, fmt.Errorf("insecure path: %s", rel)
			}

			files = append(files, f)
		}
174
175
176
177
	} else {
		files = []string{path}
	}

Michael Yang's avatar
Michael Yang committed
178
179
180
	var mu sync.Mutex
	var g errgroup.Group
	g.SetLimit(max(runtime.GOMAXPROCS(0)-1, 1))
181
	for _, f := range files {
Michael Yang's avatar
Michael Yang committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
		g.Go(func() error {
			digest, err := digestForFile(f)
			if err != nil {
				return err
			}

			mu.Lock()
			defer mu.Unlock()
			fl[f] = digest
			return nil
		})
	}

	if err := g.Wait(); err != nil {
		return nil, err
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
	}

	return fl, nil
}

func digestForFile(filename string) (string, error) {
	filepath, err := filepath.EvalSymlinks(filename)
	if err != nil {
		return "", err
	}

	bin, err := os.Open(filepath)
	if err != nil {
		return "", err
	}
	defer bin.Close()

	hash := sha256.New()
	if _, err := io.Copy(hash, bin); err != nil {
		return "", err
	}
	return fmt.Sprintf("sha256:%x", hash.Sum(nil)), nil
}

func filesForModel(path string) ([]string, error) {
	detectContentType := func(path string) (string, error) {
		f, err := os.Open(path)
		if err != nil {
			return "", err
		}
		defer f.Close()

		var b bytes.Buffer
		b.Grow(512)

		if _, err := io.CopyN(&b, f, 512); err != nil && !errors.Is(err, io.EOF) {
			return "", err
		}

		contentType, _, _ := strings.Cut(http.DetectContentType(b.Bytes()), ";")
		return contentType, nil
	}

	glob := func(pattern, contentType string) ([]string, error) {
		matches, err := filepath.Glob(pattern)
		if err != nil {
			return nil, err
		}

246
247
		for _, match := range matches {
			if ct, err := detectContentType(match); err != nil {
248
249
				return nil, err
			} else if ct != contentType {
250
				return nil, fmt.Errorf("invalid content type: expected %s for %s", ct, match)
251
252
253
254
255
256
257
			}
		}

		return matches, nil
	}

	var files []string
258
	if st, _ := glob(filepath.Join(path, "*.safetensors"), "application/octet-stream"); len(st) > 0 {
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
		// safetensors files might be unresolved git lfs references; skip if they are
		// covers model-x-of-y.safetensors, model.fp32-x-of-y.safetensors, model.safetensors
		files = append(files, st...)
	} else if pt, _ := glob(filepath.Join(path, "pytorch_model*.bin"), "application/zip"); len(pt) > 0 {
		// pytorch files might also be unresolved git lfs references; skip if they are
		// covers pytorch_model-x-of-y.bin, pytorch_model.fp32-x-of-y.bin, pytorch_model.bin
		files = append(files, pt...)
	} else if pt, _ := glob(filepath.Join(path, "consolidated*.pth"), "application/zip"); len(pt) > 0 {
		// pytorch files might also be unresolved git lfs references; skip if they are
		// covers consolidated.x.pth, consolidated.pth
		files = append(files, pt...)
	} else if gg, _ := glob(filepath.Join(path, "*.gguf"), "application/octet-stream"); len(gg) > 0 {
		// covers gguf files ending in .gguf
		files = append(files, gg...)
	} else if gg, _ := glob(filepath.Join(path, "*.bin"), "application/octet-stream"); len(gg) > 0 {
		// covers gguf files ending in .bin
		files = append(files, gg...)
	} else {
		return nil, ErrModelNotFound
	}

	// add configuration files, json files are detected as text/plain
	js, err := glob(filepath.Join(path, "*.json"), "text/plain")
	if err != nil {
		return nil, err
	}
	files = append(files, js...)

	// bert models require a nested config.json
	// TODO(mxyng): merge this with the glob above
	js, err = glob(filepath.Join(path, "**/*.json"), "text/plain")
	if err != nil {
		return nil, err
	}
	files = append(files, js...)

295
296
297
298
299
300
301
302
303
304
305
306
	// only include tokenizer.model is tokenizer.json is not present
	if !slices.ContainsFunc(files, func(s string) bool {
		return slices.Contains(strings.Split(s, string(os.PathSeparator)), "tokenizer.json")
	}) {
		if tks, _ := glob(filepath.Join(path, "tokenizer.model"), "application/octet-stream"); len(tks) > 0 {
			// add tokenizer.model if it exists, tokenizer.json is automatically picked up by the previous glob
			// tokenizer.model might be a unresolved git lfs reference; error if it is
			files = append(files, tks...)
		} else if tks, _ := glob(filepath.Join(path, "**/tokenizer.model"), "text/plain"); len(tks) > 0 {
			// some times tokenizer.model is in a subdirectory (e.g. meta-llama/Meta-Llama-3-8B)
			files = append(files, tks...)
		}
307
308
309
310
311
	}

	return files, nil
}

312
313
type Command struct {
	Name string
314
315
316
	Args string
}

Michael Yang's avatar
Michael Yang committed
317
func (c Command) String() string {
Michael Yang's avatar
Michael Yang committed
318
	var sb strings.Builder
Michael Yang's avatar
Michael Yang committed
319
320
	switch c.Name {
	case "model":
Michael Yang's avatar
Michael Yang committed
321
		fmt.Fprintf(&sb, "FROM %s", c.Args)
Michael Yang's avatar
Michael Yang committed
322
	case "license", "template", "system", "adapter":
Michael Yang's avatar
Michael Yang committed
323
		fmt.Fprintf(&sb, "%s %s", strings.ToUpper(c.Name), quote(c.Args))
Michael Yang's avatar
Michael Yang committed
324
325
	case "message":
		role, message, _ := strings.Cut(c.Args, ": ")
Michael Yang's avatar
Michael Yang committed
326
		fmt.Fprintf(&sb, "MESSAGE %s %s", role, quote(message))
Michael Yang's avatar
Michael Yang committed
327
	default:
Michael Yang's avatar
Michael Yang committed
328
		fmt.Fprintf(&sb, "PARAMETER %s %s", c.Name, quote(c.Args))
Michael Yang's avatar
Michael Yang committed
329
330
	}

Michael Yang's avatar
Michael Yang committed
331
	return sb.String()
Michael Yang's avatar
Michael Yang committed
332
333
}

Michael Yang's avatar
Michael Yang committed
334
type state int
335

Michael Yang's avatar
Michael Yang committed
336
337
338
339
340
341
342
343
const (
	stateNil state = iota
	stateName
	stateValue
	stateParameter
	stateMessage
	stateComment
)
344

Michael Yang's avatar
tests  
Michael Yang committed
345
var (
346
347
348
	errMissingFrom        = errors.New("no FROM line")
	errInvalidMessageRole = errors.New("message role must be one of \"system\", \"user\", or \"assistant\"")
	errInvalidCommand     = errors.New("command must be one of \"from\", \"license\", \"template\", \"system\", \"adapter\", \"parameter\", or \"message\"")
Michael Yang's avatar
tests  
Michael Yang committed
349
)
Michael Yang's avatar
Michael Yang committed
350

351
352
353
354
355
356
357
358
359
360
361
362
type ParserError struct {
	LineNumber int
	Msg        string
}

func (e *ParserError) Error() string {
	if e.LineNumber > 0 {
		return fmt.Sprintf("(line %d): %s", e.LineNumber, e.Msg)
	}
	return e.Msg
}

363
func ParseFile(r io.Reader) (*Modelfile, error) {
Michael Yang's avatar
Michael Yang committed
364
365
	var cmd Command
	var curr state
366
	var currLine int = 1
Michael Yang's avatar
Michael Yang committed
367
368
369
	var b bytes.Buffer
	var role string

370
	var f Modelfile
Michael Yang's avatar
Michael Yang committed
371

Michael Yang's avatar
Michael Yang committed
372
373
374
	tr := unicode.BOMOverride(unicode.UTF8.NewDecoder())
	br := bufio.NewReader(transform.NewReader(r, tr))

Michael Yang's avatar
Michael Yang committed
375
376
377
378
379
380
381
	for {
		r, _, err := br.ReadRune()
		if errors.Is(err, io.EOF) {
			break
		} else if err != nil {
			return nil, err
		}
382

383
384
385
386
		if isNewline(r) {
			currLine++
		}

Michael Yang's avatar
Michael Yang committed
387
388
389
390
		next, r, err := parseRuneForState(r, curr)
		if errors.Is(err, io.ErrUnexpectedEOF) {
			return nil, fmt.Errorf("%w: %s", err, b.String())
		} else if err != nil {
391
392
393
394
			return nil, &ParserError{
				LineNumber: currLine,
				Msg:        err.Error(),
			}
395
396
		}

Michael Yang's avatar
Michael Yang committed
397
		// process the state transition, some transitions need to be intercepted and redirected
Michael Yang's avatar
Michael Yang committed
398
399
		if next != curr {
			switch curr {
Michael Yang's avatar
Michael Yang committed
400
401
			case stateName:
				if !isValidCommand(b.String()) {
402
403
404
405
					return nil, &ParserError{
						LineNumber: currLine,
						Msg:        errInvalidCommand.Error(),
					}
Michael Yang's avatar
Michael Yang committed
406
407
				}

Michael Yang's avatar
Michael Yang committed
408
				// next state sometimes depends on the current buffer value
Michael Yang's avatar
Michael Yang committed
409
410
411
412
				switch s := strings.ToLower(b.String()); s {
				case "from":
					cmd.Name = "model"
				case "parameter":
Michael Yang's avatar
Michael Yang committed
413
					// transition to stateParameter which sets command name
Michael Yang's avatar
Michael Yang committed
414
415
					next = stateParameter
				case "message":
Michael Yang's avatar
Michael Yang committed
416
					// transition to stateMessage which validates the message role
Michael Yang's avatar
Michael Yang committed
417
418
419
420
421
					next = stateMessage
					fallthrough
				default:
					cmd.Name = s
				}
Michael Yang's avatar
Michael Yang committed
422
423
			case stateParameter:
				cmd.Name = b.String()
Michael Yang's avatar
Michael Yang committed
424
			case stateMessage:
425
426
427
428
429
				if !isValidMessageRole(b.String()) {
					return nil, &ParserError{
						LineNumber: currLine,
						Msg:        errInvalidMessageRole.Error(),
					}
430
				}
431
432

				role = b.String()
Michael Yang's avatar
Michael Yang committed
433
434
435
			case stateComment, stateNil:
				// pass
			case stateValue:
Josh Yan's avatar
Josh Yan committed
436
				s, ok := unquote(strings.TrimSpace(b.String()))
Michael Yang's avatar
Michael Yang committed
437
438
439
440
441
442
443
444
445
446
447
448
449
450
				if !ok || isSpace(r) {
					if _, err := b.WriteRune(r); err != nil {
						return nil, err
					}

					continue
				}

				if role != "" {
					s = role + ": " + s
					role = ""
				}

				cmd.Args = s
Michael Yang's avatar
Michael Yang committed
451
				f.Commands = append(f.Commands, cmd)
Michael Yang's avatar
Michael Yang committed
452
453
			}

Michael Yang's avatar
Michael Yang committed
454
455
456
457
458
459
460
			b.Reset()
			curr = next
		}

		if strconv.IsPrint(r) {
			if _, err := b.WriteRune(r); err != nil {
				return nil, err
Michael Yang's avatar
Michael Yang committed
461
			}
Michael Yang's avatar
Michael Yang committed
462
463
464
465
466
467
468
469
		}
	}

	// flush the buffer
	switch curr {
	case stateComment, stateNil:
		// pass; nothing to flush
	case stateValue:
Josh Yan's avatar
Josh Yan committed
470
		s, ok := unquote(strings.TrimSpace(b.String()))
Michael Yang's avatar
Michael Yang committed
471
		if !ok {
Michael Yang's avatar
Michael Yang committed
472
			return nil, io.ErrUnexpectedEOF
473
		}
474

Michael Yang's avatar
Michael Yang committed
475
476
477
478
479
		if role != "" {
			s = role + ": " + s
		}

		cmd.Args = s
Michael Yang's avatar
Michael Yang committed
480
		f.Commands = append(f.Commands, cmd)
Michael Yang's avatar
Michael Yang committed
481
482
	default:
		return nil, io.ErrUnexpectedEOF
483
484
	}

Michael Yang's avatar
Michael Yang committed
485
	for _, cmd := range f.Commands {
Michael Yang's avatar
Michael Yang committed
486
		if cmd.Name == "model" {
Michael Yang's avatar
Michael Yang committed
487
			return &f, nil
Michael Yang's avatar
Michael Yang committed
488
		}
489
490
	}

Michael Yang's avatar
tests  
Michael Yang committed
491
	return nil, errMissingFrom
492
}
493

Michael Yang's avatar
Michael Yang committed
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
func parseRuneForState(r rune, cs state) (state, rune, error) {
	switch cs {
	case stateNil:
		switch {
		case r == '#':
			return stateComment, 0, nil
		case isSpace(r), isNewline(r):
			return stateNil, 0, nil
		default:
			return stateName, r, nil
		}
	case stateName:
		switch {
		case isAlpha(r):
			return stateName, r, nil
		case isSpace(r):
			return stateValue, 0, nil
		default:
Michael Yang's avatar
Michael Yang committed
512
			return stateNil, 0, errInvalidCommand
Michael Yang's avatar
Michael Yang committed
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
		}
	case stateValue:
		switch {
		case isNewline(r):
			return stateNil, r, nil
		case isSpace(r):
			return stateNil, r, nil
		default:
			return stateValue, r, nil
		}
	case stateParameter:
		switch {
		case isAlpha(r), isNumber(r), r == '_':
			return stateParameter, r, nil
		case isSpace(r):
			return stateValue, 0, nil
		default:
			return stateNil, 0, io.ErrUnexpectedEOF
		}
	case stateMessage:
		switch {
		case isAlpha(r):
			return stateMessage, r, nil
		case isSpace(r):
			return stateValue, 0, nil
		default:
			return stateNil, 0, io.ErrUnexpectedEOF
		}
	case stateComment:
		switch {
		case isNewline(r):
			return stateNil, 0, nil
		default:
			return stateComment, 0, nil
		}
	default:
		return stateNil, 0, errors.New("")
550
	}
Michael Yang's avatar
Michael Yang committed
551
}
552

Michael Yang's avatar
Michael Yang committed
553
func quote(s string) string {
554
	if strings.Contains(s, "\n") || strings.HasPrefix(s, " ") || strings.HasSuffix(s, " ") {
Michael Yang's avatar
Michael Yang committed
555
556
557
558
		if strings.Contains(s, "\"") {
			return `"""` + s + `"""`
		}

559
		return `"` + s + `"`
Michael Yang's avatar
Michael Yang committed
560
561
562
563
564
	}

	return s
}

Michael Yang's avatar
Michael Yang committed
565
566
567
568
569
570
571
572
func unquote(s string) (string, bool) {
	// TODO: single quotes
	if len(s) >= 3 && s[:3] == `"""` {
		if len(s) >= 6 && s[len(s)-3:] == `"""` {
			return s[3 : len(s)-3], true
		}

		return "", false
573
574
	}

Michael Yang's avatar
Michael Yang committed
575
576
577
578
579
580
	if len(s) >= 1 && s[0] == '"' {
		if len(s) >= 2 && s[len(s)-1] == '"' {
			return s[1 : len(s)-1], true
		}

		return "", false
581
582
	}

Michael Yang's avatar
Michael Yang committed
583
	return s, true
584
585
}

Michael Yang's avatar
Michael Yang committed
586
587
588
func isAlpha(r rune) bool {
	return r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z'
}
589

Michael Yang's avatar
Michael Yang committed
590
591
592
func isNumber(r rune) bool {
	return r >= '0' && r <= '9'
}
593

Michael Yang's avatar
Michael Yang committed
594
595
596
func isSpace(r rune) bool {
	return r == ' ' || r == '\t'
}
Michael Yang's avatar
Michael Yang committed
597

Michael Yang's avatar
Michael Yang committed
598
599
600
func isNewline(r rune) bool {
	return r == '\r' || r == '\n'
}
601

602
func isValidMessageRole(role string) bool {
Michael Yang's avatar
Michael Yang committed
603
	return role == "system" || role == "user" || role == "assistant"
604
}
Michael Yang's avatar
Michael Yang committed
605
606
607
608
609
610
611
612
613

func isValidCommand(cmd string) bool {
	switch strings.ToLower(cmd) {
	case "from", "license", "template", "system", "adapter", "parameter", "message":
		return true
	default:
		return false
	}
}
614

615
func expandPathImpl(path, relativeDir string, currentUserFunc func() (*user.User, error), lookupUserFunc func(string) (*user.User, error)) (string, error) {
616
617
618
	if filepath.IsAbs(path) || strings.HasPrefix(path, "\\") || strings.HasPrefix(path, "/") {
		return filepath.Abs(path)
	} else if strings.HasPrefix(path, "~") {
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
		var homeDir string

		if path == "~" || strings.HasPrefix(path, "~/") {
			// Current user's home directory
			currentUser, err := currentUserFunc()
			if err != nil {
				return "", fmt.Errorf("failed to get current user: %w", err)
			}
			homeDir = currentUser.HomeDir
			path = strings.TrimPrefix(path, "~")
		} else {
			// Specific user's home directory
			parts := strings.SplitN(path[1:], "/", 2)
			userInfo, err := lookupUserFunc(parts[0])
			if err != nil {
				return "", fmt.Errorf("failed to find user '%s': %w", parts[0], err)
			}
			homeDir = userInfo.HomeDir
			if len(parts) > 1 {
				path = "/" + parts[1]
			} else {
				path = ""
			}
		}

		path = filepath.Join(homeDir, path)
645
646
	} else {
		path = filepath.Join(relativeDir, path)
647
648
649
650
651
	}

	return filepath.Abs(path)
}

652
653
func expandPath(path, relativeDir string) (string, error) {
	return expandPathImpl(path, relativeDir, user.Current, user.Lookup)
654
}