generativelanguage

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2024 License: Apache-2.0 Imports: 23 Imported by: 1

Documentation

Overview

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:

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.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

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.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultAuthScopes

func DefaultAuthScopes() []string

DefaultAuthScopes reports the default set of authentication scopes to use with this package.

Types

type DiscussCallOptions

type DiscussCallOptions struct {
	GenerateMessage    []gax.CallOption
	CountMessageTokens []gax.CallOption
}

DiscussCallOptions contains the retry settings for each method of DiscussClient.

type 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
}
Output:

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
}
Output:

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

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
}
Output:

func (*DiscussClient) GenerateMessage

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
}
Output:

type ModelCallOptions

type ModelCallOptions struct {
	GetModel   []gax.CallOption
	ListModels []gax.CallOption
}

ModelCallOptions contains the retry settings for each method of ModelClient.

type 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
}
Output:

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
}
Output:

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

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
}
Output:

func (*ModelClient) ListModels

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

		// 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)
	}
}
Output:

Example (All)
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.ListModelsRequest{
		// TODO: Fill request struct fields.
		// See https://pkg.go.dev/cloud.google.com/go/ai/generativelanguage/apiv1beta2/generativelanguagepb#ListModelsRequest.
	}
	for resp, err := range c.ListModels(ctx, req).All() {
		if err != nil {
			// TODO: Handle error.
		}
		// TODO: Use resp.
		_ = resp
	}
}
Output:

type 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 added in v0.9.0

All returns an iterator. If an error is returned by the iterator, the iterator will stop after that iteration.

func (*ModelIterator) Next

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.

type TextCallOptions

type TextCallOptions struct {
	GenerateText []gax.CallOption
	EmbedText    []gax.CallOption
}

TextCallOptions contains the retry settings for each method of TextClient.

type 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
}
Output:

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
}
Output:

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

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
}
Output:

func (*TextClient) GenerateText

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
}
Output:

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL