mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-05 08:55:28 +08:00
Track byte sizes of inline-uploaded files during PreprocessInlineFileInputs and convert them to conservative token estimates (bytes/3). RefFileTokens is threaded through StandardRequest into all OpenAI chat/responses usage builders so returned prompt_tokens/input_tokens reflect the full upstream context cost including attached files.
27 lines
772 B
Go
27 lines
772 B
Go
package responses
|
|
|
|
// addRefFileTokensToUsage adds inline-uploaded file token estimates to an existing
|
|
// usage map inside a response object. This keeps the token accounting aware of file
|
|
// content that the upstream model processes but that is not part of the prompt text.
|
|
func addRefFileTokensToUsage(obj map[string]any, refFileTokens int) {
|
|
if refFileTokens <= 0 || obj == nil {
|
|
return
|
|
}
|
|
usage, ok := obj["usage"].(map[string]any)
|
|
if !ok || usage == nil {
|
|
return
|
|
}
|
|
for _, key := range []string{"input_tokens", "prompt_tokens"} {
|
|
if v, ok := usage[key]; ok {
|
|
if n, ok := v.(int); ok {
|
|
usage[key] = n + refFileTokens
|
|
}
|
|
}
|
|
}
|
|
if v, ok := usage["total_tokens"]; ok {
|
|
if n, ok := v.(int); ok {
|
|
usage["total_tokens"] = n + refFileTokens
|
|
}
|
|
}
|
|
}
|