Skip to content

Commit

Permalink
Configure the agent's parameters, such as the LLM model, the human in…
Browse files Browse the repository at this point in the history
…put 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.
  • Loading branch information
fx2y committed Oct 9, 2023
1 parent 10c538d commit f2591b7
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 26 deletions.
83 changes: 69 additions & 14 deletions agent_configurator.py
Original file line number Diff line number Diff line change
@@ -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":
Expand All @@ -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
6 changes: 3 additions & 3 deletions agent_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions agent_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
13 changes: 13 additions & 0 deletions task_requirements.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 7 additions & 6 deletions test_agent_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit f2591b7

Please sign in to comment.