Skip to content

Commit

Permalink
Merge pull request #834 from TransformerOptimus/iteration_workflow
Browse files Browse the repository at this point in the history
Iteration workflow
  • Loading branch information
I’m committed Aug 4, 2023
2 parents bb2f35e + ddb60ac commit 3266d8c
Show file tree
Hide file tree
Showing 86 changed files with 3,964 additions and 1,344 deletions.
4 changes: 3 additions & 1 deletion gui/pages/Content/Agents/ActionConsole.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ function ActionBox({action, index, denied, reasons, handleDeny, handleSelection,
<div key={action.id} className={styles.history_box}
style={{background: '#272335', padding: '16px', cursor: 'default'}}>
<div style={{display: 'flex', flexDirection: 'column'}}>
<div>Tool <b>{action.tool_name}</b> is seeking for Permissions</div>
{action.question && (<div className={styles.feed_title}>{action.question}</div>)}
{!action.question && (<div>Tool <b>{action.tool_name}</b> is seeking for Permissions</div>)}

{isDenied && (
<div style={{marginTop: '26px'}}>
<div>Provide Feedback <span style={{color: '#888888'}}>(Optional)</span></div>
Expand Down
23 changes: 21 additions & 2 deletions gui/pages/Content/Agents/AgentCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
getOrganisationConfig,
getLlmModels,
updateExecution,
uploadFile
uploadFile, getAgentWorkflows
} from "@/pages/api/DashboardService";
import {
formatBytes,
Expand Down Expand Up @@ -68,8 +68,12 @@ export default function AgentCreate({
const modelRef = useRef(null);
const [modelDropdown, setModelDropdown] = useState(false);

const agentTypes = ["Don't Maintain Task Queue", "Maintain Task Queue", "Fixed Task Queue"]
// const agentTypes = ["Goal Based Workflow",
// "Dynamic Task Workflow", "Fixed Task Workflow", "Sales Research Workflow", "SuperCoder", "DocSuperCoder", "Research & send email"]
// const agentTypes = ["Don't Maintain Task Queue", "Maintain Task Queue", "Fixed Task Queue"]
const [agentTypes, setAgentTypes] = useState('');
const [agentType, setAgentType] = useState(agentTypes[0]);

const agentRef = useRef(null);
const [agentDropdown, setAgentDropdown] = useState(false);

Expand Down Expand Up @@ -156,6 +160,21 @@ export default function AgentCreate({
console.error('Error fetching models:', error);
});

getAgentWorkflows()
.then((response) => {
const agentTypes = response.data || [];
const selectedAgentType = localStorage.getItem("agent_type_" + String(internalId)) || '';
setAgentTypes(agentTypes);
if (agentTypes.length > 0 && !selectedAgentType) {
setLocalStorageValue("agent_type_" + String(internalId), agentTypes[0], setAgentType);
} else {
setAgentType(selectedAgentType);
}
})
.catch((error) => {
console.error('Error fetching agent workflows:', error);
});

if (template !== null) {
setLocalStorageValue("agent_name_" + String(internalId), template.name, setAgentName);
setLocalStorageValue("agent_description_" + String(internalId), template.description, setAgentDescription);
Expand Down
4 changes: 2 additions & 2 deletions gui/pages/Content/Agents/AgentWorkspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export default function AgentWorkspace({env, agentId, agentName, selectedView, a
<div style={{width: '40%'}}>
<div className={styles.detail_top}>
<div style={{display: 'flex', overflowX: 'scroll'}}>
{agentDetails && agentDetails.permission_type.includes('RESTRICTED') && <div>
{agentDetails && pendingPermission > 0 && <div>
<button onClick={() => setRightPanel('action_console')} className={styles.tab_button}
style={rightPanel === 'action_console' ? {background: '#454254'} : {background: 'transparent'}}>
<Image style={{marginTop: '-1px'}} width={14} height={14} src="/images/action_console.svg"
Expand Down Expand Up @@ -465,7 +465,7 @@ export default function AgentWorkspace({env, agentId, agentName, selectedView, a
</div>
</div>
<div className={styles.detail_body} style={{paddingRight: '0'}}>
{rightPanel === 'action_console' && agentDetails && agentDetails?.permission_type !== 'God Mode' && (
{rightPanel === 'action_console' && agentDetails && pendingPermission > 0 && (
<div className={styles.detail_content}>
<ActionConsole key={JSON.stringify(fetchedData)} actions={fetchedData}
pendingPermission={pendingPermission} setPendingPermissions={setPendingPermissions}/>
Expand Down
4 changes: 4 additions & 0 deletions gui/pages/api/DashboardService.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ export const getLlmModels = () => {
return api.get(`organisations/llm_models`);
};

export const getAgentWorkflows = () => {
return api.get(`organisations/agent_workflows`);
};

export const fetchVectorDBList = () => {
return api.get(`/vector_dbs/get/list`);
};
Expand Down
211 changes: 41 additions & 170 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import os
import pickle
from datetime import datetime, timedelta

import requests
from fastapi import FastAPI, HTTPException, Depends, Request, status, Query
from fastapi.middleware.cors import CORSMiddleware
Expand All @@ -15,12 +11,9 @@
from sqlalchemy.orm import sessionmaker

import superagi
import urllib.parse
import json
import http.client as http_client
from superagi.helper.twitter_tokens import TwitterTokens
from datetime import datetime, timedelta
from datetime import timedelta, datetime
from superagi.agent.agent_prompt_builder import AgentPromptBuilder
from superagi.agent.workflow_seed import IterationWorkflowSeed, AgentWorkflowSeed
from superagi.config.config import get_config
from superagi.controllers.agent import router as agent_router
from superagi.controllers.agent_config import router as agent_config_router
Expand Down Expand Up @@ -51,16 +44,14 @@
from superagi.lib.logger import logger
from superagi.llms.google_palm import GooglePalm
from superagi.llms.openai import OpenAi
from superagi.helper.auth import get_current_user
from superagi.models.agent_workflow import AgentWorkflow
from superagi.models.agent_workflow_step import AgentWorkflowStep
from superagi.models.agent_template import AgentTemplate
from superagi.models.organisation import Organisation
from superagi.models.tool_config import ToolConfig
from superagi.models.toolkit import Toolkit
from superagi.models.oauth_tokens import OauthTokens
from superagi.models.types.login_request import LoginRequest
from superagi.models.types.validate_llm_api_key_request import ValidateAPIKeyRequest
from superagi.models.user import User
from superagi.models.workflows.agent_workflow import AgentWorkflow
from superagi.models.workflows.iteration_workflow import IterationWorkflow
from superagi.models.workflows.iteration_workflow_step import IterationWorkflowStep

app = FastAPI()

Expand Down Expand Up @@ -161,6 +152,26 @@ def authjwt_exception_handler(request: Request, exc: AuthJWTException):
)


def replace_old_iteration_workflows(session):
dateTimeObj = datetime.strptime("4-August-2023", "%d-%B-%Y")
templates = session.query(AgentTemplate).filter(AgentTemplate.created_at <= dateTimeObj).all()
for template in templates:
iter_workflow = IterationWorkflow.find_by_id(session, template.agent_workflow_id)
if iter_workflow.name == "Fixed Task Queue":
agent_workflow = AgentWorkflow.find_by_name(session, "Fixed Task Workflow")
template.agent_workflow_id = agent_workflow.id
session.commit()

if iter_workflow.name == "Maintain Task Queue":
agent_workflow = AgentWorkflow.find_by_name(session, "Dynamic Task Workflow")
template.agent_workflow_id = agent_workflow.id
session.commit()

if iter_workflow.name == "Don't Maintain Task Queue" or iter_workflow.name == "Goal Based Agent":
agent_workflow = AgentWorkflow.find_by_name(session, "Goal Based Workflow")
template.agent_workflow_id = agent_workflow.id
session.commit()

@app.on_event("startup")
async def startup_event():
# Perform startup tasks here
Expand All @@ -174,158 +185,6 @@ async def startup_event():
logger.info(organisation)
register_toolkits(session, organisation)

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

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(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "gb1").first()
output = AgentPromptBuilder.get_super_agi_single_prompt()
if first_step is None:
first_step = AgentWorkflowStep(unique_id="gb1",
prompt=output["prompt"], variables=str(output["variables"]),
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 with only valid JSON conforming to the above schema")
session.add(first_step)
session.commit()
else:
first_step.prompt = output["prompt"]
first_step.variables = str(output["variables"])
first_step.output_type = "tools"
first_step.completion_prompt = "Determine which next tool to use,and respond with only valid JSON conforming to the above schema"
session.commit()
first_step.next_step_id = first_step.id
session.commit()

def build_task_based_agents():
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()

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:
workflow_step1.prompt = output["prompt"]
workflow_step1.variables = str(output["variables"])
workflow_step1.output_type = "tasks"
session.commit()

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

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

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

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

workflow_step4 = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "tb4").first()
output = AgentPromptBuilder.prioritize_tasks()
if workflow_step4 is None:
workflow_step4 = AgentWorkflowStep(unique_id="tb4",
prompt=output["prompt"], variables=str(output["variables"]),
step_type="NORMAL",
agent_workflow_id=agent_workflow.id, next_step_id=-1,
output_type="replace_tasks")

session.add(workflow_step4)
else:
workflow_step4.prompt = output["prompt"]
workflow_step4.variables = str(output["variables"])
workflow_step4.output_type = "replace_tasks"
session.commit()
session.commit()
workflow_step1.next_step_id = workflow_step3.id
workflow_step3.next_step_id = workflow_step2.id
workflow_step2.next_step_id = workflow_step4.id
workflow_step4.next_step_id = workflow_step3.id
session.commit()

def build_action_based_agents():
agent_workflow = session.query(AgentWorkflow).filter(AgentWorkflow.name == "Fixed Task Queue").first()
if agent_workflow is None:
agent_workflow = AgentWorkflow(name="Fixed Task Queue", description="Fixed Task Queue")
session.add(agent_workflow)
session.commit()

output = AgentPromptBuilder.start_task_based()

workflow_step1 = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "ab1").first()
if workflow_step1 is None:
workflow_step1 = AgentWorkflowStep(unique_id="ab1",
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:
workflow_step1.prompt = output["prompt"]
workflow_step1.variables = str(output["variables"])
workflow_step1.output_type = "tasks"
workflow_step1.agent_workflow_id = agent_workflow.id
session.commit()

workflow_step2 = session.query(AgentWorkflowStep).filter(AgentWorkflowStep.unique_id == "ab2").first()
output = AgentPromptBuilder.analyse_task()
if workflow_step2 is None:
workflow_step2 = AgentWorkflowStep(unique_id="ab2",
prompt=output["prompt"], variables=str(output["variables"]),
step_type="NORMAL",
agent_workflow_id=agent_workflow.id, next_step_id=-1,
output_type="tools")
session.add(workflow_step2)
else:
workflow_step2.prompt = output["prompt"]
workflow_step2.variables = str(output["variables"])
workflow_step2.output_type = "tools"
workflow_step2.agent_workflow_id = agent_workflow.id
session.commit()

session.commit()
workflow_step1.next_step_id = workflow_step2.id
workflow_step2.next_step_id = workflow_step2.id
session.commit()

def register_toolkit_for_all_organisation():
organizations = session.query(Organisation).all()
for organization in organizations:
Expand All @@ -339,9 +198,21 @@ def register_toolkit_for_master_organisation():
if marketplace_organisation is not None:
register_marketplace_toolkits(session, marketplace_organisation)

build_single_step_agent()
build_task_based_agents()
build_action_based_agents()
IterationWorkflowSeed.build_single_step_agent(session)
IterationWorkflowSeed.build_task_based_agents(session)
IterationWorkflowSeed.build_action_based_agents(session)
IterationWorkflowSeed.build_initialize_task_workflow(session)

AgentWorkflowSeed.build_goal_based_agent(session)
AgentWorkflowSeed.build_task_based_agent(session)
AgentWorkflowSeed.build_fixed_task_based_agent(session)
AgentWorkflowSeed.build_sales_workflow(session)
AgentWorkflowSeed.build_recruitment_workflow(session)
AgentWorkflowSeed.build_coding_workflow(session)
# AgentWorkflowSeed.doc_search_and_code(session)
# AgentWorkflowSeed.build_research_email_workflow(session)
replace_old_iteration_workflows(session)

if env != "PROD":
register_toolkit_for_all_organisation()
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""add feed group id to execution and feed
Revision ID: 2fbd6472112c
Revises: 5184645e9f12
Create Date: 2023-08-01 17:09:16.183863
"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '2fbd6472112c'
down_revision = '5184645e9f12'
branch_labels = None
depends_on = None


def upgrade() -> None:
op.add_column('agent_executions',
sa.Column('current_feed_group_id', sa.String(), nullable=True, server_default="DEFAULT"))
op.add_column('agent_execution_feeds', sa.Column('feed_group_id', sa.String(), nullable=True))


def downgrade() -> None:
op.drop_column('agent_executions', 'current_feed_group_id')
op.drop_column('agent_execution_feeds', 'feed_group_id')
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""add question to agent execution permission
Revision ID: 5184645e9f12
Revises: 9419b3340af7
Create Date: 2023-07-21 08:16:14.702389
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '5184645e9f12'
down_revision = '9419b3340af7'
branch_labels = None
depends_on = None


def upgrade() -> None:
op.add_column('agent_execution_permissions', sa.Column('question', sa.Text(), nullable=True))


def downgrade() -> None:
op.drop_column('agent_execution_permissions', "question")
Loading

0 comments on commit 3266d8c

Please sign in to comment.