Skip to content

Commit

Permalink
Merge pull request #23 from lfoppiano/manage-conversational-memory
Browse files Browse the repository at this point in the history
restrict the chat memory to avoid hallucinations
  • Loading branch information
lfoppiano authored Nov 29, 2023
2 parents 9b528b7 + c3cc60f commit 2e29565
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions document_qa/document_qa_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ def _get_context(self, doc_id, query, context_size=4):
relevant_documents = retriever.get_relevant_documents(query)
if self.memory and len(self.memory.buffer_as_messages) > 0:
relevant_documents.append(
Document(page_content="Previous conversation:\n{}\n\n".format(self.memory.buffer_as_str))
Document(
page_content="""Following, the previous question and answers. Use these information only when in the question there are unspecified references:\n{}\n\n""".format(
self.memory.buffer_as_str))
)
return relevant_documents

Expand Down Expand Up @@ -257,8 +259,9 @@ def create_memory_embeddings(self, pdf_path, doc_id=None, chunk_size=500, perc_o
metadatas=metadata,
collection_name=hash)
else:
if 'documents' in self.embeddings_dict[hash].get() and len(self.embeddings_dict[hash].get()['documents']) == 0:
self.embeddings_dict[hash].delete(ids=self.embeddings_dict[hash].get()['ids'])
# if 'documents' in self.embeddings_dict[hash].get() and len(self.embeddings_dict[hash].get()['documents']) == 0:
# self.embeddings_dict[hash].delete(ids=self.embeddings_dict[hash].get()['ids'])
self.embeddings_dict[hash].delete_collection()
self.embeddings_dict[hash] = Chroma.from_texts(texts,
embedding=self.embedding_function,
metadatas=metadata,
Expand Down

0 comments on commit 2e29565

Please sign in to comment.