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
5d7e688b
authored
Jun 08, 2025
by
Calcium-Ion
Committed by
GitHub
Jun 08, 2025
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1174 from QuantumNous/refactor/message
refactor: message content 改成 any
parents
7469446a
8343fe07
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
16 changed files
with
318 additions
and
121 deletions
+318
-121
controller/channel-test.go
+2
-2
dto/claude.go
+67
-36
dto/openai_request.go
+222
-48
main.go
+2
-2
relay/channel/ali/text.go
+1
-2
relay/channel/baidu/relay-baidu.go
+1
-2
relay/channel/claude/relay-claude.go
+5
-8
relay/channel/cohere/relay-cohere.go
+1
-2
relay/channel/coze/dto.go
+1
-1
relay/channel/dify/relay-dify.go
+1
-2
relay/channel/gemini/relay-gemini.go
+1
-2
relay/channel/palm/relay-palm.go
+1
-2
relay/channel/tencent/relay-tencent.go
+1
-2
relay/channel/xunfei/relay-xunfei.go
+1
-2
relay/channel/zhipu/relay-zhipu.go
+1
-2
service/token_counter.go
+10
-6
No files found.
controller/channel-test.go
View file @
5d7e688b
...
@@ -200,10 +200,10 @@ func buildTestRequest(model string) *dto.GeneralOpenAIRequest {
...
@@ -200,10 +200,10 @@ func buildTestRequest(model string) *dto.GeneralOpenAIRequest {
}
else
{
}
else
{
testRequest
.
MaxTokens
=
10
testRequest
.
MaxTokens
=
10
}
}
content
,
_
:=
json
.
Marshal
(
"hi"
)
testMessage
:=
dto
.
Message
{
testMessage
:=
dto
.
Message
{
Role
:
"user"
,
Role
:
"user"
,
Content
:
content
,
Content
:
"hi"
,
}
}
testRequest
.
Model
=
model
testRequest
.
Model
=
model
testRequest
.
Messages
=
append
(
testRequest
.
Messages
,
testMessage
)
testRequest
.
Messages
=
append
(
testRequest
.
Messages
,
testMessage
)
...
...
dto/claude.go
View file @
5d7e688b
package
dto
package
dto
import
"encoding/json"
import
(
"encoding/json"
"one-api/common"
)
type
ClaudeMetadata
struct
{
type
ClaudeMetadata
struct
{
UserId
string
`json:"user_id"`
UserId
string
`json:"user_id"`
...
@@ -20,11 +23,11 @@ type ClaudeMediaMessage struct {
...
@@ -20,11 +23,11 @@ type ClaudeMediaMessage struct {
Delta
string
`json:"delta,omitempty"`
Delta
string
`json:"delta,omitempty"`
CacheControl
json
.
RawMessage
`json:"cache_control,omitempty"`
CacheControl
json
.
RawMessage
`json:"cache_control,omitempty"`
// tool_calls
// tool_calls
Id
string
`json:"id,omitempty"`
Id
string
`json:"id,omitempty"`
Name
string
`json:"name,omitempty"`
Name
string
`json:"name,omitempty"`
Input
any
`json:"input,omitempty"`
Input
any
`json:"input,omitempty"`
Content
json
.
RawMessage
`json:"content,omitempty"`
Content
any
`json:"content,omitempty"`
ToolUseId
string
`json:"tool_use_id,omitempty"`
ToolUseId
string
`json:"tool_use_id,omitempty"`
}
}
func
(
c
*
ClaudeMediaMessage
)
SetText
(
s
string
)
{
func
(
c
*
ClaudeMediaMessage
)
SetText
(
s
string
)
{
...
@@ -39,15 +42,39 @@ func (c *ClaudeMediaMessage) GetText() string {
...
@@ -39,15 +42,39 @@ func (c *ClaudeMediaMessage) GetText() string {
}
}
func
(
c
*
ClaudeMediaMessage
)
IsStringContent
()
bool
{
func
(
c
*
ClaudeMediaMessage
)
IsStringContent
()
bool
{
var
content
string
if
c
.
Content
==
nil
{
return
json
.
Unmarshal
(
c
.
Content
,
&
content
)
==
nil
return
false
}
_
,
ok
:=
c
.
Content
.
(
string
)
if
ok
{
return
true
}
return
false
}
}
func
(
c
*
ClaudeMediaMessage
)
GetStringContent
()
string
{
func
(
c
*
ClaudeMediaMessage
)
GetStringContent
()
string
{
var
content
string
if
c
.
Content
==
nil
{
if
err
:=
json
.
Unmarshal
(
c
.
Content
,
&
content
);
err
==
nil
{
return
""
return
content
}
}
switch
c
.
Content
.
(
type
)
{
case
string
:
return
c
.
Content
.
(
string
)
case
[]
any
:
var
contentStr
string
for
_
,
contentItem
:=
range
c
.
Content
.
([]
any
)
{
contentMap
,
ok
:=
contentItem
.
(
map
[
string
]
any
)
if
!
ok
{
continue
}
if
contentMap
[
"type"
]
==
ContentTypeText
{
if
subStr
,
ok
:=
contentMap
[
"text"
]
.
(
string
);
ok
{
contentStr
+=
subStr
}
}
}
return
contentStr
}
return
""
return
""
}
}
...
@@ -57,16 +84,12 @@ func (c *ClaudeMediaMessage) GetJsonRowString() string {
...
@@ -57,16 +84,12 @@ func (c *ClaudeMediaMessage) GetJsonRowString() string {
}
}
func
(
c
*
ClaudeMediaMessage
)
SetContent
(
content
any
)
{
func
(
c
*
ClaudeMediaMessage
)
SetContent
(
content
any
)
{
jsonContent
,
_
:=
json
.
Marshal
(
content
)
c
.
Content
=
content
c
.
Content
=
jsonContent
}
}
func
(
c
*
ClaudeMediaMessage
)
ParseMediaContent
()
[]
ClaudeMediaMessage
{
func
(
c
*
ClaudeMediaMessage
)
ParseMediaContent
()
[]
ClaudeMediaMessage
{
var
mediaContent
[]
ClaudeMediaMessage
mediaContent
,
_
:=
common
.
Any2Type
[[]
ClaudeMediaMessage
](
c
.
Content
)
if
err
:=
json
.
Unmarshal
(
c
.
Content
,
&
mediaContent
);
err
==
nil
{
return
mediaContent
return
mediaContent
}
return
make
([]
ClaudeMediaMessage
,
0
)
}
}
type
ClaudeMessageSource
struct
{
type
ClaudeMessageSource
struct
{
...
@@ -82,14 +105,36 @@ type ClaudeMessage struct {
...
@@ -82,14 +105,36 @@ type ClaudeMessage struct {
}
}
func
(
c
*
ClaudeMessage
)
IsStringContent
()
bool
{
func
(
c
*
ClaudeMessage
)
IsStringContent
()
bool
{
if
c
.
Content
==
nil
{
return
false
}
_
,
ok
:=
c
.
Content
.
(
string
)
_
,
ok
:=
c
.
Content
.
(
string
)
return
ok
return
ok
}
}
func
(
c
*
ClaudeMessage
)
GetStringContent
()
string
{
func
(
c
*
ClaudeMessage
)
GetStringContent
()
string
{
if
c
.
IsStringContent
()
{
if
c
.
Content
==
nil
{
return
""
}
switch
c
.
Content
.
(
type
)
{
case
string
:
return
c
.
Content
.
(
string
)
return
c
.
Content
.
(
string
)
case
[]
any
:
var
contentStr
string
for
_
,
contentItem
:=
range
c
.
Content
.
([]
any
)
{
contentMap
,
ok
:=
contentItem
.
(
map
[
string
]
any
)
if
!
ok
{
continue
}
if
contentMap
[
"type"
]
==
ContentTypeText
{
if
subStr
,
ok
:=
contentMap
[
"text"
]
.
(
string
);
ok
{
contentStr
+=
subStr
}
}
}
return
contentStr
}
}
return
""
return
""
}
}
...
@@ -98,15 +143,7 @@ func (c *ClaudeMessage) SetStringContent(content string) {
...
@@ -98,15 +143,7 @@ func (c *ClaudeMessage) SetStringContent(content string) {
}
}
func
(
c
*
ClaudeMessage
)
ParseContent
()
([]
ClaudeMediaMessage
,
error
)
{
func
(
c
*
ClaudeMessage
)
ParseContent
()
([]
ClaudeMediaMessage
,
error
)
{
// map content to []ClaudeMediaMessage
return
common
.
Any2Type
[[]
ClaudeMediaMessage
](
c
.
Content
)
// parse to json
jsonContent
,
_
:=
json
.
Marshal
(
c
.
Content
)
var
contentList
[]
ClaudeMediaMessage
err
:=
json
.
Unmarshal
(
jsonContent
,
&
contentList
)
if
err
!=
nil
{
return
make
([]
ClaudeMediaMessage
,
0
),
err
}
return
contentList
,
nil
}
}
type
Tool
struct
{
type
Tool
struct
{
...
@@ -161,14 +198,8 @@ func (c *ClaudeRequest) SetStringSystem(system string) {
...
@@ -161,14 +198,8 @@ func (c *ClaudeRequest) SetStringSystem(system string) {
}
}
func
(
c
*
ClaudeRequest
)
ParseSystem
()
[]
ClaudeMediaMessage
{
func
(
c
*
ClaudeRequest
)
ParseSystem
()
[]
ClaudeMediaMessage
{
// map content to []ClaudeMediaMessage
mediaContent
,
_
:=
common
.
Any2Type
[[]
ClaudeMediaMessage
](
c
.
System
)
// parse to json
return
mediaContent
jsonContent
,
_
:=
json
.
Marshal
(
c
.
System
)
var
contentList
[]
ClaudeMediaMessage
if
err
:=
json
.
Unmarshal
(
jsonContent
,
&
contentList
);
err
==
nil
{
return
contentList
}
return
make
([]
ClaudeMediaMessage
,
0
)
}
}
type
ClaudeError
struct
{
type
ClaudeError
struct
{
...
...
dto/openai_request.go
View file @
5d7e688b
This diff is collapsed.
Click to expand it.
main.go
View file @
5d7e688b
...
@@ -25,10 +25,10 @@ import (
...
@@ -25,10 +25,10 @@ import (
_
"net/http/pprof"
_
"net/http/pprof"
)
)
//go:embed web/dist
//
go:embed web/dist
var
buildFS
embed
.
FS
var
buildFS
embed
.
FS
//go:embed web/dist/index.html
//
go:embed web/dist/index.html
var
indexPage
[]
byte
var
indexPage
[]
byte
func
main
()
{
func
main
()
{
...
...
relay/channel/ali/text.go
View file @
5d7e688b
...
@@ -96,12 +96,11 @@ func embeddingResponseAli2OpenAI(response *AliEmbeddingResponse, model string) *
...
@@ -96,12 +96,11 @@ func embeddingResponseAli2OpenAI(response *AliEmbeddingResponse, model string) *
}
}
func
responseAli2OpenAI
(
response
*
AliResponse
)
*
dto
.
OpenAITextResponse
{
func
responseAli2OpenAI
(
response
*
AliResponse
)
*
dto
.
OpenAITextResponse
{
content
,
_
:=
json
.
Marshal
(
response
.
Output
.
Text
)
choice
:=
dto
.
OpenAITextResponseChoice
{
choice
:=
dto
.
OpenAITextResponseChoice
{
Index
:
0
,
Index
:
0
,
Message
:
dto
.
Message
{
Message
:
dto
.
Message
{
Role
:
"assistant"
,
Role
:
"assistant"
,
Content
:
conten
t
,
Content
:
response
.
Output
.
Tex
t
,
},
},
FinishReason
:
response
.
Output
.
FinishReason
,
FinishReason
:
response
.
Output
.
FinishReason
,
}
}
...
...
relay/channel/baidu/relay-baidu.go
View file @
5d7e688b
...
@@ -53,12 +53,11 @@ func requestOpenAI2Baidu(request dto.GeneralOpenAIRequest) *BaiduChatRequest {
...
@@ -53,12 +53,11 @@ func requestOpenAI2Baidu(request dto.GeneralOpenAIRequest) *BaiduChatRequest {
}
}
func
responseBaidu2OpenAI
(
response
*
BaiduChatResponse
)
*
dto
.
OpenAITextResponse
{
func
responseBaidu2OpenAI
(
response
*
BaiduChatResponse
)
*
dto
.
OpenAITextResponse
{
content
,
_
:=
json
.
Marshal
(
response
.
Result
)
choice
:=
dto
.
OpenAITextResponseChoice
{
choice
:=
dto
.
OpenAITextResponseChoice
{
Index
:
0
,
Index
:
0
,
Message
:
dto
.
Message
{
Message
:
dto
.
Message
{
Role
:
"assistant"
,
Role
:
"assistant"
,
Content
:
conten
t
,
Content
:
response
.
Resul
t
,
},
},
FinishReason
:
"stop"
,
FinishReason
:
"stop"
,
}
}
...
...
relay/channel/claude/relay-claude.go
View file @
5d7e688b
...
@@ -48,9 +48,9 @@ func RequestOpenAI2ClaudeComplete(textRequest dto.GeneralOpenAIRequest) *dto.Cla
...
@@ -48,9 +48,9 @@ func RequestOpenAI2ClaudeComplete(textRequest dto.GeneralOpenAIRequest) *dto.Cla
prompt
:=
""
prompt
:=
""
for
_
,
message
:=
range
textRequest
.
Messages
{
for
_
,
message
:=
range
textRequest
.
Messages
{
if
message
.
Role
==
"user"
{
if
message
.
Role
==
"user"
{
prompt
+=
fmt
.
Sprintf
(
"
\n\n
Human: %s"
,
message
.
Content
)
prompt
+=
fmt
.
Sprintf
(
"
\n\n
Human: %s"
,
message
.
StringContent
()
)
}
else
if
message
.
Role
==
"assistant"
{
}
else
if
message
.
Role
==
"assistant"
{
prompt
+=
fmt
.
Sprintf
(
"
\n\n
Assistant: %s"
,
message
.
Content
)
prompt
+=
fmt
.
Sprintf
(
"
\n\n
Assistant: %s"
,
message
.
StringContent
()
)
}
else
if
message
.
Role
==
"system"
{
}
else
if
message
.
Role
==
"system"
{
if
prompt
==
""
{
if
prompt
==
""
{
prompt
=
message
.
StringContent
()
prompt
=
message
.
StringContent
()
...
@@ -155,15 +155,13 @@ func RequestOpenAI2ClaudeMessage(textRequest dto.GeneralOpenAIRequest) (*dto.Cla
...
@@ -155,15 +155,13 @@ func RequestOpenAI2ClaudeMessage(textRequest dto.GeneralOpenAIRequest) (*dto.Cla
}
}
if
lastMessage
.
Role
==
message
.
Role
&&
lastMessage
.
Role
!=
"tool"
{
if
lastMessage
.
Role
==
message
.
Role
&&
lastMessage
.
Role
!=
"tool"
{
if
lastMessage
.
IsStringContent
()
&&
message
.
IsStringContent
()
{
if
lastMessage
.
IsStringContent
()
&&
message
.
IsStringContent
()
{
content
,
_
:=
json
.
Marshal
(
strings
.
Trim
(
fmt
.
Sprintf
(
"%s %s"
,
lastMessage
.
StringContent
(),
message
.
StringContent
()),
"
\"
"
))
fmtMessage
.
SetStringContent
(
strings
.
Trim
(
fmt
.
Sprintf
(
"%s %s"
,
lastMessage
.
StringContent
(),
message
.
StringContent
()),
"
\"
"
))
fmtMessage
.
Content
=
content
// delete last message
// delete last message
formatMessages
=
formatMessages
[
:
len
(
formatMessages
)
-
1
]
formatMessages
=
formatMessages
[
:
len
(
formatMessages
)
-
1
]
}
}
}
}
if
fmtMessage
.
Content
==
nil
{
if
fmtMessage
.
Content
==
nil
{
content
,
_
:=
json
.
Marshal
(
"..."
)
fmtMessage
.
SetStringContent
(
"..."
)
fmtMessage
.
Content
=
content
}
}
formatMessages
=
append
(
formatMessages
,
fmtMessage
)
formatMessages
=
append
(
formatMessages
,
fmtMessage
)
lastMessage
=
fmtMessage
lastMessage
=
fmtMessage
...
@@ -397,12 +395,11 @@ func ResponseClaude2OpenAI(reqMode int, claudeResponse *dto.ClaudeResponse) *dto
...
@@ -397,12 +395,11 @@ func ResponseClaude2OpenAI(reqMode int, claudeResponse *dto.ClaudeResponse) *dto
thinkingContent
:=
""
thinkingContent
:=
""
if
reqMode
==
RequestModeCompletion
{
if
reqMode
==
RequestModeCompletion
{
content
,
_
:=
json
.
Marshal
(
strings
.
TrimPrefix
(
claudeResponse
.
Completion
,
" "
))
choice
:=
dto
.
OpenAITextResponseChoice
{
choice
:=
dto
.
OpenAITextResponseChoice
{
Index
:
0
,
Index
:
0
,
Message
:
dto
.
Message
{
Message
:
dto
.
Message
{
Role
:
"assistant"
,
Role
:
"assistant"
,
Content
:
content
,
Content
:
strings
.
TrimPrefix
(
claudeResponse
.
Completion
,
" "
)
,
Name
:
nil
,
Name
:
nil
,
},
},
FinishReason
:
stopReasonClaude2OpenAI
(
claudeResponse
.
StopReason
),
FinishReason
:
stopReasonClaude2OpenAI
(
claudeResponse
.
StopReason
),
...
...
relay/channel/cohere/relay-cohere.go
View file @
5d7e688b
...
@@ -195,11 +195,10 @@ func cohereHandler(c *gin.Context, resp *http.Response, modelName string, prompt
...
@@ -195,11 +195,10 @@ func cohereHandler(c *gin.Context, resp *http.Response, modelName string, prompt
openaiResp
.
Model
=
modelName
openaiResp
.
Model
=
modelName
openaiResp
.
Usage
=
usage
openaiResp
.
Usage
=
usage
content
,
_
:=
json
.
Marshal
(
cohereResp
.
Text
)
openaiResp
.
Choices
=
[]
dto
.
OpenAITextResponseChoice
{
openaiResp
.
Choices
=
[]
dto
.
OpenAITextResponseChoice
{
{
{
Index
:
0
,
Index
:
0
,
Message
:
dto
.
Message
{
Content
:
co
nten
t
,
Role
:
"assistant"
},
Message
:
dto
.
Message
{
Content
:
co
hereResp
.
Tex
t
,
Role
:
"assistant"
},
FinishReason
:
stopReasonCohere2OpenAI
(
cohereResp
.
FinishReason
),
FinishReason
:
stopReasonCohere2OpenAI
(
cohereResp
.
FinishReason
),
},
},
}
}
...
...
relay/channel/coze/dto.go
View file @
5d7e688b
...
@@ -10,7 +10,7 @@ type CozeError struct {
...
@@ -10,7 +10,7 @@ type CozeError struct {
type
CozeEnterMessage
struct
{
type
CozeEnterMessage
struct
{
Role
string
`json:"role"`
Role
string
`json:"role"`
Type
string
`json:"type,omitempty"`
Type
string
`json:"type,omitempty"`
Content
json
.
RawMessage
`json:"content,omitempty"`
Content
any
`json:"content,omitempty"`
MetaData
json
.
RawMessage
`json:"meta_data,omitempty"`
MetaData
json
.
RawMessage
`json:"meta_data,omitempty"`
ContentType
string
`json:"content_type,omitempty"`
ContentType
string
`json:"content_type,omitempty"`
}
}
...
...
relay/channel/dify/relay-dify.go
View file @
5d7e688b
...
@@ -278,12 +278,11 @@ func difyHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInf
...
@@ -278,12 +278,11 @@ func difyHandler(c *gin.Context, resp *http.Response, info *relaycommon.RelayInf
Created
:
common
.
GetTimestamp
(),
Created
:
common
.
GetTimestamp
(),
Usage
:
difyResponse
.
MetaData
.
Usage
,
Usage
:
difyResponse
.
MetaData
.
Usage
,
}
}
content
,
_
:=
json
.
Marshal
(
difyResponse
.
Answer
)
choice
:=
dto
.
OpenAITextResponseChoice
{
choice
:=
dto
.
OpenAITextResponseChoice
{
Index
:
0
,
Index
:
0
,
Message
:
dto
.
Message
{
Message
:
dto
.
Message
{
Role
:
"assistant"
,
Role
:
"assistant"
,
Content
:
content
,
Content
:
difyResponse
.
Answer
,
},
},
FinishReason
:
"stop"
,
FinishReason
:
"stop"
,
}
}
...
...
relay/channel/gemini/relay-gemini.go
View file @
5d7e688b
...
@@ -611,14 +611,13 @@ func responseGeminiChat2OpenAI(response *GeminiChatResponse) *dto.OpenAITextResp
...
@@ -611,14 +611,13 @@ func responseGeminiChat2OpenAI(response *GeminiChatResponse) *dto.OpenAITextResp
Created
:
common
.
GetTimestamp
(),
Created
:
common
.
GetTimestamp
(),
Choices
:
make
([]
dto
.
OpenAITextResponseChoice
,
0
,
len
(
response
.
Candidates
)),
Choices
:
make
([]
dto
.
OpenAITextResponseChoice
,
0
,
len
(
response
.
Candidates
)),
}
}
content
,
_
:=
json
.
Marshal
(
""
)
isToolCall
:=
false
isToolCall
:=
false
for
_
,
candidate
:=
range
response
.
Candidates
{
for
_
,
candidate
:=
range
response
.
Candidates
{
choice
:=
dto
.
OpenAITextResponseChoice
{
choice
:=
dto
.
OpenAITextResponseChoice
{
Index
:
int
(
candidate
.
Index
),
Index
:
int
(
candidate
.
Index
),
Message
:
dto
.
Message
{
Message
:
dto
.
Message
{
Role
:
"assistant"
,
Role
:
"assistant"
,
Content
:
content
,
Content
:
""
,
},
},
FinishReason
:
constant
.
FinishReasonStop
,
FinishReason
:
constant
.
FinishReasonStop
,
}
}
...
...
relay/channel/palm/relay-palm.go
View file @
5d7e688b
...
@@ -45,12 +45,11 @@ func responsePaLM2OpenAI(response *PaLMChatResponse) *dto.OpenAITextResponse {
...
@@ -45,12 +45,11 @@ func responsePaLM2OpenAI(response *PaLMChatResponse) *dto.OpenAITextResponse {
Choices
:
make
([]
dto
.
OpenAITextResponseChoice
,
0
,
len
(
response
.
Candidates
)),
Choices
:
make
([]
dto
.
OpenAITextResponseChoice
,
0
,
len
(
response
.
Candidates
)),
}
}
for
i
,
candidate
:=
range
response
.
Candidates
{
for
i
,
candidate
:=
range
response
.
Candidates
{
content
,
_
:=
json
.
Marshal
(
candidate
.
Content
)
choice
:=
dto
.
OpenAITextResponseChoice
{
choice
:=
dto
.
OpenAITextResponseChoice
{
Index
:
i
,
Index
:
i
,
Message
:
dto
.
Message
{
Message
:
dto
.
Message
{
Role
:
"assistant"
,
Role
:
"assistant"
,
Content
:
content
,
Content
:
c
andidate
.
C
ontent
,
},
},
FinishReason
:
"stop"
,
FinishReason
:
"stop"
,
}
}
...
...
relay/channel/tencent/relay-tencent.go
View file @
5d7e688b
...
@@ -56,12 +56,11 @@ func responseTencent2OpenAI(response *TencentChatResponse) *dto.OpenAITextRespon
...
@@ -56,12 +56,11 @@ func responseTencent2OpenAI(response *TencentChatResponse) *dto.OpenAITextRespon
},
},
}
}
if
len
(
response
.
Choices
)
>
0
{
if
len
(
response
.
Choices
)
>
0
{
content
,
_
:=
json
.
Marshal
(
response
.
Choices
[
0
]
.
Messages
.
Content
)
choice
:=
dto
.
OpenAITextResponseChoice
{
choice
:=
dto
.
OpenAITextResponseChoice
{
Index
:
0
,
Index
:
0
,
Message
:
dto
.
Message
{
Message
:
dto
.
Message
{
Role
:
"assistant"
,
Role
:
"assistant"
,
Content
:
c
ontent
,
Content
:
response
.
Choices
[
0
]
.
Messages
.
C
ontent
,
},
},
FinishReason
:
response
.
Choices
[
0
]
.
FinishReason
,
FinishReason
:
response
.
Choices
[
0
]
.
FinishReason
,
}
}
...
...
relay/channel/xunfei/relay-xunfei.go
View file @
5d7e688b
...
@@ -61,12 +61,11 @@ func responseXunfei2OpenAI(response *XunfeiChatResponse) *dto.OpenAITextResponse
...
@@ -61,12 +61,11 @@ func responseXunfei2OpenAI(response *XunfeiChatResponse) *dto.OpenAITextResponse
},
},
}
}
}
}
content
,
_
:=
json
.
Marshal
(
response
.
Payload
.
Choices
.
Text
[
0
]
.
Content
)
choice
:=
dto
.
OpenAITextResponseChoice
{
choice
:=
dto
.
OpenAITextResponseChoice
{
Index
:
0
,
Index
:
0
,
Message
:
dto
.
Message
{
Message
:
dto
.
Message
{
Role
:
"assistant"
,
Role
:
"assistant"
,
Content
:
c
ontent
,
Content
:
response
.
Payload
.
Choices
.
Text
[
0
]
.
C
ontent
,
},
},
FinishReason
:
constant
.
FinishReasonStop
,
FinishReason
:
constant
.
FinishReasonStop
,
}
}
...
...
relay/channel/zhipu/relay-zhipu.go
View file @
5d7e688b
...
@@ -108,12 +108,11 @@ func responseZhipu2OpenAI(response *ZhipuResponse) *dto.OpenAITextResponse {
...
@@ -108,12 +108,11 @@ func responseZhipu2OpenAI(response *ZhipuResponse) *dto.OpenAITextResponse {
Usage
:
response
.
Data
.
Usage
,
Usage
:
response
.
Data
.
Usage
,
}
}
for
i
,
choice
:=
range
response
.
Data
.
Choices
{
for
i
,
choice
:=
range
response
.
Data
.
Choices
{
content
,
_
:=
json
.
Marshal
(
strings
.
Trim
(
choice
.
Content
,
"
\"
"
))
openaiChoice
:=
dto
.
OpenAITextResponseChoice
{
openaiChoice
:=
dto
.
OpenAITextResponseChoice
{
Index
:
i
,
Index
:
i
,
Message
:
dto
.
Message
{
Message
:
dto
.
Message
{
Role
:
choice
.
Role
,
Role
:
choice
.
Role
,
Content
:
content
,
Content
:
strings
.
Trim
(
choice
.
Content
,
"
\"
"
)
,
},
},
FinishReason
:
""
,
FinishReason
:
""
,
}
}
...
...
service/token_counter.go
View file @
5d7e688b
...
@@ -261,12 +261,16 @@ func CountTokenClaudeMessages(messages []dto.ClaudeMessage, model string, stream
...
@@ -261,12 +261,16 @@ func CountTokenClaudeMessages(messages []dto.ClaudeMessage, model string, stream
//}
//}
tokenNum
+=
1000
tokenNum
+=
1000
case
"tool_use"
:
case
"tool_use"
:
tokenNum
+=
getTokenNum
(
tokenEncoder
,
mediaMessage
.
Name
)
if
mediaMessage
.
Input
!=
nil
{
inputJSON
,
_
:=
json
.
Marshal
(
mediaMessage
.
Input
)
tokenNum
+=
getTokenNum
(
tokenEncoder
,
mediaMessage
.
Name
)
tokenNum
+=
getTokenNum
(
tokenEncoder
,
string
(
inputJSON
))
inputJSON
,
_
:=
json
.
Marshal
(
mediaMessage
.
Input
)
tokenNum
+=
getTokenNum
(
tokenEncoder
,
string
(
inputJSON
))
}
case
"tool_result"
:
case
"tool_result"
:
contentJSON
,
_
:=
json
.
Marshal
(
mediaMessage
.
Content
)
if
mediaMessage
.
Content
!=
nil
{
tokenNum
+=
getTokenNum
(
tokenEncoder
,
string
(
contentJSON
))
contentJSON
,
_
:=
json
.
Marshal
(
mediaMessage
.
Content
)
tokenNum
+=
getTokenNum
(
tokenEncoder
,
string
(
contentJSON
))
}
}
}
}
}
}
}
...
@@ -386,7 +390,7 @@ func CountTokenMessages(info *relaycommon.RelayInfo, messages []dto.Message, mod
...
@@ -386,7 +390,7 @@ func CountTokenMessages(info *relaycommon.RelayInfo, messages []dto.Message, mod
for
_
,
message
:=
range
messages
{
for
_
,
message
:=
range
messages
{
tokenNum
+=
tokensPerMessage
tokenNum
+=
tokensPerMessage
tokenNum
+=
getTokenNum
(
tokenEncoder
,
message
.
Role
)
tokenNum
+=
getTokenNum
(
tokenEncoder
,
message
.
Role
)
if
len
(
message
.
Content
)
>
0
{
if
message
.
Content
!=
nil
{
if
message
.
Name
!=
nil
{
if
message
.
Name
!=
nil
{
tokenNum
+=
tokensPerName
tokenNum
+=
tokensPerName
tokenNum
+=
getTokenNum
(
tokenEncoder
,
*
message
.
Name
)
tokenNum
+=
getTokenNum
(
tokenEncoder
,
*
message
.
Name
)
...
...
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