feat: enhance token usage details for upstream OpenRouter
This commit is contained in:
@@ -28,7 +28,6 @@ type GeneralOpenAIRequest struct {
|
|||||||
MaxTokens uint `json:"max_tokens,omitempty"`
|
MaxTokens uint `json:"max_tokens,omitempty"`
|
||||||
MaxCompletionTokens uint `json:"max_completion_tokens,omitempty"`
|
MaxCompletionTokens uint `json:"max_completion_tokens,omitempty"`
|
||||||
ReasoningEffort string `json:"reasoning_effort,omitempty"`
|
ReasoningEffort string `json:"reasoning_effort,omitempty"`
|
||||||
//Reasoning json.RawMessage `json:"reasoning,omitempty"`
|
|
||||||
Temperature *float64 `json:"temperature,omitempty"`
|
Temperature *float64 `json:"temperature,omitempty"`
|
||||||
TopP float64 `json:"top_p,omitempty"`
|
TopP float64 `json:"top_p,omitempty"`
|
||||||
TopK int `json:"top_k,omitempty"`
|
TopK int `json:"top_k,omitempty"`
|
||||||
@@ -55,6 +54,9 @@ type GeneralOpenAIRequest struct {
|
|||||||
EnableThinking any `json:"enable_thinking,omitempty"` // ali
|
EnableThinking any `json:"enable_thinking,omitempty"` // ali
|
||||||
ExtraBody any `json:"extra_body,omitempty"`
|
ExtraBody any `json:"extra_body,omitempty"`
|
||||||
WebSearchOptions *WebSearchOptions `json:"web_search_options,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 {
|
type ToolCallRequest struct {
|
||||||
|
|||||||
@@ -152,6 +152,9 @@ func (a *Adaptor) ConvertOpenAIRequest(c *gin.Context, info *relaycommon.RelayIn
|
|||||||
if info.ChannelType != common.ChannelTypeOpenAI && info.ChannelType != common.ChannelTypeAzure {
|
if info.ChannelType != common.ChannelTypeOpenAI && info.ChannelType != common.ChannelTypeAzure {
|
||||||
request.StreamOptions = nil
|
request.StreamOptions = nil
|
||||||
}
|
}
|
||||||
|
if info.ChannelType == common.ChannelTypeOpenRouter {
|
||||||
|
request.Usage = json.RawMessage("{\"include\": true}")
|
||||||
|
}
|
||||||
if strings.HasPrefix(request.Model, "o") {
|
if strings.HasPrefix(request.Model, "o") {
|
||||||
if request.MaxCompletionTokens == 0 && request.MaxTokens != 0 {
|
if request.MaxCompletionTokens == 0 && request.MaxTokens != 0 {
|
||||||
request.MaxCompletionTokens = request.MaxTokens
|
request.MaxCompletionTokens = request.MaxTokens
|
||||||
|
|||||||
@@ -246,12 +246,15 @@ func StreamResponseOpenAI2Claude(openAIResponse *dto.ChatCompletionsStreamRespon
|
|||||||
}
|
}
|
||||||
if info.Done {
|
if info.Done {
|
||||||
claudeResponses = append(claudeResponses, generateStopBlock(info.ClaudeConvertInfo.Index))
|
claudeResponses = append(claudeResponses, generateStopBlock(info.ClaudeConvertInfo.Index))
|
||||||
if info.ClaudeConvertInfo.Usage != nil {
|
oaiUsage := info.ClaudeConvertInfo.Usage
|
||||||
|
if oaiUsage != nil {
|
||||||
claudeResponses = append(claudeResponses, &dto.ClaudeResponse{
|
claudeResponses = append(claudeResponses, &dto.ClaudeResponse{
|
||||||
Type: "message_delta",
|
Type: "message_delta",
|
||||||
Usage: &dto.ClaudeUsage{
|
Usage: &dto.ClaudeUsage{
|
||||||
InputTokens: info.ClaudeConvertInfo.Usage.PromptTokens,
|
InputTokens: oaiUsage.PromptTokens,
|
||||||
OutputTokens: info.ClaudeConvertInfo.Usage.CompletionTokens,
|
OutputTokens: oaiUsage.CompletionTokens,
|
||||||
|
CacheCreationInputTokens: oaiUsage.PromptTokensDetails.CachedCreationTokens,
|
||||||
|
CacheReadInputTokens: oaiUsage.PromptTokensDetails.CachedTokens,
|
||||||
},
|
},
|
||||||
Delta: &dto.ClaudeMediaMessage{
|
Delta: &dto.ClaudeMediaMessage{
|
||||||
StopReason: common.GetPointer[string](stopReasonOpenAI2Claude(info.FinishReason)),
|
StopReason: common.GetPointer[string](stopReasonOpenAI2Claude(info.FinishReason)),
|
||||||
|
|||||||
Reference in New Issue
Block a user