Update app.py
Browse files
app.py
CHANGED
@@ -81,17 +81,20 @@ def calculate_tokens_via_tiktoken(text, model="gpt-3.5-turbo"):
|
|
81 |
使用 tiktoken 库根据 GPT 模型计算 token 数量。
|
82 |
Claude 模型与 GPT 模型的 token 计算机制类似,因此可以使用 tiktoken。
|
83 |
"""
|
84 |
-
#
|
85 |
-
|
86 |
-
|
87 |
|
88 |
try:
|
89 |
encoding = tiktoken.encoding_for_model(model) # 获取模型的编码器
|
90 |
tokens = encoding.encode(text) # 对文本进行 tokenization
|
91 |
return len(tokens)
|
92 |
finally:
|
93 |
-
#
|
94 |
-
|
|
|
|
|
|
|
95 |
|
96 |
@app.route('/')
|
97 |
def root():
|
|
|
81 |
使用 tiktoken 库根据 GPT 模型计算 token 数量。
|
82 |
Claude 模型与 GPT 模型的 token 计算机制类似,因此可以使用 tiktoken。
|
83 |
"""
|
84 |
+
# 临时禁用代理环境变量
|
85 |
+
original_http_proxy = os.environ.pop('http_proxy', None)
|
86 |
+
original_https_proxy = os.environ.pop('https_proxy', None)
|
87 |
|
88 |
try:
|
89 |
encoding = tiktoken.encoding_for_model(model) # 获取模型的编码器
|
90 |
tokens = encoding.encode(text) # 对文本进行 tokenization
|
91 |
return len(tokens)
|
92 |
finally:
|
93 |
+
# 恢复代理
|
94 |
+
if original_http_proxy:
|
95 |
+
os.environ['http_proxy'] = original_http_proxy
|
96 |
+
if original_https_proxy:
|
97 |
+
os.environ['https_proxy'] = original_https_proxy
|
98 |
|
99 |
@app.route('/')
|
100 |
def root():
|