From de7e7510252758321ad8cf95ca52f9e620a4aae4 Mon Sep 17 00:00:00 2001 From: Kye Gomez Date: Mon, 5 May 2025 15:40:07 -0700 Subject: [PATCH] cleanup init agent 10k agents a sec --- tests/benchmark_init.py => benchmark_init.py | 2 +- {asb => examples/asb}/asb_research.py | 0 .../asb}/auto_swarm_builder_test.py | 0 .../hiearchical_swarm-example.py | 0 legal_swarm.py => examples/legal_swarm.py | 0 .../swarm_router_test.py | 0 mcp_example/agent_mcp_test.py | 24 ---- mcp_example/agent_tools_dict_example.py | 50 --------- mcp_example/mcp_test.py | 20 ---- mcp_example/mcp_utils.py | 10 -- mcp_example/test_execute.py | 8 -- mkdocs.yml | 83 -------------- auto_corp.py => new/auto_corp.py | 0 create_agent.py => new/create_agent.py | 0 swarms/structs/agent.py | 103 ++++++++---------- swarms/utils/history_output_formatter.py | 2 + 16 files changed, 46 insertions(+), 256 deletions(-) rename tests/benchmark_init.py => benchmark_init.py (99%) rename {asb => examples/asb}/asb_research.py (100%) rename {asb => examples/asb}/auto_swarm_builder_test.py (100%) rename hiearchical_swarm-example.py => examples/hiearchical_swarm-example.py (100%) rename legal_swarm.py => examples/legal_swarm.py (100%) rename swarm_router_test.py => examples/swarm_router_test.py (100%) delete mode 100644 mcp_example/agent_mcp_test.py delete mode 100644 mcp_example/agent_tools_dict_example.py delete mode 100644 mcp_example/mcp_test.py delete mode 100644 mcp_example/mcp_utils.py delete mode 100644 mcp_example/test_execute.py delete mode 100644 mkdocs.yml rename auto_corp.py => new/auto_corp.py (100%) rename create_agent.py => new/create_agent.py (100%) diff --git a/tests/benchmark_init.py b/benchmark_init.py similarity index 99% rename from tests/benchmark_init.py rename to benchmark_init.py index 36fc18df..1a700e22 100644 --- a/tests/benchmark_init.py +++ b/benchmark_init.py @@ -164,4 +164,4 @@ def benchmark_multiple_agents(num_agents=100): if __name__ == "__main__": - benchmark_multiple_agents(100) + benchmark_multiple_agents(1000) diff --git a/asb/asb_research.py b/examples/asb/asb_research.py similarity index 100% rename from asb/asb_research.py rename to examples/asb/asb_research.py diff --git a/asb/auto_swarm_builder_test.py b/examples/asb/auto_swarm_builder_test.py similarity index 100% rename from asb/auto_swarm_builder_test.py rename to examples/asb/auto_swarm_builder_test.py diff --git a/hiearchical_swarm-example.py b/examples/hiearchical_swarm-example.py similarity index 100% rename from hiearchical_swarm-example.py rename to examples/hiearchical_swarm-example.py diff --git a/legal_swarm.py b/examples/legal_swarm.py similarity index 100% rename from legal_swarm.py rename to examples/legal_swarm.py diff --git a/swarm_router_test.py b/examples/swarm_router_test.py similarity index 100% rename from swarm_router_test.py rename to examples/swarm_router_test.py diff --git a/mcp_example/agent_mcp_test.py b/mcp_example/agent_mcp_test.py deleted file mode 100644 index 86c19a25..00000000 --- a/mcp_example/agent_mcp_test.py +++ /dev/null @@ -1,24 +0,0 @@ -from swarms import Agent -from swarms.prompts.finance_agent_sys_prompt import ( - FINANCIAL_AGENT_SYS_PROMPT, -) -from swarms.tools.mcp_integration import MCPServerSseParams - -server_one = MCPServerSseParams( - url="http://127.0.0.1:6274", - headers={"Content-Type": "application/json"}, -) - -# Initialize the agent -agent = Agent( - agent_name="Financial-Analysis-Agent", - agent_description="Personal finance advisor agent", - system_prompt=FINANCIAL_AGENT_SYS_PROMPT, - max_loops=1, - mcp_servers=[server_one], - output_type="final", -) - -out = agent.run("Use the add tool to add 2 and 2") - -print(type(out)) diff --git a/mcp_example/agent_tools_dict_example.py b/mcp_example/agent_tools_dict_example.py deleted file mode 100644 index f1d02620..00000000 --- a/mcp_example/agent_tools_dict_example.py +++ /dev/null @@ -1,50 +0,0 @@ -from swarms import Agent - -tools = [ - { - "type": "function", - "function": { - "name": "add_numbers", - "description": "Add two numbers together and return the result.", - "parameters": { - "type": "object", - "properties": { - "name": { - "type": "string", - "description": "The name of the operation to perform.", - }, - "a": { - "type": "integer", - "description": "The first number to add.", - }, - "b": { - "type": "integer", - "description": "The second number to add.", - }, - }, - "required": [ - "name", - "a", - "b", - ], - }, - }, - } -] - - -# Initialize the agent -agent = Agent( - agent_name="Financial-Analysis-Agent", - agent_description="Personal finance advisor agent", - max_loops=2, - tools_list_dictionary=tools, - output_type="final", - mcp_url="http://0.0.0.0:8000/sse", -) - -out = agent.run( - "Use the multiply tool to multiply 3 and 4 together. Look at the tools available to you.", -) - -print(agent.short_memory.get_str()) diff --git a/mcp_example/mcp_test.py b/mcp_example/mcp_test.py deleted file mode 100644 index a609f84c..00000000 --- a/mcp_example/mcp_test.py +++ /dev/null @@ -1,20 +0,0 @@ -# math_server.py -from mcp.server.fastmcp import FastMCP - -mcp = FastMCP("Math") - - -@mcp.tool() -def add(a: int, b: int) -> int: - """Add two numbers""" - return a + b - - -@mcp.tool() -def multiply(a: int, b: int) -> int: - """Multiply two numbers""" - return a * b - - -if __name__ == "__main__": - mcp.run(transport="sse") diff --git a/mcp_example/mcp_utils.py b/mcp_example/mcp_utils.py deleted file mode 100644 index b7085cc3..00000000 --- a/mcp_example/mcp_utils.py +++ /dev/null @@ -1,10 +0,0 @@ -from swarms.tools.mcp_client import ( - list_tools_for_multiple_urls, -) - - -print( - list_tools_for_multiple_urls( - ["http://0.0.0.0:8000/sse"], output_type="json" - ) -) diff --git a/mcp_example/test_execute.py b/mcp_example/test_execute.py deleted file mode 100644 index ed9122a9..00000000 --- a/mcp_example/test_execute.py +++ /dev/null @@ -1,8 +0,0 @@ -from swarms.tools.mcp_client import execute_mcp_tool - -print( - execute_mcp_tool( - "http://0.0.0.0:8000/sse", - parameters={"name": "multiply", "a": 1, "b": 2}, - ) -) diff --git a/mkdocs.yml b/mkdocs.yml deleted file mode 100644 index dc4bb0b5..00000000 --- a/mkdocs.yml +++ /dev/null @@ -1,83 +0,0 @@ -site_name: Multi-Agent LLM Systems Best Practices -site_description: Comprehensive guide for building and managing multi-agent systems -site_author: Swarms Team - -theme: - name: material - features: - - navigation.tabs - - navigation.sections - - navigation.expand - - navigation.top - - search.suggest - - search.highlight - - content.tabs.link - - content.code.annotation - - content.code.copy - language: en - palette: - - scheme: default - toggle: - icon: material/toggle-switch-off-outline - name: Switch to dark mode - primary: teal - accent: purple - - scheme: slate - toggle: - icon: material/toggle-switch - name: Switch to light mode - primary: teal - accent: lime - font: - text: Roboto - code: Roboto Mono - icon: - repo: fontawesome/brands/github - -markdown_extensions: - - pymdownx.highlight: - anchor_linenums: true - - pymdownx.inlinehilite - - pymdownx.snippets - - admonition - - pymdownx.arithmatex: - generic: true - - footnotes - - pymdownx.details - - pymdownx.superfences: - custom_fences: - - name: mermaid - class: mermaid - format: !!python/name:pymdownx.superfences.fence_code_format - - pymdownx.mark - - attr_list - - pymdownx.emoji: - emoji_index: !!python/name:materialx.emoji.twemoji - emoji_generator: !!python/name:materialx.emoji.to_svg - -plugins: - - search - - minify: - minify_html: true - -extra: - social: - - icon: fontawesome/brands/github-alt - link: https://github.com/yourusername/multi-agent-best-practices - -nav: - - Home: index.md - - Core Concepts: - - Why Multi-Agent Systems?: concepts/why-multi-agent.md - - Limitations of Individual Agents: concepts/limitations.md - - Multi-Agent Architecture: concepts/architecture.md - - Best Practices: - - Implementation Guide: best-practices/implementation.md - - Communication Protocols: best-practices/communication.md - - Error Handling: best-practices/error-handling.md - - Performance Optimization: best-practices/performance.md - - FAQ: faq.md - - Tips & Troubleshooting: tips.md - - Glossary: swarms/glossary.md - -copyright: Copyright © 2024 Multi-Agent LLM Systems \ No newline at end of file diff --git a/auto_corp.py b/new/auto_corp.py similarity index 100% rename from auto_corp.py rename to new/auto_corp.py diff --git a/create_agent.py b/new/create_agent.py similarity index 100% rename from create_agent.py rename to new/create_agent.py diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 7cd4c8d4..f7bda171 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -1,4 +1,3 @@ -import concurrent.futures import asyncio import json import logging @@ -54,11 +53,11 @@ from swarms.utils.file_processing import create_file_in_folder from swarms.utils.formatter import formatter from swarms.utils.history_output_formatter import ( history_output_formatter, - HistoryOutputType, ) from swarms.utils.litellm_tokenizer import count_tokens from swarms.utils.litellm_wrapper import LiteLLM from swarms.utils.pdf_to_text import pdf_to_text +from swarms.structs.output_types import OutputType from swarms.utils.str_to_dict import str_to_dict from swarms.tools.mcp_client import ( execute_mcp_tool, @@ -337,7 +336,7 @@ class Agent: # [Tools] custom_tools_prompt: Optional[Callable] = None, tool_schema: ToolUsageType = None, - output_type: HistoryOutputType = "str-all-except-first", + output_type: OutputType = "str-all-except-first", function_calling_type: str = "json", output_cleaner: Optional[Callable] = None, function_calling_format_type: Optional[str] = "OpenAI", @@ -529,12 +528,32 @@ class Agent: # Initialize the feedback self.feedback = [] - # Initialize the executor - self.executor = ThreadPoolExecutor( - max_workers=executor_workers - ) + # self.init_handling() + # Define tasks as pairs of (function, condition) + # Each task will only run if its condition is True + self.setup_config() + + if exists(self.docs_folder): + self.get_docs_from_doc_folders() + + if exists(self.tools): + self.handle_tool_init() + + if exists(self.tool_schema) or exists(self.list_base_models): + self.handle_tool_schema_ops() + + if exists(self.sop) or exists(self.sop_list): + self.handle_sop_ops() - self.init_handling() + # Run sequential operations after all concurrent tasks are done + # self.agent_output = self.agent_output_model() + log_agent_data(self.to_dict()) + + if self.llm is None: + self.llm = self.llm_handling() + + if self.mcp_url or self.mcp_servers is not None: + self.add_mcp_tools_to_memory() def short_memory_init(self): if ( @@ -561,57 +580,20 @@ class Agent: # Each task will only run if its condition is True self.setup_config() - tasks = [ - (self.setup_config, True), # Always run setup_config - ( - self.get_docs_from_doc_folders, - exists(self.docs_folder), - ), - (self.handle_tool_init, True), # Always run tool init - ( - self.handle_tool_schema_ops, - exists(self.tool_schema) - or exists(self.list_base_models), - ), - # ( - # self.handle_sop_ops, - # exists(self.sop) or exists(self.sop_list), - # ), - ] - - # Filter out tasks whose conditions are False - filtered_tasks = [ - task for task, condition in tasks if condition - ] - - # Execute all tasks concurrently - with self.executor as executor: - # Map tasks to futures and collect results - results = {} - future_to_task = { - executor.submit(task): task.__name__ - for task in filtered_tasks - } + if exists(self.docs_folder): + self.get_docs_from_doc_folders() - # Wait for each future to complete and collect results/exceptions - for future in concurrent.futures.as_completed( - future_to_task - ): - task_name = future_to_task[future] - try: - result = future.result() - results[task_name] = result - logging.info( - f"Task {task_name} completed successfully" - ) - except Exception as e: - results[task_name] = None - logging.error( - f"Task {task_name} failed with error: {e}" - ) + if exists(self.tools): + self.handle_tool_init() + + if exists(self.tool_schema) or exists(self.list_base_models): + self.handle_tool_schema_ops() + + if exists(self.sop) or exists(self.sop_list): + self.handle_sop_ops() # Run sequential operations after all concurrent tasks are done - self.agent_output = self.agent_output_model() + # self.agent_output = self.agent_output_model() log_agent_data(self.to_dict()) if self.llm is None: @@ -1808,10 +1790,11 @@ class Agent: ) # Reinitialize executor if needed - if not hasattr(self, "executor") or self.executor is None: - self.executor = ThreadPoolExecutor( - max_workers=os.cpu_count() - ) + # if not hasattr(self, "executor") or self.executor is None: + with ThreadPoolExecutor( + max_workers=os.cpu_count() + ) as executor: + self.executor = executor # # Reinitialize tool structure if needed # if hasattr(self, 'tools') and (self.tools or getattr(self, 'list_base_models', None)): diff --git a/swarms/utils/history_output_formatter.py b/swarms/utils/history_output_formatter.py index 27375e6f..784437ed 100644 --- a/swarms/utils/history_output_formatter.py +++ b/swarms/utils/history_output_formatter.py @@ -19,6 +19,8 @@ HistoryOutputType = Literal[ "str-all-except-first", ] +output_type: HistoryOutputType + def history_output_formatter( conversation: Conversation, type: HistoryOutputType = "list"