Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
eca52104
authored
Jun 27, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: 重构
parent
fce31dd1
Show whitespace changes
Inline
Side-by-side
Showing
13 changed files
with
68 additions
and
58 deletions
+68
-58
common/constants.go
+6
-6
common/env.go
+26
-0
common/group-ratio.go
+3
-1
common/topup-ratio.go
+3
-1
common/utils.go
+0
-20
constant/env.go
+7
-0
constant/system-setting.go
+0
-4
controller/topup.go
+1
-2
dto/pricing.go
+0
-11
model/main.go
+3
-3
model/pricing.go
+16
-6
service/sensitive.go
+2
-3
service/str.go
+1
-1
No files found.
common/constants.go
View file @
eca52104
...
@@ -103,14 +103,14 @@ var IsMasterNode = os.Getenv("NODE_TYPE") != "slave"
...
@@ -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
=
Get
Env
OrDefault
(
"SYNC_FREQUENCY"
,
60
)
// unit is second
var
BatchUpdateEnabled
=
false
var
BatchUpdateEnabled
=
false
var
BatchUpdateInterval
=
GetOrDefault
(
"BATCH_UPDATE_INTERVAL"
,
5
)
var
BatchUpdateInterval
=
Get
Env
OrDefault
(
"BATCH_UPDATE_INTERVAL"
,
5
)
var
RelayTimeout
=
GetOrDefault
(
"RELAY_TIMEOUT"
,
0
)
// unit is second
var
RelayTimeout
=
Get
Env
OrDefault
(
"RELAY_TIMEOUT"
,
0
)
// unit is second
var
GeminiSafetySetting
=
GetOrDefaultString
(
"GEMINI_SAFETY_SETTING"
,
"BLOCK_NONE"
)
var
GeminiSafetySetting
=
Get
Env
OrDefaultString
(
"GEMINI_SAFETY_SETTING"
,
"BLOCK_NONE"
)
const
(
const
(
RequestIdKey
=
"X-Oneapi-Request-Id"
RequestIdKey
=
"X-Oneapi-Request-Id"
...
@@ -133,10 +133,10 @@ var (
...
@@ -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
=
Get
Env
OrDefault
(
"GLOBAL_API_RATE_LIMIT"
,
180
)
GlobalApiRateLimitDuration
int64
=
3
*
60
GlobalApiRateLimitDuration
int64
=
3
*
60
GlobalWebRateLimitNum
=
GetOrDefault
(
"GLOBAL_WEB_RATE_LIMIT"
,
60
)
GlobalWebRateLimitNum
=
Get
Env
OrDefault
(
"GLOBAL_WEB_RATE_LIMIT"
,
60
)
GlobalWebRateLimitDuration
int64
=
3
*
60
GlobalWebRateLimitDuration
int64
=
3
*
60
UploadRateLimitNum
=
10
UploadRateLimitNum
=
10
...
...
common/env.go
0 → 100644
View file @
eca52104
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
)
}
common/group-ratio.go
View file @
eca52104
package
common
package
common
import
"encoding/json"
import
(
"encoding/json"
)
var
GroupRatio
=
map
[
string
]
float64
{
var
GroupRatio
=
map
[
string
]
float64
{
"default"
:
1
,
"default"
:
1
,
...
...
common/topup-ratio.go
View file @
eca52104
package
common
package
common
import
"encoding/json"
import
(
"encoding/json"
)
var
TopupGroupRatio
=
map
[
string
]
float64
{
var
TopupGroupRatio
=
map
[
string
]
float64
{
"default"
:
1
,
"default"
:
1
,
...
...
common/utils.go
View file @
eca52104
...
@@ -8,7 +8,6 @@ import (
...
@@ -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 {
...
@@ -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
)
}
}
...
...
constant/env.go
0 → 100644
View file @
eca52104
package
constant
import
(
"one-api/common"
)
var
StreamingTimeout
=
common
.
GetEnvOrDefault
(
"STREAMING_TIMEOUT"
,
30
)
constant/system-setting.go
View file @
eca52104
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
!=
""
}
}
controller/topup.go
View file @
eca52104
...
@@ -5,11 +5,10 @@ import (
...
@@ -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"
...
...
dto/pricing.go
View file @
eca52104
...
@@ -24,14 +24,3 @@ type OpenAIModels struct {
...
@@ -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"`
}
model/main.go
View file @
eca52104
...
@@ -86,9 +86,9 @@ func InitDB() (err error) {
...
@@ -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
.
Get
Env
OrDefault
(
"SQL_MAX_IDLE_CONNS"
,
100
))
sqlDB
.
SetMaxOpenConns
(
common
.
GetOrDefault
(
"SQL_MAX_OPEN_CONNS"
,
1000
))
sqlDB
.
SetMaxOpenConns
(
common
.
Get
Env
OrDefault
(
"SQL_MAX_OPEN_CONNS"
,
1000
))
sqlDB
.
SetConnMaxLifetime
(
time
.
Second
*
time
.
Duration
(
common
.
GetOrDefault
(
"SQL_MAX_LIFETIME"
,
60
)))
sqlDB
.
SetConnMaxLifetime
(
time
.
Second
*
time
.
Duration
(
common
.
Get
Env
OrDefault
(
"SQL_MAX_LIFETIME"
,
60
)))
if
!
common
.
IsMasterNode
{
if
!
common
.
IsMasterNode
{
return
nil
return
nil
...
...
model/pricing.go
View file @
eca52104
...
@@ -2,18 +2,28 @@ package model
...
@@ -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
.
Model
Pricing
pricingMap
[]
Pricing
lastGetPricingTime
time
.
Time
lastGetPricingTime
time
.
Time
updatePricingLock
sync
.
Mutex
updatePricingLock
sync
.
Mutex
)
)
func
GetPricing
(
group
string
)
[]
dto
.
Model
Pricing
{
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 {
...
@@ -21,7 +31,7 @@ func GetPricing(group string) []dto.ModelPricing {
updatePricing
()
updatePricing
()
}
}
if
group
!=
""
{
if
group
!=
""
{
userPricingMap
:=
make
([]
dto
.
Model
Pricing
,
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() {
...
@@ -42,9 +52,9 @@ func updatePricing() {
allModels
[
model
]
=
i
allModels
[
model
]
=
i
}
}
pricingMap
=
make
([]
dto
.
Model
Pricing
,
0
)
pricingMap
=
make
([]
Pricing
,
0
)
for
model
,
_
:=
range
allModels
{
for
model
,
_
:=
range
allModels
{
pricing
:=
dto
.
Model
Pricing
{
pricing
:=
Pricing
{
Available
:
true
,
Available
:
true
,
ModelName
:
model
,
ModelName
:
model
,
}
}
...
...
service/sensitive.go
View file @
eca52104
...
@@ -3,7 +3,6 @@ package service
...
@@ -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) {
...
@@ -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,
...
@@ -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
)
...
...
common
/str.go
→
service
/str.go
View file @
eca52104
package
common
package
service
import
(
import
(
"bytes"
"bytes"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment