parser: remove role validation from Modelfile parser (#9874)

* updates parser/parser.go to allow arbitrary roles in Modelfile MESSAGE blocks
This commit is contained in:
rylativity 2025-03-20 16:11:17 -04:00 committed by GitHub
parent 42a14f7f63
commit ffbfe833da
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 16 deletions

View File

@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log/slog"
"net/http" "net/http"
"os" "os"
"os/user" "os/user"
@ -301,7 +302,6 @@ const (
var ( var (
errMissingFrom = errors.New("no FROM line") 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\"") errInvalidCommand = errors.New("command must be one of \"from\", \"license\", \"template\", \"system\", \"adapter\", \"parameter\", or \"message\"")
) )
@ -379,14 +379,10 @@ func ParseFile(r io.Reader) (*Modelfile, error) {
case stateParameter: case stateParameter:
cmd.Name = b.String() cmd.Name = b.String()
case stateMessage: case stateMessage:
if !isValidMessageRole(b.String()) {
return nil, &ParserError{
LineNumber: currLine,
Msg: errInvalidMessageRole.Error(),
}
}
role = b.String() role = b.String()
if !isKnownMessageRole(b.String()) {
slog.Warn("received non-standard role", "role", role)
}
case stateComment, stateNil: case stateComment, stateNil:
// pass // pass
case stateValue: case stateValue:
@ -556,7 +552,7 @@ func isNewline(r rune) bool {
return r == '\r' || r == '\n' return r == '\r' || r == '\n'
} }
func isValidMessageRole(role string) bool { func isKnownMessageRole(role string) bool {
return role == "system" || role == "user" || role == "assistant" return role == "system" || role == "user" || role == "assistant"
} }

View File

@ -256,13 +256,13 @@ You are a multiline file parser. Always parse things.
{ {
` `
FROM foo FROM foo
MESSAGE badguy I'm a bad guy! MESSAGE somerandomrole I'm ok with you adding any role message now!
`, `,
nil, []Command{
&ParserError{ {Name: "model", Args: "foo"},
LineNumber: 3, {Name: "message", Args: "somerandomrole: I'm ok with you adding any role message now!"},
Msg: errInvalidMessageRole.Error(),
}, },
nil,
}, },
{ {
` `