Files
opencatd-open/internal/pkg/tokenizer/tokenizer.go
T
Sakurasan ef3025dd80 refactor: complete backend rewrite for multi-protocol proxy
Major rewrite of the Go backend to support:
- Three API format imports: openai, anthropic, compatible
- Three protocol conversions: Chat Completions, Responses, Messages
- Hub-and-spoke architecture with Chat as intermediate format

Deleted:
- opencat.go (old entry)
- store/, team/, pkg/team/, pkg/store/ (old data layer)
- internal/model/, internal/consts/ (old types)
- internal/service/team/, internal/controller/team/ (old handlers)
- llm/ (removed LLM client library, pure proxy mode)
- dist/, assets/ (old build artifacts)

Added:
- internal/store/ — 9 GORM models + multi-DB support
- internal/pkg/ — crypto (AES-GCM), apikey, jwt, ratelimit, resp, tokenizer
- internal/channel/ — channel selection, weighted LB, health checks
- internal/proxy/convert/ — 6 protocol conversion functions + SSE streaming
- internal/proxy/ — gateway with request dispatch and upstream selection
- internal/usage/ — async usage recorder with batch writes
- internal/api/ — management API (auth, users, keys, channels, models)
- Makefile for build/test/deploy

Fixed API to match frontend expectations:
- Login response wraps token in { data: { token } }
- GET /api/profile route added
- Profile response wraps user in { code, data }
- Role returned as number (10=admin, 1=user)
2026-08-30 11:49:31 +08:00

87 lines
2.2 KiB
Go

package tokenizer
import (
"fmt"
"strings"
"github.com/pkoukk/tiktoken-go"
)
// Count 计算字符串的 token 数量
func Count(text, model string) int {
tkm, err := tiktoken.EncodingForModel(model)
if err != nil {
tkm, _ = tiktoken.GetEncoding("cl100k_base")
}
return len(tkm.Encode(text, nil, nil))
}
// Cost 计算模型调用成本(USD,按每百万 token 定价)
func Cost(model string, inputTokens, outputTokens int) float64 {
var inputPrice, outputPrice float64
switch {
case strings.Contains(model, "gpt-4o-mini"):
inputPrice = 0.15
outputPrice = 0.60
case strings.Contains(model, "gpt-4o"):
inputPrice = 2.50
outputPrice = 10.00
case strings.Contains(model, "gpt-4-turbo"):
inputPrice = 10.00
outputPrice = 30.00
case strings.Contains(model, "gpt-4"):
inputPrice = 30.00
outputPrice = 60.00
case strings.Contains(model, "gpt-3.5-turbo"):
inputPrice = 0.50
outputPrice = 1.50
case strings.Contains(model, "claude-3-5-sonnet"):
inputPrice = 3.00
outputPrice = 15.00
case strings.Contains(model, "claude-3-opus"):
inputPrice = 15.00
outputPrice = 75.00
case strings.Contains(model, "claude-3-haiku"):
inputPrice = 0.25
outputPrice = 1.25
case strings.Contains(model, "claude"):
inputPrice = 8.00
outputPrice = 24.00
case strings.Contains(model, "gemini-1.5-pro"):
inputPrice = 3.50
outputPrice = 10.50
case strings.Contains(model, "gemini-1.5-flash"):
inputPrice = 0.35
outputPrice = 0.53
case strings.Contains(model, "gemini"):
inputPrice = 0.50
outputPrice = 1.50
default:
inputPrice = 0.15
outputPrice = 0.60
}
cost := float64(inputTokens)/1e6*inputPrice + float64(outputTokens)/1e6*outputPrice
if cost < 0.000001 {
cost = 0.000001
}
return cost
}
// CostWithModel 从数据库模型记录获取定价
func CostWithModel(inputTokens, outputTokens, cacheReadTokens, cacheCreationTokens int64, inputPrice, outputPrice, cacheReadPrice float64) float64 {
cost := float64(inputTokens)/1e6*inputPrice +
float64(outputTokens)/1e6*outputPrice +
float64(cacheReadTokens)/1e6*cacheReadPrice +
float64(cacheCreationTokens)/1e6*inputPrice*1.25
if cost < 0.000001 {
cost = 0.000001
}
return cost
}
func init() {
_ = fmt.Sprintf // ensure fmt is used
}