swarms 7.6.4__py3-none-any.whl → 7.6.5__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.
@@ -0,0 +1,146 @@
1
+ from typing import List, Optional, Dict, Any
2
+ from loguru import logger
3
+
4
+ try:
5
+ from vllm import LLM, SamplingParams
6
+ except ImportError:
7
+ import subprocess
8
+ import sys
9
+
10
+ print("Installing vllm")
11
+ subprocess.check_call(
12
+ [sys.executable, "-m", "pip", "install", "-U", "vllm"]
13
+ )
14
+ print("vllm installed")
15
+ from vllm import LLM, SamplingParams
16
+
17
+
18
+ class VLLMWrapper:
19
+ """
20
+ A wrapper class for vLLM that provides a similar interface to LiteLLM.
21
+ This class handles model initialization and inference using vLLM.
22
+ """
23
+
24
+ def __init__(
25
+ self,
26
+ model_name: str = "meta-llama/Llama-2-7b-chat-hf",
27
+ system_prompt: Optional[str] = None,
28
+ stream: bool = False,
29
+ temperature: float = 0.5,
30
+ max_tokens: int = 4000,
31
+ max_completion_tokens: int = 4000,
32
+ tools_list_dictionary: Optional[List[Dict[str, Any]]] = None,
33
+ tool_choice: str = "auto",
34
+ parallel_tool_calls: bool = False,
35
+ *args,
36
+ **kwargs,
37
+ ):
38
+ """
39
+ Initialize the vLLM wrapper with the given parameters.
40
+
41
+ Args:
42
+ model_name (str): The name of the model to use. Defaults to "meta-llama/Llama-2-7b-chat-hf".
43
+ system_prompt (str, optional): The system prompt to use. Defaults to None.
44
+ stream (bool): Whether to stream the output. Defaults to False.
45
+ temperature (float): The temperature for sampling. Defaults to 0.5.
46
+ max_tokens (int): The maximum number of tokens to generate. Defaults to 4000.
47
+ max_completion_tokens (int): The maximum number of completion tokens. Defaults to 4000.
48
+ tools_list_dictionary (List[Dict[str, Any]], optional): List of available tools. Defaults to None.
49
+ tool_choice (str): How to choose tools. Defaults to "auto".
50
+ parallel_tool_calls (bool): Whether to allow parallel tool calls. Defaults to False.
51
+ """
52
+ self.model_name = model_name
53
+ self.system_prompt = system_prompt
54
+ self.stream = stream
55
+ self.temperature = temperature
56
+ self.max_tokens = max_tokens
57
+ self.max_completion_tokens = max_completion_tokens
58
+ self.tools_list_dictionary = tools_list_dictionary
59
+ self.tool_choice = tool_choice
60
+ self.parallel_tool_calls = parallel_tool_calls
61
+
62
+ # Initialize vLLM
63
+ self.llm = LLM(model=model_name, **kwargs)
64
+ self.sampling_params = SamplingParams(
65
+ temperature=temperature,
66
+ max_tokens=max_tokens,
67
+ )
68
+
69
+ def _prepare_prompt(self, task: str) -> str:
70
+ """
71
+ Prepare the prompt for the given task.
72
+
73
+ Args:
74
+ task (str): The task to prepare the prompt for.
75
+
76
+ Returns:
77
+ str: The prepared prompt.
78
+ """
79
+ if self.system_prompt:
80
+ return f"{self.system_prompt}\n\nUser: {task}\nAssistant:"
81
+ return f"User: {task}\nAssistant:"
82
+
83
+ def run(self, task: str, *args, **kwargs) -> str:
84
+ """
85
+ Run the model for the given task.
86
+
87
+ Args:
88
+ task (str): The task to run the model for.
89
+ *args: Additional positional arguments.
90
+ **kwargs: Additional keyword arguments.
91
+
92
+ Returns:
93
+ str: The model's response.
94
+ """
95
+ try:
96
+ prompt = self._prepare_prompt(task)
97
+
98
+ outputs = self.llm.generate(prompt, self.sampling_params)
99
+ response = outputs[0].outputs[0].text.strip()
100
+
101
+ return response
102
+
103
+ except Exception as error:
104
+ logger.error(f"Error in VLLMWrapper: {error}")
105
+ raise error
106
+
107
+ def __call__(self, task: str, *args, **kwargs) -> str:
108
+ """
109
+ Call the model for the given task.
110
+
111
+ Args:
112
+ task (str): The task to run the model for.
113
+ *args: Additional positional arguments.
114
+ **kwargs: Additional keyword arguments.
115
+
116
+ Returns:
117
+ str: The model's response.
118
+ """
119
+ return self.run(task, *args, **kwargs)
120
+
121
+ def batched_run(
122
+ self, tasks: List[str], batch_size: int = 10
123
+ ) -> List[str]:
124
+ """
125
+ Run the model for multiple tasks in batches.
126
+
127
+ Args:
128
+ tasks (List[str]): List of tasks to run.
129
+ batch_size (int): Size of each batch. Defaults to 10.
130
+
131
+ Returns:
132
+ List[str]: List of model responses.
133
+ """
134
+ logger.info(
135
+ f"Running tasks in batches of size {batch_size}. Total tasks: {len(tasks)}"
136
+ )
137
+ results = []
138
+
139
+ for i in range(0, len(tasks), batch_size):
140
+ batch = tasks[i : i + batch_size]
141
+ for task in batch:
142
+ logger.info(f"Running task: {task}")
143
+ results.append(self.run(task))
144
+
145
+ logger.info("Completed all tasks.")
146
+ return results
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: swarms
3
- Version: 7.6.4
3
+ Version: 7.6.5
4
4
  Summary: Swarms - TGSC
