Skip to content

Commit 64ba6c5

Browse files
hustxiayangnacx
andauthored
fix: add reasoning tokens into token metrics in stream mode for gemini models (#2259)
**Description** The previous implementation on tokens tracking is like ``` if chunk.UsageMetadata.CandidatesTokenCount >= 0 { tokenUsage.SetOutputTokens(uint32(chunk.UsageMetadata.CandidatesTokenCount)) //nolint:gosec } ``` The output tokens should include both the reasoning tokens and the candidates tokens. Thus fix this by reusing the the variable `usage`, just like non-streaming mode. Also updated the tests to include reasoning tokens --------- Signed-off-by: yxia216 <yxia216@bloomberg.net> Co-authored-by: Ignasi Barrera <ignasi@tetrate.io>
1 parent 08ba26e commit 64ba6c5

2 files changed

Lines changed: 11 additions & 21 deletions

File tree

internal/translator/openai_gcpvertexai.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -249,21 +249,11 @@ func (o *openAIToGCPVertexAITranslatorV1ChatCompletion) handleStreamingResponse(
249249
span.RecordResponseChunk(usageChunk)
250250
}
251251

252-
if chunk.UsageMetadata.PromptTokenCount >= 0 {
253-
tokenUsage.SetInputTokens(uint32(chunk.UsageMetadata.PromptTokenCount)) //nolint:gosec
254-
}
255-
if chunk.UsageMetadata.CandidatesTokenCount >= 0 {
256-
tokenUsage.SetOutputTokens(uint32(chunk.UsageMetadata.CandidatesTokenCount)) //nolint:gosec
257-
}
258-
if chunk.UsageMetadata.TotalTokenCount >= 0 {
259-
tokenUsage.SetTotalTokens(uint32(chunk.UsageMetadata.TotalTokenCount)) //nolint:gosec
260-
}
261-
if chunk.UsageMetadata.CachedContentTokenCount >= 0 {
262-
tokenUsage.SetCachedInputTokens(uint32(chunk.UsageMetadata.CachedContentTokenCount)) //nolint:gosec
263-
}
264-
if chunk.UsageMetadata.ThoughtsTokenCount >= 0 {
265-
tokenUsage.SetReasoningTokens(uint32(chunk.UsageMetadata.ThoughtsTokenCount)) //nolint:gosec
266-
}
252+
tokenUsage.SetInputTokens(uint32(usage.PromptTokens)) //nolint:gosec
253+
tokenUsage.SetOutputTokens(uint32(usage.CompletionTokens)) //nolint:gosec
254+
tokenUsage.SetTotalTokens(uint32(usage.TotalTokens)) //nolint:gosec
255+
tokenUsage.SetCachedInputTokens(uint32(usage.PromptTokensDetails.CachedTokens)) //nolint:gosec
256+
tokenUsage.SetReasoningTokens(uint32(usage.CompletionTokensDetails.ReasoningTokens)) //nolint:gosec
267257
}
268258
}
269259

internal/translator/openai_gcpvertexai_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,7 @@ data: [DONE]
12301230
},
12311231
body: `data: {"candidates":[{"content":{"parts":[{"text":"let me think step by step and reply you.", "thought": true}]}}]}
12321232
1233-
data: {"candidates":[{"content":{"parts":[{"text":"Hello"}]}}],"usageMetadata":{"promptTokenCount":5,"candidatesTokenCount":3,"totalTokenCount":8}}`,
1233+
data: {"candidates":[{"content":{"parts":[{"text":"Hello"}]}}],"usageMetadata":{"promptTokenCount":5,"candidatesTokenCount":3,"totalTokenCount":18,"thoughtsTokenCount":10}}`,
12341234
stream: true,
12351235
endOfStream: true,
12361236
wantError: false,
@@ -1239,11 +1239,11 @@ data: {"candidates":[{"content":{"parts":[{"text":"Hello"}]}}],"usageMetadata":{
12391239
12401240
data: {"choices":[{"index":0,"delta":{"content":"Hello","role":"assistant"}}],"object":"chat.completion.chunk"}
12411241
1242-
data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":5,"completion_tokens":3,"total_tokens":8,"completion_tokens_details":{},"prompt_tokens_details":{}}}
1242+
data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":5,"completion_tokens":13,"total_tokens":18,"completion_tokens_details":{"reasoning_tokens":10},"prompt_tokens_details":{}}}
12431243
12441244
data: [DONE]
12451245
`),
1246-
wantTokenUsage: tokenUsageFrom(5, 0, -1, 3, 8, 0), // Does not support Cache Creation.
1246+
wantTokenUsage: tokenUsageFrom(5, 0, -1, 13, 18, 10), // Does not support Cache Creation.
12471247
},
12481248
{
12491249
name: "stream chunks with thought signature on text part",
@@ -1252,7 +1252,7 @@ data: [DONE]
12521252
},
12531253
body: `data: {"candidates":[{"content":{"parts":[{"text":"let me think about this.", "thought": true}]}}]}
12541254
1255-
data: {"candidates":[{"content":{"parts":[{"text":"The answer is 42.", "thoughtSignature": "dGVzdHNpZ25hdHVyZQ=="}]}}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":8,"totalTokenCount":18}}`,
1255+
data: {"candidates":[{"content":{"parts":[{"text":"The answer is 42.", "thoughtSignature": "dGVzdHNpZ25hdHVyZQ=="}]}}],"usageMetadata":{"promptTokenCount":10,"candidatesTokenCount":8,"totalTokenCount":33,"thoughtsTokenCount":15}}`,
12561256
stream: true,
12571257
endOfStream: true,
12581258
wantError: false,
@@ -1261,11 +1261,11 @@ data: {"candidates":[{"content":{"parts":[{"text":"The answer is 42.", "thoughtS
12611261
12621262
data: {"choices":[{"index":0,"delta":{"content":"The answer is 42.","role":"assistant","reasoning_content":{"signature":"dGVzdHNpZ25hdHVyZQ=="}}}],"object":"chat.completion.chunk"}
12631263
1264-
data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":10,"completion_tokens":8,"total_tokens":18,"completion_tokens_details":{},"prompt_tokens_details":{}}}
1264+
data: {"choices":[],"object":"chat.completion.chunk","usage":{"prompt_tokens":10,"completion_tokens":23,"total_tokens":33,"completion_tokens_details":{"reasoning_tokens":15},"prompt_tokens_details":{}}}
12651265
12661266
data: [DONE]
12671267
`),
1268-
wantTokenUsage: tokenUsageFrom(10, 0, -1, 8, 18, 0),
1268+
wantTokenUsage: tokenUsageFrom(10, 0, -1, 23, 33, 15),
12691269
},
12701270
}
12711271

0 commit comments

Comments
 (0)