Skip to content

Commit

Permalink
[FEATS][Tools]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Apr 12, 2024
1 parent 20c8334 commit 6c83400
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 42 deletions.
6 changes: 3 additions & 3 deletions Swarm Orchestrator_state.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"agent_id": "<function agent_id at 0x1303e2980>",
"agent_id": "<function agent_id at 0x12e11e700>",
"agent_name": "Swarm Orchestrator",
"agent_description": null,
"system_prompt": "Create an instruction prompt for an swarm orchestrator to create a series of personalized, agents for the following objective: Create a self-driving car system using a team of AI agents to decompose a very complicated problem or tasks, the orchestrator is the team leader. Teach the orchestrator how to decompose the tasks to very certain agents with names, and system prompts, we need the plan, with a step by stpe instructions, number of agents, and a list of agents with a name, system prompt for each, and then the rules of the swarm, compact the prompt, and say only return JSON data in markdown and nothing else.Follow the schema here: \n{\n \"plan\": [\"Step 1\", \"Step 2\", \"Step 3\"],\n \"agents\": [\n {\n \"name\": \"Agent 1\",\n \"system_prompt\": \"Prompt 1\"\n },\n {\n \"name\": \"Agent 2\",\n \"system_prompt\": \"Prompt 2\"\n }\n ]\n}\n *############ Here are some examples:\n{\n \"plan\": [\"Room Management\", \"Guest Services\", \"Reservations Handling\", \"Facility Maintenance\", \"Staff Coordination\"],\n \"agents\": [\n {\n \"name\": \"Room Management Agent\",\n \"system_prompt\": \"Automate room assignments, minibar restocking, and housekeeping schedules\"\n },\n {\n \"name\": \"Guest Services Agent\",\n \"system_prompt\": \"Handle check-ins, check-outs, guest requests, and complaints efficiently\"\n },\n {\n \"name\": \"Reservations Agent\",\n \"system_prompt\": \"Manage room bookings, table reservations, and special requests\"\n },\n {\n \"name\": \"Maintenance Agent\",\n \"system_prompt\": \"Schedule and track maintenance tasks for facilities and rooms\"\n },\n {\n \"name\": \"Staff Coordination Agent\",\n \"system_prompt\": \"Optimize staff schedules, task assignments, and workload distribution\"\n }\n ]\n}\n and another example\n{\n \"plan\": [\"Problem Identification\", \"Solution Design\", \"Implementation\", \"Testing\", \"Deployment\"],\n \"agents\": [\n {\n \"name\": \"Identification Agent\",\n \"system_prompt\": \"Identify the problem\"\n },\n {\n \"name\": \"Design Agent\",\n \"system_prompt\": \"Design the solution\"\n },\n {\n \"name\": \"Implementation Agent\",\n \"system_prompt\": \"Implement the solution\"\n },\n {\n \"name\": \"Deployment Agent\",\n \"system_prompt\": \"Deploy the solution\"\n }\n ]\n}\n ",
"system_prompt": "Create an instruction prompt for an swarm orchestrator to create a series of personalized, agents for the following objective: what is the current squad of Ukrainian football national team? Who and where are they playing soon? to decompose a very complicated problem or tasks, the orchestrator is the team leader. Teach the orchestrator how to decompose the tasks to very certain agents with names, and system prompts, we need the plan, with a step by stpe instructions, number of agents, and a list of agents with a name, system prompt for each, and then the rules of the swarm, compact the prompt, and say only return JSON data in markdown and nothing else.Follow the schema here: \n{\n \"plan\": [\"Step 1\", \"Step 2\", \"Step 3\"],\n \"agents\": [\n {\n \"name\": \"Agent 1\",\n \"system_prompt\": \"Prompt 1\"\n },\n {\n \"name\": \"Agent 2\",\n \"system_prompt\": \"Prompt 2\"\n }\n ]\n}\n *############ Here are some examples:\n{\n \"plan\": [\"Room Management\", \"Guest Services\", \"Reservations Handling\", \"Facility Maintenance\", \"Staff Coordination\"],\n \"agents\": [\n {\n \"name\": \"Room Management Agent\",\n \"system_prompt\": \"Automate room assignments, minibar restocking, and housekeeping schedules\"\n },\n {\n \"name\": \"Guest Services Agent\",\n \"system_prompt\": \"Handle check-ins, check-outs, guest requests, and complaints efficiently\"\n },\n {\n \"name\": \"Reservations Agent\",\n \"system_prompt\": \"Manage room bookings, table reservations, and special requests\"\n },\n {\n \"name\": \"Maintenance Agent\",\n \"system_prompt\": \"Schedule and track maintenance tasks for facilities and rooms\"\n },\n {\n \"name\": \"Staff Coordination Agent\",\n \"system_prompt\": \"Optimize staff schedules, task assignments, and workload distribution\"\n }\n ]\n}\n and another example\n{\n \"plan\": [\"Problem Identification\", \"Solution Design\", \"Implementation\", \"Testing\", \"Deployment\"],\n \"agents\": [\n {\n \"name\": \"Identification Agent\",\n \"system_prompt\": \"Identify the problem\"\n },\n {\n \"name\": \"Design Agent\",\n \"system_prompt\": \"Design the solution\"\n },\n {\n \"name\": \"Implementation Agent\",\n \"system_prompt\": \"Implement the solution\"\n },\n {\n \"name\": \"Deployment Agent\",\n \"system_prompt\": \"Deploy the solution\"\n }\n ]\n}\n ",
"sop": null,
"short_memory": "system: Create an instruction prompt for an swarm orchestrator to create a series of personalized, agents for the following objective: Create a self-driving car system using a team of AI agents to decompose a very complicated problem or tasks, the orchestrator is the team leader. Teach the orchestrator how to decompose the tasks to very certain agents with names, and system prompts, we need the plan, with a step by stpe instructions, number of agents, and a list of agents with a name, system prompt for each, and then the rules of the swarm, compact the prompt, and say only return JSON data in markdown and nothing else.Follow the schema here: \n{\n \"plan\": [\"Step 1\", \"Step 2\", \"Step 3\"],\n \"agents\": [\n {\n \"name\": \"Agent 1\",\n \"system_prompt\": \"Prompt 1\"\n },\n {\n \"name\": \"Agent 2\",\n \"system_prompt\": \"Prompt 2\"\n }\n ]\n}\n *############ Here are some examples:\n{\n \"plan\": [\"Room Management\", \"Guest Services\", \"Reservations Handling\", \"Facility Maintenance\", \"Staff Coordination\"],\n \"agents\": [\n {\n \"name\": \"Room Management Agent\",\n \"system_prompt\": \"Automate room assignments, minibar restocking, and housekeeping schedules\"\n },\n {\n \"name\": \"Guest Services Agent\",\n \"system_prompt\": \"Handle check-ins, check-outs, guest requests, and complaints efficiently\"\n },\n {\n \"name\": \"Reservations Agent\",\n \"system_prompt\": \"Manage room bookings, table reservations, and special requests\"\n },\n {\n \"name\": \"Maintenance Agent\",\n \"system_prompt\": \"Schedule and track maintenance tasks for facilities and rooms\"\n },\n {\n \"name\": \"Staff Coordination Agent\",\n \"system_prompt\": \"Optimize staff schedules, task assignments, and workload distribution\"\n }\n ]\n}\n and another example\n{\n \"plan\": [\"Problem Identification\", \"Solution Design\", \"Implementation\", \"Testing\", \"Deployment\"],\n \"agents\": [\n {\n \"name\": \"Identification Agent\",\n \"system_prompt\": \"Identify the problem\"\n },\n {\n \"name\": \"Design Agent\",\n \"system_prompt\": \"Design the solution\"\n },\n {\n \"name\": \"Implementation Agent\",\n \"system_prompt\": \"Implement the solution\"\n },\n {\n \"name\": \"Deployment Agent\",\n \"system_prompt\": \"Deploy the solution\"\n }\n ]\n}\n \n\n\nHuman:: Create a self-driving car system using a team of AI agents\n\n\nSwarm Orchestrator: \n```json\n{\n \"plan\": [\"Identify driving environment\", \"Interpret sensor data\", \"Calculate navigation path\", \"Execute driving maneuvers\"],\n \"agents\": [\n {\n \"name\": \"Perception Agent\",\n \"system_prompt\": \"Analyze visual data from cameras and LiDAR to identity orientation, location of roads, obstacles\"\n },\n {\n \"name\": \"Planning Agent\",\n \"system_prompt\": \"Plan optimal driving path and maneuvers based on traffic, routes and safety\"\n }, \n {\n \"name\": \"Control Agent\",\n \"system_prompt\": \"Execute driving by controlling acceleration, brakes and steering\"\n },\n {\n \"name\": \"Monitoring Agent\", \n \"system_prompt\": \"Continuously monitor vehicle health, mechanics and fail-safes\"\n }\n ]\n}\n```\n\nI've structured this to decompose the self-driving functionality into 4 key agents - Perception, Planning, Control and Monitoring. Each handles a distinct aspect of autonomous driving with a specific system prompt for its role. The overall plan captures the high-level workflow to incorporate the agents into an effective system. Let me know if you need any modifications to this!\n\n",
"short_memory": "system: Create an instruction prompt for an swarm orchestrator to create a series of personalized, agents for the following objective: what is the current squad of Ukrainian football national team? Who and where are they playing soon? to decompose a very complicated problem or tasks, the orchestrator is the team leader. Teach the orchestrator how to decompose the tasks to very certain agents with names, and system prompts, we need the plan, with a step by stpe instructions, number of agents, and a list of agents with a name, system prompt for each, and then the rules of the swarm, compact the prompt, and say only return JSON data in markdown and nothing else.Follow the schema here: \n{\n \"plan\": [\"Step 1\", \"Step 2\", \"Step 3\"],\n \"agents\": [\n {\n \"name\": \"Agent 1\",\n \"system_prompt\": \"Prompt 1\"\n },\n {\n \"name\": \"Agent 2\",\n \"system_prompt\": \"Prompt 2\"\n }\n ]\n}\n *############ Here are some examples:\n{\n \"plan\": [\"Room Management\", \"Guest Services\", \"Reservations Handling\", \"Facility Maintenance\", \"Staff Coordination\"],\n \"agents\": [\n {\n \"name\": \"Room Management Agent\",\n \"system_prompt\": \"Automate room assignments, minibar restocking, and housekeeping schedules\"\n },\n {\n \"name\": \"Guest Services Agent\",\n \"system_prompt\": \"Handle check-ins, check-outs, guest requests, and complaints efficiently\"\n },\n {\n \"name\": \"Reservations Agent\",\n \"system_prompt\": \"Manage room bookings, table reservations, and special requests\"\n },\n {\n \"name\": \"Maintenance Agent\",\n \"system_prompt\": \"Schedule and track maintenance tasks for facilities and rooms\"\n },\n {\n \"name\": \"Staff Coordination Agent\",\n \"system_prompt\": \"Optimize staff schedules, task assignments, and workload distribution\"\n }\n ]\n}\n and another example\n{\n \"plan\": [\"Problem Identification\", \"Solution Design\", \"Implementation\", \"Testing\", \"Deployment\"],\n \"agents\": [\n {\n \"name\": \"Identification Agent\",\n \"system_prompt\": \"Identify the problem\"\n },\n {\n \"name\": \"Design Agent\",\n \"system_prompt\": \"Design the solution\"\n },\n {\n \"name\": \"Implementation Agent\",\n \"system_prompt\": \"Implement the solution\"\n },\n {\n \"name\": \"Deployment Agent\",\n \"system_prompt\": \"Deploy the solution\"\n }\n ]\n}\n \n\n\nHuman:: what is the current squad of Ukrainian football national team? Who and where are they playing soon?\n\n\nSwarm Orchestrator: \n```json\n{\n \"plan\": [\n \"Research current Ukrainian national football team roster\",\n \"Identify upcoming matches and opponents\", \n \"Research player backgrounds and current club teams\"\n ],\n \"agents\": [\n {\n \"name\": \"Roster Agent\",\n \"system_prompt\": \"Provide the names and positions of the players currently on the Ukraine national football team roster.\"\n },\n { \n \"name\": \"Matches Agent\",\n \"system_prompt\": \"List the dates, opponents, locations and tournaments for the upcoming matches involving the Ukrainian national team.\"\n },\n {\n \"name\": \"Player Background Agent\", \n \"system_prompt\": \"For each player currently on the Ukraine roster, provide their date of birth, height, current club team and a brief bio.\"\n }\n ]\n}\n```\n\nThis breaks down the overall objective into three key subtasks, each handled by a specialized agent. The Roster Agent identifies the current players, the Matches Agent provides the upcoming schedule, and the Player Background Agent gives background details on each national team member. The plan shows the high-level workflow and agents allow delegation of distinct aspects.\n\n",
"loop_interval": 0,
"retry_attempts": 3,
"retry_interval": 1,
Expand Down
3 changes: 2 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Run the swarm
out = run_swarm(
"Create a self-driving car system using a team of AI agents"
"what is the current squad of Ukrainian football national team?"
" Who and where are they playing soon?"
)
print(out)
50 changes: 12 additions & 38 deletions neo_sapiens/hass_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
boss_sys_prompt,
)
from loguru import logger

