Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite memory condenser #2021

Closed
wants to merge 143 commits into from
Closed

Rewrite memory condenser #2021

wants to merge 143 commits into from

Conversation

enyst
Copy link
Collaborator

@enyst enyst commented May 23, 2024

This PR is proposing an implementation of the memory condenser inspired by this comment. #1771 (review)

The agent uses memory component to select stuff, summarize stuff, truncate stuff ==> which it then injects in the context.

Please note: the refactoring of state.history is a bit large and it changes the basic assumption that events come in tuples (action, observation). They no longer did in the Event Stream anyway, but the controller was still filling in the blanks for the Agent and its history, so they continued to rely on tuples.

This PR reimplements the "short term history" part of the Memory component to work like a memory stream basically: it doesn't store its own versions, it doesn't fill in tuples, it just filters the stream and uses summaries in place of chunks of events to send to the LLM when necessary.

Goals:

  • happens when ContextWindowExceeded is raised, or if optional user config for max_input_tokens is hit
  • logic:
    • condense agent events per chunk, where one (action, obs) <= chunk < user message
    • keep user messages (aside from system prompt of course)
    • condense only the earlier chunk(s), until needed again - very lazy! 😅
  • the content of a user message will be summarized, if it's not after a finish action
  • refactor Agent's state.history to use the EventStream
    • Short Term History filters the event stream and adds summaries when necessary, to be inserted in the prompt
    • refactor state.history to be used as a list[Event] instead of list[tuple[Action, Observation]]
      • fairly necessary to start fixing things so that summaries don't create a third alternative history... 😅
    • refactor further to use event stream instead of storing copies

Quick overview of the memory component:
https://github.com/OpenDevin/OpenDevin/tree/enyst/memories-condenser/opendevin/memory

Not included in this PR:

  • [ ] if the agent needs original events content, it should be able to retrieve it
    • follow-up PR on memory backend
  • summarize delegate events
  • have some mechanism for keeping more events than system/initial/user (should_condense or include more in prompt messages that don't get summarized)

Note: this PR is CodeAct/Claude's playground. 😅

enyst and others added 4 commits May 23, 2024 18:00
opendevin: rework and doc according to review
mypy, algo fixes
…e (Claude)

Update CodeAct agent to use new memory condenser interface (Claude)

Fix docstring (Claude)

Fix docstring for condense method (Claude)

Fix __init__ docstring (Claude)

Revert "Implement lazy condensing, repeated condensing, and simplify interface (Claude)"

This reverts commit 216cb215ed67a553bd340028cc0525a8ecf56f86.

add SummaryObservation to init

fix stuff

Co-authored-by: OpenDevin <opendevin@opendevin.ai>
@enyst enyst marked this pull request as draft May 23, 2024 22:24
@enyst
Copy link
Collaborator Author

enyst commented May 26, 2024

Currently:

Old prompt:

Make a bash script that writes hello world

----------

Sure! Let's create a bash script that writes "Hello World".

First, I'll create a new file named `hello_world.sh`:
<execute_ipython>
create_file('hello_world.sh')
</execute_ipython>

----------

OBSERVATION:
---------------------------------------------------------------------------
FileExistsError                           Traceback (most recent call last)
Cell In[1], line 1
----> 1 create_file('hello_world.sh')

File /opendevin/plugins/agent_skills/agentskills.py:184, in create_file(filename)
    182 global CURRENT_FILE, CURRENT_LINE
    183 if os.path.exists(filename):
--> 184     raise FileExistsError(f"File '{filename}' already exists.")
    186 with open(filename, 'w') as file:
    187     file.write('\n')

FileExistsError: File 'hello_world.sh' already exists.

----------

It looks like the file `hello_world.sh` already exists. Let's open and edit it to include the "Hello World" script.

First, I'll open the file:
<execute_ipython>
open_file('hello_world.sh')
</execute_ipython>

----------

OBSERVATION:
[File: /workplace/hello_world.sh (2 lines total)]
1|#!/bin/bash
2|echo "Hello World"


----------

The file `hello_world.sh` already contains the script to write "Hello World". Let's run the script to verify it works correctly.
<execute_bash>
bash hello_world.sh
</execute_bash>

----------

OBSERVATION:
Hello World
[Command -1 finished with exit code 0]]

----------

The script executed successfully and printed "Hello World". If you have any further tasks or questions, feel free to ask!
(USER message here... etc)

What one condense does:

{
  "action": "SUMMARIZE",
  "actions": "IPythonRunCellAction, IPythonRunCellAction, CmdRunAction, MessageAction",
  "summary": "An attempt was made to create a file named 'hello_world.sh', but it already existed, so the file was opened, the 'Hello World' script was verified, and the script was successfully executed."
}

@enyst enyst force-pushed the enyst/memories-condenser branch from 481f449 to aa01bda Compare May 26, 2024 22:16
opendevin/events/stream.py Outdated Show resolved Hide resolved
@SmartManoj SmartManoj mentioned this pull request Jun 27, 2024
2 tasks
Copy link
Contributor

@neubig neubig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a comment to note that I'll review this again when it's ready. Please re-request review at that point.

@enyst enyst closed this Jul 27, 2024
@SmartManoj
Copy link
Contributor

Closing reason?

@enyst
Copy link
Collaborator Author

enyst commented Jul 28, 2024

Closed in favor of #2937 . 2937 re-implements this PR using memGPT prompts and some of its algo, and we're working to get it merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants