Skip to content

Commit

Permalink
[MixtureOfAgents.reference_agents -> agents]
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Nov 23, 2024
1 parent c1c98a8 commit b6ca99b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
71 changes: 71 additions & 0 deletions example_async_vs_multithread.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
Expand Down
18 changes: 9 additions & 9 deletions swarms/structs/mixture_of_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -60,7 +60,7 @@ def __init__(
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,
Expand All @@ -71,23 +71,23 @@ def __init__(
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

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,
Expand All @@ -113,7 +113,7 @@ def reliability_check(self) -> None:
"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:
Expand Down Expand Up @@ -205,7 +205,7 @@ async def _run_async(self, task: str) -> None:
results: List[str] = await asyncio.gather(
*[
self._run_agent_async(agent, task)
for agent in self.reference_agents
for agent in self.agents
]
)

Expand All @@ -216,7 +216,7 @@ async def _run_async(self, task: str) -> None:
self._run_agent_async(
agent, task, prev_responses=results
)
for agent in self.reference_agents
for agent in self.agents
]
)

Expand Down
2 changes: 1 addition & 1 deletion swarms/structs/swarm_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def _create_swarm(
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,
Expand Down

0 comments on commit b6ca99b

Please sign in to comment.