griptape icon indicating copy to clipboard operation
griptape copied to clipboard

feat: persist summary in conversation memory

Open torabshaikh opened this issue 7 months ago • 5 comments

Overrode after_add_run method to add summary in metadata so it can persist.

#1590

I used this code to test it. I ran it one time as it is and second time after commenting the agent.run and found that I was able to load the runs but not summaries.

import os
from griptape.drivers.prompt.google import GooglePromptDriver
from griptape.memory.structure import SummaryConversationMemory
from griptape.structures import Agent
from griptape.drivers.memory.conversation.redis import RedisConversationMemoryDriver

prompt_driver = GooglePromptDriver(model="gemini-2.0-flash", api_key=os.environ["GOOGLE_API_KEY"])
redis_conversation_driver = RedisConversationMemoryDriver(
    host=os.environ["REDIS_HOST"],
    username=os.environ["REDIS_USERNAME"],
    port=int(os.environ["REDIS_PORT"]),
    password=os.environ["REDIS_PASSWORD"],
    index=os.environ["REDIS_INDEX"],
    conversation_id='torab_test',
)


conversation_memory = SummaryConversationMemory(
    offset=2,
    conversation_memory_driver=redis_conversation_driver,
    prompt_driver=prompt_driver
    )
agent = Agent(
    conversation_memory=conversation_memory,
    prompt_driver=prompt_driver
    )

agent.run("My name is Torab?")
agent.run("I am trying to resolve summary conversation issue.")
agent.run("What am I doing?")

print('conversation: ', conversation_memory.to_dict())
print('conversation summary: ', conversation_memory.summary)

I did not update the TestSummaryConversationMemory since it does not uses persistent memory.

torabshaikh avatar Apr 30 '25 06:04 torabshaikh

Thanks @torabshaikh! Could you please share a reproducible code snippet that uses LocalConversationMemoryDriver with a persist_file? That will make it easy for me to verify whether things are working as expected.

collindutter avatar Apr 30 '25 17:04 collindutter

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests.

:loudspeaker: Thoughts on this report? Let us know!

codecov[bot] avatar Apr 30 '25 17:04 codecov[bot]

Thanks for the review. As I understand this, It will be something like this, right?

   def after_add_run(self) -> None:
        self.meta["summary"] = self.summary
        self.meta["summary_index"] = self.summary_index
        super().after_add_run()
    
    def load_runs(self) -> list[Run]:
        runs, meta = self.conversation_memory_driver.load()
        self.runs.extend(runs)
        self.meta = dict_merge(self.meta, meta)
        self.summary = meta['summary']
        self.summary_index = meta['summary_index']
        return self.runs

We could have called super().load_runs() when overriding load_runs but we can't do it since it returns only self.runs. Right?

torabshaikh avatar Apr 30 '25 18:04 torabshaikh

@torabshaikh looks like tests are failing. You can run make test/unit locally to see why.

collindutter avatar May 02 '25 20:05 collindutter

Yeah. I was able to replicate the issue locally. I will update the PR.

torabshaikh avatar May 02 '25 21:05 torabshaikh

@collindutter I debugged this and found out that metadata was not getting initialized properly.

torabshaikh avatar May 22 '25 01:05 torabshaikh

@torabshaikh thanks for the continued updates. Could you please some unit tests to cover this new behavior? Then I think we can consider this done.

collindutter avatar Jun 20 '25 20:06 collindutter

Hi @collindutter, Sorry for the late response. I had been busy. I added some test cases to track this behavior and to test the full run.

torabshaikh avatar Aug 01 '25 14:08 torabshaikh