# from neo_sapiens.chroma_db_s import ChromaDB
from neo_sapiens.tools_preset import terminal, browser

# Load environment variables
load_dotenv()
Expand Down Expand Up @@ -120,7 +119,6 @@ def parse_json_from_input(input_str):
json_pattern = re.compile(r"```json\n(.*?)\n```", re.DOTALL)
match = json_pattern.search(input_str)
json_str = match.group(1).strip() if match else input_str.strip()
# logger.info(json_str)

# Attempt to parse the JSON string
try:
Expand Down Expand Up @@ -194,7 +192,6 @@ def create_agents(
f"Creating agent: {name} with system prompt:"
f" {system_prompt}"
)
logger.info("\n")

out = Agent(
agent_name=name,
Expand All @@ -207,6 +204,7 @@ def create_agents(
dashboard=False,
verbose=True,
stopping_token="<DONE>",
tools=[browser, terminal],
)

network.add_agent(out)
Expand All @@ -217,7 +215,7 @@ def create_agents(

def print_agent_names(agents: list):
for agent in agents:
logger.info(agent.name)
logger.info(f"Agent Name: {agent.agent_name}")


@tool
Expand All @@ -238,20 +236,6 @@ def send_task_to_network_agent(name: str, task: str):
return out


# out = create_agents(agents)
# # logger.info(out)

# # # Use network
# # list_agents = network.list_agents()
# # logger.info(list_agents)

# # Run the workflow on a task
# run = network.run_single_agent(
# agent2.id, "What's your name?"
# )
# logger.info(out)


def master_creates_agents(task: str, *args, **kwargs):
"""
Master function to create agents based on a task.
Expand All @@ -277,8 +261,6 @@ def master_creates_agents(task: str, *args, **kwargs):
dashboard=False,
verbose=True,
stopping_token="<DONE>",
# interactive=True,
# long_term_memory=memory,
*args,
**kwargs,
)
Expand All @@ -303,24 +285,23 @@ def master_creates_agents(task: str, *args, **kwargs):
)

# Task 1: Run the agent and parse the output
logger.info("Creating the workers ...")
out = agent.run(str(task))
json_agents = out
logger.info(f"Output: {out}")
# logger.info(f"Output: {out}")
out = parse_json_from_input(out)
logger.info(str(out))
json_agentic_output = out
# logger.info(str(out))
plan, agents = out

# Task 2: Print agent names and create agents
logger.info(agents)
logger.info(print_agent_names(agents))
# logger.info(agents)
# logger.info("Creating agents...")
agents = create_agents(agents)
logger.info(agents)
print(type(agents))

# Send JSON of agents to boss
boss.add_message_to_memory(select_workers(json_agents, task))

print(boss.short_memory)
boss.add_message_to_memory(
select_workers(json_agentic_output, task)
)

# Task 3: Now add the agents as tools
boss.add_tool(send_task_to_network_agent)
Expand Down Expand Up @@ -367,10 +348,3 @@ def run_swarm(task: str = None, *args, **kwargs):
out = master_creates_agents(task, *args, **kwargs)
# return passed
return out


# out = run_task(
# "Create a team of AI engineers to create an AI for a"
# " self-driving car"
# )
# # logger.info(out)
75 changes: 75 additions & 0 deletions neo_sapiens/tools_preset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from swarms import tool
import subprocess


# Tools
@tool
def terminal(
code: str,
):
"""
Run code in the terminal.
Args:
code (str): The code to run in the terminal.
Returns:
str: The output of the code.
"""
out = subprocess.run(
code, shell=True, capture_output=True, text=True
).stdout
return str(out)


@tool
def browser(query: str):
"""
Search the query in the browser with the `browser` tool.
Args:
query (str): The query to search in the browser.
Returns:
str: The search results.
"""
import webbrowser

url = f"https://www.google.com/search?q={query}"
webbrowser.open(url)
return f"Searching for {query} in the browser."


@tool
def create_file(file_path: str, content: str):
"""
Create a file using the file editor tool.
Args:
file_path (str): The path to the file.
content (str): The content to write to the file.
Returns:
str: The result of the file creation operation.
"""
with open(file_path, "w") as file:
file.write(content)
return f"File {file_path} created successfully."


@tool
def file_editor(file_path: str, mode: str, content: str):
"""
Edit a file using the file editor tool.
Args:
file_path (str): The path to the file.
mode (str): The mode to open the file in.
content (str): The content to write to the file.
Returns:
str: The result of the file editing operation.
"""
with open(file_path, mode) as file:
file.write(content)
return f"File {file_path} edited successfully."

0 comments on commit 6c83400

Please sign in to comment.