Skip to content

Commit

Permalink
renaming agent templates to agent workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
TransformerOptimus committed Jun 10, 2023
1 parent a03c337 commit a8172cf
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 105 deletions.
86 changes: 43 additions & 43 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
from superagi.controllers.tool import router as tool_router
from superagi.controllers.user import router as user_router
from superagi.controllers.config import router as config_router
from superagi.models.agent_template import AgentTemplate
from superagi.models.agent_template_step import AgentTemplateStep
from superagi.models.agent_workflow import AgentWorkflow
from superagi.models.agent_workflow_step import AgentWorkflowStep
from superagi.models.organisation import Organisation
from superagi.models.tool import Tool
from superagi.models.types.login_request import LoginRequest
Expand Down Expand Up @@ -209,21 +209,21 @@ def process_files(folder_path):
class_name=tool.class_name)

def build_single_step_agent():
agent_template = session.query(AgentTemplate).filter(AgentTemplate.name == "Goal Based Agent").first()
agent_workflow = session.query(AgentWorkflow).filter(AgentWorkflow.name == "Goal Based Agent").first()

if agent_template is None:
agent_template = AgentTemplate(name="Goal Based Agent", description="Goal based agent")
session.add(agent_template)
if agent_workflow is None:
agent_workflow = AgentWorkflow(name="Goal Based Agent", description="Goal based agent")
session.add(agent_workflow)
session.commit()

# step will have a prompt
# output of step is either tasks or set commands
first_step = session.query(AgentTemplateStep).filter(AgentTemplateStep.unique_id == "gb1").first()
first_step = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "gb1").first()
output = AgentPromptBuilder.get_super_agi_single_prompt()
if first_step is None:
first_step = AgentTemplateStep(unique_id="gb1",
first_step = AgentWorkflowStep(unique_id="gb1",
prompt=output["prompt"], variables=str(output["variables"]),
agent_template_id=agent_template.id, output_type="tools",
agent_workflow_id=agent_workflow.id, output_type="tools",
step_type="TRIGGER",
history_enabled=True,
completion_prompt= "Determine which next tool to use, and respond using the format specified above:")
Expand All @@ -239,63 +239,63 @@ def build_single_step_agent():
session.commit()

def build_task_based_agents():
agent_template = session.query(AgentTemplate).filter(AgentTemplate.name == "Task Queue Agent With Seed").first()
if agent_template is None:
agent_template = AgentTemplate(name="Task Queue Agent With Seed", description="Task queue based agent")
session.add(agent_template)
agent_workflow = session.query(AgentWorkflow).filter(AgentWorkflow.name == "Task Queue Agent With Seed").first()
if agent_workflow is None:
agent_workflow = AgentWorkflow(name="Task Queue Agent With Seed", description="Task queue based agent")
session.add(agent_workflow)
session.commit()

output = AgentPromptBuilder.start_task_based()

template_step1 = session.query(AgentTemplateStep).filter(AgentTemplateStep.unique_id == "tb1").first()
if template_step1 is None:
template_step1 = AgentTemplateStep(unique_id="tb1",
prompt=output["prompt"], variables=str(output["variables"]),
step_type="TRIGGER",
agent_template_id=agent_template.id, next_step_id=-1,
output_type="tasks")
session.add(template_step1)
workflow_step1 = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "tb1").first()
if workflow_step1 is None:
workflow_step1 = AgentWorkflowStep(unique_id="tb1",
prompt=output["prompt"], variables=str(output["variables"]),
step_type="TRIGGER",
agent_workflow_id=agent_workflow.id, next_step_id=-1,
output_type="tasks")
session.add(workflow_step1)
else:
template_step1.prompt=output["prompt"]
template_step1.variables=str(output["variables"])
template_step1.output_type="tasks"
workflow_step1.prompt=output["prompt"]
workflow_step1.variables=str(output["variables"])
workflow_step1.output_type="tasks"
session.commit()

template_step2 = session.query(AgentTemplateStep).filter(AgentTemplateStep.unique_id == "tb2").first()
workflow_step2 = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "tb2").first()
output = AgentPromptBuilder.create_tasks()
if template_step2 is None:
template_step2 = AgentTemplateStep(unique_id="tb2",
if workflow_step2 is None:
workflow_step2 = AgentWorkflowStep(unique_id="tb2",
prompt=output["prompt"], variables=str(output["variables"]),
step_type="NORMAL",
agent_template_id=agent_template.id, next_step_id=-1,
agent_workflow_id=agent_workflow.id, next_step_id=-1,
output_type="tasks")
session.add(template_step2)
session.add(workflow_step2)
else:
template_step2.prompt=output["prompt"]
template_step2.variables=str(output["variables"])
template_step2.output_type="tasks"
workflow_step2.prompt=output["prompt"]
workflow_step2.variables=str(output["variables"])
workflow_step2.output_type="tasks"
session.commit()

template_step3 = session.query(AgentTemplateStep).filter(AgentTemplateStep.unique_id == "tb3").first()
workflow_step3 = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "tb3").first()

