Skip to content

Commit 76abca3

Browse files
assertionaaren.xzh
andauthored
feat: simplify state.history with to_memory call in micro-agent. Or the call to LLM may exceed the token limit. (All-Hands-AI#1806)
* feat: simplify state.history with to_memory call in micro-agent. * feat: merge master and replace to_memory with event_to_memory. --------- Co-authored-by: aaren.xzh <[email protected]>
1 parent bf14b47 commit 76abca3

File tree

10 files changed

+23
-9
lines changed

10 files changed

+23
-9
lines changed

agenthub/micro/agent.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from opendevin.core.utils import json
66
from opendevin.events.action import Action
77
from opendevin.events.serialization.action import action_from_dict
8+
from opendevin.events.serialization.event import event_to_memory
89
from opendevin.llm.llm import LLM
910

1011
from .instructions import instructions
@@ -26,6 +27,18 @@ def to_json(obj, **kwargs):
2627
return json.dumps(obj, **kwargs)
2728

2829

30+
def history_to_json(obj, **kwargs):
31+
"""
32+
Serialize and simplify history to str format
33+
"""
34+
if isinstance(obj, list):
35+
# process history, make it simpler.
36+
processed_history = []
37+
for action, observation in obj:
38+
processed_history.append((event_to_memory(action), event_to_memory(observation)))
39+
return json.dumps(processed_history, **kwargs)
40+
41+
2942
class MicroAgent(Agent):
3043
prompt = ''
3144
agent_definition: dict = {}
@@ -44,6 +57,7 @@ def step(self, state: State) -> Action:
4457
state=state,
4558
instructions=instructions,
4659
to_json=to_json,
60+
history_to_json=history_to_json,
4761
delegates=self.delegates,
4862
latest_user_message=latest_user_message,
4963
)

agenthub/micro/coder/prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Do NOT finish until you have completed the tasks.
2121

2222
## History
2323
{{ instructions.history_truncated }}
24-
{{ to_json(state.history[-10:]) }}
24+
{{ history_to_json(state.history[-10:]) }}
2525

2626
## Format
2727
{{ instructions.format.action }}

agenthub/micro/commit_writer/prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ the `reject` action with `outputs.answer` set to the reason.
1818

1919
## History
2020
{{ instructions.history_truncated }}
21-
{{ to_json(state.history[-10:]) }}
21+
{{ history_to_json(state.history[-10:]) }}
2222

2323
If the last item in the history is an error, you should try to fix it.
2424

agenthub/micro/manager/prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ provide the correct inputs for the delegate you select.
1717

1818
## History
1919
{{ instructions.history_truncated }}
20-
{{ to_json(state.history[-10:]) }}
20+
{{ history_to_json(state.history[-10:]) }}
2121

2222
## Available Actions
2323
{{ instructions.actions.delegate }}

agenthub/micro/math_agent/prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ and call the `finish` action with `outputs.answer` set to the answer.
1010

1111
## History
1212
{{ instructions.history_truncated }}
13-
{{ to_json(state.history[-10:]) }}
13+
{{ history_to_json(state.history[-10:]) }}
1414

1515
If the last item in the history is an error, you should try to fix it.
1616

agenthub/micro/postgres_agent/prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You may take any of the following actions:
1818

1919
## History
2020
{{ instructions.history_truncated }}
21-
{{ to_json(state.history[-10:]) }}
21+
{{ history_to_json(state.history[-10:]) }}
2222

2323
## Format
2424
{{ instructions.format.action }}

agenthub/micro/repo_explorer/prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ When you're done, put your summary into the output of the `finish` action.
2020

2121
## History
2222
{{ instructions.history_truncated }}
23-
{{ to_json(state.history[-10:]) }}
23+
{{ history_to_json(state.history[-10:]) }}
2424

2525
## Format
2626
{{ instructions.format.action }}

agenthub/micro/study_repo_for_task/prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ When you're done, put your summary in `outputs.summary` in the `finish` action.
1919

2020
## History
2121
{{ instructions.history_truncated }}
22-
{{ to_json(state.history[-10:]) }}
22+
{{ history_to_json(state.history[-10:]) }}
2323

2424
## Format
2525
{{ instructions.format.action }}

agenthub/micro/typo_fixer_agent/prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Do NOT finish until you have fixed all the typos and generated a summary.
2323

2424
## History
2525
{{ instructions.history_truncated }}
26-
{{ to_json(state.history[-5:]) }}
26+
{{ history_to_json(state.history[-5:]) }}
2727

2828
## Format
2929
{{ instructions.format.action }}

agenthub/micro/verifier/prompt.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ explaining what the problem is.
2121

2222
## History
2323
{{ instructions.history_truncated }}
24-
{{ to_json(state.history[-10:]) }}
24+
{{ history_to_json(state.history[-10:]) }}
2525

2626
## Format
2727
{{ instructions.format.action }}

0 commit comments

Comments
 (0)