forked from OpenBMB/AgentVerse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Manager module in decision-making. [To-Do]: 1. optimizer parser…
… 2. Manager's prompt
- Loading branch information
1 parent
f06aab2
commit 5c010f5
Showing
10 changed files
with
243 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
from colorama import Fore | ||
|
||
from agentverse.logging import get_logger | ||
import bdb | ||
from string import Template | ||
from typing import TYPE_CHECKING, List, Tuple | ||
|
||
# from agentverse.environments import PipelineEnvironment | ||
from agentverse.message import Message | ||
|
||
from agentverse.agents import agent_registry | ||
from agentverse.agents.base import BaseAgent | ||
from agentverse.utils import AgentCriticism | ||
|
||
import random | ||
|
||
|
||
logger = get_logger() | ||
|
||
|
||
@agent_registry.register("manager") | ||
class ManagerAgent(BaseAgent): | ||
prompt_template: str | ||
|
||
def step( | ||
self, | ||
former_solution: str, | ||
critic_opinions: List[AgentCriticism], | ||
advice: str, | ||
task_description: str = "", | ||
previous_sentence: str = "", | ||
) -> Message: | ||
prompt = self._fill_prompt_template( | ||
former_solution, critic_opinions, advice, task_description, previous_sentence | ||
) | ||
|
||
logger.debug(f"Prompt:\n{prompt}", "Manager", Fore.CYAN) | ||
parsed_response = None | ||
for i in range(self.max_retry): | ||
try: | ||
# LLM Manager | ||
''' | ||
response = self.llm.generate_response(prompt) | ||
parsed_response = self.output_parser.parse(response) | ||
''' | ||
# Random Manager | ||
parsed_response = random.choice(critic_opinions) | ||
break | ||
except (KeyboardInterrupt, bdb.BdbQuit): | ||
raise | ||
except Exception as e: | ||
logger.error(e) | ||
logger.warn("Retrying...") | ||
continue | ||
return parsed_response | ||
|
||
|
||
async def astep(self, env_description: str = "") -> Message: | ||
"""Asynchronous version of step""" | ||
pass | ||
|
||
def _fill_prompt_template( | ||
self, | ||
former_solution: str, | ||
critic_opinions: List[AgentCriticism], | ||
advice: str, | ||
task_description: str, | ||
previous_sentence: str, | ||
) -> str: | ||
"""Fill the placeholders in the prompt template | ||
In the role_assigner agent, three placeholders are supported: | ||
- ${task_description} | ||
- ${former_solution} | ||
- ${critic_messages} | ||
- ${advice} | ||
- ${previous_sentence} | ||
""" | ||
input_arguments = { | ||
"task_description": task_description, | ||
"former_solution": former_solution, | ||
"previous_sentence": previous_sentence, | ||
"critic_opinions": "\n".join( | ||
[ | ||
f"{critic.sender_agent.role_description} said: {critic.criticism}" | ||
for critic in critic_opinions | ||
] | ||
), | ||
"advice": advice, | ||
} | ||
|
||
|
||
# manger select the proper sentence | ||
template = Template(self.prompt_template) | ||
return template.safe_substitute(input_arguments) | ||
|
||
def add_message_to_memory(self, messages: List[Message]) -> None: | ||
self.memory.add_message(messages) | ||
|
||
def reset(self) -> None: | ||
"""Reset the agent""" | ||
self.memory.reset() | ||
# TODO: reset receiver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters