swarms 7.8.4__py3-none-any.whl → 7.8.7__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.
- swarms/agents/ape_agent.py +5 -22
- swarms/agents/consistency_agent.py +1 -1
- swarms/agents/i_agent.py +1 -1
- swarms/agents/reasoning_agents.py +99 -3
- swarms/agents/reasoning_duo.py +1 -1
- swarms/cli/main.py +1 -1
- swarms/communication/__init__.py +1 -0
- swarms/communication/duckdb_wrap.py +32 -2
- swarms/communication/pulsar_struct.py +45 -19
- swarms/communication/redis_wrap.py +56 -11
- swarms/communication/supabase_wrap.py +1659 -0
- swarms/prompts/prompt.py +0 -3
- swarms/schemas/agent_completion_response.py +71 -0
- swarms/schemas/agent_rag_schema.py +7 -0
- swarms/schemas/conversation_schema.py +9 -0
- swarms/schemas/llm_agent_schema.py +99 -81
- swarms/schemas/swarms_api_schemas.py +164 -0
- swarms/structs/__init__.py +14 -11
- swarms/structs/agent.py +219 -199
- swarms/structs/agent_rag_handler.py +685 -0
- swarms/structs/base_swarm.py +2 -1
- swarms/structs/conversation.py +608 -87
- swarms/structs/csv_to_agent.py +153 -100
- swarms/structs/deep_research_swarm.py +197 -193
- swarms/structs/dynamic_conversational_swarm.py +18 -7
- swarms/structs/hiearchical_swarm.py +1 -1
- swarms/structs/hybrid_hiearchical_peer_swarm.py +2 -18
- swarms/structs/image_batch_processor.py +261 -0
- swarms/structs/interactive_groupchat.py +356 -0
- swarms/structs/ma_blocks.py +75 -0
- swarms/structs/majority_voting.py +1 -1
- swarms/structs/mixture_of_agents.py +1 -1
- swarms/structs/multi_agent_router.py +3 -2
- swarms/structs/rearrange.py +3 -3
- swarms/structs/sequential_workflow.py +3 -3
- swarms/structs/swarm_matcher.py +500 -411
- swarms/structs/swarm_router.py +15 -97
- swarms/structs/swarming_architectures.py +1 -1
- swarms/tools/mcp_client_call.py +3 -0
- swarms/utils/__init__.py +10 -2
- swarms/utils/check_all_model_max_tokens.py +43 -0
- swarms/utils/generate_keys.py +0 -27
- swarms/utils/history_output_formatter.py +5 -20
- swarms/utils/litellm_wrapper.py +208 -60
- swarms/utils/output_types.py +24 -0
- swarms/utils/vllm_wrapper.py +5 -6
- swarms/utils/xml_utils.py +37 -2
- {swarms-7.8.4.dist-info → swarms-7.8.7.dist-info}/METADATA +31 -55
- {swarms-7.8.4.dist-info → swarms-7.8.7.dist-info}/RECORD +53 -48
- swarms/structs/multi_agent_collab.py +0 -242
- swarms/structs/output_types.py +0 -6
- swarms/utils/markdown_message.py +0 -21
- swarms/utils/visualizer.py +0 -510
- swarms/utils/wrapper_clusterop.py +0 -127
- /swarms/{tools → schemas}/tool_schema_base_model.py +0 -0
- {swarms-7.8.4.dist-info → swarms-7.8.7.dist-info}/LICENSE +0 -0
- {swarms-7.8.4.dist-info → swarms-7.8.7.dist-info}/WHEEL +0 -0
- {swarms-7.8.4.dist-info → swarms-7.8.7.dist-info}/entry_points.txt +0 -0
swarms/utils/visualizer.py
DELETED
@@ -1,510 +0,0 @@
|
|
1
|
-
import asyncio
|
2
|
-
from dataclasses import dataclass
|
3
|
-
from datetime import datetime
|
4
|
-
from typing import Any, Callable, Dict, List, Optional
|
5
|
-
|
6
|
-
import psutil
|
7
|
-
from rich.console import Console
|
8
|
-
from rich.layout import Layout
|
9
|
-
from rich.live import Live
|
10
|
-
from rich.panel import Panel
|
11
|
-
from rich.progress import (
|
12
|
-
Progress,
|
13
|
-
SpinnerColumn,
|
14
|
-
TextColumn,
|
15
|
-
TimeElapsedColumn,
|
16
|
-
)
|
17
|
-
from rich.table import Table
|
18
|
-
from rich.text import Text
|
19
|
-
from rich.tree import Tree
|
20
|
-
|
21
|
-
from swarms.structs.agent import Agent
|
22
|
-
|
23
|
-
try:
|
24
|
-
import pynvml
|
25
|
-
|
26
|
-
pynvml.nvmlInit()
|
27
|
-
GPU_ENABLED = True
|
28
|
-
except ImportError:
|
29
|
-
GPU_ENABLED = False
|
30
|
-
|
31
|
-
|
32
|
-
@dataclass
|
33
|
-
class SwarmMetadata:
|
34
|
-
name: Optional[str] = None
|
35
|
-
description: Optional[str] = None
|
36
|
-
version: Optional[str] = None
|
37
|
-
type: Optional[str] = None # hierarchical, parallel, sequential
|
38
|
-
created_at: Optional[datetime] = None
|
39
|
-
author: Optional[str] = None
|
40
|
-
tags: Optional[List[str]] = None
|
41
|
-
primary_objective: Optional[str] = None
|
42
|
-
secondary_objectives: Optional[List[str]] = None
|
43
|
-
|
44
|
-
def __post_init__(self):
|
45
|
-
self.tags = self.tags or []
|
46
|
-
self.secondary_objectives = self.secondary_objectives or []
|
47
|
-
self.created_at = self.created_at or datetime.now()
|
48
|
-
|
49
|
-
|
50
|
-
class SwarmVisualizationRich:
|
51
|
-
def __init__(
|
52
|
-
self,
|
53
|
-
swarm_metadata: SwarmMetadata,
|
54
|
-
agents: List[Agent],
|
55
|
-
update_resources: bool = True,
|
56
|
-
refresh_rate: float = 0.1,
|
57
|
-
):
|
58
|
-
"""
|
59
|
-
Initializes the visualizer with a list of agents.
|
60
|
-
|
61
|
-
Args:
|
62
|
-
swarm_metadata (SwarmMetadata): Metadata for the swarm.
|
63
|
-
agents (List[Agent]): List of root agents.
|
64
|
-
update_resources (bool): Whether to update system resource stats.
|
65
|
-
refresh_rate (float): Refresh rate for the live visualization.
|
66
|
-
"""
|
67
|
-
self.swarm_metadata = swarm_metadata
|
68
|
-
self.agents = agents
|
69
|
-
self.update_resources = update_resources
|
70
|
-
self.refresh_rate = refresh_rate
|
71
|
-
self.console = Console()
|
72
|
-
self.live = None
|
73
|
-
# A dictionary mapping agent names to list of output messages
|
74
|
-
self.output_history: Dict[str, List[Dict[str, Any]]] = {}
|
75
|
-
|
76
|
-
# System monitoring
|
77
|
-
self.cores_available = 0
|
78
|
-
self.memory_usage = "N/A"
|
79
|
-
self.gpu_power = "N/A"
|
80
|
-
self.start_time = datetime.now()
|
81
|
-
|
82
|
-
if self.update_resources:
|
83
|
-
self._update_resource_stats()
|
84
|
-
|
85
|
-
def _format_uptime(self) -> str:
|
86
|
-
"""Formats the swarm's uptime."""
|
87
|
-
delta = datetime.now() - self.start_time
|
88
|
-
hours, remainder = divmod(delta.seconds, 3600)
|
89
|
-
minutes, seconds = divmod(remainder, 60)
|
90
|
-
return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
|
91
|
-
|
92
|
-
def _build_agent_tree(self, agents: List[Agent]) -> Tree:
|
93
|
-
"""
|
94
|
-
Builds a detailed tree visualization for a list of agents.
|
95
|
-
|
96
|
-
Args:
|
97
|
-
agents (List[Agent]): The list of root agents.
|
98
|
-
|
99
|
-
Returns:
|
100
|
-
Tree: A rich Tree object displaying agent metadata.
|
101
|
-
"""
|
102
|
-
tree = Tree("[bold underline]Agents[/bold underline]")
|
103
|
-
for agent in agents:
|
104
|
-
self._add_agent_to_tree(agent, tree)
|
105
|
-
return tree
|
106
|
-
|
107
|
-
def _add_agent_to_tree(self, agent: Agent, tree: Tree) -> None:
|
108
|
-
"""
|
109
|
-
Recursively adds an agent and its children to the given tree.
|
110
|
-
|
111
|
-
Args:
|
112
|
-
agent (Agent): The agent to add.
|
113
|
-
tree (Tree): The tree to update.
|
114
|
-
"""
|
115
|
-
agent_info = [
|
116
|
-
f"[bold cyan]{agent.name}[/bold cyan]",
|
117
|
-
f"[yellow]Role:[/yellow] {agent.role}",
|
118
|
-
]
|
119
|
-
|
120
|
-
# # Add any custom metadata from the agent (if available)
|
121
|
-
# for key, value in getattr(agent, "metadata", {}).items():
|
122
|
-
# agent_info.append(f"[white]{key}:[/white] {value}")
|
123
|
-
|
124
|
-
# # Parameters summary if available
|
125
|
-
# parameters = getattr(agent, "parameters", {})
|
126
|
-
# if parameters:
|
127
|
-
# param_summary = ", ".join(f"{k}: {v}" for k, v in parameters.items())
|
128
|
-
# agent_info.append(f"[white]Parameters:[/white] {param_summary}")
|
129
|
-
|
130
|
-
node_text = "\n".join(agent_info)
|
131
|
-
branch = tree.add(node_text)
|
132
|
-
for child in getattr(agent, "children", []):
|
133
|
-
self._add_agent_to_tree(child, branch)
|
134
|
-
|
135
|
-
def _count_agents(self, agents: List[Agent]) -> int:
|
136
|
-
"""
|
137
|
-
Recursively counts total number of agents from a list of root agents.
|
138
|
-
|
139
|
-
Args:
|
140
|
-
agents (List[Agent]): List of agents.
|
141
|
-
|
142
|
-
Returns:
|
143
|
-
int: Total count of agents including children.
|
144
|
-
"""
|
145
|
-
return len(agents)
|
146
|
-
|
147
|
-
def _create_unified_info_panel(self) -> Panel:
|
148
|
-
"""
|
149
|
-
Creates a unified panel showing swarm metadata and agents' metadata.
|
150
|
-
"""
|
151
|
-
info_layout = Layout()
|
152
|
-
info_layout.split_column(
|
153
|
-
Layout(name="metadata", size=15),
|
154
|
-
Layout(name="architecture"),
|
155
|
-
)
|
156
|
-
|
157
|
-
total_agents = self._count_agents(self.agents)
|
158
|
-
|
159
|
-
# Metadata section
|
160
|
-
metadata_table = Table.grid(padding=1, expand=True)
|
161
|
-
metadata_table.add_column("Label", style="bold cyan")
|
162
|
-
metadata_table.add_column("Value", style="white")
|
163
|
-
|
164
|
-
# Update system resources if needed
|
165
|
-
if self.update_resources:
|
166
|
-
self._update_resource_stats()
|
167
|
-
|
168
|
-
# Wrap the description text properly
|
169
|
-
description_text = Text(
|
170
|
-
self.swarm_metadata.description or "", style="italic"
|
171
|
-
)
|
172
|
-
description_text.wrap(self.console, width=60, overflow="fold")
|
173
|
-
|
174
|
-
metadata_table.add_row(
|
175
|
-
"Swarm Name", self.swarm_metadata.name or "N/A"
|
176
|
-
)
|
177
|
-
metadata_table.add_row("Description", description_text)
|
178
|
-
metadata_table.add_row(
|
179
|
-
"Version", self.swarm_metadata.version or "N/A"
|
180
|
-
)
|
181
|
-
metadata_table.add_row("Total Agents", str(total_agents))
|
182
|
-
metadata_table.add_row(
|
183
|
-
"Author", self.swarm_metadata.author or "N/A"
|
184
|
-
)
|
185
|
-
metadata_table.add_row(
|
186
|
-
"System",
|
187
|
-
f"CPU: {self.cores_available} cores | Memory: {self.memory_usage}",
|
188
|
-
)
|
189
|
-
metadata_table.add_row(
|
190
|
-
"Primary Objective",
|
191
|
-
self.swarm_metadata.primary_objective or "N/A",
|
192
|
-
)
|
193
|
-
|
194
|
-
info_layout["metadata"].update(metadata_table)
|
195
|
-
|
196
|
-
# Architecture section with the agent tree
|
197
|
-
architecture_tree = self._build_agent_tree(self.agents)
|
198
|
-
info_layout["architecture"].update(architecture_tree)
|
199
|
-
|
200
|
-
return Panel(
|
201
|
-
info_layout,
|
202
|
-
title="[bold]Swarm Information & Architecture[/bold]",
|
203
|
-
)
|
204
|
-
|
205
|
-
def _create_outputs_panel(self) -> Panel:
|
206
|
-
"""
|
207
|
-
Creates a panel that displays stacked message history for all agents.
|
208
|
-
"""
|
209
|
-
all_messages = []
|
210
|
-
|
211
|
-
def collect_agent_messages(agent: Agent):
|
212
|
-
"""Recursively collect messages from an agent and its children."""
|
213
|
-
messages = self.output_history.get(agent.name, [])
|
214
|
-
for msg in messages:
|
215
|
-
all_messages.append(
|
216
|
-
{
|
217
|
-
"agent": agent.name,
|
218
|
-
"time": msg["time"],
|
219
|
-
"content": msg["content"],
|
220
|
-
"style": msg["style"],
|
221
|
-
}
|
222
|
-
)
|
223
|
-
for child in getattr(agent, "children", []):
|
224
|
-
collect_agent_messages(child)
|
225
|
-
|
226
|
-
# Collect messages from every root agent
|
227
|
-
for agent in self.agents:
|
228
|
-
collect_agent_messages(agent)
|
229
|
-
|
230
|
-
# Sort messages by timestamp
|
231
|
-
all_messages.sort(key=lambda x: x["time"])
|
232
|
-
|
233
|
-
messages_container = []
|
234
|
-
for msg in all_messages:
|
235
|
-
message_text = Text()
|
236
|
-
message_text.append(f"[{msg['time']}] ", style="dim")
|
237
|
-
message_text.append(
|
238
|
-
f"{msg['agent']}: ", style="bold cyan"
|
239
|
-
)
|
240
|
-
message_text.append(msg["content"], style=msg["style"])
|
241
|
-
messages_container.append(message_text)
|
242
|
-
|
243
|
-
if messages_container:
|
244
|
-
final_text = Text("\n").join(messages_container)
|
245
|
-
else:
|
246
|
-
final_text = Text("No messages yet...", style="dim")
|
247
|
-
|
248
|
-
return Panel(
|
249
|
-
final_text,
|
250
|
-
title="[bold]Agent Communication Log[/bold]",
|
251
|
-
border_style="green",
|
252
|
-
padding=(1, 2),
|
253
|
-
)
|
254
|
-
|
255
|
-
def _update_resource_stats(self):
|
256
|
-
"""Updates system resource statistics."""
|
257
|
-
self.cores_available = psutil.cpu_count(logical=True)
|
258
|
-
mem_info = psutil.virtual_memory()
|
259
|
-
total_gb = mem_info.total / (1024**3)
|
260
|
-
used_gb = mem_info.used / (1024**3)
|
261
|
-
self.memory_usage = f"{used_gb:.1f}GB / {total_gb:.1f}GB ({mem_info.percent}%)"
|
262
|
-
|
263
|
-
if GPU_ENABLED:
|
264
|
-
try:
|
265
|
-
device_count = pynvml.nvmlDeviceGetCount()
|
266
|
-
gpu_info = []
|
267
|
-
for i in range(device_count):
|
268
|
-
handle = pynvml.nvmlDeviceGetHandleByIndex(i)
|
269
|
-
name = pynvml.nvmlDeviceGetName(handle).decode()
|
270
|
-
mem = pynvml.nvmlDeviceGetMemoryInfo(handle)
|
271
|
-
usage = (mem.used / mem.total) * 100
|
272
|
-
gpu_info.append(f"{name}: {usage:.1f}%")
|
273
|
-
self.gpu_power = " | ".join(gpu_info)
|
274
|
-
except Exception as e:
|
275
|
-
self.gpu_power = f"GPU Error: {str(e)}"
|
276
|
-
else:
|
277
|
-
self.gpu_power = "No GPU detected"
|
278
|
-
|
279
|
-
async def stream_output(
|
280
|
-
self,
|
281
|
-
agent: Agent,
|
282
|
-
text: str,
|
283
|
-
title: Optional[str] = None,
|
284
|
-
style: str = "bold cyan",
|
285
|
-
delay: float = 0.05,
|
286
|
-
by_word: bool = False,
|
287
|
-
):
|
288
|
-
"""
|
289
|
-
Streams output for a specific agent with token-by-token animation.
|
290
|
-
|
291
|
-
Args:
|
292
|
-
agent (Agent): The agent whose output is being streamed.
|
293
|
-
text (str): The text to stream.
|
294
|
-
title (Optional[str]): Custom title for the output panel.
|
295
|
-
style (str): Style for the output text.
|
296
|
-
delay (float): Delay between tokens.
|
297
|
-
by_word (bool): If True, stream word by word instead of character by character.
|
298
|
-
"""
|
299
|
-
display_text = Text(style=style)
|
300
|
-
current_output = ""
|
301
|
-
|
302
|
-
tokens = text.split() if by_word else text
|
303
|
-
title = title or f"{agent.name} Output"
|
304
|
-
|
305
|
-
for token in tokens:
|
306
|
-
token_with_space = token + (" " if by_word else "")
|
307
|
-
current_output += token_with_space
|
308
|
-
display_text.append(token_with_space)
|
309
|
-
|
310
|
-
if agent.name not in self.output_history:
|
311
|
-
self.output_history[agent.name] = []
|
312
|
-
|
313
|
-
if token == tokens[-1]:
|
314
|
-
timestamp = datetime.now().strftime("%H:%M:%S")
|
315
|
-
self.output_history[agent.name].append(
|
316
|
-
{
|
317
|
-
"time": timestamp,
|
318
|
-
"content": current_output,
|
319
|
-
"style": style,
|
320
|
-
}
|
321
|
-
)
|
322
|
-
|
323
|
-
if self.live:
|
324
|
-
self.live.update(self._create_layout())
|
325
|
-
await asyncio.sleep(delay)
|
326
|
-
|
327
|
-
def log_agent_output(self, agent: Agent, text: str):
|
328
|
-
asyncio.create_task(
|
329
|
-
self.stream_output(
|
330
|
-
agent=agent,
|
331
|
-
text=text,
|
332
|
-
title=f"{agent.name} Output {agent.max_loops}",
|
333
|
-
)
|
334
|
-
)
|
335
|
-
|
336
|
-
async def print_progress(
|
337
|
-
self,
|
338
|
-
description: str,
|
339
|
-
task_fn: Callable,
|
340
|
-
*args: Any,
|
341
|
-
**kwargs: Any,
|
342
|
-
) -> Any:
|
343
|
-
"""
|
344
|
-
Displays a progress spinner while executing a task.
|
345
|
-
|
346
|
-
Args:
|
347
|
-
description (str): Task description.
|
348
|
-
task_fn (Callable): Function to execute.
|
349
|
-
*args (Any): Arguments for task_fn.
|
350
|
-
**kwargs (Any): Keyword arguments for task_fn.
|
351
|
-
|
352
|
-
Returns:
|
353
|
-
Any: The result of task_fn.
|
354
|
-
"""
|
355
|
-
progress = Progress(
|
356
|
-
SpinnerColumn(),
|
357
|
-
TextColumn("[progress.description]{task.description}"),
|
358
|
-
TimeElapsedColumn(),
|
359
|
-
)
|
360
|
-
|
361
|
-
try:
|
362
|
-
with progress:
|
363
|
-
task = progress.add_task(description, total=None)
|
364
|
-
result = await task_fn(*args, **kwargs)
|
365
|
-
progress.update(task, completed=True)
|
366
|
-
return result
|
367
|
-
except Exception as e:
|
368
|
-
progress.stop()
|
369
|
-
raise e
|
370
|
-
|
371
|
-
def _create_layout(self) -> Layout:
|
372
|
-
"""Creates the main visualization layout."""
|
373
|
-
layout = Layout()
|
374
|
-
layout.split_row(
|
375
|
-
Layout(name="info", ratio=2),
|
376
|
-
Layout(name="outputs", ratio=3),
|
377
|
-
)
|
378
|
-
|
379
|
-
layout["info"].update(self._create_unified_info_panel())
|
380
|
-
layout["outputs"].update(self._create_outputs_panel())
|
381
|
-
|
382
|
-
return layout
|
383
|
-
|
384
|
-
async def start(self):
|
385
|
-
"""Starts the visualization with live updates."""
|
386
|
-
with Live(
|
387
|
-
self._create_layout(),
|
388
|
-
refresh_per_second=int(1 / self.refresh_rate),
|
389
|
-
) as self.live:
|
390
|
-
while True:
|
391
|
-
|
392
|
-
def process_agent_streams(agent: Agent):
|
393
|
-
while not agent.output_stream.empty():
|
394
|
-
new_output = agent.output_stream.get()
|
395
|
-
asyncio.create_task(
|
396
|
-
self.stream_output(agent, new_output)
|
397
|
-
)
|
398
|
-
for child in getattr(agent, "children", []):
|
399
|
-
process_agent_streams(child)
|
400
|
-
|
401
|
-
# Process streams for each root agent
|
402
|
-
for agent in self.agents:
|
403
|
-
process_agent_streams(agent)
|
404
|
-
await asyncio.sleep(self.refresh_rate)
|
405
|
-
|
406
|
-
|
407
|
-
# # Example usage
|
408
|
-
# if __name__ == "__main__":
|
409
|
-
# # Create swarm metadata
|
410
|
-
# swarm_metadata = SwarmMetadata(
|
411
|
-
# name="Financial Advisory Swarm",
|
412
|
-
# description="Intelligent swarm for financial analysis and advisory",
|
413
|
-
# version="1.0.0",
|
414
|
-
# type="hierarchical",
|
415
|
-
# created_at=datetime.now(),
|
416
|
-
# author="AI Research Team",
|
417
|
-
# # tags=["finance", "analysis", "advisory"],
|
418
|
-
# primary_objective="Provide comprehensive financial analysis and recommendations",
|
419
|
-
# secondary_objectives=[
|
420
|
-
# "Monitor market trends",
|
421
|
-
# "Analyze competitor behavior",
|
422
|
-
# "Generate investment strategies",
|
423
|
-
# ],
|
424
|
-
# )
|
425
|
-
|
426
|
-
# # Create agent hierarchy with detailed parameters
|
427
|
-
# analyst = Agent(
|
428
|
-
# name="Financial Analyst",
|
429
|
-
# role="Analysis",
|
430
|
-
# description="Analyzes financial data and market trends",
|
431
|
-
# agent_type="LLM",
|
432
|
-
# capabilities=[
|
433
|
-
# "data analysis",
|
434
|
-
# "trend detection",
|
435
|
-
# "risk assessment",
|
436
|
-
# ],
|
437
|
-
# parameters={"model": "gpt-4", "temperature": 0.7},
|
438
|
-
# metadata={
|
439
|
-
# "specialty": "Market Analysis",
|
440
|
-
# "confidence_threshold": "0.85",
|
441
|
-
# },
|
442
|
-
# )
|
443
|
-
|
444
|
-
# researcher = Agent(
|
445
|
-
# name="Market Researcher",
|
446
|
-
# role="Research",
|
447
|
-
# description="Conducts market research and competitor analysis",
|
448
|
-
# agent_type="Neural",
|
449
|
-
# capabilities=[
|
450
|
-
# "competitor analysis",
|
451
|
-
# "market sentiment",
|
452
|
-
# "trend forecasting",
|
453
|
-
# ],
|
454
|
-
# parameters={"batch_size": 32, "learning_rate": 0.001},
|
455
|
-
# metadata={
|
456
|
-
# "data_sources": "Bloomberg, Reuters",
|
457
|
-
# "update_frequency": "1h",
|
458
|
-
# },
|
459
|
-
# )
|
460
|
-
|
461
|
-
# advisor = Agent(
|
462
|
-
# name="Investment Advisor",
|
463
|
-
# role="Advisory",
|
464
|
-
# description="Provides investment recommendations",
|
465
|
-
# agent_type="Hybrid",
|
466
|
-
# capabilities=[
|
467
|
-
# "portfolio optimization",
|
468
|
-
# "risk management",
|
469
|
-
# "strategy generation",
|
470
|
-
# ],
|
471
|
-
# parameters={
|
472
|
-
# "risk_tolerance": "moderate",
|
473
|
-
# "time_horizon": "long",
|
474
|
-
# },
|
475
|
-
# metadata={
|
476
|
-
# "certification": "CFA Level 3",
|
477
|
-
# "specialization": "Equity",
|
478
|
-
# },
|
479
|
-
# children=[analyst, researcher],
|
480
|
-
# )
|
481
|
-
|
482
|
-
# # Create visualization
|
483
|
-
# viz = SwarmVisualizationRich(
|
484
|
-
# swarm_metadata=swarm_metadata,
|
485
|
-
# root_agent=advisor,
|
486
|
-
# refresh_rate=0.1,
|
487
|
-
# )
|
488
|
-
|
489
|
-
# # Example of streaming output simulation
|
490
|
-
# async def simulate_outputs():
|
491
|
-
# await viz.stream_output(
|
492
|
-
# advisor,
|
493
|
-
# "Analyzing market conditions...\nGenerating investment advice...",
|
494
|
-
# )
|
495
|
-
# await viz.stream_output(
|
496
|
-
# analyst,
|
497
|
-
# "Processing financial data...\nIdentifying trends...",
|
498
|
-
# )
|
499
|
-
# await viz.stream_output(
|
500
|
-
# researcher,
|
501
|
-
# "Researching competitor movements...\nAnalyzing market share...",
|
502
|
-
# )
|
503
|
-
|
504
|
-
# # Run the visualization
|
505
|
-
# async def main():
|
506
|
-
# viz_task = asyncio.create_task(viz.start())
|
507
|
-
# await simulate_outputs()
|
508
|
-
# await viz_task
|
509
|
-
|
510
|
-
# asyncio.run(main())
|
@@ -1,127 +0,0 @@
|
|
1
|
-
import platform
|
2
|
-
from typing import Any
|
3
|
-
|
4
|
-
|
5
|
-
from clusterops import (
|
6
|
-
execute_on_gpu,
|
7
|
-
execute_on_multiple_gpus,
|
8
|
-
list_available_gpus,
|
9
|
-
execute_with_all_cpu_cores,
|
10
|
-
execute_on_cpu,
|
11
|
-
)
|
12
|
-
from swarms.utils.loguru_logger import initialize_logger
|
13
|
-
|
14
|
-
logger = initialize_logger(log_folder="clusterops_wrapper")
|
15
|
-
|
16
|
-
|
17
|
-
def exec_callable_with_clusterops(
|
18
|
-
device: str = "cpu",
|
19
|
-
device_id: int = 1,
|
20
|
-
all_cores: bool = True,
|
21
|
-
all_gpus: bool = False,
|
22
|
-
func: callable = None,
|
23
|
-
enable_logging: bool = True,
|
24
|
-
*args,
|
25
|
-
**kwargs,
|
26
|
-
) -> Any:
|
27
|
-
"""
|
28
|
-
Executes a given function on a specified device, either CPU or GPU.
|
29
|
-
|
30
|
-
This method attempts to execute a given function on a specified device, either CPU or GPU. It logs the device selection and the number of cores or GPU ID used. If the device is set to CPU, it can use all available cores or a specific core specified by `device_id`. If the device is set to GPU, it uses the GPU specified by `device_id`.
|
31
|
-
|
32
|
-
Args:
|
33
|
-
device (str, optional): The device to use for execution. Defaults to "cpu".
|
34
|
-
device_id (int, optional): The ID of the GPU to use if device is set to "gpu". Defaults to 0.
|
35
|
-
all_cores (bool, optional): If True, uses all available CPU cores. Defaults to True.
|
36
|
-
all_gpus (bool, optional): If True, uses all available GPUs. Defaults to False.
|
37
|
-
func (callable): The function to execute.
|
38
|
-
enable_logging (bool, optional): If True, enables logging. Defaults to True.
|
39
|
-
*args: Additional positional arguments to be passed to the execution method.
|
40
|
-
**kwargs: Additional keyword arguments to be passed to the execution method.
|
41
|
-
|
42
|
-
Returns:
|
43
|
-
Any: The result of the execution.
|
44
|
-
|
45
|
-
Raises:
|
46
|
-
ValueError: If an invalid device is specified.
|
47
|
-
Exception: If any other error occurs during execution.
|
48
|
-
"""
|
49
|
-
if func is None:
|
50
|
-
raise ValueError("A callable function must be provided")
|
51
|
-
|
52
|
-
try:
|
53
|
-
if enable_logging:
|
54
|
-
logger.info(f"Attempting to run on device: {device}")
|
55
|
-
device = device.lower()
|
56
|
-
|
57
|
-
# Check if the platform is Windows and do nothing if true
|
58
|
-
if platform.system() == "Windows":
|
59
|
-
if enable_logging:
|
60
|
-
logger.info(
|
61
|
-
"Platform is Windows, not executing on device."
|
62
|
-
)
|
63
|
-
return None
|
64
|
-
|
65
|
-
if device == "cpu":
|
66
|
-
if enable_logging:
|
67
|
-
logger.info("Device set to CPU")
|
68
|
-
|
69
|
-
if all_cores:
|
70
|
-
if enable_logging:
|
71
|
-
logger.info("Using all CPU cores")
|
72
|
-
return execute_with_all_cpu_cores(
|
73
|
-
func, *args, **kwargs
|
74
|
-
)
|
75
|
-
|
76
|
-
if device_id is not None:
|
77
|
-
if enable_logging:
|
78
|
-
logger.info(
|
79
|
-
f"Using specific CPU core: {device_id}"
|
80
|
-
)
|
81
|
-
return execute_on_cpu(
|
82
|
-
device_id, func, *args, **kwargs
|
83
|
-
)
|
84
|
-
|
85
|
-
elif device == "gpu":
|
86
|
-
if enable_logging:
|
87
|
-
logger.info("Device set to GPU")
|
88
|
-
|
89
|
-
if all_gpus:
|
90
|
-
if enable_logging:
|
91
|
-
logger.info("Using all available GPUs")
|
92
|
-
gpus = [int(gpu) for gpu in list_available_gpus()]
|
93
|
-
return execute_on_multiple_gpus(
|
94
|
-
gpus, func, *args, **kwargs
|
95
|
-
)
|
96
|
-
|
97
|
-
if enable_logging:
|
98
|
-
logger.info(f"Using GPU device ID: {device_id}")
|
99
|
-
return execute_on_gpu(device_id, func, *args, **kwargs)
|
100
|
-
|
101
|
-
else:
|
102
|
-
raise ValueError(
|
103
|
-
f"Invalid device specified: {device}. Supported devices are 'cpu' and 'gpu'."
|
104
|
-
)
|
105
|
-
|
106
|
-
except ValueError as e:
|
107
|
-
if enable_logging:
|
108
|
-
logger.error(
|
109
|
-
f"Invalid device or configuration specified: {e}"
|
110
|
-
)
|
111
|
-
raise
|
112
|
-
except Exception as e:
|
113
|
-
if enable_logging:
|
114
|
-
logger.error(f"An error occurred during execution: {e}")
|
115
|
-
raise
|
116
|
-
|
117
|
-
|
118
|
-
# def test_clusterops(x):
|
119
|
-
# return x + 1
|
120
|
-
|
121
|
-
# example = exec_callable_with_clusterops(
|
122
|
-
# device="cpu",
|
123
|
-
# all_cores=True,
|
124
|
-
# func = test_clusterops,
|
125
|
-
# )
|
126
|
-
|
127
|
-
# print(example)
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|