From 7d2520ca099b2b747bd49cec28a3ce39a2526e8c Mon Sep 17 00:00:00 2001 From: Kye Gomez Date: Tue, 18 Jun 2024 09:32:15 -0700 Subject: [PATCH] [DOCS][CLEANUP] --- multion_example.py | 27 ++++++++++ buy_abunch_of_cybertrucks.py | 72 +++++++++++++++++++++++++ docs/swarms/structs/agent_rearrange.md | 27 +++++----- example.py | 19 ++++++- phising_my_parents.py | 53 ++++++++++++++++++ scripts/cleanup/json_log_cleanup.py | 2 +- swarms/agents/multion_wrapper.py | 75 ++++++++++++++++++++++++++ 7 files changed, 259 insertions(+), 16 deletions(-) create mode 100644 multion_example.py create mode 100644 buy_abunch_of_cybertrucks.py create mode 100644 phising_my_parents.py create mode 100644 swarms/agents/multion_wrapper.py diff --git a/ multion_example.py b/ multion_example.py new file mode 100644 index 00000000..2a4c0826 --- /dev/null +++ b/ multion_example.py @@ -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() diff --git a/buy_abunch_of_cybertrucks.py b/buy_abunch_of_cybertrucks.py new file mode 100644 index 00000000..536cde1c --- /dev/null +++ b/buy_abunch_of_cybertrucks.py @@ -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") diff --git a/docs/swarms/structs/agent_rearrange.md b/docs/swarms/structs/agent_rearrange.md index 967b69f7..55baa9bf 100644 --- a/docs/swarms/structs/agent_rearrange.md +++ b/docs/swarms/structs/agent_rearrange.md @@ -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. @@ -119,8 +118,8 @@ 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, @@ -128,13 +127,13 @@ director = Agent( verbose=True, stopping_token="", 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, @@ -142,13 +141,13 @@ worker1 = Agent( verbose=True, stopping_token="", 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, @@ -156,22 +155,22 @@ worker2 = Agent( verbose=True, stopping_token="", 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) ``` diff --git a/example.py b/example.py index c0fad2a1..7f8d77d4 100644 --- a/example.py +++ b/example.py @@ -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): @@ -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( @@ -46,8 +62,9 @@ agent = Agent( # 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." ) diff --git a/phising_my_parents.py b/phising_my_parents.py new file mode 100644 index 00000000..81b7df21 --- /dev/null +++ b/phising_my_parents.py @@ -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") diff --git a/scripts/cleanup/json_log_cleanup.py b/scripts/cleanup/json_log_cleanup.py index e7831095..d9b2cddc 100644 --- a/scripts/cleanup/json_log_cleanup.py +++ b/scripts/cleanup/json_log_cleanup.py @@ -31,4 +31,4 @@ def cleanup_json_logs(name: str = None): # Call the function -cleanup_json_logs("artifacts_five") +cleanup_json_logs("artifacts_six") diff --git a/swarms/agents/multion_wrapper.py b/swarms/agents/multion_wrapper.py new file mode 100644 index 00000000..9dc0c0c4 --- /dev/null +++ b/swarms/agents/multion_wrapper.py @@ -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) +