Package generativelanguage is an auto-generated package for the Generative Language API.
The Gemini API allows developers to build generative AI applications using Gemini models. Gemini is our most capable model, built from the ground up to be multimodal. It can generalize and seamlessly understand, operate across, and combine different types of information including language, images, audio, video, and code. You can use the Gemini API for use cases like reasoning across text and images, content generation, dialogue agents, summarization and classification systems, and more.
NOTE : This package is in beta . It is not stable , and may be subject to changes .
General documentation
For information that is relevant for all client libraries please reference https://pkg.go.dev/cloud.google.com/go#pkg-overview . Some information on this page includes:
- Authentication and Authorization
- Timeouts and Cancellation
- Testing against Client Libraries
- Debugging Client Libraries
- Inspecting errors
Example usage
To get started with this package, create a client.
// go get cloud.google.com/go/ai/generativelanguage/apiv1@latest ctx := context . Background () // This snippet has been automatically generated and should be regarded as a code template only. // It will require modifications to work: // - It may require correct/in-range values for request initialization. // - It may require specifying regional endpoints when creating the service client as shown in: // https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options c , err := generativelanguage . NewGenerativeClient ( ctx ) if err != nil { // TODO: Handle error. } defer c . Close ()
The client will use your default application credentials. Clients should be reused instead of created as needed. The methods of Client are safe for concurrent use by multiple goroutines. The returned client must be Closed when it is done being used.
Using the Client
The following is an example of making an API call with the newly created client, mentioned above.
req := & generativelanguagepb . BatchEmbedContentsRequest { // TODO: Fill request struct fields. // See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb#BatchEmbedContentsRequest. } resp , err := c . BatchEmbedContents ( ctx , req ) if err != nil { // TODO: Handle error. } // TODO: Use resp. _ = resp
Use of Context
The ctx passed to NewGenerativeClient is used for authentication requests and for creating the underlying connection, but is not used for subsequent calls. Individual methods on the client use the ctx given to them.
To close the open connection, use the Close() method.
Functions
func DefaultAuthScopes
func
DefaultAuthScopes
()
[]
string
DefaultAuthScopes reports the default set of authentication scopes to use with this package.
GenerativeCallOptions
type
GenerativeCallOptions
struct
{
GenerateContent
[]
gax
.
CallOption
StreamGenerateContent
[]
gax
.
CallOption
EmbedContent
[]
gax
.
CallOption
BatchEmbedContents
[]
gax
.
CallOption
CountTokens
[]
gax
.
CallOption
CancelOperation
[]
gax
.
CallOption
GetOperation
[]
gax
.
CallOption
ListOperations
[]
gax
.
CallOption
}
GenerativeCallOptions contains the retry settings for each method of GenerativeClient.
GenerativeClient
type
GenerativeClient
struct
{
// The call options for this service.
CallOptions
*
GenerativeCallOptions
// contains filtered or unexported fields
}
GenerativeClient is a client for interacting with Generative Language API. Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
API for using Large Models that generate multimodal content and have additional capabilities beyond text generation.
func NewGenerativeClient
func
NewGenerativeClient
(
ctx
context
.
Context
,
opts
...
option
.
ClientOption
)
(
*
GenerativeClient
,
error
)
NewGenerativeClient creates a new generative service client based on gRPC. The returned client must be Closed when it is done being used to clean up its underlying connections.
API for using Large Models that generate multimodal content and have additional capabilities beyond text generation.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewGenerativeClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
// TODO: Use client.
_
=
c
}
func NewGenerativeRESTClient
func
NewGenerativeRESTClient
(
ctx
context
.
Context
,
opts
...
option
.
ClientOption
)
(
*
GenerativeClient
,
error
)
NewGenerativeRESTClient creates a new generative service rest client.
API for using Large Models that generate multimodal content and have additional capabilities beyond text generation.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewGenerativeRESTClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
// TODO: Use client.
_
=
c
}
func (*GenerativeClient) BatchEmbedContents
func
(
c
*
GenerativeClient
)
BatchEmbedContents
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
BatchEmbedContentsRequest
,
opts
...
gax
.
CallOption
)
(
*
generativelanguagepb
.
BatchEmbedContentsResponse
,
error
)
BatchEmbedContents generates multiple embedding vectors from the input Content which consists of a batch of strings represented as EmbedContentRequest objects.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewGenerativeClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
BatchEmbedContentsRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb#BatchEmbedContentsRequest.
}
resp
,
err
:=
c
.
BatchEmbedContents
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}
func (*GenerativeClient) CancelOperation
func
(
c
*
GenerativeClient
)
CancelOperation
(
ctx
context
.
Context
,
req
*
longrunningpb
.
CancelOperationRequest
,
opts
...
gax
.
CallOption
)
error
CancelOperation is a utility method from google.longrunning.Operations.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
longrunningpb
"cloud.google.com/go/longrunning/autogen/longrunningpb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewGenerativeClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& longrunningpb
.
CancelOperationRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#CancelOperationRequest.
}
err
=
c
.
CancelOperation
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
}
func (*GenerativeClient) Close
func
(
c
*
GenerativeClient
)
Close
()
error
Close closes the connection to the API service. The user should invoke this when the client is no longer required.
func (*GenerativeClient) Connection (deprecated)
func
(
c
*
GenerativeClient
)
Connection
()
*
grpc
.
ClientConn
Connection returns a connection to the API service.
Deprecated: Connections are now pooled so this method does not always return the same resource.
func (*GenerativeClient) CountTokens
func
(
c
*
GenerativeClient
)
CountTokens
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
CountTokensRequest
,
opts
...
gax
.
CallOption
)
(
*
generativelanguagepb
.
CountTokensResponse
,
error
)
CountTokens runs a model’s tokenizer on input Content and returns the token count. Refer to the tokens guide (at https://ai.google.dev/gemini-api/docs/tokens ) to learn more about tokens.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewGenerativeClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
CountTokensRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb#CountTokensRequest.
}
resp
,
err
:=
c
.
CountTokens
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}
func (*GenerativeClient) EmbedContent
func
(
c
*
GenerativeClient
)
EmbedContent
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
EmbedContentRequest
,
opts
...
gax
.
CallOption
)
(
*
generativelanguagepb
.
EmbedContentResponse
,
error
)
EmbedContent generates a text embedding vector from the input Content using the specified Gemini Embedding model (at https://ai.google.dev/gemini-api/docs/models/gemini#text-embedding ).
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewGenerativeClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
EmbedContentRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb#EmbedContentRequest.
}
resp
,
err
:=
c
.
EmbedContent
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}
func (*GenerativeClient) GenerateContent
func
(
c
*
GenerativeClient
)
GenerateContent
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
GenerateContentRequest
,
opts
...
gax
.
CallOption
)
(
*
generativelanguagepb
.
GenerateContentResponse
,
error
)
GenerateContent generates a model response given an input GenerateContentRequest. Refer to the text generation guide (at https://ai.google.dev/gemini-api/docs/text-generation ) for detailed usage information. Input capabilities differ between models, including tuned models. Refer to the model guide (at https://ai.google.dev/gemini-api/docs/models/gemini ) and tuning guide (at https://ai.google.dev/gemini-api/docs/model-tuning ) for details.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewGenerativeClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
GenerateContentRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb#GenerateContentRequest.
}
resp
,
err
:=
c
.
GenerateContent
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}
func (*GenerativeClient) GetOperation
func
(
c
*
GenerativeClient
)
GetOperation
(
ctx
context
.
Context
,
req
*
longrunningpb
.
GetOperationRequest
,
opts
...
gax
.
CallOption
)
(
*
longrunningpb
.
Operation
,
error
)
GetOperation is a utility method from google.longrunning.Operations.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
longrunningpb
"cloud.google.com/go/longrunning/autogen/longrunningpb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewGenerativeClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& longrunningpb
.
GetOperationRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#GetOperationRequest.
}
resp
,
err
:=
c
.
GetOperation
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}
func (*GenerativeClient) ListOperations
func
(
c
*
GenerativeClient
)
ListOperations
(
ctx
context
.
Context
,
req
*
longrunningpb
.
ListOperationsRequest
,
opts
...
gax
.
CallOption
)
*
OperationIterator
ListOperations is a utility method from google.longrunning.Operations.
Examples
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
longrunningpb
"cloud.google.com/go/longrunning/autogen/longrunningpb"
"google.golang.org/api/iterator"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewGenerativeClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& longrunningpb
.
ListOperationsRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#ListOperationsRequest.
}
it
:=
c
.
ListOperations
(
ctx
,
req
)
for
{
resp
,
err
:=
it
.
Next
()
if
err
==
iterator
.
Done
{
break
}
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
// If you need to access the underlying RPC response,
// you can do so by casting the `Response` as below.
// Otherwise, remove this line. Only populated after
// first call to Next(). Not safe for concurrent access.
_
=
it
.
Response
.(
*
longrunningpb
.
ListOperationsResponse
)
}
}
all
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
longrunningpb
"cloud.google.com/go/longrunning/autogen/longrunningpb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewGenerativeClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& longrunningpb
.
ListOperationsRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#ListOperationsRequest.
}
for
resp
,
err
:=
range
c
.
ListOperations
(
ctx
,
req
).
All
()
{
if
err
!=
nil
{
// TODO: Handle error and break/return/continue. Iteration will stop after any error.
}
// TODO: Use resp.
_
=
resp
}
}
func (*GenerativeClient) SetGoogleClientInfo
func
(
c
*
GenerativeClient
)
SetGoogleClientInfo
(
keyval
string
)
func (*GenerativeClient) StreamGenerateContent
func
(
c
*
GenerativeClient
)
StreamGenerateContent
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
GenerateContentRequest
,
opts
...
gax
.
CallOption
)
(
generativelanguagepb
.
GenerativeService_StreamGenerateContentClient
,
error
)
StreamGenerateContent generates a streamed response (at https://ai.google.dev/gemini-api/docs/text-generation?lang=python#generate-a-text-stream ) from the model given an input GenerateContentRequest.
ModelCallOptions
type
ModelCallOptions
struct
{
GetModel
[]
gax
.
CallOption
ListModels
[]
gax
.
CallOption
CancelOperation
[]
gax
.
CallOption
GetOperation
[]
gax
.
CallOption
ListOperations
[]
gax
.
CallOption
}
ModelCallOptions contains the retry settings for each method of ModelClient.
ModelClient
type
ModelClient
struct
{
// The call options for this service.
CallOptions
*
ModelCallOptions
// contains filtered or unexported fields
}
ModelClient is a client for interacting with Generative Language API. Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
Provides methods for getting metadata information about Generative Models.
func NewModelClient
func
NewModelClient
(
ctx
context
.
Context
,
opts
...
option
.
ClientOption
)
(
*
ModelClient
,
error
)
NewModelClient creates a new model service client based on gRPC. The returned client must be Closed when it is done being used to clean up its underlying connections.
Provides methods for getting metadata information about Generative Models.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewModelClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
// TODO: Use client.
_
=
c
}
func NewModelRESTClient
func
NewModelRESTClient
(
ctx
context
.
Context
,
opts
...
option
.
ClientOption
)
(
*
ModelClient
,
error
)
NewModelRESTClient creates a new model service rest client.
Provides methods for getting metadata information about Generative Models.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewModelRESTClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
// TODO: Use client.
_
=
c
}
func (*ModelClient) CancelOperation
func
(
c
*
ModelClient
)
CancelOperation
(
ctx
context
.
Context
,
req
*
longrunningpb
.
CancelOperationRequest
,
opts
...
gax
.
CallOption
)
error
CancelOperation is a utility method from google.longrunning.Operations.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
longrunningpb
"cloud.google.com/go/longrunning/autogen/longrunningpb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewModelClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& longrunningpb
.
CancelOperationRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#CancelOperationRequest.
}
err
=
c
.
CancelOperation
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
}
func (*ModelClient) Close
func
(
c
*
ModelClient
)
Close
()
error
Close closes the connection to the API service. The user should invoke this when the client is no longer required.
func (*ModelClient) Connection (deprecated)
func
(
c
*
ModelClient
)
Connection
()
*
grpc
.
ClientConn
Connection returns a connection to the API service.
Deprecated: Connections are now pooled so this method does not always return the same resource.
func (*ModelClient) GetModel
func
(
c
*
ModelClient
)
GetModel
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
GetModelRequest
,
opts
...
gax
.
CallOption
)
(
*
generativelanguagepb
.
Model
,
error
)
GetModel gets information about a specific Model such as its version number, token limits, parameters (at https://ai.google.dev/gemini-api/docs/models/generative-models#model-parameters ) and other metadata. Refer to the Gemini models guide (at https://ai.google.dev/gemini-api/docs/models/gemini ) for detailed model information.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewModelClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
GetModelRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb#GetModelRequest.
}
resp
,
err
:=
c
.
GetModel
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}
func (*ModelClient) GetOperation
func
(
c
*
ModelClient
)
GetOperation
(
ctx
context
.
Context
,
req
*
longrunningpb
.
GetOperationRequest
,
opts
...
gax
.
CallOption
)
(
*
longrunningpb
.
Operation
,
error
)
GetOperation is a utility method from google.longrunning.Operations.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
longrunningpb
"cloud.google.com/go/longrunning/autogen/longrunningpb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewModelClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& longrunningpb
.
GetOperationRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#GetOperationRequest.
}
resp
,
err
:=
c
.
GetOperation
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}
func (*ModelClient) ListModels
func
(
c
*
ModelClient
)
ListModels
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
ListModelsRequest
,
opts
...
gax
.
CallOption
)
*
ModelIterator
ListModels lists the Models (at https://ai.google.dev/gemini-api/docs/models/gemini ) available through the Gemini API.
Examples
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb"
"google.golang.org/api/iterator"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewModelClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
ListModelsRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb#ListModelsRequest.
}
it
:=
c
.
ListModels
(
ctx
,
req
)
for
{
resp
,
err
:=
it
.
Next
()
if
err
==
iterator
.
Done
{
break
}
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
// If you need to access the underlying RPC response,
// you can do so by casting the `Response` as below.
// Otherwise, remove this line. Only populated after
// first call to Next(). Not safe for concurrent access.
_
=
it
.
Response
.(
*
generativelanguagepb
.
ListModelsResponse
)
}
}
all
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewModelClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
ListModelsRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1/generativelanguagepb#ListModelsRequest.
}
for
resp
,
err
:=
range
c
.
ListModels
(
ctx
,
req
).
All
()
{
if
err
!=
nil
{
// TODO: Handle error and break/return/continue. Iteration will stop after any error.
}
// TODO: Use resp.
_
=
resp
}
}
func (*ModelClient) ListOperations
func
(
c
*
ModelClient
)
ListOperations
(
ctx
context
.
Context
,
req
*
longrunningpb
.
ListOperationsRequest
,
opts
...
gax
.
CallOption
)
*
OperationIterator
ListOperations is a utility method from google.longrunning.Operations.
Examples
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
longrunningpb
"cloud.google.com/go/longrunning/autogen/longrunningpb"
"google.golang.org/api/iterator"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewModelClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& longrunningpb
.
ListOperationsRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#ListOperationsRequest.
}
it
:=
c
.
ListOperations
(
ctx
,
req
)
for
{
resp
,
err
:=
it
.
Next
()
if
err
==
iterator
.
Done
{
break
}
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
// If you need to access the underlying RPC response,
// you can do so by casting the `Response` as below.
// Otherwise, remove this line. Only populated after
// first call to Next(). Not safe for concurrent access.
_
=
it
.
Response
.(
*
longrunningpb
.
ListOperationsResponse
)
}
}
all
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1"
longrunningpb
"cloud.google.com/go/longrunning/autogen/longrunningpb"
)
func
main
()
{
ctx
:=
context
.
Background
()
// This snippet has been automatically generated and should be regarded as a code template only.
// It will require modifications to work:
// - It may require correct/in-range values for request initialization.
// - It may require specifying regional endpoints when creating the service client as shown in:
// https://pkg.go.dev/cloud.google.com/go#hdr-Client_Options
c
,
err
:=
generativelanguage
.
NewModelClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& longrunningpb
.
ListOperationsRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/longrunning/autogen/longrunningpb#ListOperationsRequest.
}
for
resp
,
err
:=
range
c
.
ListOperations
(
ctx
,
req
).
All
()
{
if
err
!=
nil
{
// TODO: Handle error and break/return/continue. Iteration will stop after any error.
}
// TODO: Use resp.
_
=
resp
}
}
ModelIterator
type
ModelIterator
struct
{
// Response is the raw response for the current page.
// It must be cast to the RPC response type.
// Calling Next() or InternalFetch() updates this value.
Response
interface
{}
// InternalFetch is for use by the Google Cloud Libraries only.
// It is not part of the stable interface of this package.
//
// InternalFetch returns results from a single call to the underlying RPC.
// The number of results is no greater than pageSize.
// If there are no more results, nextPageToken is empty and err is nil.
InternalFetch
func
(
pageSize
int
,
pageToken
string
)
(
results
[]
*
generativelanguagepb
.
Model
,
nextPageToken
string
,
err
error
)
// contains filtered or unexported fields
}
ModelIterator manages a stream of *generativelanguagepb.Model.
func (*ModelIterator) All
func
(
it
*
ModelIterator
)
All
()
iter
.
Seq2
[
*
generativelanguagepb
.
Model
,
error
]
All returns an iterator. If an error is returned by the iterator, the iterator will stop after that iteration.
func (*ModelIterator) Next
func
(
it
*
ModelIterator
)
Next
()
(
*
generativelanguagepb
.
Model
,
error
)
Next returns the next result. Its second return value is iterator.Done if there are no more results. Once Next returns Done, all subsequent calls will return Done.
func (*ModelIterator) PageInfo
func
(
it
*
ModelIterator
)
PageInfo
()
*
iterator
.
PageInfo
PageInfo supports pagination. See the [google.golang.org/api/iterator] package for details.
OperationIterator
type
OperationIterator
struct
{
// Response is the raw response for the current page.
// It must be cast to the RPC response type.
// Calling Next() or InternalFetch() updates this value.
Response
interface
{}
// InternalFetch is for use by the Google Cloud Libraries only.
// It is not part of the stable interface of this package.
//
// InternalFetch returns results from a single call to the underlying RPC.
// The number of results is no greater than pageSize.
// If there are no more results, nextPageToken is empty and err is nil.
InternalFetch
func
(
pageSize
int
,
pageToken
string
)
(
results
[]
*
longrunningpb
.
Operation
,
nextPageToken
string
,
err
error
)
// contains filtered or unexported fields
}
OperationIterator manages a stream of *longrunningpb.Operation.
func (*OperationIterator) All
func
(
it
*
OperationIterator
)
All
()
iter
.
Seq2
[
*
longrunningpb
.
Operation
,
error
]
All returns an iterator. If an error is returned by the iterator, the iterator will stop after that iteration.
func (*OperationIterator) Next
func
(
it
*
OperationIterator
)
Next
()
(
*
longrunningpb
.
Operation
,
error
)
Next returns the next result. Its second return value is iterator.Done if there are no more results. Once Next returns Done, all subsequent calls will return Done.
func (*OperationIterator) PageInfo
func
(
it
*
OperationIterator
)
PageInfo
()
*
iterator
.
PageInfo
PageInfo supports pagination. See the [google.golang.org/api/iterator] package for details.