From 9cbbdc9a10b762f8fe4b782cf11d7b1b7e95067a Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 19 Apr 2024 23:09:12 -0400 Subject: [PATCH] [CLEANUP] --- .../auto_docs.py => auto_docs.py | 2 - docs/install.md | 4 +- docs/swarms/tokenizers/majorityvoting.md | 1 + docs/swarms/tokenizers/stackoverflowswarm.md | 1 + docs/swarms/tokenizers/taskqueuebase.md | 1 + playground/agents/llama3_agent.py | 27 ++++++ playground/swarms/build_a_swarm.py | 90 +++++++++++++++++++ playground/swarms/swarm_example.py | 49 ++++++++++ swarms/structs/base_swarm.py | 17 ++++ swarms/utils/__init__.py | 34 ++++--- swarms/utils/csv_and_pandas.py | 34 ------- swarms/utils/load_model_torch.py | 57 ------------ swarms/utils/pandas_to_str.py | 49 ---------- 13 files changed, 204 insertions(+), 162 deletions(-) rename scripts/auto_tests_docs/auto_docs.py => auto_docs.py (97%) create mode 100644 docs/swarms/tokenizers/majorityvoting.md create mode 100644 docs/swarms/tokenizers/stackoverflowswarm.md create mode 100644 docs/swarms/tokenizers/taskqueuebase.md create mode 100644 playground/agents/llama3_agent.py create mode 100644 playground/swarms/build_a_swarm.py create mode 100644 playground/swarms/swarm_example.py delete mode 100644 swarms/utils/csv_and_pandas.py delete mode 100644 swarms/utils/load_model_torch.py delete mode 100644 swarms/utils/pandas_to_str.py diff --git a/scripts/auto_tests_docs/auto_docs.py b/auto_docs.py similarity index 97% rename from scripts/auto_tests_docs/auto_docs.py rename to auto_docs.py index 570793c8..48441c38 100644 --- a/scripts/auto_tests_docs/auto_docs.py +++ b/auto_docs.py @@ -10,7 +10,6 @@ from swarms import OpenAIChat from swarms.structs.majority_voting import MajorityVoting from swarms.structs.stackoverflow_swarm import StackOverflowSwarm from swarms.structs.task_queue_base import TaskQueueBase -from swarms.structs.tool_json_schema import JSON ########## @@ -60,7 +59,6 @@ def process_documentation(cls): def main(): classes = [ - JSON, MajorityVoting, StackOverflowSwarm, TaskQueueBase, diff --git a/docs/install.md b/docs/install.md index 7adbe441..716f1f31 100644 --- a/docs/install.md +++ b/docs/install.md @@ -31,7 +31,6 @@ You can install `swarms` with pip in a ``` - !!! example "git clone (for development)" === "virtualenv" @@ -71,6 +70,9 @@ You can install `swarms` with pip in a poetry install --extras "desktop" ``` + +# Javascript + !!! example "NPM install |WIP|" === "headless" diff --git a/docs/swarms/tokenizers/majorityvoting.md b/docs/swarms/tokenizers/majorityvoting.md new file mode 100644 index 00000000..aeb7a17e --- /dev/null +++ b/docs/swarms/tokenizers/majorityvoting.md @@ -0,0 +1 @@ +content='# Swarms Structs Documentation\n\n## Module: MajorityVoting\n\n### Overview\n\nThe `MajorityVoting` module in the Swarms Structs library represents a majority voting system for agents. This module allows multiple agents to provide responses to a given task, and the system aggregates these responses to determine the majority vote. The purpose of this module is to leverage the collective intelligence of multiple agents to arrive at consensual decisions or answers.\n\n### Class Definition\n\n```python\nclass MajorityVoting:\n """\n Class representing a majority voting system for agents.\n\n Args:\n agents (list): A list of agents to be used in the majority voting system.\n output_parser (function, optional): A function used to parse the output of the agents.\n If not provided, the default majority voting function is used.\n autosave (bool, optional): A boolean indicating whether to autosave the conversation to a file.\n verbose (bool, optional): A boolean indicating whether to enable verbose logging.\n Examples:\n >>> from swarms.structs.agent import Agent\n >>> from swarms.structs.majority_voting import MajorityVoting\n >>> agents = [\n ... Agent("GPT-3"),\n ... Agent("Codex"),\n ... Agent("Tabnine"),\n ... ]\n >>> majority_voting = MajorityVoting(agents)\n >>> majority_voting.run("What is the capital of France?")\n \'Paris\'\n """\n\n def __init__(\n self,\n agents: List[Agent],\n output_parser: Optional[Callable] = majority_voting,\n autosave: bool = False,\n verbose: bool = False,\n *args,\n **kwargs,\n ):\n # Constructor code here\n```\n\n### Functionality and Usage\n\nThe `MajorityVoting` class is used to create a majority voting system for a given set of agents. The key features and usage of this class are as follows:\n\n- **Initialization**: The class is initialized with a list of agents, an optional output parser function, and flags for autosave and verbose logging.\n- **Run Method**: The `run` method is used to execute the majority voting system. It takes a task as input, routes the task to each agent concurrently, aggregates agent responses, and performs a majority vote.\n\n#### Examples\n\n1. **Basic Usage**:\n\n```python\nfrom swarms.structs.agent import Agent\nfrom swarms.structs.majority_voting import MajorityVoting\n\nagents = [\n Agent("GPT-3"),\n Agent("Codex"),\n Agent("Tabnine"),\n]\nmajority_voting = MajorityVoting(agents)\nresult = majority_voting.run("What is the capital of France?")\nprint(result) # Output: \'Paris\'\n```\n\n2. **Custom Output Parser**:\n\n```python\ndef custom_parser(responses):\n # Custom parsing logic here\n return most_common_response\n\nmajority_voting = MajorityVoting(agents, output_parser=custom_parser)\nresult = majority_voting.run("What is the capital of France?")\n```\n\n3. **Autosave Conversation**:\n\n```python\nmajority_voting = MajorityVoting(agents, autosave=True)\nresult = majority_voting.run("What is the capital of France?")\n```\n\n### Additional Information\n\n- **Threaded Execution**: The `MajorityVoting` class uses a ThreadPoolExecutor for concurrent execution of agents\' tasks.\n- **Logging**: Verbose logging can be enabled to track agent responses and system operations.\n- **Output Parsing**: Custom output parsers can be provided to handle different response aggregation strategies.\n\n### References\n\nFor more information on multi-agent systems and consensus algorithms, refer to the following resources:\n\n1. [Multi-Agent Systems: A Modern Approach to Distributed Artificial Intelligence](https://www.cambridge.org/highereducation/books/multi-agent-systems/7A7C7D3E4E0B6A53C4C4C24E7E5F1B58)\n2. [Consensus Algorithms in Multi-Agent Systems](https://link.springer.com/chapter/10.1007/978-3-319-02813-0_22)\n\n---\n\nThis detailed documentation provides a comprehensive guide for understanding and utilizing the `MajorityVoting` module in the Swarms Structs library. It covers class definition, functionality, usage examples, additional information, and external references to enhance the user\'s knowledge and application of the module.' response_metadata={'token_usage': {'completion_tokens': 918, 'prompt_tokens': 2388, 'total_tokens': 3306}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_c2295e73ad', 'finish_reason': 'stop', 'logprobs': None} diff --git a/docs/swarms/tokenizers/stackoverflowswarm.md b/docs/swarms/tokenizers/stackoverflowswarm.md new file mode 100644 index 00000000..8b1bf520 --- /dev/null +++ b/docs/swarms/tokenizers/stackoverflowswarm.md @@ -0,0 +1 @@ +content='# Module/Class Name: StackOverflowSwarm\n\n## Overview:\nThe `StackOverflowSwarm` class represents a swarm of agents that collaborate to solve problems or answer questions on Stack Overflow. This class encapsulates the behavior of a group of agents working together to provide solutions in a simulated Stack Overflow environment.\n\n## Purpose:\nThe purpose of the `StackOverflowSwarm` class is to simulate a collaborative environment where agents can interact, share information, and collectively work towards solving problems or answering questions similar to how users on Stack Overflow collaborate to address programming-related queries.\n\n## Class Definition:\n```python\nclass StackOverflowSwarm(BaseSwarm):\n """\n Represents a swarm of agents that work together to solve a problem or answer a question on Stack Overflow.\n\n Attributes:\n agents (List[Agent]): The list of agents in the swarm.\n autosave (bool): Flag indicating whether to automatically save the conversation.\n verbose (bool): Flag indicating whether to display verbose output.\n save_filepath (str): The filepath to save the conversation.\n conversation (Conversation): The conversation object for storing the interactions.\n """\n \n def __init__(\n self,\n agents: List[Agent],\n autosave: bool = False,\n verbose: bool = False,\n save_filepath: str = "stack_overflow_swarm.json",\n eval_agent: Agent = None,\n *args,\n **kwargs,\n ):\n """\n Initialize the StackOverflowSwarm object with the provided parameters.\n\n Parameters:\n - agents (List[Agent]): The list of agents in the swarm.\n - autosave (bool): Flag indicating whether to automatically save the conversation. Default: False.\n - verbose (bool): Flag indicating whether to display verbose output. Default: False.\n - save_filepath (str): The filepath to save the conversation. Default: "stack_overflow_swarm.json".\n - eval_agent (Agent): The evaluation agent for the swarm. Default: None.\n - *args: Variable length argument list.\n - **kwargs: Arbitrary keyword arguments.\n """\n \n def run(self, task: str, *args, **kwargs):\n """\n Run the StackOverflowSwarm to perform a given task.\n\n Args:\n - task (str): The task to be performed by the agents.\n - *args: Variable length argument list.\n - **kwargs: Arbitrary keyword arguments.\n\n Returns:\n - List[str]: The conversation history.\n """\n```\n\n## Functionality and Usage:\nThe `StackOverflowSwarm` class initializes a swarm of agents to collaborate on solving problems or answering questions. It maintains a conversation history and allows agents to interact and provide responses to tasks.\n\n### Usage Examples:\n#### Example 1: Initializing and Running a StackOverflowSwarm\n```python\nfrom swarms.structs.agent import Agent\nfrom swarms.structs.stack_overflow_swarm import StackOverflowSwarm\n\n# Create a list of agents\nagents = [Agent(name="Agent1"), Agent(name="Agent2")]\n\n# Initialize the StackOverflowSwarm\nswarm = StackOverflowSwarm(agents=agents, autosave=True, verbose=True)\n\n# Run the swarm for a task\ntask = "How to implement a binary search algorithm?"\nconversation_history = swarm.run(task)\n\nprint(conversation_history)\n```\n\n#### Example 2: Adding and Configuring Evaluation Agent\n```python\nfrom swarms.structs.agent import Agent\nfrom swarms.structs.stack_overflow_swarm import StackOverflowSwarm\n\n# Create a list of agents\nagents = [Agent(name="Agent1"), Agent(name="Agent2")]\n\n# Initialize the evaluation agent\neval_agent = Agent(name="EvalAgent")\n\n# Initialize the StackOverflowSwarm with evaluation agent\nswarm = StackOverflowSwarm(agents=agents, eval_agent=eval_agent)\n\n# Run the swarm for a task\ntask = "How to handle null pointer exceptions in Python?"\nconversation_history = swarm.run(task)\n\nprint(conversation_history)\n```\n\n#### Example 3: Verbose Output\n```python\nfrom swarms.structs.agent import Agent\nfrom swarms.structs.stack_overflow_swarm import StackOverflowSwarm\n\n# Create a list of agents\nagents = [Agent(name="Agent1"), Agent(name="Agent2")]\n\n# Initialize the StackOverflowSwarm with verbose output\nswarm = StackOverflowSwarm(agents=agents, verbose=True)\n\n# Run the swarm for a task\ntask = "How to optimize code performance in Java?"\nconversation_history = swarm.run(task)\n\nprint(conversation_history)\n```\n\n## Additional Information:\n- The `StackOverflowSwarm` class provides a collaborative environment for agents to work together.\n- Agents can interact, share information, and collectively solve problems.\n- The conversation history is stored and can be accessed after running the swarm.\n- Configurable parameters such as autosave, verbose output, and evaluation agent enhance the functionality of the swarm.\n\n## References:\n- [Link to GitHub Repository](https://github.com/example-repo)\n- [Documentation Page](https://example-docs.com)\n\nThis detailed documentation provides insights into the `StackOverflowSwarm` class, its purpose, functionality, attributes, and usage examples. By following the provided structure, users can effectively utilize the class in their projects.' response_metadata={'token_usage': {'completion_tokens': 1078, 'prompt_tokens': 2122, 'total_tokens': 3200}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_c2295e73ad', 'finish_reason': 'stop', 'logprobs': None} diff --git a/docs/swarms/tokenizers/taskqueuebase.md b/docs/swarms/tokenizers/taskqueuebase.md new file mode 100644 index 00000000..a075122e --- /dev/null +++ b/docs/swarms/tokenizers/taskqueuebase.md @@ -0,0 +1 @@ +content='# Module/Class Name: TaskQueueBase\n\n## Overview\nThe `TaskQueueBase` class is a helper class that provides a standard way to create an Abstract Base Class (ABC) using inheritance. It includes methods for adding tasks to a queue, getting the next task from the queue, marking tasks as completed, and resetting tasks if they were not completed successfully.\n\n## Class Definition\n```python\nclass TaskQueueBase(ABC):\n def __init__(self):\n self.lock = threading.Lock()\n\n @synchronized_queue\n @abstractmethod\n def add(self, task: Task) -> bool:\n """Adds a task to the queue.\n\n Args:\n task (Task): The task to be added to the queue.\n\n Returns:\n bool: True if the task was successfully added, False otherwise.\n """\n\n @synchronized_queue\n @abstractmethod\n def get(self, agent: Agent) -> Task:\n """Gets the next task from the queue.\n\n Args:\n agent (Agent): The agent requesting the task.\n\n Returns:\n Task: The next task from the queue.\n """\n\n @synchronized_queue\n @abstractmethod\n def complete_task(self, task_id: str):\n """Sets the task as completed.\n\n Args:\n task_id (str): The ID of the task to be marked as completed.\n """\n\n @synchronized_queue\n @abstractmethod\n def reset(self, task_id: str):\n """Resets the task if the agent failed to complete it.\n\n Args:\n task_id (str): The ID of the task to be reset.\n """\n```\n\n## Functionality and Usage\nThe `TaskQueueBase` class is designed to be used as a base class for creating task queue implementations. It provides a set of abstract methods that must be implemented by subclasses to define the specific behavior of the task queue.\n\n### Adding a Task\nTo add a task to the queue, you need to implement the `add` method in a subclass of `TaskQueueBase`. This method takes a `Task` object as input and returns a boolean value indicating whether the task was successfully added to the queue.\n\n```python\nclass MyTaskQueue(TaskQueueBase):\n def add(self, task: Task) -> bool:\n # Custom implementation to add task to the queue\n pass\n```\n\n### Getting the Next Task\nThe `get` method is used to retrieve the next task from the queue. This method should be implemented in a subclass of `TaskQueueBase` to define how tasks are retrieved from the queue based on the requesting agent.\n\n```python\nclass MyTaskQueue(TaskQueueBase):\n def get(self, agent: Agent) -> Task:\n # Custom implementation to get the next task from the queue\n pass\n```\n\n### Completing and Resetting Tasks\nThe `complete_task` method is used to mark a task as completed, while the `reset` method is used to reset a task if the agent failed to complete it. Subclasses of `TaskQueueBase` should provide implementations for these methods based on the specific requirements of the task queue.\n\n```python\nclass MyTaskQueue(TaskQueueBase):\n def complete_task(self, task_id: str):\n # Custom implementation to mark task as completed\n pass\n\n def reset(self, task_id: str):\n # Custom implementation to reset task\n pass\n```\n\n## Additional Information\n- It is important to ensure thread safety when implementing the methods of `TaskQueueBase` due to the presence of the `threading.Lock` object in the class.\n- Subclasses of `TaskQueueBase` should provide detailed implementations for each abstract method to define the behavior of the task queue.\n- Consider using the `synchronized_queue` decorator to ensure that the queue operations are thread-safe.\n\n## References\n- Python threading documentation: [Python Threading](https://docs.python.org/3/library/threading.html)\n- Abstract Base Classes in Python: [ABCs in Python](https://docs.python.org/3/library/abc.html)' response_metadata={'token_usage': {'completion_tokens': 835, 'prompt_tokens': 1734, 'total_tokens': 2569}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_c2295e73ad', 'finish_reason': 'stop', 'logprobs': None} diff --git a/playground/agents/llama3_agent.py b/playground/agents/llama3_agent.py new file mode 100644 index 00000000..4a204188 --- /dev/null +++ b/playground/agents/llama3_agent.py @@ -0,0 +1,27 @@ +import os + +from dotenv import load_dotenv + +# Import the OpenAIChat model and the Agent struct +from swarms import Agent, HuggingfaceLLM + +# Load the environment variables +load_dotenv() + +# Get the API key from the environment +api_key = os.environ.get("OPENAI_API_KEY") + +# Initialize the language model +llm = HuggingfaceLLM(model_id="meta-llama/Meta-Llama-3-8B").cuda() + +## Initialize the workflow +agent = Agent( + llm=llm, + max_loops="auto", + autosave=True, + dashboard=True, + interactive=True, +) + +# Run the workflow on a task +agent.run("Generate a 10,000 word blog on health and wellness.") diff --git a/playground/swarms/build_a_swarm.py b/playground/swarms/build_a_swarm.py new file mode 100644 index 00000000..b28ba146 --- /dev/null +++ b/playground/swarms/build_a_swarm.py @@ -0,0 +1,90 @@ +from swarms import BaseSwarm, AutoSwarm, AutoSwarmRouter, Agent, Anthropic + + +class MarketingSwarm(BaseSwarm): + """ + A class representing a marketing swarm. + + Attributes: + name (str): The name of the marketing swarm. + market_trend_analyzer (Agent): An agent for analyzing market trends. + content_idea_generator (Agent): An agent for generating content ideas. + campaign_optimizer (Agent): An agent for optimizing marketing campaigns. + + Methods: + run(task: str, *args, **kwargs) -> Any: Runs the marketing swarm for the given task. + + """ + + def __init__(self, name="kyegomez/marketingswarm", *args, **kwargs): + super().__init__(*args, **kwargs) + self.name = name + + # Agent for market trend analyzer + self.market_trend_analyzer = Agent( + agent_name="Market Trend Analyzer", + system_prompt="Analyze market trends to identify opportunities for marketing campaigns.", + llm=Anthropic(), + meax_loops=1, + autosave=True, + dashboard=False, + streaming_on=True, + verbose=True, + stopping_token="", + ) + + # Agent for content idea generator + self.content_idea_generator = Agent( + agent_name="Content Idea Generator", + system_prompt="Generate content ideas based on market trends.", + llm=Anthropic(), + max_loops=1, + autosave=True, + dashboard=False, + streaming_on=True, + verbose=True, + stopping_token="", + ) + + # Agent for campaign optimizer + self.campaign_optimizer = Agent( + agent_name="Campaign Optimizer", + system_prompt="Optimize marketing campaigns based on content ideas and market trends.", + llm=Anthropic(), + max_loops=1, + autosave=True, + dashboard=False, + streaming_on=True, + verbose=True, + stopping_token="", + ) + + def run(self, task: str, *args, **kwargs): + """ + Runs the marketing swarm for the given task. + + Args: + task (str): The task to be performed by the marketing swarm. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + + Returns: + Any: The result of running the marketing swarm. + + """ + # Analyze market trends + analyzed_trends = self.market_trend_analyzer.run( + task, *args, **kwargs + ) + + # Generate content ideas based on market trends + content_ideas = self.content_idea_generator.run( + task, analyzed_trends, *args, **kwargs + ) + + # Optimize marketing campaigns based on content ideas and market trends + optimized_campaigns = self.campaign_optimizer.run( + task, content_ideas, analyzed_trends, *args, **kwargs + ) + + return optimized_campaigns diff --git a/playground/swarms/swarm_example.py b/playground/swarms/swarm_example.py new file mode 100644 index 00000000..d170c5b2 --- /dev/null +++ b/playground/swarms/swarm_example.py @@ -0,0 +1,49 @@ +from swarms import AutoSwarm, AutoSwarmRouter, BaseSwarm, Agent, Anthropic + + +class MySwarm(BaseSwarm): + def __init__(self, name="kyegomez/myswarm", *args, **kwargs): + super(self, MySwarm).__init__(*args, **kwargs) + self.name = name + + # Define and add your agents here + self.agent1 = Agent( + agent_name="Agent 1", + system_prompt="A specialized agent for task 1.", + llm=Anthropic(), + max_loops=1, + autosave=True, + dashboard=False, + streaming_on=True, + verbose=True, + stopping_token="", + ) + self.agent2 = Agent( + agent_name="Agent 2", + system_prompt="A specialized agent for task 2.", + llm=Anthropic(), + max_loops=1, + autosave=True, + dashboard=False, + streaming_on=True, + verbose=True, + stopping_token="", + ) + self.agent3 = Agent( + agent_name="Agent 3", + system_prompt="A specialized agent for task 3.", + llm=Anthropic(), + max_loops=1, + autosave=True, + dashboard=False, + streaming_on=True, + verbose=True, + stopping_token="", + ) + + def run(self, task: str, *args, **kwargs): + # Add your multi-agent logic here + output1 = self.agent1.run(task, *args, **kwargs) + output2 = self.agent2.run(task, output1, *args, **kwargs) + output3 = self.agent3.run(task, output2, *args, **kwargs) + return output3 diff --git a/swarms/structs/base_swarm.py b/swarms/structs/base_swarm.py index 95f2b57f..4a39488e 100644 --- a/swarms/structs/base_swarm.py +++ b/swarms/structs/base_swarm.py @@ -189,10 +189,27 @@ class BaseSwarm(ABC): # @abstractmethod def add_agent(self, agent: agent): """Add a agent to the swarm""" + self.agents.append(agent) + + def add_agents(self, agents: List[agent]): + """Add a list of agents to the swarm""" + self.agents.extend(agents) + + def add_agent_by_id(self, agent_id: str): + """Add a agent to the swarm by id""" + agent = self.get_agent_by_id(agent_id) + self.add_agent(agent) # @abstractmethod def remove_agent(self, agent: agent): """Remove a agent from the swarm""" + self.agents.remove(agent) + + def get_agent_by_name(self, name: str): + """Get a agent by name""" + for agent in self.agents: + if agent.name == name: + return agent # @abstractmethod def broadcast(self, message: str, sender: Optional[agent] = None): diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index 50d08bab..cc6f954d 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -1,9 +1,5 @@ from swarms.utils.class_args_wrapper import print_class_parameters from swarms.tools.code_interpreter import SubprocessCodeInterpreter -# from swarms.utils.csv_and_pandas import ( -# csv_to_dataframe, -# dataframe_to_strings, -# ) from swarms.utils.data_to_text import ( csv_to_text, data_to_text, @@ -24,7 +20,6 @@ from swarms.utils.json_output_parser import JsonOutputParser from swarms.utils.llm_metrics_decorator import metrics_decorator from swarms.utils.markdown_message import display_markdown_message from swarms.tools.math_eval import math_eval -# from swarms.utils.pandas_to_str import dataframe_to_text from swarms.utils.parse_code import extract_code_from_markdown from swarms.utils.pdf_to_text import pdf_to_text from swarms.utils.remove_json_whitespace import ( @@ -36,31 +31,32 @@ from swarms.utils.try_except_wrapper import try_except_wrapper from swarms.utils.yaml_output_parser import YamlOutputParser from swarms.utils.concurrent_utils import execute_concurrently + __all__ = [ - "download_img_from_url", - "ExponentialBackoffMixin", - "find_image_path", - "JsonOutputParser", - "metrics_decorator", - "display_markdown_message", - "math_eval", - "parse_log_file", + "print_class_parameters", "SubprocessCodeInterpreter", - "try_except_wrapper", - "YamlOutputParser", "csv_to_text", "data_to_text", "json_to_text", "txt_to_text", + "download_img_from_url", + "ExponentialBackoffMixin", "load_json", "sanitize_file_path", "zip_workspace", "create_file_in_folder", "zip_folders", - "remove_whitespace_from_json", - "remove_whitespace_from_yaml", + "find_image_path", + "JsonOutputParser", + "metrics_decorator", + "display_markdown_message", + "math_eval", "extract_code_from_markdown", "pdf_to_text", + "remove_whitespace_from_json", + "remove_whitespace_from_yaml", + "parse_log_file", + "try_except_wrapper", + "YamlOutputParser", "execute_concurrently", - "print_class_parameters", -] \ No newline at end of file +] diff --git a/swarms/utils/csv_and_pandas.py b/swarms/utils/csv_and_pandas.py deleted file mode 100644 index 2a868d13..00000000 --- a/swarms/utils/csv_and_pandas.py +++ /dev/null @@ -1,34 +0,0 @@ -import pandas as pd - - -# CSV to dataframe -def csv_to_dataframe(file_path): - """ - Read a CSV file and return a pandas DataFrame. - - Parameters: - file_path (str): The path to the CSV file. - - Returns: - pandas.DataFrame: The DataFrame containing the data from the CSV file. - """ - df = pd.read_csv(file_path) - return df - - -# Dataframe to strings -def dataframe_to_strings(df): - """ - Converts a pandas DataFrame to a list of string representations of each row. - - Args: - df (pandas.DataFrame): The DataFrame to convert. - - Returns: - list: A list of string representations of each row in the DataFrame. - """ - row_strings = [] - for index, row in df.iterrows(): - row_string = row.to_string() - row_strings.append(row_string) - return row_strings diff --git a/swarms/utils/load_model_torch.py b/swarms/utils/load_model_torch.py deleted file mode 100644 index 53649e93..00000000 --- a/swarms/utils/load_model_torch.py +++ /dev/null @@ -1,57 +0,0 @@ -import torch -from torch import nn - - -def load_model_torch( - model_path: str = None, - device: torch.device = None, - model: nn.Module = None, - strict: bool = True, - map_location=None, - *args, - **kwargs, -) -> nn.Module: - """ - Load a PyTorch model from a given path and move it to the specified device. - - Args: - model_path (str): Path to the saved model file. - device (torch.device): Device to move the model to. - model (nn.Module): The model architecture, if the model file only contains the state dictionary. - strict (bool): Whether to strictly enforce that the keys in the state dictionary match the keys returned by the model's `state_dict()` function. - map_location (callable): A function to remap the storage locations of the loaded model. - *args: Additional arguments to pass to `torch.load`. - **kwargs: Additional keyword arguments to pass to `torch.load`. - - Returns: - nn.Module: The loaded model. - - Raises: - FileNotFoundError: If the model file is not found. - RuntimeError: If there is an error while loading the model. - """ - if device is None: - device = torch.device( - "cuda" if torch.cuda.is_available() else "cpu" - ) - - try: - if model is None: - model = torch.load( - model_path, map_location=map_location, *args, **kwargs - ) - else: - model.load_state_dict( - torch.load( - model_path, - map_location=map_location, - *args, - **kwargs, - ), - strict=strict, - ) - return model.to(device) - except FileNotFoundError: - raise FileNotFoundError(f"Model file not found: {model_path}") - except RuntimeError as e: - raise RuntimeError(f"Error loading model: {str(e)}") diff --git a/swarms/utils/pandas_to_str.py b/swarms/utils/pandas_to_str.py deleted file mode 100644 index 464a7b31..00000000 --- a/swarms/utils/pandas_to_str.py +++ /dev/null @@ -1,49 +0,0 @@ -import pandas as pd - - -def dataframe_to_text( - df: pd.DataFrame, - parsing_func: callable = None, -) -> str: - """ - Convert a pandas DataFrame to a string representation. - - Args: - df (pd.DataFrame): The pandas DataFrame to convert. - parsing_func (callable, optional): A function to parse the resulting text. Defaults to None. - - Returns: - str: The string representation of the DataFrame. - - Example: - >>> df = pd.DataFrame({ - ... 'A': [1, 2, 3], - ... 'B': [4, 5, 6], - ... 'C': [7, 8, 9], - ... }) - >>> print(dataframe_to_text(df)) - - """ - # Get a string representation of the dataframe - df_str = df.to_string() - - # Get a string representation of the column names - info_str = df.info() - - # Combine the dataframe string and the info string - text = f"DataFrame:\n{df_str}\n\nInfo:\n{info_str}" - - if parsing_func: - text = parsing_func(text) - - return text - - -# # # Example usage: -# df = pd.DataFrame({ -# 'A': [1, 2, 3], -# 'B': [4, 5, 6], -# 'C': [7, 8, 9], -# }) - -# print(dataframe_to_text(df))