-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
77 lines (56 loc) · 1.83 KB
/
config.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
package ai
import "golang.org/x/time/rate"
type ClientConfig struct {
LLMs LLMs
APIKey string
Endpoint string
Proxy string
Limit *rate.Limit
Model string
ModelConfig ModelConfig
}
type ModelConfig struct {
Count *int32
MaxTokens *int32
Temperature *float32
TopP *float32
JSONResponse *bool
}
func ApplyModelConfig(ai AI, cfg ModelConfig) {
if cfg.Count != nil {
ai.SetCount(*cfg.Count)
}
if cfg.MaxTokens != nil {
ai.SetMaxTokens(*cfg.MaxTokens)
}
if cfg.Temperature != nil {
ai.SetTemperature(*cfg.Temperature)
}
if cfg.TopP != nil {
ai.SetTopP(*cfg.TopP)
}
if cfg.JSONResponse != nil {
ai.SetJSONResponse(*cfg.JSONResponse)
}
}
type ClientOption interface {
Apply(*ClientConfig)
}
func WithAPIKey(apiKey string) ClientOption { return withAPIKey(apiKey) }
func WithEndpoint(endpoint string) ClientOption { return withEndpoint(endpoint) }
func WithProxy(proxy string) ClientOption { return withProxy(proxy) }
func WithLimit(limit rate.Limit) ClientOption { return withLimit(limit) }
func WithModel(model string) ClientOption { return withModel(model) }
func WithModelConfig(config ModelConfig) ClientOption { return withModelConfig(config) }
type withAPIKey string
func (w withAPIKey) Apply(cfg *ClientConfig) { cfg.APIKey = string(w) }
type withEndpoint string
func (w withEndpoint) Apply(cfg *ClientConfig) { cfg.Endpoint = string(w) }
type withProxy string
func (w withProxy) Apply(cfg *ClientConfig) { cfg.Proxy = string(w) }
type withLimit rate.Limit
func (w withLimit) Apply(cfg *ClientConfig) { cfg.Limit = (*rate.Limit)(&w) }
type withModel string
func (w withModel) Apply(cfg *ClientConfig) { cfg.Model = string(w) }
type withModelConfig ModelConfig
func (w withModelConfig) Apply(cfg *ClientConfig) { cfg.ModelConfig = ModelConfig(w) }