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
32c09c26
authored
Mar 08, 2025
by
Calcium-Ion
Committed by
GitHub
Mar 08, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #826 from Calcium-Ion/cache
feat: Add prompt cache hit tokens support for DeepSeek channel #406
parents
14959944
f939c28b
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
26 additions
and
17 deletions
+26
-17
dto/openai_response.go
+1
-0
relay/channel/openai/relay-openai.go
+6
-0
relay/relay-text.go
+9
-8
service/quota.go
+7
-6
setting/operation_setting/cache_ratio.go
+3
-3
No files found.
dto/openai_response.go
View file @
32c09c26
...
@@ -166,6 +166,7 @@ type Usage struct {
...
@@ -166,6 +166,7 @@ type Usage struct {
PromptTokens
int
`json:"prompt_tokens"`
PromptTokens
int
`json:"prompt_tokens"`
CompletionTokens
int
`json:"completion_tokens"`
CompletionTokens
int
`json:"completion_tokens"`
TotalTokens
int
`json:"total_tokens"`
TotalTokens
int
`json:"total_tokens"`
PromptCacheHitTokens
int
`json:"prompt_cache_hit_tokens,omitempty"`
PromptTokensDetails
InputTokenDetails
`json:"prompt_tokens_details"`
PromptTokensDetails
InputTokenDetails
`json:"prompt_tokens_details"`
CompletionTokenDetails
OutputTokenDetails
`json:"completion_tokens_details"`
CompletionTokenDetails
OutputTokenDetails
`json:"completion_tokens_details"`
}
}
relay/channel/openai/relay-openai.go
View file @
32c09c26
...
@@ -254,6 +254,12 @@ func OaiStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rel
...
@@ -254,6 +254,12 @@ func OaiStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rel
if
!
containStreamUsage
{
if
!
containStreamUsage
{
usage
,
_
=
service
.
ResponseText2Usage
(
responseTextBuilder
.
String
(),
info
.
UpstreamModelName
,
info
.
PromptTokens
)
usage
,
_
=
service
.
ResponseText2Usage
(
responseTextBuilder
.
String
(),
info
.
UpstreamModelName
,
info
.
PromptTokens
)
usage
.
CompletionTokens
+=
toolCount
*
7
usage
.
CompletionTokens
+=
toolCount
*
7
}
else
{
if
info
.
ChannelType
==
common
.
ChannelTypeDeepSeek
{
if
usage
.
PromptCacheHitTokens
!=
0
{
usage
.
PromptTokensDetails
.
CachedTokens
=
usage
.
PromptCacheHitTokens
}
}
}
}
if
info
.
ShouldIncludeUsage
&&
!
containStreamUsage
{
if
info
.
ShouldIncludeUsage
&&
!
containStreamUsage
{
...
...
relay/relay-text.go
View file @
32c09c26
...
@@ -320,19 +320,20 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
...
@@ -320,19 +320,20 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
groupRatio
:=
priceData
.
GroupRatio
groupRatio
:=
priceData
.
GroupRatio
modelPrice
:=
priceData
.
ModelPrice
modelPrice
:=
priceData
.
ModelPrice
quota
:=
0
quota
Calculate
:=
0.
0
if
!
priceData
.
UsePrice
{
if
!
priceData
.
UsePrice
{
quota
=
(
promptTokens
-
cacheTokens
)
+
int
(
math
.
Round
(
float64
(
cacheTokens
)
*
cacheRatio
))
quota
Calculate
=
float64
(
promptTokens
-
cacheTokens
)
+
float64
(
cacheTokens
)
*
cacheRatio
quota
+=
int
(
math
.
Round
(
float64
(
completionTokens
)
*
completionRatio
))
quota
Calculate
+=
float64
(
completionTokens
)
*
completionRatio
quota
=
int
(
math
.
Round
(
float64
(
quota
)
*
ratio
))
quota
Calculate
=
quotaCalculate
*
ratio
if
ratio
!=
0
&&
quota
<=
0
{
if
ratio
!=
0
&&
quota
Calculate
<=
0
{
quota
=
1
quota
Calculate
=
1
}
}
}
else
{
}
else
{
quota
=
int
(
modelPrice
*
common
.
QuotaPerUnit
*
groupRatio
)
quota
Calculate
=
modelPrice
*
common
.
QuotaPerUnit
*
groupRatio
}
}
quota
:=
int
(
quotaCalculate
)
totalTokens
:=
promptTokens
+
completionTokens
totalTokens
:=
promptTokens
+
completionTokens
var
logContent
string
var
logContent
string
if
!
priceData
.
UsePrice
{
if
!
priceData
.
UsePrice
{
logContent
=
fmt
.
Sprintf
(
"模型倍率 %.2f,补全倍率 %.2f,分组倍率 %.2f"
,
modelRatio
,
completionRatio
,
groupRatio
)
logContent
=
fmt
.
Sprintf
(
"模型倍率 %.2f,补全倍率 %.2f,分组倍率 %.2f"
,
modelRatio
,
completionRatio
,
groupRatio
)
...
...
service/quota.go
View file @
32c09c26
...
@@ -4,7 +4,6 @@ import (
...
@@ -4,7 +4,6 @@ import (
"errors"
"errors"
"fmt"
"fmt"
"github.com/bytedance/gopkg/util/gopool"
"github.com/bytedance/gopkg/util/gopool"
"math"
"one-api/common"
"one-api/common"
constant2
"one-api/constant"
constant2
"one-api/constant"
"one-api/dto"
"one-api/dto"
...
@@ -44,16 +43,18 @@ func calculateAudioQuota(info QuotaInfo) int {
...
@@ -44,16 +43,18 @@ func calculateAudioQuota(info QuotaInfo) int {
audioCompletionRatio
:=
operation_setting
.
GetAudioCompletionRatio
(
info
.
ModelName
)
audioCompletionRatio
:=
operation_setting
.
GetAudioCompletionRatio
(
info
.
ModelName
)
ratio
:=
info
.
GroupRatio
*
info
.
ModelRatio
ratio
:=
info
.
GroupRatio
*
info
.
ModelRatio
quota
:=
info
.
InputDetails
.
TextTokens
+
int
(
math
.
Round
(
float64
(
info
.
OutputDetails
.
TextTokens
)
*
completionRatio
))
quota
:=
0.0
quota
+=
int
(
math
.
Round
(
float64
(
info
.
InputDetails
.
AudioTokens
)
*
audioRatio
))
+
quota
+=
float64
(
info
.
InputDetails
.
TextTokens
)
int
(
math
.
Round
(
float64
(
info
.
OutputDetails
.
AudioTokens
)
*
audioRatio
*
audioCompletionRatio
))
quota
+=
float64
(
info
.
OutputDetails
.
TextTokens
)
*
completionRatio
quota
+=
float64
(
info
.
InputDetails
.
AudioTokens
)
*
audioRatio
quota
+=
float64
(
info
.
OutputDetails
.
AudioTokens
)
*
audioRatio
*
audioCompletionRatio
quota
=
int
(
math
.
Round
(
float64
(
quota
)
*
ratio
))
quota
=
quota
*
ratio
if
ratio
!=
0
&&
quota
<=
0
{
if
ratio
!=
0
&&
quota
<=
0
{
quota
=
1
quota
=
1
}
}
return
quota
return
int
(
quota
)
}
}
func
PreWssConsumeQuota
(
ctx
*
gin
.
Context
,
relayInfo
*
relaycommon
.
RelayInfo
,
usage
*
dto
.
RealtimeUsage
)
error
{
func
PreWssConsumeQuota
(
ctx
*
gin
.
Context
,
relayInfo
*
relaycommon
.
RelayInfo
,
usage
*
dto
.
RealtimeUsage
)
error
{
...
...
setting/operation_setting/cache_ratio.go
View file @
32c09c26
...
@@ -16,9 +16,9 @@ var defaultCacheRatio = map[string]float64{
...
@@ -16,9 +16,9 @@ var defaultCacheRatio = map[string]float64{
"gpt-4o-mini-2024-07-18"
:
0.5
,
"gpt-4o-mini-2024-07-18"
:
0.5
,
"gpt-4o-realtime-preview"
:
0.5
,
"gpt-4o-realtime-preview"
:
0.5
,
"gpt-4o-mini-realtime-preview"
:
0.5
,
"gpt-4o-mini-realtime-preview"
:
0.5
,
"deepseek-chat"
:
0.
5
,
"deepseek-chat"
:
0.
1
,
"deepseek-reasoner"
:
0.
5
,
"deepseek-reasoner"
:
0.
1
,
"deepseek-coder"
:
0.
5
,
"deepseek-coder"
:
0.
1
,
}
}
var
defaultCreateCacheRatio
=
map
[
string
]
float64
{}
var
defaultCreateCacheRatio
=
map
[
string
]
float64
{}
...
...
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