swarms 7.6.4__py3-none-any.whl → 7.6.6__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.
@@ -1,229 +0,0 @@
1
- from typing import Any, Callable, Dict, Optional, Sequence
2
-
3
- from swarms.structs.base_swarm import BaseSwarm
4
- from swarms.utils.loguru_logger import logger
5
-
6
-
7
- class AutoSwarmRouter(BaseSwarm):
8
- """AutoSwarmRouter class represents a router for the AutoSwarm class.
9
-
10
- This class is responsible for routing tasks to the appropriate swarm based on the provided name.
11
- It allows customization of the preprocessing, routing, and postprocessing of tasks.
12
-
13
- Attributes:
14
- name (str): The name of the router.
15
- description (str): The description of the router.
16
- verbose (bool): Whether to enable verbose mode.
17
- custom_params (dict): Custom parameters for the router.
18
- swarms (list): A list of BaseSwarm objects.
19
- custom_preprocess (callable): Custom preprocessing function for tasks.
20
- custom_postprocess (callable): Custom postprocessing function for task results.
21
- custom_router (callable): Custom routing function for tasks.
22
-
23
- Methods:
24
- run(task: str = None, *args, **kwargs) -> Any:
25
- Run the swarm simulation and route the task to the appropriate swarm.
26
-
27
- Flow:
28
- name -> router -> swarm entry point
29
- """
30
-
31
- def __init__(
32
- self,
33
- name: Optional[str] = None,
34
- description: Optional[str] = None,
35
- verbose: bool = False,
36
- custom_params: Optional[Dict[str, Any]] = None,
37
- swarms: Sequence[BaseSwarm] = None,
38
- custom_preprocess: Optional[Callable] = None,
39
- custom_postprocess: Optional[Callable] = None,
40
- custom_router: Optional[Callable] = None,
41
- *args,
42
- **kwargs,
43
- ):
44
- super().__init__(
45
- name=name, description=description, *args, **kwargs
46
- )
47
- self.name = name
48
- self.description = description
49
- self.verbose = verbose
50
- self.custom_params = custom_params
51
- self.swarms = swarms
52
- self.custom_preprocess = custom_preprocess
53
- self.custom_postprocess = custom_postprocess
54
- self.custom_router = custom_router
55
-
56
- # Create a dictionary of swarms
57
- self.swarm_dict = {swarm.name: swarm for swarm in self.swarms}
58
-
59
- logger.info(
60
- f"AutoSwarmRouter has been initialized with {self.len_of_swarms()} swarms."
61
- )
62
-
63
- def run(self, task: str = None, *args, **kwargs):
64
- try:
65
- """Run the swarm simulation and route the task to the appropriate swarm."""
66
-
67
- if self.custom_preprocess:
68
- # If custom preprocess function is provided then run it
69
- logger.info("Running custom preprocess function.")
70
- task, args, kwargs = self.custom_preprocess(
71
- task, args, kwargs
72
- )
73
-
74
- if self.custom_router:
75
- # If custom router function is provided then use it to route the task
76
- logger.info("Running custom router function.")
77
- out = self.custom_router(self, task, *args, **kwargs)
78
-
79
- if self.custom_postprocess:
80
- # If custom postprocess function is provided then run it
81
- out = self.custom_postprocess(out)
82
-
83
- return out
84
-
85
- if self.name in self.swarm_dict:
86
- # If a match is found then send the task to the swarm
87
- out = self.swarm_dict[self.name].run(
88
- task, *args, **kwargs
89
- )
90
-
91
- if self.custom_postprocess:
92
- # If custom postprocess function is provided then run it
93
- out = self.custom_postprocess(out)
94
-
95
- return out
96
-
97
- # If no match is found then return None
98
- raise ValueError(
99
- f"Swarm with name {self.name} not found."
100
- )
101
- except Exception as e:
102
- logger.error(f"Error: {e}")
103
- raise e
104
-
105
- def len_of_swarms(self):
106
- return print(len(self.swarms))
107
-
108
- def list_available_swarms(self):
109
- for swarm in self.swarms:
110
- try:
111
- logger.info(
112
- f"Swarm Name: {swarm.name} || Swarm Description: {swarm.description} "
113
- )
114
- except Exception as error:
115
- logger.error(
116
- f"Error Detected You may not have swarms available: {error}"
117
- )
118
- raise error
119
-
120
-
121
- class AutoSwarm(BaseSwarm):
122
- """AutoSwarm class represents a swarm of agents that can be created automatically.
123
-
124
- Flow:
125
- name -> router -> swarm entry point
126
-
127
- Args:
128
- name (Optional[str]): The name of the swarm. Defaults to None.
129
- description (Optional[str]): The description of the swarm. Defaults to None.
130
- verbose (bool): Whether to enable verbose mode. Defaults to False.
131
- custom_params (Optional[Dict[str, Any]]): Custom parameters for the swarm. Defaults to None.
132
- router (Optional[AutoSwarmRouter]): The router for the swarm. Defaults to None.
133
- """
134
-
135
- def __init__(
136
- self,
137
- name: Optional[str] = None,
138
- description: Optional[str] = None,
139
- verbose: bool = False,
140
- custom_params: Optional[Dict[str, Any]] = None,
141
- custom_preprocess: Optional[Callable] = None,
142
- custom_postprocess: Optional[Callable] = None,
143
- custom_router: Optional[Callable] = None,
144
- max_loops: int = 1,
145
- *args,
146
- **kwargs,
147
- ):
148
- super().__init__()
149
- self.name = name
150
- self.description = description
151
- self.verbose = verbose
152
- self.custom_params = custom_params
153
- self.custom_preprocess = custom_preprocess
154
- self.custom_postprocess = custom_postprocess
155
- self.custom_router = custom_router
156
- self.max_loops = max_loops
157
- self.router = AutoSwarmRouter(
158
- name=name,
159
- description=description,
160
- verbose=verbose,
161
- custom_params=custom_params,
162
- custom_preprocess=custom_preprocess,
163
- custom_postprocess=custom_postprocess,
164
- custom_router=custom_router,
165
- *args,
166
- **kwargs,
167
- )
168
-
169
- if name is None:
170
- raise ValueError(
171
- "A name must be provided for the AutoSwarm, what swarm do you want to use?"
172
- )
173
-
174
- if verbose is True:
175
- self.init_logging()
176
-
177
- def init_logging(self):
178
- logger.info("AutoSwarm has been activated. Ready for usage.")
179
-
180
- # def name_swarm_check(self, name: str = None):
181
-
182
- def run(self, task: str = None, *args, **kwargs):
183
- """Run the swarm simulation."""
184
- try:
185
- loop = 0
186
-
187
- while loop < self.max_loops:
188
- if self.custom_preprocess:
189
- # If custom preprocess function is provided then run it
190
- logger.info("Running custom preprocess function.")
191
- task, args, kwargs = self.custom_preprocess(
192
- task, args, kwargs
193
- )
194
-
195
- if self.custom_router:
196
- # If custom router function is provided then use it to route the task
197
- logger.info("Running custom router function.")
198
- out = self.custom_router(
199
- self, task, *args, **kwargs
200
- )
201
-
202
- else:
203
- out = self.router.run(task, *args, **kwargs)
204
-
205
- if self.custom_postprocess:
206
- # If custom postprocess function is provided then run it
207
- out = self.custom_postprocess(out)
208
-
209
- # LOOP
210
- loop += 1
211
-
212
- return out
213
- except Exception as e:
214
- logger.error(
215
- f"Error: {e} try optimizing the inputs and try again."
216
- )
217
- raise e
218
-
219
- def list_all_swarms(self):
220
- for swarm in self.swarms:
221
- try:
222
- logger.info(
223
- f"Swarm Name: {swarm.name} || Swarm Description: {swarm.description} "
224
- )
225
- except Exception as error:
226
- logger.error(
227
- f"Error Detected You may not have swarms available: {error}"
228
- )
229
- raise error
File without changes