fix multiline

This commit is contained in:
Michael Yang 2024-04-24 19:17:26 -07:00
parent abe614c705
commit 8907bf51d2
2 changed files with 53 additions and 26 deletions

View file

@ -110,11 +110,16 @@ func Parse(r io.Reader) (cmds []Command, err error) {
case stateComment, stateNil: case stateComment, stateNil:
// pass; nothing to flush // pass; nothing to flush
case stateValue: case stateValue:
if _, ok := unquote(b.String()); !ok { s, ok := unquote(b.String())
if !ok {
return nil, io.ErrUnexpectedEOF return nil, io.ErrUnexpectedEOF
} }
cmd.Args = b.String() if role != "" {
s = role + ": " + s
}
cmd.Args = s
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
default: default:
return nil, io.ErrUnexpectedEOF return nil, io.ErrUnexpectedEOF

View file

@ -124,6 +124,16 @@ MESSAGE system You are a Parser. Always Parse things.
{ {
` `
FROM foo FROM foo
MESSAGE system You are a Parser. Always Parse things.`,
[]Command{
{Name: "model", Args: "foo"},
{Name: "message", Args: "system: You are a Parser. Always Parse things."},
},
nil,
},
{
`
FROM foo
MESSAGE system You are a Parser. Always Parse things. MESSAGE system You are a Parser. Always Parse things.
MESSAGE user Hey there! MESSAGE user Hey there!
MESSAGE assistant Hello, I want to parse all the things! MESSAGE assistant Hello, I want to parse all the things!
@ -192,57 +202,57 @@ func TestParserQuoted(t *testing.T) {
{ {
` `
FROM foo FROM foo
TEMPLATE """ SYSTEM """
This is a This is a
multiline template. multiline system.
""" """
`, `,
[]Command{ []Command{
{Name: "model", Args: "foo"}, {Name: "model", Args: "foo"},
{Name: "template", Args: "\nThis is a\nmultiline template.\n"}, {Name: "system", Args: "\nThis is a\nmultiline system.\n"},
}, },
nil, nil,
}, },
{ {
` `
FROM foo FROM foo
TEMPLATE """ SYSTEM """
This is a This is a
multiline template.""" multiline system."""
`, `,
[]Command{ []Command{
{Name: "model", Args: "foo"}, {Name: "model", Args: "foo"},
{Name: "template", Args: "\nThis is a\nmultiline template."}, {Name: "system", Args: "\nThis is a\nmultiline system."},
}, },
nil, nil,
}, },
{ {
` `
FROM foo FROM foo
TEMPLATE """This is a SYSTEM """This is a
multiline template.""" multiline system."""
`, `,
[]Command{ []Command{
{Name: "model", Args: "foo"}, {Name: "model", Args: "foo"},
{Name: "template", Args: "This is a\nmultiline template."}, {Name: "system", Args: "This is a\nmultiline system."},
}, },
nil, nil,
}, },
{ {
` `
FROM foo FROM foo
TEMPLATE """This is a multiline template.""" SYSTEM """This is a multiline system."""
`, `,
[]Command{ []Command{
{Name: "model", Args: "foo"}, {Name: "model", Args: "foo"},
{Name: "template", Args: "This is a multiline template."}, {Name: "system", Args: "This is a multiline system."},
}, },
nil, nil,
}, },
{ {
` `
FROM foo FROM foo
TEMPLATE """This is a multiline template."" SYSTEM """This is a multiline system.""
`, `,
nil, nil,
io.ErrUnexpectedEOF, io.ErrUnexpectedEOF,
@ -250,7 +260,7 @@ TEMPLATE """This is a multiline template.""
{ {
` `
FROM foo FROM foo
TEMPLATE " SYSTEM "
`, `,
nil, nil,
io.ErrUnexpectedEOF, io.ErrUnexpectedEOF,
@ -258,57 +268,69 @@ TEMPLATE "
{ {
` `
FROM foo FROM foo
TEMPLATE """ SYSTEM """
This is a multiline template with "quotes". This is a multiline system with "quotes".
""" """
`, `,
[]Command{ []Command{
{Name: "model", Args: "foo"}, {Name: "model", Args: "foo"},
{Name: "template", Args: "\nThis is a multiline template with \"quotes\".\n"}, {Name: "system", Args: "\nThis is a multiline system with \"quotes\".\n"},
}, },
nil, nil,
}, },
{ {
` `
FROM foo FROM foo
TEMPLATE """""" SYSTEM """"""
`, `,
[]Command{ []Command{
{Name: "model", Args: "foo"}, {Name: "model", Args: "foo"},
{Name: "template", Args: ""}, {Name: "system", Args: ""},
}, },
nil, nil,
}, },
{ {
` `
FROM foo FROM foo
TEMPLATE "" SYSTEM ""
`, `,
[]Command{ []Command{
{Name: "model", Args: "foo"}, {Name: "model", Args: "foo"},
{Name: "template", Args: ""}, {Name: "system", Args: ""},
}, },
nil, nil,
}, },
{ {
` `
FROM foo FROM foo
TEMPLATE "'" SYSTEM "'"
`, `,
[]Command{ []Command{
{Name: "model", Args: "foo"}, {Name: "model", Args: "foo"},
{Name: "template", Args: "'"}, {Name: "system", Args: "'"},
}, },
nil, nil,
}, },
{ {
` `
FROM foo FROM foo
TEMPLATE """''"'""'""'"'''''""'""'""" SYSTEM """''"'""'""'"'''''""'""'"""
`, `,
[]Command{ []Command{
{Name: "model", Args: "foo"}, {Name: "model", Args: "foo"},
{Name: "template", Args: `''"'""'""'"'''''""'""'`}, {Name: "system", Args: `''"'""'""'"'''''""'""'`},
},
nil,
},
{
`
FROM foo
TEMPLATE """
{{ .Prompt }}
"""`,
[]Command{
{Name: "model", Args: "foo"},
{Name: "template", Args: "\n{{ .Prompt }}\n"},
}, },
nil, nil,
}, },