Skip to content

Commit

Permalink
[DOCS][CLEANUP]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye Gomez authored and Kye Gomez committed Jun 18, 2024
1 parent f229f91 commit 7d2520c
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 16 deletions.
27 changes: 27 additions & 0 deletions multion_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import threading
from swarms.agents.multion_wrapper import MultiOnAgent

def run_model(api_key):
model = MultiOnAgent(api_key=api_key, max_steps=500, url="https://x.com")
out = model.run(
"""
click on the 'Tweet' button to start a new tweet and post it saying: $pip3 install swarms
"""
)
print(out)

# Create a list to store the threads
threads = []

# Run 100 instances using multithreading
for _ in range(10):
api_key = os.getenv("MULTION_API_KEY")
thread = threading.Thread(target=run_model, args=(api_key,))
thread.start()
threads.append(thread)

# Wait for all threads to finish
for thread in threads:
thread.join()
72 changes: 72 additions & 0 deletions buy_abunch_of_cybertrucks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@

from swarms import Agent, AgentRearrange, OpenAIChat
from swarms.agents.multion_wrapper import MultiOnAgent

model = MultiOnAgent(
url="https://tesla.com",
)


llm = OpenAIChat()


def browser_automation(task: str):
"""
Run a task on the browser automation agent.
Args:
task (str): The task to be executed on the browser automation agent.
"""
out = model.run(task)
return out


# Purpose = To detect email spam using three different agents
agent1 = Agent(
agent_name="CyberTruckBuyer1",
system_prompt="Find the best deal on a Cyber Truck and provide your reasoning",
llm=llm,
max_loops=1,
# output_type=str,
metadata="json",
function_calling_format_type="OpenAI",
function_calling_type="json",
streaming_on=True,
tools=[browser_automation],
)

agent2 = Agent(
agent_name="CyberTruckBuyer2",
system_prompt="Find the best deal on a Cyber Truck and provide your reasoning",
llm=llm,
max_loops=1,
# output_type=str,
metadata="json",
function_calling_format_type="OpenAI",
function_calling_type="json",
streaming_on=True,
tools=[browser_automation],
)

agent3 = Agent(
agent_name="CyberTruckBuyer3",
system_prompt="Find the best deal on a Cyber Truck and provide your reasoning",
llm=llm,
max_loops=1,
# output_type=str,
metadata="json",
function_calling_format_type="OpenAI",
function_calling_type="json",
streaming_on=True,
tools=[browser_automation],
)

swarm = AgentRearrange(
flow="CyberTruckBuyer1 -> CyberTruckBuyer2 -> CyberTruckBuyer3",
agents=[agent1, agent2, agent3],
logging_enabled=True,
max_loops=1,
)

# Run all the agents
swarm.run("Let's buy a cyber truck")
27 changes: 13 additions & 14 deletions docs/swarms/structs/agent_rearrange.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# Documentation for `AgentRearrange` Class
-----
# `AgentRearrange` Class

The `AgentRearrange` class represents a swarm of agents for rearranging tasks. It allows you to create a swarm of agents, add or remove agents from the swarm, and run the swarm to process tasks based on a specified flow pattern.

Expand Down Expand Up @@ -119,59 +118,59 @@ from typing import List

# Initialize the director agent
director = Agent(
agent_name="Director",
system_prompt="Directs the tasks for the workers",
agent_name="Accounting Director",
system_prompt="Directs the accounting tasks for the workers",
llm=Anthropic(),
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
state_save_file_type="json",
saved_state_path="director.json",
saved_state_path="accounting_director.json",
)

# Initialize worker 1
worker1 = Agent(
agent_name="Worker1",
system_prompt="Generates a transcript for a youtube video on what swarms are",
agent_name="Accountant 1",
system_prompt="Processes financial transactions and prepares financial statements",
llm=Anthropic(),
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
state_save_file_type="json",
saved_state_path="worker1.json",
saved_state_path="accountant1.json",
)

# Initialize worker 2
worker2 = Agent(
agent_name="Worker2",
system_prompt="Summarizes the transcript generated by Worker1",
agent_name="Accountant 2",
system_prompt="Performs audits and ensures compliance with financial regulations",
llm=Anthropic(),
max_loops=1,
dashboard=False,
streaming_on=True,
verbose=True,
stopping_token="<DONE>",
state_save_file_type="json",
saved_state_path="worker2.json",
saved_state_path="accountant2.json",
)

# Create a list of agents
agents = [director, worker1, worker2]

# Define the flow pattern
flow = "Director -> Worker1 -> Worker2"
flow = "Accounting Director -> Accountant 1 -> Accountant 2"

# Using AgentRearrange class
agent_system = AgentRearrange(agents=agents, flow=flow)
output = agent_system.run("Create a format to express and communicate swarms of llms in a structured manner for youtube")
output = agent_system.run("Process monthly financial statements")
print(output)

