feat: update and optimize tokenizer performance (#191)

Co-Authored-By: Minghan Zhang <112773885+zmh-program@users.noreply.github.com>
This commit is contained in:
Deng Junhai 2024-06-22 01:23:27 +08:00
parent 2024302316
commit 4c3843b3be
3 changed files with 24 additions and 22 deletions

View File

@ -2,10 +2,11 @@ package globals
import ( import (
"fmt" "fmt"
"strings"
"github.com/natefinch/lumberjack" "github.com/natefinch/lumberjack"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
"strings"
) )
const DefaultLoggerFile = "chatnio.log" const DefaultLoggerFile = "chatnio.log"
@ -25,7 +26,7 @@ func (l *AppLogger) Format(entry *logrus.Entry) ([]byte, error) {
) )
if !viper.GetBool("log.ignore_console") { if !viper.GetBool("log.ignore_console") {
fmt.Println(data) fmt.Print(data)
} }
return []byte(data), nil return []byte(data), nil

View File

@ -197,11 +197,6 @@ func (b *Buffer) IsFunctionCalling() bool {
return b.FunctionCall != nil || b.ToolCalls != nil return b.FunctionCall != nil || b.ToolCalls != nil
} }
func (b *Buffer) WriteBytes(data []byte) []byte {
b.Write(string(data))
return data
}
func (b *Buffer) IsEmpty() bool { func (b *Buffer) IsEmpty() bool {
return b.Cursor == 0 && !b.IsFunctionCalling() return b.Cursor == 0 && !b.IsFunctionCalling()
} }
@ -237,12 +232,12 @@ func (b *Buffer) SetInputTokens(tokens int) {
b.InputTokens = tokens b.InputTokens = tokens
} }
func (b *Buffer) CountInputToken() int { func (b *Buffer) CountOutputToken() int {
return b.InputTokens return b.ReadTimes() * GetWeightByModel(b.Model)
} }
func (b *Buffer) CountOutputToken() int { func (b *Buffer) CountOutputToken() int {
return b.ReadTimes() * GetWeightByModel(b.Model) return b.CountInputToken() + b.CountOutputToken()
} }
func (b *Buffer) CountToken() int { func (b *Buffer) CountToken() int {

View File

@ -3,8 +3,9 @@ package utils
import ( import (
"chat/globals" "chat/globals"
"fmt" "fmt"
"github.com/pkoukk/tiktoken-go"
"strings" "strings"
"github.com/pkoukk/tiktoken-go"
) )
// Using https://github.com/pkoukk/tiktoken-go // Using https://github.com/pkoukk/tiktoken-go
@ -45,9 +46,10 @@ func GetWeightByModel(model string) int {
} }
} }
} }
func NumTokensFromMessages(messages []globals.Message, model string) (tokens int) { func NumTokensFromMessages(messages []globals.Message, model string, responseType bool) (tokens int) {
tokensPerMessage := GetWeightByModel(model) tokensPerMessage := GetWeightByModel(model)
tkm, err := tiktoken.EncodingForModel(model) tkm, err := tiktoken.EncodingForModel(model)
if err != nil { if err != nil {
// the method above was deprecated, use the recall method instead // the method above was deprecated, use the recall method instead
// can not encode messages, use length of messages as a proxy for number of tokens // can not encode messages, use length of messages as a proxy for number of tokens
@ -59,16 +61,20 @@ func NumTokensFromMessages(messages []globals.Message, model string) (tokens int
if globals.DebugMode { if globals.DebugMode {
globals.Debug(fmt.Sprintf("[tiktoken] error encoding messages: %s (model: %s), using default model instead", err, model)) globals.Debug(fmt.Sprintf("[tiktoken] error encoding messages: %s (model: %s), using default model instead", err, model))
} }
return NumTokensFromMessages(messages, globals.GPT3Turbo0613) return NumTokensFromMessages(messages, globals.GPT3Turbo0613, responseType)
} }
for _, message := range messages { for _, message := range messages {
tokens += tokens += len(tkm.Encode(message.Content, nil, nil))
len(tkm.Encode(message.Content, nil, nil)) +
len(tkm.Encode(message.Role, nil, nil)) + if !responseType {
tokensPerMessage tokens += len(tkm.Encode(message.Role, nil, nil)) + tokensPerMessage
}
}
if !responseType {
tokens += 3 // every reply is primed with <|start|>assistant<|message|>
} }
tokens += 3 // every reply is primed with <|start|>assistant<|message|>
if globals.DebugMode { if globals.DebugMode {
globals.Debug(fmt.Sprintf("[tiktoken] num tokens from messages: %d (tokens per message: %d, model: %s)", tokens, tokensPerMessage, model)) globals.Debug(fmt.Sprintf("[tiktoken] num tokens from messages: %d (tokens per message: %d, model: %s)", tokens, tokensPerMessage, model))
@ -76,8 +82,8 @@ func NumTokensFromMessages(messages []globals.Message, model string) (tokens int
return tokens return tokens
} }
func CountTokenPrice(messages []globals.Message, model string) int { func NumTokensFromResponse(response string, model string) int {
return NumTokensFromMessages(messages, model) * GetWeightByModel(model) return NumTokensFromMessages([]globals.Message{{Content: response}}, model, true)
} }
func CountInputQuota(charge Charge, token int) float32 { func CountInputQuota(charge Charge, token int) float32 {
@ -88,10 +94,10 @@ func CountInputQuota(charge Charge, token int) float32 {
return 0 return 0
} }
func CountOutputToken(charge Charge, model string, token int) float32 { func CountOutputToken(charge Charge, token int) float32 {
switch charge.GetType() { switch charge.GetType() {
case globals.TokenBilling: case globals.TokenBilling:
return float32(token*GetWeightByModel(model)) / 1000 * charge.GetOutput() return float32(token) / 1000 * charge.GetOutput()
case globals.TimesBilling: case globals.TimesBilling:
return charge.GetOutput() return charge.GetOutput()
default: default: