diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py index 5b894886..5de3b948 100644 --- a/swarms/structs/__init__.py +++ b/swarms/structs/__init__.py @@ -1,21 +1,26 @@ from swarms.structs.agent import Agent -from swarms.structs.sequential_workflow import SequentialWorkflow from swarms.structs.autoscaler import AutoScaler +from swarms.structs.base_swarm import AbstractSwarm from swarms.structs.conversation import Conversation +from swarms.structs.groupchat import GroupChat, GroupChatManager +from swarms.structs.model_parallizer import ModelParallelizer +from swarms.structs.multi_agent_collab import MultiAgentCollaboration from swarms.structs.schemas import ( - TaskInput, Artifact, ArtifactUpload, StepInput, + TaskInput, ) +from swarms.structs.sequential_workflow import SequentialWorkflow from swarms.structs.swarm_net import SwarmNetwork - - -from swarms.structs.model_parallizer import ModelParallelizer -from swarms.structs.multi_agent_collab import MultiAgentCollaboration -from swarms.structs.base_swarm import AbstractSwarm -from swarms.structs.groupchat import GroupChat, GroupChatManager - +from swarms.structs.utils import ( + distribute_tasks, + extract_key_from_json, + extract_tokens_from_text, + find_agent_by_id, + find_token_in_text, + parse_tasks, +) __all__ = [ "Agent", @@ -32,4 +37,10 @@ __all__ = [ "AbstractSwarm", "GroupChat", "GroupChatManager", + "parse_tasks", + "find_agent_by_id", + "distribute_tasks", + "find_token_in_text", + "extract_key_from_json", + "extract_tokens_from_text", ] diff --git a/swarms/structs/all_to_all_swarm.py b/swarms/structs/all_to_all_swarm.py new file mode 100644 index 00000000..e69de29b diff --git a/swarms/structs/utils.py b/swarms/structs/utils.py index 73da08df..3afb5fea 100644 --- a/swarms/structs/utils.py +++ b/swarms/structs/utils.py @@ -1,4 +1,5 @@ -from typing import Dict, Any, List +import json +from typing import Dict, Any, List, Optional from swarms.structs.agent import Agent @@ -66,3 +67,54 @@ def distribute_tasks( f"No agent found with ID {agent_id}. Task '{task}' is" " not assigned." ) + + +def find_token_in_text(text: str, token: str = "") -> bool: + """ + Parse a block of text for a specific token. + + Args: + text (str): The text to parse. + token (str): The token to find. + + Returns: + bool: True if the token is found in the text, False otherwise. + """ + # Check if the token is in the text + if token in text: + return True + else: + return False + + +def extract_key_from_json( + json_response: str, key: str +) -> Optional[str]: + """ + Extract a specific key from a JSON response. + + Args: + json_response (str): The JSON response to parse. + key (str): The key to extract. + + Returns: + Optional[str]: The value of the key if it exists, None otherwise. + """ + response_dict = json.loads(json_response) + return response_dict.get(key) + + +def extract_tokens_from_text( + text: str, tokens: List[str] +) -> List[str]: + """ + Extract a list of tokens from a text response. + + Args: + text (str): The text to parse. + tokens (List[str]): The tokens to extract. + + Returns: + List[str]: The tokens that were found in the text. + """ + return [token for token in tokens if token in text]