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
)
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.
CountTokensRequest
type
CountTokensRequest
struct
{
// Required. The name of the Endpoint requested to perform token counting.
// Format:
// `projects/{project}/locations/{location}/endpoints/{endpoint}`
Endpoint
string
// Required. The name of the publisher model requested to serve the
// prediction. Format:
// `projects/{project}/locations/{location}/publishers/*/models/*`
Model
string
// Required. Input content.
Contents
[]
*
Content
}
CountTokensRequest is request message for [PredictionService.CountTokens][google.cloud.aiplatform.v1beta1.PredictionService.CountTokens].
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
(
i
FinishReason
)
String
()
string
GenerateContentResponse
type
GenerateContentResponse
struct
{
Candidates
[]
*
Candidate
PromptFeedback
*
PromptFeedback
}
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
// 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.
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
)
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
)
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
)
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.
Text
type
Text
string
A Text is a piece of text, like a question or phrase.

