parser.go 13.8 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
142
143
144
145
146
147
148
149
			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() {
		files, err = filesForModel(path)
		if err != nil {
			return nil, err
		}
	} else {
		files = []string{path}
	}

Michael Yang's avatar
Michael Yang committed
150
151
152
	var mu sync.Mutex
	var g errgroup.Group
	g.SetLimit(max(runtime.GOMAXPROCS(0)-1, 1))
153
	for _, f := range files {
Michael Yang's avatar
Michael Yang committed
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
		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
169
170
171
172
173
174
175
176
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
223
224
225
226
227
228
229
	}

	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
		}

		for _, safetensor := range matches {
			if ct, err := detectContentType(safetensor); err != nil {
				return nil, err
			} else if ct != contentType {
				return nil, fmt.Errorf("invalid content type: expected %s for %s", ct, safetensor)
			}
		}

		return matches, nil
	}

	var files []string
230
	if st, _ := glob(filepath.Join(path, "*.safetensors"), "application/octet-stream"); len(st) > 0 {
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
		// 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
}

279
280
type Command struct {
	Name string
281
282
283
	Args string
}

Michael Yang's avatar
Michael Yang committed
284
func (c Command) String() string {
Michael Yang's avatar
Michael Yang committed
285
	var sb strings.Builder
Michael Yang's avatar
Michael Yang committed
286
287
	switch c.Name {
	case "model":
Michael Yang's avatar
Michael Yang committed
288
		fmt.Fprintf(&sb, "FROM %s", c.Args)
Michael Yang's avatar
Michael Yang committed
289
	case "license", "template", "system", "adapter":
Michael Yang's avatar
Michael Yang committed
290
		fmt.Fprintf(&sb, "%s %s", strings.ToUpper(c.Name), quote(c.Args))
Michael Yang's avatar
Michael Yang committed
291
292
	case "message":
		role, message, _ := strings.Cut(c.Args, ": ")
Michael Yang's avatar
Michael Yang committed
293
		fmt.Fprintf(&sb, "MESSAGE %s %s", role, quote(message))
Michael Yang's avatar
Michael Yang committed
294
	default:
Michael Yang's avatar
Michael Yang committed
295
		fmt.Fprintf(&sb, "PARAMETER %s %s", c.Name, quote(c.Args))
Michael Yang's avatar
Michael Yang committed
296
297
	}

Michael Yang's avatar
Michael Yang committed
298
	return sb.String()
Michael Yang's avatar
Michael Yang committed
299
300
}

Michael Yang's avatar
Michael Yang committed
301
type state int
302

Michael Yang's avatar
Michael Yang committed
303
304
305
306
307
308
309
310
const (
	stateNil state = iota
	stateName
	stateValue
	stateParameter
	stateMessage
	stateComment
)
311

Michael Yang's avatar
tests  
Michael Yang committed
312
var (
313
314
315
	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
316
)
Michael Yang's avatar
Michael Yang committed
317

318
319
320
321
322
323
324
325
326
327
328
329
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
}

330
func ParseFile(r io.Reader) (*Modelfile, error) {
Michael Yang's avatar
Michael Yang committed
331
332
	var cmd Command
	var curr state
333
	var currLine int = 1
Michael Yang's avatar
Michael Yang committed
334
335
336
	var b bytes.Buffer
	var role string

337
	var f Modelfile
Michael Yang's avatar
Michael Yang committed
338

Michael Yang's avatar
Michael Yang committed
339
340
341
	tr := unicode.BOMOverride(unicode.UTF8.NewDecoder())
	br := bufio.NewReader(transform.NewReader(r, tr))

Michael Yang's avatar
Michael Yang committed
342
343
344
345
346
347
348
	for {
		r, _, err := br.ReadRune()
		if errors.Is(err, io.EOF) {
			break
		} else if err != nil {
			return nil, err
		}
349

350
351
352
353
		if isNewline(r) {
			currLine++
		}

Michael Yang's avatar
Michael Yang committed
354
355
356
357
		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 {
358
359
360
361
			return nil, &ParserError{
				LineNumber: currLine,
				Msg:        err.Error(),
			}
362
363
		}

Michael Yang's avatar
Michael Yang committed
364
		// process the state transition, some transitions need to be intercepted and redirected
Michael Yang's avatar
Michael Yang committed
365
366
		if next != curr {
			switch curr {
Michael Yang's avatar
Michael Yang committed
367
368
			case stateName:
				if !isValidCommand(b.String()) {
369
370
371
372
					return nil, &ParserError{
						LineNumber: currLine,
						Msg:        errInvalidCommand.Error(),
					}
Michael Yang's avatar
Michael Yang committed
373
374
				}

Michael Yang's avatar
Michael Yang committed
375
				// next state sometimes depends on the current buffer value
Michael Yang's avatar
Michael Yang committed
376
377
378
379
				switch s := strings.ToLower(b.String()); s {
				case "from":
					cmd.Name = "model"
				case "parameter":
Michael Yang's avatar
Michael Yang committed
380
					// transition to stateParameter which sets command name
Michael Yang's avatar
Michael Yang committed
381
382
					next = stateParameter
				case "message":
Michael Yang's avatar
Michael Yang committed
383
					// transition to stateMessage which validates the message role
Michael Yang's avatar
Michael Yang committed
384
385
386
387
388
					next = stateMessage
					fallthrough
				default:
					cmd.Name = s
				}
Michael Yang's avatar
Michael Yang committed
389
390
			case stateParameter:
				cmd.Name = b.String()
Michael Yang's avatar
Michael Yang committed
391
			case stateMessage:
392
393
394
395
396
				if !isValidMessageRole(b.String()) {
					return nil, &ParserError{
						LineNumber: currLine,
						Msg:        errInvalidMessageRole.Error(),
					}
397
				}
398
399

				role = b.String()
Michael Yang's avatar
Michael Yang committed
400
401
402
			case stateComment, stateNil:
				// pass
			case stateValue:
Josh Yan's avatar
Josh Yan committed
403
				s, ok := unquote(strings.TrimSpace(b.String()))
Michael Yang's avatar
Michael Yang committed
404
405
406
407
408
409
410
411
412
413
414
415
416
417
				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
418
				f.Commands = append(f.Commands, cmd)
Michael Yang's avatar
Michael Yang committed
419
420
			}

Michael Yang's avatar
Michael Yang committed
421
422
423
424
425
426
427
			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
428
			}
Michael Yang's avatar
Michael Yang committed
429
430
431
432
433
434
435
436
		}
	}

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