5
5
  Home-page: https://github.com/kyegomez/swarms
6
6
  License: MIT
@@ -81,15 +81,14 @@ swarms/schemas/__init__.py,sha256=EqqtVcpoptF1kfy19Wykp22ut4AA0z-yMQ5H9WB7ptA,18
81
81
  swarms/schemas/agent_input_schema.py,sha256=qhPyThMx2on91yG9mzNdP_08GpMh1IRDHDwFna29jPs,6345
82
82
  swarms/schemas/agent_step_schemas.py,sha256=a14gb58vR0xOwB_fwSJQbN6yb9HddEaT30E6hUrzEQA,2573
83
83
  swarms/schemas/base_schemas.py,sha256=UvBLVWg2qRen4tK5GJz50v42SiX95EQ5qK7hfyAHTEU,3267
84
- swarms/structs/__init__.py,sha256=LgAeWDQxbBpUyIYgvikgSdDoDPrvNryDKYTBfflmLMk,4352
85
- swarms/structs/agent.py,sha256=JC48tv2Jn4hNGTBvBb5Lk-LiTwnyfBO81Doyv6EWbOA,92409
84
+ swarms/structs/__init__.py,sha256=ER0HI-9RQI22i10x6XQj6TaKoWJgk1a5XIP1KxiBsCU,4310
85
+ swarms/structs/agent.py,sha256=IChi9EZiFUwCmj7dPa9H70TBkYd6VnTt-5Jx5TLPbWI,95234
86
86
  swarms/structs/agent_builder.py,sha256=tYNpfO4_8cgfMHfgA5DAOWffHnt70p6CLt59esqfVCY,12133
87
87
  swarms/structs/agent_registry.py,sha256=il507cO1NF-d4ChyANVLuWrN8bXsEAi8_7bLJ_sTU6A,12112
88
88
  swarms/structs/agent_roles.py,sha256=8XEw6RjOOZelaZaWt4gXaYQm5WMLEhSO7W6Z8sQjmFg,582
89
89
  swarms/structs/agent_router.py,sha256=YZw5AaK2yTvxkOA7ouED_4MoYgn0XZggvo1wrglp-4E,13017
90
90
  swarms/structs/agents_available.py,sha256=SedxDim-0IWgGsNwJZxRIUMfKyAFFXdvXSYeBNu0zGw,2804
91
91
  swarms/structs/async_workflow.py,sha256=7YWsLPyGY-1-mMxoIXWQ0FnYH6F227nxsS9PFAJoF9Q,26214
92
- swarms/structs/auto_swarm.py,sha256=AHWswlEWDL_i3V8IP362tx6pi_B2arlZhALykrkI5OA,8215
93
92
  swarms/structs/auto_swarm_builder.py,sha256=vPM5Kq59D_FvuWJB8hxgHuEvTXsxDxovlBnHGVQsM4o,10938
94
93
  swarms/structs/base_structure.py,sha256=GDu4QJQQmhU7IyuFJHIh9UVThACCva-L7uoMbVD9l4s,15901
