Tuchuanhuhuhu commited on
Commit
ba1c857
1 Parent(s): eab29c1

改动:将历史记录文件保存在文件夹里

Browse files
Files changed (1) hide show
  1. ChuanhuChatbot.py +13 -8
ChuanhuChatbot.py CHANGED
@@ -10,6 +10,7 @@ import requests
10
  my_api_key = "" # 在这里输入你的 API 密钥
11
  initial_prompt = "You are a helpful assistant."
12
  API_URL = "https://api.openai.com/v1/chat/completions"
 
13
 
14
 
15
 
@@ -18,7 +19,7 @@ if os.environ.get('dockerrun') == 'yes':
18
  dockerflag = True
19
  else:
20
  dockerflag = False
21
-
22
  if dockerflag:
23
  my_api_key = os.environ.get('my_api_key')
24
  if my_api_key == "empty":
@@ -159,25 +160,29 @@ def delete_last_conversation(chatbot, history):
159
  history.pop()
160
  return chatbot, history
161
 
162
- def save_chat_history(filepath, system, history, chatbot):
163
- if filepath == "":
164
  return
165
- if not filepath.endswith(".json"):
166
- filepath += ".json"
 
167
  json_s = {"system": system, "history": history, "chatbot": chatbot}
168
- with open(filepath, "w") as f:
169
  json.dump(json_s, f)
170
 
171
 
172
  def load_chat_history(filename):
173
- with open(filename, "r") as f:
174
  json_s = json.load(f)
175
  return filename, json_s["system"], json_s["history"], json_s["chatbot"]
176
 
177
 
178
  def get_history_names(plain=False):
179
  # find all json files in the current directory and return their names
180
- files = [f for f in os.listdir() if f.endswith(".json")]
 
 
 
181
  if plain:
182
  return files
183
  else:
 
10
  my_api_key = "" # 在这里输入你的 API 密钥
11
  initial_prompt = "You are a helpful assistant."
12
  API_URL = "https://api.openai.com/v1/chat/completions"
13
+ HISTORY_DIR = "history"
14
 
15
 
16
 
 
19
  dockerflag = True
20
  else:
21
  dockerflag = False
22
+
23
  if dockerflag:
24
  my_api_key = os.environ.get('my_api_key')
25
  if my_api_key == "empty":
 
160
  history.pop()
161
  return chatbot, history
162
 
163
+ def save_chat_history(filename, system, history, chatbot):
164
+ if filename == "":
165
  return
166
+ if not filename.endswith(".json"):
167
+ filename += ".json"
168
+ os.makedirs(HISTORY_DIR, exist_ok=True)
169
  json_s = {"system": system, "history": history, "chatbot": chatbot}
170
+ with open(os.path.join(HISTORY_DIR, filename), "w") as f:
171
  json.dump(json_s, f)
172
 
173
 
174
  def load_chat_history(filename):
175
+ with open(os.path.join(HISTORY_DIR, filename), "r") as f:
176
  json_s = json.load(f)
177
  return filename, json_s["system"], json_s["history"], json_s["chatbot"]
178
 
179
 
180
  def get_history_names(plain=False):
181
  # find all json files in the current directory and return their names
182
+ try:
183
+ files = [f for f in os.listdir(HISTORY_DIR) if f.endswith(".json")]
184
+ except FileNotFoundError:
185
+ files = []
186
  if plain:
187
  return files
188
  else: