Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prompt: Update #11

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions prompt/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import "fmt"
type Example struct {
Input []string
Output string
Prefix string
}

func (ex Example) String() string {
func (ex Example) Sprint(prefix string) string {
switch len(ex.Input) {
case 0:
return ""
case 1:
return fmt.Sprintf("Input: %s\nOutput: %s", ex.Input[0], ex.Output)
default:
return fmt.Sprintf("Input:%s\nOutput: %s", printBatch(ex.Input, ex.Prefix, 0), ex.Output)
return fmt.Sprintf("Input:\"\"\"\n%s\"\"\"\nOutput: %s", printBatch(ex.Input, prefix, 0), ex.Output)
}
}
11 changes: 6 additions & 5 deletions prompt/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import "testing"
func TestExample(t *testing.T) {
for i, tc := range []struct {
ex Example
prefix string
output string
}{
{Example{nil, "result", "%d|"}, ""},
{Example{[]string{"abc"}, "result", "%d|"}, "Input: abc\nOutput: result"},
{Example{[]string{"abc", "def", "ghi"}, "result", ""}, "Input:\"\"\"\nabc\ndef\nghi\n\"\"\"\nOutput: result"},
{Example{[]string{"abc", "def", "ghi"}, "result", "%d|"}, "Input:\"\"\"\n1|abc\n2|def\n3|ghi\n\"\"\"\nOutput: result"},
{Example{nil, "result"}, "%d|", ""},
{Example{[]string{"abc"}, "result"}, "%d|", "Input:\"\"\"\n1|abc\n\"\"\"\nOutput: result"},
{Example{[]string{"abc", "def", "ghi"}, "result"}, "", "Input:\"\"\"\nabc\ndef\nghi\n\"\"\"\nOutput: result"},
{Example{[]string{"abc", "def", "ghi"}, "result"}, "%d|", "Input:\"\"\"\n1|abc\n2|def\n3|ghi\n\"\"\"\nOutput: result"},
} {
if output := tc.ex.String(); output != tc.output {
if output := tc.ex.Sprint(tc.prefix); output != tc.output {
t.Errorf("#%d: expected %q; got %q", i, tc.output, output)
}
}
Expand Down
14 changes: 8 additions & 6 deletions prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@ const (
defaultWorkers = 3
)

const defaultTemplate = `{{.Request}}{{with .Example}}
const defaultTemplate = `{{.Request}}{{if .Example}}
###
Example:
{{.}}
{{.Example.Sprint .Prefix}}
###{{end}}{{if .Input}}
Input:{{if gt (len .Input) 1}}{{printBatch .Input .Prefix .Start}}{{else}} {{index .Input 0}}{{end}}{{end}}
Input:"""
{{printBatch .Input .Prefix .Start}}"""{{end}}
Output:`

func printBatch(s []string, prefix string, start int) string {
var b strings.Builder
fmt.Fprintln(&b, `"""`)
for i, s := range s {
if prefix == "" {
fmt.Fprintln(&b, s)
} else if strings.Count(prefix, "%d") == 0 {
fmt.Fprintln(&b, prefix+s)
} else {
fmt.Fprintf(&b, prefix+"%s\n", start+i+1, s)
fmt.Fprintln(&b, fmt.Sprintf(prefix, start+i+1)+s)
}
}
fmt.Fprint(&b, `"""`)
return b.String()
}

Expand Down
24 changes: 15 additions & 9 deletions prompt/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ func TestPrompt(t *testing.T) {
New("no example single input"),
[]string{"test"},
"",
[]string{"no example single input\nInput: test\nOutput:"},
[]string{"no example single input\nInput:\"\"\"\ntest\n\"\"\"\nOutput:"},
},
{
New("has example single input").SetExample(Example{[]string{"abc", "def"}, "example", ""}),
New("has example single input").SetExample(Example{[]string{"abc", "def"}, "example"}),
[]string{"test"},
"",
[]string{
"has example single input\nExample:\nInput:\"\"\"\nabc\ndef\n\"\"\"\nOutput: example\n###\nInput: test\nOutput:",
"has example single input\n###\nExample:\nInput:\"\"\"\nabc\ndef\n\"\"\"\nOutput: example\n###\nInput:\"\"\"\ntest\n\"\"\"\nOutput:",
},
},
{
New("has example with prefix single input").SetExample(Example{[]string{"abc", "def"}, "example", "%d|"}),
New("has example single input with prefix").SetExample(Example{[]string{"abc", "def"}, "example"}),
[]string{"test"},
"",
"%d|",
[]string{
"has example with prefix single input\nExample:\nInput:\"\"\"\n1|abc\n2|def\n\"\"\"\nOutput: example\n###\nInput: test\nOutput:",
"has example single input with prefix\n###\nExample:\nInput:\"\"\"\n1|abc\n2|def\n\"\"\"\nOutput: example\n###\nInput:\"\"\"\n1|test\n\"\"\"\nOutput:",
},
},
{
Expand All @@ -47,12 +47,18 @@ func TestPrompt(t *testing.T) {
[]string{"no example multiple inputs with prefix\nInput:\"\"\"\n1|test1\n2|test2\n\"\"\"\nOutput:"},
},
{
New("test limit").SetExample(Example{[]string{"abc", "def"}, "example", "%d|"}).SetLimit(2),
New("no example multiple inputs with fixed prefix"),
[]string{"test1", "test2"},
"test|",
[]string{"no example multiple inputs with fixed prefix\nInput:\"\"\"\ntest|test1\ntest|test2\n\"\"\"\nOutput:"},
},
{
New("test limit").SetExample(Example{[]string{"abc", "def"}, "example"}).SetLimit(2),
[]string{"test1", "test2", "test3", "test4"},
"%d|",
[]string{
"test limit\nExample:\nInput:\"\"\"\n1|abc\n2|def\n\"\"\"\nOutput: example\n###\nInput:\"\"\"\n1|test1\n2|test2\n\"\"\"\nOutput:",
"test limit\nExample:\nInput:\"\"\"\n1|abc\n2|def\n\"\"\"\nOutput: example\n###\nInput:\"\"\"\n3|test3\n4|test4\n\"\"\"\nOutput:",
"test limit\n###\nExample:\nInput:\"\"\"\n1|abc\n2|def\n\"\"\"\nOutput: example\n###\nInput:\"\"\"\n1|test1\n2|test2\n\"\"\"\nOutput:",
"test limit\n###\nExample:\nInput:\"\"\"\n1|abc\n2|def\n\"\"\"\nOutput: example\n###\nInput:\"\"\"\n3|test3\n4|test4\n\"\"\"\nOutput:",
},
},
} {
Expand Down