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:
parent
42a14f7f63
commit
ffbfe833da
@ -7,6 +7,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/user"
|
"os/user"
|
||||||
@ -300,9 +301,8 @@ 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\"")
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ParserError struct {
|
type ParserError struct {
|
||||||
@ -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"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
`
|
`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user