# Using rearrange function
output = rearrange(agents, flow, "Create a format to express and communicate swarms of llms in a structured manner for youtube")
output = rearrange(agents, flow, "Process monthly financial statements")
print(output)

```
Expand Down
19 changes: 18 additions & 1 deletion example.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
from swarms import Agent, OpenAIChat
from swarms.agents.multion_wrapper import MultiOnAgent

model = MultiOnAgent(
url="https://tesla.com",
)



def calculate_profit(revenue: float, expenses: float):
Expand Down Expand Up @@ -28,6 +34,16 @@ def generate_report(company_name: str, profit: float):
"""
return f"The profit for {company_name} is ${profit}."

def browser_automation(task: str = None):
"""
Run a task on the browser automation agent.
Args:
task (str): The task to be executed on the browser automation agent.
"""
out = model.run(task)
return out


# Initialize the agent
agent = Agent(
Expand All @@ -46,8 +62,9 @@ def generate_report(company_name: str, profit: float):
# tools=[calculate_profit, generate_report],
# docs_folder="docs",
# pdf_path="docs/accounting_agent.pdf",
# tools=[browser_automation],
)

agent.run(
"We're the Swarm Corporation, our total revenue is $100,000 and our total expenses are $50,000, is our revenue good?"
"Calculate the profit for Tesla with a revenue of $100,000 and expenses of $50,000."
)
53 changes: 53 additions & 0 deletions phising_my_parents.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os

from swarms import OpenAIChat, Agent, AgentRearrange

# Purpose = To detect email spam using three different agents
agent1 = Agent(
agent_name="SpamDetector1",
system_prompt="Detect if the email is spam or not, and provide your reasoning",
llm=OpenAIChat(openai_api_key=os.getenv("OPENAI_API_KEY")),
max_loops=1,
output_type=str,
# tools=[],
metadata="json",
function_calling_format_type="OpenAI",
function_calling_type="json",
streaming_on=True,
)

agent2 = Agent(
agent_name="SpamDetector2",
system_prompt="Detect if the email is spam or not, and provide your reasoning",
llm=OpenAIChat(openai_api_key=os.getenv("OPENAI_API_KEY")),
max_loops=1,
output_type=str,
# tools=[],
metadata="json",
function_calling_format_type="OpenAI",
function_calling_type="json",
streaming_on=True,
)

agent3 = Agent(
agent_name="SpamDetector3",
system_prompt="Detect if the email is spam or not, and provide your reasoning",
llm=OpenAIChat(openai_api_key=os.getenv("OPENAI_API_KEY")),
max_loops=1,
output_type=str,
# tools=[],
metadata="json",
function_calling_format_type="OpenAI",
function_calling_type="json",
streaming_on=True,
)

swarm = AgentRearrange(
flow="SpamDetector1 -> SpamDetector2 -> SpamDetector3",
agents=[agent1, agent2, agent3],
logging_enabled=True,
max_loops=1,
)

# Run all the agents
swarm.run("Find YSL bag with the biggest discount")
2 changes: 1 addition & 1 deletion scripts/cleanup/json_log_cleanup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ def cleanup_json_logs(name: str = None):


# Call the function
cleanup_json_logs("artifacts_five")
cleanup_json_logs("artifacts_six")
75 changes: 75 additions & 0 deletions swarms/agents/multion_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# pip3 install multion
# pip3 install swarms
from multion.client import MultiOn
import os
from swarms.models.base_llm import BaseLLM


def check_multion_api_key():
"""
Checks if the MultiOn API key is available in the environment variables.
Returns:
str: The MultiOn API key.
"""
api_key = os.getenv("MULTION_API_KEY")
return api_key


class MultiOnAgent(BaseLLM):
"""
Represents an agent that interacts with the MultiOn API to run tasks on a remote session.
Args:
api_key (str): The API key for accessing the MultiOn API.
url (str): The URL of the remote session.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
Attributes:
client (MultiOn): The MultiOn client instance.
url (str): The URL of the remote session.
session_id (str): The ID of the current session.
Methods:
run: Runs a task on the remote session.
"""

def __init__(
self,
name: str = None,
system_prompt: str = None,
api_key: str = check_multion_api_key,
url: str = "https://huggingface.co/papers",
max_steps: int = 1,
*args,
**kwargs,
):
super().__init__(*args, **kwargs)
self.name = name
self.client = MultiOn(api_key=api_key)
self.url = url
self.system_prompt = system_prompt
self.max_steps = max_steps

def run(self, task: str, *args, **kwargs):
"""
Runs a task on the remote session.
Args:
task (str): The task to be executed on the remote session.
*args: Variable length argument list.
**kwargs: Arbitrary keyword arguments.
"""
response = self.client.browse(
cmd=task,
url=self.url,
local=True,
max_steps=self.max_steps,
)

# response = response.json()

# print(response.message)
return str(response.message)

0 comments on commit 7d2520c

Please sign in to comment.