-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbonus_agent.py
69 lines (53 loc) · 2.21 KB
/
bonus_agent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from typing import List, Tuple, Dict, Any
from simulation_engine.gpt_structure import gpt_request_messages
from cs222_assignment_1_bonus.environment import BakingEnvironment
class Agent:
def __init__(self, name: str, description: str):
self.name, self.description = name, description
self.message_history = []
self.env = BakingEnvironment(self)
def perceive(self) -> None:
env_description = f"""
Current baking environment:
Available ingredients:
{', '.join(self.env.ingredients.keys())}
Ingredients added:
{', '.join(ing for ing, data in self.env.ingredients.items() if data['current'] > 0)}
Available tools:
{', '.join(self.env.tools.keys())}
Tools in use:
{', '.join(tool for tool, data in self.env.tools.items() if data['used'])}"""
self.message_history.append({
"role": "user",
"content": env_description
})
def retrieve(self) -> str:
"""
TODO
In the agent's memories, there is a recipe for a cake buried amongst
information about other topics.
Come up with a way to retrieve the relevant text from the agent's memory,
without modifying the text file and minimizing the number of irrelevant
information retrieved.
Return the retrieved recipe (ingredients and steps) as a string.
"""
memories = open(f"cs222_assignment_1_bonus/{self.name}/memory/cake.txt", 'r').read().split("\n\n")
return ""
def act(self) -> str:
persona = f"""
You are {self.name}. {self.description} You are baking a cake right now.
You remember the recipe for the cake:
{self.retrieve()}
Speak in character, no asterisks. Take only one action at a time.
"""
response = gpt_request_messages(
messages=[{"role": "system", "content": persona}] + self.message_history)
return response
def reflect(self, response: str) -> None:
self.message_history.append({"role": "assistant", "content": response})
def baking_step(self) -> Tuple[str, List[Dict[str, Any]], List[Dict[str, Any]], List[str]]:
self.perceive()
action = self.act()
self.reflect(action)
attempted, executed, feedbacks = self.env.process_action(action)
return action, attempted, executed, feedbacks