-
-
Notifications
You must be signed in to change notification settings - Fork 668
/
palm_llm_option.go
101 lines (84 loc) · 2.58 KB
/
palm_llm_option.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package palm
import (
"net/http"
"os"
"sync"
"google.golang.org/api/option"
"google.golang.org/grpc"
)
const (
projectIDEnvVarName = "GOOGLE_CLOUD_PROJECT" //nolint:gosec
)
var (
// nolint: gochecknoglobals
initOptions sync.Once
// nolint: gochecknoglobals
defaultOptions *options
)
type options struct {
projectID string
location string
clientOptions []option.ClientOption
}
// Option is a function that can be passed to NewClient to configure options.
type Option func(*options)
// initOpts initializes defaultOptions with the environment variables.
func initOpts() {
defaultOptions = &options{
projectID: os.Getenv(projectIDEnvVarName),
}
}
// WithProjectID passes the Google Cloud project ID to the client. If not set, the project
// is read from the GOOGLE_CLOUD_PROJECT environment variable.
func WithProjectID(projectID string) Option {
return func(opts *options) {
opts.projectID = projectID
}
}
// WithLocation passes the Google Cloud location to the client.
func WithLocation(location string) Option {
return func(opts *options) {
opts.location = location
}
}
// WithAPIKey returns a ClientOption that specifies an API key to be used
// as the basis for authentication.
func WithAPIKey(apiKey string) Option {
return convertStringOption(option.WithAPIKey)(apiKey)
}
// WithCredentialsFile returns a ClientOption that authenticates
// API calls with the given service account or refresh token JSON
// credentials file.
func WithCredentialsFile(path string) Option {
return convertStringOption(option.WithCredentialsFile)(path)
}
// WithCredentialsJSON returns a ClientOption that authenticates
// API calls with the given service account or refresh token JSON
// credentials.
func WithCredentialsJSON(json []byte) Option {
return convertByteArrayOption(option.WithCredentialsJSON)(json)
}
func WithGRPCDialOption(opt grpc.DialOption) Option {
return func(opts *options) {
opts.clientOptions = append(opts.clientOptions, option.WithGRPCDialOption(opt))
}
}
func WithHTTPClient(client *http.Client) Option {
return func(opts *options) {
opts.clientOptions = append(opts.clientOptions, option.WithHTTPClient(client))
}
}
func convertStringOption(fopt func(string) option.ClientOption) func(string) Option {
return func(param string) Option {
return func(opts *options) {
opts.clientOptions = append(opts.clientOptions, fopt(param))
}
}
}
func convertByteArrayOption(fopt func([]byte) option.ClientOption) func([]byte) Option {
return func(param []byte) Option {
return func(opts *options) {
opts.clientOptions = append(opts.clientOptions, fopt(param))
}
}
}