-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
options.go
123 lines (104 loc) · 3.21 KB
/
options.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package gemini
import (
"errors"
)
var (
errorsMissingToken = errors.New("missing gemini api key")
errorsMissingModel = errors.New("missing model")
)
const (
defaultMaxTokens = 300
defaultModel = "gemini-1.5-flash-latest"
defaultTemperature = 1.0
defaultTopP = 1.0
)
// Option is an interface that specifies instrumentation configuration options.
type Option interface {
apply(*config)
}
// optionFunc is a type of function that can be used to implement the Option interface.
// It takes a pointer to a config struct and modifies it.
type optionFunc func(*config)
// Ensure that optionFunc satisfies the Option interface.
var _ Option = (*optionFunc)(nil)
// The apply method of optionFunc type is implemented here to modify the config struct based on the function passed.
func (o optionFunc) apply(c *config) {
o(c)
}
// WithToken is a function that returns an Option, which sets the token field of the config struct.
func WithToken(val string) Option {
return optionFunc(func(c *config) {
c.token = val
})
}
// WithModel is a function that returns an Option, which sets the model field of the config struct.
func WithModel(val string) Option {
return optionFunc(func(c *config) {
c.model = val
})
}
// WithMaxTokens returns a new Option that sets the max tokens for the client configuration.
// The maximum number of tokens to generate in the chat completion.
// The total length of input tokens and generated tokens is limited by the model's context length.
func WithMaxTokens(val int) Option {
if val <= 0 {
val = defaultMaxTokens
}
return optionFunc(func(c *config) {
c.maxTokens = val
})
}
// WithTemperature returns a new Option that sets the temperature for the client configuration.
// What sampling temperature to use, between 0 and 2.
// Higher values like 0.8 will make the output more random,
// while lower values like 0.2 will make it more focused and deterministic.
func WithTemperature(val float32) Option {
if val <= 0 {
val = defaultTemperature
}
return optionFunc(func(c *config) {
c.temperature = val
})
}
// WithTopP returns a new Option that sets the topP for the client configuration.
func WithTopP(val float32) Option {
return optionFunc(func(c *config) {
c.topP = val
})
}
// config is a struct that stores configuration options for the instrumentation.
type config struct {
token string
model string
maxTokens int
temperature float32
topP float32
}
// valid checks whether a config object is valid, returning an error if it is not.
func (cfg *config) valid() error {
// Check that the token is not empty.
if cfg.token == "" {
return errorsMissingToken
}
if cfg.model == "" {
return errorsMissingModel
}
// If all checks pass, return nil (no error).
return nil
}
// newConfig creates a new config object with default values, and applies the given options.
func newConfig(opts ...Option) *config {
// Create a new config object with default values.
c := &config{
model: defaultModel,
maxTokens: defaultMaxTokens,
temperature: defaultTemperature,
topP: defaultTopP,
}
// Apply each of the given options to the config object.
for _, opt := range opts {
opt.apply(c)
}
// Return the resulting config object.
return c
}