Package genai is a client for the generative VertexAI model.
Blob
type
Blob
struct
{
// Required. The IANA standard MIME type of the source data.
MIMEType
string
// Required. Raw bytes for media formats.
Data
[]
byte
}
Blob contains raw media bytes.
Text should not be sent as raw bytes, use the 'text' field.
func ImageData
ImageData is a convenience function for creating an image Blob 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
type
BlockedError
struct
{
// 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
type
BlockedReason
int32
BlockedReason is blocked reason enumeration.
BlockedReasonUnspecified, BlockedReasonSafety, BlockedReasonOther
const
(
// BlockedReasonUnspecified means unspecified blocked reason.
BlockedReasonUnspecified
BlockedReason
=
0
// BlockedReasonSafety means candidates blocked due to safety.
BlockedReasonSafety
BlockedReason
=
1
// BlockedReasonOther means candidates blocked due to other reason.
BlockedReasonOther
BlockedReason
=
2
)
func (BlockedReason) String
func
(
v
BlockedReason
)
String
()
string
Candidate
type
Candidate
struct
{
// Output only. Index of the candidate.
Index
int32
// 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.
FinishReason
FinishReason
// 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.
FinishMessage
string
// Output only. Source attribution of the generated content.
CitationMetadata
*
CitationMetadata
}
Candidate is a response candidate generated from the model.
ChatSession
type
ChatSession
struct
{
History
[]
*
Content
// contains filtered or unexported fields
}
A ChatSession provides interactive chat.
Example
package
main
import
(
"context"
"fmt"
"log"
"cloud.google.com/go/vertexai/genai"
"google.golang.org/api/iterator"
)
const
projectID
=
"your-project"
const
model
=
"some-model"
const
location
=
"some-location"
func
main
()
{
ctx
:=
context
.
Background
()
client
,
err
:=
genai
.
NewClient
(
ctx
,
projectID
,
location
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
client
.
Close
()
model
:=
client
.
GenerativeModel
(
model
)
cs
:=
model
.
StartChat
()
send
:=
func
(
msg
string
)
*
genai
.
GenerateContentResponse
{
fmt
.
Printf
(
"== Me: %s\n== Model:\n"
,
msg
)
res
,
err
:=
cs
.
SendMessage
(
ctx
,
genai
.
Text
(
msg
))
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
return
res
}
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
()
if
err
==
iterator
.
Done
{
break
}
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
printResponse
(
res
)
}
for
i
,
c
:=
range
cs
.
History
{
log
.
Printf
(
" %d: %+v"
,
i
,
c
)
}
res
=
send
(
"Why do you like the Philips?"
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
printResponse
(
res
)
}
func
printResponse
(
resp
*
genai
.
GenerateContentResponse
)
{
for
_
,
cand
:=
range
resp
.
Candidates
{
for
_
,
part
:=
range
cand
.
Content
.
Parts
{
fmt
.
Println
(
part
)
}
}
fmt
.
Println
(
"---"
)
}
func (*ChatSession) SendMessage
func
(
cs
*
ChatSession
)
SendMessage
(
ctx
context
.
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
(
ctx
context
.
Context
,
parts
...
Part
)
*
GenerateContentResponseIterator
SendMessageStream is like SendMessage, but with a streaming request.
Citation
type
Citation
struct
{
// Output only. Start index into the content.
StartIndex
int32
// Output only. End index into the content.
EndIndex
int32
// Output only. Url reference of the attribution.
URI
string
// Output only. Title of the attribution.
Title
string
// Output only. License of the attribution.
License
string
// Output only. Publication date of the attribution.
PublicationDate
civil
.
Date
}
Citation contains source attributions for content.
CitationMetadata
type
CitationMetadata
struct
{
// Output only. List of citations.
Citations
[]
*
Citation
}
CitationMetadata is a collection of source attributions for a piece of content.
Client
type
Client
struct
{
// contains filtered or unexported fields
}
A Client is a Google Vertex AI client.
func NewClient
func
NewClient
(
ctx
context
.
Context
,
projectID
,
location
string
,
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 Client are safe for concurrent use by multiple goroutines.
You may configure the client by passing in options from the [google.golang.org/api/option] package.
func (*Client) Close
Close closes the client.
func (*Client) GenerativeModel
func
(
c
*
Client
)
GenerativeModel
(
name
string
)
*
GenerativeModel
GenerativeModel creates a new instance of the named model.
Content
type
Content
struct
{
// 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.
Role
string
// 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.
A Content
includes a role
field designating the producer of the Content
and a parts
field containing multi-part data that contains the content of
the message turn.
CountTokensResponse
type
CountTokensResponse
struct
{
// The total number of tokens counted across all instances from the request.
TotalTokens
int32
// The total number of billable characters counted across all instances from
// the request.
TotalBillableCharacters
int32
}
CountTokensResponse is response message for [PredictionService.CountTokens][google.cloud.aiplatform.v1beta1.PredictionService.CountTokens].
FileData
type
FileData
struct
{
// Required. The IANA standard MIME type of the source data.
MIMEType
string
// Required. URI.
FileURI
string
}
FileData is URI based data.
FinishReason
type
FinishReason
int32
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
const
(
// FinishReasonUnspecified means the finish reason is unspecified.
FinishReasonUnspecified
FinishReason
=
0
// FinishReasonStop means natural stop point of the model or provided stop sequence.
FinishReasonStop
FinishReason
=
1
// FinishReasonMaxTokens means the maximum number of tokens as specified in the request was reached.
FinishReasonMaxTokens
FinishReason
=
2
// FinishReasonSafety means the token generation was stopped as the response was flagged for safety
// reasons. NOTE: When streaming the Candidate.content will be empty if
// content filters blocked the output.
FinishReasonSafety
FinishReason
=
3
// FinishReasonRecitation means the token generation was stopped as the response was flagged for
// unauthorized citations.
FinishReasonRecitation
FinishReason
=
4
// FinishReasonOther means all other reasons that stopped the token generation
FinishReasonOther
FinishReason
=
5
)
func (FinishReason) String
func
(
v
FinishReason
)
String
()
string
FunctionCall
type
FunctionCall
struct
{
// Required. The name of the function to call.
// Matches [FunctionDeclaration.name].
Name
string
// Optional. Required. The function parameters and values in JSON object
// format. See [FunctionDeclaration.parameters] for parameter details.
Args
map
[
string
]
any
}
FunctionCall is a predicted [FunctionCall] returned from the model that contains a string representing the [FunctionDeclaration.name] and a structured JSON object containing the parameters and their values.
FunctionDeclaration
type
FunctionDeclaration
struct
{
// 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 and dashes, with a maximum
// length of 64.
Name
string
// Optional. Description and purpose of the function.
// Model uses it to decide how and whether to call the function.
Description
string
// 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. Example with 1 required and 1 optional
// parameter: type: OBJECT properties:
// param1:
// type: STRING
// param2:
// type: INTEGER
// required:
// - param1
Parameters
*
Schema
}
FunctionDeclaration is structured representation of a function declaration as defined by the OpenAPI 3.0 specification
. Included
in this declaration are the function name and parameters. This
FunctionDeclaration is a representation of a block of code that can be used
as a Tool
by the model and executed by the client.
FunctionResponse
type
FunctionResponse
struct
{
// Required. The name of the function to call.
// Matches [FunctionDeclaration.name] and [FunctionCall.name].
Name
string
// Required. The function response in JSON object format.
Response
map
[
string
]
any
}
FunctionResponse is the result output from a [FunctionCall] that contains a string representing the [FunctionDeclaration.name] and a structured JSON object containing any output from the function is used as context to the model. This should contain the result of a [FunctionCall] made based on model prediction.
GenerateContentResponse
type
GenerateContentResponse
struct
{
// 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
type
GenerateContentResponseIterator
struct
{
// contains filtered or unexported fields
}
GenerateContentResponseIterator is an iterator over GnerateContentResponse.
func (*GenerateContentResponseIterator) Next
func
(
iter
*
GenerateContentResponseIterator
)
Next
()
(
*
GenerateContentResponse
,
error
)
Next returns the next response.
GenerationConfig
type
GenerationConfig
struct
{
// 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
float32
// 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
}
GenerationConfig is generation config.
GenerativeModel
type
GenerativeModel
struct
{
GenerationConfig
SafetySettings
[]
*
SafetySetting
Tools
[]
*
Tool
// contains filtered or unexported fields
}
GenerativeModel is a model that can generate text. Create one with [Client.GenerativeModel], then configure it by setting the exported fields.
The model holds all the config for a GenerateContentRequest, so the GenerateContent method can use a vararg for the content.
func (*GenerativeModel) CountTokens
func
(
m
*
GenerativeModel
)
CountTokens
(
ctx
context
.
Context
,
parts
...
Part
)
(
*
CountTokensResponse
,
error
)
CountTokens counts the number of tokens in the content.
Example
package
main
import
(
"context"
"fmt"
"log"
"cloud.google.com/go/vertexai/genai"
)
const
projectID
=
"your-project"
const
model
=
"some-model"
const
location
=
"some-location"
func
main
()
{
ctx
:=
context
.
Background
()
client
,
err
:=
genai
.
NewClient
(
ctx
,
projectID
,
location
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
client
.
Close
()
model
:=
client
.
GenerativeModel
(
model
)
resp
,
err
:=
model
.
CountTokens
(
ctx
,
genai
.
Text
(
"What kind of fish is this?"
))
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
fmt
.
Println
(
"Num tokens:"
,
resp
.
TotalTokens
)
}
func (*GenerativeModel) GenerateContent
func
(
m
*
GenerativeModel
)
GenerateContent
(
ctx
context
.
Context
,
parts
...
Part
)
(
*
GenerateContentResponse
,
error
)
GenerateContent produces a single request and response.
Example
package
main
import
(
"context"
"fmt"
"log"
"cloud.google.com/go/vertexai/genai"
)
const
projectID
=
"your-project"
const
model
=
"some-model"
const
location
=
"some-location"
func
main
()
{
ctx
:=
context
.
Background
()
client
,
err
:=
genai
.
NewClient
(
ctx
,
projectID
,
location
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
client
.
Close
()
model
:=
client
.
GenerativeModel
(
model
)
model
.
Temperature
=
0.9
resp
,
err
:=
model
.
GenerateContent
(
ctx
,
genai
.
Text
(
"What is the average size of a swallow?"
))
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
printResponse
(
resp
)
}
func
printResponse
(
resp
*
genai
.
GenerateContentResponse
)
{
for
_
,
cand
:=
range
resp
.
Candidates
{
for
_
,
part
:=
range
cand
.
Content
.
Parts
{
fmt
.
Println
(
part
)
}
}
fmt
.
Println
(
"---"
)
}
func (*GenerativeModel) GenerateContentStream
func
(
m
*
GenerativeModel
)
GenerateContentStream
(
ctx
context
.
Context
,
parts
...
Part
)
*
GenerateContentResponseIterator
GenerateContentStream returns an iterator that enumerates responses.
Example
package
main
import
(
"context"
"fmt"
"log"
"cloud.google.com/go/vertexai/genai"
"google.golang.org/api/iterator"
)
const
projectID
=
"your-project"
const
model
=
"some-model"
const
location
=
"some-location"
func
main
()
{
ctx
:=
context
.
Background
()
client
,
err
:=
genai
.
NewClient
(
ctx
,
projectID
,
location
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
defer
client
.
Close
()
model
:=
client
.
GenerativeModel
(
model
)
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
()
if
err
==
iterator
.
Done
{
break
}
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
printResponse
(
resp
)
}
}
func
printResponse
(
resp
*
genai
.
GenerateContentResponse
)
{
for
_
,
cand
:=
range
resp
.
Candidates
{
for
_
,
part
:=
range
cand
.
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.
HarmBlockThreshold
type
HarmBlockThreshold
int32
HarmBlockThreshold specifies probability based thresholds levels for blocking.
HarmBlockUnspecified, HarmBlockLowAndAbove, HarmBlockMediumAndAbove, HarmBlockOnlyHigh, HarmBlockNone
const
(
// HarmBlockUnspecified means unspecified harm block threshold.
HarmBlockUnspecified
HarmBlockThreshold
=
0
// HarmBlockLowAndAbove means block low threshold and above (i.e. block more).
HarmBlockLowAndAbove
HarmBlockThreshold
=
1
// HarmBlockMediumAndAbove means block medium threshold and above.
HarmBlockMediumAndAbove
HarmBlockThreshold
=
2
// HarmBlockOnlyHigh means block only high threshold (i.e. block less).
HarmBlockOnlyHigh
HarmBlockThreshold
=
3
// HarmBlockNone means block none.
HarmBlockNone
HarmBlockThreshold
=
4
)
func (HarmBlockThreshold) String
func
(
v
HarmBlockThreshold
)
String
()
string
HarmCategory
type
HarmCategory
int32
HarmCategory specifies harm categories that will block the content.
HarmCategoryUnspecified, HarmCategoryHateSpeech, HarmCategoryDangerousContent, HarmCategoryHarassment, HarmCategorySexuallyExplicit
const
(
// HarmCategoryUnspecified means the harm category is unspecified.
HarmCategoryUnspecified
HarmCategory
=
0
// HarmCategoryHateSpeech means the harm category is hate speech.
HarmCategoryHateSpeech
HarmCategory
=
1
// HarmCategoryDangerousContent means the harm category is dangerous content.
HarmCategoryDangerousContent
HarmCategory
=
2
// HarmCategoryHarassment means the harm category is harassment.
HarmCategoryHarassment
HarmCategory
=
3
// HarmCategorySexuallyExplicit means the harm category is sexually explicit content.
HarmCategorySexuallyExplicit
HarmCategory
=
4
)
func (HarmCategory) String
func
(
v
HarmCategory
)
String
()
string
HarmProbability
type
HarmProbability
int32
HarmProbability specifies harm probability levels in the content.
HarmProbabilityUnspecified, HarmProbabilityNegligible, HarmProbabilityLow, HarmProbabilityMedium, HarmProbabilityHigh
const
(
// HarmProbabilityUnspecified means harm probability unspecified.
HarmProbabilityUnspecified
HarmProbability
=
0
// HarmProbabilityNegligible means negligible level of harm.
HarmProbabilityNegligible
HarmProbability
=
1
// HarmProbabilityLow means low level of harm.
HarmProbabilityLow
HarmProbability
=
2
// HarmProbabilityMedium means medium level of harm.
HarmProbabilityMedium
HarmProbability
=
3
// HarmProbabilityHigh means high level of harm.
HarmProbabilityHigh
HarmProbability
=
4
)
func (HarmProbability) String
func
(
v
HarmProbability
)
String
()
string
Part
type
Part
interface
{
// contains filtered or unexported methods
}
A Part is either a Text, a Blob, or a FileData.
PromptFeedback
type
PromptFeedback
struct
{
// Output only. Blocked reason.
BlockReason
BlockedReason
// Output only. Safety ratings.
SafetyRatings
[]
*
SafetyRating
// Output only. A readable block reason message.
BlockReasonMessage
string
}
PromptFeedback contains content filter results for a prompt sent in the request.
SafetyRating
type
SafetyRating
struct
{
// Output only. Harm category.
Category
HarmCategory
// Output only. Harm probability levels in the content.
Probability
HarmProbability
// Output only. Indicates whether the content was filtered out because of this
// rating.
Blocked
bool
}
SafetyRating is the safety rating corresponding to the generated content.
SafetySetting
type
SafetySetting
struct
{
// Required. Harm category.
Category
HarmCategory
// Required. The harm block threshold.
Threshold
HarmBlockThreshold
}
SafetySetting is safety settings.
Schema
type
Schema
struct
{
// Optional. The type of the data.
Type
Type
// Optional. The format of the data.
// Supported formats:
// for NUMBER type: float, double
// for INTEGER type: int32, int64
Format
string
// Optional. The description of the data.
Description
string
// Optional. Indicates if the value may be null.
Nullable
bool
// Optional. Schema of the elements of Type.ARRAY.
Items
*
Schema
// 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. Properties of Type.OBJECT.
Properties
map
[
string
]
*
Schema
// Optional. Required properties of Type.OBJECT.
Required
[]
string
}
Schema is used to define the format of input/output data. Represents a select subset of an OpenAPI 3.0 schema object . More fields may be added in the future as needed.
Text
type
Text
string
A Text is a piece of text, like a question or phrase.
Tool
type
Tool
struct
{
// Optional. 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][content.part.function_call] in the
// response. User should provide a
// [FunctionResponse][content.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 64 function declarations can be
// provided.
FunctionDeclarations
[]
*
FunctionDeclaration
}
Tool details that the model may use to generate response.
A Tool
is a piece of code that enables the system to interact with
external systems to perform an action, or set of actions, outside of
knowledge and scope of the model.
Type
type
Type
int32
Type contains the list of OpenAPI data types as defined by https://swagger.io/docs/specification/data-models/data-types/
TypeUnspecified, TypeString, TypeNumber, TypeInteger, TypeBoolean, TypeArray, TypeObject
const
(
// TypeUnspecified means not specified, should not be used.
TypeUnspecified
Type
=
0
// TypeString means openAPI string type
TypeString
Type
=
1
// TypeNumber means openAPI number type
TypeNumber
Type
=
2
// TypeInteger means openAPI integer type
TypeInteger
Type
=
3
// TypeBoolean means openAPI boolean type
TypeBoolean
Type
=
4
// TypeArray means openAPI array type
TypeArray
Type
=
5
// TypeObject means openAPI object type
TypeObject
Type
=
6
)
func (Type) String
UsageMetadata
type
UsageMetadata
struct
{
// Number of tokens in the request.
PromptTokenCount
int32
// Number of tokens in the response(s).
CandidatesTokenCount
int32
TotalTokenCount
int32
}
UsageMetadata is usage metadata about response(s).