-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
77 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package prompt | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"text/template" | ||
) | ||
|
||
type PromptTemplate struct { | ||
template *template.Template | ||
} | ||
|
||
func NewPromptTemplate(input string) (*PromptTemplate, error) { | ||
tmpl, err := template.New("tmpl").Parse(input) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse template: %w", err) | ||
} | ||
|
||
return &PromptTemplate{ | ||
template: tmpl, | ||
}, nil | ||
} | ||
|
||
func (t *PromptTemplate) Invoke(input any) (string, error) { | ||
var buf bytes.Buffer | ||
if err := t.template.Execute(&buf, input); err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package prompt | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestInvoke(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
template string | ||
input any | ||
expected string | ||
}{ | ||
{ | ||
name: "template is string", | ||
template: "template", | ||
expected: "template", | ||
}, | ||
{ | ||
name: "template includes input", | ||
template: "the meaning of {{.Word}}?", | ||
input: map[string]string{ | ||
"Word": "satisfaction", | ||
}, | ||
expected: "the meaning of satisfaction?", | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
prompt, err := NewPromptTemplate(tc.template) | ||
if err != nil { | ||
t.Fatalf("Error happens: %v", err) | ||
} | ||
have, err := prompt.Invoke(tc.input) | ||
if err != nil { | ||
t.Fatalf("Error happens: %v", err) | ||
} | ||
if have != tc.expected { | ||
t.Fatalf("unexpected string: %v != %v", have, tc.expected) | ||
} | ||
}) | ||
} | ||
} |