forked from tmc/langchaingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vertexai_palm_llm_option.go
93 lines (77 loc) · 2.4 KB
/
vertexai_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
package vertexai
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
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
}
}
// 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))
}
}
}