From b0eeeea7a3c655e517d236c12c0381c03f35ced6 Mon Sep 17 00:00:00 2001 From: chenweize1998 Date: Tue, 7 Nov 2023 11:00:17 +0800 Subject: [PATCH] fix: update outdated configs and fix logging #95 --- agentverse/agents/base.py | 3 +- .../simulation_agent/prisoner_dilemma.py | 14 +++---- agentverse/agents/simulation_agent/tool.py | 19 ++++----- .../environments/simulation_env/pokemon.py | 4 +- .../simulation_env/rules/order/classroom.py | 4 +- .../environments/simulation_env/sde_team.py | 3 +- .../simulation_env/sde_team_given_tests.py | 4 +- agentverse/llms/openai.py | 6 +-- agentverse/output_parser/output_parser.py | 39 +++++++++++++------ .../math_problem_2players_tools/config.yaml | 8 ++-- .../config.yaml | 13 +++---- .../nlp_classroom_9players_group/config.yaml | 22 +++++------ agentverse/utils/prompts.py | 4 +- 13 files changed, 80 insertions(+), 63 deletions(-) diff --git a/agentverse/agents/base.py b/agentverse/agents/base.py index 1cc2cbb49..e17b94a50 100644 --- a/agentverse/agents/base.py +++ b/agentverse/agents/base.py @@ -6,6 +6,7 @@ from pydantic import BaseModel, Field from agentverse.llms import BaseLLM +from agentverse.logging import logger from agentverse.llms.utils import count_string_tokens from agentverse.memory import BaseMemory, ChatHistoryMemory from agentverse.message import Message @@ -106,7 +107,7 @@ def remove_receiver(self, receiver: Union[Set[str], str]) -> None: try: self.receiver.remove(receiver) except KeyError as e: - logging.warning(f"Receiver {receiver} not found.") + logger.warn(f"Receiver {receiver} not found.") elif isinstance(receiver, set): self.receiver = self.receiver.difference(receiver) else: diff --git a/agentverse/agents/simulation_agent/prisoner_dilemma.py b/agentverse/agents/simulation_agent/prisoner_dilemma.py index bf257c168..5a8674222 100644 --- a/agentverse/agents/simulation_agent/prisoner_dilemma.py +++ b/agentverse/agents/simulation_agent/prisoner_dilemma.py @@ -1,10 +1,10 @@ from __future__ import annotations -import logging from string import Template from typing import TYPE_CHECKING, List from agentverse.message import Message +from agentverse.logging import logger # from . import agent_registry # from .base import BaseAgent @@ -30,12 +30,12 @@ def step( parsed_response = self.output_parser.parse(self, environment, response) break except Exception as e: - logging.error(e) - logging.warning("Retrying...") + logger.error(e) + logger.warn("Retrying...") continue if parsed_response is None: - logging.error(f"{self.name} failed to generate valid response.") + logger.error(f"{self.name} failed to generate valid response.") message = Message( content="" @@ -59,12 +59,12 @@ async def astep( parsed_response = self.output_parser.parse(self, environment, response) break except Exception as e: - logging.error(e) - logging.warning("Retrying...") + logger.error(e) + logger.warn("Retrying...") continue if parsed_response is None: - logging.error(f"{self.name} failed to generate valid response.") + logger.error(f"{self.name} failed to generate valid response.") message = Message( content="" diff --git a/agentverse/agents/simulation_agent/tool.py b/agentverse/agents/simulation_agent/tool.py index 6833a22a1..4bb0e7181 100644 --- a/agentverse/agents/simulation_agent/tool.py +++ b/agentverse/agents/simulation_agent/tool.py @@ -1,4 +1,3 @@ -import logging from string import Template from typing import List, NamedTuple, Optional, Union @@ -9,13 +8,15 @@ from agentverse.memory import BaseMemory, ChatHistoryMemory from agentverse.message import Message from agentverse.utils import AgentAction, AgentFinish +from agentverse.logging import logger -#from . import agent_registry -#from .base import BaseAgent +# from . import agent_registry +# from .base import BaseAgent from agentverse.agents import agent_registry from agentverse.agents.base import BaseAgent + class ToolNotExistError(BaseException): """Exception raised when parsing output from a command fails.""" @@ -50,14 +51,14 @@ def step(self, env_description: str = "") -> Message: ) break except BaseException as e: - logging.error(e) - logging.warning("Retrying...") + logger.error(e) + logger.warn("Retrying...") continue if parsed_response is None or isinstance(parsed_response, AgentFinish): break if parsed_response is None: - logging.error(f"{self.name} failed to generate valid response.") + logger.error(f"{self.name} failed to generate valid response.") self._update_tool_memory(tool_observation) @@ -92,14 +93,14 @@ async def astep(self, env_description: str = "") -> Message: ) break except BaseException as e: - logging.error(e) - logging.warning("Retrying...") + logger.error(e) + logger.warn("Retrying...") continue if parsed_response is None or isinstance(parsed_response, AgentFinish): break if parsed_response is None: - logging.error(f"{self.name} failed to generate valid response.") + logger.error(f"{self.name} failed to generate valid response.") self._update_tool_memory(tool_observation) diff --git a/agentverse/environments/simulation_env/pokemon.py b/agentverse/environments/simulation_env/pokemon.py index d62b32cf6..bec9684ae 100644 --- a/agentverse/environments/simulation_env/pokemon.py +++ b/agentverse/environments/simulation_env/pokemon.py @@ -1,10 +1,10 @@ import asyncio import datetime -import logging from typing import Any, Dict, List, Optional, Set # from agentverse.agents.agent import Agent from agentverse.agents.simulation_agent.conversation import BaseAgent +from agentverse.logging import logger # from agentverse.environments.simulation_env.rules.base import Rule from agentverse.environments.simulation_env.rules.base import SimulationRule as Rule @@ -167,7 +167,7 @@ def get_agent_to_location(self) -> Dict[str, str]: def print_messages(self, messages: List[Message]) -> None: for message in messages: if message is not None: - logging.info(f"{message.sender}: {message.content}") + logger.info(f"{message.sender}: {message.content}") def reset(self) -> None: """Reset the environment""" diff --git a/agentverse/environments/simulation_env/rules/order/classroom.py b/agentverse/environments/simulation_env/rules/order/classroom.py index dba459bee..1e2321a03 100644 --- a/agentverse/environments/simulation_env/rules/order/classroom.py +++ b/agentverse/environments/simulation_env/rules/order/classroom.py @@ -1,11 +1,11 @@ from __future__ import annotations -import logging import re from typing import TYPE_CHECKING, Any, List, Optional from . import order_registry as OrderRegistry from .base import BaseOrder +from agentverse.logging import logger if TYPE_CHECKING: from agentverse.environments import BaseEnvironment @@ -75,7 +75,7 @@ def get_next_agent_idx_grouped(self, environment: BaseEnvironment) -> List[int]: # `groups` should be set in the corresponding `visibility`, # and `group_speaker_mapping` should be maintained here. if "groups" not in environment.rule_params: - logging.warning( + logger.warn( "The environment is grouped, but the grouping information is not provided." ) groups = environment.rule_params.get( diff --git a/agentverse/environments/simulation_env/sde_team.py b/agentverse/environments/simulation_env/sde_team.py index 325c1eac0..474f979bb 100644 --- a/agentverse/environments/simulation_env/sde_team.py +++ b/agentverse/environments/simulation_env/sde_team.py @@ -4,6 +4,7 @@ import json from agentverse.agents.simulation_agent.conversation import BaseAgent +from agentverse.logging import logger # from agentverse.environments.simulation_env.rules.base import Rule from agentverse.environments.simulation_env.rules.base import SimulationRule as Rule @@ -116,7 +117,7 @@ async def step(self) -> List[Message]: def print_messages(self, messages: List[Message]) -> None: for message in messages: if message is not None: - logging.info(f"{message.sender}: {message.content}") + logger.info(f"{message.sender}: {message.content}") def reset(self) -> None: """Reset the environment""" diff --git a/agentverse/environments/simulation_env/sde_team_given_tests.py b/agentverse/environments/simulation_env/sde_team_given_tests.py index fdef4a863..b60a6ed65 100644 --- a/agentverse/environments/simulation_env/sde_team_given_tests.py +++ b/agentverse/environments/simulation_env/sde_team_given_tests.py @@ -1,5 +1,4 @@ import asyncio -import logging from typing import Any, Dict, List import json @@ -8,6 +7,7 @@ # from agentverse.environments.simulation_env.rules.base import Rule from agentverse.environments.simulation_env.rules.base import SimulationRule as Rule from agentverse.message import Message +from agentverse.logging import logger from .. import env_registry as EnvironmentRegistry from ..base import BaseEnvironment @@ -104,7 +104,7 @@ async def step(self) -> List[Message]: def print_messages(self, messages: List[Message]) -> None: for message in messages: if message is not None: - logging.info(f"{message.sender}: {message.content}") + logger.info(f"{message.sender}: {message.content}") def reset(self) -> None: """Reset the environment""" diff --git a/agentverse/llms/openai.py b/agentverse/llms/openai.py index 1331d9155..72669fc59 100644 --- a/agentverse/llms/openai.py +++ b/agentverse/llms/openai.py @@ -22,7 +22,7 @@ from openai.error import OpenAIError except ImportError: is_openai_available = False - logging.warning("openai package is not installed") + logger.warn("openai package is not installed") else: # openai.proxy = os.environ.get("http_proxy") # if openai.proxy is None: @@ -37,7 +37,7 @@ openai.api_version = "2023-05-15" is_openai_available = True else: - logging.warning( + logger.warn( "OpenAI API key is not set. Please set the environment variable OPENAI_API_KEY" ) is_openai_available = False @@ -110,7 +110,7 @@ def __init__(self, max_retry: int = 3, **kwargs): for k, v in args.items(): args[k] = kwargs.pop(k, v) if len(kwargs) > 0: - logging.warning(f"Unused arguments: {kwargs}") + logger.warn(f"Unused arguments: {kwargs}") if args["model"] in LOCAL_LLMS: openai.api_base = "http://localhost:5000/v1" super().__init__(args=args, max_retry=max_retry) diff --git a/agentverse/output_parser/output_parser.py b/agentverse/output_parser/output_parser.py index 556d9ff6e..59cc47947 100644 --- a/agentverse/output_parser/output_parser.py +++ b/agentverse/output_parser/output_parser.py @@ -18,6 +18,7 @@ from agentverse.agents.base import BaseAgent from agentverse.environments.base import BaseEnvironment + class OutputParserError(Exception): """Exception raised when parsing output from a command fails.""" @@ -157,24 +158,38 @@ def parse(self, output: LLMResult) -> Union[AgentAction, AgentFinish]: class NlpClassroom9PlayersGroupParser(OutputParser): def parse(self, output: LLMResult) -> Union[AgentAction, AgentFinish]: text = output.content - cleaned_output = text.strip() - cleaned_output = re.sub(r"\n+", "\n", cleaned_output) - cleaned_output = cleaned_output.split("\n") - if not ( - len(cleaned_output) == 2 - and cleaned_output[0].startswith("Action:") - and cleaned_output[1].startswith("Action Input:") - ): + # cleaned_output = text.strip() + # cleaned_output = re.sub(r"\n+", "\n", cleaned_output) + # cleaned_output = cleaned_output.split("\n") + # if not ( + # len(cleaned_output) == 2 + # and cleaned_output[0].startswith("Action:") + # and cleaned_output[1].startswith("Action Input:") + # ): + # raise OutputParserError(text) + # action = cleaned_output[0][len("Action:") :].strip() + # action_input = cleaned_output[1][len("Action Input:") :].strip() + action_result = re.findall(r"Action:(.+)", text) + result = re.findall(r"Action:(.+?)Action Input:(.+)", text, re.DOTALL) + if len(action_result) == 0: raise OutputParserError(text) - action = cleaned_output[0][len("Action:") :].strip() - action_input = cleaned_output[1][len("Action Input:") :].strip() + + action = action_result[0].strip() + if action not in ["Listen", "RaiseHand"]: + if len(result) == 0: + raise OutputParserError(text) + action_input = result[0][1].strip() + if action == "RaiseHand": + action_input = "" + if action == "Speak": return AgentFinish({"output": action_input}, text) - elif action in ["CallOn", "RaiseHand", "GroupDiscuss"]: + elif action in ["CallOn", "GroupDiscuss", "RaiseHand"]: return AgentFinish({"output": f"[{action}] {action_input}"}, text) - elif action == "Listen": + elif action in ["Listen"]: return AgentFinish({"output": ""}, text) else: + action_input = result[0][1].strip() return AgentAction(action, action_input, text) diff --git a/agentverse/tasks/simulation/math_problem_2players_tools/config.yaml b/agentverse/tasks/simulation/math_problem_2players_tools/config.yaml index 6e154b9d2..0bc9e29ab 100644 --- a/agentverse/tasks/simulation/math_problem_2players_tools/config.yaml +++ b/agentverse/tasks/simulation/math_problem_2players_tools/config.yaml @@ -94,8 +94,8 @@ agents: prompt_template: *prompt verbose: true llm: - llm_type: text-davinci-003 - model: text-davinci-003 + llm_type: gpt-4 + model: gpt-4 temperature: 0.7 max_tokens: 250 output_parser: @@ -110,8 +110,8 @@ agents: prompt_template: *prompt verbose: true llm: - llm_type: text-davinci-003 - model: text-davinci-003 + llm_type: gpt-4 + model: gpt-4 temperature: 0.7 max_tokens: 250 output_parser: diff --git a/agentverse/tasks/simulation/nlp_classroom_3players_withtool/config.yaml b/agentverse/tasks/simulation/nlp_classroom_3players_withtool/config.yaml index 336c7e25e..762085b9a 100644 --- a/agentverse/tasks/simulation/nlp_classroom_3players_withtool/config.yaml +++ b/agentverse/tasks/simulation/nlp_classroom_3players_withtool/config.yaml @@ -155,9 +155,8 @@ agents: 5. Provide an envision towards the future development of neural networks. Your goal is to ensure that the students understand the material, so it's important to speak slowly and clearly. You don't necessarily have to strictly follow the course outline when teaching, you can also talk about some other relevant topics. Remember, in each round of conversation, your response should only address one topic at most. Please take your time and don't rush through the content. prompt_template: *professor_prompt - llm: - llm_type: text-davinci-003 - model: text-davinci-003 + ll gpt-4 + model: gpt-4 temperature: 0.7 max_tokens: 250 output_parser: @@ -180,8 +179,8 @@ agents: prompt_template: *summary_prompt recursive: true llm: - llm_type: text-davinci-003 - model: text-davinci-003 + llm_type: gpt-4 + model: gpt-4 temperature: 0.7 max_tokens: 100 output_parser: @@ -203,8 +202,8 @@ agents: prompt_template: *summary_prompt recursive: true llm: - llm_type: text-davinci-003 - model: text-davinci-003 + llm_type: gpt-4 + model: gpt-4 temperature: 0.7 max_tokens: 100 output_parser: diff --git a/agentverse/tasks/simulation/nlp_classroom_9players_group/config.yaml b/agentverse/tasks/simulation/nlp_classroom_9players_group/config.yaml index b8d9d0319..89d840dfd 100644 --- a/agentverse/tasks/simulation/nlp_classroom_9players_group/config.yaml +++ b/agentverse/tasks/simulation/nlp_classroom_9players_group/config.yaml @@ -43,11 +43,11 @@ prompts: - During class, it's recommended that you listen to the professor by responding: Action: Listen - Action Input: None + Action Input: listen - If you have a question that you think it's worth discussing in class, you should first raise your hand using the following format to let the professor notice you: Action: RaiseHand - Action Input: None + Action Input: raise hand if the professor does call on your name, you MUST speak or ask a question, and use the following format: Action: Speak @@ -115,7 +115,7 @@ agents: When teaching, it's not necessary to strictly adhere to the course outline. You can also incorporate other relevant topics into your lectures. It's important to take your time and not rush through the content, explaining each topic carefully and ensuring that your students fully grasp the material. prompt_template: *professor_prompt llm: - llm_type: text-davinci-003 + llm_type: gpt-4 temperature: 0.7 max_tokens: 250 output_parser: @@ -130,7 +130,7 @@ agents: memory: memory_type: chat_history llm: - llm_type: text-davinci-003 + llm_type: gpt-4 temperature: 0.7 max_tokens: 100 output_parser: @@ -143,7 +143,7 @@ agents: memory: memory_type: chat_history llm: - llm_type: text-davinci-003 + llm_type: gpt-4 temperature: 0.7 max_tokens: 100 output_parser: @@ -156,7 +156,7 @@ agents: memory: memory_type: chat_history llm: - llm_type: text-davinci-003 + llm_type: gpt-4 temperature: 0.7 max_tokens: 100 output_parser: @@ -169,7 +169,7 @@ agents: memory: memory_type: chat_history llm: - llm_type: text-davinci-003 + llm_type: gpt-4 temperature: 0.7 max_tokens: 100 output_parser: @@ -182,7 +182,7 @@ agents: memory: memory_type: chat_history llm: - llm_type: text-davinci-003 + llm_type: gpt-4 temperature: 0.7 max_tokens: 100 output_parser: @@ -195,7 +195,7 @@ agents: memory: memory_type: chat_history llm: - llm_type: text-davinci-003 + llm_type: gpt-4 temperature: 0.7 max_tokens: 100 output_parser: @@ -208,7 +208,7 @@ agents: memory: memory_type: chat_history llm: - llm_type: text-davinci-003 + llm_type: gpt-4 temperature: 0.7 max_tokens: 100 output_parser: @@ -221,7 +221,7 @@ agents: memory: memory_type: chat_history llm: - llm_type: text-davinci-003 + llm_type: gpt-4 temperature: 0.7 max_tokens: 100 output_parser: diff --git a/agentverse/utils/prompts.py b/agentverse/utils/prompts.py index e9824ecbc..e7be8aa8f 100644 --- a/agentverse/utils/prompts.py +++ b/agentverse/utils/prompts.py @@ -1,6 +1,6 @@ import json import os -import logging +from agentverse.logging import logger base_prompt = { @@ -208,5 +208,5 @@ def load_prompt(file_dir, file_name="prompts.json", key=None): prompt = base_prompt.get(key, "") if prompt == "": - logging.warning(f"No prompt of {key} has been found") + logger.warn(f"No prompt of {key} has been found") return prompt