lubomirovna commited on
Commit
33110d2
1 Parent(s): 5084546

Fix chat history

Browse files
Files changed (1) hide show
  1. app.py +34 -11
app.py CHANGED
@@ -28,6 +28,31 @@ FREQUENCY_PENALTY = 0
28
  PRESENCE_PENALTY = 0.6
29
  MAX_CONTEXT_QUESTIONS = 10
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  @cl.on_chat_start
32
  async def start_chat():
33
  settings = {
@@ -46,21 +71,17 @@ async def start_chat():
46
  cl.user_session.set("settings", settings)
47
  cl.user_session.set("messages", messages)
48
 
 
49
  @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
50
  async def main(message: cl.Message):
51
  settings = cl.user_session.get("settings")
52
  messages = cl.user_session.get("messages")
53
- chat_history = cl.user_session.get("chat_history", [])
54
-
55
  client = AsyncOpenAI()
56
 
57
- # Add the previous questions and answers
58
- for question, answer in chat_history[-MAX_CONTEXT_QUESTIONS:]:
59
- messages.append({"role": "user", "content": question})
60
- messages.append({"role": "assistant", "content": answer})
61
-
62
  # Add the new question
63
- messages.append({"role": "user", "content": message.content})
 
 
64
 
65
  msg = cl.Message(content="")
66
  full_response = ""
@@ -77,9 +98,11 @@ async def main(message: cl.Message):
77
 
78
  # Send and close the message stream
79
  await msg.send()
80
-
81
  # Update chat history
82
- chat_history.append((message.content, full_response))
83
- cl.user_session.set("chat_history", chat_history)
 
 
84
  cl.user_session.set("messages", messages)
85
 
 
28
  PRESENCE_PENALTY = 0.6
29
  MAX_CONTEXT_QUESTIONS = 10
30
 
31
+
32
+ def update_chat_history(history, role, text):
33
+ """
34
+ Updates the chat history by adding a new message and ensuring the length of the history does not exceed max_len.
35
+ The first element (system message) is never removed.
36
+
37
+ Parameters:
38
+ history (list): The current chat history.
39
+ new_message (dict): The new message to be added.
40
+ max_len (int): The maximum length of the chat history including the system message.
41
+
42
+ Returns:
43
+ list: The updated chat history.
44
+ """
45
+
46
+ # Add the new message
47
+ history.append({ 'role': role, 'content': text })
48
+
49
+ # Slice the history to keep the system message and the most recent messages
50
+ if len(history) > MAX_CONTEXT_QUESTIONS:
51
+ history = history[0] + history[1-MAX_CONTEXT_QUESTIONS:]
52
+
53
+ return history
54
+
55
+
56
  @cl.on_chat_start
57
  async def start_chat():
58
  settings = {
 
71
  cl.user_session.set("settings", settings)
72
  cl.user_session.set("messages", messages)
73
 
74
+
75
  @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
76
  async def main(message: cl.Message):
77
  settings = cl.user_session.get("settings")
78
  messages = cl.user_session.get("messages")
 
 
79
  client = AsyncOpenAI()
80
 
 
 
 
 
 
81
  # Add the new question
82
+ messages = update_chat_history(messages, "user", message.content)
83
+
84
+ print(f"Messages: {messages[1:]}")
85
 
86
  msg = cl.Message(content="")
87
  full_response = ""
 
98
 
99
  # Send and close the message stream
100
  await msg.send()
101
+
102
  # Update chat history
103
+ messages = update_chat_history(messages, "assistant", full_response)
104
+
105
+ print(f"Messages: {messages[1:]}")
106
+
107
  cl.user_session.set("messages", messages)
108