rohan13 commited on
Commit
10656ee
·
1 Parent(s): 1935802

getting last 4 messages

Browse files
Files changed (1) hide show
  1. utils.py +4 -8
utils.py CHANGED
@@ -39,7 +39,7 @@ message_history = CustomMongoDBChatMessageHistory(
39
  collection_name='3d_printing_revolution'
40
  )
41
 
42
- memory = ConversationBufferWindowMemory(memory_key="chat_history", k=10)
43
 
44
  vectorstore_index = None
45
 
@@ -210,15 +210,11 @@ def get_chat_history(inputs) -> str:
210
  def generate_answer(question) -> str:
211
  global vectorstore_index
212
  chain = get_qa_chain(vectorstore_index)
213
- history = memory.chat_memory.messages
214
- print("chat history: " + str(history))
215
  result = chain(
216
  {"question": question, "chat_history": history})
217
- # chat_tuple = (question, result["answer"])
218
- # chat_history.append(chat_tuple)
219
  save_chat_history(question, result)
220
- chat_history = [message.content for message in memory.chat_memory.messages]
221
- print("chat history after response: " + str(chat_history))
222
  sources = []
223
  print(result)
224
 
@@ -234,4 +230,4 @@ def generate_answer(question) -> str:
234
  def save_chat_history(question, result):
235
  memory.chat_memory.add_user_message(question)
236
  memory.chat_memory.add_ai_message(result["answer"])
237
- print("chat history on saving: " + str(memory.chat_memory.messages))
 
39
  collection_name='3d_printing_revolution'
40
  )
41
 
42
+ memory = ConversationBufferWindowMemory(memory_key="chat_history", k=4)
43
 
44
  vectorstore_index = None
45
 
 
210
  def generate_answer(question) -> str:
211
  global vectorstore_index
212
  chain = get_qa_chain(vectorstore_index)
213
+ history = memory.chat_memory.messages[-4:]
 
214
  result = chain(
215
  {"question": question, "chat_history": history})
 
 
216
  save_chat_history(question, result)
217
+
 
218
  sources = []
219
  print(result)
220
 
 
230
  def save_chat_history(question, result):
231
  memory.chat_memory.add_user_message(question)
232
  memory.chat_memory.add_ai_message(result["answer"])
233
+ print("chat history after saving: " + str(memory.chat_memory.messages))