diff --git a/README.md b/README.md index da6819c3..dfd58bdc 100644 --- a/README.md +++ b/README.md @@ -1271,24 +1271,28 @@ The `run` method returns a dictionary containing the execution results of all no ```python -import os - -from dotenv import load_dotenv - - from swarms import Agent, Edge, GraphWorkflow, Node, NodeType -from swarm_models import OpenAIChat - -load_dotenv() - -api_key = os.environ.get("OPENAI_API_KEY") +# Initialize agents with model_name parameter +agent1 = Agent( + agent_name="Agent1", + model_name="openai/gpt-4o-mini", # Using provider prefix + temperature=0.5, + max_tokens=4000, + max_loops=1, + autosave=True, + dashboard=True, +) -llm = OpenAIChat( - temperature=0.5, openai_api_key=api_key, max_tokens=4000 +agent2 = Agent( + agent_name="Agent2", + model_name="openai/gpt-4o-mini", # Using provider prefix + temperature=0.5, + max_tokens=4000, + max_loops=1, + autosave=True, + dashboard=True, ) -agent1 = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True) -agent2 = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True) def sample_task(): print("Running sample task") @@ -1297,9 +1301,8 @@ def sample_task(): wf_graph = GraphWorkflow() wf_graph.add_node(Node(id="agent1", type=NodeType.AGENT, agent=agent1)) wf_graph.add_node(Node(id="agent2", type=NodeType.AGENT, agent=agent2)) -wf_graph.add_node( - Node(id="task1", type=NodeType.TASK, callable=sample_task) -) +wf_graph.add_node(Node(id="task1", type=NodeType.TASK, callable=sample_task)) + wf_graph.add_edge(Edge(source="agent1", target="task1")) wf_graph.add_edge(Edge(source="agent2", target="task1")) @@ -1308,10 +1311,8 @@ wf_graph.set_end_points(["task1"]) print(wf_graph.visualize()) -# Run the workflow results = wf_graph.run() print("Execution results:", results) - ``` ## `MixtureOfAgents` @@ -2216,21 +2217,20 @@ Documentation is located here at: [docs.swarms.world](https://docs.swarms.world) ----- ## Folder Structure -The swarms package has been meticlously crafted for extreme use-ability and understanding, the swarms package is split up into various modules such as `swarms.agents` that holds pre-built agents, `swarms.structs` that holds a vast array of structures like `Agent` and multi agent structures. The 3 most important are `structs`, `models`, and `agents`. +The swarms package has been meticulously crafted for extreme usability and understanding,the swarms package is split up into various modules such as `swarms.agents` that holds pre-built agents, `swarms.structs` that holds a vast array of structures like `Agent` and multi agent structures. The package is split into various modules, with the most important being `structs`, `tools`, and `agents`. ```sh ├── __init__.py -├── agents -├── artifacts -├── memory -├── schemas -├── models -> swarm_models -├── prompts -├── structs -├── telemetry -├── tools -├── utils -└── workers +├── agents/ +├── artifacts/ +├── client/ +├── cli/ +├── prompts/ +├── schemas/ +├── structs/ +├── telemetry/ +├── tools/ +└── utils/ ``` ---- diff --git a/docs/swarms/install/quickstart.md b/docs/swarms/install/quickstart.md index ce71f5ed..24aab047 100644 --- a/docs/swarms/install/quickstart.md +++ b/docs/swarms/install/quickstart.md @@ -20,35 +20,27 @@ $ pip install -U swarms ### **Usage Example: Single Agent** -Here’s a simple example of creating a financial analysis agent powered by OpenAI’s GPT-4 model. This agent will analyze financial queries like how to set up a ROTH IRA. +Here's a simple example of creating a financial analysis agent powered by OpenAI's GPT-4o-mini model. This agent will analyze financial queries like how to set up a ROTH IRA. ```python -import os -from swarms import Agent -from swarm_models import OpenAIChat -from dotenv import load_dotenv - -load_dotenv() +from swarms.structs.agent import Agent -# Initialize OpenAI model -model = OpenAIChat( - openai_api_key=os.getenv("OPENAI_API_KEY"), model_name="gpt-4o-mini", temperature=0.1 -) - -# Initialize the agent +# Initialize the agent with GPT-4o-mini model agent = Agent( agent_name="Financial-Analysis-Agent", system_prompt="Analyze financial situations and provide advice...", - llm=model, max_loops=1, autosave=True, dashboard=False, verbose=True, - saved_state_path="finance_agent.json" + saved_state_path="finance_agent.json", + model_name="gpt-4o-mini", ) -# Run the agent on a financial query -out = agent.run("How can I establish a ROTH IRA to buy stocks and get a tax break? What are the criteria?") +# Run your query +out = agent.run( + "How can I establish a ROTH IRA to buy stocks and get a tax break? What are the criteria?" +) print(out) ``` @@ -57,13 +49,13 @@ print(out) - **Attributes:** - `agent_name`: Name of the agent. - `system_prompt`: System-level instruction guiding the agent's behavior. - - `llm`: Language model used by the agent (e.g., GPT, Anthropic). + - `model_name`: Name of the model to use (e.g., "gpt-4o-mini"). - `max_loops`: Max iterations for a task. - `autosave`: Auto-saves the state after each iteration. - **Methods:** - - `run(task: str)`: Executes the agent’s task. - - `ingest_docs(doc_path: str)`: Ingests documents into the agent’s knowledge base. + - `run(task: str)`: Executes the agent's task. + - `ingest_docs(doc_path: str)`: Ingests documents into the agent's knowledge base. - `filtered_run(task: str)`: Runs agent with a filtered system prompt. ----- @@ -121,7 +113,7 @@ agents: ### Key Configuration Fields: - **agent_name**: Name of the agent. - **model**: Defines the language model settings (e.g., API key, model name, temperature, and max tokens). -- **system_prompt**: The system prompt used to guide the agent’s behavior. +- **system_prompt**: The system prompt used to guide the agent's behavior. - **task**: (Optional) Task for the agent to execute once created. --- @@ -248,7 +240,7 @@ Steps: For example, here's an example on how to create an agent from griptape. -Here’s how you can create a custom **Griptape** agent that integrates with the **Swarms** framework by inheriting from the `Agent` class in **Swarms** and overriding the `run(task: str) -> str` method. +Here's how you can create a custom **Griptape** agent that integrates with the **Swarms** framework by inheriting from the `Agent` class in **Swarms** and overriding the `run(task: str) -> str` method. ```python @@ -420,7 +412,7 @@ print(output) ### 5. **Spreadsheet Swarm** -**Overview**: `SpreadSheetSwarm` enables the management of thousands of agents simultaneously, where each agent operates on its own thread. It’s ideal for overseeing large-scale agent outputs. +**Overview**: `SpreadSheetSwarm` enables the management of thousands of agents simultaneously, where each agent operates on its own thread. It's ideal for overseeing large-scale agent outputs. #### Mermaid Graph: @@ -466,7 +458,7 @@ These are the key swarm architectures available in the **Swarms Framework**. Eac #### **Workflow Classes** - **SequentialWorkflow:** - - Chains agents, where one agent's output becomes the next agent’s input. + - Chains agents, where one agent's output becomes the next agent's input. - **AgentRearrange:** - Dynamically rearranges agent tasks either in parallel or sequentially based on defined flow. diff --git a/example.py b/example.py index 71005361..922e16c2 100644 --- a/example.py +++ b/example.py @@ -14,4 +14,4 @@ agent = Agent( interactive=False, ) -agent.run("Conduct an analysis of the best real undervalued ETFs") +agent.run("Conduct an analysis of the best real undervalued ETFs") \ No newline at end of file diff --git a/examples/async_agent.py b/examples/async_agent.py index 5c23a8b8..92bf0b2d 100644 --- a/examples/async_agent.py +++ b/examples/async_agent.py @@ -2,19 +2,17 @@ from swarms import Agent from swarms.prompts.finance_agent_sys_prompt import ( FINANCIAL_AGENT_SYS_PROMPT, ) -from swarm_models import OpenAIChat -model = OpenAIChat(model_name="gpt-4o") - - -# Initialize the agent +# Initialize the agent (no swarm_models import needed) agent = Agent( agent_name="Financial-Analysis-Agent", agent_description="Personal finance advisor agent", - system_prompt=FINANCIAL_AGENT_SYS_PROMPT - + "Output the token when you're done creating a portfolio of etfs, index, funds, and more for AI", + system_prompt=( + FINANCIAL_AGENT_SYS_PROMPT + + " Output the token when you're done creating a portfolio" + ), max_loops=1, - llm=model, + model_name="gpt-4o", dynamic_temperature_enabled=True, user_name="Kye", retry_attempts=3, @@ -33,7 +31,8 @@ agent = Agent( async def run_agent(): await agent.arun( - "Create a table of super high growth opportunities for AI. I have $40k to invest in ETFs, index funds, and more. Please create a table in markdown.", + "Create a table of super high-growth AI investment opportunities " + "with $40k in ETFs, index funds, etc., and output it in Markdown.", all_cores=True, ) diff --git a/examples/example_async_vs_multithread.py b/examples/example_async_vs_multithread.py index 25d514aa..c85e4019 100644 --- a/examples/example_async_vs_multithread.py +++ b/examples/example_async_vs_multithread.py @@ -1,30 +1,14 @@ -import os import asyncio -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") +from swarms.structs.agent import Agent +from swarms.prompts.finance_agent_sys_prompt import FINANCIAL_AGENT_SYS_PROMPT -# 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 +# Initialize the agent (no external imports or env lookups needed here) agent = Agent( agent_name="Financial-Analysis-Agent", system_prompt=FINANCIAL_AGENT_SYS_PROMPT, - llm=model, max_loops=1, autosave=True, dashboard=False, @@ -37,39 +21,35 @@ agent = Agent( return_step_meta=False, output_type="string", streaming_on=False, + model_name="gpt-4o-mini", ) - -# Function to measure time and memory usage +# Helper decorator to measure time and memory usage def measure_time_and_memory(func): def wrapper(*args, **kwargs): - start_time = time.time() + start = 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") + elapsed = time.time() - start + mem_mb = psutil.Process().memory_info().rss / 1024**2 + print(f"[{func.__name__}] Time: {elapsed:.2f}s | Memory: {mem_mb:.2f} MB") return result - return wrapper - -# Function to run the agent asynchronously +# Async wrapper using asyncio.to_thread for the blocking call @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" - ) + return await asyncio.to_thread( + 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 +# Threaded wrapper simply runs the async version in the event loop again @measure_time_and_memory -def run_agent_thread(): +def run_agent_in_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() +if __name__ == "__main__": + # 1) Run asynchronously + asyncio.run(run_agent_async()) + # 2) Then run again via the threaded wrapper + run_agent_in_thread()