File size: 8,044 Bytes
01523b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
from __future__ import annotations

"""
An agent based upon Observation-Planning-Reflection architecture.
"""

from logging import getLogger

from abc import abstractmethod
from typing import List, Set, Union, NamedTuple, TYPE_CHECKING

from pydantic import BaseModel, Field, validator

from agentverse.llms import BaseLLM
from agentverse.memory import BaseMemory, ChatHistoryMemory
from agentverse.message import Message
from agentverse.output_parser import OutputParser

from agentverse.message import Message
from agentverse.agents.base import BaseAgent

from datetime import datetime as dt
import datetime

#from . import agent_registry
from string import Template

from agentverse.agents import agent_registry
from agentverse.agents.base import BaseAgent

logger = getLogger(__file__)

if TYPE_CHECKING:
    from agentverse.environments.base import BaseEnvironment


@agent_registry.register("reflection")
class ReflectionAgent(BaseAgent):
    async_mode: bool = (True,)
    current_time: str = (None,)
    environment: BaseEnvironment = None
    step_cnt: int = 0

    manipulated_memory: str = Field(
        default="", description="one fragment used in prompt construction"
    )

    @validator("current_time")
    def convert_str_to_dt(cls, current_time):
        if not isinstance(current_time, str):
            raise ValueError("current_time should be str")
        return dt.strptime(current_time, "%Y-%m-%d %H:%M:%S")

    def step(self, current_time: dt, env_description: str = "") -> Message:
        """
        Call this method at each time frame
        """
        self.current_time = current_time

        self.manipulated_memory = self.memory_manipulator.manipulate_memory()

        prompt = self._fill_prompt_template(env_description)

        parsed_response, reaction, target = None, None, None
        for i in range(self.max_retry):
            try:
                response = self.llm.agenerate_response(prompt)
                parsed_response = self.output_parser.parse(response)

                if "say(" in parsed_response.return_values["output"]:
                    reaction, target = eval(
                        "self._" + parsed_response.return_values["output"].strip()
                    )
                elif "act(" in parsed_response.return_values["output"]:
                    reaction, target = eval(
                        "self._" + parsed_response.return_values["output"].strip()
                    )
                elif "do_nothing(" in parsed_response.return_values["output"]:
                    reaction, target = None, None
                else:
                    raise Exception(
                        f"no valid parsed_response detected, "
                        f"cur response {parsed_response.return_values['output']}"
                    )
                break

            except Exception as e:
                logger.error(e)
                logger.warn("Retrying...")
                continue

        if parsed_response is None:
            logger.error(f"{self.name} failed to generate valid response.")

        if reaction is None:
            reaction = "Keep doing last action ..."

        message = Message(
            content="" if reaction is None else reaction,
            sender=self.name,
            receiver=self.get_receiver()
            if target is None
            else self.get_valid_receiver(target),
        )

        self.step_cnt += 1

        return message

    async def astep(self, current_time: dt, env_description: str = "") -> Message:
        """Asynchronous version of step"""
        # use environment's time to update agent's time
        self.current_time = current_time
        # Before the agent step, we check current status,
        # TODO add this func after
        # self.check_status_passive()

        self.manipulated_memory = self.memory_manipulator.manipulate_memory()

        prompt = self._fill_prompt_template(env_description)

        parsed_response, reaction, target = None, None, None
        for i in range(self.max_retry):
            try:
                response = await self.llm.agenerate_response(prompt)
                parsed_response = self.output_parser.parse(response)

                if "say(" in parsed_response.return_values["output"]:
                    reaction, target = eval(
                        "self._" + parsed_response.return_values["output"].strip()
                    )
                elif "act(" in parsed_response.return_values["output"]:
                    reaction, target = eval(
                        "self._" + parsed_response.return_values["output"].strip()
                    )
                elif "do_nothing(" in parsed_response.return_values["output"]:
                    reaction, target = None, None
                else:
                    raise Exception(
                        f"no valid parsed_response detected, "
                        f"cur response {parsed_response.return_values['output']}"
                    )

                break

            except Exception as e:
                logger.error(e)
                logger.warn("Retrying...")
                continue

        if parsed_response is None:
            logger.error(f"{self.name} failed to generate valid response.")

        if reaction is None:
            reaction = "Keep doing last action ..."

        message = Message(
            content="" if reaction is None else reaction,
            sender=self.name,
            receiver=self.get_receiver()
            if target is None
            else self.get_valid_receiver(target),
        )

        self.step_cnt += 1

        return message

    def _act(self, description=None, target=None):
        if description is None:
            return ""
        if target is None:
            reaction_content = f"{self.name} performs action: '{description}'."
        else:
            reaction_content = (
                f"{self.name} performs action to {target}: '{description}'."
            )
        # self.environment.broadcast_observations(self, target, reaction_content)
        return reaction_content, target

    def _say(self, description, target=None):
        if description is None:
            return ""
        if target is None:
            reaction_content = f"{self.name} says: '{description}'."
        else:
            reaction_content = f"{self.name} says to {target}: '{description}'."
        # self.environment.broadcast_observations(self, target, reaction_content)
        return reaction_content, target

    def get_valid_receiver(self, target: str) -> set():
        all_agents_name = []
        for agent in self.environment.agents:
            all_agents_name.append(agent.name)

        if not (target in all_agents_name):
            return {"all"}
        else:
            return {target}

    def _fill_prompt_template(self, env_description: str = "") -> str:
        """Fill the placeholders in the prompt template

        In the conversation agent, three placeholders are supported:
        - ${agent_name}: the name of the agent
        - ${env_description}: the description of the environment
        - ${role_description}: the description of the role of the agent
        - ${chat_history}: the chat history of the agent
        """
        input_arguments = {
            "agent_name": self.name,
            "role_description": self.role_description,
            "chat_history": self.memory.to_string(add_sender_prefix=True),
            "current_time": self.current_time,
            "env_description": env_description,
        }
        return Template(self.prompt_template).safe_substitute(input_arguments)

    def add_message_to_memory(self, messages: List[Message]) -> None:
        self.memory.add_message(messages)

    def reset(self, environment: BaseEnvironment) -> None:
        """Reset the agent"""
        self.environment = environment
        self.memory.reset()
        self.memory_manipulator.agent = self
        self.memory_manipulator.memory = self.memory