From f2591b7eb435c199d0edb378d4986fbe493ae54f Mon Sep 17 00:00:00 2001 From: abdullah <44318011+fx2y@users.noreply.github.com> Date: Mon, 9 Oct 2023 13:41:46 +0700 Subject: [PATCH] Configure the agent's parameters, such as the LLM model, the human input source, the tools, etc., based on the agent's mode and the user's specifications. If any error or conflict is detected, notify the user and suggest possible solutions. Select the appropriate LLM model based on the task requirements and the agent's mode. --- agent_configurator.py | 83 +++++++++++++++++++++++++++++++++++-------- agent_factory.py | 6 ++-- agent_manager.py | 6 ++-- task_requirements.py | 13 +++++++ test_agent_factory.py | 13 +++---- 5 files changed, 95 insertions(+), 26 deletions(-) diff --git a/agent_configurator.py b/agent_configurator.py index 86d644d..89ec255 100644 --- a/agent_configurator.py +++ b/agent_configurator.py @@ -1,29 +1,44 @@ +import json + +import pymongo + + class AgentConfigurator: def __init__(self): - self.llm_models = ["GPT-2", "BERT", "XLNet"] + self.llm_models = {} self.input_sources = ["text", "voice", "image"] self.tools = ["NLTK", "spaCy", "gensim"] - def configure_agent(self, agent_id, mode): - llm_model = self._select_llm_model(mode) + # Configure agent's parameters based on mode + def configure_agent(self, agent_id, mode, source): + # Select the appropriate LLM model based on the task requirements and the agent's mode + llm_model = self._select_llm_model(mode, source) input_source = self._select_input_source(mode) tools = self._select_tools(mode) + + if not llm_model: + print("Error: No compatible LLM model found.") + return False + if not llm_model or not input_source or not tools: print("Error: Invalid mode for agent.") return False - # Configure agent's parameters based on mode - # ... + + # Configure agent's parameters based on mode and user's specifications + if not self._configure_llm_model(agent_id, llm_model): + return False + return True - def _select_llm_model(self, mode): - if mode == "text": - return "GPT-2" - elif mode == "voice": - return "BERT" - elif mode == "image": - return "XLNet" - else: - return None + # Select the appropriate LLM model based on the task requirements and the agent's mode + def _select_llm_model(self, mode, source): + # Check if LLM models have been loaded + if not self.llm_models: + self._load_llm_models(source) + + # Select the appropriate LLM model based on the task requirements and the agent's mode + if mode in self.llm_models: + return self.llm_models[mode] def _select_input_source(self, mode): if mode == "text": @@ -44,3 +59,43 @@ def _select_tools(self, mode): return ["spaCy", "gensim"] else: return None + + # Load LLM models from database or file system + def _load_llm_models(self, source): + if source == "database": + # Establish a connection to the database + client = pymongo.MongoClient("mongodb://localhost:27017/") + db = client["llm_models"] + collection = db["models"] + + # Retrieve the LLM models from the database + models = {} + for model in collection.find(): + models[model["name"]] = model["model"] + + elif source == "file_system": + # Read the LLM models from the file system + models = {} + with open("llm_models.json", "r") as f: + data = json.load(f) + for model in data["models"]: + models[model["name"]] = model["model"] + + else: + raise ValueError("Invalid source specified") + + # Store the LLM models in a dictionary + self.llm_models = { + "text": models["GPT-2"], + "voice": models["Bert"], + "image": models["XLNet"] + } + + return self.llm_models + + # Configure agent's LLM model based on user's specifications + def _configure_llm_model(self, agent_id, llm_model): + # Configure agent's LLM model based on user's specifications + # ... + + return True diff --git a/agent_factory.py b/agent_factory.py index 97559b5..4d2c331 100644 --- a/agent_factory.py +++ b/agent_factory.py @@ -13,7 +13,7 @@ def __init__(self): self.agents = {} # Create new agent based on user's specifications - def create_agent(self, name, personality, role, mode): + def create_agent(self, name, personality, role, mode, source): # Check if agent name is unique and follows naming convention if not self._is_valid_name(name): return None @@ -34,10 +34,10 @@ def create_agent(self, name, personality, role, mode): agent_id = self._generate_id() communication_channel = self._create_communication_channel(agent_id) - # Configure agent's parameters based on mode + # Configure agent's parameters based on mode and source from agent_configurator import AgentConfigurator agent_configurator = AgentConfigurator() - if not agent_configurator.configure_agent(agent_id, mode): + if not agent_configurator.configure_agent(agent_id, mode, source): return None # Initialize agent's state based on role and task diff --git a/agent_manager.py b/agent_manager.py index da4ccbe..f9267f1 100644 --- a/agent_manager.py +++ b/agent_manager.py @@ -8,11 +8,11 @@ def __init__(self): self.agents: Dict[str, Dict[str, Any]] = {} self.factory = AgentFactory() - def create_agent(self, name: str, personality: str, role: str, mode: str) -> Optional[str]: - agent_id = self.factory.create_agent(name, personality, role, mode) + def create_agent(self, name: str, personality: str, role: str, mode: str, source: str) -> Optional[str]: + agent_id = self.factory.create_agent(name, personality, role, mode, source) if agent_id is None: return None - self.agents[agent_id] = {"name": name, "personality": personality, "role": role, "mode": mode} + self.agents[agent_id] = {"name": name, "personality": personality, "role": role, "mode": mode, "source": source} return agent_id def get_agent_name(self, agent_id: str) -> Optional[str]: diff --git a/task_requirements.py b/task_requirements.py index a0dbcfb..37d3f7a 100644 --- a/task_requirements.py +++ b/task_requirements.py @@ -1,4 +1,17 @@ class TaskRequirements: + valid_tasks = ["task1", "task2", "task3"] + + @staticmethod + def is_compatible_mode(mode): + if mode == "mode1": + return True + elif mode == "mode2": + return False + elif mode == "mode3": + return True + else: + return False + @staticmethod def is_compatible_role(role, mode): # Check if role is defined diff --git a/test_agent_factory.py b/test_agent_factory.py index 7f518b9..f0cda36 100644 --- a/test_agent_factory.py +++ b/test_agent_factory.py @@ -11,24 +11,25 @@ def setUp(self): def test_create_agent(self): # Test creating a new agent with valid parameters - agent_id = self.agent_factory.create_agent("test_agent", "friendly", "assistant", "training") + agent_id = self.agent_factory.create_agent("test_agent", "friendly", "assistant", "training", "file_system") self.assertIsNotNone(agent_id) self.assertIn(agent_id, self.agent_factory.agents.keys()) # Test creating a new agent with invalid name - agent_id = self.agent_factory.create_agent("invalid name", "friendly", "assistant", "training") + agent_id = self.agent_factory.create_agent("invalid name", "friendly", "assistant", "training", "file_system") self.assertIsNone(agent_id) # Test creating a new agent with invalid mode - agent_id = self.agent_factory.create_agent("test_agent", "friendly", "assistant", "invalid_mode") + agent_id = self.agent_factory.create_agent("test_agent", "friendly", "assistant", "invalid_mode", "file_system") self.assertIsNone(agent_id) # Test creating a new agent with invalid role - agent_id = self.agent_factory.create_agent("test_agent", "friendly", "invalid_role", "training") + agent_id = self.agent_factory.create_agent("test_agent", "friendly", "invalid_role", "training", "file_system") self.assertIsNone(agent_id) # Test creating a new agent with invalid personality - agent_id = self.agent_factory.create_agent("test_agent", "invalid_personality", "assistant", "training") + agent_id = self.agent_factory.create_agent("test_agent", "invalid_personality", "assistant", "training", + "file_system") self.assertIsNone(agent_id) def test_is_valid_name(self): @@ -63,7 +64,7 @@ def test_create_agent_with_mock(self, mock_register_communication_channel, mock_ # Test creating a new agent with valid parameters mock_configure_agent.return_value = True mock_initialize_agent.return_value = True - agent_id = self.agent_factory.create_agent("test_agent", "friendly", "assistant", "training") + agent_id = self.agent_factory.create_agent("test_agent", "friendly", "assistant", "training", "file_system") self.assertIsNotNone(agent_id) self.assertIn(agent_id, self.agent_factory.agents.keys()) mock_configure_agent.assert_called_once_with(agent_id, "training")