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
d43751f2
authored
Oct 12, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: support free model setting
parent
a781622d
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
89 additions
and
27 deletions
+89
-27
controller/relay.go
+7
-3
relay/helper/price.go
+30
-11
setting/operation_setting/quota_setting.go
+21
-0
types/price_data.go
+14
-13
web/src/components/settings/OperationSetting.jsx
+1
-0
web/src/pages/Setting/Operation/SettingsCreditLimit.jsx
+16
-0
No files found.
controller/relay.go
View file @
d43751f2
...
...
@@ -140,9 +140,13 @@ func Relay(c *gin.Context, relayFormat types.RelayFormat) {
// common.SetContextKey(c, constant.ContextKeyTokenCountMeta, meta)
newAPIError
=
service
.
PreConsumeQuota
(
c
,
priceData
.
ShouldPreConsumedQuota
,
relayInfo
)
if
newAPIError
!=
nil
{
return
if
priceData
.
FreeModel
{
logger
.
LogInfo
(
c
,
fmt
.
Sprintf
(
"模型 %s 免费,跳过预扣费"
,
relayInfo
.
OriginModelName
))
}
else
{
newAPIError
=
service
.
PreConsumeQuota
(
c
,
priceData
.
QuotaToPreConsume
,
relayInfo
)
if
newAPIError
!=
nil
{
return
}
}
defer
func
()
{
...
...
relay/helper/price.go
View file @
d43751f2
...
...
@@ -5,6 +5,7 @@ import (
"github.com/QuantumNous/new-api/common"
relaycommon
"github.com/QuantumNous/new-api/relay/common"
"github.com/QuantumNous/new-api/setting/operation_setting"
"github.com/QuantumNous/new-api/setting/ratio_setting"
"github.com/QuantumNous/new-api/types"
...
...
@@ -55,6 +56,7 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
var
cacheCreationRatio
float64
var
audioRatio
float64
var
audioCompletionRatio
float64
var
freeModel
bool
if
!
usePrice
{
preConsumedTokens
:=
common
.
Max
(
promptTokens
,
common
.
PreConsumedQuota
)
if
meta
.
MaxTokens
!=
0
{
...
...
@@ -87,18 +89,35 @@ func ModelPriceHelper(c *gin.Context, info *relaycommon.RelayInfo, promptTokens
preConsumedQuota
=
int
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatioInfo
.
GroupRatio
)
}
// check if free model pre-consume is disabled
if
!
operation_setting
.
GetQuotaSetting
()
.
EnableFreeModelPreConsume
{
// if model price or ratio is 0, do not pre-consume quota
if
usePrice
{
if
modelPrice
==
0
{
preConsumedQuota
=
0
freeModel
=
true
}
}
else
{
if
modelRatio
==
0
{
preConsumedQuota
=
0
freeModel
=
true
}
}
}
priceData
:=
types
.
PriceData
{
ModelPrice
:
modelPrice
,
ModelRatio
:
modelRatio
,
CompletionRatio
:
completionRatio
,
GroupRatioInfo
:
groupRatioInfo
,
UsePrice
:
usePrice
,
CacheRatio
:
cacheRatio
,
ImageRatio
:
imageRatio
,
AudioRatio
:
audioRatio
,
AudioCompletionRatio
:
audioCompletionRatio
,
CacheCreationRatio
:
cacheCreationRatio
,
ShouldPreConsumedQuota
:
preConsumedQuota
,
FreeModel
:
freeModel
,
ModelPrice
:
modelPrice
,
ModelRatio
:
modelRatio
,
CompletionRatio
:
completionRatio
,
GroupRatioInfo
:
groupRatioInfo
,
UsePrice
:
usePrice
,
CacheRatio
:
cacheRatio
,
ImageRatio
:
imageRatio
,
AudioRatio
:
audioRatio
,
AudioCompletionRatio
:
audioCompletionRatio
,
CacheCreationRatio
:
cacheCreationRatio
,
QuotaToPreConsume
:
preConsumedQuota
,
}
if
common
.
DebugEnabled
{
...
...
setting/operation_setting/quota_setting.go
0 → 100644
View file @
d43751f2
package
operation_setting
import
"github.com/QuantumNous/new-api/setting/config"
type
QuotaSetting
struct
{
EnableFreeModelPreConsume
bool
`json:"enable_free_model_pre_consume"`
// 是否对免费模型启用预消耗
}
// 默认配置
var
quotaSetting
=
QuotaSetting
{
EnableFreeModelPreConsume
:
true
,
}
func
init
()
{
// 注册到全局配置管理器
config
.
GlobalConfig
.
Register
(
"quota_setting"
,
&
quotaSetting
)
}
func
GetQuotaSetting
()
*
QuotaSetting
{
return
&
quotaSetting
}
types/price_data.go
View file @
d43751f2
...
...
@@ -9,18 +9,19 @@ type GroupRatioInfo struct {
}
type
PriceData
struct
{
ModelPrice
float64
ModelRatio
float64
CompletionRatio
float64
CacheRatio
float64
CacheCreationRatio
float64
ImageRatio
float64
AudioRatio
float64
AudioCompletionRatio
float64
OtherRatios
map
[
string
]
float64
UsePrice
bool
ShouldPreConsumedQuota
int
GroupRatioInfo
GroupRatioInfo
FreeModel
bool
ModelPrice
float64
ModelRatio
float64
CompletionRatio
float64
CacheRatio
float64
CacheCreationRatio
float64
ImageRatio
float64
AudioRatio
float64
AudioCompletionRatio
float64
OtherRatios
map
[
string
]
float64
UsePrice
bool
QuotaToPreConsume
int
// 预消耗额度
GroupRatioInfo
GroupRatioInfo
}
type
PerCallPriceData
struct
{
...
...
@@ -30,5 +31,5 @@ type PerCallPriceData struct {
}
func
(
p
PriceData
)
ToSetting
()
string
{
return
fmt
.
Sprintf
(
"ModelPrice: %f, ModelRatio: %f, CompletionRatio: %f, CacheRatio: %f, GroupRatio: %f, UsePrice: %t, CacheCreationRatio: %f,
ShouldPreConsumedQuota: %d, ImageRatio: %f, AudioRatio: %f, AudioCompletionRatio: %f"
,
p
.
ModelPrice
,
p
.
ModelRatio
,
p
.
CompletionRatio
,
p
.
CacheRatio
,
p
.
GroupRatioInfo
.
GroupRatio
,
p
.
UsePrice
,
p
.
CacheCreationRatio
,
p
.
ShouldPreConsumedQuota
,
p
.
ImageRatio
,
p
.
AudioRatio
,
p
.
AudioCompletionRatio
)
return
fmt
.
Sprintf
(
"ModelPrice: %f, ModelRatio: %f, CompletionRatio: %f, CacheRatio: %f, GroupRatio: %f, UsePrice: %t, CacheCreationRatio: %f,
QuotaToPreConsume: %d, ImageRatio: %f, AudioRatio: %f, AudioCompletionRatio: %f"
,
p
.
ModelPrice
,
p
.
ModelRatio
,
p
.
CompletionRatio
,
p
.
CacheRatio
,
p
.
GroupRatioInfo
.
GroupRatio
,
p
.
UsePrice
,
p
.
CacheCreationRatio
,
p
.
QuotaToPreConsume
,
p
.
ImageRatio
,
p
.
AudioRatio
,
p
.
AudioCompletionRatio
)
}
web/src/components/settings/OperationSetting.jsx
View file @
d43751f2
...
...
@@ -35,6 +35,7 @@ const OperationSetting = () => {
PreConsumedQuota
:
0
,
QuotaForInviter
:
0
,
QuotaForInvitee
:
0
,
'quota_setting.enable_free_model_pre_consume'
:
true
,
/* 通用设置 */
TopUpLink
:
''
,
...
...
web/src/pages/Setting/Operation/SettingsCreditLimit.jsx
View file @
d43751f2
...
...
@@ -36,6 +36,7 @@ export default function SettingsCreditLimit(props) {
PreConsumedQuota
:
''
,
QuotaForInviter
:
''
,
QuotaForInvitee
:
''
,
'quota_setting.enable_free_model_pre_consume'
:
true
,
});
const
refForm
=
useRef
();
const
[
inputsRow
,
setInputsRow
]
=
useState
(
inputs
);
...
...
@@ -166,6 +167,21 @@ export default function SettingsCreditLimit(props) {
/>
</
Col
>
</
Row
>
<
Row
>
<
Col
>
<
Form
.
Switch
label=
{
t
(
'对免费模型启用预消耗'
)
}
field=
{
'quota_setting.enable_free_model_pre_consume'
}
extraText=
{
t
(
'开启后,对免费模型(倍率为0,或者价格为0)的模型也会预消耗额度'
)
}
onChange=
{
(
value
)
=>
setInputs
({
...
inputs
,
'quota_setting.enable_free_model_pre_consume'
:
value
,
})
}
/>
</
Col
>
</
Row
>
<
Row
>
<
Button
size=
'default'
onClick=
{
onSubmit
}
>
...
...
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