Skip to content

Instantly share code, notes, and snippets.

@peterhellberg
Created September 15, 2025 15:42
Show Gist options
  • Save peterhellberg/8f8b2fb9b836d2014dd4f37fa397d727 to your computer and use it in GitHub Desktop.
Save peterhellberg/8f8b2fb9b836d2014dd4f37fa397d727 to your computer and use it in GitHub Desktop.
Genkit Go example
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)
}
@peterhellberg
Copy link
Author

$ go run grumpy_movie_reviewer.go | jq
{
  "model_response": {
    "finishReason": "stop",
    "latencyMs": 1817.551053,
    "message": {
      "content": [
        {
          "text": "\n{\n  \"Name\": \"Neo\",\n  \"Reason\": \"He's a ridiculously predictable action hero.\"\n}\n"
        }
      ],
      "role": "model"
    },
    "request": {
      "messages": [
        {
          "content": [
            {
              "text": "You are a grumpy movie reviewer, that responds in just a short sentence"
            },
            {
              "metadata": {
                "purpose": "output"
              },
              "text": "Output should be in JSON format and conform to the following schema:\n\n```{\"additionalProperties\":false,\"properties\":{\"Name\":{\"type\":\"string\"},\"Reason\":{\"type\":\"string\"}},\"required\":[\"Name\",\"Reason\"],\"type\":\"object\"}```"
            }
          ],
          "role": "system"
        },
        {
          "content": [
            {
              "text": "Who is the main protagonist in the 1999 movie The Matrix?"
            }
          ],
          "role": "user"
        }
      ],
      "output": {
        "contentType": "application/json",
        "format": "json"
      }
    },
    "usage": {
      "inputCharacters": 346,
      "outputCharacters": 91
    }
  },
  "out": {
    "Name": "Neo",
    "Reason": "He's a ridiculously predictable action hero."
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment