AllenYkl commited on
Commit
0e4b4f6
1 Parent(s): 6e561cc

Update bin_public/utils/utils.py

Browse files
Files changed (1) hide show
  1. bin_public/utils/utils.py +26 -9
bin_public/utils/utils.py CHANGED
@@ -336,21 +336,38 @@ def delete_last_conversation(chatbot, history, previous_token_count):
336
  return chatbot, history, previous_token_count, construct_token_message(sum(previous_token_count))
337
 
338
 
339
- def save_chat_history(filename, system, history, chatbot):
340
- # 初步想法,不自己取名,统一叫data.json, 待实现
341
  logging.info("保存对话历史中……")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
342
  if filename == "":
343
  return
344
  if not filename.endswith(".json"):
345
  filename += ".json"
346
- # os.makedirs(HISTORY_DIR, exist_ok=True)
347
- json_s = {"system": system, "history": history, "chatbot": chatbot}
348
- logging.info(json_s)
349
- with open(f"./data.json", "w") as f:
350
- json.dump(json_s, f, ensure_ascii=False, indent=4)
351
- logging.info("保存对话历史完毕")
352
 
353
- return f"./data.json"
 
 
 
 
 
354
 
355
 
356
  def load_chat_history(filename, system, history, chatbot):
 
336
  return chatbot, history, previous_token_count, construct_token_message(sum(previous_token_count))
337
 
338
 
339
+ def save_file(filename, system, history, chatbot):
 
340
  logging.info("保存对话历史中……")
341
+ os.makedirs(HISTORY_DIR, exist_ok=True)
342
+ if filename.endswith(".json"):
343
+ json_s = {"system": system, "history": history, "chatbot": chatbot}
344
+ print(json_s)
345
+ with open(os.path.join(HISTORY_DIR, filename), "w") as f:
346
+ json.dump(json_s, f)
347
+ elif filename.endswith(".md"):
348
+ md_s = f"system: \n- {system} \n"
349
+ for data in history:
350
+ md_s += f"\n{data['role']}: \n- {data['content']} \n"
351
+ with open(os.path.join(HISTORY_DIR, filename), "w", encoding="utf8") as f:
352
+ f.write(md_s)
353
+ logging.info("保存对话历史完毕")
354
+ return os.path.join(HISTORY_DIR, filename)
355
+
356
+
357
+ def save_chat_history(filename, system, history, chatbot):
358
  if filename == "":
359
  return
360
  if not filename.endswith(".json"):
361
  filename += ".json"
362
+ return save_file(filename, system, history, chatbot)
363
+
 
 
 
 
364
 
365
+ def export_markdown(filename, system, history, chatbot):
366
+ if filename == "":
367
+ return
368
+ if not filename.endswith(".md"):
369
+ filename += ".md"
370
+ return save_file(filename, system, history, chatbot)
371
 
372
 
373
  def load_chat_history(filename, system, history, chatbot):