10zinten commited on
Commit
ec5af7e
·
1 Parent(s): 48b9027

add clear chatbot history

Browse files
Files changed (2) hide show
  1. app.py +2 -4
  2. chat.py +4 -0
app.py CHANGED
@@ -26,7 +26,7 @@ CHATBOT_HISTORY = List[CHATBOT_MSG]
26
  LANG_BO = "bo"
27
  LANG_MEDIUM = "en"
28
 
29
- chatbot: Optional[ChatGpt] = None
30
 
31
 
32
  def bing_translate(text: str, from_lang: str, to_lang: str):
@@ -83,9 +83,6 @@ def bot(history_bo: list, chat_id: str):
83
  history_bo (CHATBOT_HISTORY): Tibetan history of gradio chatbot
84
  history_en (CHATGPT_HISTORY): English history of OpenAI ChatGPT
85
  """
86
- global chatbot
87
- if len(history_bo) <= 1:
88
- chatbot = ChatGpt(OPENAI_API_KEY)
89
  input_bo = history_bo[-1][0]
90
  input_ = bing_translate(input_bo, LANG_BO, LANG_MEDIUM)
91
  response = chatbot.generate_response(input_)
@@ -107,6 +104,7 @@ def bot(history_bo: list, chat_id: str):
107
 
108
 
109
  def get_chat_id():
 
110
  return str(uuid.uuid4())
111
 
112
 
 
26
  LANG_BO = "bo"
27
  LANG_MEDIUM = "en"
28
 
29
+ chatbot = ChatGpt(OPENAI_API_KEY)
30
 
31
 
32
  def bing_translate(text: str, from_lang: str, to_lang: str):
 
83
  history_bo (CHATBOT_HISTORY): Tibetan history of gradio chatbot
84
  history_en (CHATGPT_HISTORY): English history of OpenAI ChatGPT
85
  """
 
 
 
86
  input_bo = history_bo[-1][0]
87
  input_ = bing_translate(input_bo, LANG_BO, LANG_MEDIUM)
88
  response = chatbot.generate_response(input_)
 
104
 
105
 
106
  def get_chat_id():
107
+ chatbot.clear_history()
108
  return str(uuid.uuid4())
109
 
110
 
chat.py CHANGED
@@ -14,6 +14,10 @@ class ChatGpt:
14
  # Set up the OpenAI API client
15
  openai.api_key = self.api_key
16
 
 
 
 
 
17
  def add_message(self, role: str, content: str):
18
  self.message_history.append({"role": role, "content": content})
19
  self._truncate_history()
 
14
  # Set up the OpenAI API client
15
  openai.api_key = self.api_key
16
 
17
+ def clear_history(self):
18
+ self.message_history = []
19
+ self.total_tokens = 0
20
+
21
  def add_message(self, role: str, content: str):
22
  self.message_history.append({"role": role, "content": content})
23
  self._truncate_history()