2023-07-17 00:02:22 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2023-07-25 17:22:23 +00:00
|
|
|
"bytes"
|
2023-07-17 21:21:27 +00:00
|
|
|
"errors"
|
2023-07-27 16:55:48 +00:00
|
|
|
"fmt"
|
2023-07-17 00:02:22 +00:00
|
|
|
"io"
|
2023-07-25 18:50:23 +00:00
|
|
|
"log"
|
2023-07-17 00:02:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Command struct {
|
|
|
|
Name string
|
2023-07-17 21:21:27 +00:00
|
|
|
Args string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) Reset() {
|
|
|
|
c.Name = ""
|
|
|
|
c.Args = ""
|
2023-07-17 00:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Parse(reader io.Reader) ([]Command, error) {
|
|
|
|
var commands []Command
|
2023-07-17 21:21:27 +00:00
|
|
|
var command, modelCommand Command
|
2023-07-17 00:02:22 +00:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(reader)
|
2023-07-28 18:43:25 +00:00
|
|
|
scanner.Buffer(make([]byte, 0, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)
|
2023-07-17 21:21:27 +00:00
|
|
|
scanner.Split(scanModelfile)
|
2023-07-17 00:02:22 +00:00
|
|
|
for scanner.Scan() {
|
2023-07-17 21:21:27 +00:00
|
|
|
line := scanner.Bytes()
|
|
|
|
|
|
|
|
fields := bytes.SplitN(line, []byte(" "), 2)
|
2023-07-25 18:50:23 +00:00
|
|
|
if len(fields) == 0 || len(fields[0]) == 0 {
|
2023-07-17 00:02:22 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-07-25 17:22:23 +00:00
|
|
|
switch string(bytes.ToUpper(fields[0])) {
|
2023-07-17 00:02:22 +00:00
|
|
|
case "FROM":
|
|
|
|
command.Name = "model"
|
2023-07-17 21:21:27 +00:00
|
|
|
command.Args = string(fields[1])
|
|
|
|
// copy command for validation
|
|
|
|
modelCommand = command
|
2023-07-20 08:13:30 +00:00
|
|
|
case "LICENSE", "TEMPLATE", "SYSTEM", "PROMPT":
|
2023-07-25 17:22:23 +00:00
|
|
|
command.Name = string(bytes.ToLower(fields[0]))
|
2023-07-17 21:21:27 +00:00
|
|
|
command.Args = string(fields[1])
|
2023-07-17 00:02:22 +00:00
|
|
|
case "PARAMETER":
|
2023-07-17 21:21:27 +00:00
|
|
|
fields = bytes.SplitN(fields[1], []byte(" "), 2)
|
|
|
|
command.Name = string(fields[0])
|
|
|
|
command.Args = string(fields[1])
|
2023-07-17 00:02:22 +00:00
|
|
|
default:
|
2023-07-25 17:22:23 +00:00
|
|
|
// log a warning for unknown commands
|
2023-07-25 18:50:23 +00:00
|
|
|
log.Printf("WARNING: Unknown command: %s", fields[0])
|
2023-07-25 17:22:23 +00:00
|
|
|
continue
|
2023-07-17 00:02:22 +00:00
|
|
|
}
|
2023-07-17 21:21:27 +00:00
|
|
|
|
|
|
|
commands = append(commands, command)
|
|
|
|
command.Reset()
|
2023-07-17 00:02:22 +00:00
|
|
|
}
|
|
|
|
|
2023-07-17 21:21:27 +00:00
|
|
|
if modelCommand.Args == "" {
|
2023-07-24 12:41:13 +00:00
|
|
|
return nil, errors.New("no FROM line for the model was specified")
|
2023-07-17 00:02:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return commands, scanner.Err()
|
|
|
|
}
|
2023-07-17 21:21:27 +00:00
|
|
|
|
|
|
|
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) {
|
2023-07-17 21:21:27 +00:00
|
|
|
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)
|
2023-07-17 21:21:27 +00:00
|
|
|
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-17 21:21:27 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 16:55:48 +00:00
|
|
|
n := start + len(openBytes) + end + len(closeBytes)
|
2023-07-25 18:50:23 +00:00
|
|
|
|
|
|
|
newData := data[:start]
|
2023-07-27 16:55:48 +00:00
|
|
|
newData = append(newData, data[start+len(openBytes):n-len(closeBytes)]...)
|
2023-07-25 18:50:23 +00:00
|
|
|
return n, newData, nil
|
2023-07-17 21:21:27 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 16:55:48 +00:00
|
|
|
return 0, nil, nil
|
2023-07-17 21:21:27 +00:00
|
|
|
}
|