mirror of
https://github.com/yangjian102621/geekai.git
synced 2025-11-12 04:03:42 +08:00
refactor: 调整项目目录结构,移除其他语言 API 目录
This commit is contained in:
20
api/utils/openai.go
Normal file
20
api/utils/openai.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/pkoukk/tiktoken-go"
|
||||
)
|
||||
|
||||
func CalcTokens(text string, model string) (int, error) {
|
||||
encoding, ok := tiktoken.MODEL_TO_ENCODING[model]
|
||||
if !ok {
|
||||
encoding = "cl100k_base"
|
||||
}
|
||||
tke, err := tiktoken.GetEncoding(encoding)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("getEncoding: %v", err)
|
||||
}
|
||||
|
||||
token := tke.Encode(text, nil, nil)
|
||||
return len(token), nil
|
||||
}
|
||||
Reference in New Issue
Block a user