diff --git a/prompt/example.go b/prompt/example.go index 2bea119..3b254be 100644 --- a/prompt/example.go +++ b/prompt/example.go @@ -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) } } diff --git a/prompt/example_test.go b/prompt/example_test.go index 11c8251..3677ea4 100644 --- a/prompt/example_test.go +++ b/prompt/example_test.go @@ -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) } } diff --git a/prompt/prompt.go b/prompt/prompt.go index f2659ed..c684d40 100644 --- a/prompt/prompt.go +++ b/prompt/prompt.go @@ -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() } diff --git a/prompt/prompt_test.go b/prompt/prompt_test.go index 5285b32..2d80eb2 100644 --- a/prompt/prompt_test.go +++ b/prompt/prompt_test.go @@ -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:", }, }, { @@ -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:", }, }, } {