diff --git a/README.md b/README.md index bb9dd54b..f98f43ea 100644 --- a/README.md +++ b/README.md @@ -1201,7 +1201,7 @@ Your final report should be well-structured, easy to understand, and provide a h # Create the Mixture of Agents class moa = MixtureOfAgents( - reference_agents=[agent1, agent2, agent3], + agents=[agent1, agent2, agent3], aggregator_agent=aggregator_agent, aggregator_system_prompt="""As the 10-K Report Aggregator, your task is to synthesize the analyses provided by the Financial Statement Analyzer, Risk Assessment Specialist, and Business Strategy Evaluator into a comprehensive and coherent report. diff --git a/example_async_vs_multithread.py b/example_async_vs_multithread.py new file mode 100644 index 00000000..f547abc8 --- /dev/null +++ b/example_async_vs_multithread.py @@ -0,0 +1,71 @@ +import os +import asyncio +import threading +from swarms import Agent +from swarm_models import OpenAIChat +import time +import psutil + +from swarms.prompts.finance_agent_sys_prompt import ( + FINANCIAL_AGENT_SYS_PROMPT, +) +from dotenv import load_dotenv + +load_dotenv() + +# Get the OpenAI API key from the environment variable +api_key = os.getenv("OPENAI_API_KEY") + +# Create an instance of the OpenAIChat class +model = OpenAIChat( + openai_api_key=api_key, model_name="gpt-4o-mini", temperature=0.1 +) + +# Initialize the agent +agent = Agent( + agent_name="Financial-Analysis-Agent", + system_prompt=FINANCIAL_AGENT_SYS_PROMPT, + llm=model, + max_loops=1, + autosave=True, + dashboard=False, + verbose=True, + dynamic_temperature_enabled=True, + saved_state_path="finance_agent.json", + user_name="swarms_corp", + retry_attempts=1, + context_length=200000, + return_step_meta=False, + output_type="string", + streaming_on=False, +) + +# Function to measure time and memory usage +def measure_time_and_memory(func): + def wrapper(*args, **kwargs): + start_time = time.time() + result = func(*args, **kwargs) + end_time = time.time() + memory_usage = psutil.Process().memory_info().rss / 1024 ** 2 + print(f"Time taken: {end_time - start_time} seconds") + print(f"Memory used: {memory_usage} MB") + return result + return wrapper + +# Function to run the agent asynchronously +@measure_time_and_memory +async def run_agent_async(): + await asyncio.gather( + agent.run( + "How can I establish a ROTH IRA to buy stocks and get a tax break? What are the criteria" + ) + ) + +# Function to run the agent on another thread +@measure_time_and_memory +def run_agent_thread(): + asyncio.run(run_agent_async()) + +# Run the agent asynchronously and on another thread to test the speed +asyncio.run(run_agent_async()) +run_agent_thread() diff --git a/pyproject.toml b/pyproject.toml index 576efe03..d8d06c61 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "6.2.8" +version = "6.2.9" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/structs/mixture_of_agents.py b/swarms/structs/mixture_of_agents.py index 7c97afd2..f80701ef 100644 --- a/swarms/structs/mixture_of_agents.py +++ b/swarms/structs/mixture_of_agents.py @@ -20,7 +20,7 @@ class MixtureOfAgentsInput(BaseModel): description: str = ( "A class to run a mixture of agents and aggregate their responses." ) - reference_agents: List[Dict[str, Any]] + agents: List[Dict[str, Any]] aggregator_agent: Any = Field( ..., description="An aggregator agent to be used in the mixture.", @@ -60,7 +60,7 @@ class MixtureOfAgents: self, name: str = "MixtureOfAgents", description: str = "A class to run a mixture of agents and aggregate their responses.", - reference_agents: List[Agent] = [], + agents: List[Agent] = [], aggregator_agent: Agent = None, aggregator_system_prompt: str = "", layers: int = 3, @@ -71,14 +71,14 @@ class MixtureOfAgents: Args: name (str, optional): The name of the mixture of agents. Defaults to "MixtureOfAgents". description (str, optional): A description of the mixture of agents. Defaults to "A class to run a mixture of agents and aggregate their responses.". - reference_agents (List[Agent], optional): A list of reference agents to be used in the mixture. Defaults to []. + agents (List[Agent], optional): A list of reference agents to be used in the mixture. Defaults to []. aggregator_agent (Agent, optional): The aggregator agent to be used in the mixture. Defaults to None. aggregator_system_prompt (str, optional): The system prompt for the aggregator agent. Defaults to "". layers (int, optional): The number of layers to process in the mixture. Defaults to 3. """ self.name = name self.description = description - self.reference_agents: List[Agent] = reference_agents + self.agents: List[Agent] = agents self.aggregator_agent: Agent = aggregator_agent self.aggregator_system_prompt: str = aggregator_system_prompt self.layers: int = layers @@ -86,8 +86,8 @@ class MixtureOfAgents: self.input_schema = MixtureOfAgentsInput( name=name, description=description, - reference_agents=[ - agent.to_dict() for agent in self.reference_agents + agents=[ + agent.to_dict() for agent in self.agents ], aggregator_agent=aggregator_agent.to_dict(), aggregator_system_prompt=self.aggregator_system_prompt, @@ -113,7 +113,7 @@ class MixtureOfAgents: "Checking the reliability of the Mixture of Agents class." ) - if not self.reference_agents: + if not self.agents: raise ValueError("No reference agents provided.") if not self.aggregator_agent: @@ -205,7 +205,7 @@ class MixtureOfAgents: results: List[str] = await asyncio.gather( *[ self._run_agent_async(agent, task) - for agent in self.reference_agents + for agent in self.agents ] ) @@ -216,7 +216,7 @@ class MixtureOfAgents: self._run_agent_async( agent, task, prev_responses=results ) - for agent in self.reference_agents + for agent in self.agents ] ) diff --git a/swarms/structs/swarm_router.py b/swarms/structs/swarm_router.py index 1b1cc44c..0d2ef9dd 100644 --- a/swarms/structs/swarm_router.py +++ b/swarms/structs/swarm_router.py @@ -295,7 +295,7 @@ class SwarmRouter: return MixtureOfAgents( name=self.name, description=self.description, - reference_agents=self.agents, + agents=self.agents, aggregator_system_prompt=aggregator_system_prompt.get_prompt(), aggregator_agent=self.agents[-1], layers=self.max_loops,