forked from TransformerOptimus/SuperAGI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
274 lines (223 loc) · 9.75 KB
/
agent.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from __future__ import annotations
import json
from sqlalchemy import Column, Integer, String, Boolean
import superagi.models
from superagi.models.agent_config import AgentConfiguration
from superagi.models.agent_template import AgentTemplate
from superagi.models.agent_template_config import AgentTemplateConfig
from superagi.models.agent_workflow import AgentWorkflow
# from superagi.models import AgentConfiguration
from superagi.models.base_model import DBBaseModel
from superagi.lib.logger import logger
from superagi.models.organisation import Organisation
from superagi.models.project import Project
class Agent(DBBaseModel):
"""
Represents an agent entity.
Attributes:
id (int): The unique identifier of the agent.
name (str): The name of the agent.
project_id (int): The identifier of the associated project.
description (str): The description of the agent.
agent_workflow_id (int): The identifier of the associated agent workflow.
is_deleted (bool): The flag associated for agent deletion
"""
__tablename__ = 'agents'
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String)
project_id = Column(Integer)
description = Column(String)
agent_workflow_id = Column(Integer)
is_deleted = Column(Boolean, default = False)
def __repr__(self):
"""
Returns a string representation of the Agent object.
Returns:
str: String representation of the Agent.
"""
return f"Agent(id={self.id}, name='{self.name}', project_id={self.project_id}, " \
f"description='{self.description}', agent_workflow_id={self.agent_workflow_id}," \
f"is_deleted='{self.is_deleted}')"
@classmethod
def fetch_configuration(cls, session, agent_id: int):
"""
Fetches the configuration of an agent.
Args:
session: The database session object.
agent_id (int): The ID of the agent.
Returns:
dict: Parsed agent configuration.
"""
agent = session.query(Agent).filter_by(id=agent_id).first()
agent_configurations = session.query(superagi.models.agent_config.AgentConfiguration).filter_by(
agent_id=agent_id).all()
parsed_config = {
"agent_id": agent.id,
"name": agent.name,
"project_id": agent.project_id,
"description": agent.description,
"goal": [],
"instruction": [],
"agent_type": None,
"constraints": [],
"tools": [],
"exit": None,
"iteration_interval": None,
"model": None,
"permission_type": None,
"LTM_DB": None,
"memory_window": None,
"max_iterations": None,
"is_deleted": agent.is_deleted,
}
if not agent_configurations:
return parsed_config
for item in agent_configurations:
parsed_config[item.key] = cls.eval_agent_config(item.key, item.value)
return parsed_config
@classmethod
def eval_agent_config(cls, key, value):
"""
Evaluates the value of an agent configuration setting based on its key.
Args:
key (str): The key of the configuration setting.
value (str): The value of the configuration setting.
Returns:
object: The evaluated value of the configuration setting.
"""
if key in ["name", "description", "agent_type", "exit", "model", "permission_type", "LTM_DB", "resource_summary"]:
return value
elif key in ["project_id", "memory_window", "max_iterations", "iteration_interval"]:
return int(value)
elif key in ["goal", "constraints", "instruction", "is_deleted"]:
return eval(value)
elif key == "tools":
return [int(x) for x in json.loads(value)]
@classmethod
def create_agent_with_config(cls, db, agent_with_config):
"""
Creates a new agent with the provided configuration.
Args:
db: The database object.
agent_with_config: The object containing the agent and configuration details.
Returns:
Agent: The created agent.
"""
db_agent = Agent(name=agent_with_config.name, description=agent_with_config.description,
project_id=agent_with_config.project_id)
db.session.add(db_agent)
db.session.flush() # Flush pending changes to generate the agent's ID
db.session.commit()
if agent_with_config.agent_type == "Don't Maintain Task Queue":
agent_workflow = db.session.query(AgentWorkflow).filter(AgentWorkflow.name == "Goal Based Agent").first()
logger.info(agent_workflow)
db_agent.agent_workflow_id = agent_workflow.id
elif agent_with_config.agent_type == "Maintain Task Queue":
agent_workflow = db.session.query(AgentWorkflow).filter(
AgentWorkflow.name == "Task Queue Agent With Seed").first()
db_agent.agent_workflow_id = agent_workflow.id
elif agent_with_config.agent_type == "Fixed Task Queue":
agent_workflow = db.session.query(AgentWorkflow).filter(
AgentWorkflow.name == "Fixed Task Queue").first()
db_agent.agent_workflow_id = agent_workflow.id
db.session.commit()
# Create Agent Configuration
agent_config_values = {
"goal": agent_with_config.goal,
"instruction": agent_with_config.instruction,
"agent_type": agent_with_config.agent_type,
"constraints": agent_with_config.constraints,
"tools": agent_with_config.tools,
"exit": agent_with_config.exit,
"iteration_interval": agent_with_config.iteration_interval,
"model": agent_with_config.model,
"permission_type": agent_with_config.permission_type,
"LTM_DB": agent_with_config.LTM_DB,
"max_iterations": agent_with_config.max_iterations,
"user_timezone": agent_with_config.user_timezone
}
agent_configurations = [
AgentConfiguration(agent_id=db_agent.id, key=key, value=str(value))
for key, value in agent_config_values.items()
]
db.session.add_all(agent_configurations)
db.session.commit()
db.session.flush()
return db_agent
@classmethod
def create_agent_with_template_id(cls, db, project_id, agent_template):
"""
Creates a new agent using the provided agent template ID.
Args:
db: The database object.
project_id (int): The ID of the project.
agent_template: The agent template object.
Returns:
Agent: The created agent.
"""
db_agent = Agent(name=agent_template.name, description=agent_template.description,
project_id=project_id,
agent_workflow_id=agent_template.agent_workflow_id)
db.session.add(db_agent)
db.session.flush() # Flush pending changes to generate the agent's ID
db.session.commit()
configs = db.session.query(AgentTemplateConfig).filter(
AgentTemplateConfig.agent_template_id == agent_template.id).all()
agent_configurations = [
AgentConfiguration(
agent_id=db_agent.id, key=config.key, value=config.value
)
for config in configs
]
db.session.add_all(agent_configurations)
db.session.commit()
db.session.flush()
return db_agent
@classmethod
def create_agent_with_marketplace_template_id(cls, db, project_id, agent_template_id):
"""
Creates a new agent using the agent template ID from the marketplace.
Args:
db: The database object.
project_id (int): The ID of the project.
agent_template_id (int): The ID of the agent template from the marketplace.
Returns:
Agent: The created agent.
"""
agent_template = AgentTemplate.fetch_marketplace_detail(agent_template_id)
# we need to create agent workflow if not present. Add it once we get org id in agent workflow
db_agent = Agent(name=agent_template["name"], description=agent_template["description"],
project_id=project_id,
agent_workflow_id=agent_template["agent_workflow_id"])
db.session.add(db_agent)
db.session.flush() # Flush pending changes to generate the agent's ID
db.session.commit()
agent_configurations = [
AgentConfiguration(agent_id=db_agent.id, key=key, value=value["value"])
for key, value in agent_template["configs"].items()
]
db.session.add_all(agent_configurations)
db.session.commit()
db.session.flush()
return db_agent
def get_agent_organisation(self, session):
"""
Get the organization of the agent.
Args:
session: The database session.
Returns:
Organization: The organization of the agent.
"""
project = session.query(Project).filter(Project.id == self.project_id).first()
return session.query(Organisation).filter(Organisation.id == project.organisation_id).first()
@classmethod
def get_agent_from_id(cls, session, agent_id):
"""
Get Agent from agent_id
Args:
session: The database session.
agent_id(int) : Unique identifier of an Agent.
Returns:
Agent: Agent object is returned.
"""
return session.query(Agent).filter(Agent.id == agent_id).first()