mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-19 08:19:22 +08:00
fix(antigravity): include thoughts token count in openai completion tokens
- Add `thoughtsTokenCount` to `candidatesTokenCount` when calculating `usage.completion_tokens` for Antigravity OpenAI response translation Closes: #6463
This commit is contained in:
@@ -285,7 +285,7 @@ func resolveOpenAIFinishReason(params *convertCliResponseToOpenAIChatParams) (fi
|
||||
|
||||
func setOpenAIUsageMetadata(template []byte, usageResult gjson.Result) []byte {
|
||||
cachedTokenCount := usageResult.Get("cachedContentTokenCount").Int()
|
||||
template, _ = sjson.SetBytes(template, "usage.completion_tokens", usageResult.Get("candidatesTokenCount").Int())
|
||||
template, _ = sjson.SetBytes(template, "usage.completion_tokens", usageResult.Get("candidatesTokenCount").Int()+usageResult.Get("thoughtsTokenCount").Int())
|
||||
if totalTokenCountResult := usageResult.Get("totalTokenCount"); totalTokenCountResult.Exists() {
|
||||
template, _ = sjson.SetBytes(template, "usage.total_tokens", totalTokenCountResult.Int())
|
||||
}
|
||||
|
||||
@@ -128,17 +128,44 @@ func TestNoFinishReasonOnIntermediateChunks(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertAntigravityResponseToOpenAIIncludesZeroCompletionTokensWhenMissing(t *testing.T) {
|
||||
var param any
|
||||
chunk := []byte(`{"response":{"usageMetadata":{"promptTokenCount":16,"thoughtsTokenCount":42,"totalTokenCount":58}}}`)
|
||||
|
||||
result := ConvertAntigravityResponseToOpenAI(context.Background(), "model", nil, nil, chunk, ¶m)
|
||||
if len(result) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(result))
|
||||
func TestConvertAntigravityResponseToOpenAICompletionTokensIncludeThoughts(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
chunk string
|
||||
wantOutput int64
|
||||
}{
|
||||
{
|
||||
name: "candidates and thoughts",
|
||||
chunk: `{"response":{"usageMetadata":{"promptTokenCount":16,"candidatesTokenCount":5,"thoughtsTokenCount":42,"totalTokenCount":63}}}`,
|
||||
wantOutput: 47,
|
||||
},
|
||||
{
|
||||
name: "thoughts without candidates",
|
||||
chunk: `{"response":{"usageMetadata":{"promptTokenCount":16,"thoughtsTokenCount":42,"totalTokenCount":58}}}`,
|
||||
wantOutput: 42,
|
||||
},
|
||||
}
|
||||
completionTokens := gjson.GetBytes(result[0], "usage.completion_tokens")
|
||||
if !completionTokens.Exists() || completionTokens.Int() != 0 {
|
||||
t.Fatalf("completion_tokens = %s, want present with value 0. Output: %s", completionTokens.Raw, result[0])
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var param any
|
||||
result := ConvertAntigravityResponseToOpenAI(context.Background(), "model", nil, nil, []byte(tt.chunk), ¶m)
|
||||
if len(result) != 1 {
|
||||
t.Fatalf("expected 1 result, got %d", len(result))
|
||||
}
|
||||
completionTokens := gjson.GetBytes(result[0], "usage.completion_tokens")
|
||||
if !completionTokens.Exists() || completionTokens.Int() != tt.wantOutput {
|
||||
t.Fatalf("completion_tokens = %s, want present with value %d. Output: %s", completionTokens.Raw, tt.wantOutput, result[0])
|
||||
}
|
||||
if got := gjson.GetBytes(result[0], "usage.completion_tokens_details.reasoning_tokens").Int(); got != 42 {
|
||||
t.Fatalf("reasoning_tokens = %d, want 42. Output: %s", got, result[0])
|
||||
}
|
||||
promptTokens := gjson.GetBytes(result[0], "usage.prompt_tokens").Int()
|
||||
totalTokens := gjson.GetBytes(result[0], "usage.total_tokens").Int()
|
||||
if promptTokens+completionTokens.Int() != totalTokens {
|
||||
t.Fatalf("prompt_tokens + completion_tokens = %d, total_tokens = %d", promptTokens+completionTokens.Int(), totalTokens)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user