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
4f3024ad
authored
Jun 20, 2025
by
creamlike1024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: gemini 原生格式流模式中断请求未计费
parent
8ceaaf78
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
0 deletions
+15
-0
relay/channel/gemini/relay-gemini-native.go
+15
-0
No files found.
relay/channel/gemini/relay-gemini-native.go
View file @
4f3024ad
...
@@ -75,6 +75,9 @@ func GeminiTextGenerationStreamHandler(c *gin.Context, resp *http.Response, info
...
@@ -75,6 +75,9 @@ func GeminiTextGenerationStreamHandler(c *gin.Context, resp *http.Response, info
helper
.
SetEventStreamHeaders
(
c
)
helper
.
SetEventStreamHeaders
(
c
)
// 本地统计的completion tokens
localCompletionTokens
:=
0
helper
.
StreamScannerHandler
(
c
,
resp
,
info
,
func
(
data
string
)
bool
{
helper
.
StreamScannerHandler
(
c
,
resp
,
info
,
func
(
data
string
)
bool
{
var
geminiResponse
GeminiChatResponse
var
geminiResponse
GeminiChatResponse
err
:=
common
.
DecodeJsonStr
(
data
,
&
geminiResponse
)
err
:=
common
.
DecodeJsonStr
(
data
,
&
geminiResponse
)
...
@@ -89,6 +92,12 @@ func GeminiTextGenerationStreamHandler(c *gin.Context, resp *http.Response, info
...
@@ -89,6 +92,12 @@ func GeminiTextGenerationStreamHandler(c *gin.Context, resp *http.Response, info
if
part
.
InlineData
!=
nil
&&
part
.
InlineData
.
MimeType
!=
""
{
if
part
.
InlineData
!=
nil
&&
part
.
InlineData
.
MimeType
!=
""
{
imageCount
++
imageCount
++
}
}
// 本地统计completion tokens
textTokens
,
err
:=
service
.
CountTextToken
(
part
.
Text
,
info
.
UpstreamModelName
)
if
err
!=
nil
{
common
.
LogError
(
c
,
"error counting text token: "
+
err
.
Error
())
}
localCompletionTokens
+=
textTokens
}
}
}
}
...
@@ -122,6 +131,12 @@ func GeminiTextGenerationStreamHandler(c *gin.Context, resp *http.Response, info
...
@@ -122,6 +131,12 @@ func GeminiTextGenerationStreamHandler(c *gin.Context, resp *http.Response, info
}
}
}
}
// 如果usage.CompletionTokens为0,则使用本地统计的completion tokens
if
usage
.
CompletionTokens
==
0
{
usage
.
CompletionTokens
=
localCompletionTokens
usage
.
TotalTokens
=
usage
.
PromptTokens
+
usage
.
CompletionTokens
}
// 计算最终使用量
// 计算最终使用量
// usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
// usage.CompletionTokens = usage.TotalTokens - usage.PromptTokens
...
...
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