mirror of
https://github.com/OpenHands/OpenHands.git
synced 2025-12-26 05:48:36 +08:00
Message property sent to llm (#1273)
* Add to_memory for observations * Don't send 'message' property to the llm from observation; fix other agents
This commit is contained in:
parent
f55f7be2e6
commit
39a851bd95
@ -38,7 +38,7 @@ class SWEAgent(Agent):
|
||||
|
||||
def _remember(self, action: Action, observation: Observation) -> None:
|
||||
"""Agent has a limited memory of the few steps implemented as a queue"""
|
||||
memory = MEMORY_FORMAT(action.to_dict(), observation.to_dict())
|
||||
memory = MEMORY_FORMAT(action.to_memory(), observation.to_memory())
|
||||
self.running_memory.append(memory)
|
||||
|
||||
def _think_act(self, messages: List[dict]) -> tuple[Action, str]:
|
||||
|
||||
@ -162,7 +162,7 @@ class MonologueAgent(Agent):
|
||||
observation = BrowserOutputObservation(
|
||||
content=thought, url='', screenshot=''
|
||||
)
|
||||
self._add_event(observation.to_dict())
|
||||
self._add_event(observation.to_memory())
|
||||
output_type = ''
|
||||
else:
|
||||
action: Action = NullAction()
|
||||
@ -205,7 +205,7 @@ class MonologueAgent(Agent):
|
||||
self._initialize(state.plan.main_goal)
|
||||
for prev_action, obs in state.updated_info:
|
||||
self._add_event(prev_action.to_memory())
|
||||
self._add_event(obs.to_dict())
|
||||
self._add_event(obs.to_memory())
|
||||
|
||||
state.updated_info = []
|
||||
|
||||
|
||||
@ -171,7 +171,7 @@ def get_prompt(plan: Plan, history: List[Tuple[Action, Observation]]) -> str:
|
||||
history_dicts.append(action.to_memory())
|
||||
latest_action = action
|
||||
if not isinstance(observation, NullObservation):
|
||||
observation_dict = observation.to_dict()
|
||||
observation_dict = observation.to_memory()
|
||||
if (
|
||||
'extras' in observation_dict
|
||||
and 'screenshot' in observation_dict['extras']
|
||||
|
||||
@ -15,6 +15,12 @@ class Observation:
|
||||
return self.content
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
"""Converts the observation to a dictionary and adds user message."""
|
||||
memory_dict = self.to_memory()
|
||||
memory_dict['message'] = self.message
|
||||
return memory_dict
|
||||
|
||||
def to_memory(self) -> dict:
|
||||
"""Converts the observation to a dictionary."""
|
||||
extras = copy.deepcopy(self.__dict__)
|
||||
content = extras.pop('content', '')
|
||||
@ -23,7 +29,6 @@ class Observation:
|
||||
'observation': observation,
|
||||
'content': content,
|
||||
'extras': extras,
|
||||
'message': self.message,
|
||||
}
|
||||
|
||||
@property
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import pytest
|
||||
from opendevin.observation import observation_from_dict, Observation, CmdOutputObservation
|
||||
|
||||
|
||||
def test_observation_serialization_deserialization():
|
||||
original_observation_dict = {
|
||||
'observation': 'run',
|
||||
@ -12,6 +12,9 @@ def test_observation_serialization_deserialization():
|
||||
assert isinstance(observation_instance, Observation), 'The observation instance should be an instance of Action.'
|
||||
assert isinstance(observation_instance, CmdOutputObservation), 'The observation instance should be an instance of AgentThinkAction.'
|
||||
serialized_observation_dict = observation_instance.to_dict()
|
||||
serialized_observation_memory = observation_instance.to_memory()
|
||||
assert serialized_observation_dict == original_observation_dict, 'The serialized observation should match the original observation dict.'
|
||||
original_observation_dict.pop('message')
|
||||
assert serialized_observation_memory == original_observation_dict, 'The serialized observation in memory should match the original observation dict.'
|
||||
|
||||
# Additional tests for various observation subclasses can be included here
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user