95
94
  swarms/structs/base_swarm.py,sha256=LSGJDPJdyUCcK6698mNtjxoC1OU3s_J2NxC2k_ccGUs,23779
@@ -153,7 +152,7 @@ swarms/tools/function_util.py,sha256=DAnAPO0Ik__TAqL7IJzFmkukHnhpsW_QtALl3yj837g
153
152
  swarms/tools/json_former.py,sha256=4ugLQ_EZpghhuhFsVKsy-ehin9K64pqVE2gLU7BTO_M,14376
154
153
  swarms/tools/json_utils.py,sha256=WKMZjcJ0Vt6lgIjiTBenslcfjgRSLX4UWs4uDkKFMQI,1316
155
154
  swarms/tools/logits_processor.py,sha256=NifZZ5w9yemWGJAJ5nHFrphtZVX1XlyesgvYZTxK1GM,2965
156
- swarms/tools/mcp_integration.py,sha256=Aqt2gLTIinJdHM_ZnIICqCXp2Fz_AbDPfaJVljDuIAI,17529
155
+ swarms/tools/mcp_integration.py,sha256=rUXxC9NvXQ3V4B7Lt1AoI4ZYiCl2-T4FW3_689HTRZk,12839
157
156
  swarms/tools/openai_func_calling_schema_pydantic.py,sha256=6BAH9kuaVTvJIbjgSSJ5XvHhWvWszPxgarkfUuE5Ads,978
158
157
  swarms/tools/openai_tool_creator_decorator.py,sha256=SYZjHnARjWvnH9cBdj7Kc_Yy1muvNxMT3RQz8KkA2SE,2578
159
158
  swarms/tools/py_func_to_openai_func_str.py,sha256=W112Gu0CmAiHrNWnRMcnoGiVZEy2FxAU4xMvnG9XP4g,15710
@@ -182,9 +181,10 @@ swarms/utils/str_to_dict.py,sha256=T3Jsdjz87WIlkSo7jAW6BB80sv0Ns49WT1qXlOrdEoE,8
182
181
  swarms/utils/swarm_reliability_checks.py,sha256=MsgUULt3HYg72D0HifZNmtCyJYpLA2UDA2wQixI-NbA,2562
183
182
  swarms/utils/try_except_wrapper.py,sha256=appEGu9Afy3TmdkNNXUgQ9yU9lj2j0uNkIoW0JhVzzY,3917
184
183
  swarms/utils/visualizer.py,sha256=0ylohEk62MAS6iPRaDOV03m9qo2k5J56tWlKJk_46p4,16927
184
+ swarms/utils/vllm_wrapper.py,sha256=OIGnU9Vf81vE_hul1FK-xEhChFK8fxqZX6-fhQeW22c,4987
185
185
  swarms/utils/wrapper_clusterop.py,sha256=PMSCVM7ZT1vgj1D_MYAe835RR3SMLYxA-si2JS02yNQ,4220
186
- swarms-7.6.4.dist-info/LICENSE,sha256=jwRtEmTWjLrEsvFB6QFdYs2cEeZPRMdj-UMOFkPF8_0,11363
187
- swarms-7.6.4.dist-info/METADATA,sha256=FihAIDYyoPwxvfsXfvpCsMzdQMHvBZ7fXkp5v06DIUE,104909
188
- swarms-7.6.4.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
189
- swarms-7.6.4.dist-info/entry_points.txt,sha256=2K0rTtfO1X1WaO-waJlXIKw5Voa_EpAL_yU0HXE2Jgc,47
190
- swarms-7.6.4.dist-info/RECORD,,
186
+ swarms-7.6.5.dist-info/LICENSE,sha256=jwRtEmTWjLrEsvFB6QFdYs2cEeZPRMdj-UMOFkPF8_0,11363
187
+ swarms-7.6.5.dist-info/METADATA,sha256=2w553wEExRZlKYKxX_LCvhpbyJbhefIxw43-I1Jvwmw,104909
188
+ swarms-7.6.5.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
189
+ swarms-7.6.5.dist-info/entry_points.txt,sha256=2K0rTtfO1X1WaO-waJlXIKw5Voa_EpAL_yU0HXE2Jgc,47
190
+ swarms-7.6.5.dist-info/RECORD,,
@@ -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