mirror of
https://github.com/linux-do/new-api.git
synced 2025-11-12 00:53:41 +08:00
chore: 重构
This commit is contained in:
@@ -103,14 +103,14 @@ var IsMasterNode = os.Getenv("NODE_TYPE") != "slave"
|
|||||||
var requestInterval, _ = strconv.Atoi(os.Getenv("POLLING_INTERVAL"))
|
var requestInterval, _ = strconv.Atoi(os.Getenv("POLLING_INTERVAL"))
|
||||||
var RequestInterval = time.Duration(requestInterval) * time.Second
|
var RequestInterval = time.Duration(requestInterval) * time.Second
|
||||||
|
|
||||||
var SyncFrequency = GetOrDefault("SYNC_FREQUENCY", 60) // unit is second
|
var SyncFrequency = GetEnvOrDefault("SYNC_FREQUENCY", 60) // unit is second
|
||||||
|
|
||||||
var BatchUpdateEnabled = false
|
var BatchUpdateEnabled = false
|
||||||
var BatchUpdateInterval = GetOrDefault("BATCH_UPDATE_INTERVAL", 5)
|
var BatchUpdateInterval = GetEnvOrDefault("BATCH_UPDATE_INTERVAL", 5)
|
||||||
|
|
||||||
var RelayTimeout = GetOrDefault("RELAY_TIMEOUT", 0) // unit is second
|
var RelayTimeout = GetEnvOrDefault("RELAY_TIMEOUT", 0) // unit is second
|
||||||
|
|
||||||
var GeminiSafetySetting = GetOrDefaultString("GEMINI_SAFETY_SETTING", "BLOCK_NONE")
|
var GeminiSafetySetting = GetEnvOrDefaultString("GEMINI_SAFETY_SETTING", "BLOCK_NONE")
|
||||||
|
|
||||||
const (
|
const (
|
||||||
RequestIdKey = "X-Oneapi-Request-Id"
|
RequestIdKey = "X-Oneapi-Request-Id"
|
||||||
@@ -133,10 +133,10 @@ var (
|
|||||||
// All duration's unit is seconds
|
// All duration's unit is seconds
|
||||||
// Shouldn't larger then RateLimitKeyExpirationDuration
|
// Shouldn't larger then RateLimitKeyExpirationDuration
|
||||||
var (
|
var (
|
||||||
GlobalApiRateLimitNum = GetOrDefault("GLOBAL_API_RATE_LIMIT", 180)
|
GlobalApiRateLimitNum = GetEnvOrDefault("GLOBAL_API_RATE_LIMIT", 180)
|
||||||
GlobalApiRateLimitDuration int64 = 3 * 60
|
GlobalApiRateLimitDuration int64 = 3 * 60
|
||||||
|
|
||||||
GlobalWebRateLimitNum = GetOrDefault("GLOBAL_WEB_RATE_LIMIT", 60)
|
GlobalWebRateLimitNum = GetEnvOrDefault("GLOBAL_WEB_RATE_LIMIT", 60)
|
||||||
GlobalWebRateLimitDuration int64 = 3 * 60
|
GlobalWebRateLimitDuration int64 = 3 * 60
|
||||||
|
|
||||||
UploadRateLimitNum = 10
|
UploadRateLimitNum = 10
|
||||||
|
|||||||
26
common/env.go
Normal file
26
common/env.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetEnvOrDefault(env string, defaultValue int) int {
|
||||||
|
if env == "" || os.Getenv(env) == "" {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
num, err := strconv.Atoi(os.Getenv(env))
|
||||||
|
if err != nil {
|
||||||
|
SysError(fmt.Sprintf("failed to parse %s: %s, using default value: %d", env, err.Error(), defaultValue))
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
return num
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetEnvOrDefaultString(env string, defaultValue string) string {
|
||||||
|
if env == "" || os.Getenv(env) == "" {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
return os.Getenv(env)
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import "encoding/json"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
var GroupRatio = map[string]float64{
|
var GroupRatio = map[string]float64{
|
||||||
"default": 1,
|
"default": 1,
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import "encoding/json"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
var TopupGroupRatio = map[string]float64{
|
var TopupGroupRatio = map[string]float64{
|
||||||
"default": 1,
|
"default": 1,
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -191,25 +190,6 @@ func Max(a int, b int) int {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOrDefault(env string, defaultValue int) int {
|
|
||||||
if env == "" || os.Getenv(env) == "" {
|
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
num, err := strconv.Atoi(os.Getenv(env))
|
|
||||||
if err != nil {
|
|
||||||
SysError(fmt.Sprintf("failed to parse %s: %s, using default value: %d", env, err.Error(), defaultValue))
|
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
return num
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetOrDefaultString(env string, defaultValue string) string {
|
|
||||||
if env == "" || os.Getenv(env) == "" {
|
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
return os.Getenv(env)
|
|
||||||
}
|
|
||||||
|
|
||||||
func MessageWithRequestId(message string, id string) string {
|
func MessageWithRequestId(message string, id string) string {
|
||||||
return fmt.Sprintf("%s (request id: %s)", message, id)
|
return fmt.Sprintf("%s (request id: %s)", message, id)
|
||||||
}
|
}
|
||||||
|
|||||||
7
constant/env.go
Normal file
7
constant/env.go
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package constant
|
||||||
|
|
||||||
|
import (
|
||||||
|
"one-api/common"
|
||||||
|
)
|
||||||
|
|
||||||
|
var StreamingTimeout = common.GetEnvOrDefault("STREAMING_TIMEOUT", 30)
|
||||||
@@ -1,13 +1,9 @@
|
|||||||
package constant
|
package constant
|
||||||
|
|
||||||
import "one-api/common"
|
|
||||||
|
|
||||||
var ServerAddress = "http://localhost:3000"
|
var ServerAddress = "http://localhost:3000"
|
||||||
var WorkerUrl = ""
|
var WorkerUrl = ""
|
||||||
var WorkerValidKey = ""
|
var WorkerValidKey = ""
|
||||||
|
|
||||||
var StreamingTimeout = common.GetOrDefault("STREAMING_TIMEOUT", 30)
|
|
||||||
|
|
||||||
func EnableWorker() bool {
|
func EnableWorker() bool {
|
||||||
return WorkerUrl != ""
|
return WorkerUrl != ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,11 +5,10 @@ import (
|
|||||||
"github.com/Calcium-Ion/go-epay/epay"
|
"github.com/Calcium-Ion/go-epay/epay"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
"one-api/constant"
|
|
||||||
|
|
||||||
"log"
|
"log"
|
||||||
"net/url"
|
"net/url"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
|
"one-api/constant"
|
||||||
"one-api/model"
|
"one-api/model"
|
||||||
"one-api/service"
|
"one-api/service"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|||||||
@@ -24,14 +24,3 @@ type OpenAIModels struct {
|
|||||||
Root string `json:"root"`
|
Root string `json:"root"`
|
||||||
Parent *string `json:"parent"`
|
Parent *string `json:"parent"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type ModelPricing struct {
|
|
||||||
Available bool `json:"available"`
|
|
||||||
ModelName string `json:"model_name"`
|
|
||||||
QuotaType int `json:"quota_type"`
|
|
||||||
ModelRatio float64 `json:"model_ratio"`
|
|
||||||
ModelPrice float64 `json:"model_price"`
|
|
||||||
OwnerBy string `json:"owner_by"`
|
|
||||||
CompletionRatio float64 `json:"completion_ratio"`
|
|
||||||
EnableGroup []string `json:"enable_group,omitempty"`
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -86,9 +86,9 @@ func InitDB() (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
sqlDB.SetMaxIdleConns(common.GetOrDefault("SQL_MAX_IDLE_CONNS", 100))
|
sqlDB.SetMaxIdleConns(common.GetEnvOrDefault("SQL_MAX_IDLE_CONNS", 100))
|
||||||
sqlDB.SetMaxOpenConns(common.GetOrDefault("SQL_MAX_OPEN_CONNS", 1000))
|
sqlDB.SetMaxOpenConns(common.GetEnvOrDefault("SQL_MAX_OPEN_CONNS", 1000))
|
||||||
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetOrDefault("SQL_MAX_LIFETIME", 60)))
|
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetEnvOrDefault("SQL_MAX_LIFETIME", 60)))
|
||||||
|
|
||||||
if !common.IsMasterNode {
|
if !common.IsMasterNode {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -2,18 +2,28 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
"one-api/dto"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Pricing struct {
|
||||||
|
Available bool `json:"available"`
|
||||||
|
ModelName string `json:"model_name"`
|
||||||
|
QuotaType int `json:"quota_type"`
|
||||||
|
ModelRatio float64 `json:"model_ratio"`
|
||||||
|
ModelPrice float64 `json:"model_price"`
|
||||||
|
OwnerBy string `json:"owner_by"`
|
||||||
|
CompletionRatio float64 `json:"completion_ratio"`
|
||||||
|
EnableGroup []string `json:"enable_group,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
pricingMap []dto.ModelPricing
|
pricingMap []Pricing
|
||||||
lastGetPricingTime time.Time
|
lastGetPricingTime time.Time
|
||||||
updatePricingLock sync.Mutex
|
updatePricingLock sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetPricing(group string) []dto.ModelPricing {
|
func GetPricing(group string) []Pricing {
|
||||||
updatePricingLock.Lock()
|
updatePricingLock.Lock()
|
||||||
defer updatePricingLock.Unlock()
|
defer updatePricingLock.Unlock()
|
||||||
|
|
||||||
@@ -21,7 +31,7 @@ func GetPricing(group string) []dto.ModelPricing {
|
|||||||
updatePricing()
|
updatePricing()
|
||||||
}
|
}
|
||||||
if group != "" {
|
if group != "" {
|
||||||
userPricingMap := make([]dto.ModelPricing, 0)
|
userPricingMap := make([]Pricing, 0)
|
||||||
models := GetGroupModels(group)
|
models := GetGroupModels(group)
|
||||||
for _, pricing := range pricingMap {
|
for _, pricing := range pricingMap {
|
||||||
if !common.StringsContains(models, pricing.ModelName) {
|
if !common.StringsContains(models, pricing.ModelName) {
|
||||||
@@ -42,9 +52,9 @@ func updatePricing() {
|
|||||||
allModels[model] = i
|
allModels[model] = i
|
||||||
}
|
}
|
||||||
|
|
||||||
pricingMap = make([]dto.ModelPricing, 0)
|
pricingMap = make([]Pricing, 0)
|
||||||
for model, _ := range allModels {
|
for model, _ := range allModels {
|
||||||
pricing := dto.ModelPricing{
|
pricing := Pricing{
|
||||||
Available: true,
|
Available: true,
|
||||||
ModelName: model,
|
ModelName: model,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package service
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"one-api/common"
|
|
||||||
"one-api/constant"
|
"one-api/constant"
|
||||||
"one-api/dto"
|
"one-api/dto"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -62,7 +61,7 @@ func SensitiveWordContains(text string) (bool, []string) {
|
|||||||
}
|
}
|
||||||
checkText := strings.ToLower(text)
|
checkText := strings.ToLower(text)
|
||||||
// 构建一个AC自动机
|
// 构建一个AC自动机
|
||||||
m := common.InitAc()
|
m := InitAc()
|
||||||
hits := m.MultiPatternSearch([]rune(checkText), false)
|
hits := m.MultiPatternSearch([]rune(checkText), false)
|
||||||
if len(hits) > 0 {
|
if len(hits) > 0 {
|
||||||
words := make([]string, 0)
|
words := make([]string, 0)
|
||||||
@@ -80,7 +79,7 @@ func SensitiveWordReplace(text string, returnImmediately bool) (bool, []string,
|
|||||||
return false, nil, text
|
return false, nil, text
|
||||||
}
|
}
|
||||||
checkText := strings.ToLower(text)
|
checkText := strings.ToLower(text)
|
||||||
m := common.InitAc()
|
m := InitAc()
|
||||||
hits := m.MultiPatternSearch([]rune(checkText), returnImmediately)
|
hits := m.MultiPatternSearch([]rune(checkText), returnImmediately)
|
||||||
if len(hits) > 0 {
|
if len(hits) > 0 {
|
||||||
words := make([]string, 0)
|
words := make([]string, 0)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package common
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
Reference in New Issue
Block a user