mirror of
				https://github.com/songquanpeng/one-api.git
				synced 2025-11-04 15:53:42 +08:00 
			
		
		
		
	feat: update ratio config if missing
This commit is contained in:
		@@ -148,6 +148,26 @@ func init() {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func AddNewMissingRatio(oldRatio string) string {
 | 
				
			||||||
 | 
						newRatio := make(map[string]float64)
 | 
				
			||||||
 | 
						err := json.Unmarshal([]byte(oldRatio), &newRatio)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							logger.SysError("error unmarshalling old ratio: " + err.Error())
 | 
				
			||||||
 | 
							return oldRatio
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for k, v := range DefaultModelRatio {
 | 
				
			||||||
 | 
							if _, ok := newRatio[k]; !ok {
 | 
				
			||||||
 | 
								newRatio[k] = v
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						jsonBytes, err := json.Marshal(newRatio)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							logger.SysError("error marshalling new ratio: " + err.Error())
 | 
				
			||||||
 | 
							return oldRatio
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return string(jsonBytes)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ModelRatio2JSONString() string {
 | 
					func ModelRatio2JSONString() string {
 | 
				
			||||||
	jsonBytes, err := json.Marshal(ModelRatio)
 | 
						jsonBytes, err := json.Marshal(ModelRatio)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										2
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								main.go
									
									
									
									
									
								
							@@ -9,7 +9,6 @@ import (
 | 
				
			|||||||
	"github.com/songquanpeng/one-api/common"
 | 
						"github.com/songquanpeng/one-api/common"
 | 
				
			||||||
	"github.com/songquanpeng/one-api/common/config"
 | 
						"github.com/songquanpeng/one-api/common/config"
 | 
				
			||||||
	"github.com/songquanpeng/one-api/common/logger"
 | 
						"github.com/songquanpeng/one-api/common/logger"
 | 
				
			||||||
	"github.com/songquanpeng/one-api/common/message"
 | 
					 | 
				
			||||||
	"github.com/songquanpeng/one-api/controller"
 | 
						"github.com/songquanpeng/one-api/controller"
 | 
				
			||||||
	"github.com/songquanpeng/one-api/middleware"
 | 
						"github.com/songquanpeng/one-api/middleware"
 | 
				
			||||||
	"github.com/songquanpeng/one-api/model"
 | 
						"github.com/songquanpeng/one-api/model"
 | 
				
			||||||
@@ -88,7 +87,6 @@ func main() {
 | 
				
			|||||||
		logger.SysLog("metric enabled, will disable channel if too much request failed")
 | 
							logger.SysLog("metric enabled, will disable channel if too much request failed")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	openai.InitTokenEncoders()
 | 
						openai.InitTokenEncoders()
 | 
				
			||||||
	_ = message.SendMessage("One API", "", fmt.Sprintf("One API %s started", common.Version))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Initialize HTTP server
 | 
						// Initialize HTTP server
 | 
				
			||||||
	server := gin.New()
 | 
						server := gin.New()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -81,6 +81,9 @@ func InitOptionMap() {
 | 
				
			|||||||
func loadOptionsFromDatabase() {
 | 
					func loadOptionsFromDatabase() {
 | 
				
			||||||
	options, _ := AllOption()
 | 
						options, _ := AllOption()
 | 
				
			||||||
	for _, option := range options {
 | 
						for _, option := range options {
 | 
				
			||||||
 | 
							if option.Key == "ModelRatio" {
 | 
				
			||||||
 | 
								option.Value = common.AddNewMissingRatio(option.Value)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
		err := updateOptionMap(option.Key, option.Value)
 | 
							err := updateOptionMap(option.Key, option.Value)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			logger.SysError("failed to update option map: " + err.Error())
 | 
								logger.SysError("failed to update option map: " + err.Error())
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user