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
0e9a193e
authored
Jun 07, 2025
by
creamlike1024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: gemini audio input billing
parent
f3114454
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
118 additions
and
16 deletions
+118
-16
relay/channel/gemini/dto.go
+10
-4
relay/channel/gemini/relay-gemini-native.go
+18
-1
relay/channel/gemini/relay-gemini.go
+17
-2
relay/relay-text.go
+38
-9
service/audio.go
+17
-0
setting/operation_setting/tools.go
+18
-0
web/src/components/table/LogsTable.js
+0
-0
web/src/helpers/render.js
+0
-0
No files found.
relay/channel/gemini/dto.go
View file @
0e9a193e
...
...
@@ -112,10 +112,16 @@ type GeminiChatResponse struct {
}
type
GeminiUsageMetadata
struct
{
PromptTokenCount
int
`json:"promptTokenCount"`
CandidatesTokenCount
int
`json:"candidatesTokenCount"`
TotalTokenCount
int
`json:"totalTokenCount"`
ThoughtsTokenCount
int
`json:"thoughtsTokenCount"`
PromptTokenCount
int
`json:"promptTokenCount"`
CandidatesTokenCount
int
`json:"candidatesTokenCount"`
TotalTokenCount
int
`json:"totalTokenCount"`
ThoughtsTokenCount
int
`json:"thoughtsTokenCount"`
PromptTokensDetails
[]
GeminiPromptTokensDetails
`json:"promptTokensDetails"`
}
type
GeminiPromptTokensDetails
struct
{
Modality
string
`json:"modality"`
TokenCount
int
`json:"tokenCount"`
}
// Imagen related structs
...
...
relay/channel/gemini/relay-gemini-native.go
View file @
0e9a193e
...
...
@@ -55,6 +55,16 @@ func GeminiTextGenerationHandler(c *gin.Context, resp *http.Response, info *rela
TotalTokens
:
geminiResponse
.
UsageMetadata
.
TotalTokenCount
,
}
usage
.
CompletionTokenDetails
.
ReasoningTokens
=
geminiResponse
.
UsageMetadata
.
ThoughtsTokenCount
for
_
,
detail
:=
range
geminiResponse
.
UsageMetadata
.
PromptTokensDetails
{
if
detail
.
Modality
==
"AUDIO"
{
usage
.
PromptTokensDetails
.
AudioTokens
=
detail
.
TokenCount
}
else
if
detail
.
Modality
==
"TEXT"
{
usage
.
PromptTokensDetails
.
TextTokens
=
detail
.
TokenCount
}
}
// 直接返回 Gemini 原生格式的 JSON 响应
jsonResponse
,
err
:=
json
.
Marshal
(
geminiResponse
)
if
err
!=
nil
{
...
...
@@ -100,6 +110,14 @@ func GeminiTextGenerationStreamHandler(c *gin.Context, resp *http.Response, info
usage
.
PromptTokens
=
geminiResponse
.
UsageMetadata
.
PromptTokenCount
usage
.
CompletionTokens
=
geminiResponse
.
UsageMetadata
.
CandidatesTokenCount
usage
.
TotalTokens
=
geminiResponse
.
UsageMetadata
.
TotalTokenCount
usage
.
CompletionTokenDetails
.
ReasoningTokens
=
geminiResponse
.
UsageMetadata
.
ThoughtsTokenCount
for
_
,
detail
:=
range
geminiResponse
.
UsageMetadata
.
PromptTokensDetails
{
if
detail
.
Modality
==
"AUDIO"
{
usage
.
PromptTokensDetails
.
AudioTokens
=
detail
.
TokenCount
}
else
if
detail
.
Modality
==
"TEXT"
{
usage
.
PromptTokensDetails
.
TextTokens
=
detail
.
TokenCount
}
}
}
// 直接发送 GeminiChatResponse 响应
...
...
@@ -118,7 +136,6 @@ func GeminiTextGenerationStreamHandler(c *gin.Context, resp *http.Response, info
}
// 计算最终使用量
usage
.
PromptTokensDetails
.
TextTokens
=
usage
.
PromptTokens
usage
.
CompletionTokens
=
usage
.
TotalTokens
-
usage
.
PromptTokens
// 移除流式响应结尾的[Done],因为Gemini API没有发送Done的行为
...
...
relay/channel/gemini/relay-gemini.go
View file @
0e9a193e
...
...
@@ -313,13 +313,13 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest, info *relaycommon
if
part
.
GetInputAudio
()
.
Data
==
""
{
return
nil
,
fmt
.
Errorf
(
"only base64 audio is supported in gemini"
)
}
format
,
base64String
,
err
:=
service
.
DecodeBase64File
Data
(
part
.
GetInputAudio
()
.
Data
)
base64String
,
err
:=
service
.
DecodeBase64Audio
Data
(
part
.
GetInputAudio
()
.
Data
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"decode base64 audio data failed: %s"
,
err
.
Error
())
}
parts
=
append
(
parts
,
GeminiPart
{
InlineData
:
&
GeminiInlineData
{
MimeType
:
f
ormat
,
MimeType
:
"audio/"
+
part
.
GetInputAudio
()
.
F
ormat
,
Data
:
base64String
,
},
})
...
...
@@ -771,6 +771,13 @@ func GeminiChatStreamHandler(c *gin.Context, resp *http.Response, info *relaycom
usage
.
CompletionTokens
=
geminiResponse
.
UsageMetadata
.
CandidatesTokenCount
usage
.
CompletionTokenDetails
.
ReasoningTokens
=
geminiResponse
.
UsageMetadata
.
ThoughtsTokenCount
usage
.
TotalTokens
=
geminiResponse
.
UsageMetadata
.
TotalTokenCount
for
_
,
detail
:=
range
geminiResponse
.
UsageMetadata
.
PromptTokensDetails
{
if
detail
.
Modality
==
"AUDIO"
{
usage
.
PromptTokensDetails
.
AudioTokens
=
detail
.
TokenCount
}
else
if
detail
.
Modality
==
"TEXT"
{
usage
.
PromptTokensDetails
.
TextTokens
=
detail
.
TokenCount
}
}
}
err
=
helper
.
ObjectData
(
c
,
response
)
if
err
!=
nil
{
...
...
@@ -845,6 +852,14 @@ func GeminiChatHandler(c *gin.Context, resp *http.Response, info *relaycommon.Re
usage
.
CompletionTokenDetails
.
ReasoningTokens
=
geminiResponse
.
UsageMetadata
.
ThoughtsTokenCount
usage
.
CompletionTokens
=
usage
.
TotalTokens
-
usage
.
PromptTokens
for
_
,
detail
:=
range
geminiResponse
.
UsageMetadata
.
PromptTokensDetails
{
if
detail
.
Modality
==
"AUDIO"
{
usage
.
PromptTokensDetails
.
AudioTokens
=
detail
.
TokenCount
}
else
if
detail
.
Modality
==
"TEXT"
{
usage
.
PromptTokensDetails
.
TextTokens
=
detail
.
TokenCount
}
}
fullTextResponse
.
Usage
=
usage
jsonResponse
,
err
:=
json
.
Marshal
(
fullTextResponse
)
if
err
!=
nil
{
...
...
relay/relay-text.go
View file @
0e9a193e
...
...
@@ -352,6 +352,7 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
promptTokens
:=
usage
.
PromptTokens
cacheTokens
:=
usage
.
PromptTokensDetails
.
CachedTokens
imageTokens
:=
usage
.
PromptTokensDetails
.
ImageTokens
audioTokens
:=
usage
.
PromptTokensDetails
.
AudioTokens
completionTokens
:=
usage
.
CompletionTokens
modelName
:=
relayInfo
.
OriginModelName
...
...
@@ -367,6 +368,7 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
dPromptTokens
:=
decimal
.
NewFromInt
(
int64
(
promptTokens
))
dCacheTokens
:=
decimal
.
NewFromInt
(
int64
(
cacheTokens
))
dImageTokens
:=
decimal
.
NewFromInt
(
int64
(
imageTokens
))
dAudioTokens
:=
decimal
.
NewFromInt
(
int64
(
audioTokens
))
dCompletionTokens
:=
decimal
.
NewFromInt
(
int64
(
completionTokens
))
dCompletionRatio
:=
decimal
.
NewFromFloat
(
completionRatio
)
dCacheRatio
:=
decimal
.
NewFromFloat
(
cacheRatio
)
...
...
@@ -412,23 +414,43 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
dFileSearchQuota
=
decimal
.
NewFromFloat
(
fileSearchPrice
)
.
Mul
(
decimal
.
NewFromInt
(
int64
(
fileSearchTool
.
CallCount
)))
.
Div
(
decimal
.
NewFromInt
(
1000
))
.
Mul
(
dGroupRatio
)
.
Mul
(
dQuotaPerUnit
)
extraContent
+=
fmt
.
Sprintf
(
"File Search 调用 %d 次,调用花费
$
%s"
,
extraContent
+=
fmt
.
Sprintf
(
"File Search 调用 %d 次,调用花费 %s"
,
fileSearchTool
.
CallCount
,
dFileSearchQuota
.
String
())
}
}
var
quotaCalculateDecimal
decimal
.
Decimal
var
audioInputQuota
decimal
.
Decimal
var
audioInputPrice
float64
if
!
priceData
.
UsePrice
{
nonCachedTokens
:=
dPromptTokens
.
Sub
(
dCacheTokens
)
cachedTokensWithRatio
:=
dCacheTokens
.
Mul
(
dCacheRatio
)
promptQuota
:=
nonCachedTokens
.
Add
(
cachedTokensWithRatio
)
if
imageTokens
>
0
{
nonImageTokens
:=
dPromptTokens
.
Sub
(
dImageTokens
)
imageTokensWithRatio
:=
dImageTokens
.
Mul
(
dImageRatio
)
promptQuota
=
nonImageTokens
.
Add
(
imageTokensWithRatio
)
baseTokens
:=
dPromptTokens
// 减去 cached tokens
var
cachedTokensWithRatio
decimal
.
Decimal
if
!
dCacheTokens
.
IsZero
()
{
baseTokens
=
baseTokens
.
Sub
(
dCacheTokens
)
cachedTokensWithRatio
=
dCacheTokens
.
Mul
(
dCacheRatio
)
}
// 减去 image tokens
var
imageTokensWithRatio
decimal
.
Decimal
if
!
dImageTokens
.
IsZero
()
{
baseTokens
=
baseTokens
.
Sub
(
dImageTokens
)
imageTokensWithRatio
=
dImageTokens
.
Mul
(
dImageRatio
)
}
// 减去 Gemini audio tokens
if
!
dAudioTokens
.
IsZero
()
{
audioInputPrice
=
operation_setting
.
GetGeminiInputAudioPricePerMillionTokens
(
modelName
)
if
audioInputPrice
>
0
{
// 重新计算 base tokens
baseTokens
=
baseTokens
.
Sub
(
dAudioTokens
)
audioInputQuota
=
decimal
.
NewFromFloat
(
audioInputPrice
)
.
Div
(
decimal
.
NewFromInt
(
1000000
))
.
Mul
(
dAudioTokens
)
.
Mul
(
dGroupRatio
)
.
Mul
(
dQuotaPerUnit
)
extraContent
+=
fmt
.
Sprintf
(
"Audio Input 花费 %s"
,
audioInputQuota
.
String
())
}
}
promptQuota
:=
baseTokens
.
Add
(
cachedTokensWithRatio
)
.
Add
(
imageTokensWithRatio
)
completionQuota
:=
dCompletionTokens
.
Mul
(
dCompletionRatio
)
quotaCalculateDecimal
=
promptQuota
.
Add
(
completionQuota
)
.
Mul
(
ratio
)
...
...
@@ -442,6 +464,8 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
// 添加 responses tools call 调用的配额
quotaCalculateDecimal
=
quotaCalculateDecimal
.
Add
(
dWebSearchQuota
)
quotaCalculateDecimal
=
quotaCalculateDecimal
.
Add
(
dFileSearchQuota
)
// 添加 audio input 独立计费
quotaCalculateDecimal
=
quotaCalculateDecimal
.
Add
(
audioInputQuota
)
quota
:=
int
(
quotaCalculateDecimal
.
Round
(
0
)
.
IntPart
())
totalTokens
:=
promptTokens
+
completionTokens
...
...
@@ -512,6 +536,11 @@ func postConsumeQuota(ctx *gin.Context, relayInfo *relaycommon.RelayInfo,
other
[
"file_search_price"
]
=
fileSearchPrice
}
}
if
!
audioInputQuota
.
IsZero
()
{
other
[
"audio_input_seperate_price"
]
=
true
other
[
"audio_input_token_count"
]
=
audioTokens
other
[
"audio_input_price"
]
=
audioInputPrice
}
model
.
RecordConsumeLog
(
ctx
,
relayInfo
.
UserId
,
relayInfo
.
ChannelId
,
promptTokens
,
completionTokens
,
logModel
,
tokenName
,
quota
,
logContent
,
relayInfo
.
TokenId
,
userQuota
,
int
(
useTimeSeconds
),
relayInfo
.
IsStream
,
relayInfo
.
Group
,
other
)
}
service/audio.go
View file @
0e9a193e
...
...
@@ -3,6 +3,7 @@ package service
import
(
"encoding/base64"
"fmt"
"strings"
)
func
parseAudio
(
audioBase64
string
,
format
string
)
(
duration
float64
,
err
error
)
{
...
...
@@ -29,3 +30,19 @@ func parseAudio(audioBase64 string, format string) (duration float64, err error)
duration
=
float64
(
samplesCount
)
/
float64
(
sampleRate
)
return
duration
,
nil
}
func
DecodeBase64AudioData
(
audioBase64
string
)
(
string
,
error
)
{
// 检查并移除 data:audio/xxx;base64, 前缀
idx
:=
strings
.
Index
(
audioBase64
,
","
)
if
idx
!=
-
1
{
audioBase64
=
audioBase64
[
idx
+
1
:
]
}
// 解码 Base64 数据
_
,
err
:=
base64
.
StdEncoding
.
DecodeString
(
audioBase64
)
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"base64 decode error: %v"
,
err
)
}
return
audioBase64
,
nil
}
setting/operation_setting/tools.go
View file @
0e9a193e
...
...
@@ -14,6 +14,13 @@ const (
FileSearchPrice
=
2.5
)
const
(
// Gemini Audio Input Price
Gemini25FlashPreviewInputAudioPrice
=
1.00
Gemini25FlashNativeAudioInputAudioPrice
=
3.00
Gemini20FlashInputAudioPrice
=
0.70
)
func
GetWebSearchPricePerThousand
(
modelName
string
,
contextSize
string
)
float64
{
// 确定模型类型
// https://platform.openai.com/docs/pricing Web search 价格按模型类型和 search context size 收费
...
...
@@ -55,3 +62,14 @@ func GetWebSearchPricePerThousand(modelName string, contextSize string) float64
func
GetFileSearchPricePerThousand
()
float64
{
return
FileSearchPrice
}
func
GetGeminiInputAudioPricePerMillionTokens
(
modelName
string
)
float64
{
if
strings
.
HasPrefix
(
modelName
,
"gemini-2.5-flash-preview"
)
{
return
Gemini25FlashPreviewInputAudioPrice
}
else
if
strings
.
HasPrefix
(
modelName
,
"gemini-2.5-flash-preview-native-audio"
)
{
return
Gemini25FlashNativeAudioInputAudioPrice
}
else
if
strings
.
HasPrefix
(
modelName
,
"gemini-2.0-flash"
)
{
return
Gemini20FlashInputAudioPrice
}
return
0
}
web/src/components/table/LogsTable.js
View file @
0e9a193e
This diff is collapsed.
Click to expand it.
web/src/helpers/render.js
View file @
0e9a193e
This diff is collapsed.
Click to expand it.
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