MZhaovo commited on
Commit
b25e7e2
1 Parent(s): 5f19e45

feat: 优化自动命名提示词

Browse files
modules/models/base_model.py CHANGED
@@ -682,7 +682,7 @@ class BaseLLMModel:
682
  def auto_name_chat_history(self, name_chat_method, user_question, chatbot, user_name, single_turn_checkbox):
683
  if len(self.history) == 2 and not single_turn_checkbox:
684
  user_question = self.history[0]["content"]
685
- filename = user_question[:16] + ".json"
686
  return self.rename_chat_history(filename, chatbot, user_name)
687
  else:
688
  return gr.update()
 
682
  def auto_name_chat_history(self, name_chat_method, user_question, chatbot, user_name, single_turn_checkbox):
683
  if len(self.history) == 2 and not single_turn_checkbox:
684
  user_question = self.history[0]["content"]
685
+ filename = replace_special_symbols(user_question)[:16] + ".json"
686
  return self.rename_chat_history(filename, chatbot, user_name)
687
  else:
688
  return gr.update()
modules/models/models.py CHANGED
@@ -264,18 +264,18 @@ class OpenAIClient(BaseLLMModel):
264
  try:
265
  history = [
266
  { "role": "system", "content": SUMMARY_CHAT_SYSTEM_PROMPT},
267
- { "role": "user", "content": f"User: {user_question}\nAssistant: {ai_answer}"}
268
  ]
269
  response = self._single_query_at_once(history, temperature=0.0)
270
  response = json.loads(response.text)
271
  content = response["choices"][0]["message"]["content"]
272
- filename = content + ".json"
273
  except Exception as e:
274
  logging.info(f"自动命名失败。{e}")
275
- filename = user_question[:16] + ".json"
276
  return self.rename_chat_history(filename, chatbot, user_name)
277
  elif name_chat_method == i18n("第一条提问"):
278
- filename = user_question[:16] + ".json"
279
  return self.rename_chat_history(filename, chatbot, user_name)
280
  else:
281
  return gr.update()
 
264
  try:
265
  history = [
266
  { "role": "system", "content": SUMMARY_CHAT_SYSTEM_PROMPT},
267
+ { "role": "user", "content": f"Please write a title based on the following conversation:\n---\nUser: {user_question}\nAssistant: {ai_answer}"}
268
  ]
269
  response = self._single_query_at_once(history, temperature=0.0)
270
  response = json.loads(response.text)
271
  content = response["choices"][0]["message"]["content"]
272
+ filename = replace_special_symbols(content) + ".json"
273
  except Exception as e:
274
  logging.info(f"自动命名失败。{e}")
275
+ filename = replace_special_symbols(user_question)[:16] + ".json"
276
  return self.rename_chat_history(filename, chatbot, user_name)
277
  elif name_chat_method == i18n("第一条提问"):
278
+ filename = replace_special_symbols(user_question)[:16] + ".json"
279
  return self.rename_chat_history(filename, chatbot, user_name)
280
  else:
281
  return gr.update()
modules/utils.py CHANGED
@@ -717,3 +717,11 @@ def get_file_hash(file_src=None, file_paths=None):
717
 
718
  def myprint(**args):
719
  print(args)
 
 
 
 
 
 
 
 
 
717
 
718
  def myprint(**args):
719
  print(args)
720
+
721
+ def replace_special_symbols(string, replace_string=" "):
722
+ # 定义正则表达式,匹配所有特殊符号
723
+ pattern = r'[!@#$%^&*()<>?/\|}{~:]'
724
+
725
+ new_string = re.sub(pattern, replace_string, string)
726
+
727
+ return new_string