parser.go 14.1 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
var deprecatedParameters = []string{"penalize_newline"}

44
// CreateRequest creates a new *api.CreateRequest from an existing Modelfile
45
func (f Modelfile) CreateRequest(relativeDir string) (*api.CreateRequest, error) {
46
47
48
49
50
51
52
53
54
	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":
55
			path, err := expandPath(c.Args, relativeDir)
56
57
58
59
60
61
62
63
64
65
66
67
			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
			}

68
69
70
71
72
73
74
			if req.Files == nil {
				req.Files = digestMap
			} else {
				for k, v := range digestMap {
					req.Files[k] = v
				}
			}
75
		case "adapter":
76
			path, err := expandPath(c.Args, relativeDir)
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
			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:
97
98
99
100
101
			if slices.Contains(deprecatedParameters, c.Name) {
				fmt.Printf("warning: parameter %s is deprecated\n", c.Name)
				break
			}

102
103
104
105
106
107
108
109
110
111
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
			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() {
142
		fs, err := filesForModel(path)
143
144
145
		if err != nil {
			return nil, err
		}
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

		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)
		}
164
165
166
167
	} else {
		files = []string{path}
	}

Michael Yang's avatar
Michael Yang committed
168
169
170
	var mu sync.Mutex
	var g errgroup.Group
	g.SetLimit(max(runtime.GOMAXPROCS(0)-1, 1))
171
	for _, f := range files {
Michael Yang's avatar
Michael Yang committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
		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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
	}

	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
		}

236
237
		for _, match := range matches {
			if ct, err := detectContentType(match); err != nil {
238
239
				return nil, err
			} else if ct != contentType {
240
				return nil, fmt.Errorf("invalid content type: expected %s for %s", ct, match)
241
242
243
244
245
246
247
			}
		}

		return matches, nil
	}

	var files []string
248
	if st, _ := glob(filepath.Join(path, "*.safetensors"), "application/octet-stream"); len(st) > 0 {
249
250
251
252
253
254
255
256
257
258
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
295
296
		// 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...)

	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...)
	}

	return files, nil
}

297
298
type Command struct {
	Name string
299
300
301
	Args string
}

Michael Yang's avatar
Michael Yang committed
302
func (c Command) String() string {
Michael Yang's avatar
Michael Yang committed
303
	var sb strings.Builder
Michael Yang's avatar
Michael Yang committed
304
305
	switch c.Name {
	case "model":
Michael Yang's avatar
Michael Yang committed
306
		fmt.Fprintf(&sb, "FROM %s", c.Args)
Michael Yang's avatar
Michael Yang committed
307
	case "license", "template", "system", "adapter":
Michael Yang's avatar
Michael Yang committed
308
		fmt.Fprintf(&sb, "%s %s", strings.ToUpper(c.Name), quote(c.Args))
Michael Yang's avatar
Michael Yang committed
309
310
	case "message":
		role, message, _ := strings.Cut(c.Args, ": ")
Michael Yang's avatar
Michael Yang committed
311
		fmt.Fprintf(&sb, "MESSAGE %s %s", role, quote(message))
Michael Yang's avatar
Michael Yang committed
312
	default:
Michael Yang's avatar
Michael Yang committed
313
		fmt.Fprintf(&sb, "PARAMETER %s %s", c.Name, quote(c.Args))
Michael Yang's avatar
Michael Yang committed
314
315
	}

Michael Yang's avatar
Michael Yang committed
316
	return sb.String()
Michael Yang's avatar
Michael Yang committed
317
318
}

Michael Yang's avatar
Michael Yang committed
319
type state int
320

Michael Yang's avatar
Michael Yang committed
321
322
323
324
325
326
327
328
const (
	stateNil state = iota
	stateName
	stateValue
	stateParameter
	stateMessage
	stateComment
)
329

Michael Yang's avatar
tests  
Michael Yang committed
330
var (
331
332
333
	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
334
)
Michael Yang's avatar
Michael Yang committed
335

336
337
338
339
340
341
342
343
344
345
346
347
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
}

