Created
September 15, 2025 15:42
-
-
Save peterhellberg/8f8b2fb9b836d2014dd4f37fa397d727 to your computer and use it in GitHub Desktop.
Genkit Go example
This file contains hidden or 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 ( | |
"cmp" | |
"context" | |
"encoding/json" | |
"fmt" | |
"io" | |
"log" | |
"os" | |
"strings" | |
"github.com/firebase/genkit/go/ai" | |
"github.com/firebase/genkit/go/genkit" | |
"github.com/firebase/genkit/go/plugins/ollama" | |
) | |
type Response struct { | |
Name string | |
Reason string | |
} | |
func main() { | |
run(context.Background(), os.Stdout) | |
} | |
func run(ctx context.Context, w io.Writer) { | |
o := &ollama.Ollama{ | |
ServerAddress: ollamaServerAddress(), | |
} | |
g := genkit.Init(ctx, | |
genkit.WithPlugins(o), | |
genkit.WithDefaultModel("ollama/gemma3:latest"), | |
) | |
o.DefineModel(g, | |
ollama.ModelDefinition{Name: "gemma3:latest"}, | |
nil, | |
) | |
out, modelResponse, err := genkit.GenerateData[Response](ctx, g, | |
ai.WithSystem("You are a grumpy movie reviewer, that responds in just a short sentence"), | |
ai.WithPrompt("Who is the main protagonist in the 1999 movie The Matrix?"), | |
) | |
if err != nil { | |
log.Fatal(err) | |
} | |
encode(w, map[string]any{ | |
"model_response": modelResponse, | |
"out": out, | |
}) | |
} | |
func ollamaServerAddress() string { | |
ollamaAddr := cmp.Or(os.Getenv("OLLAMA_HOST"), "127.0.0.1:11434") | |
if !strings.Contains(ollamaAddr, ":") { | |
ollamaAddr += ":11434" | |
} | |
return fmt.Sprintf("http://%s", ollamaAddr) | |
} | |
func encode(w io.Writer, v any) error { | |
enc := json.NewEncoder(w) | |
enc.SetIndent("", " ") | |
return enc.Encode(v) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ go run grumpy_movie_reviewer.go | jq