parser.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package parser
  2. import (
  3. "bufio"
  4. "bytes"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "log/slog"
  9. "slices"
  10. )
  11. type Command struct {
  12. Name string
  13. Args string
  14. }
  15. func (c *Command) Reset() {
  16. c.Name = ""
  17. c.Args = ""
  18. }
  19. func Parse(reader io.Reader) ([]Command, error) {
  20. var commands []Command
  21. var command, modelCommand Command
  22. scanner := bufio.NewScanner(reader)
  23. scanner.Buffer(make([]byte, 0, bufio.MaxScanTokenSize), bufio.MaxScanTokenSize)
  24. scanner.Split(scanModelfile)
  25. for scanner.Scan() {
  26. line := scanner.Bytes()
  27. fields := bytes.SplitN(line, []byte(" "), 2)
  28. if len(fields) == 0 || len(fields[0]) == 0 {
  29. continue
  30. }
  31. switch string(bytes.ToUpper(fields[0])) {
  32. case "FROM":
  33. command.Name = "model"
  34. command.Args = string(bytes.TrimSpace(fields[1]))
  35. // copy command for validation
  36. modelCommand = command
  37. case "ADAPTER":
  38. command.Name = string(bytes.ToLower(fields[0]))
  39. command.Args = string(bytes.TrimSpace(fields[1]))
  40. case "LICENSE", "TEMPLATE", "SYSTEM", "PROMPT":
  41. command.Name = string(bytes.ToLower(fields[0]))
  42. command.Args = string(fields[1])
  43. case "PARAMETER":
  44. fields = bytes.SplitN(fields[1], []byte(" "), 2)
  45. if len(fields) < 2 {
  46. return nil, fmt.Errorf("missing value for %s", fields)
  47. }
  48. command.Name = string(fields[0])
  49. command.Args = string(bytes.TrimSpace(fields[1]))
  50. case "EMBED":
  51. return nil, fmt.Errorf("deprecated command: EMBED is no longer supported, use the /embed API endpoint instead")
  52. case "MESSAGE":
  53. command.Name = string(bytes.ToLower(fields[0]))
  54. fields = bytes.SplitN(fields[1], []byte(" "), 2)
  55. if len(fields) < 2 {
  56. return nil, fmt.Errorf("should be in the format <role> <message>")
  57. }
  58. if !slices.Contains([]string{"system", "user", "assistant"}, string(bytes.ToLower(fields[0]))) {
  59. return nil, fmt.Errorf("role must be one of \"system\", \"user\", or \"assistant\"")
  60. }
  61. command.Args = fmt.Sprintf("%s: %s", string(bytes.ToLower(fields[0])), string(fields[1]))
  62. default:
  63. if !bytes.HasPrefix(fields[0], []byte("#")) {
  64. // log a warning for unknown commands
  65. slog.Warn(fmt.Sprintf("Unknown command: %s", fields[0]))
  66. }
  67. continue
  68. }
  69. commands = append(commands, command)
  70. command.Reset()
  71. }
  72. if modelCommand.Args == "" {
  73. return nil, errors.New("no FROM line for the model was specified")
  74. }
  75. return commands, scanner.Err()
  76. }
  77. func scanModelfile(data []byte, atEOF bool) (advance int, token []byte, err error) {
  78. advance, token, err = scan([]byte(`"""`), []byte(`"""`), data, atEOF)
  79. if err != nil {
  80. return 0, nil, err
  81. }
  82. if advance > 0 && token != nil {
  83. return advance, token, nil
  84. }
  85. advance, token, err = scan([]byte(`"`), []byte(`"`), data, atEOF)
  86. if err != nil {
  87. return 0, nil, err
  88. }
  89. if advance > 0 && token != nil {
  90. return advance, token, nil
  91. }
  92. return bufio.ScanLines(data, atEOF)
  93. }
  94. func scan(openBytes, closeBytes, data []byte, atEOF bool) (advance int, token []byte, err error) {
  95. newline := bytes.IndexByte(data, '\n')
  96. if start := bytes.Index(data, openBytes); start >= 0 && start < newline {
  97. end := bytes.Index(data[start+len(openBytes):], closeBytes)
  98. if end < 0 {
  99. if atEOF {
  100. return 0, nil, fmt.Errorf("unterminated %s: expecting %s", openBytes, closeBytes)
  101. } else {
  102. return 0, nil, nil
  103. }
  104. }
  105. n := start + len(openBytes) + end + len(closeBytes)
  106. newData := data[:start]
  107. newData = append(newData, data[start+len(openBytes):n-len(closeBytes)]...)
  108. return n, newData, nil
  109. }
  110. return 0, nil, nil
  111. }