core icon indicating copy to clipboard operation
core copied to clipboard

Modifying agent_input.chat_history in before_agent_starts hook has no effect

Open AlessandroSpallina opened this issue 5 months ago • 1 comments

When a plugin developer wants to use the hook before_agent_starts for tasks like modifying the chat history, acting on agent_input.chat_history has no effect.

This code isn't working:

@hook
def before_agent_starts(agent_input, cat):
    # When the LLM reads the chat history, it will lie generating the string "Generated in x seconds"
    # this is not an intended behavour, so we delete each occurrence of that string
    
    agent_input['chat_history'] = "\n".join([line for line in agent_input["chat_history"].splitlines() if '✨ _Generated' not in line])
   
    return agent_input

Instead, acting on cat.working_memory.history is the right way to modify the conversation history. The following code is working properly

@hook
def before_agent_starts(agent_input, cat):
    # When the LLM reads the chat history, it will lie generating the string "Generated in x seconds"
    # this is not an intended behavour, so we delete each occurrence of that string

    if len(cat.working_memory.history) >= 2:
        cat.working_memory.history[-2]['message'] = "\n".join([line for line in cat.working_memory.history[-2]['message'].splitlines() if '✨ _Generated' not in line])

    return agent_input

AlessandroSpallina avatar Sep 18 '24 08:09 AlessandroSpallina