mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-02 05:47:31 +08:00
## Summary Relate to #17284 . - Add `usage_parser.go`: `extractOpenAIUsage` / `extractOpenAIStreamUsage` with multi-field fallback (`prompt_tokens`/`input_tokens`, `completion_tokens`/`output_tokens`). - Add `parser_config.go`: `ParserConfig` struct and `OpenAIParserConfig`. - Add `response_handler.go`: `HandleNonStreamingResponse` / `HandleStreamingResponse` as single entry points that extract usage, populate `chatConfig.UsageResult`, and emit a `StreamUsage` log. - Extend `base_model.go` with `doRequest` / `doStreamRequest`. - Migrate `deepseek` driver to shared handlers, cutting ~100 lines. - Add `usage_parser_test.go`. --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Haruko386 <tryeverypossible@163.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
78 lines
2.3 KiB
Go
78 lines
2.3 KiB
Go
//
|
|
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
|
|
package models
|
|
|
|
import "encoding/json"
|
|
|
|
// extractOpenAIUsage extracts token usage from an OpenAI-compatible
|
|
// non-streaming response body. Returns (nil, false) when the response
|
|
// carries no usable usage block.
|
|
func extractOpenAIUsage(body map[string]any) (*TokenUsage, bool) {
|
|
rawUsage, ok := body["usage"].(map[string]any)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
|
|
return tokenUsageFromRaw(rawUsage), true
|
|
}
|
|
|
|
// extractOpenAIStreamUsage extracts token usage from one OpenAI-compatible
|
|
// streaming event. A missing or null usage field is not an error.
|
|
func extractOpenAIStreamUsage(event map[string]any) (*TokenUsage, bool) {
|
|
rawUsage, ok := event["usage"].(map[string]any)
|
|
if !ok || rawUsage == nil {
|
|
return nil, false
|
|
}
|
|
|
|
return tokenUsageFromRaw(rawUsage), true
|
|
}
|
|
|
|
func tokenUsageFromRaw(rawUsage map[string]any) *TokenUsage {
|
|
usage := &TokenUsage{}
|
|
usage.PromptTokens = extractToken(rawUsage, "prompt_tokens", "input_tokens")
|
|
usage.CompletionTokens = extractToken(rawUsage, "completion_tokens", "output_tokens")
|
|
usage.TotalTokens = extractToken(rawUsage, "total_tokens")
|
|
|
|
if usage.TotalTokens == 0 {
|
|
usage.TotalTokens = usage.PromptTokens + usage.CompletionTokens
|
|
}
|
|
|
|
return usage
|
|
}
|
|
|
|
// extractToken reads a numeric field from a map, trying each key in order.
|
|
// Returns 0 when no key is present or the value is not a number.
|
|
func extractToken(m map[string]any, keys ...string) int {
|
|
for _, k := range keys {
|
|
if v, ok := m[k]; ok {
|
|
switch val := v.(type) {
|
|
case float64:
|
|
return int(val)
|
|
case int:
|
|
return val
|
|
case int64:
|
|
return int(val)
|
|
case json.Number:
|
|
if n, err := val.Int64(); err == nil {
|
|
return int(n)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return 0
|
|
}
|