-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwire.go
131 lines (115 loc) · 3.67 KB
/
wire.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
//go:build wireinject
// +build wireinject
package main
import (
"fmt"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/google/wire"
"github.com/huweiATgithub/chatgpt-apiserver/apiserver"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"log"
"strings"
)
func Initialize() *Server {
wire.Build(NewServer, NewControllers, NewWeights, ProviderConfig, NewPool, ProviderPoolConfig, ProviderControllerConfigs)
return &Server{}
}
func NewControllers(configs []ControllerConfig) []apiserver.Controller {
controllers := make([]apiserver.Controller, len(configs))
for i, config := range configs {
controller, err := ControllerFactories[config.Type](&config)
if err != nil {
log.Printf("In constructing controller: Config %v with error %s", config, err)
continue
}
controllers[i] = controller
}
return controllers
}
func NewWeights(configs []ControllerConfig) []int {
weights := make([]int, len(configs))
for i, config := range configs {
weights[i] = config.Weight
if weights[i] == 0 {
weights[i] = defaultWeight
}
}
return weights
}
func NewPool(config *PoolConfig, controllers []apiserver.Controller, weights []int) apiserver.ControllersPool {
validControllers := make([]apiserver.Controller, 0, len(controllers))
validWeights := make([]int, 0, len(weights))
for i := 0; i < len(controllers); i++ {
if controllers[i] != nil {
validControllers = append(validControllers, controllers[i])
validWeights = append(validWeights, weights[i])
}
}
log.Printf("%v controllers are available for the pool %s\n", len(validControllers), config.Type)
return apiserver.PoolFactories[config.Type](validControllers, validWeights)
}
func defaultConfig() {
viper.SetDefault("port", "8080")
viper.SetDefault("pool.type", "balanced")
}
func ProviderConfig() *Config {
appName := "chatgpt-apiserver"
viper.SetConfigName(appName)
viper.SetEnvPrefix(appName)
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
// Command-line or Env provided config file / path
pflag.String("config_file", "", "path to the server config file")
pflag.String("config_path", "", "path to the server config file")
pflag.Parse()
err := viper.BindPFlags(pflag.CommandLine)
if err != nil {
panic(fmt.Errorf("fatal error binding pflags: %s", err))
}
viper.MustBindEnv("config_file", "CONFIG_FILE")
viper.MustBindEnv("config_path", "CONFIG_PATH")
viper.AutomaticEnv()
if viper.GetString("config_file") != "" {
viper.SetConfigFile(viper.GetString("config_file")) // it has the highest priority if set
}
if viper.GetString("config_path") != "" {
viper.AddConfigPath(viper.GetString("config_path")) // earlier path has higher priority
}
// Other Paths
viper.AddConfigPath(".")
viper.AddConfigPath("config")
viper.AddConfigPath(fmt.Sprintf("/etc/%s/", appName))
// Default Values
defaultConfig()
// Read Config
err = viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("fatal error reading config file: %s", err))
}
viper.AutomaticEnv() // Environment variables have higher priority
log.Printf("Loading config file %s", viper.ConfigFileUsed())
// Unmarshal Config
var config Config
if viper.Unmarshal(&config) != nil {
panic(fmt.Errorf("fatal error parsing config file: %s", err))
}
return &config
}
func ProviderPoolConfig(config *Config) *PoolConfig {
return &config.Pool
}
func ProviderControllerConfigs(config *Config) []ControllerConfig {
return config.Controllers
}
func NewServer(pool apiserver.ControllersPool, config *Config) *Server {
r := gin.New()
r.Use(gin.Logger(), gin.Recovery())
r.Use(cors.Default())
r.GET("/status", statusOK)
r.POST("/v1/chat/completions", apiserver.CompleteChat(pool))
return &Server{
r: r,
config: config,
}
}