How I Implemented Short Term memory in Kairo

In short, we are storing summary in the database, we fetch summary from database before calling the agent, then pass that fetched summary to agent, agent response in context of that summary and then in background we summarise the new conversation ( user text, and ai response ) and push this new summary to the summary array in the database.

Full steps

  • fetch summary from database
  • send that fetched summary to chat agent via context
  • fetch the summary inside the agent in prompt 
  • after getting ai response, summarise the ai response and user text and update the summary database - in background


Step 1. Fetching the summary from database


We are passing thread_id to get_summary function, using that thread_id we will find the summary in the database. If summary is present then return that fetched summary , if there is no any summary present with this thread id then we will return a blank string ' '.


Step 2. Pass the fetched summary to Agent

Summary that we get from get_summary, we will pass it to main agent while invoking along with user message by using context parameter, 

Step 3. Fetching the summary inside the Agent



To fetch the summary that we passed to agent by using context, we have to use Runtime
  • First import Runtime - from  Langgraph.runtime import Runtime
  • in chat node function pass runtime -  ( runtime : Runtime )
  • now you can extract anything that you passed in the context by using runtime.context

Step 4. Summarise and Update




Now, as soon as agent response came with ai message, user will get that message instantly but in background we will summarise that response and update the summary in database, by doing this thing in background user will not have to wait for summarisation by calling an llm and then database update. User will get his response instantly and these things would run in background and will complete till user will ask the next message.

we are running the task in background by using asyncio - asyncio.create_task()

Comments

Post a Comment