Skip to content

Commit

Permalink
feat: Rimuovi costante di impostazione e migliora la gestione della m…
Browse files Browse the repository at this point in the history
…emoria sentimentale
  • Loading branch information
d-beezee committed Dec 24, 2024
1 parent 98726d6 commit f692915
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 32 deletions.
29 changes: 9 additions & 20 deletions lib/activation.py
Original file line number Diff line number Diff line change
@@ -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()

Expand Down Expand Up @@ -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)]

Expand All @@ -103,5 +93,4 @@ def activation(plugin):
process(item)
valid_injested_ids.append(item['id'])


update_injested_ids(setting_key, valid_injested_ids)

1 change: 0 additions & 1 deletion lib/const.py

This file was deleted.

10 changes: 9 additions & 1 deletion lib/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)


30 changes: 30 additions & 0 deletions lib/sentiment_memory.py
Original file line number Diff line number Diff line change
@@ -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"})
19 changes: 9 additions & 10 deletions queen_of_hearts.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)}

0 comments on commit f692915

Please sign in to comment.