-
Notifications
You must be signed in to change notification settings - Fork 64
/
config.go
425 lines (338 loc) · 9.6 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package config
import (
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/bricks-cloud/bricksllm/internal/util"
"gopkg.in/yaml.v3"
)
type RateLimitConfig struct {
Count string `yaml:"count"`
Interval string `yaml:"interval"`
}
type Protocol string
const (
Http Protocol = "http"
Https Protocol = "https"
)
func (p Protocol) Valid() bool {
if p != Http && p != Https {
return false
}
return true
}
type Provider string
const (
OpenaiProvider Provider = "openai"
)
func (p Provider) Valid() bool {
if p != OpenaiProvider {
return false
}
return true
}
type ApiLoggerConfig struct {
HideIp bool `yaml:"hide_ip"`
HideHeaders bool `yaml:"hide_headers"`
}
func (alc *ApiLoggerConfig) GetHideIp() bool {
if alc == nil {
return false
}
return alc.HideIp
}
func (alc *ApiLoggerConfig) GetHideHeaders() bool {
if alc == nil {
return false
}
return alc.HideHeaders
}
type LlmLoggerConfig struct {
HideHeaders bool `yaml:"hide_headers"`
HideResponseContent bool `yaml:"hide_response_content"`
HidePromptContent bool `yaml:"hide_prompt_content"`
}
func (llc *LlmLoggerConfig) GetHideResponseContent() bool {
if llc == nil {
return false
}
return llc.HideResponseContent
}
func (llc *LlmLoggerConfig) GetHidePromptContent() bool {
if llc == nil {
return false
}
return llc.HidePromptContent
}
func (llc *LlmLoggerConfig) GetHideHeaders() bool {
if llc == nil {
return false
}
return llc.HideHeaders
}
type LoggerConfig struct {
Api *ApiLoggerConfig `yaml:"api"`
Llm *LlmLoggerConfig `yaml:"llm"`
}
type ServerConfig struct {
Port int `yaml:"port"`
}
type DataType string
const (
StringDataType DataType = "string"
NumberDataType DataType = "number"
ArrayDataType DataType = "array"
ObjectDataType DataType = "object"
BooleanDataType DataType = "boolean"
)
func (d DataType) Valid() bool {
if d != StringDataType && d != NumberDataType && d != ArrayDataType && d != ObjectDataType && d != BooleanDataType {
return false
}
return true
}
type InputValue struct {
DataType DataType `yaml:"type"`
Properties map[string]interface{} `yaml:"properties"`
Items interface{} `yaml:"items"`
}
type CorsConfig struct {
AllowedOrgins []string `yaml:"allowed_origins"`
AllowedCredentials bool `yaml:"allowed_credentials"`
}
func (cc *CorsConfig) Enabled() bool {
return cc != nil
}
func (cc *CorsConfig) GetAllowedOrigins() []string {
return cc.AllowedOrgins
}
func (cc *CorsConfig) GetAllowedCredentials() bool {
return cc.AllowedCredentials
}
type KeyAuthConfig struct {
Key string `yaml:"key"`
}
func (kac *KeyAuthConfig) Enabled() bool {
return kac != nil
}
func (kac *KeyAuthConfig) GetKey() string {
return kac.Key
}
type RouteConfig struct {
Path string `yaml:"path"`
CorsConfig *CorsConfig `yaml:"cors"`
Input map[string]InputValue `yaml:"input"`
Provider Provider `yaml:"provider"`
OpenAiConfig *OpenAiRouteConfig `yaml:"openai_config"`
Description string `yaml:"description"`
Protocol Protocol `yaml:"protocol"`
CertFile string `yaml:"cert_file"`
KeyFile string `yaml:"key_file"`
KeyAuthConfig *KeyAuthConfig `yaml:"key_auth"`
UpstreamSendTimeout time.Duration `yaml:"upstream_send_time"`
}
type OpenAiMessageRole string
const (
SystemMessageRole OpenAiMessageRole = "system"
UserMessageRole OpenAiMessageRole = "user"
AssitantMessageRole OpenAiMessageRole = "assistant"
FunctionMessageRole OpenAiMessageRole = "function"
)
func (r OpenAiMessageRole) Valid() bool {
if r != SystemMessageRole && r != UserMessageRole && r != AssitantMessageRole && r != FunctionMessageRole {
return false
}
return true
}
type OpenAiPrompt struct {
Role OpenAiMessageRole `yaml:"role"`
Content string `yaml:"content"`
}
type OpenAiModel string
const (
Gpt35Turbo OpenAiModel = "gpt-3.5-turbo"
Gpt35Turbo16k OpenAiModel = "gpt-3.5-turbo-16k"
Gpt35Turbo0613 OpenAiModel = "gpt-3.5-turbo-0613"
Gpt35Turbo16k0613 OpenAiModel = "gpt-3.5-turbo-16k-0613"
Gpt4 OpenAiModel = "gpt-4"
Gpt40613 OpenAiModel = "gpt-4-0613"
Gpt432k OpenAiModel = "gpt-4-32k"
Gpt432k0613 OpenAiModel = "gpt-4-32k-0613"
)
func (m OpenAiModel) Valid() bool {
if m != Gpt35Turbo && m != Gpt35Turbo16k && m != Gpt35Turbo0613 && m != Gpt35Turbo16k0613 && m != Gpt4 && m != Gpt40613 && m != Gpt432k && m != Gpt432k0613 {
return false
}
return true
}
type OpenAiRouteConfig struct {
ApiCredential string `yaml:"api_credential"`
Model OpenAiModel `yaml:"model"`
Prompts []*OpenAiPrompt `yaml:"prompts"`
}
type OpenAiConfig struct {
ApiCredential string `yaml:"api_credential"`
}
type Config struct {
LoggerConfig *LoggerConfig `yaml:"logger"`
Routes []*RouteConfig `yaml:"routes"`
Server *ServerConfig `yaml:"server"`
OpenAiConfig *OpenAiConfig `yaml:"openai"`
}
func NewConfig(filePath string) (*Config, error) {
yamlFile, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("unable to read config file with path %s: %w", filePath, err)
}
yamlFile = []byte(os.ExpandEnv(string(yamlFile)))
c := &Config{}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal yaml file with path %s: %w", filePath, err)
}
// default server port to 8080
if c.Server == nil {
c.Server = &ServerConfig{
Port: 8080,
}
}
// routes have to be configured
if len(c.Routes) == 0 {
return nil, fmt.Errorf("routes are not configured in config file %s", filePath)
}
// default server port to 8080
if c.LoggerConfig == nil {
c.LoggerConfig = &LoggerConfig{
Api: &ApiLoggerConfig{
HideIp: false,
HideHeaders: false,
},
Llm: &LlmLoggerConfig{
HideHeaders: false,
HideResponseContent: false,
HidePromptContent: false,
},
}
}
apiCredentialConfigured := false
if c.OpenAiConfig != nil && len(c.OpenAiConfig.ApiCredential) != 0 {
apiCredentialConfigured = true
}
for _, route := range c.Routes {
err = parseRouteConfig(route, apiCredentialConfigured)
if err != nil {
return nil, err
}
}
return c, nil
}
func parseRouteConfig(rc *RouteConfig, isOpenAiConfigured bool) error {
if len(rc.Path) == 0 {
return errors.New("path is empty")
}
if len(rc.Provider) == 0 {
return errors.New("provider is empty")
}
if !rc.Provider.Valid() {
return errors.New("provider must be openai")
}
if rc.CorsConfig != nil {
if len(rc.CorsConfig.AllowedOrgins) == 0 {
return fmt.Errorf("cors config is present but allowed_origins is not specified for route: %s", rc.Path)
}
}
if rc.KeyAuthConfig != nil {
if len(rc.KeyAuthConfig.Key) == 0 {
return fmt.Errorf("key_auth config is present but key is not specified for route: %s", rc.Path)
}
}
if rc.Provider == OpenaiProvider {
if rc.OpenAiConfig == nil {
return errors.New("openai config is not provided")
}
if len(rc.OpenAiConfig.Model) == 0 {
return errors.New("openai model cannot be empty")
}
if !rc.OpenAiConfig.Model.Valid() {
return errors.New("openai model must be of gpt-3.5-turbo, gpt-3.5-turbo-16k, gpt-3.5-turbo-0613, gpt-3.5-turbo-16k-0613, gpt-4, gpt-4-0613, gpt-4-32k and gpt-4-32k-0613")
}
for _, prompt := range rc.OpenAiConfig.Prompts {
if len(prompt.Role) == 0 {
return errors.New("role cannot be empty in openai prompt")
}
if !prompt.Role.Valid() {
return errors.New("role must be of user, system, assistant or function")
}
if !isOpenAiConfigured && len(rc.OpenAiConfig.ApiCredential) == 0 {
return errors.New("openai api credential is not configrued")
}
if len(prompt.Content) == 0 {
return errors.New("content is not provided in openai prompt")
}
variableMap := util.GetVariableMap(prompt.Content)
err := validateInput(rc.Input, variableMap)
if err != nil {
return err
}
}
}
if rc.Protocol == Https {
if len(rc.CertFile) == 0 {
return errors.New("cert file is not provided for https protocol")
}
if len(rc.KeyFile) == 0 {
return errors.New("key file is not provided for https protocol")
}
}
// defaut route protocol to http
if len(rc.Protocol) == 0 {
rc.Protocol = Http
}
if !rc.Protocol.Valid() {
return errors.New("protocol must be of http or https")
}
return nil
}
func validateInput(input map[string]InputValue, variableMap map[string]string) error {
if len(variableMap) == 0 {
return nil
}
for _, reference := range variableMap {
parts := strings.Split(reference, ".")
if len(parts) == 0 {
return errors.New("no references found inside `{{ }}` syntax")
}
if len(parts) == 1 {
if _, found := input[parts[0]]; found {
continue
}
return errors.New("referenced value in prompt is not defined in input")
}
innerInput := input
for index, part := range parts {
value, found := innerInput[part]
if !found {
return fmt.Errorf("referenced var: %s in prompt does not exist", value)
}
if index != len(parts)-1 && value.DataType != ObjectDataType {
return fmt.Errorf("input value is not represented as object for referenced var: %s", reference)
}
if value.DataType == ObjectDataType && len(value.Properties) == 0 {
return fmt.Errorf("object properties is empty for referenced var: %s", reference)
}
js, err := json.Marshal(value.Properties)
if err != nil {
return err
}
innerInput = map[string]InputValue{}
err = json.Unmarshal(js, &innerInput)
if err != nil {
return err
}
}
}
return nil
}