Google Gen AI Go SDK

repository·main·Indexed 22 days ago

https://github.com/googleapis/go-genai

A programmatic interface for integrating Google's generative AI models, specifically the Gemini Developer API and the Gemini Enterprise Agent Platform, into Go applications. The SDK supports both Vertex AI and Gemini API backends.

Tokens
30.9K
Snippets
120
Records
157
Agent score
78%

What's inside go-genai

  1. Configure Thinking (Reasoning) capabilities

    main

    Gemini 3 series models support explicit reasoning.

    • Gemini 3: Thinking is on by default for gemini-3-pro-preview and gemini-3-flash-preview. You can control the level via ThinkingConfig.ThinkingLevel.
    • Gemini 2.5: Thinking is on by default. To turn it off, set ThinkingBudget to zero using genai.Ptr[int32](0).
    // Gemini 3 Example
    config := &genai.GenerateContentConfig{
    	ThinkingConfig: &genai.ThinkingConfig{
    		ThinkingLevel: genai.ThinkingLevelHigh,
    	},
    }
    result, err := client.Models.GenerateContent(ctx, "gemini-3-pro-preview", genai.Text("What is AI?"), config)
    
    for _, part := range result.Candidates[0].Content.Parts {
    	if part.Thought {
    		fmt.Printf("Thought: %s\n", part.Text)
    	} else {
    		fmt.Printf("Response: %s\n", part.Text)
    	}
    }
    
    // Gemini 2.5 Example (Turn off thinking)
    config := &genai.GenerateContentConfig{
    	ThinkingConfig: &genai.ThinkingConfig{
    		ThinkingBudget: genai.Ptr[int32](0),
    	},
    }
  2. Run the Image Upscale example

    main

    To run the image upscale example, first ensure you have the google.golang.org/genai package installed. Then, navigate to the example directory and execute the image.go file.

    Note: Ensure you have completed the environment variable setup (VertexAI or GeminiAPI) before running these commands.

    $ go get google.golang.org/genai
    $ cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/models/upscale_image`
    $ go run image.go
  3. Run the Caches example

    main

    To run the caches example, which demonstrates creating, getting, and deleting caches, follow these steps:

    1. Install the SDK using go get.
    2. Navigate to the example directory using go list to find the absolute path.
    3. Execute the create_get_delete.go file with go run.

    Ensure you have configured your environment variables (Vertex AI or Gemini API) before running the command.

    $ go get google.golang.org/genai
    $ cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/caches`
    $ go run create_get_delete.go
  4. Run the live_with_ephemeral_token example

    main

    To run the live_with_ephemeral_token example, you must first configure your environment variables for the GeminiAPI backend and then use the Go toolchain to download the dependency, navigate to the example directory, and execute the code.

    Environment Setup

    For the GeminiAPI Backend, set the following environment variables:

    • GOOGLE_GENAI_USE_VERTEXAI: Set to false.
    • GOOGLE_API_KEY: Set to your valid Google API key.

    Execution Steps

    1. Install the SDK: go get google.golang.org/genai
    2. Navigate to the example directory: cd $(go list -f '{{.Dir}}' google.golang.org/genai/examples/live_with_ephemeral_token)
    3. Run the example: go run *.go
    export GOOGLE_GENAI_USE_VERTEXAI=false
    export GOOGLE_API_KEY={YOUR_API_KEY}
    
    go get google.golang.org/genai
    cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/live_with_ephemeral_token`
    go run *.go
  5. Configure environment variables for Batch API backends

    main

    The Batch API examples require specific environment variables depending on whether you are using the Vertex AI backend or the Gemini API backend.

    Vertex AI Backend

    To use Vertex AI, set the following variables:

    • GOOGLE_GENAI_USE_VERTEXAI=true
    • GOOGLE_CLOUD_PROJECT: Your Google Cloud Project ID.
    • GOOGLE_CLOUD_LOCATION: Your preferred Google Cloud location (e.g., us-central1).

    Gemini API Backend

    To use the Gemini API, set the following variables:

    • GOOGLE_GENAI_USE_VERTEXAI=false
    • GOOGLE_API_KEY: Your Gemini API key.
    ### For VertexAI Backend
    ```bash
    export GOOGLE_GENAI_USE_VERTEXAI=true
    export GOOGLE_CLOUD_PROJECT={YOUR_PROJECT_ID}
    export GOOGLE_CLOUD_LOCATION={YOUR_LOCATION}

    For GeminiAPI Backend

    export GOOGLE_GENAI_USE_VERTEXAI=false
    export GOOGLE_API_KEY={YOUR_API_KEY}
  6. Configure environment variables for GeminiAPI Backend

    main

    When using the GeminiAPI backend for the filesearchstores example, ensure the following environment variables are exported in your shell:

    VariableValueDescription
    GOOGLE_GENAI_USE_VERTEXAIfalseDisables Vertex AI usage to ensure the GeminiAPI backend is used.
    GOOGLE_API_KEY{YOUR_API_KEY}Your personal Gemini API key.
    export GOOGLE_GENAI_USE_VERTEXAI=false
    export GOOGLE_API_KEY={YOUR_API_KEY}
  7. Install and run the files example

    main

    To run the files example, first install the SDK, navigate to the example directory, and then execute the specific Go files provided in the example package.

    1. Install the SDK: go get google.golang.org/genai
    2. Navigate to the example directory using go list.
    3. Run the example scripts: list_download.go and register_files.go.
    $ go get google.golang.org/genai
    $ cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/files`
    $ go run list_download.go
    $ go run register_files.go
  8. Install and run the live example

    main

    To run the live example provided in the repository, first install the SDK, then navigate to the example directory and execute the Go files. Ensure you have configured your environment variables for either VertexAI or GeminiAPI before running.

    $ go get google.golang.org/genai
    $ cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/live`
    $ go run *.go
  9. Configure environment variables for the Gen AI Go SDK

    main

    The Gen AI Go SDK supports two backends: Vertex AI and Gemini API. You must set environment variables to specify which backend to use and provide the necessary credentials.

    Vertex AI Backend

    To use the Vertex AI backend, set the following variables:

    • GOOGLE_GENAI_USE_VERTEXAI=true
    • GOOGLE_CLOUD_PROJECT: Your Google Cloud Project ID.
    • GOOGLE_CLOUD_LOCATION: Your preferred Google Cloud location (e.g., us-central1).

    Gemini API Backend

    To use the Gemini API backend, set the following variables:

    • GOOGLE_GENAI_USE_VERTEXAI=false
    • GOOGLE_API_KEY: Your Gemini API key.
    ### For VertexAI Backend
    ```bash
    export GOOGLE_GENAI_USE_VERTEXAI=true
    export GOOGLE_CLOUD_PROJECT={YOUR_PROJECT_ID}
    export GOOGLE_CLOUD_LOCATION={YOUR_LOCATION}

    For GeminiAPI Backend

    export GOOGLE_GENAI_USE_VERTEXAI=false
    export GOOGLE_API_KEY={YOUR_API_KEY}
  10. Install and run the generate_content example

    main

    To run the generate_content example, first install the SDK, navigate to the example directory, and execute the code. You can optionally specify a specific model using the --model flag.

    # Install the SDK
    go get google.golang.org/genai
    
    # Navigate to the example directory
    cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/models/generate_content`
    
    # Run the example
    go run text.go
    
    # Run with a specific model
    go run text.go --model=gemini-2.5-flash
  11. Run the File Search Stores example

    main

    To run the filesearchstores example, you must first configure your environment variables for the GeminiAPI backend and then use the Go toolchain to download, navigate to, and execute the example code.

    1. Configure Environment Variables

    For the GeminiAPI Backend, set the following variables in your terminal:

    • GOOGLE_GENAI_USE_VERTEXAI: Set to false.
    • GOOGLE_API_KEY: Set to your valid Gemini API key.

    2. Execute the Example

    Run the following commands to install the dependency, locate the example directory, and execute the main script:

    go get google.golang.org/genai
    cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/filesearchstores`
    go run create_upload_and_call_file_search.go
    export GOOGLE_GENAI_USE_VERTEXAI=false
    export GOOGLE_API_KEY={YOUR_API_KEY}
    
    go get google.golang.org/genai
    cd `go list -f '{{.Dir}}' google.golang.org/genai/examples/filesearchstores`
    go run create_upload_and_call_file_search.go