swarms 7.7.1__py3-none-any.whl → 7.7.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. swarms/prompts/ag_prompt.py +51 -19
  2. swarms/prompts/agent_system_prompts.py +13 -4
  3. swarms/prompts/multi_agent_collab_prompt.py +18 -0
  4. swarms/prompts/prompt.py +6 -10
  5. swarms/schemas/__init__.py +0 -3
  6. swarms/structs/__init__.py +3 -8
  7. swarms/structs/agent.py +211 -163
  8. swarms/structs/aop.py +8 -1
  9. swarms/structs/auto_swarm_builder.py +271 -210
  10. swarms/structs/conversation.py +23 -56
  11. swarms/structs/hiearchical_swarm.py +93 -122
  12. swarms/structs/ma_utils.py +96 -0
  13. swarms/structs/mixture_of_agents.py +20 -103
  14. swarms/structs/{multi_agent_orchestrator.py → multi_agent_router.py} +32 -95
  15. swarms/structs/output_types.py +3 -16
  16. swarms/structs/stopping_conditions.py +30 -0
  17. swarms/structs/swarm_router.py +57 -5
  18. swarms/structs/swarming_architectures.py +576 -185
  19. swarms/telemetry/main.py +6 -2
  20. swarms/tools/mcp_client.py +209 -53
  21. swarms/tools/mcp_integration.py +1 -53
  22. swarms/utils/formatter.py +15 -1
  23. swarms/utils/generate_keys.py +64 -0
  24. swarms/utils/history_output_formatter.py +2 -0
  25. {swarms-7.7.1.dist-info → swarms-7.7.3.dist-info}/METADATA +98 -263
  26. {swarms-7.7.1.dist-info → swarms-7.7.3.dist-info}/RECORD +29 -38
  27. swarms/schemas/agent_input_schema.py +0 -149
  28. swarms/structs/agents_available.py +0 -87
  29. swarms/structs/async_workflow.py +0 -818
  30. swarms/structs/graph_swarm.py +0 -612
  31. swarms/structs/octotools.py +0 -844
  32. swarms/structs/pulsar_swarm.py +0 -469
  33. swarms/structs/queue_swarm.py +0 -193
  34. swarms/structs/swarm_builder.py +0 -395
  35. swarms/structs/swarm_load_balancer.py +0 -344
  36. swarms/structs/swarm_output_type.py +0 -23
  37. swarms/structs/talk_hier.py +0 -729
  38. {swarms-7.7.1.dist-info → swarms-7.7.3.dist-info}/LICENSE +0 -0
  39. {swarms-7.7.1.dist-info → swarms-7.7.3.dist-info}/WHEEL +0 -0
  40. {swarms-7.7.1.dist-info → swarms-7.7.3.dist-info}/entry_points.txt +0 -0
