Tuchuanhuhuhu commited on
Commit
c9a9fba
·
1 Parent(s): 2a7a9d4

chore: 历史记录管理代码逻辑改进

Browse files
ChuanhuChatbot.py CHANGED
@@ -549,7 +549,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
549
 
550
  load_history_from_file_args = dict(
551
  fn=load_chat_history,
552
- inputs=[current_model, historySelectList, user_name],
553
  outputs=[saveFileName, systemPromptTxt, chatbot]
554
  )
555
 
@@ -559,7 +559,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
559
 
560
  auto_name_chat_history_args = dict(
561
  fn=auto_name_chat_history,
562
- inputs=[current_model, name_chat_method, user_question, chatbot, user_name, single_turn_checkbox],
563
  outputs=[historySelectList],
564
  show_progress=False,
565
  )
@@ -679,12 +679,12 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
679
  )
680
  exportMarkdownBtn.click(
681
  export_markdown,
682
- [current_model, saveFileName, chatbot, user_name],
683
  [],
684
  show_progress=True,
685
  )
686
  historyRefreshBtn.click(**refresh_history_args)
687
- historyDeleteBtn.click(delete_chat_history, [current_model, historySelectList, user_name], [status_display, historySelectList, chatbot], _js='(a,b,c)=>{return showConfirmationDialog(a, b, c);}').then(
688
  reset,
689
  inputs=[current_model, retain_system_prompt_checkbox],
690
  outputs=[chatbot, status_display, historySelectList, systemPromptTxt],
@@ -692,7 +692,7 @@ with gr.Blocks(theme=small_and_beautiful_theme) as demo:
692
  _js='(a,b)=>{return clearChatbot(a,b);}',
693
  )
694
  historySelectList.input(**load_history_from_file_args)
695
- uploadFileBtn.upload(upload_chat_history, [current_model, uploadFileBtn, user_name], [
696
  saveFileName, systemPromptTxt, chatbot]).then(**refresh_history_args)
697
  historyDownloadBtn.click(None, [
698
  user_name, historySelectList], None, _js='(a,b)=>{return downloadHistory(a,b,".json");}')
 
549
 
550
  load_history_from_file_args = dict(
551
  fn=load_chat_history,
552
+ inputs=[current_model, historySelectList],
553
  outputs=[saveFileName, systemPromptTxt, chatbot]
554
  )
555
 
 
559
 
560
  auto_name_chat_history_args = dict(
561
  fn=auto_name_chat_history,
562
+ inputs=[current_model, name_chat_method, user_question, chatbot, single_turn_checkbox],
563
  outputs=[historySelectList],
564
  show_progress=False,
565
  )
 
679
  )
680
  exportMarkdownBtn.click(
681
  export_markdown,
682
+ [current_model, saveFileName, chatbot],
683
  [],
684
  show_progress=True,
685
  )
686
  historyRefreshBtn.click(**refresh_history_args)
687
+ historyDeleteBtn.click(delete_chat_history, [current_model, historySelectList], [status_display, historySelectList, chatbot], _js='(a,b,c)=>{return showConfirmationDialog(a, b, c);}').then(
688
  reset,
689
  inputs=[current_model, retain_system_prompt_checkbox],
690
  outputs=[chatbot, status_display, historySelectList, systemPromptTxt],
 
692
  _js='(a,b)=>{return clearChatbot(a,b);}',
693
  )
694
  historySelectList.input(**load_history_from_file_args)
695
+ uploadFileBtn.upload(upload_chat_history, [current_model, uploadFileBtn], [
696
  saveFileName, systemPromptTxt, chatbot]).then(**refresh_history_args)
697
  historyDownloadBtn.click(None, [
698
  user_name, historySelectList], None, _js='(a,b)=>{return downloadHistory(a,b,".json");}')
modules/models/OpenAI.py CHANGED
@@ -252,7 +252,7 @@ class OpenAIClient(BaseLLMModel):
252
  return response
253
 
254
 
255
- def auto_name_chat_history(self, name_chat_method, user_question, chatbot, user_name, single_turn_checkbox):
256
  if len(self.history) == 2 and not single_turn_checkbox and not hide_history_when_not_logged_in:
