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
c0df3876
authored
May 29, 2025
by
neotf
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: enhance token usage details for upstream OpenRouter
parent
ac9df0c1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
40 deletions
+48
-40
dto/openai_request.go
+39
-37
relay/channel/openai/adaptor.go
+3
-0
service/convert.go
+6
-3
No files found.
dto/openai_request.go
View file @
c0df3876
...
...
@@ -18,43 +18,45 @@ type FormatJsonSchema struct {
}
type
GeneralOpenAIRequest
struct
{
Model
string
`json:"model,omitempty"`
Messages
[]
Message
`json:"messages,omitempty"`
Prompt
any
`json:"prompt,omitempty"`
Prefix
any
`json:"prefix,omitempty"`
Suffix
any
`json:"suffix,omitempty"`
Stream
bool
`json:"stream,omitempty"`
StreamOptions
*
StreamOptions
`json:"stream_options,omitempty"`
MaxTokens
uint
`json:"max_tokens,omitempty"`
MaxCompletionTokens
uint
`json:"max_completion_tokens,omitempty"`
ReasoningEffort
string
`json:"reasoning_effort,omitempty"`
//Reasoning json.RawMessage `json:"reasoning,omitempty"`
Temperature
*
float64
`json:"temperature,omitempty"`
TopP
float64
`json:"top_p,omitempty"`
TopK
int
`json:"top_k,omitempty"`
Stop
any
`json:"stop,omitempty"`
N
int
`json:"n,omitempty"`
Input
any
`json:"input,omitempty"`
Instruction
string
`json:"instruction,omitempty"`
Size
string
`json:"size,omitempty"`
Functions
any
`json:"functions,omitempty"`
FrequencyPenalty
float64
`json:"frequency_penalty,omitempty"`
PresencePenalty
float64
`json:"presence_penalty,omitempty"`
ResponseFormat
*
ResponseFormat
`json:"response_format,omitempty"`
EncodingFormat
any
`json:"encoding_format,omitempty"`
Seed
float64
`json:"seed,omitempty"`
ParallelTooCalls
*
bool
`json:"parallel_tool_calls,omitempty"`
Tools
[]
ToolCallRequest
`json:"tools,omitempty"`
ToolChoice
any
`json:"tool_choice,omitempty"`
User
string
`json:"user,omitempty"`
LogProbs
bool
`json:"logprobs,omitempty"`
TopLogProbs
int
`json:"top_logprobs,omitempty"`
Dimensions
int
`json:"dimensions,omitempty"`
Modalities
any
`json:"modalities,omitempty"`
Audio
any
`json:"audio,omitempty"`
EnableThinking
any
`json:"enable_thinking,omitempty"`
// ali
ExtraBody
any
`json:"extra_body,omitempty"`
WebSearchOptions
*
WebSearchOptions
`json:"web_search_options,omitempty"`
Model
string
`json:"model,omitempty"`
Messages
[]
Message
`json:"messages,omitempty"`
Prompt
any
`json:"prompt,omitempty"`
Prefix
any
`json:"prefix,omitempty"`
Suffix
any
`json:"suffix,omitempty"`
Stream
bool
`json:"stream,omitempty"`
StreamOptions
*
StreamOptions
`json:"stream_options,omitempty"`
MaxTokens
uint
`json:"max_tokens,omitempty"`
MaxCompletionTokens
uint
`json:"max_completion_tokens,omitempty"`
ReasoningEffort
string
`json:"reasoning_effort,omitempty"`
Temperature
*
float64
`json:"temperature,omitempty"`
TopP
float64
`json:"top_p,omitempty"`
TopK
int
`json:"top_k,omitempty"`
Stop
any
`json:"stop,omitempty"`
N
int
`json:"n,omitempty"`
Input
any
`json:"input,omitempty"`
Instruction
string
`json:"instruction,omitempty"`
Size
string
`json:"size,omitempty"`
Functions
any
`json:"functions,omitempty"`
FrequencyPenalty
float64
`json:"frequency_penalty,omitempty"`
PresencePenalty
float64
`json:"presence_penalty,omitempty"`
ResponseFormat
*
ResponseFormat
`json:"response_format,omitempty"`
EncodingFormat
any
`json:"encoding_format,omitempty"`
Seed
float64
`json:"seed,omitempty"`
ParallelTooCalls
*
bool
`json:"parallel_tool_calls,omitempty"`
Tools
[]
ToolCallRequest
`json:"tools,omitempty"`
ToolChoice
any
`json:"tool_choice,omitempty"`
User
string
`json:"user,omitempty"`
LogProbs
bool
`json:"logprobs,omitempty"`
TopLogProbs
int
`json:"top_logprobs,omitempty"`
Dimensions
int
`json:"dimensions,omitempty"`
Modalities
any
`json:"modalities,omitempty"`
Audio
any
`json:"audio,omitempty"`
EnableThinking
any
`json:"enable_thinking,omitempty"`
// ali
ExtraBody
any
`json:"extra_body,omitempty"`
WebSearchOptions
*
WebSearchOptions
`json:"web_search_options,omitempty"`
// OpenRouter Params
Usage
json
.
RawMessage
`json:"usage,omitempty"`
Reasoning
json
.
RawMessage
`json:"reasoning,omitempty"`
}
type
ToolCallRequest
struct
{
...
...
relay/channel/openai/adaptor.go
View file @
c0df3876
...
...
@@ -152,6 +152,9 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
if
info
.
ChannelType
!=
common
.
ChannelTypeOpenAI
&&
info
.
ChannelType
!=
common
.
ChannelTypeAzure
{
request
.
StreamOptions
=
nil
}
if
info
.
ChannelType
==
common
.
ChannelTypeOpenRouter
{
request
.
Usage
=
json
.
RawMessage
(
"{
\"
include
\"
: true}"
)
}
if
strings
.
HasPrefix
(
request
.
Model
,
"o"
)
{
if
request
.
MaxCompletionTokens
==
0
&&
request
.
MaxTokens
!=
0
{
request
.
MaxCompletionTokens
=
request
.
MaxTokens
...
...
service/convert.go
View file @
c0df3876
...
...
@@ -246,12 +246,15 @@ func StreamResponseOpenAI2Claude(openAIResponse *dto.ChatCompletionsStreamRespon
}
if
info
.
Done
{
claudeResponses
=
append
(
claudeResponses
,
generateStopBlock
(
info
.
ClaudeConvertInfo
.
Index
))
if
info
.
ClaudeConvertInfo
.
Usage
!=
nil
{
oaiUsage
:=
info
.
ClaudeConvertInfo
.
Usage
if
oaiUsage
!=
nil
{
claudeResponses
=
append
(
claudeResponses
,
&
dto
.
ClaudeResponse
{
Type
:
"message_delta"
,
Usage
:
&
dto
.
ClaudeUsage
{
InputTokens
:
info
.
ClaudeConvertInfo
.
Usage
.
PromptTokens
,
OutputTokens
:
info
.
ClaudeConvertInfo
.
Usage
.
CompletionTokens
,
InputTokens
:
oaiUsage
.
PromptTokens
,
OutputTokens
:
oaiUsage
.
CompletionTokens
,
CacheCreationInputTokens
:
oaiUsage
.
PromptTokensDetails
.
CachedCreationTokens
,
CacheReadInputTokens
:
oaiUsage
.
PromptTokensDetails
.
CachedTokens
,
},
Delta
:
&
dto
.
ClaudeMediaMessage
{
StopReason
:
common
.
GetPointer
[
string
](
stopReasonOpenAI2Claude
(
info
.
FinishReason
)),
...
...
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