@@ -1,395 +0,0 @@
1
- import os
2
- from typing import List, Optional
3
-
4
- from loguru import logger
5
- from pydantic import BaseModel, Field
6
- from pydantic.v1 import validator
7
- from tenacity import (
8
- retry,
9
- stop_after_attempt,
10
- wait_exponential,
11
- )
12
-
13
- from swarms.structs.agent import Agent
14
- from swarms.structs.swarm_router import SwarmRouter, SwarmType
15
- from swarms.utils.function_caller_model import OpenAIFunctionCaller
16
-
17
-
18
- BOSS_SYSTEM_PROMPT = """
19
- Manage a swarm of worker agents to efficiently serve the user by deciding whether to create new agents or delegate tasks. Ensure operations are efficient and effective.
20
-
21
- ### Instructions:
22
-
23
- 1. **Task Assignment**:
24
- - Analyze available worker agents when a task is presented.
25
- - Delegate tasks to existing agents with clear, direct, and actionable instructions if an appropriate agent is available.
26
- - If no suitable agent exists, create a new agent with a fitting system prompt to handle the task.
27
-
28
- 2. **Agent Creation**:
29
- - Name agents according to the task they are intended to perform (e.g., "Twitter Marketing Agent").
30
- - Provide each new agent with a concise and clear system prompt that includes its role, objectives, and any tools it can utilize.
31
-
32
- 3. **Efficiency**:
33
- - Minimize redundancy and maximize task completion speed.
34
- - Avoid unnecessary agent creation if an existing agent can fulfill the task.
35
-
36
- 4. **Communication**:
37
- - Be explicit in task delegation instructions to avoid ambiguity and ensure effective task execution.
38
- - Require agents to report back on task completion or encountered issues.
39
-
40
- 5. **Reasoning and Decisions**:
41
- - Offer brief reasoning when selecting or creating agents to maintain transparency.
42
- - Avoid using an agent if unnecessary, with a clear explanation if no agents are suitable for a task.
43
-
44
- # Output Format
45
-
46
- Present your plan in clear, bullet-point format or short concise paragraphs, outlining task assignment, agent creation, efficiency strategies, and communication protocols.
47
-
48
- # Notes
49
-
50
- - Preserve transparency by always providing reasoning for task-agent assignments and creation.
51
- - Ensure instructions to agents are unambiguous to minimize error.
52
-
53
- """
54
-
55
-
56
- class AgentConfig(BaseModel):
57
- """Configuration for an individual agent in a swarm"""
58
-
59
- name: str = Field(
60
- description="The name of the agent",
61
- )
62
- description: str = Field(
63
- description="A description of the agent's purpose and capabilities",
64
- )
65
- system_prompt: str = Field(
66
- description="The system prompt that defines the agent's behavior",
67
- )
68
-
69
-
70
- class SwarmConfig(BaseModel):
71
- """Configuration for a swarm of cooperative agents"""
72
-
73
- name: str = Field(
74
- description="The name of the swarm",
75
- example="Research-Writing-Swarm",
76
- )
77
- description: str = Field(
78
- description="The description of the swarm's purpose and capabilities",
79
- example="A swarm of agents that work together to research topics and write articles",
80
- )
81
- agents: List[AgentConfig] = Field(
82
- description="The list of agents that make up the swarm",
83
- )
84
- max_loops: int = Field(
85
- description="The maximum number of loops for the swarm to iterate on",
86
- )
87
-
88
- @validator("agents")
89
- def validate_agents(cls, v):
90
- if not v:
91
- raise ValueError("Swarm must have at least one agent")
92
- return v
93
-
94
-
95
- class AutoSwarmBuilderOutput(BaseModel):
96
- """A class that automatically builds and manages swarms of AI agents with enhanced error handling."""
97
-
98
- name: Optional[str] = Field(
99
- description="The name of the swarm",
100
- example="DefaultSwarm",
101
- default=None,
102
- )
103
- description: Optional[str] = Field(
104
- description="The description of the swarm's purpose and capabilities",
105
- example="Generic AI Agent Swarm",
106
- default=None,
107
- )
108
- verbose: Optional[bool] = Field(
109
- description="Whether to display verbose output",
110
- default=None,
111
- )
112
- model_name: Optional[str] = Field(
113
- description="The name of the OpenAI model to use",
114
- default=None,
115
- )
116
- boss_output_schema: Optional[list] = Field(
117
- description="The schema for the output of the BOSS system prompt",
118
- default=None,
119
- )
120
- director_agents_created: Optional[SwarmConfig] = Field(
121
- description="The agents created by the director",
122
- default=None,
123
- )
124
- swarm_router_outputs: Optional[list] = Field(
125
- description="The outputs from the swarm router",
126
- default=None,
127
- )
128
- max_loops: Optional[int] = Field(
129
- description="The maximum number of loops for the swarm to iterate on",
130
- default=None,
131
- )
132
- swarm_type: Optional[SwarmType] = Field(
133
- description="The type of swarm to build",
134
- default=None,
135
- )
136
-
137
-
138
- class AutoSwarmBuilder:
139
- """A class that automatically builds and manages swarms of AI agents with enhanced error handling."""
140
-
141
- def __init__(
142
- self,
143
- name: Optional[str] = "autonomous-swarm-builder",
144
- description: Optional[
145
- str
146
- ] = "Given a task, this swarm will automatically create specialized agents and route it to the appropriate agents.",
147
- verbose: bool = True,
148
- model_name: str = "gpt-4o",
149
- boss_output_schema: list = None,
150
- swarm_router_outputs: AutoSwarmBuilderOutput = None,
151
- max_loops: int = 1,
152
- swarm_type: str = "SequentialWorkflow",
153
- auto_generate_prompts_for_agents: bool = False,
154
- shared_memory_system: callable = None,
155
- ):
156
- self.name = name or "DefaultSwarm"
157
- self.description = description or "Generic AI Agent Swarm"
158
- self.verbose = verbose
159
- self.agents_pool = []
160
- self.api_key = os.getenv("OPENAI_API_KEY")
161
- self.model_name = model_name
162
- self.boss_output_schema = boss_output_schema
163
- self.max_loops = max_loops
164
- self.swarm_type = swarm_type
165
- self.auto_generate_prompts_for_agents = (
166
- auto_generate_prompts_for_agents
167
- )
168
- self.shared_memory_system = shared_memory_system
169
- self.auto_swarm_builder_output = AutoSwarmBuilderOutput(
170
- name=name,
171
- description=description,
172
- verbose=verbose,
173
- model_name=model_name,
174
- boss_output_schema=boss_output_schema or [],
175
- swarm_router_outputs=swarm_router_outputs or [],
176
- max_loops=max_loops,
177
- swarm_type=swarm_type,
178
- )
179
-
180
- logger.info(
181
- "Initialized AutoSwarmBuilder",
182
- extra={
183
- "swarm_name": self.name,
184
- "description": self.description,
185
- "model": self.model_name,
186
- },
187
- )
188
-
189
- def run(
190
- self,
191
- task: str,
192
- image_url: Optional[str] = None,
193
- *args,
194
- **kwargs,
195
- ):
196
- """Run the swarm on a given task with error handling and retries."""
197
- if not task or not task.strip():
198
- raise ValueError("Task cannot be empty")
199
-
200
- logger.info("Starting swarm execution", extra={"task": task})
201
-
202
- try:
203
- # Create agents for the task
204
- agents = self._create_agents(task)
205
- if not agents:
206
- raise ValueError(
207
- "No agents were created for the task"
208
- )
209
-
210
- # Execute the task through the swarm router
211
- logger.info(
212
- "Routing task through swarm",
213
- extra={"num_agents": len(agents)},
214
- )
215
- output = self.swarm_router(
216
- agents=agents,
217
- task=task,
218
- image_url=image_url,
219
- *args,
220
- **kwargs,
221
- )
222
- self.auto_swarm_builder_output.swarm_router_outputs.append(
223
- output
224
- )
225
- print(output)
226
-
227
- logger.info("Swarm execution completed successfully")
228
- # return output
229
- return self.auto_swarm_builder_output.model_dump_json(
230
- indent=4
231
- )
232
-
233
- except Exception as e:
234
- logger.error(
235
- f"Error during swarm execution: {str(e)}",
236
- )
237
- raise e
238
-
239
- def _create_agents(
240
- self,
241
- task: str,
242
- ) -> List[Agent]:
243
- """Create the necessary agents for a task with enhanced error handling."""
244
- logger.info("Creating agents for task", extra={"task": task})
245
-
246
- try:
247
- model = OpenAIFunctionCaller(
248
- system_prompt=BOSS_SYSTEM_PROMPT,
249
- api_key=self.api_key,
250
- temperature=0.1,
251
- base_model=SwarmConfig,
252
- )
253
-
254
- agents_config = model.run(task)
255
- logger.info(
256
- f"Director has successfully created agents: {agents_config}"
257
- )
258
- self.auto_swarm_builder_output.director_agents_created = (
259
- agents_config
260
- )
261
-
262
- if isinstance(agents_config, dict):
263
- agents_config = SwarmConfig(**agents_config)
264
-
265
- # Update swarm configuration
266
- self.name = agents_config.name
267
- self.description = agents_config.description
268
-
269
- # Create agents from configuration
270
- agents = []
271
- for agent_config in agents_config.agents:
272
- if isinstance(agent_config, dict):
273
- agent_config = AgentConfig(**agent_config)
274
-
275
- agent = self.build_agent(
276
- agent_name=agent_config.name,
277
- agent_description=agent_config.description,
278
- agent_system_prompt=agent_config.system_prompt,
279
- )
280
- agents.append(agent)
281
-
282
- print(
283
- f"Agent created: {agent_config.name}: Description: {agent_config.description}"
284
- )
285
-
286
- # # Add available agents showcase to system prompts
287
- # agents_available = showcase_available_agents(
288
- # name=self.name,
289
- # description=self.description,
290
- # agents=agents,
291
- # )
292
-
293
- # for agent in agents:
294
- # agent.system_prompt += "\n" + agents_available
295
-
296
- logger.info(
297
- "Successfully created agents",
298
- extra={"num_agents": len(agents)},
299
- )
300
- return agents
301
-
302
- except Exception as e:
303
- logger.error(
304
- f"Error creating agents: {str(e)}", exc_info=True
305
- )
306
- raise
307
-
308
- def build_agent(
309
- self,
310
- agent_name: str,
311
- agent_description: str,
312
- agent_system_prompt: str,
313
- *args,
314
- **kwargs,
315
- ) -> Agent:
316
- """Build a single agent with enhanced error handling."""
317
- logger.info(
318
- "Building agent", extra={"agent_name": agent_name}
319
- )
320
-
321
- try:
322
- agent = Agent(
323
- agent_name=agent_name,
324
- description=agent_description,
325
- system_prompt=agent_system_prompt,
326
- model_name="gpt-4o",
327
- verbose=self.verbose,
328
- dynamic_temperature_enabled=False,
329
- return_step_meta=False,
330
- output_type="str",
331
- streaming_on=True,
332
- )
333
- return agent
334
-
335
- except Exception as e:
336
- logger.error(
337
- f"Error building agent: {str(e)}", exc_info=True
338
- )
339
- raise
340
-
341
- @retry(
342
- stop=stop_after_attempt(3),
343
- wait=wait_exponential(multiplier=1, min=4, max=10),
344
- )
345
- def swarm_router(
346
- self,
347
- agents: List[Agent],
348
- task: str,
349
- img: Optional[str] = None,
350
- *args,
351
- **kwargs,
352
- ) -> str:
353
- """Route tasks between agents in the swarm with error handling and retries."""
354
- logger.info(
355
- "Initializing swarm router",
356
- extra={"num_agents": len(agents)},
357
- )
358
-
359
- try:
360
- swarm_router_instance = SwarmRouter(
361
- name=self.name,
362
- description=self.description,
363
- agents=agents,
364
- swarm_type=self.swarm_type,
365
- auto_generate_prompts=self.auto_generate_prompts_for_agents,
366
- )
367
-
368
- # formatted_task = f"{self.name} {self.description} {task}"
369
- result = swarm_router_instance.run(
370
- task=task, *args, **kwargs
371
- )
372
-
373
- logger.info("Successfully completed swarm routing")
374
- return result
375
-
376
- except Exception as e:
377
- logger.error(
378
- f"Error in swarm router: {str(e)}", exc_info=True
379
- )
380
- raise
381
-
382
-
383
- # swarm = AutoSwarmBuilder(
384
- # name="ChipDesign-Swarm",
385
- # description="A swarm of specialized AI agents for chip design",
386
- # swarm_type="ConcurrentWorkflow",
387
- # )
388
-
389
- # try:
390
- # result = swarm.run(
391
- # "Design a new AI accelerator chip optimized for transformer model inference..."
392
- # )
393
- # print(result)
394
- # except Exception as e:
395
- # print(f"An error occurred: {e}")