From 6231dfacb630a9817b0afa6d3fa8c307c827ee4b Mon Sep 17 00:00:00 2001 From: shihanwan Date: Wed, 16 Oct 2024 17:08:43 -0400 Subject: [PATCH 1/4] fix forgetting vector or triple stores in ephemeral mode --- memonto/core/forget.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/memonto/core/forget.py b/memonto/core/forget.py index f87dd4f..e5043b9 100644 --- a/memonto/core/forget.py +++ b/memonto/core/forget.py @@ -14,14 +14,14 @@ def _forget( ) -> None: if ephemeral: data.remove((None, None, None)) + else: + try: + if vector_store: + vector_store.delete(id) - try: - if vector_store: - vector_store.delete(id) - - if triple_store: - triple_store.delete_all(id) - except ValueError as e: - logger.warning(e) - except Exception as e: - logger.error(e) + if triple_store: + triple_store.delete_all(id) + except ValueError as e: + logger.warning(e) + except Exception as e: + logger.error(e) From e6357327c9f721cd4b3ad12c7b8f188e86b07167 Mon Sep 17 00:00:00 2001 From: shihanwan Date: Wed, 16 Oct 2024 17:23:51 -0400 Subject: [PATCH 2/4] add relevant memories to retain --- memonto/core/retain.py | 29 ++++++++++++++++++++++++- memonto/prompts/commit_to_memory.prompt | 12 +++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/memonto/core/retain.py b/memonto/core/retain.py index e95e1ef..9221c36 100644 --- a/memonto/core/retain.py +++ b/memonto/core/retain.py @@ -145,6 +145,23 @@ def update_memory( return str(updated_memory) +def find_relevant_memories( + data: Graph, + vector_store: VectorStoreModel, + message: str, + id: str, + ephemeral: bool, +) -> str: + relevant_memory = "" + + if ephemeral: + relevant_memory = str(data.serialize(format="turtle")) + else: + relevant_memory = str(vector_store.search(message=message, id=id, k=3)) + + logger.debug(f"relevant_memory\n{relevant_memory}\n") + return relevant_memory + def save_memory( ontology: Graph, namespaces: dict[str, Namespace], @@ -158,12 +175,22 @@ def save_memory( str_ontology: str, updated_memory: str, ) -> None: + relevant_memory = find_relevant_memories( + data=data, + vector_store=vector_store, + message=message, + id=id, + ephemeral=ephemeral, + ) + print(relevant_memory) + script = llm.prompt( prompt_name="commit_to_memory", temperature=0.2, ontology=str_ontology, user_message=message, updated_memory=updated_memory, + relevant_memory=relevant_memory, ) logger.debug(f"Retain Script\n{script}\n") @@ -180,7 +207,7 @@ def save_memory( logger.debug(f"Data Graph\n{data.serialize(format='turtle')}\n") # debug - # _render(g=data, ns=namespaces, format="image") + _render(g=data, ns=namespaces, format="image") if not ephemeral: hydrate_graph_with_ids(data) diff --git a/memonto/prompts/commit_to_memory.prompt b/memonto/prompts/commit_to_memory.prompt index c136bb7..7980f1f 100644 --- a/memonto/prompts/commit_to_memory.prompt +++ b/memonto/prompts/commit_to_memory.prompt @@ -10,14 +10,20 @@ And this user message: ${user_message} ``` -And these removed triples: +And these removed memories: ``` ${updated_memory} ``` +And these relevant memories: +``` +${relevant_memory} +``` + Analyze the user message to find AS MUCH new information AS POSSIBLE that could fit onto the above ontology while adhering to these rules: -- First find all the new information in the user message that maps onto BOTH the above ontology and ESPECIALLY the removed triples. -- Second apply the existing namespaces to the extracted information. +- First find all the new information in the user message that maps onto BOTH the above ontology and ESPECIALLY the removed memories. +- Second check if the relevant memories can help extract even more information from the user message that maps onto the ontology or removed memories. +- Third apply only the existing namespaces to the extracted information. - Finally create the script that will add the extracted information to an rdflib graph called `data`. - NEVER generate code that initializes new graphs, namespaces, classes, properties, etc. - GENERATE Python code to add the triples with the relevant information assuming rdflib Graph `data` and the newly added namespaces already exists. From 38af4c19bd7c10521d63b481774194d3943ba850 Mon Sep 17 00:00:00 2001 From: shihanwan Date: Wed, 16 Oct 2024 17:26:49 -0400 Subject: [PATCH 3/4] fix tests --- memonto/core/retain.py | 4 ++-- tests/core/test_retain.py | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/memonto/core/retain.py b/memonto/core/retain.py index 9221c36..16436e6 100644 --- a/memonto/core/retain.py +++ b/memonto/core/retain.py @@ -158,10 +158,11 @@ def find_relevant_memories( relevant_memory = str(data.serialize(format="turtle")) else: relevant_memory = str(vector_store.search(message=message, id=id, k=3)) - + logger.debug(f"relevant_memory\n{relevant_memory}\n") return relevant_memory + def save_memory( ontology: Graph, namespaces: dict[str, Namespace], @@ -182,7 +183,6 @@ def save_memory( id=id, ephemeral=ephemeral, ) - print(relevant_memory) script = llm.prompt( prompt_name="commit_to_memory", diff --git a/tests/core/test_retain.py b/tests/core/test_retain.py index d3507db..32e0a95 100644 --- a/tests/core/test_retain.py +++ b/tests/core/test_retain.py @@ -66,6 +66,7 @@ def test_commit_memory( ontology=ANY, user_message=user_query, updated_memory="", + relevant_memory=ANY, ) assert mock_llm.prompt.call_count == 1 @@ -103,6 +104,7 @@ def test_commit_memory_with_exception( ontology=ANY, user_message=user_query, updated_memory="", + relevant_memory=ANY, ) ctmeh_prompt = call( @@ -146,6 +148,7 @@ def test_commit_memory_auto_expand( ontology=ANY, user_message=user_query, updated_memory="", + relevant_memory=ANY, ) eo_prompt = call( From 4f2e657470326b5020adce17f0f7c9edfceea545 Mon Sep 17 00:00:00 2001 From: shihanwan Date: Wed, 16 Oct 2024 17:47:08 -0400 Subject: [PATCH 4/4] remove debug --- memonto/core/retain.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/memonto/core/retain.py b/memonto/core/retain.py index 16436e6..f170ce9 100644 --- a/memonto/core/retain.py +++ b/memonto/core/retain.py @@ -207,7 +207,7 @@ def save_memory( logger.debug(f"Data Graph\n{data.serialize(format='turtle')}\n") # debug - _render(g=data, ns=namespaces, format="image") + # _render(g=data, ns=namespaces, format="image") if not ephemeral: hydrate_graph_with_ids(data)