Chuan Hu commited on
Commit
298f638
1 Parent(s): 70416c7

显示Token用量

Browse files
Files changed (1) hide show
  1. ChuanhuChatbot.py +23 -11
ChuanhuChatbot.py CHANGED
@@ -16,36 +16,46 @@ class ChatGPT:
16
  model="gpt-3.5-turbo",
17
  messages=[self.system, *messages],
18
  )
19
- response = response["choices"][0]["message"]["content"]
20
- response = markdown.markdown(response)
21
- return response
 
 
22
 
23
  def predict(self, chatbot, input_sentence, context):
24
  if len(input_sentence) == 0:
25
  return [], context
26
  context.append({"role": "user", "content": f"{input_sentence}"})
27
 
28
- response = self.get_response(context)
29
 
30
- context.append({"role": "assistant", "content": response})
31
 
32
- chatbot.append((input_sentence, response))
33
 
34
  return chatbot, context
35
 
36
  def retry(self, chatbot, context):
37
  if len(context) == 0:
38
  return [], []
39
- response = self.get_response(context[:-1])
40
- context[-1] = {"role": "assistant", "content": response}
41
 
42
- chatbot[-1] = (context[-2]["content"], response)
43
  return chatbot, context
44
 
45
  def update_system(self, new_system_prompt):
46
  self.system = {"role": "system", "content": new_system_prompt}
47
  return new_system_prompt
48
 
 
 
 
 
 
 
 
 
49
  def reset_state():
50
  return [], []
51
 
@@ -61,9 +71,10 @@ with gr.Blocks() as demo:
61
  with gr.Row():
62
  emptyBth = gr.Button("重置")
63
  retryBth = gr.Button("再试一次")
 
64
 
65
- system = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=False)
66
- syspromptTxt = gr.Textbox(show_label=True, placeholder=initial_prompt, interactive=False, label="目前的 System prompt").style(container=False)
67
 
68
  txt.submit(mychatGPT.predict, [chatbot, txt, state], [chatbot, state], show_progress=True)
69
  txt.submit(lambda :"", None, txt)
@@ -71,5 +82,6 @@ with gr.Blocks() as demo:
71
  system.submit(mychatGPT.update_system, system, syspromptTxt)
72
  system.submit(lambda :"", None, system)
73
  retryBth.click(mychatGPT.retry, [chatbot, state], [chatbot, state], show_progress=True)
 
74
 
75
  demo.launch()
 
16
  model="gpt-3.5-turbo",
17
  messages=[self.system, *messages],
18
  )
19
+ statistics = f'Token用量(最多4096):补全 {response["usage"]["completion_tokens"]}, 提问 {response["usage"]["prompt_tokens"]}, 总用量 {response["usage"]["total_tokens"]}'
20
+ message = response["choices"][0]["message"]["content"]
21
+ message_with_stats = f'{message}\n\n================\n\n{statistics}'
22
+ message_with_stats = markdown.markdown(message_with_stats)
23
+ return message, message_with_stats
24
 
25
  def predict(self, chatbot, input_sentence, context):
26
  if len(input_sentence) == 0:
27
  return [], context
28
  context.append({"role": "user", "content": f"{input_sentence}"})
29
 
30
+ message, message_with_stats = self.get_response(context)
31
 
32
+ context.append({"role": "assistant", "content": message})
33
 
34
+ chatbot.append((input_sentence, message_with_stats))
35
 
36
  return chatbot, context
37
 
38
  def retry(self, chatbot, context):
39
  if len(context) == 0:
40
  return [], []
41
+ message, message_with_stats = self.get_response(context[:-1])
42
+ context[-1] = {"role": "assistant", "content": message}
43
 
44
+ chatbot[-1] = (context[-2]["content"], message_with_stats)
45
  return chatbot, context
46
 
47
  def update_system(self, new_system_prompt):
48
  self.system = {"role": "system", "content": new_system_prompt}
49
  return new_system_prompt
50
 
51
+ def delete_last_conversation(self, chatbot, context):
52
+ if len(context) == 0:
53
+ return [], []
54
+ chatbot = chatbot[:-1]
55
+ context = context[:-2]
56
+ return chatbot, context[:-2]
57
+
58
+
59
  def reset_state():
60
  return [], []
61
 
 
71
  with gr.Row():
72
  emptyBth = gr.Button("重置")
73
  retryBth = gr.Button("再试一次")
74
+ delLastBth = gr.Button("删除上一个问答")
75
 
76
+ system = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=True)
77
+ syspromptTxt = gr.Textbox(show_label=True, placeholder=initial_prompt, interactive=False, label="目前的 System prompt").style(container=True)
78
 
79
  txt.submit(mychatGPT.predict, [chatbot, txt, state], [chatbot, state], show_progress=True)
80
  txt.submit(lambda :"", None, txt)
 
82
  system.submit(mychatGPT.update_system, system, syspromptTxt)
83
  system.submit(lambda :"", None, system)
84
  retryBth.click(mychatGPT.retry, [chatbot, state], [chatbot, state], show_progress=True)
85
+ delLastBth.click(mychatGPT.delete_last_conversation, [chatbot, state], [chatbot, state], show_progress=True)
86
 
87
  demo.launch()