From f1b26e4690787b441db4dbbec9dfea135c69b373 Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 21 Nov 2023 22:36:32 -0800 Subject: [PATCH] CLEAN UP: display markdown, nonlinear workflow, playground, static utils --- playground/demos/paper_to_code.py | 19 ---- playground/demos/positive_med_sequential.py | 26 ----- swarms/structs/nonlinear_workflow.py | 105 -------------------- swarms/utils/display_markdown.py | 23 ----- swarms/utils/main.py | 36 ++----- swarms/utils/markdown_message.py | 2 +- swarms/utils/static.py | 29 ------ tests/memory/main.py | 2 +- tests/memory/oceandb.py | 2 +- 9 files changed, 12 insertions(+), 232 deletions(-) delete mode 100644 playground/demos/paper_to_code.py delete mode 100644 playground/demos/positive_med_sequential.py delete mode 100644 swarms/structs/nonlinear_workflow.py delete mode 100644 swarms/utils/display_markdown.py delete mode 100644 swarms/utils/static.py diff --git a/playground/demos/paper_to_code.py b/playground/demos/paper_to_code.py deleted file mode 100644 index 250653f4..00000000 --- a/playground/demos/paper_to_code.py +++ /dev/null @@ -1,19 +0,0 @@ -from swarms.structs import Flow, SequentialWorkflow -from swarms.models import OpenAIChat, Anthropic - -# llm -llm = OpenAIChat() -llm2 = Anthropic() - -# 2 Flows, one that creates an algorithmic pseuedocode and another that creates the pytorch code -flow1 = Flow(llm2, max_loops=1) -flow2 = Flow(llm, max_loops=1) - -# SequentialWorkflow -workflow = SequentialWorkflow( - [flow1, flow2], - max_loops=1, - name="Paper to Code", - autosave=True, - description="This workflow takes a paper and converts it to code.", -) diff --git a/playground/demos/positive_med_sequential.py b/playground/demos/positive_med_sequential.py deleted file mode 100644 index 1e943a6c..00000000 --- a/playground/demos/positive_med_sequential.py +++ /dev/null @@ -1,26 +0,0 @@ -""" -Swarm Flow -Topic selection agent -> draft agent -> review agent -> distribution agent - -Topic Selection Agent: -- Generate 10 topics on gaining mental clarity using Taosim and Christian meditation - -Draft Agent: -- Write a 100% unique, creative and in human-like style article of a minimum of 5,000 words using headings and sub-headings. - -Review Agent: -- Refine the article to meet PositiveMed’s stringent publication standards. - -Distribution Agent: -- Social Media posts for the article. - - -# TODO -- Add shorter and better topic generator prompt -- Optimize writer prompt to create longer and more enjoyeable blogs -- Use Local Models like Storywriter - - -""" -from swarms.models import OpenAIChat -from termcolor import colored diff --git a/swarms/structs/nonlinear_workflow.py b/swarms/structs/nonlinear_workflow.py deleted file mode 100644 index 2357f614..00000000 --- a/swarms/structs/nonlinear_workflow.py +++ /dev/null @@ -1,105 +0,0 @@ -from concurrent.futures import ThreadPoolExecutor, as_completed -from graphlib import TopologicalSorter -from typing import Dict, List - - -class Task: - """ - Task is a unit of work that can be executed by an agent - """ - - def __init__( - self, id: str, parents: List["Task"] = None, children: List["Task"] = None - ): - self.id = id - self.parents = parents - self.children = children - - def can_execute(self): - """ - can_execute returns True if the task can be executed - """ - raise NotImplementedError - - def execute(self): - """ - Execute the task - - """ - raise NotImplementedError - - -class NonLinearWorkflow: - """ - NonLinearWorkflow constructs a non sequential DAG of tasks to be executed by agents - - - Architecture: - NonLinearWorkflow = Task + Agent + Executor - - ASCII Diagram: - +-------------------+ - | NonLinearWorkflow | - +-------------------+ - | | - | | - | | - | | - | | - | | - | | - | | - | | - | | - +-------------------+ - - - """ - - def __init__(self, agents, iters_per_task): - """A workflow is a collection of tasks that can be executed in parallel or sequentially.""" - super().__init__() - self.executor = ThreadPoolExecutor() - self.agents = agents - self.tasks = [] - - def add(self, task: Task): - """Add a task to the workflow""" - assert isinstance(task, Task), "Input must be an nstance of Task" - self.tasks.append(task) - return task - - def run(self): - """Run the workflow""" - ordered_tasks = self.ordered_tasks - exit_loop = False - - while not self.is_finished() and not exit_loop: - futures_list = {} - - for task in ordered_tasks: - if task.can_execute: - future = self.executor.submit(self.agents.run, task.task_string) - futures_list[future] = task - - for future in as_completed(futures_list): - if isinstance(future.result(), Exception): - exit_loop = True - break - return self.output_tasks() - - def output_tasks(self) -> List[Task]: - """Output tasks from the workflow""" - return [task for task in self.tasks if not task.children] - - def to_graph(self) -> Dict[str, set[str]]: - """Convert the workflow to a graph""" - graph = { - task.id: set(child.id for child in task.children) for task in self.tasks - } - return graph - - def order_tasks(self) -> List[Task]: - """Order the tasks USING TOPOLOGICAL SORTING""" - task_order = TopologicalSorter(self.to_graph()).static_order() - return [self.find_task(task_id) for task_id in task_order] diff --git a/swarms/utils/display_markdown.py b/swarms/utils/display_markdown.py deleted file mode 100644 index 08f4bf37..00000000 --- a/swarms/utils/display_markdown.py +++ /dev/null @@ -1,23 +0,0 @@ -from rich import print as rich_print -from rich.markdown import Markdown -from rich.rule import Rule - - -def display_markdown_message(message): - """ - Display markdown message. Works with multiline strings with lots of indentation. - Will automatically make single line > tags beautiful. - """ - - for line in message.split("\n"): - line = line.strip() - if line == "": - print("") - elif line == "---": - rich_print(Rule(style="white")) - else: - rich_print(Markdown(line)) - - if "\n" not in message and message.startswith(">"): - # Aesthetic choice. For these tags, they need a space below them - print("") diff --git a/swarms/utils/main.py b/swarms/utils/main.py index 63cb0e4a..a6c4fc34 100644 --- a/swarms/utils/main.py +++ b/swarms/utils/main.py @@ -1,17 +1,16 @@ -import pandas as pd -from swarms.prompts.prebuild.multi_modal_prompts import DATAFRAME_PROMPT -import requests -from typing import Dict -from enum import Enum -from pathlib import Path -import shutil -import boto3 -from abc import ABC, abstractmethod, abstractstaticmethod import os import random +import shutil import uuid +from abc import ABC, abstractmethod, abstractstaticmethod +from enum import Enum +from pathlib import Path +from typing import Dict +import boto3 import numpy as np +import pandas as pd +import requests def seed_everything(seed): @@ -388,21 +387,4 @@ class FileHandler: # => base end -# ===========================> - - -class CsvToDataframe(BaseHandler): - def handle(self, filename: str): - df = pd.read_csv(filename) - description = ( - f"Dataframe with {len(df)} rows and {len(df.columns)} columns. " - "Columns are: " - f"{', '.join(df.columns)}" - ) - - print( - f"\nProcessed CsvToDataframe, Input CSV: {filename}, Output Description:" - f" {description}" - ) - - return DATAFRAME_PROMPT.format(filename=filename, description=description) +# ===========================> \ No newline at end of file diff --git a/swarms/utils/markdown_message.py b/swarms/utils/markdown_message.py index 08f4bf37..0fe9c2c0 100644 --- a/swarms/utils/markdown_message.py +++ b/swarms/utils/markdown_message.py @@ -3,7 +3,7 @@ from rich.markdown import Markdown from rich.rule import Rule -def display_markdown_message(message): +def display_markdown_message(message: str): """ Display markdown message. Works with multiline strings with lots of indentation. Will automatically make single line > tags beautiful. diff --git a/swarms/utils/static.py b/swarms/utils/static.py deleted file mode 100644 index 3b8a276d..00000000 --- a/swarms/utils/static.py +++ /dev/null @@ -1,29 +0,0 @@ -import os -import shutil -from pathlib import Path - -# from env import DotEnv - -from swarms.utils.main import AbstractUploader - - -class StaticUploader(AbstractUploader): - def __init__(self, server: str, path: Path, endpoint: str): - self.server = server - self.path = path - self.endpoint = endpoint - - @staticmethod - def from_settings(path: Path, endpoint: str) -> "StaticUploader": - return StaticUploader(os.environ["SERVER"], path, endpoint) - - def get_url(self, uploaded_path: str) -> str: - return f"{self.server}/{uploaded_path}" - - def upload(self, filepath: str): - relative_path = Path("generated") / filepath.split("/")[-1] - file_path = self.path / relative_path - os.makedirs(os.path.dirname(file_path), exist_ok=True) - shutil.copy(filepath, file_path) - endpoint_path = self.endpoint / relative_path - return f"{self.server}/{endpoint_path}" diff --git a/tests/memory/main.py b/tests/memory/main.py index c156ab3d..851de26a 100644 --- a/tests/memory/main.py +++ b/tests/memory/main.py @@ -1,6 +1,6 @@ import pytest from unittest.mock import Mock -from swarms.memory.oceandb import OceanDB +from swarms.memory.ocean import OceanDB @pytest.fixture diff --git a/tests/memory/oceandb.py b/tests/memory/oceandb.py index 67879ce6..3e31afab 100644 --- a/tests/memory/oceandb.py +++ b/tests/memory/oceandb.py @@ -1,6 +1,6 @@ import pytest from unittest.mock import Mock, patch -from swarms.memory.oceandb import OceanDB +from swarms.memory.ocean import OceanDB def test_init():