Get started with Genkit using Go (alpha)

The Firebase Genkit libraries for Go are now available for preview! Because the Go libraries are currently in Alpha, you might see API and functional changes as development progresses. We recommend using it only for prototyping and exploration.

If you discover issues with the libraries or this documentation please report them in our GitHub repository.

To get started with Genkit, install the Genkit CLI and run genkit init in a Go project. The rest of this page shows you how.

Requirements

  • Go 1.22 or later. See Download and install in the official Go docs.

  • Node.js 20 or later (for the Genkit CLI and UI). See the next section for a brief guide on installing Node.

Install Genkit

  1. If you don't already have Node 20 or newer on your system, install it now.

    Recommendation: The nvm and nvm-windows tools are a convenient way to install specific versions of Node if it's not already installed on your system. These tools install Node on a per-user basis, so you don't need to make system-wide changes.

    To install nvm:

    Linux, macOS, etc.

    Run the following command:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash

    Windows

    Download and run the installer as described in the nvm-windows docs.

    Then, to install Node and npm, open a new shell and run the following command:

    nvm install 20
  2. Install the Genkit CLI by running the following command:

    npm i -g genkit

    This command installs the Genkit CLI into your Node installation directory so that it can be used outside of a Node project.

Create and explore a sample project

  1. Create a new project directory:

    mkdir genkit-intro && cd genkit-intro
  2. Initialize a Genkit project:

    genkit init
    1. Select Go as the runtime environment.

    2. Select your model:

      Gemini (Google AI)

      The simplest way to get started is with Google AI Gemini API. Make sure it's available in your region.

      Generate an API key for the Gemini API using Google AI Studio. Then, set the GOOGLE_GENAI_API_KEY environment variable to your key:

      export GOOGLE_GENAI_API_KEY=<your API key>

      Gemini (Vertex AI)

      If the Google AI Gemini API is not available in your region, consider using the Vertex AI API which also offers Gemini and other models. You will need to have a billing-enabled Google Cloud project, enable AI Platform API, and set some additional environment variables:

      gcloud services enable aiplatform.googleapis.com
      export GCLOUD_PROJECT=<your project ID>
      export GCLOUD_LOCATION=us-central1

      See Vertex AI pricing.

    3. Specify anything for the module name. For example: example/genkit-intro

    4. Choose default answers to the rest of the questions, which will initialize your project folder with some sample code.

    The genkit init command creates a sample Go module and installs the required dependencies. The file main.go contains a single flow, menuSuggestionFlow, that prompts an LLM to suggest an item for a restaurant with a given theme.

    This file looks something like the following (the plugin configuration steps might look different if you selected Vertex AI):

    package main
    
    import (
    	"context"
    	"errors"
    	"fmt"
    	"log"
    
    	// Import Genkit and the Google AI plugin
    	"github.com/firebase/genkit/go/ai"
    	"github.com/firebase/genkit/go/genkit"
    	"github.com/firebase/genkit/go/plugins/googleai"
    )
    
    func main() {
    	ctx := context.Background()
    
    	// Initialize the Google AI plugin. When you pass nil for the
    	// Config parameter, the Google AI plugin will get the API key from the
    	// GOOGLE_GENAI_API_KEY environment variable, which is the recommended
    	// practice.
    	if err := googleai.Init(ctx, nil); err != nil {
    		log.Fatal(err)
    	}
    
    	// Define a simple flow that prompts an LLM to generate menu suggestions.
    	genkit.DefineFlow("menuSuggestionFlow", func(ctx context.Context, input string) (string, error) {
    		// The Google AI API provides access to several generative models. Here,
    		// we specify gemini-1.5-flash.
    		m := googleai.Model("gemini-1.5-flash")
    		if m == nil {
    			return "", errors.New("menuSuggestionFlow: failed to find model")
    		}
    
    		// Construct a request and send it to the model API (Google AI).
    		resp, err := ai.Generate(ctx, m,
    			ai.WithConfig(&ai.GenerationCommonConfig{Temperature: 1}),
    			ai.WithTextPrompt(fmt.Sprintf(`Suggest an item for the menu of a %s themed restaurant`, input)))
    		if err != nil {
    			return "", err
    		}
    
    		// Handle the response from the model API. In this sample, we just
    		// convert it to a string. but more complicated flows might coerce the
    		// response into structured output or chain the response into another
    		// LLM call.
    		text := resp.Text()
    		return text, nil
    	})
    
    	// Initialize Genkit and start a flow server. This call must come last,
    	// after all of your plug-in configuration and flow definitions. When you
    	// pass a nil configuration to Init, Genkit starts a local flow server,
    	// which you can interact with using the developer UI.
    	if err := genkit.Init(ctx, nil); err != nil {
    		log.Fatal(err)
    	}
    }
    
    

    As you build out your app's AI features with Genkit, you will likely create flows with multiple steps such as input preprocessing, more sophisticated prompt construction, integrating external information sources for retrieval-augmented generation (RAG), and more.

  3. Now you can run and explore Genkit features and the sample project locally on your machine. Download and start the Genkit Developer UI:

    genkit start

    Welcome to
Genkit Developer UI

    The Genkit Developer UI is now running on your machine. When you run models or flows in the next step, your machine will perform the orchestration tasks needed to get the steps of your flow working together; calls to external services such as the Gemini API will continue to be made against live servers.

    Also, because you are in a dev environment, Genkit will store traces and flow state in local files.

  4. The Genkit Developer UI downloads and opens automatically when you run the genkit start command.

    The Developer UI lets you see which flows you have defined and models you configured, run them, and examine traces of previous runs. Try out some of these features:

    • On the Run tab, you will see a list of all of the flows that you have defined and any models that have been configured by plugins.

      Click menuSuggestionFlow and try running it with some input text (for example, "cat"). If all goes well, you'll be rewarded with a menu suggestion for a cat themed restaurant.

    • On the Inspect tab, you'll see a history of flow executions. For each flow, you can see the parameters that were passed to the flow and a trace of each step as they ran.