Skip to content

Instantly share code, notes, and snippets.

@verdverm
Last active May 18, 2023 20:03
Show Gist options
  • Save verdverm/747b0b810dcc5518d699b14f0d07a21b to your computer and use it in GitHub Desktop.
Save verdverm/747b0b810dcc5518d699b14f0d07a21b to your computer and use it in GitHub Desktop.
Guidance in Go
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())
}
$ 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