-
Notifications
You must be signed in to change notification settings - Fork 58
/
provider_setting.go
166 lines (131 loc) · 4.76 KB
/
provider_setting.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package manager
import (
"fmt"
"strings"
"time"
internal_errors "github.com/bricks-cloud/bricksllm/internal/errors"
"github.com/bricks-cloud/bricksllm/internal/provider"
"github.com/bricks-cloud/bricksllm/internal/provider/custom"
"github.com/bricks-cloud/bricksllm/internal/util"
)
type ProviderSettingsStorage interface {
UpdateProviderSetting(id string, setting *provider.UpdateSetting) (*provider.Setting, error)
CreateProviderSetting(setting *provider.Setting) (*provider.Setting, error)
GetProviderSetting(id string, withSecret bool) (*provider.Setting, error)
GetCustomProviderByName(name string) (*custom.Provider, error)
GetProviderSettings(withSecret bool, ids []string) ([]*provider.Setting, error)
}
type ProviderSettingsMemStorage interface {
GetSetting(id string) *provider.Setting
GetSettings(ids []string) []*provider.Setting
}
type ProviderSettingsManager struct {
Storage ProviderSettingsStorage
MemDb ProviderSettingsMemStorage
}
func NewProviderSettingsManager(s ProviderSettingsStorage, memdb ProviderSettingsMemStorage) *ProviderSettingsManager {
return &ProviderSettingsManager{
Storage: s,
MemDb: memdb,
}
}
func isProviderNativelySupported(provider string) bool {
return provider == "openai" || provider == "anthropic" || provider == "azure" || provider == "vllm" || provider == "deepinfra"
}
func findMissingAuthParams(providerName string, params map[string]string) string {
missingFields := []string{}
if providerName == "openai" || providerName == "anthropic" || providerName == "deepinfra" {
val := params["apikey"]
if len(val) == 0 {
missingFields = append(missingFields, "apikey")
}
return strings.Join(missingFields, " ,")
}
if providerName == "azure" {
val := params["resourceName"]
if len(val) == 0 {
missingFields = append(missingFields, "resourceName")
}
val = params["apikey"]
if len(val) == 0 {
missingFields = append(missingFields, "apikey")
}
}
if providerName == "vllm" {
val := params["url"]
if len(val) == 0 {
missingFields = append(missingFields, "url")
}
}
return strings.Join(missingFields, ",")
}
func (m *ProviderSettingsManager) validateSettings(providerName string, setting map[string]string) error {
if !isProviderNativelySupported(providerName) {
provider, err := m.Storage.GetCustomProviderByName(providerName)
_, ok := err.(notFoundError)
if ok {
return internal_errors.NewValidationError(fmt.Sprintf("provider %s is not supported", providerName))
}
if len(provider.AuthenticationParam) != 0 {
val := setting[provider.AuthenticationParam]
if len(val) == 0 {
return internal_errors.NewValidationError(fmt.Sprintf("provider %s is missing value for field %s", providerName, provider.AuthenticationParam))
}
}
}
missing := findMissingAuthParams(providerName, setting)
if len(missing) != 0 {
return internal_errors.NewValidationError(fmt.Sprintf("provider %s is missing fields %s", providerName, missing))
}
return nil
}
func (m *ProviderSettingsManager) CreateSetting(setting *provider.Setting) (*provider.Setting, error) {
if len(setting.Provider) == 0 {
return nil, internal_errors.NewValidationError("provider field cannot be empty")
}
if err := m.validateSettings(setting.Provider, setting.Setting); err != nil {
return nil, err
}
setting.Id = util.NewUuid()
setting.CreatedAt = time.Now().Unix()
setting.UpdatedAt = time.Now().Unix()
return m.Storage.CreateProviderSetting(setting)
}
func (m *ProviderSettingsManager) UpdateSetting(id string, setting *provider.UpdateSetting) (*provider.Setting, error) {
if len(id) == 0 {
return nil, internal_errors.NewValidationError("id cannot be empty")
}
existing, _ := m.Storage.GetProviderSetting(id, true)
if existing == nil {
return nil, internal_errors.NewNotFoundError("provider setting is not found")
}
if len(setting.Setting) != 0 {
if err := m.validateSettings(existing.Provider, setting.Setting); err != nil {
return nil, err
}
merged := existing.Setting
for k, v := range setting.Setting {
merged[k] = v
}
setting.Setting = merged
}
setting.UpdatedAt = time.Now().Unix()
return m.Storage.UpdateProviderSetting(id, setting)
}
func (m *ProviderSettingsManager) GetSettingFromDb(id string) (*provider.Setting, error) {
return m.Storage.GetProviderSetting(id, true)
}
func (m *ProviderSettingsManager) GetSetting(id string) (*provider.Setting, error) {
setting := m.MemDb.GetSetting(id)
if setting == nil {
return nil, internal_errors.NewNotFoundError("provider setting is not found")
}
return setting, nil
}
func (m *ProviderSettingsManager) GetSettings(ids []string) ([]*provider.Setting, error) {
settings, err := m.Storage.GetProviderSettings(false, ids)
if err != nil {
return nil, internal_errors.NewNotFoundError("provider setting is not found")
}
return settings, nil
}