1004 lines
35 KiB
Go
1004 lines
35 KiB
Go
// Copyright 2024 Google LLC
|
||
//
|
||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
// you may not use this file except in compliance with the License.
|
||
// You may obtain a copy of the License at
|
||
//
|
||
// https://www.apache.org/licenses/LICENSE-2.0
|
||
//
|
||
// Unless required by applicable law or agreed to in writing, software
|
||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
// See the License for the specific language governing permissions and
|
||
// limitations under the License.
|
||
|
||
// Code generated by protoc-gen-go_gapic. DO NOT EDIT.
|
||
|
||
package generativelanguage
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"io"
|
||
"math"
|
||
"net/http"
|
||
"net/url"
|
||
"time"
|
||
|
||
generativelanguagepb "cloud.google.com/go/ai/generativelanguage/apiv1beta/generativelanguagepb"
|
||
gax "github.com/googleapis/gax-go/v2"
|
||
"google.golang.org/api/googleapi"
|
||
"google.golang.org/api/option"
|
||
"google.golang.org/api/option/internaloption"
|
||
gtransport "google.golang.org/api/transport/grpc"
|
||
httptransport "google.golang.org/api/transport/http"
|
||
"google.golang.org/grpc"
|
||
"google.golang.org/grpc/codes"
|
||
"google.golang.org/grpc/metadata"
|
||
"google.golang.org/protobuf/encoding/protojson"
|
||
)
|
||
|
||
var newGenerativeClientHook clientHook
|
||
|
||
// GenerativeCallOptions contains the retry settings for each method of GenerativeClient.
|
||
type GenerativeCallOptions struct {
|
||
GenerateContent []gax.CallOption
|
||
GenerateAnswer []gax.CallOption
|
||
StreamGenerateContent []gax.CallOption
|
||
EmbedContent []gax.CallOption
|
||
BatchEmbedContents []gax.CallOption
|
||
CountTokens []gax.CallOption
|
||
}
|
||
|
||
func defaultGenerativeGRPCClientOptions() []option.ClientOption {
|
||
return []option.ClientOption{
|
||
internaloption.WithDefaultEndpoint("generativelanguage.googleapis.com:443"),
|
||
internaloption.WithDefaultEndpointTemplate("generativelanguage.UNIVERSE_DOMAIN:443"),
|
||
internaloption.WithDefaultMTLSEndpoint("generativelanguage.mtls.googleapis.com:443"),
|
||
internaloption.WithDefaultUniverseDomain("googleapis.com"),
|
||
internaloption.WithDefaultAudience("https://generativelanguage.googleapis.com/"),
|
||
internaloption.WithDefaultScopes(DefaultAuthScopes()...),
|
||
internaloption.EnableJwtWithScope(),
|
||
option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
|
||
grpc.MaxCallRecvMsgSize(math.MaxInt32))),
|
||
}
|
||
}
|
||
|
||
func defaultGenerativeCallOptions() *GenerativeCallOptions {
|
||
return &GenerativeCallOptions{
|
||
GenerateContent: []gax.CallOption{
|
||
gax.WithTimeout(600000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnCodes([]codes.Code{
|
||
codes.Unavailable,
|
||
}, gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
})
|
||
}),
|
||
},
|
||
GenerateAnswer: []gax.CallOption{
|
||
gax.WithTimeout(60000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnCodes([]codes.Code{
|
||
codes.Unavailable,
|
||
}, gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
})
|
||
}),
|
||
},
|
||
StreamGenerateContent: []gax.CallOption{
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnCodes([]codes.Code{
|
||
codes.Unavailable,
|
||
}, gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
})
|
||
}),
|
||
},
|
||
EmbedContent: []gax.CallOption{
|
||
gax.WithTimeout(60000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnCodes([]codes.Code{
|
||
codes.Unavailable,
|
||
}, gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
})
|
||
}),
|
||
},
|
||
BatchEmbedContents: []gax.CallOption{
|
||
gax.WithTimeout(60000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnCodes([]codes.Code{
|
||
codes.Unavailable,
|
||
}, gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
})
|
||
}),
|
||
},
|
||
CountTokens: []gax.CallOption{
|
||
gax.WithTimeout(60000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnCodes([]codes.Code{
|
||
codes.Unavailable,
|
||
}, gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
})
|
||
}),
|
||
},
|
||
}
|
||
}
|
||
|
||
func defaultGenerativeRESTCallOptions() *GenerativeCallOptions {
|
||
return &GenerativeCallOptions{
|
||
GenerateContent: []gax.CallOption{
|
||
gax.WithTimeout(600000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnHTTPCodes(gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
},
|
||
http.StatusServiceUnavailable)
|
||
}),
|
||
},
|
||
GenerateAnswer: []gax.CallOption{
|
||
gax.WithTimeout(60000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnHTTPCodes(gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
},
|
||
http.StatusServiceUnavailable)
|
||
}),
|
||
},
|
||
StreamGenerateContent: []gax.CallOption{
|
||
gax.WithTimeout(600000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnHTTPCodes(gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
},
|
||
http.StatusServiceUnavailable)
|
||
}),
|
||
},
|
||
EmbedContent: []gax.CallOption{
|
||
gax.WithTimeout(60000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnHTTPCodes(gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
},
|
||
http.StatusServiceUnavailable)
|
||
}),
|
||
},
|
||
BatchEmbedContents: []gax.CallOption{
|
||
gax.WithTimeout(60000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnHTTPCodes(gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
},
|
||
http.StatusServiceUnavailable)
|
||
}),
|
||
},
|
||
CountTokens: []gax.CallOption{
|
||
gax.WithTimeout(60000 * time.Millisecond),
|
||
gax.WithRetry(func() gax.Retryer {
|
||
return gax.OnHTTPCodes(gax.Backoff{
|
||
Initial: 1000 * time.Millisecond,
|
||
Max: 10000 * time.Millisecond,
|
||
Multiplier: 1.30,
|
||
},
|
||
http.StatusServiceUnavailable)
|
||
}),
|
||
},
|
||
}
|
||
}
|
||
|
||
// internalGenerativeClient is an interface that defines the methods available from Generative Language API.
|
||
type internalGenerativeClient interface {
|
||
Close() error
|
||
setGoogleClientInfo(...string)
|
||
Connection() *grpc.ClientConn
|
||
GenerateContent(context.Context, *generativelanguagepb.GenerateContentRequest, ...gax.CallOption) (*generativelanguagepb.GenerateContentResponse, error)
|
||
GenerateAnswer(context.Context, *generativelanguagepb.GenerateAnswerRequest, ...gax.CallOption) (*generativelanguagepb.GenerateAnswerResponse, error)
|
||
StreamGenerateContent(context.Context, *generativelanguagepb.GenerateContentRequest, ...gax.CallOption) (generativelanguagepb.GenerativeService_StreamGenerateContentClient, error)
|
||
EmbedContent(context.Context, *generativelanguagepb.EmbedContentRequest, ...gax.CallOption) (*generativelanguagepb.EmbedContentResponse, error)
|
||
BatchEmbedContents(context.Context, *generativelanguagepb.BatchEmbedContentsRequest, ...gax.CallOption) (*generativelanguagepb.BatchEmbedContentsResponse, error)
|
||
CountTokens(context.Context, *generativelanguagepb.CountTokensRequest, ...gax.CallOption) (*generativelanguagepb.CountTokensResponse, error)
|
||
}
|
||
|
||
// 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.
|
||
type GenerativeClient struct {
|
||
// The internal transport-dependent client.
|
||
internalClient internalGenerativeClient
|
||
|
||
// The call options for this service.
|
||
CallOptions *GenerativeCallOptions
|
||
}
|
||
|
||
// Wrapper methods routed to the internal client.
|
||
|
||
// Close closes the connection to the API service. The user should invoke this when
|
||
// the client is no longer required.
|
||
func (c *GenerativeClient) Close() error {
|
||
return c.internalClient.Close()
|
||
}
|
||
|
||
// setGoogleClientInfo sets the name and version of the application in
|
||
// the `x-goog-api-client` header passed on each request. Intended for
|
||
// use by Google-written clients.
|
||
func (c *GenerativeClient) setGoogleClientInfo(keyval ...string) {
|
||
c.internalClient.setGoogleClientInfo(keyval...)
|
||
}
|
||
|
||
// Connection returns a connection to the API service.
|
||
//
|
||
// Deprecated: Connections are now pooled so this method does not always
|
||
// return the same resource.
|
||
func (c *GenerativeClient) Connection() *grpc.ClientConn {
|
||
return c.internalClient.Connection()
|
||
}
|
||
|
||
// GenerateContent generates a response from the model given an input
|
||
// GenerateContentRequest.
|
||
//
|
||
// Input capabilities differ between models, including tuned models. See the
|
||
// model guide (at https://ai.google.dev/models/gemini) and
|
||
// tuning guide (at https://ai.google.dev/docs/model_tuning_guidance) for
|
||
// details.
|
||
func (c *GenerativeClient) GenerateContent(ctx context.Context, req *generativelanguagepb.GenerateContentRequest, opts ...gax.CallOption) (*generativelanguagepb.GenerateContentResponse, error) {
|
||
return c.internalClient.GenerateContent(ctx, req, opts...)
|
||
}
|
||
|
||
// GenerateAnswer generates a grounded answer from the model given an input
|
||
// GenerateAnswerRequest.
|
||
func (c *GenerativeClient) GenerateAnswer(ctx context.Context, req *generativelanguagepb.GenerateAnswerRequest, opts ...gax.CallOption) (*generativelanguagepb.GenerateAnswerResponse, error) {
|
||
return c.internalClient.GenerateAnswer(ctx, req, opts...)
|
||
}
|
||
|
||
// StreamGenerateContent generates a streamed response from the model given an input
|
||
// GenerateContentRequest.
|
||
func (c *GenerativeClient) StreamGenerateContent(ctx context.Context, req *generativelanguagepb.GenerateContentRequest, opts ...gax.CallOption) (generativelanguagepb.GenerativeService_StreamGenerateContentClient, error) {
|
||
return c.internalClient.StreamGenerateContent(ctx, req, opts...)
|
||
}
|
||
|
||
// EmbedContent generates an embedding from the model given an input Content.
|
||
func (c *GenerativeClient) EmbedContent(ctx context.Context, req *generativelanguagepb.EmbedContentRequest, opts ...gax.CallOption) (*generativelanguagepb.EmbedContentResponse, error) {
|
||
return c.internalClient.EmbedContent(ctx, req, opts...)
|
||
}
|
||
|
||
// BatchEmbedContents generates multiple embeddings from the model given input text in a
|
||
// synchronous call.
|
||
func (c *GenerativeClient) BatchEmbedContents(ctx context.Context, req *generativelanguagepb.BatchEmbedContentsRequest, opts ...gax.CallOption) (*generativelanguagepb.BatchEmbedContentsResponse, error) {
|
||
return c.internalClient.BatchEmbedContents(ctx, req, opts...)
|
||
}
|
||
|
||
// CountTokens runs a model’s tokenizer on input content and returns the token count.
|
||
func (c *GenerativeClient) CountTokens(ctx context.Context, req *generativelanguagepb.CountTokensRequest, opts ...gax.CallOption) (*generativelanguagepb.CountTokensResponse, error) {
|
||
return c.internalClient.CountTokens(ctx, req, opts...)
|
||
}
|
||
|
||
// generativeGRPCClient is a client for interacting with Generative Language API over gRPC transport.
|
||
//
|
||
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
|
||
type generativeGRPCClient struct {
|
||
// Connection pool of gRPC connections to the service.
|
||
connPool gtransport.ConnPool
|
||
|
||
// Points back to the CallOptions field of the containing GenerativeClient
|
||
CallOptions **GenerativeCallOptions
|
||
|
||
// The gRPC API client.
|
||
generativeClient generativelanguagepb.GenerativeServiceClient
|
||
|
||
// The x-goog-* metadata to be sent with each request.
|
||
xGoogHeaders []string
|
||
}
|
||
|
||
// 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.
|
||
func NewGenerativeClient(ctx context.Context, opts ...option.ClientOption) (*GenerativeClient, error) {
|
||
clientOpts := defaultGenerativeGRPCClientOptions()
|
||
if newGenerativeClientHook != nil {
|
||
hookOpts, err := newGenerativeClientHook(ctx, clientHookParams{})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
clientOpts = append(clientOpts, hookOpts...)
|
||
}
|
||
|
||
connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
client := GenerativeClient{CallOptions: defaultGenerativeCallOptions()}
|
||
|
||
c := &generativeGRPCClient{
|
||
connPool: connPool,
|
||
generativeClient: generativelanguagepb.NewGenerativeServiceClient(connPool),
|
||
CallOptions: &client.CallOptions,
|
||
}
|
||
c.setGoogleClientInfo()
|
||
|
||
client.internalClient = c
|
||
|
||
return &client, nil
|
||
}
|
||
|
||
// Connection returns a connection to the API service.
|
||
//
|
||
// Deprecated: Connections are now pooled so this method does not always
|
||
// return the same resource.
|
||
func (c *generativeGRPCClient) Connection() *grpc.ClientConn {
|
||
return c.connPool.Conn()
|
||
}
|
||
|
||
// setGoogleClientInfo sets the name and version of the application in
|
||
// the `x-goog-api-client` header passed on each request. Intended for
|
||
// use by Google-written clients.
|
||
func (c *generativeGRPCClient) setGoogleClientInfo(keyval ...string) {
|
||
kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
|
||
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
|
||
c.xGoogHeaders = []string{
|
||
"x-goog-api-client", gax.XGoogHeader(kv...),
|
||
}
|
||
}
|
||
|
||
// Close closes the connection to the API service. The user should invoke this when
|
||
// the client is no longer required.
|
||
func (c *generativeGRPCClient) Close() error {
|
||
return c.connPool.Close()
|
||
}
|
||
|
||
// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
|
||
type generativeRESTClient struct {
|
||
// The http endpoint to connect to.
|
||
endpoint string
|
||
|
||
// The http client.
|
||
httpClient *http.Client
|
||
|
||
// The x-goog-* headers to be sent with each request.
|
||
xGoogHeaders []string
|
||
|
||
// Points back to the CallOptions field of the containing GenerativeClient
|
||
CallOptions **GenerativeCallOptions
|
||
}
|
||
|
||
// NewGenerativeRESTClient creates a new generative service rest client.
|
||
//
|
||
// API for using Large Models that generate multimodal content and have
|
||
// additional capabilities beyond text generation.
|
||
func NewGenerativeRESTClient(ctx context.Context, opts ...option.ClientOption) (*GenerativeClient, error) {
|
||
clientOpts := append(defaultGenerativeRESTClientOptions(), opts...)
|
||
httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
callOpts := defaultGenerativeRESTCallOptions()
|
||
c := &generativeRESTClient{
|
||
endpoint: endpoint,
|
||
httpClient: httpClient,
|
||
CallOptions: &callOpts,
|
||
}
|
||
c.setGoogleClientInfo()
|
||
|
||
return &GenerativeClient{internalClient: c, CallOptions: callOpts}, nil
|
||
}
|
||
|
||
func defaultGenerativeRESTClientOptions() []option.ClientOption {
|
||
return []option.ClientOption{
|
||
internaloption.WithDefaultEndpoint("https://generativelanguage.googleapis.com"),
|
||
internaloption.WithDefaultEndpointTemplate("https://generativelanguage.UNIVERSE_DOMAIN"),
|
||
internaloption.WithDefaultMTLSEndpoint("https://generativelanguage.mtls.googleapis.com"),
|
||
internaloption.WithDefaultUniverseDomain("googleapis.com"),
|
||
internaloption.WithDefaultAudience("https://generativelanguage.googleapis.com/"),
|
||
internaloption.WithDefaultScopes(DefaultAuthScopes()...),
|
||
}
|
||
}
|
||
|
||
// setGoogleClientInfo sets the name and version of the application in
|
||
// the `x-goog-api-client` header passed on each request. Intended for
|
||
// use by Google-written clients.
|
||
func (c *generativeRESTClient) setGoogleClientInfo(keyval ...string) {
|
||
kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
|
||
kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
|
||
c.xGoogHeaders = []string{
|
||
"x-goog-api-client", gax.XGoogHeader(kv...),
|
||
}
|
||
}
|
||
|
||
// Close closes the connection to the API service. The user should invoke this when
|
||
// the client is no longer required.
|
||
func (c *generativeRESTClient) Close() error {
|
||
// Replace httpClient with nil to force cleanup.
|
||
c.httpClient = nil
|
||
return nil
|
||
}
|
||
|
||
// Connection returns a connection to the API service.
|
||
//
|
||
// Deprecated: This method always returns nil.
|
||
func (c *generativeRESTClient) Connection() *grpc.ClientConn {
|
||
return nil
|
||
}
|
||
func (c *generativeGRPCClient) GenerateContent(ctx context.Context, req *generativelanguagepb.GenerateContentRequest, opts ...gax.CallOption) (*generativelanguagepb.GenerateContentResponse, error) {
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
|
||
opts = append((*c.CallOptions).GenerateContent[0:len((*c.CallOptions).GenerateContent):len((*c.CallOptions).GenerateContent)], opts...)
|
||
var resp *generativelanguagepb.GenerateContentResponse
|
||
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
var err error
|
||
resp, err = c.generativeClient.GenerateContent(ctx, req, settings.GRPC...)
|
||
return err
|
||
}, opts...)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
func (c *generativeGRPCClient) GenerateAnswer(ctx context.Context, req *generativelanguagepb.GenerateAnswerRequest, opts ...gax.CallOption) (*generativelanguagepb.GenerateAnswerResponse, error) {
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
|
||
opts = append((*c.CallOptions).GenerateAnswer[0:len((*c.CallOptions).GenerateAnswer):len((*c.CallOptions).GenerateAnswer)], opts...)
|
||
var resp *generativelanguagepb.GenerateAnswerResponse
|
||
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
var err error
|
||
resp, err = c.generativeClient.GenerateAnswer(ctx, req, settings.GRPC...)
|
||
return err
|
||
}, opts...)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
func (c *generativeGRPCClient) StreamGenerateContent(ctx context.Context, req *generativelanguagepb.GenerateContentRequest, opts ...gax.CallOption) (generativelanguagepb.GenerativeService_StreamGenerateContentClient, error) {
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
|
||
opts = append((*c.CallOptions).StreamGenerateContent[0:len((*c.CallOptions).StreamGenerateContent):len((*c.CallOptions).StreamGenerateContent)], opts...)
|
||
var resp generativelanguagepb.GenerativeService_StreamGenerateContentClient
|
||
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
var err error
|
||
resp, err = c.generativeClient.StreamGenerateContent(ctx, req, settings.GRPC...)
|
||
return err
|
||
}, opts...)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
func (c *generativeGRPCClient) EmbedContent(ctx context.Context, req *generativelanguagepb.EmbedContentRequest, opts ...gax.CallOption) (*generativelanguagepb.EmbedContentResponse, error) {
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
|
||
opts = append((*c.CallOptions).EmbedContent[0:len((*c.CallOptions).EmbedContent):len((*c.CallOptions).EmbedContent)], opts...)
|
||
var resp *generativelanguagepb.EmbedContentResponse
|
||
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
var err error
|
||
resp, err = c.generativeClient.EmbedContent(ctx, req, settings.GRPC...)
|
||
return err
|
||
}, opts...)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
func (c *generativeGRPCClient) BatchEmbedContents(ctx context.Context, req *generativelanguagepb.BatchEmbedContentsRequest, opts ...gax.CallOption) (*generativelanguagepb.BatchEmbedContentsResponse, error) {
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
|
||
opts = append((*c.CallOptions).BatchEmbedContents[0:len((*c.CallOptions).BatchEmbedContents):len((*c.CallOptions).BatchEmbedContents)], opts...)
|
||
var resp *generativelanguagepb.BatchEmbedContentsResponse
|
||
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
var err error
|
||
resp, err = c.generativeClient.BatchEmbedContents(ctx, req, settings.GRPC...)
|
||
return err
|
||
}, opts...)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
func (c *generativeGRPCClient) CountTokens(ctx context.Context, req *generativelanguagepb.CountTokensRequest, opts ...gax.CallOption) (*generativelanguagepb.CountTokensResponse, error) {
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
|
||
opts = append((*c.CallOptions).CountTokens[0:len((*c.CallOptions).CountTokens):len((*c.CallOptions).CountTokens)], opts...)
|
||
var resp *generativelanguagepb.CountTokensResponse
|
||
err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
var err error
|
||
resp, err = c.generativeClient.CountTokens(ctx, req, settings.GRPC...)
|
||
return err
|
||
}, opts...)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
// GenerateContent generates a response from the model given an input
|
||
// GenerateContentRequest.
|
||
//
|
||
// Input capabilities differ between models, including tuned models. See the
|
||
// model guide (at https://ai.google.dev/models/gemini) and
|
||
// tuning guide (at https://ai.google.dev/docs/model_tuning_guidance) for
|
||
// details.
|
||
func (c *generativeRESTClient) GenerateContent(ctx context.Context, req *generativelanguagepb.GenerateContentRequest, opts ...gax.CallOption) (*generativelanguagepb.GenerateContentResponse, error) {
|
||
m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
|
||
jsonReq, err := m.Marshal(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
baseUrl, err := url.Parse(c.endpoint)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
baseUrl.Path += fmt.Sprintf("/v1beta/%v:generateContent", req.GetModel())
|
||
|
||
params := url.Values{}
|
||
params.Add("$alt", "json;enum-encoding=int")
|
||
|
||
baseUrl.RawQuery = params.Encode()
|
||
|
||
// Build HTTP headers from client and context metadata.
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
hds = append(hds, "Content-Type", "application/json")
|
||
headers := gax.BuildHeaders(ctx, hds...)
|
||
opts = append((*c.CallOptions).GenerateContent[0:len((*c.CallOptions).GenerateContent):len((*c.CallOptions).GenerateContent)], opts...)
|
||
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
|
||
resp := &generativelanguagepb.GenerateContentResponse{}
|
||
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
if settings.Path != "" {
|
||
baseUrl.Path = settings.Path
|
||
}
|
||
httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
httpReq = httpReq.WithContext(ctx)
|
||
httpReq.Header = headers
|
||
|
||
httpRsp, err := c.httpClient.Do(httpReq)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer httpRsp.Body.Close()
|
||
|
||
if err = googleapi.CheckResponse(httpRsp); err != nil {
|
||
return err
|
||
}
|
||
|
||
buf, err := io.ReadAll(httpRsp.Body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if err := unm.Unmarshal(buf, resp); err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}, opts...)
|
||
if e != nil {
|
||
return nil, e
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
// GenerateAnswer generates a grounded answer from the model given an input
|
||
// GenerateAnswerRequest.
|
||
func (c *generativeRESTClient) GenerateAnswer(ctx context.Context, req *generativelanguagepb.GenerateAnswerRequest, opts ...gax.CallOption) (*generativelanguagepb.GenerateAnswerResponse, error) {
|
||
m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
|
||
jsonReq, err := m.Marshal(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
baseUrl, err := url.Parse(c.endpoint)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
baseUrl.Path += fmt.Sprintf("/v1beta/%v:generateAnswer", req.GetModel())
|
||
|
||
params := url.Values{}
|
||
params.Add("$alt", "json;enum-encoding=int")
|
||
|
||
baseUrl.RawQuery = params.Encode()
|
||
|
||
// Build HTTP headers from client and context metadata.
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
hds = append(hds, "Content-Type", "application/json")
|
||
headers := gax.BuildHeaders(ctx, hds...)
|
||
opts = append((*c.CallOptions).GenerateAnswer[0:len((*c.CallOptions).GenerateAnswer):len((*c.CallOptions).GenerateAnswer)], opts...)
|
||
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
|
||
resp := &generativelanguagepb.GenerateAnswerResponse{}
|
||
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
if settings.Path != "" {
|
||
baseUrl.Path = settings.Path
|
||
}
|
||
httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
httpReq = httpReq.WithContext(ctx)
|
||
httpReq.Header = headers
|
||
|
||
httpRsp, err := c.httpClient.Do(httpReq)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer httpRsp.Body.Close()
|
||
|
||
if err = googleapi.CheckResponse(httpRsp); err != nil {
|
||
return err
|
||
}
|
||
|
||
buf, err := io.ReadAll(httpRsp.Body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if err := unm.Unmarshal(buf, resp); err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}, opts...)
|
||
if e != nil {
|
||
return nil, e
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
// StreamGenerateContent generates a streamed response from the model given an input
|
||
// GenerateContentRequest.
|
||
func (c *generativeRESTClient) StreamGenerateContent(ctx context.Context, req *generativelanguagepb.GenerateContentRequest, opts ...gax.CallOption) (generativelanguagepb.GenerativeService_StreamGenerateContentClient, error) {
|
||
m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
|
||
jsonReq, err := m.Marshal(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
baseUrl, err := url.Parse(c.endpoint)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
baseUrl.Path += fmt.Sprintf("/v1beta/%v:streamGenerateContent", req.GetModel())
|
||
|
||
params := url.Values{}
|
||
params.Add("$alt", "json;enum-encoding=int")
|
||
|
||
baseUrl.RawQuery = params.Encode()
|
||
|
||
// Build HTTP headers from client and context metadata.
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
hds = append(hds, "Content-Type", "application/json")
|
||
headers := gax.BuildHeaders(ctx, hds...)
|
||
var streamClient *streamGenerateContentRESTClient
|
||
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
if settings.Path != "" {
|
||
baseUrl.Path = settings.Path
|
||
}
|
||
httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
httpReq = httpReq.WithContext(ctx)
|
||
httpReq.Header = headers
|
||
|
||
httpRsp, err := c.httpClient.Do(httpReq)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if err = googleapi.CheckResponse(httpRsp); err != nil {
|
||
return err
|
||
}
|
||
|
||
streamClient = &streamGenerateContentRESTClient{
|
||
ctx: ctx,
|
||
md: metadata.MD(httpRsp.Header),
|
||
stream: gax.NewProtoJSONStreamReader(httpRsp.Body, (&generativelanguagepb.GenerateContentResponse{}).ProtoReflect().Type()),
|
||
}
|
||
return nil
|
||
}, opts...)
|
||
|
||
return streamClient, e
|
||
}
|
||
|
||
// streamGenerateContentRESTClient is the stream client used to consume the server stream created by
|
||
// the REST implementation of StreamGenerateContent.
|
||
type streamGenerateContentRESTClient struct {
|
||
ctx context.Context
|
||
md metadata.MD
|
||
stream *gax.ProtoJSONStream
|
||
}
|
||
|
||
func (c *streamGenerateContentRESTClient) Recv() (*generativelanguagepb.GenerateContentResponse, error) {
|
||
if err := c.ctx.Err(); err != nil {
|
||
defer c.stream.Close()
|
||
return nil, err
|
||
}
|
||
msg, err := c.stream.Recv()
|
||
if err != nil {
|
||
defer c.stream.Close()
|
||
return nil, err
|
||
}
|
||
res := msg.(*generativelanguagepb.GenerateContentResponse)
|
||
return res, nil
|
||
}
|
||
|
||
func (c *streamGenerateContentRESTClient) Header() (metadata.MD, error) {
|
||
return c.md, nil
|
||
}
|
||
|
||
func (c *streamGenerateContentRESTClient) Trailer() metadata.MD {
|
||
return c.md
|
||
}
|
||
|
||
func (c *streamGenerateContentRESTClient) CloseSend() error {
|
||
// This is a no-op to fulfill the interface.
|
||
return errors.New("this method is not implemented for a server-stream")
|
||
}
|
||
|
||
func (c *streamGenerateContentRESTClient) Context() context.Context {
|
||
return c.ctx
|
||
}
|
||
|
||
func (c *streamGenerateContentRESTClient) SendMsg(m interface{}) error {
|
||
// This is a no-op to fulfill the interface.
|
||
return errors.New("this method is not implemented for a server-stream")
|
||
}
|
||
|
||
func (c *streamGenerateContentRESTClient) RecvMsg(m interface{}) error {
|
||
// This is a no-op to fulfill the interface.
|
||
return errors.New("this method is not implemented, use Recv")
|
||
}
|
||
|
||
// EmbedContent generates an embedding from the model given an input Content.
|
||
func (c *generativeRESTClient) EmbedContent(ctx context.Context, req *generativelanguagepb.EmbedContentRequest, opts ...gax.CallOption) (*generativelanguagepb.EmbedContentResponse, error) {
|
||
m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
|
||
jsonReq, err := m.Marshal(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
baseUrl, err := url.Parse(c.endpoint)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
baseUrl.Path += fmt.Sprintf("/v1beta/%v:embedContent", req.GetModel())
|
||
|
||
params := url.Values{}
|
||
params.Add("$alt", "json;enum-encoding=int")
|
||
|
||
baseUrl.RawQuery = params.Encode()
|
||
|
||
// Build HTTP headers from client and context metadata.
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
hds = append(hds, "Content-Type", "application/json")
|
||
headers := gax.BuildHeaders(ctx, hds...)
|
||
opts = append((*c.CallOptions).EmbedContent[0:len((*c.CallOptions).EmbedContent):len((*c.CallOptions).EmbedContent)], opts...)
|
||
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
|
||
resp := &generativelanguagepb.EmbedContentResponse{}
|
||
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
if settings.Path != "" {
|
||
baseUrl.Path = settings.Path
|
||
}
|
||
httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
httpReq = httpReq.WithContext(ctx)
|
||
httpReq.Header = headers
|
||
|
||
httpRsp, err := c.httpClient.Do(httpReq)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer httpRsp.Body.Close()
|
||
|
||
if err = googleapi.CheckResponse(httpRsp); err != nil {
|
||
return err
|
||
}
|
||
|
||
buf, err := io.ReadAll(httpRsp.Body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if err := unm.Unmarshal(buf, resp); err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}, opts...)
|
||
if e != nil {
|
||
return nil, e
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
// BatchEmbedContents generates multiple embeddings from the model given input text in a
|
||
// synchronous call.
|
||
func (c *generativeRESTClient) BatchEmbedContents(ctx context.Context, req *generativelanguagepb.BatchEmbedContentsRequest, opts ...gax.CallOption) (*generativelanguagepb.BatchEmbedContentsResponse, error) {
|
||
m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
|
||
jsonReq, err := m.Marshal(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
baseUrl, err := url.Parse(c.endpoint)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
baseUrl.Path += fmt.Sprintf("/v1beta/%v:batchEmbedContents", req.GetModel())
|
||
|
||
params := url.Values{}
|
||
params.Add("$alt", "json;enum-encoding=int")
|
||
|
||
baseUrl.RawQuery = params.Encode()
|
||
|
||
// Build HTTP headers from client and context metadata.
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
hds = append(hds, "Content-Type", "application/json")
|
||
headers := gax.BuildHeaders(ctx, hds...)
|
||
opts = append((*c.CallOptions).BatchEmbedContents[0:len((*c.CallOptions).BatchEmbedContents):len((*c.CallOptions).BatchEmbedContents)], opts...)
|
||
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
|
||
resp := &generativelanguagepb.BatchEmbedContentsResponse{}
|
||
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
if settings.Path != "" {
|
||
baseUrl.Path = settings.Path
|
||
}
|
||
httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
httpReq = httpReq.WithContext(ctx)
|
||
httpReq.Header = headers
|
||
|
||
httpRsp, err := c.httpClient.Do(httpReq)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer httpRsp.Body.Close()
|
||
|
||
if err = googleapi.CheckResponse(httpRsp); err != nil {
|
||
return err
|
||
}
|
||
|
||
buf, err := io.ReadAll(httpRsp.Body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if err := unm.Unmarshal(buf, resp); err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}, opts...)
|
||
if e != nil {
|
||
return nil, e
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
// CountTokens runs a model’s tokenizer on input content and returns the token count.
|
||
func (c *generativeRESTClient) CountTokens(ctx context.Context, req *generativelanguagepb.CountTokensRequest, opts ...gax.CallOption) (*generativelanguagepb.CountTokensResponse, error) {
|
||
m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
|
||
jsonReq, err := m.Marshal(req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
baseUrl, err := url.Parse(c.endpoint)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
baseUrl.Path += fmt.Sprintf("/v1beta/%v:countTokens", req.GetModel())
|
||
|
||
params := url.Values{}
|
||
params.Add("$alt", "json;enum-encoding=int")
|
||
|
||
baseUrl.RawQuery = params.Encode()
|
||
|
||
// Build HTTP headers from client and context metadata.
|
||
hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "model", url.QueryEscape(req.GetModel()))}
|
||
|
||
hds = append(c.xGoogHeaders, hds...)
|
||
hds = append(hds, "Content-Type", "application/json")
|
||
headers := gax.BuildHeaders(ctx, hds...)
|
||
opts = append((*c.CallOptions).CountTokens[0:len((*c.CallOptions).CountTokens):len((*c.CallOptions).CountTokens)], opts...)
|
||
unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
|
||
resp := &generativelanguagepb.CountTokensResponse{}
|
||
e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
|
||
if settings.Path != "" {
|
||
baseUrl.Path = settings.Path
|
||
}
|
||
httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
|
||
if err != nil {
|
||
return err
|
||
}
|
||
httpReq = httpReq.WithContext(ctx)
|
||
httpReq.Header = headers
|
||
|
||
httpRsp, err := c.httpClient.Do(httpReq)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
defer httpRsp.Body.Close()
|
||
|
||
if err = googleapi.CheckResponse(httpRsp); err != nil {
|
||
return err
|
||
}
|
||
|
||
buf, err := io.ReadAll(httpRsp.Body)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
if err := unm.Unmarshal(buf, resp); err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}, opts...)
|
||
if e != nil {
|
||
return nil, e
|
||
}
|
||
return resp, nil
|
||
}
|