cleanup init agent 10k agents a sec

pull/836/head
Kye Gomez 5 days ago
parent f49eb0d8aa
commit de7e751025

@ -164,4 +164,4 @@ def benchmark_multiple_agents(num_agents=100):
if __name__ == "__main__": if __name__ == "__main__":
benchmark_multiple_agents(100) benchmark_multiple_agents(1000)

@ -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))

@ -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())

@ -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")

@ -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"
)
)

@ -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},
)
)

@ -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

@ -1,4 +1,3 @@
import concurrent.futures
import asyncio import asyncio
import json import json
import logging 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.formatter import formatter
from swarms.utils.history_output_formatter import ( from swarms.utils.history_output_formatter import (
history_output_formatter, history_output_formatter,
HistoryOutputType,
) )
from swarms.utils.litellm_tokenizer import count_tokens from swarms.utils.litellm_tokenizer import count_tokens
from swarms.utils.litellm_wrapper import LiteLLM from swarms.utils.litellm_wrapper import LiteLLM
from swarms.utils.pdf_to_text import pdf_to_text 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.utils.str_to_dict import str_to_dict
from swarms.tools.mcp_client import ( from swarms.tools.mcp_client import (
execute_mcp_tool, execute_mcp_tool,
@ -337,7 +336,7 @@ class Agent:
# [Tools] # [Tools]
custom_tools_prompt: Optional[Callable] = None, custom_tools_prompt: Optional[Callable] = None,
tool_schema: ToolUsageType = None, tool_schema: ToolUsageType = None,
output_type: HistoryOutputType = "str-all-except-first", output_type: OutputType = "str-all-except-first",
function_calling_type: str = "json", function_calling_type: str = "json",
output_cleaner: Optional[Callable] = None, output_cleaner: Optional[Callable] = None,
function_calling_format_type: Optional[str] = "OpenAI", function_calling_format_type: Optional[str] = "OpenAI",
@ -529,12 +528,32 @@ class Agent:
# Initialize the feedback # Initialize the feedback
self.feedback = [] self.feedback = []
# Initialize the executor # self.init_handling()
self.executor = ThreadPoolExecutor( # Define tasks as pairs of (function, condition)
max_workers=executor_workers # 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): def short_memory_init(self):
if ( if (
@ -561,57 +580,20 @@ class Agent:
# Each task will only run if its condition is True # Each task will only run if its condition is True
self.setup_config() self.setup_config()
tasks = [ if exists(self.docs_folder):
(self.setup_config, True), # Always run setup_config self.get_docs_from_doc_folders()
(
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
}
# Wait for each future to complete and collect results/exceptions if exists(self.tools):
for future in concurrent.futures.as_completed( self.handle_tool_init()
future_to_task
): if exists(self.tool_schema) or exists(self.list_base_models):
task_name = future_to_task[future] self.handle_tool_schema_ops()
try:
result = future.result() if exists(self.sop) or exists(self.sop_list):
results[task_name] = result self.handle_sop_ops()
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}"
)
# Run sequential operations after all concurrent tasks are done # 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()) log_agent_data(self.to_dict())
if self.llm is None: if self.llm is None:
@ -1808,10 +1790,11 @@ class Agent:
) )
# Reinitialize executor if needed # Reinitialize executor if needed
if not hasattr(self, "executor") or self.executor is None: # if not hasattr(self, "executor") or self.executor is None:
self.executor = ThreadPoolExecutor( with ThreadPoolExecutor(
max_workers=os.cpu_count() max_workers=os.cpu_count()
) ) as executor:
self.executor = executor
# # Reinitialize tool structure if needed # # Reinitialize tool structure if needed
# if hasattr(self, 'tools') and (self.tools or getattr(self, 'list_base_models', None)): # if hasattr(self, 'tools') and (self.tools or getattr(self, 'list_base_models', None)):

@ -19,6 +19,8 @@ HistoryOutputType = Literal[
"str-all-except-first", "str-all-except-first",
] ]
output_type: HistoryOutputType
def history_output_formatter( def history_output_formatter(
conversation: Conversation, type: HistoryOutputType = "list" conversation: Conversation, type: HistoryOutputType = "list"

Loading…
Cancel
Save