vertex

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

README

Vertex AI

Use the Responses API through Google Vertex AI with the OpenAI Go client. You need a Google Cloud project, a location, a model available to your project, and Google Application Default Credentials.

Install the Vertex module:

go get github.com/openai/openai-go/v3/vertex

The Vertex module brings in Google authentication dependencies. Applications that only use github.com/openai/openai-go/v3 do not need to install it.

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/openai/openai-go/v3"
	"github.com/openai/openai-go/v3/responses"
	"github.com/openai/openai-go/v3/vertex"
)

func main() {
	ctx := context.Background()
	client, err := vertex.NewClient(ctx, vertex.Config{
		ProjectID: "your-project-id",
		Location:  "your-location",
	})
	if err != nil {
		log.Fatal(err)
	}

	response, err := client.Responses.New(ctx, responses.ResponseNewParams{
		Model: "your-model-id",
		Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Hello")},
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(response.ID)
}

ProjectID and Location default to GOOGLE_CLOUD_PROJECT and GOOGLE_CLOUD_LOCATION. To use credentials managed by your application, set Credentials or TokenProvider instead of using Application Default Credentials. The two fields are mutually exclusive.

For a custom endpoint, set BaseURL to the complete provider base before /responses. HTTP endpoints can be used for testing. The client sends credentials to the configured URL; use HTTPS for production.

Documentation

Overview

Package vertex configures the standard OpenAI client for Google Vertex AI.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewClient

func NewClient(ctx context.Context, cfg Config, userOpts ...option.RequestOption) (openai.Client, error)

NewClient creates a normal OpenAI client using Google Application Default Credentials unless Credentials or TokenProvider is supplied. Google token refresh and quota lookup receive the request context and its timeout.

Types

type Config

type Config struct {
	// ProjectID defaults to GOOGLE_CLOUD_PROJECT when BaseURL is not supplied.
	ProjectID string
	// Location defaults to GOOGLE_CLOUD_LOCATION when BaseURL is not supplied.
	Location string
	// BaseURL overrides the provider base URL.
	BaseURL string
	// TokenProvider overrides ADC, e.g. to use credentials managed by the application.
	TokenProvider TokenProvider
	// Credentials supplies native Google credentials, including their refresh
	// and quota project behavior. Mutually exclusive with TokenProvider.
	Credentials *auth.Credentials
}

Config selects a Vertex project and location. BaseURL, if supplied, is the complete provider base URL up to the final /v1 (before /responses). Only this explicitly trusted origin can receive credentials. The OpenAI environment is ignored.

type TokenProvider

type TokenProvider func(context.Context) (string, error)

TokenProvider supplies an access token for each request attempt, including retries. Implementations may cache valid tokens and must be safe for concurrent calls.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址