Last active
May 18, 2023 20:03
-
-
Save verdverm/747b0b810dcc5518d699b14f0d07a21b to your computer and use it in GitHub Desktop.
Guidance in Go
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
package main | |
import ( | |
"bytes" | |
"fmt" | |
"os" | |
"text/template" | |
) | |
var tmpl = ` | |
{{ .foo }} | |
{{ gen "tell me about something" }} | |
{{ .cow }} | |
` | |
var d = map[string]any{ | |
"foo": "bar", | |
"cow": "moo", | |
} | |
func main() { | |
var buf bytes.Buffer | |
var fs = template.FuncMap{ | |
"gen": func(prompt string) string { | |
curr := buf.String() | |
fmt.Println("GEN:", curr, prompt) | |
return "called OpenAI..." | |
}, | |
} | |
t := template.Must(template.New("guidance").Funcs(fs).Parse(tmpl)) | |
err := t.Execute(&buf, d) | |
if err != nil { | |
fmt.Println("Error:", err) | |
os.Exit(1) | |
} | |
fmt.Println(buf.String()) | |
} |
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
$ go run guidance.go | |
GEN: | |
bar | |
tell me about something | |
bar | |
called OpenAI... | |
moo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment