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
8101cd3c
authored
Feb 21, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Add reasoning content support in OpenAI response handling
parent
4a49d6c7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
4 deletions
+14
-4
dto/openai_response.go
+11
-3
relay/channel/openai/relay-openai.go
+3
-1
No files found.
dto/openai_response.go
View file @
8101cd3c
...
...
@@ -62,9 +62,10 @@ type ChatCompletionsStreamResponseChoice struct {
}
type
ChatCompletionsStreamResponseChoiceDelta
struct
{
Content
*
string
`json:"content,omitempty"`
Role
string
`json:"role,omitempty"`
ToolCalls
[]
ToolCall
`json:"tool_calls,omitempty"`
Content
*
string
`json:"content,omitempty"`
ReasoningContent
*
string
`json:"reasoning_content,omitempty"`
Role
string
`json:"role,omitempty"`
ToolCalls
[]
ToolCall
`json:"tool_calls,omitempty"`
}
func
(
c
*
ChatCompletionsStreamResponseChoiceDelta
)
SetContentString
(
s
string
)
{
...
...
@@ -78,6 +79,13 @@ func (c *ChatCompletionsStreamResponseChoiceDelta) GetContentString() string {
return
*
c
.
Content
}
func
(
c
*
ChatCompletionsStreamResponseChoiceDelta
)
GetReasoningContent
()
string
{
if
c
.
ReasoningContent
==
nil
{
return
""
}
return
*
c
.
ReasoningContent
}
type
ToolCall
struct
{
// Index is not nil only in chat completion chunk object
Index
*
int
`json:"index,omitempty"`
...
...
relay/channel/openai/relay-openai.go
View file @
8101cd3c
...
...
@@ -162,6 +162,7 @@ func OaiStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rel
//}
for
_
,
choice
:=
range
streamResponse
.
Choices
{
responseTextBuilder
.
WriteString
(
choice
.
Delta
.
GetContentString
())
responseTextBuilder
.
WriteString
(
choice
.
Delta
.
GetReasoningContent
())
if
choice
.
Delta
.
ToolCalls
!=
nil
{
if
len
(
choice
.
Delta
.
ToolCalls
)
>
toolCount
{
toolCount
=
len
(
choice
.
Delta
.
ToolCalls
)
...
...
@@ -182,6 +183,7 @@ func OaiStreamHandler(c *gin.Context, resp *http.Response, info *relaycommon.Rel
//}
for
_
,
choice
:=
range
streamResponse
.
Choices
{
responseTextBuilder
.
WriteString
(
choice
.
Delta
.
GetContentString
())
responseTextBuilder
.
WriteString
(
choice
.
Delta
.
GetReasoningContent
())
if
choice
.
Delta
.
ToolCalls
!=
nil
{
if
len
(
choice
.
Delta
.
ToolCalls
)
>
toolCount
{
toolCount
=
len
(
choice
.
Delta
.
ToolCalls
)
...
...
@@ -273,7 +275,7 @@ func OpenaiHandler(c *gin.Context, resp *http.Response, promptTokens int, model
if
simpleResponse
.
Usage
.
TotalTokens
==
0
||
(
simpleResponse
.
Usage
.
PromptTokens
==
0
&&
simpleResponse
.
Usage
.
CompletionTokens
==
0
)
{
completionTokens
:=
0
for
_
,
choice
:=
range
simpleResponse
.
Choices
{
ctkm
,
_
:=
service
.
CountTextToken
(
string
(
choice
.
Message
.
Content
)
,
model
)
ctkm
,
_
:=
service
.
CountTextToken
(
choice
.
Message
.
StringContent
()
+
choice
.
Message
.
ReasoningContent
,
model
)
completionTokens
+=
ctkm
}
simpleResponse
.
Usage
=
dto
.
Usage
{
...
...
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