Skip to content

Commit

Permalink
modify the config of mgsm and logic_grid. add vertical_solver_first s…
Browse files Browse the repository at this point in the history
…tructure
  • Loading branch information
ubuntu committed Sep 6, 2023
1 parent eb36831 commit ee8ced1
Show file tree
Hide file tree
Showing 27 changed files with 2,505 additions and 46 deletions.
4 changes: 3 additions & 1 deletion agentverse/agents/pipeline/critic.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

@agent_registry.register("critic")
class CriticAgent(BaseAgent):
max_history: int = 3

def step(self, env_description: str = "") -> CriticMessage:
pass

Expand All @@ -35,7 +37,7 @@ async def astep(
task_description=task_description,
role_description=self.role_description,
)
history = self.memory.to_messages(self.name)
history = self.memory.to_messages(self.name, start_index=-self.max_history)
parsed_response: Union[AgentCriticism, None] = None
for i in range(self.max_retry):
try:
Expand Down
5 changes: 4 additions & 1 deletion agentverse/agents/pipeline/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

@agent_registry.register("solver")
class SolverAgent(BaseAgent):
max_history: int = 3

def step(
self,
former_solution: str,
Expand All @@ -35,8 +37,9 @@ def step(
former_solution=former_solution,
task_description=task_description,
advice=advice,
role_description=self.role_description,
)
history = self.memory.to_messages(self.name) # Critic Opinions
history = self.memory.to_messages(self.name, start_index=-self.max_history)
parsed_response = None
for i in range(self.max_retry):
try:
Expand Down
3 changes: 2 additions & 1 deletion agentverse/agentversepipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def run(
"""
self.environment.reset()
self.logs = []
advice = "No advice yet."
previous_plan = "No solution yet."
while not self.environment.is_done():
Expand All @@ -97,7 +98,7 @@ def run(
)
self.logs += logs
self.save_result(previous_plan, single_agent)
return previous_plan, result, logs
return previous_plan, result, self.logs

'''
def iter_run(
Expand Down
2 changes: 2 additions & 0 deletions agentverse/environments/decision_maker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@
from .horizontal import HorizontalDecisionMaker
from .vertical import VerticalDecisionMaker
from .dynamic import DynamicDecisionMaker
from .vertical_solver_first import VerticalSolverFirstDecisionMaker
from .concurrent import ConcurrentDecisionMaker
87 changes: 87 additions & 0 deletions agentverse/environments/decision_maker/vertical_solver_first.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from __future__ import annotations
import asyncio
from colorama import Fore

from typing import TYPE_CHECKING, List

from . import decision_maker_registry
from .base import BaseDecisionMaker
from agentverse.logging import typewriter_log, logger
from agentverse.message import Message

if TYPE_CHECKING:
from agentverse.agents import BaseAgent, SolverAgent, CriticAgent
from agentverse.message import CriticMessage, SolverMessage


@decision_maker_registry.register("vertical-solver-first")
class VerticalSolverFirstDecisionMaker(BaseDecisionMaker):
"""
Discuss in a vertical manner.
"""

name: str = "vertical"
max_inner_turns: int = 3

async def astep(
self,
agents: List[BaseAgent],
task_description: str,
previous_plan: str = "No solution yet.",
advice: str = "No advice yet.",
*args,
**kwargs,
) -> List[SolverMessage]:
# Here we assume that the first agent is the solver.
# The rest of the agents are the reviewers.
if advice != "No advice yet.":
self.broadcast_messages(
agents, [Message(content=advice, sender="Evaluator")]
)
previous_plan = agents[0].step(previous_plan, advice, task_description)
self.broadcast_messages(agents, [previous_plan])
logger.info("", f"Initial Plan:\n{previous_plan.content}", Fore.BLUE)
for i in range(self.max_inner_turns):
reviews: List[CriticMessage] = await asyncio.gather(
*[
agent.astep(previous_plan, advice, task_description)
for agent in agents[1:]
]
)
logger.info(
"",
"Reviews:\n"
+ "\n".join(
[f"[{review.sender}]: {review.content}" for review in reviews]
),
Fore.YELLOW,
)

nonempty_reviews = []
for review in reviews:
if not review.is_agree and review.content != "":
nonempty_reviews.append(review)
if len(nonempty_reviews) == 0:
logger.info("", "Consensus Reached!.", Fore.GREEN)
break
self.broadcast_messages(agents, nonempty_reviews)
previous_plan = agents[0].step(previous_plan, advice, task_description)
logger.info("", f"Updated Plan:\n{previous_plan.content}", Fore.BLUE)
self.broadcast_messages(agents, [previous_plan])
result = previous_plan
return [result]

def broadcast_messages(self, agents, messages) -> None:
for agent in agents:
agent.add_message_to_memory(messages)

def p2p_messages(self, agents, messages) -> None:
agents[0].add_message_to_memory(messages)
for message in messages:
for agent in agents[1:]:
if agent.name == message.sender:
agent.add_message_to_memory(messages)
break

def reset(self):
pass
12 changes: 9 additions & 3 deletions agentverse/environments/executor/code_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ def execute_command(command: str) -> str:

@executor_registry.register("code-test")
class CodeTestExecutor(BaseExecutor):
has_test: dict = {}

def step(
self,
agent: ExecutorAgent,
Expand All @@ -27,9 +29,13 @@ def step(
) -> Any:
os.makedirs("tmp", exist_ok=True)
self.write_to_file("tmp/main.py", solution)
response = agent.step(task_description, solution).content
self.write_to_file(response["file_path"], response["code"])
result = execute_command(f"python {response['file_path']}")
if task_description not in self.has_test:
response = agent.step(task_description, solution).content
self.write_to_file(response["file_path"], response["code"])
self.has_test[task_description] = f"python {response['file_path']}"
result = execute_command(f"python {response['file_path']}")
else:
result = execute_command(self.has_test[task_description])
return result

def write_to_file(self, file_name, file_content):
Expand Down
5 changes: 3 additions & 2 deletions agentverse/initialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
from typing import Dict, List, TYPE_CHECKING

import yaml
'''

"""
try:
from bmtools.agent.singletool import import_all_apis, load_single_tools
except:
print("BMTools is not installed, tools cannot be used. To install BMTools, \
please follow the instruction in the README.md file.")
'''
"""

from agentverse.llms import llm_registry

Expand Down
4 changes: 2 additions & 2 deletions agentverse/memory/chat_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def to_string(self, add_sender_prefix: bool = False) -> str:
else:
return "\n".join([message.content for message in self.messages])

def to_messages(self, my_name: str = ""):
def to_messages(self, my_name: str = "", start_index: int = 0) -> List[dict]:
messages = []
for message in self.messages:
for message in self.messages[start_index:]:
messages.append(
{
"role": "user" if message.sender != my_name else "assistant",
Expand Down
Loading

0 comments on commit ee8ced1

Please sign in to comment.