Skip to content

Commit 14606ed

Browse files
committed
Refactored memory challenge a
Signed-off-by: Merwane Hamadi <[email protected]>
1 parent f1ee81e commit 14606ed

File tree

7 files changed

+47
-231
lines changed

7 files changed

+47
-231
lines changed

docs/challenges/memory/challenge_d.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
**Command to try**:
66
```
7-
pytest -s tests/integration/challenges/memory/test_memory_challenge_d.py --level=1
7+
pytest -s tests/challenges/memory/test_memory_challenge_d.py --level=1
88
``
99
1010
## Description

plugins_config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

tests/challenges/current_score.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
"memory_challenge_c": {
4444
"max_level": 5,
4545
"max_level_beaten": 1
46+
},
47+
"memory_challenge_d": {
48+
"max_level": 5,
49+
"max_level_beaten": null
4650
}
4751
}
4852
}

tests/challenges/memory/test_memory_challenge_d.py

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,56 @@
11
import json
2+
from typing import Dict
23

34
import pytest
5+
from pytest_mock import MockerFixture
46

57
from autogpt.agent import Agent
68
from autogpt.commands.file_operations import read_file, write_to_file
7-
from tests.integration.agent_utils import run_interaction_loop
8-
from tests.integration.challenges.utils import get_level_to_run
9+
from autogpt.config import Config
10+
from tests.challenges.challenge_decorator.challenge_decorator import challenge
11+
from tests.challenges.utils import get_workspace_path, run_interaction_loop
912
from tests.utils import requires_api_key
1013

1114
LEVEL_CURRENTLY_BEATEN = 1
1215
MAX_LEVEL = 5
16+
OUTPUT_LOCATION = "output.txt"
1317

1418

15-
@pytest.mark.vcr
19+
# @pytest.mark.vcr
1620
@requires_api_key("OPENAI_API_KEY")
21+
@challenge
1722
def test_memory_challenge_d(
18-
memory_management_agent: Agent, user_selected_level: int
23+
memory_management_agent: Agent,
24+
patched_api_requestor: MockerFixture,
25+
monkeypatch: pytest.MonkeyPatch,
26+
config: Config,
27+
level_to_run: int,
1928
) -> None:
2029
"""
2130
The agent is given a series of events and must remember the respective beliefs of the characters.
2231
Args:
2332
memory_management_agent (Agent)
2433
user_selected_level (int)
2534
"""
26-
current_level = get_level_to_run(
27-
user_selected_level, LEVEL_CURRENTLY_BEATEN, MAX_LEVEL
28-
)
2935
sally_anne_test_phrases = [
3036
"Sally has a marble (marble A) and she puts it in her basket (basket S), then leaves the room. Anne moves marble A from Sally's basket (basket S) to her own basket (basket A).",
31-
"Sally gives a new marble (marble B) to Bob who is outside with her. Bob goes into the room and places marble B into Anne's basket (basket A). Anne tells Bob to tell Sally that he lost the marble b. Bob leaves the room and speaks to Sally about the marble B. Meanwhile, after Bob left the room, Anne moves marble A into the green box, but tells Charlie to tell Sally that marble A is under the sofa. Charlie leaves the room and speak to Sally about the marble A as instructed by Anne.",
37+
"Sally gives a new marble (marble B) to Bob who is outside with her. Bob goes into the room and places marble B into Anne's basket (basket A). Anne tells Bob to tell Sally that he lost the marble b. Bob leaves the room and speaks to Sally about the marble B. Meanwhile, after Bob left the room, Anne moves marble A into the green box, but tells Charlie to tell Sally that marble A is under the sofa. Charlie leaves the room and speaks to Sally about the marble A as instructed by Anne.",
3238
"Sally gives a new marble (marble C) to Charlie who is outside with her. Charlie enters the room and exchanges marble C with marble B in Anne's basket (basket A). Anne tells Charlie to tell Sally that he put marble C into the red box. Charlie leaves the room and speak to Sally about marble C as instructed by Anne. Meanwhile, after Charlie leaves the room, Bob enters into the room and moves marble A from the green box to under the sofa, but tells Anne to tell Sally that marble A is in the green box. Anne leaves the room and speak to Sally about the marble A as instructed by Bob",
3339
"Sally gives a new marble (marble D) to Anne. Anne gives the marble to Charlie. Charlie enters the room and gives marble D to Bob. Bob tells Charlie to tell Sally that he put marble D under the sofa. Bob put marble D under the sofa Charlie leaves the room and speaks to Sally about marble D. Meanwhile, after Charlie leaves the room, Bob takes marble A from under the sofa and places it in the blue box.",
3440
"Sally gives a new marble (marble E) to Charlie who is outside with her. Charlie enters the room and places marble E in the red box. Anne, who is already in the room, takes marble E from the red box, and hides it under the sofa. Then Anne leaves the room and tells Sally that marble E is in the green box. Meanwhile, after Anne leaves the room, Charlie who re-enters the room takes marble D from under the sofa and places it in his own basket (basket C).",
3541
]
36-
level_sally_anne_test_phrases = sally_anne_test_phrases[:current_level]
42+
level_sally_anne_test_phrases = sally_anne_test_phrases[:level_to_run]
3743
create_instructions_files(
38-
memory_management_agent, current_level, level_sally_anne_test_phrases
44+
memory_management_agent, level_to_run, level_sally_anne_test_phrases, config
3945
)
40-
try:
41-
run_interaction_loop(memory_management_agent, 90)
42-
except SystemExit:
43-
file_path = str(memory_management_agent.workspace.get_path("output.txt"))
44-
content = read_file(file_path)
45-
check_beliefs(content, current_level)
46+
run_interaction_loop(monkeypatch, memory_management_agent, level_to_run + 2)
47+
file_path = get_workspace_path(memory_management_agent, OUTPUT_LOCATION)
4648

49+
content = read_file(file_path, config)
50+
check_beliefs(content, level_to_run)
4751

48-
def check_beliefs(content, level):
52+
53+
def check_beliefs(content: str, level: int) -> None:
4954
# Define the expected beliefs for each level
5055
expected_beliefs = {
5156
1: {
@@ -59,16 +64,17 @@ def check_beliefs(content, level):
5964
2: {
6065
"Sally": {
6166
"marble A": "sofa", # Because Charlie told her
67+
"marble B": "lost", # Because Bob told her
6268
},
6369
"Anne": {
6470
"marble A": "green box", # Because she moved it there
6571
"marble B": "basket A", # Because Bob put it there and she was in the room
6672
},
6773
"Bob": {
68-
"B": "basket A", # Last place he put it
74+
"marble B": "basket A", # Last place he put it
6975
},
7076
"Charlie": {
71-
"A": "sofa", # Because Anne told him to tell Sally so
77+
"marble A": "sofa", # Because Anne told him to tell Sally so
7278
},
7379
},
7480
3: {
@@ -147,16 +153,19 @@ def check_beliefs(content, level):
147153
},
148154
},
149155
}
156+
150157
# Extract the beliefs from the AI's response
151158
ai_beliefs = extract_beliefs(content)
152159
# Check the AI's beliefs against the expected beliefs
153160
for character, belief in expected_beliefs[level].items():
154-
assert (
155-
ai_beliefs.get(character) == belief
156-
), f"For {character}, expected '{belief}' but got '{ai_beliefs.get(character)}'"
161+
for marble, location in belief.items():
162+
ai_belief = ai_beliefs.get(character, {}).get(marble, "")
163+
assert (
164+
location in ai_belief
165+
), f"For {character}'s {marble}, expected '{location}' to be in '{ai_belief}'"
157166

158167

159-
def extract_beliefs(content):
168+
def extract_beliefs(content: str) -> Dict[str, Dict[str, str]]:
160169
"""Extract the beliefs of each character from the AI's output."""
161170
# Parse the JSON content
162171
content_dict = json.loads(content)
@@ -168,6 +177,7 @@ def create_instructions_files(
168177
memory_management_agent: Agent,
169178
level: int,
170179
test_phrases: list,
180+
config: Config,
171181
base_filename: str = "instructions_",
172182
) -> None:
173183
"""
@@ -176,13 +186,14 @@ def create_instructions_files(
176186
level:
177187
memory_management_agent (Agent)
178188
test_phrases (list)
189+
config (Config)
179190
base_filename (str, optional)
180191
"""
181192
for i in range(1, level + 1):
182193
content = generate_content(i, test_phrases, base_filename, level)
183194
file_name = f"{base_filename}{i}.txt"
184-
file_path = str(memory_management_agent.workspace.get_path(file_name))
185-
write_to_file(file_path, content)
195+
file_path = get_workspace_path(memory_management_agent, file_name)
196+
write_to_file(file_path, content, config)
186197

187198

188199
def generate_content(
@@ -208,9 +219,9 @@ def generate_content(
208219
2. Everyone outside the room do not see and do not know the actions of the people inside the room.
209220
3. Only write about the marbles that are present in the level
210221
4. Marbles names are marble A, marble B, marble C, marble D, ...
211-
5. Believed_location_of_the_specific_marble describes where is the marble like drawer, basket S, sofa, ... Also, refer to the location by the name of the object (like 'sofa', 'drawer', etc.), not by its relative position (like 'under the sofa', 'in the drawer', etc.)
222+
5. Believed_location_of_the_specific_marble describes where is the marble like drawer, basket S, sofa, ...
212223
6. Do not use expression like <Anne's basket> use <basket A> instead.
213-
7. Do not use expression like <under the sofa> use <sofa> instead.
224+
7. All characters believe what they're being told by other characters.
214225
Instructions:
215226
I) Write the following information in the file output.txt in JSON format:
216227
1. The respective beliefs of the characters (which means where every marble is according to character x, y, z. Character x should say where it believes every marble it is aware exist is)

tests/integration/agent_factory.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,7 @@ def file_system_agents(
107107

108108
@pytest.fixture
109109
def memory_management_agent(agent_test_config, memory_json_file, workspace: Workspace):
110-
command_registry = CommandRegistry()
111-
command_registry.import_commands("autogpt.commands.file_operations")
112-
command_registry.import_commands("autogpt.app")
113-
command_registry.import_commands("autogpt.commands.task_statuses")
110+
command_registry = get_command_registry(agent_test_config)
114111

115112
ai_config = AIConfig(
116113
ai_name="Follow-Instructions-GPT",
@@ -125,7 +122,7 @@ def memory_management_agent(agent_test_config, memory_json_file, workspace: Work
125122
system_prompt = ai_config.construct_full_prompt()
126123

127124
agent = Agent(
128-
ai_name="",
125+
ai_name="Follow-Instructions-GPT",
129126
memory=memory_json_file,
130127
command_registry=command_registry,
131128
config=ai_config,

tests/integration/challenges/memory/cassettes/test_memory_challenge_d/test_memory_challenge_d.yaml

Lines changed: 0 additions & 197 deletions
This file was deleted.

0 commit comments

Comments
 (0)