Package generativelanguage is an auto-generated package for the Generative Language API.
The PaLM API allows developers to build generative AI applications using the PaLM model. Large Language Models (LLMs) are a powerful, versatile type of machine learning model that enables computers to comprehend and generate natural language through a series of prompts. The PaLM API is based on Google’s next generation LLM, PaLM. It excels at a variety of different tasks like code generation, reasoning, and writing. You can use the PaLM API to build generative AI applications for use cases like 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.
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 . NewDiscussClient ( 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.
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 . NewDiscussClient ( ctx ) if err != nil { // TODO: Handle error. } defer c . Close () req := & generativelanguagepb . GenerateMessageRequest { // TODO: Fill request struct fields. // See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1beta2/generativelanguagepb#GenerateMessageRequest. } resp , err := c . GenerateMessage ( ctx , req ) if err != nil { // TODO: Handle error. } // TODO: Use resp. _ = resp
Use of Context
The ctx passed to NewDiscussClient 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.
DiscussCallOptions
type
DiscussCallOptions
struct
{
GenerateMessage
[]
gax
.
CallOption
CountMessageTokens
[]
gax
.
CallOption
}
DiscussCallOptions contains the retry settings for each method of DiscussClient.
DiscussClient
type
DiscussClient
struct
{
// The call options for this service.
CallOptions
*
DiscussCallOptions
// contains filtered or unexported fields
}
DiscussClient 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.
An API for using Generative Language Models (GLMs) in dialog applications.
Also known as large language models (LLMs), this API provides models that are trained for multi-turn dialog.
func NewDiscussClient
func
NewDiscussClient
(
ctx
context
.
Context
,
opts
...
option
.
ClientOption
)
(
*
DiscussClient
,
error
)
NewDiscussClient creates a new discuss service client based on gRPC. The returned client must be Closed when it is done being used to clean up its underlying connections.
An API for using Generative Language Models (GLMs) in dialog applications.
Also known as large language models (LLMs), this API provides models that are trained for multi-turn dialog.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1beta2"
)
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
.
NewDiscussClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
// TODO: Use client.
_
=
c
}
func NewDiscussRESTClient
func
NewDiscussRESTClient
(
ctx
context
.
Context
,
opts
...
option
.
ClientOption
)
(
*
DiscussClient
,
error
)
NewDiscussRESTClient creates a new discuss service rest client.
An API for using Generative Language Models (GLMs) in dialog applications.
Also known as large language models (LLMs), this API provides models that are trained for multi-turn dialog.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1beta2"
)
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
.
NewDiscussRESTClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
// TODO: Use client.
_
=
c
}
func (*DiscussClient) Close
func
(
c
*
DiscussClient
)
Close
()
error
Close closes the connection to the API service. The user should invoke this when the client is no longer required.
func (*DiscussClient) Connection (deprecated)
func
(
c
*
DiscussClient
)
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 (*DiscussClient) CountMessageTokens
func
(
c
*
DiscussClient
)
CountMessageTokens
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
CountMessageTokensRequest
,
opts
...
gax
.
CallOption
)
(
*
generativelanguagepb
.
CountMessageTokensResponse
,
error
)
CountMessageTokens runs a model’s tokenizer on a string and returns the token count.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1beta2"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1beta2/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
.
NewDiscussClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
CountMessageTokensRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1beta2/generativelanguagepb#CountMessageTokensRequest.
}
resp
,
err
:=
c
.
CountMessageTokens
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}
func (*DiscussClient) GenerateMessage
func
(
c
*
DiscussClient
)
GenerateMessage
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
GenerateMessageRequest
,
opts
...
gax
.
CallOption
)
(
*
generativelanguagepb
.
GenerateMessageResponse
,
error
)
GenerateMessage generates a response from the model given an input MessagePrompt.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1beta2"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1beta2/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
.
NewDiscussClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
GenerateMessageRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1beta2/generativelanguagepb#GenerateMessageRequest.
}
resp
,
err
:=
c
.
GenerateMessage
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}
ModelCallOptions
type
ModelCallOptions
struct
{
GetModel
[]
gax
.
CallOption
ListModels
[]
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/apiv1beta2"
)
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/apiv1beta2"
)
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) 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.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1beta2"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1beta2/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/apiv1beta2/generativelanguagepb#GetModelRequest.
}
resp
,
err
:=
c
.
GetModel
(
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 models available through the API.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1beta2"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1beta2/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/apiv1beta2/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
}
}
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) 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.
TextCallOptions
type
TextCallOptions
struct
{
GenerateText
[]
gax
.
CallOption
EmbedText
[]
gax
.
CallOption
}
TextCallOptions contains the retry settings for each method of TextClient.
TextClient
type
TextClient
struct
{
// The call options for this service.
CallOptions
*
TextCallOptions
// contains filtered or unexported fields
}
TextClient 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 Generative Language Models (GLMs) trained to generate text.
Also known as Large Language Models (LLM)s, these generate text given an input prompt from the user.
func NewTextClient
func
NewTextClient
(
ctx
context
.
Context
,
opts
...
option
.
ClientOption
)
(
*
TextClient
,
error
)
NewTextClient creates a new text 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 Generative Language Models (GLMs) trained to generate text.
Also known as Large Language Models (LLM)s, these generate text given an input prompt from the user.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1beta2"
)
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
.
NewTextClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
// TODO: Use client.
_
=
c
}
func NewTextRESTClient
func
NewTextRESTClient
(
ctx
context
.
Context
,
opts
...
option
.
ClientOption
)
(
*
TextClient
,
error
)
NewTextRESTClient creates a new text service rest client.
API for using Generative Language Models (GLMs) trained to generate text.
Also known as Large Language Models (LLM)s, these generate text given an input prompt from the user.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1beta2"
)
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
.
NewTextRESTClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
// TODO: Use client.
_
=
c
}
func (*TextClient) Close
func
(
c
*
TextClient
)
Close
()
error
Close closes the connection to the API service. The user should invoke this when the client is no longer required.
func (*TextClient) Connection (deprecated)
func
(
c
*
TextClient
)
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 (*TextClient) EmbedText
func
(
c
*
TextClient
)
EmbedText
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
EmbedTextRequest
,
opts
...
gax
.
CallOption
)
(
*
generativelanguagepb
.
EmbedTextResponse
,
error
)
EmbedText generates an embedding from the model given an input message.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1beta2"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1beta2/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
.
NewTextClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
EmbedTextRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1beta2/generativelanguagepb#EmbedTextRequest.
}
resp
,
err
:=
c
.
EmbedText
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}
func (*TextClient) GenerateText
func
(
c
*
TextClient
)
GenerateText
(
ctx
context
.
Context
,
req
*
generativelanguagepb
.
GenerateTextRequest
,
opts
...
gax
.
CallOption
)
(
*
generativelanguagepb
.
GenerateTextResponse
,
error
)
GenerateText generates a response from the model given an input message.
Example
package
main
import
(
"context"
generativelanguage
"cloud.google.com/go/ai/generativelanguage/apiv1beta2"
generativelanguagepb
"cloud.google.com/go/ai/generativelanguage/apiv1beta2/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
.
NewTextClient
(
ctx
)
if
err
!=
nil
{
// TODO: Handle error.
}
defer
c
.
Close
()
req
:=
& generativelanguagepb
.
GenerateTextRequest
{
// TODO: Fill request struct fields.
// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1beta2/generativelanguagepb#GenerateTextRequest.
}
resp
,
err
:=
c
.
GenerateText
(
ctx
,
req
)
if
err
!=
nil
{
// TODO: Handle error.
}
// TODO: Use resp.
_
=
resp
}