348
func ParseFile(r io.Reader) (*Modelfile, error) {
Michael Yang's avatar
Michael Yang committed
349
350
	var cmd Command
	var curr state
351
	var currLine int = 1
Michael Yang's avatar
Michael Yang committed
352
353
354
	var b bytes.Buffer
	var role string

355
	var f Modelfile
Michael Yang's avatar
Michael Yang committed
356

Michael Yang's avatar
Michael Yang committed
357
358
359
	tr := unicode.BOMOverride(unicode.UTF8.NewDecoder())
	br := bufio.NewReader(transform.NewReader(r, tr))

Michael Yang's avatar
Michael Yang committed
360
361
362
363
364
365
366
	for {
		r, _, err := br.ReadRune()
		if errors.Is(err, io.EOF) {
			break
		} else if err != nil {
			return nil, err
		}
367

368
369
370
371
		if isNewline(r) {
			currLine++
		}

Michael Yang's avatar
Michael Yang committed
372
373
374
375
		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 {
376
377
378
379
			return nil, &ParserError{
				LineNumber: currLine,
				Msg:        err.Error(),
			}
380
381
		}

Michael Yang's avatar
Michael Yang committed
382
		// process the state transition, some transitions need to be intercepted and redirected
Michael Yang's avatar
Michael Yang committed
383
384
		if next != curr {
			switch curr {
Michael Yang's avatar
Michael Yang committed
385
386
			case stateName:
				if !isValidCommand(b.String()) {
387
388
389
390
					return nil, &ParserError{
						LineNumber: currLine,
						Msg:        errInvalidCommand.Error(),
					}
Michael Yang's avatar
Michael Yang committed
391
392
				}

Michael Yang's avatar
Michael Yang committed
393
				// next state sometimes depends on the current buffer value
Michael Yang's avatar
Michael Yang committed
394
395
396
397
				switch s := strings.ToLower(b.String()); s {
				case "from":
					cmd.Name = "model"
				case "parameter":
Michael Yang's avatar
Michael Yang committed
398
					// transition to stateParameter which sets command name
Michael Yang's avatar
Michael Yang committed
399
400
					next = stateParameter
				case "message":
Michael Yang's avatar
Michael Yang committed
401
					// transition to stateMessage which validates the message role
Michael Yang's avatar
Michael Yang committed
402
403
404
405
406
					next = stateMessage
					fallthrough
				default:
					cmd.Name = s
				}
Michael Yang's avatar
Michael Yang committed
407
408
			case stateParameter:
				cmd.Name = b.String()
Michael Yang's avatar
Michael Yang committed
409
			case stateMessage:
410
411
412
413
414
				if !isValidMessageRole(b.String()) {
					return nil, &ParserError{
						LineNumber: currLine,
						Msg:        errInvalidMessageRole.Error(),
					}
415
				}
416
417

				role = b.String()
Michael Yang's avatar
Michael Yang committed
418
419
420
			case stateComment, stateNil:
				// pass
			case stateValue:
Josh Yan's avatar
Josh Yan committed
421
				s, ok := unquote(strings.TrimSpace(b.String()))
Michael Yang's avatar
Michael Yang committed
422
423
424
425
426
427
428
429
430
431
432
433
434
435
				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
436
				f.Commands = append(f.Commands, cmd)
Michael Yang's avatar
Michael Yang committed
437
438
			}

Michael Yang's avatar
Michael Yang committed
439
440
441
442
443
444
445
			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
446
			}
Michael Yang's avatar
Michael Yang committed
447
448
449
450
451
452
453
454
		}
	}

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

Michael Yang's avatar
Michael Yang committed
460
461
462
463
464
		if role != "" {
			s = role + ": " + s
		}

		cmd.Args = s
Michael Yang's avatar
Michael Yang committed
465
		f.Commands = append(f.Commands, cmd)
Michael Yang's avatar
Michael Yang committed
466
467
	default:
		return nil, io.ErrUnexpectedEOF
468
469
	}

Michael Yang's avatar
Michael Yang committed
470
	for _, cmd := range f.Commands {
Michael Yang's avatar
Michael Yang committed
471
		if cmd.Name == "model" {
Michael Yang's avatar
Michael Yang committed
472
			return &f, nil
Michael Yang's avatar
Michael Yang committed
473
		}
474
475
	}

Michael Yang's avatar
tests  
Michael Yang committed
476
	return nil, errMissingFrom
477
}
478

Michael Yang's avatar
Michael Yang committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
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
497
			return stateNil, 0, errInvalidCommand
Michael Yang's avatar
Michael Yang committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
		}
	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("")
535
	}
Michael Yang's avatar
Michael Yang committed
536
}
537

Michael Yang's avatar
Michael Yang committed
538
func quote(s string) string {
539
	if strings.Contains(s, "\n") || strings.HasPrefix(s, " ") || strings.HasSuffix(s, " ") {
Michael Yang's avatar
Michael Yang committed
540
541
542
543
		if strings.Contains(s, "\"") {
			return `"""` + s + `"""`
		}

544
		return `"` + s + `"`
Michael Yang's avatar
Michael Yang committed
545
546
547
548
549
	}

	return s
}

Michael Yang's avatar
Michael Yang committed
550
551
552
553
554
555
556
557
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
558
559
	}

Michael Yang's avatar
Michael Yang committed
560
561
562
563
564
565
	if len(s) >= 1 && s[0] == '"' {
		if len(s) >= 2 && s[len(s)-1] == '"' {
			return s[1 : len(s)-1], true
		}

		return "", false
566
567
	}

Michael Yang's avatar
Michael Yang committed
568
	return s, true
569
570
}

Michael Yang's avatar
Michael Yang committed
571
572
573
func isAlpha(r rune) bool {
	return r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z'
}
574

Michael Yang's avatar
Michael Yang committed
575
576
577
func isNumber(r rune) bool {
	return r >= '0' && r <= '9'
}
578

Michael Yang's avatar
Michael Yang committed
579
580
581
func isSpace(r rune) bool {
	return r == ' ' || r == '\t'
}
Michael Yang's avatar
Michael Yang committed
582

Michael Yang's avatar
Michael Yang committed
583
584
585
func isNewline(r rune) bool {
	return r == '\r' || r == '\n'
}
586

587
func isValidMessageRole(role string) bool {
Michael Yang's avatar
Michael Yang committed
588
	return role == "system" || role == "user" || role == "assistant"
589
}
Michael Yang's avatar
Michael Yang committed
590
591
592
593
594
595
596
597
598

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

600
func expandPathImpl(path, relativeDir string, currentUserFunc func() (*user.User, error), lookupUserFunc func(string) (*user.User, error)) (string, error) {
601
602
603
	if filepath.IsAbs(path) || strings.HasPrefix(path, "\\") || strings.HasPrefix(path, "/") {
		return filepath.Abs(path)
	} else if strings.HasPrefix(path, "~") {
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
		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)
630
631
	} else {
		path = filepath.Join(relativeDir, path)
632
633
634
635
636
	}

	return filepath.Abs(path)
}

637
638
func expandPath(path, relativeDir string) (string, error) {
	return expandPathImpl(path, relativeDir, user.Current, user.Lookup)
639
}