257
  user_question = self.history[0]["content"]
258
  if name_chat_method == i18n("模型自动总结(消耗tokens)"):
@@ -269,10 +269,10 @@ class OpenAIClient(BaseLLMModel):
269
  except Exception as e:
270
  logging.info(f"自动命名失败。{e}")
271
  filename = replace_special_symbols(user_question)[:16] + ".json"
272
- return self.rename_chat_history(filename, chatbot, user_name)
273
  elif name_chat_method == i18n("第一条提问"):
274
  filename = replace_special_symbols(user_question)[:16] + ".json"
275
- return self.rename_chat_history(filename, chatbot, user_name)
276
  else:
277
  return gr.update()
278
  else:
 
252
  return response
253
 
254
 
255
+ def auto_name_chat_history(self, name_chat_method, user_question, chatbot, single_turn_checkbox):
256
  if len(self.history) == 2 and not single_turn_checkbox and not hide_history_when_not_logged_in:
257
  user_question = self.history[0]["content"]
258
  if name_chat_method == i18n("模型自动总结(消耗tokens)"):
 
269
  except Exception as e:
270
  logging.info(f"自动命名失败。{e}")
271
  filename = replace_special_symbols(user_question)[:16] + ".json"
272
+ return self.rename_chat_history(filename, chatbot)
273
  elif name_chat_method == i18n("第一条提问"):
274
  filename = replace_special_symbols(user_question)[:16] + ".json"
275
+ return self.rename_chat_history(filename, chatbot)
276
  else:
277
  return gr.update()
278
  else:
modules/models/base_model.py CHANGED
@@ -721,31 +721,31 @@ class BaseLLMModel:
721
  token_sum += sum(token_lst[: i + 1])
722
  return i18n("Token 计数: ") + f"{sum(token_lst)}" + i18n(",本次对话累计消耗了 ") + f"{token_sum} tokens"
723
 
724
- def rename_chat_history(self, filename, chatbot, user_name):
725
  if filename == "":
726
  return gr.update()
727
  if not filename.endswith(".json"):
728
  filename += ".json"
729
- self.delete_chat_history(self.history_file_path, user_name)
730
  # 命名重复检测
731
  repeat_file_index = 2
732
- full_path = os.path.join(HISTORY_DIR, user_name, filename)
733
  while os.path.exists(full_path):
734
- full_path = os.path.join(HISTORY_DIR, user_name, f"{repeat_file_index}_{filename}")
735
  repeat_file_index += 1
736
  filename = os.path.basename(full_path)
737
 
738
  self.history_file_path = filename
739
- save_file(filename, self.system_prompt, self.history, chatbot, user_name)
740
- return init_history_list(user_name)
741
 
742
- def auto_name_chat_history(self, name_chat_method, user_question, chatbot, user_name, single_turn_checkbox):
743
  if len(self.history) == 2 and not single_turn_checkbox:
744
  user_question = self.history[0]["content"]
745
  if type(user_question) == list:
746
  user_question = user_question[0]["text"]
747
  filename = replace_special_symbols(user_question)[:16] + ".json"
748
- return self.rename_chat_history(filename, chatbot, user_name)
749
  else:
750
  return gr.update()
751
 
@@ -753,14 +753,14 @@ class BaseLLMModel:
753
  save_file(self.history_file_path, self.system_prompt,
754
  self.history, chatbot, self.user_identifier)
755
 
756
- def export_markdown(self, filename, chatbot, user_name):
757
  if filename == "":
758
  return
759
  if not filename.endswith(".md"):
760
  filename += ".md"
761
- save_file(filename, self.system_prompt, self.history, chatbot, user_name)
762
 
763
- def load_chat_history(self, new_history_file_path=None, username=None):
764
  logging.debug(f"{self.user_identifier} 加载对话历史中……")
765
  if new_history_file_path is not None:
766
  if type(new_history_file_path) != str:
@@ -806,7 +806,7 @@ class BaseLLMModel:
806
  logging.info(f"没有找到对话历史记录 {self.history_file_path}")
807
  return self.history_file_path, "", []
808
 
809
- def delete_chat_history(self, filename, user_name):
810
  if filename == "CANCELED":
811
  return gr.update(), gr.update(), gr.update()
