googleai

package
v1.0.178 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

This directory contains the provider for Google's Gemini models served by the Gemini API (https://ai.google.dev/), built on google.golang.org/genai.

  • googleai.New creates the llms.Model; configure it with the With* options in option.go (API key or credentials, default model, generation defaults, harm threshold, effort policy).
  • Infer / ValidateInference in inference.go implement the portable inference contract used by the router.
  • NewModelLister and GoogleAI.ListModels list the models an account can call.
  • internal/genaiutils converts llms.Tool definitions and JSON Schemas to genai types.

Testing:

Unit tests need no credentials; model listing runs against recorded responses in testdata/. TestIntegration* tests call the live API and are skipped under make test.

Documentation

Overview

Package googleai implements Google AI (Vertex/Gemini) llms.Model wrappers. Configure with an API key and default model; advanced flows are provided via internal clients in the package's internal submodules.

Example

mdl, err := googleai.New(context.Background(),
    googleai.WithAPIKey(os.Getenv("GOOGLE_API_KEY")),
    googleai.WithDefaultModel("gemini-1.5-flash"),
)
_ = mdl; _ = err

The models an account can call are listed with NewModelLister or with GoogleAI.ListModels.

package googleai implements a langchaingo provider for Google AI LLMs. See https://ai.google.dev/ for more details.

Index

Constants

View Source
const (
	CITATIONS            = "citations"
	SAFETY               = "safety"
	RoleSystem           = "system"
	RoleModel            = "model"
	RoleUser             = "user"
	RoleTool             = "tool"
	ResponseMIMETypeJson = "application/json"
)

Variables

View Source
var (
	ErrNoContentInResponse   = errors.New("no content in generation response")
	ErrUnknownPartInResponse = errors.New("unknown part type in generation response")
	ErrInvalidMimeType       = errors.New("invalid mime type on content")
)

Functions

func NewModelLister added in v1.0.173

func NewModelLister(ctx context.Context, opts ...Option) (llms.ModelLister, error)

NewModelLister returns a client that lists the models of a Google AI account. It takes the options of New; the default model is not used.

Types

type GoogleAI

type GoogleAI struct {
	// contains filtered or unexported fields
}

GoogleAI is a type that represents a Google AI API client.

func New

func New(ctx context.Context, opts ...Option) (*GoogleAI, error)

New creates a new GoogleAI client.

func (*GoogleAI) GenerateContent

func (g *GoogleAI) GenerateContent(
	ctx context.Context,
	messages []llms.Message,
	options ...llms.CallOption,
) (*llms.ContentResponse, error)

GenerateContent implements the llms.Model interface.

func (*GoogleAI) GetName added in v0.10.61

func (g *GoogleAI) GetName() string

GetName implements the Model interface.

func (*GoogleAI) GetProviderType added in v0.10.54

func (g *GoogleAI) GetProviderType() llms.ProviderType

GetProviderType implements the Model interface.

func (*GoogleAI) Infer added in v0.21.160

Infer preserves raw JSON schemas, function IDs, explicit zero sampling values and thought signatures.

func (*GoogleAI) ListModels added in v1.0.173

func (g *GoogleAI) ListModels(ctx context.Context) ([]llms.ModelInfo, error)

ListModels implements llms.ModelLister. It returns the models that can generate content, in the provider's order, named as a request names them: without the "models/" prefix. Embedding and live-only models are left out.

The API reports neither a lifecycle nor reasoning levels, so those fields are empty; a model that is withdrawn simply leaves the list.

func (*GoogleAI) ValidateInference added in v0.21.160

func (g *GoogleAI) ValidateInference(r llms.InferenceRequest) error

ValidateInference checks controls unsupported by the portable Gemini connector.

type Option

type Option func(*Options)

func WithAPIKey

func WithAPIKey(apiKey string) Option

WithAPIKey passes the API KEY (token) to the client. This is useful for googleai clients.

func WithCloudLocation

func WithCloudLocation(l string) Option

WithCloudLocation passes the GCP cloud location (region) name to the client. This is useful for vertex clients.

func WithCloudProject

func WithCloudProject(p string) Option

WithCloudProject passes the GCP cloud project name to the client. This is useful for vertex clients.

func WithCredentials added in v0.13.68

func WithCredentials(credentials *auth.Credentials) Option

WithCredentialsJSON append a ClientOption that authenticates API calls with the given service account or refresh token JSON credentials.

func WithDefaultCandidateCount

func WithDefaultCandidateCount(defaultCandidateCount int) Option

WithDefaultCandidateCount sets the candidate count for the model.

func WithDefaultEmbeddingModel

func WithDefaultEmbeddingModel(defaultEmbeddingModel string) Option

WithDefaultModel passes a default embedding model name to the client. This model name is used if not explicitly provided in specific client invocations.

func WithDefaultMaxTokens

func WithDefaultMaxTokens(maxTokens int) Option

WithDefaultMaxTokens sets the maximum token count for the model.

func WithDefaultModel

func WithDefaultModel(defaultModel string) Option

WithDefaultModel passes a default content model name to the client. This model name is used if not explicitly provided in specific client invocations.

func WithDefaultTemperature

func WithDefaultTemperature(defaultTemperature float64) Option

WithDefaultTemperature sets the maximum token count for the model.

func WithDefaultTopK

func WithDefaultTopK(defaultTopK int) Option

WithDefaultTopK sets the TopK for the model.

func WithDefaultTopP

func WithDefaultTopP(defaultTopP float64) Option

WithDefaultTopP sets the TopP for the model.

func WithEffortPolicy added in v1.0.173

func WithEffortPolicy(policy llms.EffortPolicy) Option

WithEffortPolicy sets the reasoning effort used when a call requests none, and the highest effort any call may use. Each call still sends the nearest thinking level its model accepts. Legacy GenerateContent only; the portable Infer path is unaffected.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient append a ClientOption that uses the provided HTTP client to make requests. This is useful for vertex clients.

func WithHarmThreshold

func WithHarmThreshold(ht genai.HarmBlockThreshold) Option

WithHarmThreshold sets the safety/harm setting for the model, potentially limiting any harmful content it may generate.

type Options

type Options struct {
	CloudProject          string
	CloudLocation         string
	DefaultModel          string
	DefaultEmbeddingModel string
	DefaultCandidateCount int
	DefaultMaxTokens      int
	DefaultTemperature    float64
	DefaultTopK           int
	DefaultTopP           float64
	HarmThreshold         genai.HarmBlockThreshold
	APIKey                string
	Credentials           *auth.Credentials
	HTTPClient            *http.Client
	// Effort is the reasoning effort default and ceiling for every call.
	Effort llms.EffortPolicy
}

Options is a set of options for GoogleAI and Vertex clients.

func DefaultOptions

func DefaultOptions() Options

func (*Options) EnsureAuthPresent

func (o *Options) EnsureAuthPresent()

EnsureAuthPresent fills in the API key from the GOOGLE_API_KEY environment variable when neither an API key nor credentials were configured. An explicit WithAPIKey or WithCredentials always wins over the environment.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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