Michael Yang's avatar
Michael Yang committed
442
443
444
445
446
		if role != "" {
			s = role + ": " + s
		}

		cmd.Args = s
Michael Yang's avatar
Michael Yang committed
447
		f.Commands = append(f.Commands, cmd)
Michael Yang's avatar
Michael Yang committed
448
449
	default:
		return nil, io.ErrUnexpectedEOF
450
451
	}

Michael Yang's avatar
Michael Yang committed
452
	for _, cmd := range f.Commands {
Michael Yang's avatar
Michael Yang committed
453
		if cmd.Name == "model" {
Michael Yang's avatar
Michael Yang committed
454
			return &f, nil
Michael Yang's avatar
Michael Yang committed
455
		}
456
457
	}

Michael Yang's avatar
tests  
Michael Yang committed
458
	return nil, errMissingFrom
459
}
460

Michael Yang's avatar
Michael Yang committed
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
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
479
			return stateNil, 0, errInvalidCommand
Michael Yang's avatar
Michael Yang committed
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
		}
	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("")
517
	}
Michael Yang's avatar
Michael Yang committed
518
}
519

Michael Yang's avatar
Michael Yang committed
520
func quote(s string) string {
521
	if strings.Contains(s, "\n") || strings.HasPrefix(s, " ") || strings.HasSuffix(s, " ") {
Michael Yang's avatar
Michael Yang committed
522
523
524
525
		if strings.Contains(s, "\"") {
			return `"""` + s + `"""`
		}

526
		return `"` + s + `"`
Michael Yang's avatar
Michael Yang committed
527
528
529
530
531
	}

	return s
}

Michael Yang's avatar
Michael Yang committed
532
533
534
535
536
537
538
539
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
540
541
	}

Michael Yang's avatar
Michael Yang committed
542
543
544
545
546
547
	if len(s) >= 1 && s[0] == '"' {
		if len(s) >= 2 && s[len(s)-1] == '"' {
			return s[1 : len(s)-1], true
		}

		return "", false
548
549
	}

Michael Yang's avatar
Michael Yang committed
550
	return s, true
551
552
}

Michael Yang's avatar
Michael Yang committed
553
554
555
func isAlpha(r rune) bool {
	return r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z'
}
556

Michael Yang's avatar
Michael Yang committed
557
558
559
func isNumber(r rune) bool {
	return r >= '0' && r <= '9'
}
560

Michael Yang's avatar
Michael Yang committed
561
562
563
func isSpace(r rune) bool {
	return r == ' ' || r == '\t'
}
Michael Yang's avatar
Michael Yang committed
564

Michael Yang's avatar
Michael Yang committed
565
566
567
func isNewline(r rune) bool {
	return r == '\r' || r == '\n'
}
568

569
func isValidMessageRole(role string) bool {
Michael Yang's avatar
Michael Yang committed
570
	return role == "system" || role == "user" || role == "assistant"
571
}
Michael Yang's avatar
Michael Yang committed
572
573
574
575
576
577
578
579
580

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

582
func expandPathImpl(path, relativeDir string, currentUserFunc func() (*user.User, error), lookupUserFunc func(string) (*user.User, error)) (string, error) {
583
584
585
	if filepath.IsAbs(path) || strings.HasPrefix(path, "\\") || strings.HasPrefix(path, "/") {
		return filepath.Abs(path)
	} else if strings.HasPrefix(path, "~") {
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
		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)
612
613
	} else {
		path = filepath.Join(relativeDir, path)
614
615
616
617
618
	}

	return filepath.Abs(path)
}

619
620
func expandPath(path, relativeDir string) (string, error) {
	return expandPathImpl(path, relativeDir, user.Current, user.Lookup)
621
}