output = AgentPromptBuilder.analyse_task()
if template_step3 is None:
template_step3 = AgentTemplateStep(unique_id="tb3",
if workflow_step3 is None:
workflow_step3 = AgentWorkflowStep(unique_id="tb3",
prompt=output["prompt"], variables=str(output["variables"]),
step_type="NORMAL",
agent_template_id=agent_template.id, next_step_id=-1, output_type="tools")
agent_workflow_id=agent_workflow.id, next_step_id=-1, output_type="tools")

session.add(template_step3)
session.add(workflow_step3)
else:
template_step3.prompt=output["prompt"]
template_step3.variables=str(output["variables"])
template_step3.output_type="tools"
workflow_step3.prompt=output["prompt"]
workflow_step3.variables=str(output["variables"])
workflow_step3.output_type="tools"
session.commit()

session.commit()
template_step1.next_step_id = template_step3.id
template_step3.next_step_id = template_step2.id
template_step2.next_step_id = template_step3.id
workflow_step1.next_step_id = workflow_step3.id
workflow_step3.next_step_id = workflow_step2.id
workflow_step2.next_step_id = workflow_step3.id
session.commit()

build_single_step_agent()
Expand Down
34 changes: 34 additions & 0 deletions migrations/versions/d9b3436197eb_renaming_templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""renaming templates
Revision ID: d9b3436197eb
Revises: 3356a2f89a33
Create Date: 2023-06-10 09:28:28.262705
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'd9b3436197eb'
down_revision = '3356a2f89a33'
branch_labels = None
depends_on = None


def upgrade() -> None:
op.rename_table('agent_templates', 'agent_workflows')
op.rename_table('agent_template_steps', 'agent_workflow_steps')
with op.batch_alter_table('agent_workflow_steps') as bop:
bop.alter_column('agent_template_id', new_column_name='agent_workflow_id')
with op.batch_alter_table('agents') as bop:
bop.alter_column('agent_template_id', new_column_name='agent_workflow_id')


