mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-03 04:50:52 +08:00
feat(translators): add token counting support for Claude and Gemini responses
- Implemented `TokenCount` transform method across translators to calculate token usage. - Integrated token counting logic into executor pipelines for Claude, Gemini, and CLI translators. - Added corresponding API endpoints and handlers (`/messages/count_tokens`) for token usage retrieval. - Enhanced translation registry to support `TokenCount` functionality alongside existing response types.
This commit is contained in:
@@ -7,6 +7,8 @@ package geminiCLI
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
@@ -47,3 +49,7 @@ func ConvertGeminiResponseToGeminiCLINonStream(_ context.Context, _ string, orig
|
||||
rawJSON, _ = sjson.SetRawBytes([]byte(json), "response", rawJSON)
|
||||
return string(rawJSON)
|
||||
}
|
||||
|
||||
func GeminiCLITokenCount(ctx context.Context, count int64) string {
|
||||
return fmt.Sprintf(`{"totalTokens":%d,"promptTokensDetails":[{"modality":"TEXT","tokenCount":%d}]}`, count, count)
|
||||
}
|
||||
|
||||
@@ -12,8 +12,9 @@ func init() {
|
||||
Gemini,
|
||||
ConvertGeminiCLIRequestToGemini,
|
||||
interfaces.TranslateResponse{
|
||||
Stream: ConvertGeminiResponseToGeminiCLI,
|
||||
NonStream: ConvertGeminiResponseToGeminiCLINonStream,
|
||||
Stream: ConvertGeminiResponseToGeminiCLI,
|
||||
NonStream: ConvertGeminiResponseToGeminiCLINonStream,
|
||||
TokenCount: GeminiCLITokenCount,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user