-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_factory.py
119 lines (93 loc) · 4.37 KB
/
agent_factory.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Agent Factory
import re
import uuid
from communication_channel import CommunicationChannel
from llm_model import LLMModel
from mode_requirements import ModeRequirements
from task_requirements import TaskRequirements
class AgentFactory:
def __init__(self):
self.agents = {}
# Create new agent based on user's specifications
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
# Check if agent personality is compatible with LLM model and task requirements
if not self._is_compatible_personality(personality, mode):
return None
# Check if agent role is defined and consistent with task requirements
if not self._is_valid_role(role, mode):
return None
# Check if agent mode is valid and consistent with task requirements
if not self._is_valid_mode(mode):
return None
# Create new agent with unique ID and communication channel
agent_id = self._generate_id()
communication_channel = self._create_communication_channel(agent_id)
# 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, source):
return None
# Initialize agent's state based on role and task
from agent_initializer import AgentInitializer
agent_initializer = AgentInitializer()
if not agent_initializer.initialize_agent(agent_id, role):
return None
# Register agent with Agent Manager and Communication Manager
from agent_manager import AgentManager
from communication_manager import CommunicationManager
agent_manager = AgentManager()
communication_manager = CommunicationManager()
agent_manager.register_agent(agent_id, name, personality, role, mode)
communication_manager.register_communication_channel(agent_id, communication_channel)
# Add agent to list of agents
self.agents[agent_id] = name
return agent_id
# Check if agent name is unique and follows naming convention
def _is_valid_name(self, name):
# Check if name is unique
if name in self.agents.values():
print("Error: Agent name is not unique.")
return False
# Check if name follows naming convention
if not re.match(r'^[a-zA-Z0-9_-]+$', name):
print("Error: Agent name does not follow naming convention.")
return False
return True
# Check if agent personality is compatible with LLM model and task requirements
def _is_compatible_personality(self, personality, mode):
# Check if personality is compatible with LLM model and task requirements
if not LLMModel.is_compatible_personality(personality, mode):
print("Error: Agent personality is not compatible with LLM model and task requirements.")
return False
return True
# Check if agent role is defined and consistent with task requirements
def _is_valid_role(self, role, mode):
# Check if role is defined
if not role:
print("Error: Agent role is not defined.")
return False
# Check if role is consistent with task requirements
if not TaskRequirements.is_compatible_role(role, mode):
print("Error: Agent role is not consistent with task requirements.")
return False
return True
# Check if agent mode is valid and consistent with task requirements
def _is_valid_mode(self, mode):
# Check if mode is valid
if mode not in ModeRequirements.valid_modes:
print("Error: Agent mode is not valid.")
return False
# Check if mode is consistent with task requirements
if not TaskRequirements.is_compatible_mode(mode):
print("Error: Agent mode is not consistent with task requirements.")
return False
return True
# Generate unique ID for agent
def _generate_id(self):
return uuid.uuid4()
# Create communication channel for agent
def _create_communication_channel(self, agent_id):
return CommunicationChannel(agent_id)