def downgrade() -> None:
op.rename_table('agent_workflows', 'agent_templates')
op.rename_table('agent_workflow_steps', 'agent_template_steps')
with op.batch_alter_table('agent_templates') as bop:
bop.alter_column('agent_workflow_id', new_column_name='agent_template_id')
with op.batch_alter_table('agents') as bop:
bop.alter_column('agent_workflow_id', new_column_name='agent_template_id')
18 changes: 9 additions & 9 deletions superagi/agent/super_agi.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from superagi.models.agent_execution import AgentExecution
# from superagi.models.types.agent_with_config import AgentWithConfig
from superagi.models.agent_execution_feed import AgentExecutionFeed
from superagi.models.agent_template_step import AgentTemplateStep
from superagi.models.agent_workflow_step import AgentWorkflowStep
from superagi.models.db import connect_db
from superagi.tools.base_tool import BaseTool
from superagi.types.common import BaseMessage, HumanMessage, AIMessage, SystemMessage
Expand Down Expand Up @@ -112,7 +112,7 @@ def split_history(self, history: List, pending_token_limit: int) -> Tuple[List[B
return [], history


def execute(self, template_step: AgentTemplateStep):
def execute(self, workflow_step: AgentWorkflowStep):
session = Session()
agent_execution_id = self.agent_config["agent_execution_id"]
task_queue = TaskQueue(str(agent_execution_id))
Expand All @@ -125,8 +125,8 @@ def execute(self, template_step: AgentTemplateStep):
messages = []
max_token_limit = 600
# adding history to the messages
if template_step.history_enabled:
prompt = self.build_agent_prompt(template_step.prompt, task_queue=task_queue, max_token_limit=max_token_limit)
if workflow_step.history_enabled:
prompt = self.build_agent_prompt(workflow_step.prompt, task_queue=task_queue, max_token_limit=max_token_limit)
messages.append({"role": "system", "content": prompt})
messages.append({"role": "system", "content": f"The current time and date is {time.strftime('%c')}"})
base_token_limit = TokenCounter.count_message_tokens(messages, self.llm.get_model())
Expand All @@ -135,9 +135,9 @@ def execute(self, template_step: AgentTemplateStep):
token_limit - base_token_limit - max_token_limit)
for history in current_messages:
messages.append({"role": history["role"], "content": history["content"]})
messages.append({"role": "user", "content": template_step.completion_prompt})
messages.append({"role": "user", "content": workflow_step.completion_prompt})
else:
prompt = self.build_agent_prompt(template_step.prompt, task_queue=task_queue, max_token_limit=max_token_limit)
prompt = self.build_agent_prompt(workflow_step.prompt, task_queue=task_queue, max_token_limit=max_token_limit)
messages.append({"role": "system", "content": prompt})
# agent_execution_feed = AgentExecutionFeed(agent_execution_id=self.agent_config["agent_execution_id"],
# agent_id=self.agent_config["agent_id"], feed=template_step.prompt,
Expand Down Expand Up @@ -165,7 +165,7 @@ def execute(self, template_step: AgentTemplateStep):

final_response = {"result": "PENDING", "retry": False}

if template_step.output_type == "tools":
if workflow_step.output_type == "tools":
agent_execution_feed = AgentExecutionFeed(agent_execution_id=self.agent_config["agent_execution_id"],
agent_id=self.agent_config["agent_id"], feed=assistant_reply,
role="assistant")
Expand All @@ -178,7 +178,7 @@ def execute(self, template_step: AgentTemplateStep):
session.add(agent_execution_feed)
final_response = tool_response
final_response["pending_task_count"] = len(task_queue.get_tasks())
elif template_step.output_type == "tasks":
elif workflow_step.output_type == "tasks":
tasks = eval(assistant_reply)
for task in reversed(tasks):
task_queue.add_task(task)
Expand All @@ -196,7 +196,7 @@ def execute(self, template_step: AgentTemplateStep):
else:
final_response = {"result": "PENDING", "pending_task_count": len(current_tasks)}

if template_step.output_type == "tools" and final_response["retry"] == False:
if workflow_step.output_type == "tools" and final_response["retry"] == False:
task_queue.complete_task(final_response["result"])
current_tasks = task_queue.get_tasks()
if len(current_tasks) > 0 and final_response["result"] == "COMPLETE":
Expand Down
14 changes: 7 additions & 7 deletions superagi/controllers/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from fastapi import APIRouter
from pydantic_sqlalchemy import sqlalchemy_to_pydantic

from superagi.models.agent_template import AgentTemplate
from superagi.models.agent_workflow import AgentWorkflow
from superagi.models.types.agent_with_config import AgentWithConfig
from superagi.models.agent_config import AgentConfiguration
from superagi.models.agent_execution import AgentExecution
Expand Down Expand Up @@ -94,12 +94,12 @@ def create_agent_with_config(agent_with_config: AgentWithConfig,
db.session.commit()

if agent_with_config.agent_type == "Don't Maintain Task Queue":
agent_template = db.session.query(AgentTemplate).filter(AgentTemplate.name=="Goal Based Agent").first()
print(agent_template)
db_agent.agent_template_id = agent_template.id
agent_workflow = db.session.query(AgentWorkflow).filter(AgentWorkflow.name == "Goal Based Agent").first()
print(agent_workflow)
db_agent.agent_workflow_id = agent_workflow.id
elif agent_with_config.agent_type == "Maintain Task Queue":
agent_template = db.session.query(AgentTemplate).filter(AgentTemplate.name=="Task Queue Agent With Seed").first()
db_agent.agent_template_id = agent_template.id
agent_workflow = db.session.query(AgentWorkflow).filter(AgentWorkflow.name == "Task Queue Agent With Seed").first()
db_agent.agent_workflow_id = agent_workflow.id
db.session.commit()


Expand All @@ -126,7 +126,7 @@ def create_agent_with_config(agent_with_config: AgentWithConfig,
]

db.session.add_all(agent_configurations)
start_step_id = AgentTemplate.fetch_trigger_step_id(db.session, db_agent.agent_template_id)
start_step_id = AgentWorkflow.fetch_trigger_step_id(db.session, db_agent.agent_workflow_id)
# Creating an execution with CREATED status
execution = AgentExecution(status='RUNNING', last_execution_time=datetime.now(), agent_id=db_agent.id,
name="New Run", current_step_id=start_step_id)
Expand Down
4 changes: 2 additions & 2 deletions superagi/controllers/agent_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from fastapi import HTTPException, Depends
from fastapi_jwt_auth import AuthJWT

from superagi.models.agent_template import AgentTemplate
from superagi.models.agent_workflow import AgentWorkflow
from superagi.worker import execute_agent
from superagi.models.agent_execution import AgentExecution
from superagi.models.agent import Agent
Expand All @@ -25,7 +25,7 @@ def create_agent_execution(agent_execution: sqlalchemy_to_pydantic(AgentExecutio

if not agent:
raise HTTPException(status_code=404, detail="Agent not found")
start_step_id = AgentTemplate.fetch_trigger_step_id(db.session, agent.agent_template_id)
start_step_id = AgentWorkflow.fetch_trigger_step_id(db.session, agent.agent_workflow_id)
db_agent_execution = AgentExecution(status="RUNNING", last_execution_time=datetime.now(),
agent_id=agent_execution.agent_id, name=agent_execution.name, num_of_calls=0,
num_of_tokens=0,
Expand Down
Loading

0 comments on commit a8172cf

Please sign in to comment.