Skip to content

Commit

Permalink
fix: make to_message asynchronous to accelerate compressing chat hist…
Browse files Browse the repository at this point in the history
…ory of multiple agents
  • Loading branch information
minleminzui committed Oct 25, 2023
1 parent 51f92b5 commit 3aa56d4
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 154 deletions.
2 changes: 1 addition & 1 deletion agentverse/agents/tasksolving_agent/critic.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async def astep(

max_send_token -= prompt_token

history = self.memory.to_messages(
history = await self.memory.to_messages(
self.name,
start_index=-self.max_history,
max_send_token=max_send_token,
Expand Down
20 changes: 13 additions & 7 deletions agentverse/agents/tasksolving_agent/evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ def step(
task_description: str,
all_role_description: str,
) -> EvaluatorMessage:
pass
# return parsed_response

async def astep(
self,
solution: str,
result: str,
task_description: str,
all_role_description: str,
) -> EvaluatorMessage:
"""Asynchronous version of step"""
logger.debug("", self.name, Fore.MAGENTA)
prepend_prompt, append_prompt, prompt_token = self.get_all_prompts(
solution=solution,
Expand All @@ -49,15 +60,15 @@ def step(

max_send_token -= prompt_token

history = self.memory.to_messages(
history = await self.memory.to_messages(
self.name,
max_send_token=max_send_token,
model=model_name,
)
parsed_response = None
for i in range(self.max_retry):
try:
response = self.llm.generate_response(
response = await self.llm.agenerate_response(
prepend_prompt, history, append_prompt
)
parsed_response = self.output_parser.parse(response)
Expand All @@ -78,11 +89,6 @@ def step(
advice=parsed_response[1] if parsed_response is not None else "",
)
return message
# return parsed_response

async def astep(self, solution: str) -> EvaluatorMessage:
"""Asynchronous version of step"""
pass

def _fill_prompt_template(self, solution: str, task_description: str) -> str:
"""Fill the placeholders in the prompt template
Expand Down
69 changes: 2 additions & 67 deletions agentverse/agents/tasksolving_agent/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,72 +23,7 @@ class ExecutorAgent(BaseAgent):
def step(
self, task_description: str, solution: str, tools: List[dict] = [], **kwargs
) -> ExecutorMessage:
logger.debug("", self.name, Fore.MAGENTA)
prepend_prompt, append_prompt, prompt_token = self.get_all_prompts(
task_description=task_description,
solution=solution,
agent_name=self.name,
**kwargs,
)

model_name = self.llm.args.model

if model_name.startswith("gpt-3.5-turbo"):
tokens_per_message = 4
else:
tokens_per_message = 3

max_send_token = self.llm.send_token_limit(model_name)
if len(prepend_prompt) > 0:
max_send_token -= tokens_per_message
if (len(append_prompt)) > 0:
max_send_token -= tokens_per_message

max_send_token -= prompt_token

history = self.memory.to_messages(
self.name,
start_index=-self.max_history,
max_send_token=max_send_token,
model=model_name,
)
parsed_response = None
for i in range(self.max_retry):
try:
response = self.llm.generate_response(
prepend_prompt, history, append_prompt, tools
)
parsed_response = self.output_parser.parse(response)
break
except (KeyboardInterrupt, bdb.BdbQuit):
raise
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 isinstance(parsed_response, AgentFinish):
message = ExecutorMessage(
content=parsed_response.return_values["output"],
sender=self.name,
sender_agent=self,
)
elif isinstance(parsed_response, AgentAction):
message = ExecutorMessage(
content=parsed_response.log,
sender=self.name,
sender_agent=self,
tool_name=parsed_response.tool,
tool_input=parsed_response.tool_input,
)
else:
raise ValueError(
f"Error response type: {type(parsed_response)}. Only support \
AgentFinish and AgentAction. Modify your output parser."
)
return message
pass

async def astep(
self, task_description: str, solution: str, tools: List[dict] = [], **kwargs
Expand Down Expand Up @@ -116,7 +51,7 @@ async def astep(

max_send_token -= prompt_token

history = self.memory.to_messages(
history = await self.memory.to_messages(
self.name,
start_index=-self.max_history,
max_send_token=max_send_token,
Expand Down
14 changes: 8 additions & 6 deletions agentverse/agents/tasksolving_agent/role_assigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ class RoleAssignerAgent(BaseAgent):
def step(
self, advice: str, task_description: str, cnt_critic_agents: int
) -> RoleAssignerMessage:
pass

async def astep(
self, advice: str, task_description: str, cnt_critic_agents: int
) -> RoleAssignerMessage:
"""Asynchronous version of step"""
logger.debug("", self.name, Fore.MAGENTA)
prepend_prompt, append_prompt, prompt_token = self.get_all_prompts(
advice=advice,
Expand All @@ -44,13 +50,13 @@ def step(

max_send_token -= prompt_token

history = self.memory.to_messages(
history = await self.memory.to_messages(
self.name, max_send_token=max_send_token, model=model_name
)
parsed_response = None
for i in range(self.max_retry):
try:
response = self.llm.generate_response(
response = await self.llm.agenerate_response(
prepend_prompt, history, append_prompt
)
parsed_response = self.output_parser.parse(response)
Expand All @@ -76,10 +82,6 @@ def step(
)
return message

async def astep(self, env_description: str = "") -> RoleAssignerMessage:
"""Asynchronous version of step"""
pass

def _fill_prompt_template(
self, advice, task_description: str, cnt_critic_agents: int
) -> str:
Expand Down
14 changes: 8 additions & 6 deletions agentverse/agents/tasksolving_agent/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class SolverAgent(BaseAgent):
def step(
self, former_solution: str, advice: str, task_description: str = "", **kwargs
) -> SolverMessage:
pass

async def astep(
self, former_solution: str, advice: str, task_description: str = "", **kwargs
) -> SolverMessage:
"""Asynchronous version of step"""
logger.debug("", self.name, Fore.MAGENTA)
# prompt = self._fill_prompt_template(
# former_solution, critic_opinions, advice, task_description
Expand Down Expand Up @@ -52,7 +58,7 @@ def step(

max_send_token -= prompt_token

history = self.memory.to_messages(
history = await self.memory.to_messages(
self.name,
start_index=-self.max_history,
max_send_token=max_send_token,
Expand All @@ -61,7 +67,7 @@ def step(
parsed_response = None
for i in range(self.max_retry):
try:
response = self.llm.generate_response(
response = await self.llm.agenerate_response(
prepend_prompt, history, append_prompt
)
parsed_response = self.output_parser.parse(response)
Expand All @@ -85,10 +91,6 @@ def step(
)
return message

async def astep(self, env_description: str = "") -> SolverMessage:
"""Asynchronous version of step"""
pass

def _fill_prompt_template(
self,
former_solution: str,
Expand Down
4 changes: 2 additions & 2 deletions agentverse/environments/tasksolving_env/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async def step(
logger.info(f"Loop Round {self.cnt_turn}")

# ================== EXPERT RECRUITMENT ==================
agents = self.rule.role_assign(
agents = await self.rule.role_assign(
self.task_description, self.agents, self.cnt_turn, advice
)
description = "\n".join([agent.role_description for agent in agents])
Expand Down Expand Up @@ -79,7 +79,7 @@ async def step(
# ================== EXECUTION ==================

# ================== EVALUATION ==================
score, advice = self.rule.evaluate(
score, advice = await self.rule.evaluate(
self.task_description, self.agents, plan, result
)
logs.append(
Expand Down
8 changes: 4 additions & 4 deletions agentverse/environments/tasksolving_env/rules/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def build_components(config: Dict, registry):
**kwargs,
)

def role_assign(
async def role_assign(
self,
task_description: str,
agents: List[BaseAgent],
Expand All @@ -79,7 +79,7 @@ def role_assign(
if self.role_assign_only_once and cnt_turn > 0:
agents = [agents[AGENT_TYPES.SOLVER]] + agents[AGENT_TYPES.CRITIC]
else:
agents = self.role_assigner.step(
agents = await self.role_assigner.astep(
role_assigner=agents[AGENT_TYPES.ROLE_ASSIGNMENT],
group_members=[agents[AGENT_TYPES.SOLVER]] + agents[AGENT_TYPES.CRITIC],
advice=advice,
Expand Down Expand Up @@ -137,7 +137,7 @@ async def execute(
agents[AGENT_TYPES.SOLVER].add_message_to_memory(results)
return results

def evaluate(
async def evaluate(
self,
task_description: str,
agents: List[BaseAgent],
Expand All @@ -162,7 +162,7 @@ def evaluate(
# logger.error("Bad response from human evaluator!")
# return ([comprehensiveness, detailedness, feasibility, novelty], advice)
# else:
evaluation = self.evaluator.step(
evaluation = await self.evaluator.astep(
agent=agents[AGENT_TYPES.EVALUATION],
solution=solution,
result=result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async def astep(
Fore.YELLOW,
)

result = agents[0].step(previous_plan, advice, task_description)
result = await agents[0].astep(previous_plan, advice, task_description)
for agent in agents:
agent.memory.reset()
self.broadcast_messages(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def astep(
),
)
agents[1].add_message_to_memory([result])
result = agents[0].step(
result = await agents[0].astep(
previous_plan, advice, task_description, chat_record=result.content
)
return [result]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def astep(
last_reviews = nonempty_reviews

agents[0].add_message_to_memory(last_reviews)
result = agents[0].step(previous_plan, advice, task_description)
result = await agents[0].astep(previous_plan, advice, task_description)
# agents[0].add_message_to_memory([result])
self.broadcast_messages(agents, [result])
return [result]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async def astep(
# Fore.YELLOW,
# )

previous_sentence = manager.step(
previous_sentence = await manager.astep(
previous_plan, review, advice, task_description, previous_sentence
)
reviews.append(previous_sentence)
Expand All @@ -76,7 +76,7 @@ async def astep(
nonempty_reviews.append(review)
agents[0].add_message_to_memory(nonempty_reviews)

result = agents[0].step(previous_plan, advice, task_description)
result = await agents[0].astep(previous_plan, advice, task_description)

return [result]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def astep(
Fore.YELLOW,
)

result = agents[0].step(previous_plan, advice, task_description)
result = await agents[0].astep(previous_plan, advice, task_description)
return [result]

def reset(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ async def astep(
if end_flag:
break

result: SolverMessage = agents[0].step(previous_plan, advice, task_description)
result: SolverMessage = await agents[0].astep(
previous_plan, advice, task_description
)
result_list = []
for res in result.content:
res_tmp = deepcopy(result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async def astep(
if not review.is_agree and review.content != "":
nonempty_reviews.append(review)
agents[0].add_message_to_memory(nonempty_reviews)
result = agents[0].step(previous_plan, advice, task_description)
result = await agents[0].astep(previous_plan, advice, task_description)
agents[0].add_message_to_memory([result])
return [result]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def astep(
self.broadcast_messages(
agents, [Message(content=advice, sender="Evaluator")]
)
previous_plan = agents[0].step(previous_plan, advice, task_description)
previous_plan = await agents[0].astep(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):
Expand All @@ -65,7 +65,7 @@ async def astep(
logger.info("", "Consensus Reached!.", Fore.GREEN)
break
self.broadcast_messages(agents, nonempty_reviews)
previous_plan = agents[0].step(previous_plan, advice, task_description)
previous_plan = await agents[0].astep(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class BaseEvaluator(BaseModel):
"""

@abstractmethod
def step(
def astep(
self,
agent: EvaluatorAgent,
solution: List[SolverMessage],
Expand Down
Loading

0 comments on commit 3aa56d4

Please sign in to comment.