forked from sashabaranov/go-openai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
95 lines (73 loc) · 2.06 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
package openai
import (
"net/http"
"regexp"
)
const (
OpenaiAPIURLv1 = "https://api.openai.com/v1"
AnyscaleEndpointsAPIURLv1 = "https://api.endpoints.anyscale.com/v1"
defaultEmptyMessagesLimit uint = 300
azureAPIPrefix = "openai"
azureDeploymentsPrefix = "deployments"
)
type APIType string
const (
APITypeOpenAI APIType = "OPEN_AI"
APITypeAzure APIType = "AZURE"
APITypeAzureAD APIType = "AZURE_AD"
)
const AzureAPIKeyHeader = "api-key"
// ClientConfig is a configuration of a client.
type ClientConfig struct {
authToken string
BaseURL string
OrgID string
APIType APIType
APIVersion string // required when APIType is APITypeAzure or APITypeAzureAD
AzureModelMapperFunc func(model string) string // replace model to azure deployment name func
HTTPClient *http.Client
EmptyMessagesLimit uint
}
func DefaultConfig(authToken string) ClientConfig {
return ClientConfig{
authToken: authToken,
BaseURL: OpenaiAPIURLv1,
APIType: APITypeOpenAI,
OrgID: "",
HTTPClient: &http.Client{},
EmptyMessagesLimit: defaultEmptyMessagesLimit,
}
}
func DefaultAzureConfig(apiKey, baseURL string) ClientConfig {
return ClientConfig{
authToken: apiKey,
BaseURL: baseURL,
OrgID: "",
APIType: APITypeAzure,
APIVersion: "2023-05-15",
AzureModelMapperFunc: func(model string) string {
return regexp.MustCompile(`[.:]`).ReplaceAllString(model, "")
},
HTTPClient: &http.Client{},
EmptyMessagesLimit: defaultEmptyMessagesLimit,
}
}
func CustomConfig(authToken string, apiURL string) ClientConfig {
return ClientConfig{
authToken: authToken,
BaseURL: apiURL,
APIType: APITypeOpenAI,
OrgID: "",
HTTPClient: &http.Client{},
EmptyMessagesLimit: defaultEmptyMessagesLimit,
}
}
func (ClientConfig) String() string {
return "<OpenAI API ClientConfig>"
}
func (c ClientConfig) GetAzureDeploymentByModel(model string) string {
if c.AzureModelMapperFunc != nil {
return c.AzureModelMapperFunc(model)
}
return model
}