diff --git a/gentopia/agent/base_agent.py b/gentopia/agent/base_agent.py index 2f9cacc..f94b68d 100644 --- a/gentopia/agent/base_agent.py +++ b/gentopia/agent/base_agent.py @@ -91,3 +91,9 @@ def _format_function_map(self) -> Dict[str, Callable]: else: function_map[plugin.name] = plugin.run return function_map + + def clear(self): + """ + Clear and reset the agent. + """ + pass diff --git a/gentopia/agent/openai/agent.py b/gentopia/agent/openai/agent.py index 5840db4..0171079 100644 --- a/gentopia/agent/openai/agent.py +++ b/gentopia/agent/openai/agent.py @@ -123,6 +123,7 @@ def run(self, instruction: str, output: Optional[BaseOutput] = None) -> AgentOut :param output: Output manager object to be used, defaults to None. :type output: Optional[BaseOutput], optional """ + self.clear() if output is None: output = BaseOutput() self.message_scratchpad.append({"role": "user", "content": instruction}) @@ -209,3 +210,6 @@ def stream(self, instruction: Optional[str] = None, output: Optional[BaseOutput] # else: # self.message_scratchpad.append({"role": "user", "content": "Summarize what you have done and continue if you have not finished."}) # self.stream(output=output) + + def clear(self): + self.message_scratchpad = self.message_scratchpad[:1] \ No newline at end of file diff --git a/gentopia/agent/openai_memory/agent.py b/gentopia/agent/openai_memory/agent.py index c21e301..8cf9a07 100644 --- a/gentopia/agent/openai_memory/agent.py +++ b/gentopia/agent/openai_memory/agent.py @@ -126,6 +126,7 @@ def run(self, instruction: str, output: Optional[BaseOutput] = None) -> AgentOut :return: Output of the agent. :rtype: AgentOutput """ + self.clear() if output is None: output = BaseOutput() self.memory.clear_memory_II() diff --git a/gentopia/agent/react/agent.py b/gentopia/agent/react/agent.py index b6d921c..af2f0e6 100644 --- a/gentopia/agent/react/agent.py +++ b/gentopia/agent/react/agent.py @@ -148,7 +148,7 @@ def run(self, instruction, max_iterations=10): :return: AgentOutput object. :rtype: AgentOutput """ - self.intermediate_steps.clear() + self.clear() logging.info(f"Running {self.name + ':' + self.version} with instruction: {instruction}") total_cost = 0.0 total_token = 0 @@ -192,7 +192,7 @@ def stream(self, instruction: Optional[str] = None, output: Optional[BaseOutput] :return: AgentOutput object. :rtype: AgentOutput """ - self.intermediate_steps.clear() + self.clear() total_cost = 0.0 total_token = 0 if output is None: @@ -229,3 +229,9 @@ def stream(self, instruction: Optional[str] = None, output: Optional[BaseOutput] output.panel_print(result, f"[green] Function Response of [blue]{action}: ") self.intermediate_steps[-1].append(result) return AgentOutput(output=content, cost=total_cost, token_usage=total_token) + + def clear(self): + """ + Clear and reset the agent. + """ + self.intermediate_steps.clear() \ No newline at end of file