From f6929155b8b1b9a83dd2686a69343e0ca8a5fa0a Mon Sep 17 00:00:00 2001 From: Davide Bizzi Date: Tue, 24 Dec 2024 19:32:43 +0100 Subject: [PATCH] feat: Rimuovi costante di impostazione e migliora la gestione della memoria sentimentale --- lib/activation.py | 29 +++++++++-------------------- lib/const.py | 1 - lib/memory.py | 10 +++++++++- lib/sentiment_memory.py | 30 ++++++++++++++++++++++++++++++ queen_of_hearts.py | 19 +++++++++---------- 5 files changed, 57 insertions(+), 32 deletions(-) delete mode 100644 lib/const.py create mode 100644 lib/sentiment_memory.py diff --git a/lib/activation.py b/lib/activation.py index fec1ef5..726cea1 100644 --- a/lib/activation.py +++ b/lib/activation.py @@ -1,8 +1,5 @@ from .memory import Memory from cat.looking_glass.cheshire_cat import CheshireCat -from cat.db.crud import get_setting_by_name,upsert_setting_by_name -from cat.db.models import Setting -from .const import QUEEN_OF_HEARTS_INJESTION_SETTING ccat = CheshireCat() @@ -73,28 +70,21 @@ def process(item): memory = Memory(ccat, item["value"], item["text"], item["reason"]) + memory.set_metadata("default_memory_id", item["id"]) memory.save() -def get_injested_ids(key): - sett = get_setting_by_name(key) - if not sett is None: - return sett["value"] - return [] - -def update_injested_ids(key, value): - upsert_setting_by_name( - Setting( - name=key, - value=value - ) - ) +def get_injested_ids(): + from .sentiment_memory import get_sentiment_memories + memories = get_sentiment_memories() + memories = [memory for memory in memories if "default_memory_id" in memory["metadata"]] + return [memory["metadata"]["default_memory_id"] for memory in memories] def activation(plugin): - setting_key=QUEEN_OF_HEARTS_INJESTION_SETTING - injested_ids = get_injested_ids(setting_key) + injested_ids = get_injested_ids() + valid_injested_ids = [id for id in injested_ids if any(item['id'] == id for item in default_memories)] @@ -103,5 +93,4 @@ def activation(plugin): process(item) valid_injested_ids.append(item['id']) - - update_injested_ids(setting_key, valid_injested_ids) \ No newline at end of file + \ No newline at end of file diff --git a/lib/const.py b/lib/const.py deleted file mode 100644 index f2eca7b..0000000 --- a/lib/const.py +++ /dev/null @@ -1 +0,0 @@ -QUEEN_OF_HEARTS_INJESTION_SETTING = 'queen_of_hearts_injested' \ No newline at end of file diff --git a/lib/memory.py b/lib/memory.py index 1824660..16c276f 100644 --- a/lib/memory.py +++ b/lib/memory.py @@ -7,6 +7,9 @@ def __init__(self, ccat, value, message, reason): "reason": reason } self.memory["string_value"] = self.get_sentiment_string() + + def set_metadata(self, key, value): + self.memory[key] = value def get_sentiment_string(self): @@ -33,6 +36,11 @@ def save(self): with open('sentiment_memory.txt', 'w') as file: file.write(new_memory + '\n') - self.ccat.rabbit_hole.ingest_file(self.ccat,'sentiment_memory.txt', metadata={"type": "sentiment_memory"}) + metadata = { + "type": "sentiment_memory", + } + if ("default_memory_id" in self.memory): + metadata["default_memory_id"] = self.memory["default_memory_id"] + self.ccat.rabbit_hole.ingest_file(self.ccat,'sentiment_memory.txt', metadata=metadata) diff --git a/lib/sentiment_memory.py b/lib/sentiment_memory.py new file mode 100644 index 0000000..d6a5d61 --- /dev/null +++ b/lib/sentiment_memory.py @@ -0,0 +1,30 @@ + +from cat.looking_glass.cheshire_cat import CheshireCat + +ccat = CheshireCat() + +def get_sentiment_memories(): + result = [] + + collection = ccat.memory.vectors.collections["declarative"] + memories = collection.client.scroll( + collection_name="declarative", + with_vectors=True, + limit=10000, # yeah, good for now dear :* + ) + if len(memories) > 0: + memories = memories[0] + + memories = [memory for memory in memories if memory.payload["metadata"]["type"] == "sentiment_memory"] + + # get metadata and id only + result = [{"id": memory.id, "metadata": memory.payload["metadata"]} for memory in memories] + + + return result + +def clear_sentiment_memories(): + + collections = list(ccat.memory.vectors.collections.keys()) + if ("declarative" in collections): + ccat.memory.vectors.collections["declarative"].delete_points_by_metadata_filter({"type": "sentiment_memory"}) diff --git a/queen_of_hearts.py b/queen_of_hearts.py index 02451f2..bb81ff7 100644 --- a/queen_of_hearts.py +++ b/queen_of_hearts.py @@ -1,9 +1,7 @@ from cat.mad_hatter.decorators import hook from cat.mad_hatter.decorators import plugin -from cat.mad_hatter.decorators import tool -from cat.db.crud import get_setting_by_name,delete_setting_by_id -from .lib.const import QUEEN_OF_HEARTS_INJESTION_SETTING +from .lib.sentiment_memory import get_sentiment_memories,clear_sentiment_memories from .lib.activation import activation from .lib.prefix import prompt_prefix from .lib.save_memory import save_memory @@ -27,16 +25,17 @@ def before_cat_sends_message(message, cat): @hook(priority=0) def agent_fast_reply(fast_reply, cat): if (cat.working_memory.user_message_json.text == "/empty_qoh"): + memories = get_sentiment_memories() - collections = list(cat.memory.vectors.collections.keys()) - if ("declarative" in collections): - print(cat.memory.vectors.collections["declarative"].delete_points_by_metadata_filter({"type": "sentiment_memory"})) - - setting = get_setting_by_name(QUEEN_OF_HEARTS_INJESTION_SETTING) - if setting is not None: - delete_setting_by_id(setting["setting_id"]) + if len(memories) > 0: + clear_sentiment_memories() return { "output": "Ok, la memoria di Queen of Hearts è stata svuotata." } + return {"output":"Non c'è nulla da svuotare."} + elif (cat.working_memory.user_message_json.text == "/show_qoh"): + memories = get_sentiment_memories() + + return {"output":str(memories)}