-
Notifications
You must be signed in to change notification settings - Fork 19
/
config.go
123 lines (98 loc) · 2.59 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
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 anthropic
import (
"fmt"
"net/http"
)
const (
anthropicAPIURLv1 = "https://api.anthropic.com/v1"
defaultEmptyMessagesLimit uint = 300
)
type APIVersion string
const (
APIVersion20230601 APIVersion = "2023-06-01"
APIVersionVertex20231016 APIVersion = "vertex-2023-10-16"
)
type BetaVersion string
const (
BetaTools20240404 BetaVersion = "tools-2024-04-04"
BetaTools20240516 BetaVersion = "tools-2024-05-16"
BetaPromptCaching20240731 BetaVersion = "prompt-caching-2024-07-31"
BetaMessageBatches20240924 BetaVersion = "message-batches-2024-09-24"
BetaTokenCounting20241101 BetaVersion = "token-counting-2024-11-01"
BetaMaxTokens35Sonnet20240715 BetaVersion = "max-tokens-3-5-sonnet-2024-07-15"
)
type ApiKeyFunc func() string
// ClientConfig is a configuration of a client.
type ClientConfig struct {
apiKeyFunc ApiKeyFunc
apiKey string
BaseURL string
APIVersion APIVersion
BetaVersion []BetaVersion
HTTPClient *http.Client
EmptyMessagesLimit uint
Adapter ClientAdapter
}
type ClientOption func(c *ClientConfig)
func newConfig(apiKey string, opts ...ClientOption) ClientConfig {
c := ClientConfig{
apiKey: apiKey,
BaseURL: anthropicAPIURLv1,
APIVersion: APIVersion20230601,
HTTPClient: &http.Client{},
EmptyMessagesLimit: defaultEmptyMessagesLimit,
Adapter: &DefaultAdapter{},
}
for _, opt := range opts {
opt(&c)
}
return c
}
func (c *ClientConfig) GetApiKey() string {
if c.apiKeyFunc != nil {
return c.apiKeyFunc()
}
return c.apiKey
}
func WithBaseURL(baseUrl string) ClientOption {
return func(c *ClientConfig) {
c.BaseURL = baseUrl
}
}
func WithAPIVersion(apiVersion APIVersion) ClientOption {
return func(c *ClientConfig) {
c.APIVersion = apiVersion
}
}
func WithHTTPClient(cli *http.Client) ClientOption {
return func(c *ClientConfig) {
c.HTTPClient = cli
}
}
func WithEmptyMessagesLimit(limit uint) ClientOption {
return func(c *ClientConfig) {
c.EmptyMessagesLimit = limit
}
}
func WithBetaVersion(betaVersion ...BetaVersion) ClientOption {
return func(c *ClientConfig) {
c.BetaVersion = betaVersion
}
}
func WithVertexAI(projectID string, location string) ClientOption {
return func(c *ClientConfig) {
c.BaseURL = fmt.Sprintf(
"https://%s-aiplatform.googleapis.com/v1/projects/%s/locations/%s/publishers/anthropic/models",
location,
projectID,
location,
)
c.APIVersion = APIVersionVertex20231016
c.Adapter = &VertexAdapter{}
}
}
func WithApiKeyFunc(apiKeyFunc ApiKeyFunc) ClientOption {
return func(c *ClientConfig) {
c.apiKeyFunc = apiKeyFunc
}
}