Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,16 @@ import logging
|
|
9 |
from threading import Event
|
10 |
import tiktoken # 引入 tiktoken 库
|
11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
app = Flask(__name__)
|
13 |
logging.basicConfig(level=logging.INFO)
|
14 |
|
@@ -80,13 +90,9 @@ def calculate_tokens_via_tiktoken(text, model="gpt-3.5-turbo"):
|
|
80 |
使用 tiktoken 库根据 GPT 模型计算 token 数量。
|
81 |
Claude 模型与 GPT 模型的 token 计算机制类似,因此可以使用 tiktoken。
|
82 |
"""
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
return len(tokens)
|
87 |
-
except Exception as e:
|
88 |
-
logging.error(f"Error loading encoding: {str(e)}")
|
89 |
-
raise
|
90 |
|
91 |
@app.route('/')
|
92 |
def root():
|
|
|
9 |
from threading import Event
|
10 |
import tiktoken # 引入 tiktoken 库
|
11 |
|
12 |
+
def local_encoding_for_model(model_name: str):
|
13 |
+
local_encoding_path = '/app/cl100k_base.tiktoken'
|
14 |
+
if os.path.exists(local_encoding_path):
|
15 |
+
with open(local_encoding_path, 'rb') as f:
|
16 |
+
return f.read() # 返回本地编码文件的内容
|
17 |
+
else:
|
18 |
+
raise FileNotFoundError(f"Local encoding file not found at {local_encoding_path}")
|
19 |
+
|
20 |
+
tiktoken.encoding_for_model = local_encoding_for_model
|
21 |
+
|
22 |
app = Flask(__name__)
|
23 |
logging.basicConfig(level=logging.INFO)
|
24 |
|
|
|
90 |
使用 tiktoken 库根据 GPT 模型计算 token 数量。
|
91 |
Claude 模型与 GPT 模型的 token 计算机制类似,因此可以使用 tiktoken。
|
92 |
"""
|
93 |
+
encoding = tiktoken.encoding_for_model(model) # 获取模型的编码器
|
94 |
+
tokens = encoding.encode(text) # 对文本进行 tokenization
|
95 |
+
return len(tokens)
|
|
|
|
|
|
|
|
|
96 |
|
97 |
@app.route('/')
|
98 |
def root():
|