Package cloud.google.com/go/vertexai/genai (v0.15.0)

Note: To get more information about this package, such as access to older versions, viewthis package on pkg.go.dev.

Package genai is a client for the generative VertexAI model.

New users are encouraged to use the Google GenAI Go SDK available atgoogle.golang.org/genai

Functions

func Ptr

funcPtr(tT)*T

Ptr returns a pointer to its argument.It can be used to initialize pointer fields:

model.Temperature = genai.Ptr[float32](0.1)

func WithClientInfo

funcWithClientInfo(key,valuestring)option.ClientOption

WithClientInfo is an option that sets request informationidentifying the product that is calling this client.

func WithREST

funcWithREST()option.ClientOption

WithREST is an option that enables REST transport for the client.The default transport (if this option isn't provided) is gRPC.

Blob

typeBlobstruct{// Required. The IANA standard MIME type of the source data.MIMETypestring// Required. Raw bytes.Data[]byte}

Blob contains binary data like images. Use [Text] for text.

func ImageData

funcImageData(formatstring,data[]byte)Blob

ImageData is a convenience function for creating an imageBlob for input to a model.The format should be the second part of the MIME type, after "image/".For example, for a PNG image, pass "png".

BlockedError

typeBlockedErrorstruct{// If non-nil, the model's response was blocked.// Consult the Candidate and SafetyRatings fields for details.Candidate*Candidate// If non-nil, there was a problem with the prompt.PromptFeedback*PromptFeedback}

A BlockedError indicates that the model's response was blocked.There can be two underlying causes: the prompt or a candidate response.

func (*BlockedError) Error

func(e*BlockedError)Error()string

BlockedReason

typeBlockedReasonint32

BlockedReason is blocked reason enumeration.

BlockedReasonUnspecified, BlockedReasonSafety, BlockedReasonOther, BlockedReasonBlocklist, BlockedReasonProhibitedContent

const(// BlockedReasonUnspecified means unspecified blocked reason.BlockedReasonUnspecifiedBlockedReason=0// BlockedReasonSafety means candidates blocked due to safety.BlockedReasonSafetyBlockedReason=1// BlockedReasonOther means candidates blocked due to other reason.BlockedReasonOtherBlockedReason=2// BlockedReasonBlocklist means candidates blocked due to the terms which are included from the// terminology blocklist.BlockedReasonBlocklistBlockedReason=3// BlockedReasonProhibitedContent means candidates blocked due to prohibited content.BlockedReasonProhibitedContentBlockedReason=4)

func (BlockedReason) String

func(vBlockedReason)String()string

CachedContent

typeCachedContentstruct{// Expiration time of the cached content.//// Types that are assignable to Expiration:////*CachedContent_ExpireTime//*CachedContent_TtlExpirationExpireTimeOrTTL// Immutable. Identifier. The server-generated resource name of the cached// content Format:// projects/{project}/locations/{location}/cachedContents/{cached_content}Namestring// Immutable. The name of the `Model` to use for cached content. Currently,// only the published Gemini base models are supported, in form of// projects/{PROJECT}/locations/{LOCATION}/publishers/google/models/{MODEL}Modelstring// Optional. Input only. Immutable. Developer set system instruction.// Currently, text onlySystemInstruction*Content// Optional. Input only. Immutable. The content to cacheContents[]*Content// Optional. Input only. Immutable. A list of `Tools` the model may use to// generate the next responseTools[]*Tool// Optional. Input only. Immutable. Tool config. This config is shared for all// toolsToolConfig*ToolConfig// Output only. Creation time of the cache entry.CreateTimetime.Time// Output only. When the cache entry was last updated in UTC time.UpdateTimetime.Time}

CachedContent is a resource used in LLM queries for users to explicitly specify what to cacheand how to cache.

CachedContentIterator

typeCachedContentIteratorstruct{// contains filtered or unexported fields}

A CachedContentIterator iterates over CachedContents.

func (*CachedContentIterator) Next

Next returns the next result. Its second return value is iterator.Done if there are no moreresults. Once Next returns Done, all subsequent calls will return Done.

func (*CachedContentIterator) PageInfo

func(it*CachedContentIterator)PageInfo()*iterator.PageInfo

PageInfo supports pagination. See the google.golang.org/api/iterator package for details.

CachedContentToUpdate

typeCachedContentToUpdatestruct{// If non-nil, update the expire time or TTL.Expiration*ExpireTimeOrTTL}

CachedContentToUpdate specifies which fields of a CachedContent to modify in a call to[Client.UpdateCachedContent].

Candidate

typeCandidatestruct{// Output only. Index of the candidate.Indexint32// Output only. Content parts of the candidate.Content*Content// Output only. The reason why the model stopped generating tokens.// If empty, the model has not stopped generating the tokens.FinishReasonFinishReason// Output only. List of ratings for the safety of a response candidate.//// There is at most one rating per category.SafetyRatings[]*SafetyRating// Output only. Describes the reason the mode stopped generating tokens in// more detail. This is only filled when `finish_reason` is set.FinishMessagestring// Output only. Source attribution of the generated content.CitationMetadata*CitationMetadata}

Candidate is a response candidate generated from the model.

func (*Candidate) FunctionCalls

func(c*Candidate)FunctionCalls()[]FunctionCall

FunctionCalls return all the FunctionCall parts in the candidate.

ChatSession

typeChatSessionstruct{History[]*Content// contains filtered or unexported fields}

A ChatSession provides interactive chat.

Example

packagemainimport("context""fmt""log""cloud.google.com/go/vertexai/genai""google.golang.org/api/iterator")// Your GCP projectconstprojectID="your-project"// A GCP location like "us-central1"; if you're using standard Google-published// models (like untuned Gemini models), you can keep location blank ("").constlocation="some-gcp-location"// A model name like "gemini-1.0-pro"// For custom models from different publishers, prepent the full publisher// prefix for the model, e.g.:////modelName = publishers/some-publisher/models/some-model-nameconstmodelName="some-model"funcmain(){ctx:=context.Background()client,err:=genai.NewClient(ctx,projectID,location)iferr!=nil{log.Fatal(err)}deferclient.Close()model:=client.GenerativeModel(modelName)cs:=model.StartChat()send:=func(msgstring)*genai.GenerateContentResponse{fmt.Printf("== Me: %s\n== Model:\n",msg)res,err:=cs.SendMessage(ctx,genai.Text(msg))iferr!=nil{log.Fatal(err)}returnres}res:=send("Can you name some brands of air fryer?")printResponse(res)iter:=cs.SendMessageStream(ctx,genai.Text("Which one of those do you recommend?"))for{res,err:=iter.Next()iferr==iterator.Done{break}iferr!=nil{log.Fatal(err)}printResponse(res)}fori,c:=rangecs.History{log.Printf("    %d: %+v",i,c)}res=send("Why do you like the Philips?")iferr!=nil{log.Fatal(err)}printResponse(res)}funcprintResponse(resp*genai.GenerateContentResponse){for_,cand:=rangeresp.Candidates{for_,part:=rangecand.Content.Parts{fmt.Println(part)}}fmt.Println("---")}

func (*ChatSession) SendMessage

func(cs*ChatSession)SendMessage(ctxcontext.Context,parts...Part)(*GenerateContentResponse,error)

SendMessage sends a request to the model as part of a chat session.

func (*ChatSession) SendMessageStream

func(cs*ChatSession)SendMessageStream(ctxcontext.Context,parts...Part)*GenerateContentResponseIterator

SendMessageStream is like SendMessage, but with a streaming request.

Citation

typeCitationstruct{// Output only. Start index into the content.StartIndexint32// Output only. End index into the content.EndIndexint32// Output only. Url reference of the attribution.URIstring// Output only. Title of the attribution.Titlestring// Output only. License of the attribution.Licensestring// Output only. Publication date of the attribution.PublicationDatecivil.Date}

Citation contains source attributions for content.

CitationMetadata

typeCitationMetadatastruct{// Output only. List of citations.Citations[]*Citation}

CitationMetadata is a collection of source attributions for a piece of content.

Client

typeClientstruct{// contains filtered or unexported fields}

A Client is a Google Vertex AI client.

Example

cachedContent

packagemainimport("context""log""os""cloud.google.com/go/vertexai/genai")// Your GCP projectconstprojectID="your-project"// A GCP location like "us-central1"; if you're using standard Google-published// models (like untuned Gemini models), you can keep location blank ("").constlocation="some-gcp-location"// A model name like "gemini-1.0-pro"// For custom models from different publishers, prepent the full publisher// prefix for the model, e.g.:////modelName = publishers/some-publisher/models/some-model-nameconstmodelName="some-model"funcmain(){ctx:=context.Background()client,err:=genai.NewClient(ctx,projectID,location)iferr!=nil{log.Fatal(err)}deferclient.Close()file:=genai.FileData{MIMEType:"application/pdf",FileURI:"gs://my-bucket/my-doc.pdf"}cc,err:=client.CreateCachedContent(ctx,&genai.CachedContent{Model:modelName,Contents:[]*genai.Content{genai.NewUserContent(file)},})model:=client.GenerativeModelFromCachedContent(cc)// Work with the model as usual in this program._=model// Store the CachedContent name for later use.iferr:=os.WriteFile("my-cached-content-name",[]byte(cc.Name),0o644);err!=nil{log.Fatal(err)}///////////////////////////////// Later, in another process...bytes,err:=os.ReadFile("my-cached-content-name")iferr!=nil{log.Fatal(err)}ccName:=string(bytes)// No need to call [Client.GetCachedContent]; the name is sufficient.model=client.GenerativeModel(modelName)model.CachedContentName=ccName// Proceed as usual.}

func NewClient

funcNewClient(ctxcontext.Context,projectID,locationstring,opts...option.ClientOption)(*Client,error)

NewClient creates a new Google Vertex AI client.

Clients should be reused instead of created as needed. The methods of Clientare safe for concurrent use by multiple goroutines.projectID is your GCP project; location is GCP region/location perhttps://cloud.google.com/vertex-ai/docs/general/locationsIf location is empty, this function attempts to infer it from environmentvariables and falls back to a default location if unsuccessful.

You may configure the client by passing in options from the[google.golang.org/api/option] package. You may also use options defined inthis package, such as [WithREST].

func (*Client) Close

func(c*Client)Close()error

Close closes the client.

func (*Client) CreateCachedContent

func(c*Client)CreateCachedContent(ctxcontext.Context,cc*CachedContent)(*CachedContent,error)

CreateCachedContent creates a new CachedContent.The argument should contain a model name and some data to be cached, which can includecontents, a system instruction, tools and/or tool configuration. It can alsoinclude an expiration time or TTL. But it should not include a name; the systemwill generate one.

The return value will contain the name, which should be used to refer to the CachedContentin other API calls. It will also hold various metadata like expiration and creation time.It will not contain any of the actual content provided as input.

You can use the return value to create a model with [Client.GenerativeModelFromCachedContent].Or you can set [GenerativeModel.CachedContentName] to the name of the CachedContent, in whichcase you must ensure that the model provided in this call matches the name in the [GenerativeModel].

func (*Client) DeleteCachedContent

func(c*Client)DeleteCachedContent(ctxcontext.Context,namestring)error

DeleteCachedContent deletes the CachedContent with the given name.

func (*Client) GenerativeModel

func(c*Client)GenerativeModel(namestring)*GenerativeModel

GenerativeModel creates a new instance of the named model.name is a string model name like "gemini-1.0-pro" or "models/gemini-1.0-pro"for Google-published models.Seehttps://cloud.google.com/vertex-ai/generative-ai/docs/learn/model-versioningfor details on model naming and versioning, andhttps://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/explore-modelsfor providing model garden names. The SDK isn't familiar with custom modelgarden models, and will pass your model name to the backend API server.

func (*Client) GenerativeModelFromCachedContent

func(c*Client)GenerativeModelFromCachedContent(cc*CachedContent)*GenerativeModel

GenerativeModelFromCachedContent returns a [GenerativeModel] that uses the given [CachedContent].The argument should come from a call to [Client.CreateCachedContent] or [Client.GetCachedContent].

func (*Client) GetCachedContent

func(c*Client)GetCachedContent(ctxcontext.Context,namestring)(*CachedContent,error)

GetCachedContent retrieves the CachedContent with the given name.

func (*Client) ListCachedContents

func(c*Client)ListCachedContents(ctxcontext.Context)*CachedContentIterator

ListCachedContents lists all the CachedContents associated with the project and location.

func (*Client) UpdateCachedContent

UpdateCachedContent modifies the [CachedContent] according to the valuesof the [CachedContentToUpdate] struct.It returns the modified CachedContent.

The argument CachedContent must have its Name field populated.If its UpdateTime field is non-zero, it will be compared with the update timeof the stored CachedContent and the call will fail if they differ.This avoids a race condition when two updates are attempted concurrently.All other fields of the argument CachedContent are ignored.

Content

typeContentstruct{// Optional. The producer of the content. Must be either 'user' or 'model'.//// Useful to set for multi-turn conversations, otherwise can be left blank// or unset.Rolestring// Required. Ordered `Parts` that constitute a single message. Parts may have// different IANA MIME types.Parts[]Part}

Content is the base structured datatype containing multi-part content of a message.

AContent includes arole field designating the producer of theContentand aparts field containing multi-part data that contains the content ofthe message turn.

func NewUserContent

funcNewUserContent(parts...Part)*Content

NewUserContent returns a [Content] with a "user" role set and one or moreparts.

CountTokensResponse

typeCountTokensResponsestruct{// The total number of tokens counted across all instances from the request.TotalTokensint32// The total number of billable characters counted across all instances from// the request.TotalBillableCharactersint32}

CountTokensResponse is response message for[PredictionService.CountTokens][google.cloud.aiplatform.v1beta1.PredictionService.CountTokens].

ExpireTimeOrTTL

typeExpireTimeOrTTLstruct{ExpireTimetime.TimeTTLtime.Duration}

ExpireTimeOrTTL describes the time when a resource expires.If ExpireTime is non-zero, it is the expiration time.Otherwise, the expiration time is the value of TTL ("time to live") addedto the current time.

FileData

typeFileDatastruct{// Required. The IANA standard MIME type of the source data.MIMETypestring// Required. URI.FileURIstring}

FileData is URI based data.

FinishReason

typeFinishReasonint32

FinishReason is the reason why the model stopped generating tokens.If empty, the model has not stopped generating the tokens.

FinishReasonUnspecified, FinishReasonStop, FinishReasonMaxTokens, FinishReasonSafety, FinishReasonRecitation, FinishReasonOther, FinishReasonBlocklist, FinishReasonProhibitedContent, FinishReasonSpii, FinishReasonMalformedFunctionCall

const(// FinishReasonUnspecified means the finish reason is unspecified.FinishReasonUnspecifiedFinishReason=0// FinishReasonStop means token generation reached a natural stopping point or a configured stop// sequence.FinishReasonStopFinishReason=1// FinishReasonMaxTokens means token generation reached the configured maximum output tokens.FinishReasonMaxTokensFinishReason=2// FinishReasonSafety means token generation stopped because the content potentially contains safety// violations. NOTE: When streaming,// [content][google.cloud.aiplatform.v1beta1.Candidate.content] is empty if// content filters blocks the output.FinishReasonSafetyFinishReason=3// FinishReasonRecitation means token generation stopped because the content potentially contains// copyright violations.FinishReasonRecitationFinishReason=4// FinishReasonOther means all other reasons that stopped the token generation.FinishReasonOtherFinishReason=5// FinishReasonBlocklist means token generation stopped because the content contains forbidden terms.FinishReasonBlocklistFinishReason=6// FinishReasonProhibitedContent means token generation stopped for potentially containing prohibited content.FinishReasonProhibitedContentFinishReason=7// FinishReasonSpii means token generation stopped because the content potentially contains// Sensitive Personally Identifiable Information (SPII).FinishReasonSpiiFinishReason=8// FinishReasonMalformedFunctionCall means the function call generated by the model is invalid.FinishReasonMalformedFunctionCallFinishReason=9)

func (FinishReason) String

func(vFinishReason)String()string

FunctionCall

typeFunctionCallstruct{// Required. The name of the function to call.// Matches [FunctionDeclaration.name].Namestring// Optional. Required. The function parameters and values in JSON object// format. See [FunctionDeclaration.parameters] for parameter details.Argsmap[string]any}

FunctionCall is a predicted [FunctionCall] returned from the model that contains a stringrepresenting the [FunctionDeclaration.name] and a structured JSON objectcontaining the parameters and their values.

FunctionCallingConfig

typeFunctionCallingConfigstruct{// Optional. Function calling mode.ModeFunctionCallingMode// Optional. Function names to call. Only set when the Mode is ANY. Function// names should match [FunctionDeclaration.name]. With mode set to ANY, model// will predict a function call from the set of function names provided.AllowedFunctionNames[]string}

FunctionCallingConfig holds configuration for function calling.

FunctionCallingMode

typeFunctionCallingModeint32

FunctionCallingMode is function calling mode.

FunctionCallingUnspecified, FunctionCallingAuto, FunctionCallingAny, FunctionCallingNone

const(// FunctionCallingUnspecified means unspecified function calling mode. This value should not be used.FunctionCallingUnspecifiedFunctionCallingMode=0// FunctionCallingAuto means default model behavior, model decides to predict either function calls// or natural language response.FunctionCallingAutoFunctionCallingMode=1// FunctionCallingAny means model is constrained to always predicting function calls only.// If "allowed_function_names" are set, the predicted function calls will be// limited to any one of "allowed_function_names", else the predicted// function calls will be any one of the provided "function_declarations".FunctionCallingAnyFunctionCallingMode=2// FunctionCallingNone means model will not predict any function calls. Model behavior is same as when// not passing any function declarations.FunctionCallingNoneFunctionCallingMode=3)

func (FunctionCallingMode) String

FunctionDeclaration

typeFunctionDeclarationstruct{// Required. The name of the function to call.// Must start with a letter or an underscore.// Must be a-z, A-Z, 0-9, or contain underscores, dots and dashes, with a// maximum length of 64.Namestring// Optional. Description and purpose of the function.// Model uses it to decide how and whether to call the function.Descriptionstring// Optional. Describes the parameters to this function in JSON Schema Object// format. Reflects the Open API 3.03 Parameter Object. string Key: the name// of the parameter. Parameter names are case sensitive. Schema Value: the// Schema defining the type used for the parameter. For function with no// parameters, this can be left unset. Parameter names must start with a// letter or an underscore and must only contain chars a-z, A-Z, 0-9, or// underscores with a maximum length of 64. Example with 1 required and 1// optional parameter: type: OBJECT properties:////param1://  type: STRING//param2://  type: INTEGER//// required://   - param1Parameters*Schema// Optional. Describes the output from this function in JSON Schema format.// Reflects the Open API 3.03 Response Object. The Schema defines the type// used for the response value of the function.Response*Schema}

FunctionDeclaration is structured representation of a function declaration as defined by theOpenAPI 3.0 specification. Includedin this declaration are the function name, description, parameters andresponse type. This FunctionDeclaration is a representation of a block ofcode that can be used as aTool by the model and executed by the client.

FunctionResponse

typeFunctionResponsestruct{// Required. The name of the function to call.// Matches [FunctionDeclaration.name] and [FunctionCall.name].Namestring// Required. The function response in JSON object format.// Use "output" key to specify function output and "error" key to specify// error details (if any). If "output" and "error" keys are not specified,// then whole "response" is treated as function output.Responsemap[string]any}

FunctionResponse is the result output from a [FunctionCall] that contains a string representingthe [FunctionDeclaration.name] and a structured JSON object containing anyoutput from the function is used as context to the model. This should containthe result of a [FunctionCall] made based on model prediction.

GenerateContentResponse

typeGenerateContentResponsestruct{// Output only. Generated candidates.Candidates[]*Candidate// Output only. Content filter results for a prompt sent in the request.// Note: Sent only in the first stream chunk.// Only happens when no candidates were generated due to content violations.PromptFeedback*PromptFeedback// Usage metadata about the response(s).UsageMetadata*UsageMetadata}

GenerateContentResponse is the response from a GenerateContent or GenerateContentStream call.

GenerateContentResponseIterator

typeGenerateContentResponseIteratorstruct{// contains filtered or unexported fields}

GenerateContentResponseIterator is an iterator over GnerateContentResponse.

func (*GenerateContentResponseIterator) MergedResponse

MergedResponse returns the result of combining all the streamed responses seen so far.After iteration completes, the merged response should match the response obtained without streaming(that is, if [GenerativeModel.GenerateContent] were called).

func (*GenerateContentResponseIterator) Next

Next returns the next response.

GenerationConfig

typeGenerationConfigstruct{// Optional. Controls the randomness of predictions.Temperature*float32// Optional. If specified, nucleus sampling will be used.TopP*float32// Optional. If specified, top-k sampling will be used.TopK*int32// Optional. Number of candidates to generate.CandidateCount*int32// Optional. The maximum number of output tokens to generate per message.MaxOutputTokens*int32// Optional. Stop sequences.StopSequences[]string// Optional. Positive penalties.PresencePenalty*float32// Optional. Frequency penalties.FrequencyPenalty*float32// Optional. Output response mimetype of the generated candidate text.// Supported mimetype:// - `text/plain`: (default) Text output.// - `application/json`: JSON response in the candidates.// The model needs to be prompted to output the appropriate response type,// otherwise the behavior is undefined.// This is a preview feature.ResponseMIMETypestring// Optional. The `Schema` object allows the definition of input and output// data types. These types can be objects, but also primitives and arrays.// Represents a select subset of an [OpenAPI 3.0 schema// object](https://spec.openapis.org/oas/v3.0.3#schema).// If set, a compatible response_mime_type must also be set.// Compatible mimetypes:// `application/json`: Schema for JSON response.ResponseSchema*Schema// Optional. Config for thinking features.// An error will be returned if this field is set for models that don't// support thinking.ThinkingConfig*ThinkingConfig}

GenerationConfig is generation config.

func (*GenerationConfig) SetCandidateCount

func(c*GenerationConfig)SetCandidateCount(xint32)

SetCandidateCount sets the CandidateCount field.

func (*GenerationConfig) SetMaxOutputTokens

func(c*GenerationConfig)SetMaxOutputTokens(xint32)

SetMaxOutputTokens sets the MaxOutputTokens field.

func (*GenerationConfig) SetTemperature

func(c*GenerationConfig)SetTemperature(xfloat32)

SetTemperature sets the Temperature field.

func (*GenerationConfig) SetTopK

func(c*GenerationConfig)SetTopK(xint32)

SetTopK sets the TopK field.

func (*GenerationConfig) SetTopP

func(c*GenerationConfig)SetTopP(xfloat32)

SetTopP sets the TopP field.

GenerativeModel

typeGenerativeModelstruct{GenerationConfigSafetySettings[]*SafetySettingTools[]*ToolToolConfig*ToolConfig// configuration for toolsSystemInstruction*Content// The name of the CachedContent to use.// Must have already been created with [Client.CreateCachedContent].CachedContentNamestring// contains filtered or unexported fields}

GenerativeModel is a model that can generate text.Create one with [Client.GenerativeModel], then configureit by setting the exported fields.

The model holds all the config for a GenerateContentRequest, so the GenerateContent methodcan use a vararg for the content.

func (*GenerativeModel) CountTokens

func(m*GenerativeModel)CountTokens(ctxcontext.Context,parts...Part)(*CountTokensResponse,error)

CountTokens counts the number of tokens in the content.

Example

packagemainimport("context""fmt""log""cloud.google.com/go/vertexai/genai")// Your GCP projectconstprojectID="your-project"// A GCP location like "us-central1"; if you're using standard Google-published// models (like untuned Gemini models), you can keep location blank ("").constlocation="some-gcp-location"// A model name like "gemini-1.0-pro"// For custom models from different publishers, prepent the full publisher// prefix for the model, e.g.:////modelName = publishers/some-publisher/models/some-model-nameconstmodelName="some-model"funcmain(){ctx:=context.Background()client,err:=genai.NewClient(ctx,projectID,location)iferr!=nil{log.Fatal(err)}deferclient.Close()model:=client.GenerativeModel(modelName)resp,err:=model.CountTokens(ctx,genai.Text("What kind of fish is this?"))iferr!=nil{log.Fatal(err)}fmt.Println("Num tokens:",resp.TotalTokens)}

func (*GenerativeModel) GenerateContent

func(m*GenerativeModel)GenerateContent(ctxcontext.Context,parts...Part)(*GenerateContentResponse,error)

GenerateContent produces a single request and response.

Examples

packagemainimport("context""fmt""log""cloud.google.com/go/vertexai/genai")// Your GCP projectconstprojectID="your-project"// A GCP location like "us-central1"; if you're using standard Google-published// models (like untuned Gemini models), you can keep location blank ("").constlocation="some-gcp-location"// A model name like "gemini-1.0-pro"// For custom models from different publishers, prepent the full publisher// prefix for the model, e.g.:////modelName = publishers/some-publisher/models/some-model-nameconstmodelName="some-model"funcmain(){ctx:=context.Background()client,err:=genai.NewClient(ctx,projectID,location)iferr!=nil{log.Fatal(err)}deferclient.Close()model:=client.GenerativeModel(modelName)model.SetTemperature(0.9)resp,err:=model.GenerateContent(ctx,genai.Text("What is the average size of a swallow?"))iferr!=nil{log.Fatal(err)}printResponse(resp)}funcprintResponse(resp*genai.GenerateContentResponse){for_,cand:=rangeresp.Candidates{for_,part:=rangecand.Content.Parts{fmt.Println(part)}}fmt.Println("---")}
config
packagemainimport("context""fmt""log""cloud.google.com/go/vertexai/genai")funcmain(){ctx:=context.Background()constprojectID="YOUR PROJECT ID"constlocation="GCP LOCATION"client,err:=genai.NewClient(ctx,projectID,location)iferr!=nil{log.Fatal(err)}deferclient.Close()model:=client.GenerativeModel("gemini-1.0-pro")model.SetTemperature(0.9)model.SetTopP(0.5)model.SetTopK(20)model.SetMaxOutputTokens(100)model.SystemInstruction=genai.NewUserContent(genai.Text("You are Yoda from Star Wars."))resp,err:=model.GenerateContent(ctx,genai.Text("What is the average size of a swallow?"))iferr!=nil{log.Fatal(err)}printResponse(resp)}funcprintResponse(resp*genai.GenerateContentResponse){for_,cand:=rangeresp.Candidates{for_,part:=rangecand.Content.Parts{fmt.Println(part)}}fmt.Println("---")}
goroutine
packagemainimport("context""fmt""log""cloud.google.com/go/vertexai/genai")funcmain(){ctx:=context.Background()constprojectID="YOUR PROJECT ID"constlocation="GCP LOCATION"client,err:=genai.NewClient(ctx,projectID,location)iferr!=nil{log.Fatal(err)}deferclient.Close()model:=client.GenerativeModel("gemini-1.0-pro")queries:=[]string{"Hello, World!","What's the weather today?"}resultChan:=make(chan*genai.GenerateContentResponse,len(queries))worker:=func(querystring){result,err:=model.GenerateContent(ctx,genai.Text(query))iferr!=nil{log.Fatal(err)}resultChan<-result}// Send two requests concurrentlyfor_,query:=rangequeries{goworker(query)}// Wait for the responsesfora:=0;a <len(queries);a++{result:=<-resultChanprintResponse(result)}close(resultChan)}funcprintResponse(resp*genai.GenerateContentResponse){for_,cand:=rangeresp.Candidates{for_,part:=rangecand.Content.Parts{fmt.Println(part)}}fmt.Println("---")}

func (*GenerativeModel) GenerateContentStream

func(m*GenerativeModel)GenerateContentStream(ctxcontext.Context,parts...Part)*GenerateContentResponseIterator

GenerateContentStream returns an iterator that enumerates responses.

Example

packagemainimport("context""fmt""log""cloud.google.com/go/vertexai/genai""google.golang.org/api/iterator")// Your GCP projectconstprojectID="your-project"// A GCP location like "us-central1"; if you're using standard Google-published// models (like untuned Gemini models), you can keep location blank ("").constlocation="some-gcp-location"// A model name like "gemini-1.0-pro"// For custom models from different publishers, prepent the full publisher// prefix for the model, e.g.:////modelName = publishers/some-publisher/models/some-model-nameconstmodelName="some-model"funcmain(){ctx:=context.Background()client,err:=genai.NewClient(ctx,projectID,location)iferr!=nil{log.Fatal(err)}deferclient.Close()model:=client.GenerativeModel(modelName)iter:=model.GenerateContentStream(ctx,genai.Text("Tell me a story about a lumberjack and his giant ox. Keep it very short."))for{resp,err:=iter.Next()iferr==iterator.Done{break}iferr!=nil{log.Fatal(err)}printResponse(resp)}}funcprintResponse(resp*genai.GenerateContentResponse){for_,cand:=rangeresp.Candidates{for_,part:=rangecand.Content.Parts{fmt.Println(part)}}fmt.Println("---")}

func (*GenerativeModel) Name

func(m*GenerativeModel)Name()string

Name returns the name of the model.

func (*GenerativeModel) StartChat

func(m*GenerativeModel)StartChat()*ChatSession

StartChat starts a chat session.

HarmBlockMethod

typeHarmBlockMethodint32

HarmBlockMethod determines how harm blocking is done.

HarmBlockMethodUnspecified, HarmBlockMethodSeverity, HarmBlockMethodProbability

const(// HarmBlockMethodUnspecified means the harm block method is unspecified.HarmBlockMethodUnspecifiedHarmBlockMethod=0// HarmBlockMethodSeverity means the harm block method uses both probability and severity scores.HarmBlockMethodSeverityHarmBlockMethod=1// HarmBlockMethodProbability means the harm block method uses the probability score.HarmBlockMethodProbabilityHarmBlockMethod=2)

func (HarmBlockMethod) String

func(vHarmBlockMethod)String()string

HarmBlockThreshold

typeHarmBlockThresholdint32

HarmBlockThreshold specifies probability based thresholds levels for blocking.

HarmBlockUnspecified, HarmBlockLowAndAbove, HarmBlockMediumAndAbove, HarmBlockOnlyHigh, HarmBlockNone, HarmBlockSafetysettingOff

const(// HarmBlockUnspecified means unspecified harm block threshold.HarmBlockUnspecifiedHarmBlockThreshold=0// HarmBlockLowAndAbove means block low threshold and above (i.e. block more).HarmBlockLowAndAboveHarmBlockThreshold=1// HarmBlockMediumAndAbove means block medium threshold and above.HarmBlockMediumAndAboveHarmBlockThreshold=2// HarmBlockOnlyHigh means block only high threshold (i.e. block less).HarmBlockOnlyHighHarmBlockThreshold=3// HarmBlockNone means block none.HarmBlockNoneHarmBlockThreshold=4// HarmBlockSafetysettingOff means turn off the safety filter.HarmBlockSafetysettingOffHarmBlockThreshold=5)

func (HarmBlockThreshold) String

HarmCategory

typeHarmCategoryint32

HarmCategory specifies harm categories that will block the content.

HarmCategoryUnspecified, HarmCategoryHateSpeech, HarmCategoryDangerousContent, HarmCategoryHarassment, HarmCategorySexuallyExplicit, HarmCategoryCivicIntegrity

const(// HarmCategoryUnspecified means the harm category is unspecified.HarmCategoryUnspecifiedHarmCategory=0// HarmCategoryHateSpeech means the harm category is hate speech.HarmCategoryHateSpeechHarmCategory=1// HarmCategoryDangerousContent means the harm category is dangerous content.HarmCategoryDangerousContentHarmCategory=2// HarmCategoryHarassment means the harm category is harassment.HarmCategoryHarassmentHarmCategory=3// HarmCategorySexuallyExplicit means the harm category is sexually explicit content.HarmCategorySexuallyExplicitHarmCategory=4// HarmCategoryCivicIntegrity means deprecated: Election filter is not longer supported.// The harm category is civic integrity.//// Deprecated: Marked as deprecated in google/cloud/aiplatform/v1beta1/content.proto.HarmCategoryCivicIntegrityHarmCategory=5)

func (HarmCategory) String

func(vHarmCategory)String()string

HarmProbability

typeHarmProbabilityint32

HarmProbability specifies harm probability levels in the content.

HarmProbabilityUnspecified, HarmProbabilityNegligible, HarmProbabilityLow, HarmProbabilityMedium, HarmProbabilityHigh

const(// HarmProbabilityUnspecified means harm probability unspecified.HarmProbabilityUnspecifiedHarmProbability=0// HarmProbabilityNegligible means negligible level of harm.HarmProbabilityNegligibleHarmProbability=1// HarmProbabilityLow means low level of harm.HarmProbabilityLowHarmProbability=2// HarmProbabilityMedium means medium level of harm.HarmProbabilityMediumHarmProbability=3// HarmProbabilityHigh means high level of harm.HarmProbabilityHighHarmProbability=4)

func (HarmProbability) String

func(vHarmProbability)String()string

HarmSeverity

typeHarmSeverityint32

HarmSeverity specifies harm severity levels.

HarmSeverityUnspecified, HarmSeverityNegligible, HarmSeverityLow, HarmSeverityMedium, HarmSeverityHigh

const(// HarmSeverityUnspecified means harm severity unspecified.HarmSeverityUnspecifiedHarmSeverity=0// HarmSeverityNegligible means negligible level of harm severity.HarmSeverityNegligibleHarmSeverity=1// HarmSeverityLow means low level of harm severity.HarmSeverityLowHarmSeverity=2// HarmSeverityMedium means medium level of harm severity.HarmSeverityMediumHarmSeverity=3// HarmSeverityHigh means high level of harm severity.HarmSeverityHighHarmSeverity=4)

func (HarmSeverity) String

func(vHarmSeverity)String()string

Part

typePartinterface{// contains filtered or unexported methods}

A Part is either a Text, a Blob, or a FileData.

PromptFeedback

typePromptFeedbackstruct{// Output only. Blocked reason.BlockReasonBlockedReason// Output only. Safety ratings.SafetyRatings[]*SafetyRating// Output only. A readable block reason message.BlockReasonMessagestring}

PromptFeedback contains content filter results for a prompt sent in the request.

SafetyRating

typeSafetyRatingstruct{// Output only. Harm category.CategoryHarmCategory// Output only. Harm probability levels in the content.ProbabilityHarmProbability// Output only. Harm probability score.ProbabilityScorefloat32// Output only. Harm severity levels in the content.SeverityHarmSeverity// Output only. Harm severity score.SeverityScorefloat32// Output only. Indicates whether the content was filtered out because of this// rating.Blockedbool}

SafetyRating is the safety rating corresponding to the generated content.

SafetySetting

typeSafetySettingstruct{// Required. Harm category.CategoryHarmCategory// Required. The harm block threshold.ThresholdHarmBlockThreshold// Optional. Specify if the threshold is used for probability or severity// score. If not specified, the threshold is used for probability score.MethodHarmBlockMethod}

SafetySetting is safety settings.

Schema

typeSchemastruct{// Optional. The type of the data.TypeType// Optional. The format of the data.// Supported formats:////for NUMBER type: "float", "double"//for INTEGER type: "int32", "int64"//for STRING type: "email", "byte", etcFormatstring// Optional. The title of the Schema.Titlestring// Optional. The description of the data.Descriptionstring// Optional. Indicates if the value may be null.Nullablebool// Optional. SCHEMA FIELDS FOR TYPE ARRAY// Schema of the elements of Type.ARRAY.Items*Schema// Optional. Minimum number of the elements for Type.ARRAY.MinItemsint64// Optional. Maximum number of the elements for Type.ARRAY.MaxItemsint64// Optional. Possible values of the element of Type.STRING with enum format.// For example we can define an Enum Direction as :// {type:STRING, format:enum, enum:["EAST", NORTH", "SOUTH", "WEST"]}Enum[]string// Optional. SCHEMA FIELDS FOR TYPE OBJECT// Properties of Type.OBJECT.Propertiesmap[string]*Schema// Optional. Required properties of Type.OBJECT.Required[]string// Optional. Minimum number of the properties for Type.OBJECT.MinPropertiesint64// Optional. Maximum number of the properties for Type.OBJECT.MaxPropertiesint64// Optional. SCHEMA FIELDS FOR TYPE INTEGER and NUMBER// Minimum value of the Type.INTEGER and Type.NUMBERMinimumfloat64// Optional. Maximum value of the Type.INTEGER and Type.NUMBERMaximumfloat64// Optional. SCHEMA FIELDS FOR TYPE STRING// Minimum length of the Type.STRINGMinLengthint64// Optional. Maximum length of the Type.STRINGMaxLengthint64// Optional. Pattern of the Type.STRING to restrict a string to a regular// expression.Patternstring}

Schema is used to define the format of input/output data. Represents a selectsubset of anOpenAPI 3.0 schemaobject. More fields maybe added in the future as needed.

Text

typeTextstring

A Text is a piece of text, like a question or phrase.

ThinkingConfig

typeThinkingConfigstruct{// Indicates whether to include thoughts in the response.// If true, thoughts are returned only when available.IncludeThoughts*bool// Optional. Indicates the thinking budget in tokens.// This is only applied when enable_thinking is true.ThinkingBudget*int32}

ThinkingConfig is config for thinking features.

ThoughtSignature

typeThoughtSignature[]byte

ThoughtSignature is an opaque signature for the thoughtso it can be reused in subsequent requests.

Tool

typeToolstruct{// Optional. Function tool type.// One or more function declarations to be passed to the model along with the// current user query. Model may decide to call a subset of these functions// by populating// [FunctionCall][google.cloud.aiplatform.v1beta1.Part.function_call] in the// response. User should provide a// [FunctionResponse][google.cloud.aiplatform.v1beta1.Part.function_response]// for each function call in the next turn. Based on the function responses,// Model will generate the final response back to the user.// Maximum 128 function declarations can be provided.FunctionDeclarations[]*FunctionDeclaration}

Tool details that the model may use to generate response.

ATool is a piece of code that enables the system to interact withexternal systems to perform an action, or set of actions, outside ofknowledge and scope of the model. A Tool object should contain exactlyone type of Tool (e.g FunctionDeclaration, Retrieval orGoogleSearchRetrieval).

Example

packagemainimport("context""fmt""log""cloud.google.com/go/vertexai/genai")// Your GCP projectconstprojectID="your-project"// A GCP location like "us-central1"; if you're using standard Google-published// models (like untuned Gemini models), you can keep location blank ("").constlocation="some-gcp-location"funcmain(){ctx:=context.Background()client,err:=genai.NewClient(ctx,projectID,location)iferr!=nil{log.Fatal(err)}deferclient.Close()currentWeather:=func(citystring)string{switchcity{case"New York, NY":return"cold"case"Miami, FL":return"warm"default:return"unknown"}}// To use functions / tools, we have to first define a schema that describes// the function to the model. The schema is similar to OpenAPI 3.0.//// In this example, we create a single function that provides the model with// a weather forecast in a given location.schema:=&genai.Schema{Type:genai.TypeObject,Properties:map[string]*genai.Schema{"location":{Type:genai.TypeString,Description:"The city and state, e.g. San Francisco, CA",},"unit":{Type:genai.TypeString,Enum:[]string{"celsius","fahrenheit"},},},Required:[]string{"location"},}weatherTool:=&genai.Tool{FunctionDeclarations:[]*genai.FunctionDeclaration{{Name:        "CurrentWeather",Description: "Get the current weather in a given location",Parameters:  schema,}},}model:=client.GenerativeModel("gemini-1.0-pro")// Before initiating a conversation, we tell the model which tools it has// at its disposal.model.Tools=[]*genai.Tool{weatherTool}// For using tools, the chat mode is useful because it provides the required// chat context. A model needs to have tools supplied to it in the chat// history so it can use them in subsequent conversations.//// The flow of message expected here is://// 1. We send a question to the model// 2. The model recognizes that it needs to use a tool to answer the question,//    an returns a FunctionCall response asking to use the CurrentWeather//    tool.// 3. We send a FunctionResponse message, simulating the return value of//    CurrentWeather for the model's query.// 4. The model provides its text answer in response to this message.session:=model.StartChat()res,err:=session.SendMessage(ctx,genai.Text("What is the weather like in New York?"))iferr!=nil{log.Fatal(err)}part:=res.Candidates[0].Content.Parts[0]funcall,ok:=part.(genai.FunctionCall)if!ok{log.Fatalf("expected FunctionCall: %v",part)}iffuncall.Name!="CurrentWeather"{log.Fatalf("expected CurrentWeather: %v",funcall.Name)}// Expect the model to pass a proper string "location" argument to the tool.locArg,ok:=funcall.Args["location"].(string)if!ok{log.Fatalf("expected string: %v",funcall.Args["location"])}weatherData:=currentWeather(locArg)res,err=session.SendMessage(ctx,genai.FunctionResponse{Name:weatherTool.FunctionDeclarations[0].Name,Response:map[string]any{"weather":weatherData,},})iferr!=nil{log.Fatal(err)}printResponse(res)}funcprintResponse(resp*genai.GenerateContentResponse){for_,cand:=rangeresp.Candidates{for_,part:=rangecand.Content.Parts{fmt.Println(part)}}fmt.Println("---")}

ToolConfig

typeToolConfigstruct{// Optional. Function calling config.FunctionCallingConfig*FunctionCallingConfig}

ToolConfig configures tools.

Type

typeTypeint32

Type contains the list of OpenAPI data types as defined byhttps://swagger.io/docs/specification/data-models/data-types/

TypeUnspecified, TypeString, TypeNumber, TypeInteger, TypeBoolean, TypeArray, TypeObject

const(// TypeUnspecified means not specified, should not be used.TypeUnspecifiedType=0// TypeString means openAPI string typeTypeStringType=1// TypeNumber means openAPI number typeTypeNumberType=2// TypeInteger means openAPI integer typeTypeIntegerType=3// TypeBoolean means openAPI boolean typeTypeBooleanType=4// TypeArray means openAPI array typeTypeArrayType=5// TypeObject means openAPI object typeTypeObjectType=6)

func (Type) String

func(vType)String()string

UsageMetadata

typeUsageMetadatastruct{// Number of tokens in the request. When `cached_content` is set, this is// still the total effective prompt size meaning this includes the number of// tokens in the cached content.PromptTokenCountint32// Number of tokens in the response(s).CandidatesTokenCountint32// Output only. Number of tokens present in thoughts output.ThoughtsTokenCountint32// Total token count for prompt and response candidates.TotalTokenCountint32}

UsageMetadata is usage metadata about response(s).

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-30 UTC.