812
  if filename == "":
@@ -814,17 +814,17 @@ class BaseLLMModel:
814
  if not filename.endswith(".json"):
815
  filename += ".json"
816
  if filename == os.path.basename(filename):
817
- history_file_path = os.path.join(HISTORY_DIR, user_name, filename)
818
  else:
819
  history_file_path = filename
820
  md_history_file_path = history_file_path[:-5] + ".md"
821
  try:
822
  os.remove(history_file_path)
823
  os.remove(md_history_file_path)
824
- return i18n("删除对话历史成功"), get_history_list(user_name), []
825
  except:
826
  logging.info(f"删除对话历史失败 {history_file_path}")
827
- return i18n("对话历史")+filename+i18n("已经被删除啦"), get_history_list(user_name), []
828
 
829
  def auto_load(self):
830
  filepath = get_history_filepath(self.user_identifier)
 
721
  token_sum += sum(token_lst[: i + 1])
722
  return i18n("Token 计数: ") + f"{sum(token_lst)}" + i18n(",本次对话累计消耗了 ") + f"{token_sum} tokens"
723
 
724
+ def rename_chat_history(self, filename, chatbot):
725
  if filename == "":
726
  return gr.update()
727
  if not filename.endswith(".json"):
728
  filename += ".json"
729
+ self.delete_chat_history(self.history_file_path)
730
  # 命名重复检测
731
  repeat_file_index = 2
732
+ full_path = os.path.join(HISTORY_DIR, self.user_identifier, filename)
733
  while os.path.exists(full_path):
734
+ full_path = os.path.join(HISTORY_DIR, self.user_identifier, f"{repeat_file_index}_{filename}")
735
  repeat_file_index += 1
736
  filename = os.path.basename(full_path)
737
 
738
  self.history_file_path = filename
739
+ save_file(filename, self.system_prompt, self.history, chatbot, self.user_identifier)
740
+ return init_history_list(self.user_identifier)
741
 
742
+ def auto_name_chat_history(self, name_chat_method, user_question, chatbot, single_turn_checkbox):
743
  if len(self.history) == 2 and not single_turn_checkbox:
744
  user_question = self.history[0]["content"]
745
  if type(user_question) == list:
746
  user_question = user_question[0]["text"]
747
  filename = replace_special_symbols(user_question)[:16] + ".json"
748
+ return self.rename_chat_history(filename, chatbot, self.user_identifier)
749
  else:
750
  return gr.update()
751
 
 
753
  save_file(self.history_file_path, self.system_prompt,
754
  self.history, chatbot, self.user_identifier)
755
 
756
+ def export_markdown(self, filename, chatbot):
757
  if filename == "":
758
  return
759
  if not filename.endswith(".md"):
760
  filename += ".md"
761
+ save_file(filename, self.system_prompt, self.history, chatbot, self.user_identifier)
762
 
763
+ def load_chat_history(self, new_history_file_path=None):
764
  logging.debug(f"{self.user_identifier} 加载对话历史中……")
765
  if new_history_file_path is not None:
766
  if type(new_history_file_path) != str:
 
806
  logging.info(f"没有找到对话历史记录 {self.history_file_path}")
807
  return self.history_file_path, "", []
808
 
809
+ def delete_chat_history(self, filename):
810
  if filename == "CANCELED":
811
  return gr.update(), gr.update(), gr.update()
812
  if filename == "":
 
814
  if not filename.endswith(".json"):
815
  filename += ".json"
816
  if filename == os.path.basename(filename):
817
+ history_file_path = os.path.join(HISTORY_DIR, self.user_identifier, filename)
818
  else:
819
  history_file_path = filename
820
  md_history_file_path = history_file_path[:-5] + ".md"
821
  try:
822
  os.remove(history_file_path)
823
  os.remove(md_history_file_path)
824
+ return i18n("删除对话历史成功"), get_history_list(self.user_identifier), []
825
  except:
826
  logging.info(f"删除对话历史失败 {history_file_path}")
827
+ return i18n("对话历史")+filename+i18n("已经被删除啦"), get_history_list(self.user_identifier), []
828
 
829
  def auto_load(self):
830
  filepath = get_history_filepath(self.user_identifier)