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
2041a2e4
authored
Jul 19, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: gemini stream finish reason (close #378)
parent
7354c10a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
14 deletions
+33
-14
relay/channel/gemini/relay-gemini.go
+5
-2
service/relay.go
+28
-0
service/usage_helpr.go
+0
-12
No files found.
relay/channel/gemini/relay-gemini.go
View file @
2041a2e4
...
...
@@ -198,7 +198,6 @@ func streamResponseGeminiChat2OpenAI(geminiResponse *GeminiChatResponse) *dto.Ch
choice
.
Delta
.
SetContentString
(
respFirst
.
Text
)
}
}
choice
.
FinishReason
=
&
relaycommon
.
StopFinishReason
var
response
dto
.
ChatCompletionsStreamResponse
response
.
Object
=
"chat.completion.chunk"
response
.
Model
=
"gemini"
...
...
@@ -247,10 +246,14 @@ func geminiChatStreamHandler(c *gin.Context, resp *http.Response, info *relaycom
common
.
LogError
(
c
,
err
.
Error
())
}
}
response
:=
service
.
GenerateStopResponse
(
id
,
createAt
,
info
.
UpstreamModelName
,
relaycommon
.
StopFinishReason
)
service
.
ObjectData
(
c
,
response
)
usage
.
TotalTokens
=
usage
.
PromptTokens
+
usage
.
CompletionTokens
if
info
.
ShouldIncludeUsage
{
response
:
=
service
.
GenerateFinalUsageResponse
(
id
,
createAt
,
info
.
UpstreamModelName
,
*
usage
)
response
=
service
.
GenerateFinalUsageResponse
(
id
,
createAt
,
info
.
UpstreamModelName
,
*
usage
)
err
:=
service
.
ObjectData
(
c
,
response
)
if
err
!=
nil
{
common
.
SysError
(
"send final response failed: "
+
err
.
Error
())
...
...
service/relay.go
View file @
2041a2e4
...
...
@@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin"
"net/http"
"one-api/common"
"one-api/dto"
)
func
SetEventStreamHeaders
(
c
*
gin
.
Context
)
{
...
...
@@ -45,3 +46,30 @@ func GetResponseID(c *gin.Context) string {
logID
:=
c
.
GetString
(
"X-Oneapi-Request-Id"
)
return
fmt
.
Sprintf
(
"chatcmpl-%s"
,
logID
)
}
func
GenerateStopResponse
(
id
string
,
createAt
int64
,
model
string
,
finishReason
string
)
*
dto
.
ChatCompletionsStreamResponse
{
return
&
dto
.
ChatCompletionsStreamResponse
{
Id
:
id
,
Object
:
"chat.completion.chunk"
,
Created
:
createAt
,
Model
:
model
,
SystemFingerprint
:
nil
,
Choices
:
[]
dto
.
ChatCompletionsStreamResponseChoice
{
{
FinishReason
:
&
finishReason
,
},
},
}
}
func
GenerateFinalUsageResponse
(
id
string
,
createAt
int64
,
model
string
,
usage
dto
.
Usage
)
*
dto
.
ChatCompletionsStreamResponse
{
return
&
dto
.
ChatCompletionsStreamResponse
{
Id
:
id
,
Object
:
"chat.completion.chunk"
,
Created
:
createAt
,
Model
:
model
,
SystemFingerprint
:
nil
,
Choices
:
make
([]
dto
.
ChatCompletionsStreamResponseChoice
,
0
),
Usage
:
&
usage
,
}
}
service/usage_helpr.go
View file @
2041a2e4
...
...
@@ -25,18 +25,6 @@ func ResponseText2Usage(responseText string, modeName string, promptTokens int)
return
usage
,
err
}
func
GenerateFinalUsageResponse
(
id
string
,
createAt
int64
,
model
string
,
usage
dto
.
Usage
)
*
dto
.
ChatCompletionsStreamResponse
{
return
&
dto
.
ChatCompletionsStreamResponse
{
Id
:
id
,
Object
:
"chat.completion.chunk"
,
Created
:
createAt
,
Model
:
model
,
SystemFingerprint
:
nil
,
Choices
:
make
([]
dto
.
ChatCompletionsStreamResponseChoice
,
0
),
Usage
:
&
usage
,
}
}
func
ValidUsage
(
usage
*
dto
.
Usage
)
bool
{
return
usage
!=
nil
&&
(
usage
.
PromptTokens
!=
0
||
usage
.
CompletionTokens
!=
0
)
}
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