Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -10,23 +10,37 @@ def num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613"):
|
|
10 |
except KeyError:
|
11 |
print("Warning: model not found. Using cl100k_base encoding.")
|
12 |
encoding = tiktoken.get_encoding("cl100k_base")
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
elif "gpt-3.5-turbo" in model:
|
21 |
-
print("Warning: gpt-3.5-turbo may update over time.
|
|
|
22 |
elif "gpt-4" in model:
|
23 |
-
print("Warning: gpt-4 may update over time.
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
return num_tokens
|
31 |
|
32 |
|
@@ -187,8 +201,8 @@ with app:
|
|
187 |
message_btn = gr.Button("发送", variant="primary", elem_classes="custom-button")
|
188 |
clear_btn = gr.Button("清除", variant="primary", elem_classes="custom-button")
|
189 |
with gr.Column():
|
190 |
-
output_textbox = gr.Textbox(label="显示问题答案", lines=
|
191 |
-
output_textbox2 = gr.Textbox(label="显示tokens数", lines=1, max_lines=
|
192 |
message_btn.click(generate_response,inputs=[question,model_name,endpoint,transmit_api_key, official_api_key],outputs=output_textbox,show_progress=False)
|
193 |
clear_btn.click(clear_output,outputs=[question,output_textbox],show_progress=False)
|
194 |
output_textbox.change(fn=num_tokens_from_messages,inputs=output_textbox,outputs=output_textbox2)
|
|
|
10 |
except KeyError:
|
11 |
print("Warning: model not found. Using cl100k_base encoding.")
|
12 |
encoding = tiktoken.get_encoding("cl100k_base")
|
13 |
+
if model in {
|
14 |
+
"gpt-3.5-turbo-0613",
|
15 |
+
"gpt-3.5-turbo-16k-0613",
|
16 |
+
"gpt-4-0314",
|
17 |
+
"gpt-4-32k-0314",
|
18 |
+
"gpt-4-0613",
|
19 |
+
"gpt-4-32k-0613",
|
20 |
+
}:
|
21 |
+
tokens_per_message = 3
|
22 |
+
tokens_per_name = 1
|
23 |
+
elif model == "gpt-3.5-turbo-0301":
|
24 |
+
tokens_per_message = 4 # every message follows <|start|>{role/name}\n{content}<|end|>\n
|
25 |
+
tokens_per_name = -1 # if there's a name, the role is omitted
|
26 |
elif "gpt-3.5-turbo" in model:
|
27 |
+
print("Warning: gpt-3.5-turbo may update over time. Returning num tokens assuming gpt-3.5-turbo-0613.")
|
28 |
+
return num_tokens_from_messages(messages, model="gpt-3.5-turbo-0613")
|
29 |
elif "gpt-4" in model:
|
30 |
+
print("Warning: gpt-4 may update over time. Returning num tokens assuming gpt-4-0613.")
|
31 |
+
return num_tokens_from_messages(messages, model="gpt-4-0613")
|
32 |
+
else:
|
33 |
+
raise NotImplementedError(
|
34 |
+
f"""num_tokens_from_messages() is not implemented for model {model}."""
|
35 |
+
)
|
36 |
+
num_tokens = 0
|
37 |
+
for message in messages:
|
38 |
+
num_tokens += tokens_per_message
|
39 |
+
for key, value in message.items():
|
40 |
+
num_tokens += len(encoding.encode(value))
|
41 |
+
if key == "name":
|
42 |
+
num_tokens += tokens_per_name
|
43 |
+
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
|
44 |
return num_tokens
|
45 |
|
46 |
|
|
|
201 |
message_btn = gr.Button("发送", variant="primary", elem_classes="custom-button")
|
202 |
clear_btn = gr.Button("清除", variant="primary", elem_classes="custom-button")
|
203 |
with gr.Column():
|
204 |
+
output_textbox = gr.Textbox(label="显示问题答案", lines=14, max_lines=14, elem_classes="custom-textbox",interactive=True,show_label=True,show_copy_button=True)
|
205 |
+
output_textbox2 = gr.Textbox(label="显示tokens数", lines=1, max_lines=1, elem_classes="custom-textbox",interactive=False,show_label=True)
|
206 |
message_btn.click(generate_response,inputs=[question,model_name,endpoint,transmit_api_key, official_api_key],outputs=output_textbox,show_progress=False)
|
207 |
clear_btn.click(clear_output,outputs=[question,output_textbox],show_progress=False)
|
208 |
output_textbox.change(fn=num_tokens_from_messages,inputs=output_textbox,outputs=output_textbox2)
|