ollama/parser/parser.go

117 lines
2.6 KiB
Go
Raw Normal View History

package parser
import (
"bufio"
2023-07-25 17:22:23 +00:00
"bytes"
"errors"
2023-07-27 16:55:48 +00:00
"fmt"
"io"
"log"
)
type Command struct {
Name string
Args string
}
func (c *Command) Reset() {
c.Name = ""
c.Args = ""
}
func Parse(reader io.Reader) ([]Command, error) {
var commands []Command
var command, modelCommand Command
scanner := bufio.NewScanner(reader)
scanner.Buffer(make([]byte, 0, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)
scanner.Split(scanModelfile)
for scanner.Scan() {
line := scanner.Bytes()
fields := bytes.SplitN(line, []byte(" "), 2)
if len(fields) == 0 || len(fields[0]) == 0 {
continue
}
2023-07-25 17:22:23 +00:00
switch string(bytes.ToUpper(fields[0])) {
case "FROM":
command.Name = "model"
command.Args = string(fields[1])
// copy command for validation
modelCommand = command
case "LICENSE", "TEMPLATE", "SYSTEM", "PROMPT", "EMBED", "ADAPTER":
2023-07-25 17:22:23 +00:00
command.Name = string(bytes.ToLower(fields[0]))
command.Args = string(fields[1])
case "PARAMETER":
fields = bytes.SplitN(fields[1], []byte(" "), 2)
2023-08-10 23:09:02 +00:00
if len(fields) < 2 {
return nil, fmt.Errorf("missing value for %s", fields)
}
command.Name = string(fields[0])
command.Args = string(fields[1])
default:
2023-08-10 23:22:08 +00:00
if !bytes.HasPrefix(fields[0], []byte("#")) {
// log a warning for unknown commands
log.Printf("WARNING: Unknown command: %s", fields[0])
}
2023-07-25 17:22:23 +00:00
continue
}
commands = append(commands, command)
command.Reset()
}
if modelCommand.Args == "" {
return nil, errors.New("no FROM line for the model was specified")
}
return commands, scanner.Err()
}
func scanModelfile(data []byte, atEOF bool) (advance int, token []byte, err error) {
2023-07-27 16:55:48 +00:00
advance, token, err = scan([]byte(`"""`), []byte(`"""`), data, atEOF)
if err != nil {
return 0, nil, err
}
if advance > 0 && token != nil {
return advance, token, nil
}
2023-07-25 17:22:23 +00:00
2023-07-27 16:55:48 +00:00
advance, token, err = scan([]byte(`"`), []byte(`"`), data, atEOF)
if err != nil {
return 0, nil, err
}
if advance > 0 && token != nil {
return advance, token, nil
}
return bufio.ScanLines(data, atEOF)
}
func scan(openBytes, closeBytes, data []byte, atEOF bool) (advance int, token []byte, err error) {
newline := bytes.IndexByte(data, '\n')
2023-07-27 16:55:48 +00:00
if start := bytes.Index(data, openBytes); start >= 0 && start < newline {
end := bytes.Index(data[start+len(openBytes):], closeBytes)
if end < 0 {
2023-07-20 07:37:52 +00:00
if atEOF {
2023-07-27 16:55:48 +00:00
return 0, nil, fmt.Errorf("unterminated %s: expecting %s", openBytes, closeBytes)
2023-07-20 07:37:52 +00:00
} else {
return 0, nil, nil
}
}
2023-07-27 16:55:48 +00:00
n := start + len(openBytes) + end + len(closeBytes)
newData := data[:start]
2023-07-27 16:55:48 +00:00
newData = append(newData, data[start+len(openBytes):n-len(closeBytes)]...)
return n, newData, nil
}
2023-07-27 16:55:48 +00:00
return 0, nil, nil
}