Tuchuanhuhuhu commited on
Commit
6125458
·
1 Parent(s): 64889e9

修复S&L功能

Browse files
Files changed (2) hide show
  1. modules/base_model.py +4 -4
  2. modules/utils.py +6 -6
modules/base_model.py CHANGED
@@ -408,11 +408,11 @@ class BaseLLMModel:
408
  return save_file(filename, self.system_prompt, self.history, chatbot, user_name)
409
 
410
  def load_chat_history(self, filename, chatbot, user_name):
411
- logging.info(f"{user_name} 加载对话历史中……")
412
  if type(filename) != str:
413
  filename = filename.name
414
  try:
415
- with open(os.path.join(HISTORY_DIR / user_name, filename), "r") as f:
416
  json_s = json.load(f)
417
  try:
418
  if type(json_s["history"][0]) == str:
@@ -428,9 +428,9 @@ class BaseLLMModel:
428
  except:
429
  # 没有对话历史
430
  pass
431
- logging.info(f"{user_name} 加载对话历史完毕")
432
  self.history = json_s["history"]
433
  return filename, json_s["system"], json_s["chatbot"]
434
  except FileNotFoundError:
435
- logging.info(f"{user_name} 没有找到对话历史文件,不执行任何操作")
436
  return filename, self.system_prompt, chatbot
 
408
  return save_file(filename, self.system_prompt, self.history, chatbot, user_name)
409
 
410
  def load_chat_history(self, filename, chatbot, user_name):
411
+ logging.debug(f"{user_name} 加载对话历史中……")
412
  if type(filename) != str:
413
  filename = filename.name
414
  try:
415
+ with open(os.path.join(HISTORY_DIR, user_name, filename), "r") as f:
416
  json_s = json.load(f)
417
  try:
418
  if type(json_s["history"][0]) == str:
 
428
  except:
429
  # 没有对话历史
430
  pass
431
+ logging.debug(f"{user_name} 加载对话历史完毕")
432
  self.history = json_s["history"]
433
  return filename, json_s["system"], json_s["chatbot"]
434
  except FileNotFoundError:
435
+ logging.warning(f"{user_name} 没有找到对话历史文件,不执行任何操作")
436
  return filename, self.system_prompt, chatbot
modules/utils.py CHANGED
@@ -154,21 +154,21 @@ def construct_assistant(text):
154
 
155
 
156
  def save_file(filename, system, history, chatbot, user_name):
157
- logging.info(f"{user_name} 保存对话历史中……")
158
- os.makedirs(HISTORY_DIR / user_name, exist_ok=True)
159
  if filename.endswith(".json"):
160
  json_s = {"system": system, "history": history, "chatbot": chatbot}
161
  print(json_s)
162
- with open(os.path.join(HISTORY_DIR / user_name, filename), "w") as f:
163
  json.dump(json_s, f)
164
  elif filename.endswith(".md"):
165
  md_s = f"system: \n- {system} \n"
166
  for data in history:
167
  md_s += f"\n{data['role']}: \n- {data['content']} \n"
168
- with open(os.path.join(HISTORY_DIR / user_name, filename), "w", encoding="utf8") as f:
169
  f.write(md_s)
170
- logging.info(f"{user_name} 保存对话历史完毕")
171
- return os.path.join(HISTORY_DIR / user_name, filename)
172
 
173
 
174
  def sorted_by_pinyin(list):
 
154
 
155
 
156
  def save_file(filename, system, history, chatbot, user_name):
157
+ logging.debug(f"{user_name} 保存对话历史中……")
158
+ os.makedirs(os.path.join(HISTORY_DIR, user_name), exist_ok=True)
159
  if filename.endswith(".json"):
160
  json_s = {"system": system, "history": history, "chatbot": chatbot}
161
  print(json_s)
162
+ with open(os.path.join(HISTORY_DIR, user_name, filename), "w") as f:
163
  json.dump(json_s, f)
164
  elif filename.endswith(".md"):
165
  md_s = f"system: \n- {system} \n"
166
  for data in history:
167
  md_s += f"\n{data['role']}: \n- {data['content']} \n"
168
+ with open(os.path.join(HISTORY_DIR, user_name, filename), "w", encoding="utf8") as f:
169
  f.write(md_s)
170
+ logging.debug(f"{user_name} 保存对话历史完毕")
171
+ return os.path.join(HISTORY_DIR, user_name, filename)
172
 
173
 
174
  def sorted_by_pinyin(list):