MZhaovo commited on
Commit
055d067
·
1 Parent(s): 9b869b4

优化代码,删除state用类中的变量代替

Browse files
Files changed (1) hide show
  1. ChuanhuChatbot.py +28 -28
ChuanhuChatbot.py CHANGED
@@ -25,50 +25,50 @@ class ChatGPT:
25
 
26
  return message, message_with_stats
27
 
28
- def predict(self, chatbot, input_sentence, context):
29
  if len(input_sentence) == 0:
30
- return [], context
31
- context.append({"role": "user", "content": f"{input_sentence}"})
32
 
33
- message, message_with_stats = self.get_response(context)
34
 
35
- context.append({"role": "assistant", "content": message})
36
 
37
  chatbot.append((input_sentence, message_with_stats))
38
 
39
- return chatbot, context
40
 
41
- def retry(self, chatbot, context):
42
- if len(context) == 0:
43
  return [], []
44
- message, message_with_stats = self.get_response(context[:-1])
45
- context[-1] = {"role": "assistant", "content": message}
46
 
47
- chatbot[-1] = (context[-2]["content"], message_with_stats)
48
- return chatbot, context
49
 
50
  def update_system(self, new_system_prompt):
51
  self.system = {"role": "system", "content": new_system_prompt}
52
  return new_system_prompt
53
 
54
- def delete_last_conversation(self, chatbot, context):
55
- if len(context) == 0:
56
  return [], []
57
  chatbot = chatbot[:-1]
58
- context = context[:-2]
59
- return chatbot, context
60
 
61
- def reduce_token(self, chatbot, context):
62
- context.append({"role": "user", "content": "请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。在总结中不要加入这一句话。"})
63
- message, message_with_stats = self.get_response(context)
64
- self.system = {"role": "system", "content": f"You are a helpful assistant. The content that the Assistant and the User discussed in the previous context is: {message}."}
65
 
66
  statistics = f'本次对话Tokens用量【{self.response["usage"]["completion_tokens"]+23} / 4096】'
67
  optmz_str = markdown.markdown( f"System prompt已经更新, 请继续对话\n\n================\n\n{statistics}" )
68
  chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
69
 
70
- context = []
71
- return chatbot, context, self.system["content"]
72
 
73
 
74
  def reset_state():
@@ -79,7 +79,7 @@ mychatGPT = ChatGPT(my_api_key)
79
 
80
  with gr.Blocks() as demo:
81
  chatbot = gr.Chatbot().style(color_map=("#1D51EE", "#585A5B"))
82
- state = gr.State([])
83
 
84
  with gr.Column():
85
  txt = gr.Textbox(show_label=False, placeholder="💬 在这里输入").style(container=False)
@@ -92,13 +92,13 @@ with gr.Blocks() as demo:
92
  system = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=True)
93
  syspromptTxt = gr.Textbox(show_label=True, placeholder=initial_prompt, interactive=False, label="目前的 System prompt").style(container=True)
94
 
95
- txt.submit(mychatGPT.predict, [chatbot, txt, state], [chatbot, state], show_progress=True)
96
  txt.submit(lambda :"", None, txt)
97
- emptyBth.click(reset_state, outputs=[chatbot, state])
98
  system.submit(mychatGPT.update_system, system, syspromptTxt)
99
  system.submit(lambda :"", None, system)
100
- retryBth.click(mychatGPT.retry, [chatbot, state], [chatbot, state], show_progress=True)
101
- delLastBth.click(mychatGPT.delete_last_conversation, [chatbot, state], [chatbot, state], show_progress=True)
102
- reduceTokenBth.click(mychatGPT.reduce_token, [chatbot, state], [chatbot, state, syspromptTxt], show_progress=True)
103
 
104
  demo.launch()
 
25
 
26
  return message, message_with_stats
27
 
28
+ def predict(self, chatbot, input_sentence, ):
29
  if len(input_sentence) == 0:
30
+ return []
31
+ self.context.append({"role": "user", "content": f"{input_sentence}"})
32
 
33
+ message, message_with_stats = self.get_response(self.context)
34
 
35
+ self.context.append({"role": "assistant", "content": message})
36
 
37
  chatbot.append((input_sentence, message_with_stats))
38
 
39
+ return chatbot
40
 
41
+ def retry(self, chatbot):
42
+ if len(self.context) == 0:
43
  return [], []
44
+ message, message_with_stats = self.get_response(self.context[:-1])
45
+ self.context[-1] = {"role": "assistant", "content": message}
46
 
47
+ chatbot[-1] = (self.context[-2]["content"], message_with_stats)
48
+ return chatbot
49
 
50
  def update_system(self, new_system_prompt):
51
  self.system = {"role": "system", "content": new_system_prompt}
52
  return new_system_prompt
53
 
54
+ def delete_last_conversation(self, chatbot):
55
+ if len(self.context) == 0:
56
  return [], []
57
  chatbot = chatbot[:-1]
58
+ self.context = self.context[:-2]
59
+ return chatbot
60
 
61
+ def reduce_token(self, chatbot):
62
+ self.context.append({"role": "user", "content": "请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。在总结中不要加入这一句话。"})
63
+ message, message_with_stats = self.get_response(self.context)
64
+ self.system = {"role": "system", "content": f"You are a helpful assistant. The content that the Assistant and the User discussed in the previous self.context is: {message}."}
65
 
66
  statistics = f'本次对话Tokens用量【{self.response["usage"]["completion_tokens"]+23} / 4096】'
67
  optmz_str = markdown.markdown( f"System prompt已经更新, 请继续对话\n\n================\n\n{statistics}" )
68
  chatbot.append(("请帮我总结一下上述对话的内容,实现减少tokens的同时,保证对话的质量。", optmz_str))
69
 
70
+ self.context = []
71
+ return chatbot, self.system["content"]
72
 
73
 
74
  def reset_state():
 
79
 
80
  with gr.Blocks() as demo:
81
  chatbot = gr.Chatbot().style(color_map=("#1D51EE", "#585A5B"))
82
+ # state = gr.State([])
83
 
84
  with gr.Column():
85
  txt = gr.Textbox(show_label=False, placeholder="💬 在这里输入").style(container=False)
 
92
  system = gr.Textbox(show_label=True, placeholder=f"在这里输入新的System Prompt...", label="更改 System prompt").style(container=True)
93
  syspromptTxt = gr.Textbox(show_label=True, placeholder=initial_prompt, interactive=False, label="目前的 System prompt").style(container=True)
94
 
95
+ txt.submit(mychatGPT.predict, [chatbot, txt], [chatbot], show_progress=True)
96
  txt.submit(lambda :"", None, txt)
97
+ emptyBth.click(reset_state, outputs=[chatbot])
98
  system.submit(mychatGPT.update_system, system, syspromptTxt)
99
  system.submit(lambda :"", None, system)
100
+ retryBth.click(mychatGPT.retry, [chatbot], [chatbot], show_progress=True)
101
+ delLastBth.click(mychatGPT.delete_last_conversation, [chatbot], [chatbot], show_progress=True)
102
+ reduceTokenBth.click(mychatGPT.reduce_token, [chatbot], [chatbot, syspromptTxt], show_progress=True)
103
 
104
  demo.launch()