26
26
ABS_PATH = os .getcwd ()
27
27
DB_DIR = os .path .join (ABS_PATH , "db" )
28
28
29
- memory = ConversationBufferMemory ()
30
-
31
29
32
30
class EmbedChain :
33
31
def __init__ (self , config : BaseAppConfig ):
@@ -44,6 +42,7 @@ def __init__(self, config: BaseAppConfig):
44
42
self .user_asks = []
45
43
self .is_docs_site_instance = False
46
44
self .online = False
45
+ self .memory = ConversationBufferMemory ()
47
46
48
47
# Send anonymous telemetry
49
48
self .s_id = self .config .id if self .config .id else str (uuid .uuid4 ())
@@ -362,8 +361,7 @@ def chat(self, input_query, config: ChatConfig = None, dry_run=False):
362
361
k ["web_search_result" ] = self .access_search_and_get_results (input_query )
363
362
contexts = self .retrieve_from_database (input_query , config )
364
363
365
- global memory
366
- chat_history = memory .load_memory_variables ({})["history" ]
364
+ chat_history = self .memory .load_memory_variables ({})["history" ]
367
365
368
366
if chat_history :
369
367
config .set_history (chat_history )
@@ -376,14 +374,14 @@ def chat(self, input_query, config: ChatConfig = None, dry_run=False):
376
374
377
375
answer = self .get_answer_from_llm (prompt , config )
378
376
379
- memory .chat_memory .add_user_message (input_query )
377
+ self . memory .chat_memory .add_user_message (input_query )
380
378
381
379
# Send anonymous telemetry
382
380
thread_telemetry = threading .Thread (target = self ._send_telemetry_event , args = ("chat" ,))
383
381
thread_telemetry .start ()
384
382
385
383
if isinstance (answer , str ):
386
- memory .chat_memory .add_ai_message (answer )
384
+ self . memory .chat_memory .add_ai_message (answer )
387
385
logging .info (f"Answer: { answer } " )
388
386
return answer
389
387
else :
@@ -395,7 +393,7 @@ def _stream_chat_response(self, answer):
395
393
for chunk in answer :
396
394
streamed_answer = streamed_answer + chunk
397
395
yield chunk
398
- memory .chat_memory .add_ai_message (streamed_answer )
396
+ self . memory .chat_memory .add_ai_message (streamed_answer )
399
397
logging .info (f"Answer: { streamed_answer } " )
400
398
401
399
def set_collection (self , collection_name ):
0 commit comments