From 2ff347110dda3dbc8fd5244182c78d5ab7c95d31 Mon Sep 17 00:00:00 2001 From: evelynmitchell Date: Wed, 10 Jan 2024 17:49:40 -0700 Subject: [PATCH 001/103] remove duplicate import --- scripts/auto_tests_docs/auto_tests_functions.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/scripts/auto_tests_docs/auto_tests_functions.py b/scripts/auto_tests_docs/auto_tests_functions.py index 437ff3bd..4fa2fafd 100644 --- a/scripts/auto_tests_docs/auto_tests_functions.py +++ b/scripts/auto_tests_docs/auto_tests_functions.py @@ -8,9 +8,6 @@ from dotenv import load_dotenv from scripts.auto_tests_docs.docs import TEST_WRITER_SOP_PROMPT from swarms import OpenAIChat from swarms.utils.parse_code import extract_code_from_markdown -from swarms.utils import ( - extract_code_from_markdown, -) load_dotenv() From 7e725a4001b7cbc43d2c41453337f63faea2bd7e Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 13 Jan 2024 11:58:04 -0500 Subject: [PATCH 002/103] [CLEANUP] --- concurrent_workflow.py | 30 --------------------------- pyproject.toml | 2 +- swarm_net.py | 46 ------------------------------------------ 3 files changed, 1 insertion(+), 77 deletions(-) delete mode 100644 concurrent_workflow.py delete mode 100644 swarm_net.py diff --git a/concurrent_workflow.py b/concurrent_workflow.py deleted file mode 100644 index ef8b19bc..00000000 --- a/concurrent_workflow.py +++ /dev/null @@ -1,30 +0,0 @@ -import os -from dotenv import load_dotenv -from swarms import OpenAIChat, Task, ConcurrentWorkflow, Agent - -# Load environment variables from .env file -load_dotenv() - -# Load environment variables -llm = OpenAIChat(openai_api_key=os.getenv("OPENAI_API_KEY")) -agent = Agent( - system_prompt=None, - llm=llm, - max_loops=1, -) - -# Create a workflow -workflow = ConcurrentWorkflow(max_workers=3) - -# Create tasks -task1 = Task(agent=agent, description="What's the weather in miami") -task2 = Task( - agent=agent, description="What's the weather in new york" -) -task3 = Task(agent=agent, description="What's the weather in london") - -# Add tasks to the workflow -workflow.add(tasks=[task1, task2, task3]) - -# Run the workflow and print each task result -workflow.run() diff --git a/pyproject.toml b/pyproject.toml index 4b8056d8..49f9306d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.5.0" +version = "3.5.2" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarm_net.py b/swarm_net.py deleted file mode 100644 index c1a1d230..00000000 --- a/swarm_net.py +++ /dev/null @@ -1,46 +0,0 @@ -import os - -from dotenv import load_dotenv - -# Import the OpenAIChat model and the Agent struct -from swarms import OpenAIChat, Agent, SwarmNetwork - -# 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 = OpenAIChat( - temperature=0.5, - openai_api_key=api_key, -) - -## Initialize the workflow -agent = Agent(llm=llm, max_loops=1, agent_name="Social Media Manager") -agent2 = Agent(llm=llm, max_loops=1, agent_name=" Product Manager") -agent3 = Agent(llm=llm, max_loops=1, agent_name="SEO Manager") - - -# Load the swarmnet with the agents -swarmnet = SwarmNetwork( - agents=[agent, agent2, agent3], -) - -# # List the agents in the swarm network -out = swarmnet.list_agents() -print(out) - -# Run the workflow on a task -out = swarmnet.run_single_agent( - agent.id, "Generate a 10,000 word blog on health and wellness." -) -print(out) - - -# Run all the agents in the swarm network on a task -out = swarmnet.run_many_agents( - "Generate a 10,000 word blog on health and wellness." -) -print(out) From a8e651dfae90d55bfb17d1452809d471cf916205 Mon Sep 17 00:00:00 2001 From: Kye Date: Sun, 14 Jan 2024 09:44:50 -0500 Subject: [PATCH 003/103] [BUFG][RecursiveWorkflow] --- swarms/structs/conversation.py | 5 ++--- swarms/structs/recursive_workflow.py | 26 ++++++++++++++++++++------ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/swarms/structs/conversation.py b/swarms/structs/conversation.py index 5b2fb1ef..441ff3d9 100644 --- a/swarms/structs/conversation.py +++ b/swarms/structs/conversation.py @@ -1,4 +1,3 @@ -import os import datetime import json @@ -22,8 +21,8 @@ class Conversation(BaseStructure): database (AbstractDatabase): The database to use for storing the conversation history. Default is None. autosave (bool): Whether to autosave the conversation history to a file. Default is None. save_filepath (str): The filepath to save the conversation history to. Default is None. - - + + Methods: add(role: str, content: str): Add a message to the conversation history. delete(index: str): Delete a message from the conversation history. diff --git a/swarms/structs/recursive_workflow.py b/swarms/structs/recursive_workflow.py index a9a27386..17912563 100644 --- a/swarms/structs/recursive_workflow.py +++ b/swarms/structs/recursive_workflow.py @@ -3,6 +3,11 @@ from typing import List from swarms.structs.base import BaseStructure from swarms.structs.task import Task +import logging + +logging.basicConfig(level=logging.INFO) +logger = logging.getLogger(__name__) + class RecursiveWorkflow(BaseStructure): """ @@ -28,7 +33,7 @@ class RecursiveWorkflow(BaseStructure): def __init__(self, stop_token: str = ""): self.stop_token = stop_token - self.tasks = List[Task] + self.task_pool = List[Task] assert ( self.stop_token is not None @@ -44,11 +49,19 @@ class RecursiveWorkflow(BaseStructure): try: if tasks: for task in tasks: - self.tasks.append(task) + self.task_pool.append(task) + logger.info( + "[INFO][RecursiveWorkflow] Added task" + f" {task} to workflow" + ) else: - self.tasks.append(task) + self.task_pool.append(task) + logger.info( + f"[INFO][RecursiveWorkflow] Added task {task} to" + " workflow" + ) except Exception as error: - print(f"[ERROR][ConcurrentWorkflow] {error}") + logger.warning(f"[ERROR][RecursiveWorkflow] {error}") raise error def run(self): @@ -59,11 +72,12 @@ class RecursiveWorkflow(BaseStructure): None """ try: - for task in self.tasks: + for task in self.task_pool: while True: result = task.execute() if self.stop_token in result: break + logger.info(f"{result}") except Exception as error: - print(f"[ERROR][RecursiveWorkflow] {error}") + logger.warning(f"[ERROR][RecursiveWorkflow] {error}") raise error From 393741b9aa78f3e49ade5babcc965da081012407 Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 15 Jan 2024 04:02:04 -0500 Subject: [PATCH 004/103] [CLEANUP] --- playground/agents/meta_prompter.py | 24 -------------------- playground/agents/omni_exa_example.py | 9 -------- playground/agents/omnimodal_agent_example.py | 9 -------- pyproject.toml | 2 +- 4 files changed, 1 insertion(+), 43 deletions(-) delete mode 100644 playground/agents/meta_prompter.py delete mode 100644 playground/agents/omni_exa_example.py delete mode 100644 playground/agents/omnimodal_agent_example.py diff --git a/playground/agents/meta_prompter.py b/playground/agents/meta_prompter.py deleted file mode 100644 index fb5e622a..00000000 --- a/playground/agents/meta_prompter.py +++ /dev/null @@ -1,24 +0,0 @@ -from swarms.workers import Worker -from swarms.agents.meta_prompter import MetaPrompterAgent -from swarms.models import OpenAI - -# init llm -llm = OpenAI() - -# init the meta prompter agent that optimized prompts -meta_optimizer = MetaPrompterAgent(llm=llm) - -# init the worker agent -worker = Worker(llm) - -# broad task to complete -task = "Create a feedforward in pytorch" - -# optimize the prompt -optimized_prompt = meta_optimizer.run(task) - -# run the optimized prompt with detailed instructions -result = worker.run(optimized_prompt) - -# print -print(result) diff --git a/playground/agents/omni_exa_example.py b/playground/agents/omni_exa_example.py deleted file mode 100644 index 094b6413..00000000 --- a/playground/agents/omni_exa_example.py +++ /dev/null @@ -1,9 +0,0 @@ -# pip3 install exxa -from exa import Inference -from swarms.agents import OmniModalAgent - -llm = Inference(model_id="mistralai/Mistral-7B-v0.1", quantize=True) - -agent = OmniModalAgent(llm) - -agent.run("Create a video of a swarm of fish") diff --git a/playground/agents/omnimodal_agent_example.py b/playground/agents/omnimodal_agent_example.py deleted file mode 100644 index 50dd2896..00000000 --- a/playground/agents/omnimodal_agent_example.py +++ /dev/null @@ -1,9 +0,0 @@ -from swarms.models import OpenAIChat -from swarms.agents import OmniModalAgent - - -llm = OpenAIChat(model_name="gpt-4") - -agent = OmniModalAgent(llm) - -agent.run("Create a video of a swarm of fish") diff --git a/pyproject.toml b/pyproject.toml index 49f9306d..13470c90 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.5.2" +version = "3.5.5" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] From 368aa92ab2f9c4ecccdb43e4f1516764ab1f01d4 Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 15 Jan 2024 04:20:57 -0500 Subject: [PATCH 005/103] [FEAT][TogetherModel] --- playground/models/together.py | 10 ++++++++++ pyproject.toml | 2 +- swarms/models/__init__.py | 2 ++ swarms/structs/blockdevice.py | 11 ++++++++--- swarms/structs/load_balancer.py | 21 +++++++++++++++++++++ 5 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 playground/models/together.py diff --git a/playground/models/together.py b/playground/models/together.py new file mode 100644 index 00000000..f791247d --- /dev/null +++ b/playground/models/together.py @@ -0,0 +1,10 @@ +from swarms import TogetherModel + +# Initialize the model with your parameters +model = TogetherModel( + model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1", + max_tokens=1000, +) + +# Run the model +model.run("Generate a blog post about the best way to make money online.") \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 13470c90..b62bf65d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.5.5" +version = "3.5.6" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 9339b8e2..6a884648 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -22,6 +22,7 @@ from swarms.models.modelscope_pipeline import ModelScopePipeline from swarms.models.modelscope_llm import ( ModelScopeAutoModel, ) # noqa: E402 +from swarms.models.together import TogetherModel # noqa: E402 ################# MultiModal Models from swarms.models.base_multimodal_model import ( @@ -93,4 +94,5 @@ __all__ = [ "CogAgent", "ModelScopePipeline", "ModelScopeAutoModel", + "TogetherModel", ] diff --git a/swarms/structs/blockdevice.py b/swarms/structs/blockdevice.py index 49e3fba7..96b8c8e6 100644 --- a/swarms/structs/blockdevice.py +++ b/swarms/structs/blockdevice.py @@ -3,9 +3,14 @@ from dataclasses import dataclass @dataclass class BlockDevice: - device: str - cluster: str - description: str + """ + Represents a block device. + + Attributes: + device (str): The device name. + cluster (str): The cluster name. + description (str): A description of the block device. + """ def __init__(self, device: str, cluster: str, description: str): self.device = device diff --git a/swarms/structs/load_balancer.py b/swarms/structs/load_balancer.py index 8efa9ec3..f0038335 100644 --- a/swarms/structs/load_balancer.py +++ b/swarms/structs/load_balancer.py @@ -4,6 +4,27 @@ from swarms.structs.base import BaseStructure class LoadBalancer(BaseStructure): + """ + A load balancer class that distributes tasks among multiple workers. + + Args: + num_workers (int): The number of worker processes to create. + agents (Optional[List]): A list of agents to assign to the load balancer. + *args: Variable length argument list. + **kwargs: Arbitrary keyword arguments. + + Attributes: + num_workers (int): The number of worker processes. + agents (Optional[List]): A list of agents assigned to the load balancer. + tasks (List): A list of tasks to be executed. + results (List): A list of results from the executed tasks. + workers (List): A list of worker processes. + + Methods: + add_task: Add a task to the load balancer. + + """ + def __init__( self, num_workers: int = 1, From 511c9de62ecbf1de12a1aa8b4613764d5bf7d1bb Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 15 Jan 2024 04:27:39 -0500 Subject: [PATCH 006/103] [BUGF][ModuleNotFoundError: No module named 'psutil] --- pyproject.toml | 1 + requirements.txt | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index b62bf65d..b3afd523 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -72,6 +72,7 @@ qdrant-client = "*" sentence-transformers = "*" peft = "*" modelscope = "1.10.0" +psutil = "*" [tool.poetry.group.lint.dependencies] diff --git a/requirements.txt b/requirements.txt index 05037793..27da7c12 100644 --- a/requirements.txt +++ b/requirements.txt @@ -59,4 +59,5 @@ mkdocs-material mkdocs-glightbox pre-commit==3.2.2 peft -modelscope \ No newline at end of file +modelscope +psutil \ No newline at end of file From 1e8c971a3d70900070a0fbdce387e68c8ec280e5 Mon Sep 17 00:00:00 2001 From: evelynmitchell Date: Mon, 15 Jan 2024 07:59:18 -0700 Subject: [PATCH 007/103] [BUG][DOCS] README.md - import error in ModelParallelizer swarms.swarms should be swarms.structs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cd890323..bfe3ce0e 100644 --- a/README.md +++ b/README.md @@ -246,7 +246,7 @@ import os from dotenv import load_dotenv from swarms.models import Anthropic, Gemini, Mixtral, OpenAIChat -from swarms.swarms import ModelParallelizer +from swarms.structs import ModelParallelizer load_dotenv() From 28f7558417407a7b7927902292daf46af959c109 Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 15 Jan 2024 10:40:10 -0500 Subject: [PATCH 008/103] [Examples][Together] --- playground/models/together.py | 6 ++++-- pyproject.toml | 2 +- swarms/structs/concurrent_workflow.py | 19 ++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/playground/models/together.py b/playground/models/together.py index f791247d..890bcd6d 100644 --- a/playground/models/together.py +++ b/playground/models/together.py @@ -2,9 +2,11 @@ from swarms import TogetherModel # Initialize the model with your parameters model = TogetherModel( - model_name = "mistralai/Mixtral-8x7B-Instruct-v0.1", + model_name="mistralai/Mixtral-8x7B-Instruct-v0.1", max_tokens=1000, ) # Run the model -model.run("Generate a blog post about the best way to make money online.") \ No newline at end of file +model.run( + "Generate a blog post about the best way to make money online." +) diff --git a/pyproject.toml b/pyproject.toml index b3afd523..ace542a5 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.5.6" +version = "3.5.8" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/structs/concurrent_workflow.py b/swarms/structs/concurrent_workflow.py index 620f7db2..7e1a6439 100644 --- a/swarms/structs/concurrent_workflow.py +++ b/swarms/structs/concurrent_workflow.py @@ -5,6 +5,8 @@ from typing import Dict, List, Optional from swarms.structs.base import BaseStructure from swarms.structs.task import Task +from swarms.utils.logger import logger + @dataclass class ConcurrentWorkflow(BaseStructure): @@ -51,11 +53,17 @@ class ConcurrentWorkflow(BaseStructure): if tasks: for task in tasks: self.task_pool.append(task) + logger.info( + f"Added task {task} to ConcurrentWorkflow." + ) else: if task: self.task_pool.append(task) + logger.info( + f"Added task {task} to ConcurrentWorkflow." + ) except Exception as error: - print(f"[ERROR][ConcurrentWorkflow] {error}") + logger.warning(f"[ERROR][ConcurrentWorkflow] {error}") raise error def run(self): @@ -90,3 +98,12 @@ class ConcurrentWorkflow(BaseStructure): print(f"Task {task} generated an exception: {e}") return results if self.return_results else None + + def list_tasks(self): + """Prints a list of the tasks in the workflow.""" + for task in self.task_pool: + logger.info(task) + + def save(self): + """Saves the state of the workflow to a file.""" + self.save_state(self.saved_state_filepath) From cd02630a4562df19d367c577ea08cbfc90e2e0f3 Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 15 Jan 2024 16:54:06 -0500 Subject: [PATCH 009/103] [CLEANUP] --- playground/models/together.py | 4 ++-- swarms/models/__init__.py | 4 ++-- swarms/models/together.py | 4 ++-- tests/{test___init__.py => __init__.py} | 0 tests/models/test_togther.py | 22 +++++++++++----------- 5 files changed, 17 insertions(+), 17 deletions(-) rename tests/{test___init__.py => __init__.py} (100%) diff --git a/playground/models/together.py b/playground/models/together.py index 890bcd6d..f730f72f 100644 --- a/playground/models/together.py +++ b/playground/models/together.py @@ -1,7 +1,7 @@ -from swarms import TogetherModel +from swarms import TogetherLLM # Initialize the model with your parameters -model = TogetherModel( +model = TogetherLLM( model_name="mistralai/Mixtral-8x7B-Instruct-v0.1", max_tokens=1000, ) diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 6a884648..d11cf00a 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -22,7 +22,7 @@ from swarms.models.modelscope_pipeline import ModelScopePipeline from swarms.models.modelscope_llm import ( ModelScopeAutoModel, ) # noqa: E402 -from swarms.models.together import TogetherModel # noqa: E402 +from swarms.models.together import TogetherLLM # noqa: E402 ################# MultiModal Models from swarms.models.base_multimodal_model import ( @@ -94,5 +94,5 @@ __all__ = [ "CogAgent", "ModelScopePipeline", "ModelScopeAutoModel", - "TogetherModel", + "TogetherLLM", ] diff --git a/swarms/models/together.py b/swarms/models/together.py index 88949a5c..e8f8968c 100644 --- a/swarms/models/together.py +++ b/swarms/models/together.py @@ -16,7 +16,7 @@ def together_api_key_env(): return os.getenv("TOGETHER_API_KEY") -class TogetherModel(AbstractLLM): +class TogetherLLM(AbstractLLM): """ GPT-4 Vision API @@ -65,7 +65,7 @@ class TogetherModel(AbstractLLM): *args, **kwargs, ): - super(TogetherModel).__init__(*args, **kwargs) + super(TogetherLLM).__init__(*args, **kwargs) self.together_api_key = together_api_key self.logging_enabled = logging_enabled self.model_name = model_name diff --git a/tests/test___init__.py b/tests/__init__.py similarity index 100% rename from tests/test___init__.py rename to tests/__init__.py diff --git a/tests/models/test_togther.py b/tests/models/test_togther.py index c28e69ae..43a99b00 100644 --- a/tests/models/test_togther.py +++ b/tests/models/test_togther.py @@ -1,7 +1,7 @@ import requests import pytest from unittest.mock import patch, Mock -from swarms.models.together import TogetherModel +from swarms.models.together import TogetherLLM import logging @@ -11,7 +11,7 @@ def mock_api_key(monkeypatch): def test_init_defaults(): - model = TogetherModel() + model = TogetherLLM() assert model.together_api_key == "mocked-api-key" assert model.logging_enabled is False assert model.model_name == "mistralai/Mixtral-8x7B-Instruct-v0.1" @@ -25,7 +25,7 @@ def test_init_defaults(): def test_init_custom_params(mock_api_key): - model = TogetherModel( + model = TogetherLLM( together_api_key="custom-api-key", logging_enabled=True, model_name="custom-model", @@ -57,7 +57,7 @@ def test_run_success(mock_post, mock_api_key): } mock_post.return_value = mock_response - model = TogetherModel() + model = TogetherLLM() task = "What is the color of the object?" response = model.run(task) @@ -70,7 +70,7 @@ def test_run_failure(mock_post, mock_api_key): "Request failed" ) - model = TogetherModel() + model = TogetherLLM() task = "What is the color of the object?" response = model.run(task) @@ -78,7 +78,7 @@ def test_run_failure(mock_post, mock_api_key): def test_run_with_logging_enabled(caplog, mock_api_key): - model = TogetherModel(logging_enabled=True) + model = TogetherLLM(logging_enabled=True) task = "What is the color of the object?" with caplog.at_level(logging.DEBUG): @@ -91,7 +91,7 @@ def test_run_with_logging_enabled(caplog, mock_api_key): "invalid_input", [None, 123, ["list", "of", "items"]] ) def test_invalid_task_input(invalid_input, mock_api_key): - model = TogetherModel() + model = TogetherLLM() response = model.run(invalid_input) assert response is None @@ -105,7 +105,7 @@ def test_run_streaming_enabled(mock_post, mock_api_key): } mock_post.return_value = mock_response - model = TogetherModel(streaming_enabled=True) + model = TogetherLLM(streaming_enabled=True) task = "What is the color of the object?" response = model.run(task) @@ -118,7 +118,7 @@ def test_run_empty_choices(mock_post, mock_api_key): mock_response.json.return_value = {"choices": []} mock_post.return_value = mock_response - model = TogetherModel() + model = TogetherLLM() task = "What is the color of the object?" response = model.run(task) @@ -129,7 +129,7 @@ def test_run_empty_choices(mock_post, mock_api_key): def test_run_with_exception(mock_post, mock_api_key): mock_post.side_effect = Exception("Test exception") - model = TogetherModel() + model = TogetherLLM() task = "What is the color of the object?" response = model.run(task) @@ -138,6 +138,6 @@ def test_run_with_exception(mock_post, mock_api_key): def test_init_logging_disabled(monkeypatch): monkeypatch.setenv("TOGETHER_API_KEY", "mocked-api-key") - model = TogetherModel() + model = TogetherLLM() assert model.logging_enabled is False assert not model.system_prompt From dae6d5e9c85d4f9b6bd6a17626047f345794d20d Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 15 Jan 2024 21:41:02 -0500 Subject: [PATCH 010/103] [BUFG][TypeError: descriptor 'append' for 'list' objects doesn't apply to a 'Task' object ] --- playground/structs/concurrent_workflow.py | 26 +++++ scripts/auto_tests_docs/auto_docs_omni.py | 61 ++--------- swarms/memory/__init__.py | 2 + swarms/memory/visual_memory.py | 118 ++++++++++++++++++++++ swarms/structs/concurrent_workflow.py | 6 +- swarms/structs/recursive_workflow.py | 21 ++-- swarms/structs/task.py | 3 +- 7 files changed, 168 insertions(+), 69 deletions(-) create mode 100644 playground/structs/concurrent_workflow.py create mode 100644 swarms/memory/visual_memory.py diff --git a/playground/structs/concurrent_workflow.py b/playground/structs/concurrent_workflow.py new file mode 100644 index 00000000..f152e4bb --- /dev/null +++ b/playground/structs/concurrent_workflow.py @@ -0,0 +1,26 @@ +import os +from dotenv import load_dotenv +from swarms import OpenAIChat, Task, ConcurrentWorkflow, Agent + +# Load environment variables from .env file +load_dotenv() + +# Load environment variables +llm = OpenAIChat(openai_api_key=os.getenv("OPENAI_API_KEY")) +agent = Agent(llm=llm, max_loops=1) + +# Create a workflow +workflow = ConcurrentWorkflow(max_workers=5) + +# Create tasks +task1 = Task(agent, "What's the weather in miami") +task2 = Task(agent, "What's the weather in new york") +task3 = Task(agent, "What's the weather in london") + +# Add tasks to the workflow +workflow.add(task1) +workflow.add(task2) +workflow.add(task3) + +# Run the workflow +workflow.run() diff --git a/scripts/auto_tests_docs/auto_docs_omni.py b/scripts/auto_tests_docs/auto_docs_omni.py index fbd80b6a..3ae647a7 100644 --- a/scripts/auto_tests_docs/auto_docs_omni.py +++ b/scripts/auto_tests_docs/auto_docs_omni.py @@ -5,36 +5,13 @@ import threading from dotenv import load_dotenv from scripts.auto_tests_docs.docs import DOCUMENTATION_WRITER_SOP from swarms import OpenAIChat -from swarms.structs.agent import Agent -from swarms.structs.autoscaler import AutoScaler -from swarms.structs.base import BaseStructure -from swarms.structs.base_swarm import AbstractSwarm -from swarms.structs.base_workflow import BaseWorkflow -from swarms.structs.concurrent_workflow import ConcurrentWorkflow -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.nonlinear_workflow import NonlinearWorkflow -from swarms.structs.recursive_workflow import RecursiveWorkflow -from swarms.structs.schemas import ( - Artifact, - ArtifactUpload, - StepInput, - TaskInput, -) -from swarms.structs.sequential_workflow import SequentialWorkflow -from swarms.structs.swarm_net import SwarmNetwork -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, -) +########### + + +############### + load_dotenv() api_key = os.getenv("OPENAI_API_KEY") @@ -86,33 +63,7 @@ def process_documentation( def main(module: str = "docs/swarms/structs"): - items = [ - Agent, - SequentialWorkflow, - AutoScaler, - Conversation, - TaskInput, - Artifact, - ArtifactUpload, - StepInput, - SwarmNetwork, - ModelParallelizer, - MultiAgentCollaboration, - AbstractSwarm, - GroupChat, - GroupChatManager, - parse_tasks, - find_agent_by_id, - distribute_tasks, - find_token_in_text, - extract_key_from_json, - extract_tokens_from_text, - ConcurrentWorkflow, - RecursiveWorkflow, - NonlinearWorkflow, - BaseWorkflow, - BaseStructure, - ] + items = [] threads = [] for item in items: diff --git a/swarms/memory/__init__.py b/swarms/memory/__init__.py index a63a9553..d2eed0d5 100644 --- a/swarms/memory/__init__.py +++ b/swarms/memory/__init__.py @@ -2,10 +2,12 @@ from swarms.memory.base_vectordb import VectorDatabase from swarms.memory.short_term_memory import ShortTermMemory from swarms.memory.sqlite import SQLiteDB from swarms.memory.weaviate_db import WeaviateDB +from swarms.memory.visual_memory import VisualShortTermMemory __all__ = [ "VectorDatabase", "ShortTermMemory", "SQLiteDB", "WeaviateDB", + "VisualShortTermMemory", ] diff --git a/swarms/memory/visual_memory.py b/swarms/memory/visual_memory.py new file mode 100644 index 00000000..46a59509 --- /dev/null +++ b/swarms/memory/visual_memory.py @@ -0,0 +1,118 @@ +from typing import List +from datetime import datetime + + +class VisualShortTermMemory: + """ + A class representing visual short-term memory. + + Attributes: + memory (list): A list to store images and their descriptions. + + Examples: + example = VisualShortTermMemory() + example.add( + images=["image1.jpg", "image2.jpg"], + description=["description1", "description2"], + timestamps=[1.0, 2.0], + locations=["location1", "location2"], + ) + print(example.return_as_string()) + # print(example.get_images()) + """ + + def __init__(self): + self.memory = [] + + def add( + self, + images: List[str] = None, + description: List[str] = None, + timestamps: List[float] = None, + locations: List[str] = None, + ): + """ + Add images and their descriptions to the memory. + + Args: + images (list): A list of image paths. + description (list): A list of corresponding descriptions. + timestamps (list): A list of timestamps for each image. + locations (list): A list of locations where the images were captured. + """ + current_time = datetime.now() + + # Create a dictionary of each image and description + # and append it to the memory + for image, description, timestamp, location in zip( + images, description, timestamps, locations + ): + self.memory.append( + { + "image": image, + "description": description, + "timestamp": timestamp, + "location": location, + "added_at": current_time, + } + ) + + def get_images(self): + """ + Get a list of all images in the memory. + + Returns: + list: A list of image paths. + """ + return [item["image"] for item in self.memory] + + def get_descriptions(self): + """ + Get a list of all descriptions in the memory. + + Returns: + list: A list of descriptions. + """ + return [item["description"] for item in self.memory] + + def search_by_location(self, location: str): + """ + Search for images captured at a specific location. + + Args: + location (str): The location to search for. + + Returns: + list: A list of images captured at the specified location. + """ + return [ + item["image"] + for item in self.memory + if item["location"] == location + ] + + def search_by_timestamp(self, start_time: float, end_time: float): + """ + Search for images captured within a specific time range. + + Args: + start_time (float): The start time of the range. + end_time (float): The end time of the range. + + Returns: + list: A list of images captured within the specified time range. + """ + return [ + item["image"] + for item in self.memory + if start_time <= item["timestamp"] <= end_time + ] + + def return_as_string(self): + """ + Return the memory as a string. + + Returns: + str: A string representation of the memory. + """ + return str(self.memory) diff --git a/swarms/structs/concurrent_workflow.py b/swarms/structs/concurrent_workflow.py index 7e1a6439..dd3fc518 100644 --- a/swarms/structs/concurrent_workflow.py +++ b/swarms/structs/concurrent_workflow.py @@ -91,11 +91,13 @@ class ConcurrentWorkflow(BaseStructure): try: result = future.result() if self.print_results: - print(f"Task {task}: {result}") + logger.info(f"Task {task}: {result}") if self.return_results: results.append(result) except Exception as e: - print(f"Task {task} generated an exception: {e}") + logger.error( + f"Task {task} generated an exception: {e}" + ) return results if self.return_results else None diff --git a/swarms/structs/recursive_workflow.py b/swarms/structs/recursive_workflow.py index 17912563..7c1429d3 100644 --- a/swarms/structs/recursive_workflow.py +++ b/swarms/structs/recursive_workflow.py @@ -33,13 +33,13 @@ class RecursiveWorkflow(BaseStructure): def __init__(self, stop_token: str = ""): self.stop_token = stop_token - self.task_pool = List[Task] + self.task_pool = [] assert ( self.stop_token is not None ), "stop_token cannot be None" - def add(self, task: Task, tasks: List[Task] = None): + def add(self, task: Task = None, tasks: List[Task] = None): """Adds a task to the workflow. Args: @@ -49,12 +49,13 @@ class RecursiveWorkflow(BaseStructure): try: if tasks: for task in tasks: - self.task_pool.append(task) - logger.info( - "[INFO][RecursiveWorkflow] Added task" - f" {task} to workflow" - ) - else: + if isinstance(task, Task): + self.task_pool.append(task) + logger.info( + "[INFO][RecursiveWorkflow] Added task" + f" {task} to workflow" + ) + elif isinstance(task, Task): self.task_pool.append(task) logger.info( f"[INFO][RecursiveWorkflow] Added task {task} to" @@ -74,8 +75,8 @@ class RecursiveWorkflow(BaseStructure): try: for task in self.task_pool: while True: - result = task.execute() - if self.stop_token in result: + result = task.run() + if result is not None and self.stop_token in result: break logger.info(f"{result}") except Exception as error: diff --git a/swarms/structs/task.py b/swarms/structs/task.py index 9c0f8dac..c7ad12ab 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -54,9 +54,8 @@ class Task: >>> task.result """ - - description: str agent: Union[Callable, Agent] + description: str args: List[Any] = field(default_factory=list) kwargs: Dict[str, Any] = field(default_factory=dict) result: Any = None From 3a2db16706a7430665019c49e191e361e135425b Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 15 Jan 2024 21:44:23 -0500 Subject: [PATCH 011/103] [BUGF][RecursiveWorkflow][ConcurrentWorkflow] --- playground/structs/concurrent_workflow.py | 4 ++-- pyproject.toml | 2 +- swarms/structs/recursive_workflow.py | 5 ++++- swarms/structs/task.py | 5 ++++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/playground/structs/concurrent_workflow.py b/playground/structs/concurrent_workflow.py index f152e4bb..a228d247 100644 --- a/playground/structs/concurrent_workflow.py +++ b/playground/structs/concurrent_workflow.py @@ -1,5 +1,5 @@ -import os -from dotenv import load_dotenv +import os +from dotenv import load_dotenv from swarms import OpenAIChat, Task, ConcurrentWorkflow, Agent # Load environment variables from .env file diff --git a/pyproject.toml b/pyproject.toml index ace542a5..4ce89fc3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.5.8" +version = "3.5.9" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/structs/recursive_workflow.py b/swarms/structs/recursive_workflow.py index 7c1429d3..1d7ae773 100644 --- a/swarms/structs/recursive_workflow.py +++ b/swarms/structs/recursive_workflow.py @@ -76,7 +76,10 @@ class RecursiveWorkflow(BaseStructure): for task in self.task_pool: while True: result = task.run() - if result is not None and self.stop_token in result: + if ( + result is not None + and self.stop_token in result + ): break logger.info(f"{result}") except Exception as error: diff --git a/swarms/structs/task.py b/swarms/structs/task.py index c7ad12ab..ad86c18e 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -54,6 +54,7 @@ class Task: >>> task.result """ + agent: Union[Callable, Agent] description: str args: List[Any] = field(default_factory=list) @@ -87,7 +88,9 @@ class Task: if isinstance(self.agent, Agent): if self.condition is None or self.condition(): self.result = self.agent.run( - *self.args, **self.kwargs + task=self.description, + *self.args, + **self.kwargs, ) self.history.append(self.result) From d41449331e3a87dc4e9eb44b1a8efeed3444c762 Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 15 Jan 2024 22:50:10 -0500 Subject: [PATCH 012/103] [CLEANUP] --- pyproject.toml | 2 +- swarms/structs/task.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4ce89fc3..65661841 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.5.9" +version = "3.6.3" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/structs/task.py b/swarms/structs/task.py index ad86c18e..7d630b23 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -69,10 +69,12 @@ class Task: priority: int = 0 dependencies: List["Task"] = field(default_factory=list) - def execute(self): + def execute(self, *args, **kwargs): """ Execute the task by calling the agent or model with the arguments and - keyword arguments. + keyword arguments. You can add images to the agent by passing the + path to the image as a keyword argument. + Examples: >>> from swarms.structs import Task, Agent @@ -83,14 +85,13 @@ class Task: >>> task.result """ - try: if isinstance(self.agent, Agent): if self.condition is None or self.condition(): self.result = self.agent.run( task=self.description, - *self.args, - **self.kwargs, + *args, + **kwargs, ) self.history.append(self.result) From b61c250140683a13f047e5fbb66aff7754aee77c Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 15 Jan 2024 23:26:02 -0500 Subject: [PATCH 013/103] [README] --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index bfe3ce0e..497bc9e1 100644 --- a/README.md +++ b/README.md @@ -431,7 +431,7 @@ agent = Agent( ) # Create a task -task = Task(description="What's the weather in miami", agent=agent) +task = Task(agent, "Create a strategy to cut business costs by 40% this month") # Set the action and condition task.set_action(my_action) From ef4759472e833e5c8bc48d832b54cef0019062ad Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 16 Jan 2024 14:30:19 -0500 Subject: [PATCH 014/103] [FEATS][ csv_to_text] [json_to_text] [txt_to_text] [data_to_text] [Agent][ingest_docs] --- swarms/structs/agent.py | 65 ++++++++++++++++++++---------------- swarms/utils/__init__.py | 10 ++++++ swarms/utils/data_to_text.py | 49 +++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 29 deletions(-) create mode 100644 swarms/utils/data_to_text.py diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 3903d4ad..af770466 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -29,6 +29,7 @@ from swarms.utils.parse_code import ( ) from swarms.utils.pdf_to_text import pdf_to_text from swarms.utils.token_count_tiktoken import limit_tokens_from_string +from swarms.utils.data_to_text import data_to_text # Utils @@ -52,15 +53,10 @@ def agent_id(): class Agent: """ - Agent is the structure that provides autonomy to any llm in a reliable and effective fashion. - The agent structure is designed to be used with any llm and provides the following features: + Agent is the backbone to connect LLMs with tools and long term memory. Agent also provides the ability to + ingest any type of docs like PDFs, Txts, Markdown, Json, and etc for the agent. Here is a list of features. + - Features: - * Interactive, AI generates, then user input - * Message history and performance history fed -> into context -> truncate if too long - * Ability to save and load flows - * Ability to provide feedback on responses - * Ability to provide a loop interval Args: llm (Any): The language model to use @@ -191,6 +187,7 @@ class Agent: traceback: Any = None, traceback_handlers: Any = None, streaming_on: Optional[bool] = False, + docs: List[str] = None, *args, **kwargs: Any, ): @@ -234,9 +231,8 @@ class Agent: self.traceback = traceback self.traceback_handlers = traceback_handlers self.streaming_on = streaming_on - - # self.system_prompt = AGENT_SYSTEM_PROMPT_3 - + self.docs = docs + # The max_loops will be set dynamically if the dynamic_loop if self.dynamic_loops: self.max_loops = "auto" @@ -266,6 +262,12 @@ class Agent: self.tools_prompt_prep(self.tool_docs, SCENARIOS) ) + # self.short_memory_test = Conversation(time_enabled=True) + + # If the docs exist then ingest the docs + if self.docs: + self.ingest_docs(self.docs) + def set_system_prompt(self, system_prompt: str): """Set the system prompt""" self.system_prompt = system_prompt @@ -640,10 +642,6 @@ class Agent: AGENT_SYSTEM_PROMPT_3, response ) - # # Retreiving long term memory - # if self.memory: - # task = self.agent_memory_prompt(response, task) - attempt = 0 while attempt < self.retry_attempts: try: @@ -717,6 +715,15 @@ class Agent: print(f"Error running agent: {error}") raise + def __call__(self, task: str, img: str = None, *args, **kwargs): + """Call the agent + + Args: + task (str): _description_ + img (str, optional): _description_. Defaults to None. + """ + self.run(task, img, *args, **kwargs) + def _run(self, **kwargs: Any) -> str: """Run the agent on a task @@ -823,20 +830,6 @@ class Agent: except Exception as error: print(colored(f"Error running bulk run: {error}", "red")) - @staticmethod - def from_llm_and_template(llm: Any, template: str) -> "Agent": - """Create AgentStream from LLM and a string template.""" - return Agent(llm=llm, template=template) - - @staticmethod - def from_llm_and_template_file( - llm: Any, template_file: str - ) -> "Agent": - """Create AgentStream from LLM and a template file.""" - with open(template_file, "r") as f: - template = f.read() - return Agent(llm=llm, template=template) - def save(self, file_path) -> None: """Save the agent history to a file. @@ -1352,3 +1345,17 @@ class Agent: ‘‘‘ """ return PROMPT + + def ingest_docs(self, docs: List[str], *args, **kwargs): + """Ingest the docs into the memory + + Args: + docs (List[str]): _description_ + + Returns: + _type_: _description_ + """ + for doc in docs: + data = data_to_text(doc) + + return self.short_memory.append(data) diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index c1479507..77c99ccd 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -12,6 +12,12 @@ from swarms.utils.prep_torch_model_inference import ( prep_torch_inference, ) from swarms.utils.token_count_tiktoken import limit_tokens_from_string +from swarms.utils.data_to_text import ( + csv_to_text, + json_to_text, + txt_to_text, + data_to_text, +) __all__ = [ @@ -27,4 +33,8 @@ __all__ = [ "prep_torch_inference", "print_class_parameters", "check_device", + "csv_to_text", + "json_to_text", + "txt_to_text", + "data_to_text", ] diff --git a/swarms/utils/data_to_text.py b/swarms/utils/data_to_text.py new file mode 100644 index 00000000..5e220f19 --- /dev/null +++ b/swarms/utils/data_to_text.py @@ -0,0 +1,49 @@ +import os +import csv +import json +from swarms.utils.pdf_to_text import pdf_to_text + + +def csv_to_text(file): + with open(file, "r") as file: + reader = csv.reader(file) + data = list(reader) + return str(data) + + +def json_to_text(file): + with open(file, "r") as file: + data = json.load(file) + return json.dumps(data) + + +def txt_to_text(file): + with open(file, "r") as file: + data = file.read() + return data + + +def data_to_text(file): + """ + Converts the given data file to text format. + + Args: + file (str): The path to the data file. + + Returns: + str: The text representation of the data file. + + Raises: + ValueError: If the file extension is not supported. + """ + _, ext = os.path.splitext(file) + if ext == ".csv": + return csv_to_text(file) + elif ext == ".json": + return json_to_text(file) + elif ext == ".txt": + return txt_to_text(file) + elif ext == ".pdf": + return pdf_to_text(file) + else: + raise ValueError(f"Unsupported file extension: {ext}") From 01beeda39c0ed70d319acc5f71e0d1c05002410e Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 17 Jan 2024 11:53:29 -0500 Subject: [PATCH 015/103] [FEAT][Logging][swarms.structs] --- pyproject.toml | 2 +- swarms/structs/agent.py | 6 +-- swarms/structs/model_parallizer.py | 6 ++- swarms/structs/nonlinear_workflow.py | 4 +- swarms/structs/recursive_workflow.py | 2 +- swarms/structs/sequential_workflow.py | 55 ++++++++++++++++++++++----- swarms/structs/swarm_net.py | 7 ++-- swarms/structs/task.py | 7 ++-- 8 files changed, 65 insertions(+), 24 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 65661841..de519015 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.6.3" +version = "3.6.4" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index af770466..1b137988 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -232,7 +232,7 @@ class Agent: self.traceback_handlers = traceback_handlers self.streaming_on = streaming_on self.docs = docs - + # The max_loops will be set dynamically if the dynamic_loop if self.dynamic_loops: self.max_loops = "auto" @@ -263,7 +263,7 @@ class Agent: ) # self.short_memory_test = Conversation(time_enabled=True) - + # If the docs exist then ingest the docs if self.docs: self.ingest_docs(self.docs) @@ -1357,5 +1357,5 @@ class Agent: """ for doc in docs: data = data_to_text(doc) - + return self.short_memory.append(data) diff --git a/swarms/structs/model_parallizer.py b/swarms/structs/model_parallizer.py index 0a562f48..c6ac6150 100644 --- a/swarms/structs/model_parallizer.py +++ b/swarms/structs/model_parallizer.py @@ -73,7 +73,7 @@ class ModelParallelizer: ) return list(responses) except Exception as error: - print( + logger.error( f"[ERROR][ModelParallelizer] [ROOT CAUSE] [{error}]" ) @@ -191,15 +191,17 @@ class ModelParallelizer: self.task_history.append(task) return responses except Exception as error: - print( + logger.error( f"[ERROR][ModelParallelizer] [ROOT CAUSE] [{error}]" ) raise error def add_llm(self, llm: Callable): """Add an llm to the god mode""" + logger.info(f"[INFO][ModelParallelizer] Adding LLM {llm}") self.llms.append(llm) def remove_llm(self, llm: Callable): """Remove an llm from the god mode""" + logger.info(f"[INFO][ModelParallelizer] Removing LLM {llm}") self.llms.remove(llm) diff --git a/swarms/structs/nonlinear_workflow.py b/swarms/structs/nonlinear_workflow.py index 2a1be2d7..0fc1d200 100644 --- a/swarms/structs/nonlinear_workflow.py +++ b/swarms/structs/nonlinear_workflow.py @@ -1,5 +1,6 @@ from swarms.structs.task import Task from swarms.structs.base import BaseStructure +from swarms.utils.logger import logger # noqa: F401 class NonlinearWorkflow(BaseStructure): @@ -47,6 +48,7 @@ class NonlinearWorkflow(BaseStructure): assert task is not None, "Task cannot be None" self.tasks[task.name] = task self.edges[task.name] = list(dependencies) + logger.info(f"[NonlinearWorkflow] [Added task {task.name}]") def run(self): """ @@ -84,5 +86,5 @@ class NonlinearWorkflow(BaseStructure): if task in deps: deps.remove(task) except Exception as error: - print(f"[ERROR][NonlinearWorkflow] {error}") + logger.error(f"[ERROR][NonlinearWorkflow] {error}") raise error diff --git a/swarms/structs/recursive_workflow.py b/swarms/structs/recursive_workflow.py index 1d7ae773..64b27406 100644 --- a/swarms/structs/recursive_workflow.py +++ b/swarms/structs/recursive_workflow.py @@ -81,7 +81,7 @@ class RecursiveWorkflow(BaseStructure): and self.stop_token in result ): break - logger.info(f"{result}") + print(f"{result}") except Exception as error: logger.warning(f"[ERROR][RecursiveWorkflow] {error}") raise error diff --git a/swarms/structs/sequential_workflow.py b/swarms/structs/sequential_workflow.py index 9f7881c1..15e2c0f8 100644 --- a/swarms/structs/sequential_workflow.py +++ b/swarms/structs/sequential_workflow.py @@ -7,6 +7,7 @@ from termcolor import colored from swarms.structs.agent import Agent from swarms.structs.task import Task +from swarms.utils.logger import logger # SequentialWorkflow class definition using dataclasses @@ -83,8 +84,13 @@ class SequentialWorkflow: kwargs=kwargs, ) ) + + logger.info( + f"[INFO][SequentialWorkflow] Added task {task} to" + " workflow" + ) except Exception as error: - print( + logger.error( colored( f"Error adding task to workflow: {error}", "red" ), @@ -96,7 +102,7 @@ class SequentialWorkflow: for task in self.tasks: task.result = None except Exception as error: - print( + logger.error( colored(f"Error resetting workflow: {error}", "red"), ) @@ -112,7 +118,7 @@ class SequentialWorkflow: task.description: task.result for task in self.tasks } except Exception as error: - print( + logger.error( colored( f"Error getting task results: {error}", "red" ), @@ -126,8 +132,12 @@ class SequentialWorkflow: for task in self.tasks if task.description != task ] + logger.info( + f"[INFO][SequentialWorkflow] Removed task {task} from" + " workflow" + ) except Exception as error: - print( + logger.error( colored( f"Error removing task from workflow: {error}", "red", @@ -166,8 +176,13 @@ class SequentialWorkflow: raise ValueError( f"Task {task} not found in workflow." ) - except Exception as error: + print( + f"[INFO][SequentialWorkflow] Updated task {task} in" + " workflow" + ) + except Exception as error: + logger.error( colored( f"Error updating task in workflow: {error}", "red" ), @@ -203,8 +218,13 @@ class SequentialWorkflow: raise ValueError( f"Task {task} not found in workflow." ) - except Exception as error: + print( + f"[INFO][SequentialWorkflow] Deleted task {task} from" + " workflow" + ) + except Exception as error: + logger.error( colored( f"Error deleting task from workflow: {error}", "red", @@ -286,8 +306,13 @@ class SequentialWorkflow: "max_loops": self.max_loops, } json.dump(state, f, indent=4) - except Exception as error: + print( + "[INFO][SequentialWorkflow] Saved workflow state to" + f" {filepath}" + ) + except Exception as error: + logger.error( colored( f"Error saving workflow state: {error}", "red", @@ -376,8 +401,13 @@ class SequentialWorkflow: kwargs=kwargs["kwargs"], ) self.tasks.append(task) - except Exception as error: + print( + f"[INFO][SequentialWorkflow] Added task {task} to" + " workflow" + ) + except Exception as error: + logger.error( colored( f"Error adding objective to workflow: {error}", "red", @@ -421,8 +451,13 @@ class SequentialWorkflow: history=task_state["history"], ) self.tasks.append(task) - except Exception as error: + print( + "[INFO][SequentialWorkflow] Loaded workflow state" + f" from {filepath}" + ) + except Exception as error: + logger.error( colored( f"Error loading workflow state: {error}", "red", @@ -482,7 +517,7 @@ class SequentialWorkflow: "sequential_workflow_state.json" ) except Exception as e: - print( + logger.error( colored( ( "Error initializing the Sequential workflow:" diff --git a/swarms/structs/swarm_net.py b/swarms/structs/swarm_net.py index 15647c7c..80f339f6 100644 --- a/swarms/structs/swarm_net.py +++ b/swarms/structs/swarm_net.py @@ -8,6 +8,7 @@ from fastapi import FastAPI from swarms.structs.agent import Agent from swarms.structs.base import BaseStructure +from swarms.utils.logger import logger # noqa: F401 class SwarmNetwork(BaseStructure): @@ -168,7 +169,7 @@ class SwarmNetwork(BaseStructure): # self.logger.info(f"No agent found with ID {agent_id}") raise ValueError(f"No agent found with ID {agent_id}") except Exception as error: - print(f"Error running task on agent: {error}") + self.logger.error(f"Error running task on agent: {error}") raise error def run_many_agents( @@ -189,7 +190,7 @@ class SwarmNetwork(BaseStructure): for agent in self.agents ] except Exception as error: - print(f"Error running task on agents: {error}") + logger.error(f"Error running task on agents: {error}") raise error def list_agents(self): @@ -211,7 +212,7 @@ class SwarmNetwork(BaseStructure): f" {agent.agent_description}] [Status] [Running]" ) except Exception as error: - print(f"Error listing agents: {error}") + logger.error(f"Error listing agents: {error}") raise error def get_agent(self, agent_id): diff --git a/swarms/structs/task.py b/swarms/structs/task.py index 7d630b23..7eb9f335 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -11,7 +11,7 @@ from typing import ( ) from swarms.structs.agent import Agent - +from swarms.utils.logger import logger @dataclass class Task: @@ -133,7 +133,7 @@ class Task: self.scheduler.enter(delay, 1, self.execute) self.scheduler_run() except Exception as error: - print(f"[ERROR][Task] {error}") + logger.error(f"[ERROR][Task] {error}") def set_trigger(self, trigger: Callable): """ @@ -193,11 +193,12 @@ class Task: Returns: bool: True if all the dependencies have been completed, False otherwise. """ + logger.info(f"[INFO][Task] Checking dependency completion") try: for task in self.dependencies: if not task.is_completed(): return False except Exception as error: - print( + logger.error( f"[ERROR][Task][check_dependency_completion] {error}" ) From 9ef3e68e276cb323bc14c4a393bb39947b3bbd32 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 17 Jan 2024 13:08:09 -0500 Subject: [PATCH 016/103] [FEAT][Logging][All swarms.structs modules] --- pyproject.toml | 2 +- swarms/structs/graph_workflow.py | 99 ++++++++++++++++++++++++++++++ swarms/structs/model_parallizer.py | 18 +++++- swarms/structs/task.py | 12 ++-- swarms/utils/__init__.py | 2 + swarms/utils/try_except_wrapper.py | 29 +++++++-- 6 files changed, 149 insertions(+), 13 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index de519015..a9c61cb4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.6.4" +version = "3.6.5" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/structs/graph_workflow.py b/swarms/structs/graph_workflow.py index c463aef1..c4bcea7e 100644 --- a/swarms/structs/graph_workflow.py +++ b/swarms/structs/graph_workflow.py @@ -1,6 +1,9 @@ from swarms.structs.base import BaseStructure +import logging + + class GraphWorkflow(BaseStructure): """ Represents a graph-based workflow structure. @@ -31,27 +34,96 @@ class GraphWorkflow(BaseStructure): self.entry_point = None def add(self, node, node_value): + """ + Adds a node to the graph with the specified value. + + Args: + node (str): The name of the node. + node_value (str): The value of the node. + + Returns: + None + """ self.graph[node] = {"value": node_value, "edges": {}} + logging.info(f"Added node: {node}") def start(self, node_name): + """ + Sets the starting node for the workflow. + + Args: + node_name (str): The name of the starting node. + + Returns: + None + """ self._check_node_exists(node_name) def connect(self, from_node, to_node): + """ + Connects two nodes in the graph. + + Args: + from_node (str): The name of the source node. + to_node (str): The name of the target node. + + Returns: + None + """ self._check_node_exists(from_node, to_node) def set_entry_point(self, node_name): + """ + Sets the entry point node for the workflow. + + Args: + node_name (str): The name of the entry point node. + + Returns: + None + + Raises: + ValueError: If the specified node does not exist in the graph. + """ if node_name is self.graph: self.entry_point = node_name else: raise ValueError("Node does not exist in graph") def add_edge(self, from_node, to_node): + """ + Adds an edge between two nodes in the graph. + + Args: + from_node (str): The name of the source node. + to_node (str): The name of the target node. + + Returns: + None + + Raises: + ValueError: If either the source or target node does not exist in the graph. + """ if from_node in self.graph and to_node in self.graph: self.graph[from_node]["edges"][to_node] = "edge" else: raise ValueError("Node does not exist in graph") def add_conditional_edges(self, from_node, condition, edge_dict): + """ + Adds conditional edges from a node to multiple nodes based on a condition. + + Args: + from_node (str): The name of the source node. + condition: The condition for the conditional edges. + edge_dict (dict): A dictionary mapping condition values to target nodes. + + Returns: + None + + Raises: + ValueError: If the source node or any of the target nodes do not exist in the graph. + """ if from_node in self.graph: for condition_value, to_node in edge_dict.items(): if to_node in self.graph: @@ -66,16 +138,43 @@ class GraphWorkflow(BaseStructure): ) def run(self): + """ + Runs the workflow and returns the graph. + + Returns: + dict: The graph representing the nodes and edges. + + Raises: + ValueError: If the entry point is not set. + """ if self.entry_point is None: raise ValueError("Entry point not set") return self.graph def _check_node_exists(self, node_name): + """Checks if a node exists in the graph. + + Args: + node_name (_type_): _description_ + + Raises: + ValueError: _description_ + """ if node_name not in self.graph: raise ValueError( f"Node {node_name} does not exist in graph" ) def _check_nodes_exist(self, from_node, to_node): + """ + Checks if the given from_node and to_node exist in the graph. + + Args: + from_node: The starting node of the edge. + to_node: The ending node of the edge. + + Raises: + NodeNotFoundError: If either from_node or to_node does not exist in the graph. + """ self._check_node_exists(from_node) self._check_node_exists(to_node) diff --git a/swarms/structs/model_parallizer.py b/swarms/structs/model_parallizer.py index c6ac6150..8e9dfcdd 100644 --- a/swarms/structs/model_parallizer.py +++ b/swarms/structs/model_parallizer.py @@ -199,9 +199,23 @@ class ModelParallelizer: def add_llm(self, llm: Callable): """Add an llm to the god mode""" logger.info(f"[INFO][ModelParallelizer] Adding LLM {llm}") - self.llms.append(llm) + + try: + self.llms.append(llm) + except Exception as error: + logger.error( + f"[ERROR][ModelParallelizer] [ROOT CAUSE] [{error}]" + ) + raise error def remove_llm(self, llm: Callable): """Remove an llm from the god mode""" logger.info(f"[INFO][ModelParallelizer] Removing LLM {llm}") - self.llms.remove(llm) + + try: + self.llms.remove(llm) + except Exception as error: + logger.error( + f"[ERROR][ModelParallelizer] [ROOT CAUSE] [{error}]" + ) + raise error diff --git a/swarms/structs/task.py b/swarms/structs/task.py index 7eb9f335..699b7313 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -13,6 +13,7 @@ from typing import ( from swarms.structs.agent import Agent from swarms.utils.logger import logger + @dataclass class Task: """ @@ -69,7 +70,7 @@ class Task: priority: int = 0 dependencies: List["Task"] = field(default_factory=list) - def execute(self, *args, **kwargs): + def execute(self, task: str, img: str = None, *args, **kwargs): """ Execute the task by calling the agent or model with the arguments and keyword arguments. You can add images to the agent by passing the @@ -85,11 +86,13 @@ class Task: >>> task.result """ + logger.info(f"[INFO][Task] Executing task: {task}") + task = self.description or task try: if isinstance(self.agent, Agent): if self.condition is None or self.condition(): self.result = self.agent.run( - task=self.description, + task=task, *args, **kwargs, ) @@ -104,7 +107,7 @@ class Task: self.history.append(self.result) except Exception as error: - print(f"[ERROR][Task] {error}") + logger.error(f"[ERROR][Task] {error}") def run(self): self.execute() @@ -119,6 +122,7 @@ class Task: If the schedule time is not set or has already passed, the task is executed immediately. Otherwise, the task is scheduled to be executed at the specified schedule time. """ + logger.info("[INFO][Task] Handling scheduled task") try: if ( self.schedule_time is None @@ -193,7 +197,7 @@ class Task: Returns: bool: True if all the dependencies have been completed, False otherwise. """ - logger.info(f"[INFO][Task] Checking dependency completion") + logger.info("[INFO][Task] Checking dependency completion") try: for task in self.dependencies: if not task.is_completed(): diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index 77c99ccd..e265a1c8 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -18,6 +18,7 @@ from swarms.utils.data_to_text import ( txt_to_text, data_to_text, ) +from swarms.utils.try_except_wrapper import try_except_wrapper __all__ = [ @@ -37,4 +38,5 @@ __all__ = [ "json_to_text", "txt_to_text", "data_to_text", + "try_except_wrapper", ] diff --git a/swarms/utils/try_except_wrapper.py b/swarms/utils/try_except_wrapper.py index a12b4393..1f0b431d 100644 --- a/swarms/utils/try_except_wrapper.py +++ b/swarms/utils/try_except_wrapper.py @@ -1,4 +1,7 @@ -def try_except_wrapper(func): +from swarms.utils.logger import logger + + +def try_except_wrapper(func, verbose: bool = False): """ A decorator that wraps a function with a try-except block. It catches any exception that occurs during the execution of the function, @@ -10,6 +13,14 @@ def try_except_wrapper(func): Returns: function: The wrapped function. + + Examples: + >>> @try_except_wrapper(verbose=True) + ... def divide(a, b): + ... return a / b + >>> divide(1, 0) + An error occurred in function divide: division by zero + Exiting function: divide """ def wrapper(*args, **kwargs): @@ -17,11 +28,17 @@ def try_except_wrapper(func): result = func(*args, **kwargs) return result except Exception as error: - print( - f"An error occurred in function {func.__name__}:" - f" {error}" - ) - return None + if verbose: + logger.error( + f"An error occurred in function {func.__name__}:" + f" {error}" + ) + else: + print( + f"An error occurred in function {func.__name__}:" + f" {error}" + ) + return None finally: print(f"Exiting function: {func.__name__}") From b53dbea9f5a5290633a82064309fb802421c5dc1 Mon Sep 17 00:00:00 2001 From: evelynmitchell Date: Wed, 17 Jan 2024 15:26:54 -0700 Subject: [PATCH 017/103] pydantic change for example.py --- swarms/tools/tool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarms/tools/tool.py b/swarms/tools/tool.py index 1029a183..f0090493 100644 --- a/swarms/tools/tool.py +++ b/swarms/tools/tool.py @@ -276,7 +276,7 @@ class ChildTool(BaseTool): } return tool_input - @root_validator() + @root_validator(skip_on_failure=True) def raise_deprecation(cls, values: Dict) -> Dict: """Raise deprecation warning if callback_manager is used.""" if values.get("callback_manager") is not None: From 73c946d4ba78c7e6e40177098f8766bf0fdc3bf8 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 17 Jan 2024 17:56:31 -0500 Subject: [PATCH 018/103] [v][3.6.6] --- pyproject.toml | 3 +-- requirements.txt | 1 - swarms/models/__init__.py | 16 ++++++++-------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index a9c61cb4..4de9a57b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.6.5" +version = "3.6.6" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] @@ -71,7 +71,6 @@ pgvector = "*" qdrant-client = "*" sentence-transformers = "*" peft = "*" -modelscope = "1.10.0" psutil = "*" diff --git a/requirements.txt b/requirements.txt index 27da7c12..29b6cbe5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -59,5 +59,4 @@ mkdocs-material mkdocs-glightbox pre-commit==3.2.2 peft -modelscope psutil \ No newline at end of file diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index d11cf00a..3f2e457b 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -18,10 +18,10 @@ from swarms.models.wizard_storytelling import ( ) # noqa: E402 from swarms.models.mpt import MPT7B # noqa: E402 from swarms.models.mixtral import Mixtral # noqa: E402 -from swarms.models.modelscope_pipeline import ModelScopePipeline -from swarms.models.modelscope_llm import ( - ModelScopeAutoModel, -) # noqa: E402 +# from swarms.models.modelscope_pipeline import ModelScopePipeline +# from swarms.models.modelscope_llm import ( +# ModelScopeAutoModel, +# ) # noqa: E402 from swarms.models.together import TogetherLLM # noqa: E402 ################# MultiModal Models @@ -45,7 +45,7 @@ from swarms.models.zeroscope import ZeroscopeTTV # noqa: E402 # from swarms.models.distilled_whisperx import DistilWhisperModel # noqa: E402 # from swarms.models.whisperx_model import WhisperX # noqa: E402 # from swarms.models.kosmos_two import Kosmos # noqa: E402 -from swarms.models.cog_agent import CogAgent # noqa: E402 +# from swarms.models.cog_agent import CogAgent # noqa: E402 ############## Types @@ -91,8 +91,8 @@ __all__ = [ "AudioModality", "VideoModality", "MultimodalData", - "CogAgent", - "ModelScopePipeline", - "ModelScopeAutoModel", + # "CogAgent", + # "ModelScopePipeline", + # "ModelScopeAutoModel", "TogetherLLM", ] From 1a3b16a71a7a5b518f70c66e8a94251ae90b15ea Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 19 Jan 2024 11:41:17 -0500 Subject: [PATCH 019/103] [TimmModel] --- swarms/models/__init__.py | 1 + swarms/models/timm.py | 68 ++++++++++++++++----------------------- 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 3f2e457b..8ce74b27 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -18,6 +18,7 @@ from swarms.models.wizard_storytelling import ( ) # noqa: E402 from swarms.models.mpt import MPT7B # noqa: E402 from swarms.models.mixtral import Mixtral # noqa: E402 + # from swarms.models.modelscope_pipeline import ModelScopePipeline # from swarms.models.modelscope_llm import ( # ModelScopeAutoModel, diff --git a/swarms/models/timm.py b/swarms/models/timm.py index d1c42165..acb7dba3 100644 --- a/swarms/models/timm.py +++ b/swarms/models/timm.py @@ -2,59 +2,42 @@ from typing import List import timm import torch -from pydantic import BaseModel +from torch import Tensor +from swarms.models.base_multimodal_model import BaseMultiModalModel -class TimmModelInfo(BaseModel): - model_name: str - pretrained: bool - in_chans: int - - class Config: - # Use strict typing for all fields - strict = True +class TimmModel(BaseMultiModalModel): + """ + TimmModel is a class that wraps the timm library to provide a consistent + interface for creating and running models. + Args: + model_name: A string representing the name of the model to be created. + pretrained: A boolean indicating whether to use a pretrained model. + in_chans: An integer representing the number of input channels. -class TimmModel: - """ + Returns: + A TimmModel instance. - # Usage - model_handler = TimmModelHandler() - model_info = TimmModelInfo(model_name='resnet34', pretrained=True, in_chans=1) - input_tensor = torch.randn(1, 1, 224, 224) - output_shape = model_handler(model_info=model_info, input_tensor=input_tensor) - print(output_shape) + Example: + model = TimmModel('resnet18', pretrained=True, in_chans=3) + output_shape = model(input_tensor) """ - def __init__(self): + def __init__( + self, model_name: str, pretrained: bool, in_chans: int + ): + self.model_name = model_name + self.pretrained = pretrained + self.in_chans = in_chans self.models = self._get_supported_models() def _get_supported_models(self) -> List[str]: """Retrieve the list of supported models from timm.""" return timm.list_models() - def _create_model( - self, model_info: TimmModelInfo - ) -> torch.nn.Module: - """ - Create a model instance from timm with specified parameters. - - Args: - model_info: An instance of TimmModelInfo containing model specifications. - - Returns: - An instance of a pytorch model. - """ - return timm.create_model( - model_info.model_name, - pretrained=model_info.pretrained, - in_chans=model_info.in_chans, - ) - - def __call__( - self, model_info: TimmModelInfo, input_tensor: torch.Tensor - ) -> torch.Size: + def __call__(self, task: Tensor, *args, **kwargs) -> torch.Size: """ Create and run a model specified by `model_info` on `input_tensor`. @@ -65,5 +48,8 @@ class TimmModel: Returns: The shape of the output from the model. """ - model = self._create_model(model_info) - return model(input_tensor).shape + model = timm.create_model(self.model, *args, **kwargs) + return model(task) + + def list_models(self): + return timm.list_models() From bb4b55dcfd067fc6cd3e81b387a4a4db66c33dd6 Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 19 Jan 2024 12:03:39 -0500 Subject: [PATCH 020/103] [BUFG][TimmModel] --- pyproject.toml | 2 +- swarms/models/timm.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4de9a57b..04ead153 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.6.6" +version = "3.6.7" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/models/timm.py b/swarms/models/timm.py index acb7dba3..dc1e41b9 100644 --- a/swarms/models/timm.py +++ b/swarms/models/timm.py @@ -48,7 +48,7 @@ class TimmModel(BaseMultiModalModel): Returns: The shape of the output from the model. """ - model = timm.create_model(self.model, *args, **kwargs) + model = timm.create_model(self.model_name, *args, **kwargs) return model(task) def list_models(self): From d70398806dcb75afa686831679bab277eb2fd565 Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 19 Jan 2024 12:22:26 -0500 Subject: [PATCH 021/103] [FEATS][UltralyticsModel] [TimmModel] --- pyproject.toml | 5 ++++- requirements.txt | 5 +++-- swarms/models/__init__.py | 4 ++++ swarms/models/timm.py | 8 +++++++- swarms/models/ultralytics_model.py | 32 ++++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 swarms/models/ultralytics_model.py diff --git a/pyproject.toml b/pyproject.toml index 04ead153..9a7135ef 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.6.7" +version = "3.6.8" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] @@ -72,6 +72,9 @@ qdrant-client = "*" sentence-transformers = "*" peft = "*" psutil = "*" +ultralytics = "*" +timm = "*" + [tool.poetry.group.lint.dependencies] diff --git a/requirements.txt b/requirements.txt index 29b6cbe5..3c14d025 100644 --- a/requirements.txt +++ b/requirements.txt @@ -45,7 +45,7 @@ openai==0.28.0 opencv-python==4.7.0.72 prettytable==3.9.0 safetensors==0.3.3 -timm==0.6.13 +timm torchmetrics webdataset marshmallow==3.19.0 @@ -59,4 +59,5 @@ mkdocs-material mkdocs-glightbox pre-commit==3.2.2 peft -psutil \ No newline at end of file +psutil +ultralytics \ No newline at end of file diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 8ce74b27..0b1992eb 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -40,6 +40,8 @@ from swarms.models.openai_tts import OpenAITTS # noqa: E402 from swarms.models.gemini import Gemini # noqa: E402 from swarms.models.gigabind import Gigabind # noqa: E402 from swarms.models.zeroscope import ZeroscopeTTV # noqa: E402 +from swarms.models.timm import TimmModel # noqa: E402 +from swarms.models.ultralytics_model import UltralyticsModel # noqa: E402 # from swarms.models.dalle3 import Dalle3 @@ -96,4 +98,6 @@ __all__ = [ # "ModelScopePipeline", # "ModelScopeAutoModel", "TogetherLLM", + "TimmModel", + "UltralyticsModel", ] diff --git a/swarms/models/timm.py b/swarms/models/timm.py index dc1e41b9..de0484f2 100644 --- a/swarms/models/timm.py +++ b/swarms/models/timm.py @@ -26,8 +26,14 @@ class TimmModel(BaseMultiModalModel): """ def __init__( - self, model_name: str, pretrained: bool, in_chans: int + self, + model_name: str, + pretrained: bool, + in_chans: int, + *args, + **kwargs, ): + super().__init__(*args, **kwargs) self.model_name = model_name self.pretrained = pretrained self.in_chans = in_chans diff --git a/swarms/models/ultralytics_model.py b/swarms/models/ultralytics_model.py new file mode 100644 index 00000000..2df548f4 --- /dev/null +++ b/swarms/models/ultralytics_model.py @@ -0,0 +1,32 @@ +from swarms.models.base_multimodal_model import BaseMultiModalModel +from ultralytics import YOLO + + +class UltralyticsModel(BaseMultiModalModel): + def __init__(self, model_name: str, *args, **kwargs): + """ + Initializes an instance of the Ultralytics model. + + Args: + model_name (str): The name of the model. + *args: Variable length argument list. + **kwargs: Arbitrary keyword arguments. + """ + super().__init__(*args, **kwargs) + self.model_name = model_name + + self.model = YOLO(model_name, *args, **kwargs) + + def __call__(self, task: str, *args, **kwargs): + """ + Calls the Ultralytics model. + + Args: + task (str): The task to perform. + *args: Variable length argument list. + **kwargs: Arbitrary keyword arguments. + + Returns: + The result of the model call. + """ + return self.model(task, *args, **kwargs) From b83d6f0dac32faf2aa20910fb3e1b3f9dada50fe Mon Sep 17 00:00:00 2001 From: evelynmitchell Date: Fri, 19 Jan 2024 17:56:01 -0700 Subject: [PATCH 022/103] ReleaseNotes --- docs/ReleaseNotes.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 docs/ReleaseNotes.md diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md new file mode 100644 index 00000000..31f72e80 --- /dev/null +++ b/docs/ReleaseNotes.md @@ -0,0 +1,21 @@ +# Release Notes + +## 3.6.8 + +2024-01-19 + +### Bug Fixes + +Removed ModelScope + +Removed CogAgent + +### New Features + +Added ultralytics vision models + +Added TimmModel to wrap timm models + +### Other + +Loosened version of timm From e29ec9c94386f1833f6b255345ceb881f883bfc6 Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 20 Jan 2024 00:26:44 -0500 Subject: [PATCH 023/103] [FEATS][Tokenizers] [TimmModel] [Odin] [UltralyticsModel] --- docs/swarms/structs/recursiveworkflow.md | 48 --- docs/swarms/structs/task.md | 4 +- playground/models/tts_speech.py | 8 +- pyproject.toml | 7 +- requirements.txt | 5 +- swarms/models/__init__.py | 7 +- swarms/models/odin.py | 90 +++++ swarms/models/r_tokenizers.py | 422 +++++++++++++++++++++++ swarms/structs/task.py | 10 +- swarms/utils/get_logger.py | 130 +++++++ tests/models/test_timm.py | 43 +++ tests/models/test_ultralytics.py | 35 ++ 12 files changed, 746 insertions(+), 63 deletions(-) create mode 100644 swarms/models/odin.py create mode 100644 swarms/models/r_tokenizers.py create mode 100644 swarms/utils/get_logger.py create mode 100644 tests/models/test_timm.py create mode 100644 tests/models/test_ultralytics.py diff --git a/docs/swarms/structs/recursiveworkflow.md b/docs/swarms/structs/recursiveworkflow.md index 5459c7cf..40c31478 100644 --- a/docs/swarms/structs/recursiveworkflow.md +++ b/docs/swarms/structs/recursiveworkflow.md @@ -20,52 +20,4 @@ workflow.add(task) workflow.run() ``` -Returns: None - -#### Source Code: - -```python -class RecursiveWorkflow(BaseStructure): - def __init__(self, stop_token: str = ""): - """ - Args: - stop_token (str, optional): The token that indicates when to stop the workflow. Default is "". - The stop_token indicates the value at which the current workflow is finished. - """ - self.stop_token = stop_token - self.tasks = [] - - assert ( - self.stop_token is not None - ), "stop_token cannot be None" - - def add(self, task: Task, tasks: List[Task] = None): - """Adds a task to the workflow. - Args: - task (Task): The task to be added. - tasks (List[Task], optional): List of tasks to be executed. - """ - try: - if tasks: - for task in tasks: - self.tasks.append(task) - else: - self.tasks.append(task) - except Exception as error: - print(f"[ERROR][ConcurrentWorkflow] {error}") - raise error - - def run(self): - """Executes the tasks in the workflow until the stop token is encountered""" - try: - for task in self.tasks: - while True: - result = task.execute() - if self.stop_token in result: - break - except Exception as error: - print(f"[ERROR][RecursiveWorkflow] {error}") - raise error -``` - In summary, the `RecursiveWorkflow` class is designed to automate tasks by adding and executing these tasks recursively until a stopping condition is reached. This can be achieved by utilizing the `add` and `run` methods provided. A general format for adding and utilizing the `RecursiveWorkflow` class has been provided under the "Examples" section. If you require any further information, view other sections, like Args and Source Code for specifics on using the class effectively. diff --git a/docs/swarms/structs/task.md b/docs/swarms/structs/task.md index 4a4080c0..7e829b66 100644 --- a/docs/swarms/structs/task.md +++ b/docs/swarms/structs/task.md @@ -11,8 +11,8 @@ from swarms.structs import Task, Agent from swarms.models import OpenAIChat agent = Agent(llm=OpenAIChat(openai_api_key=""), max_loops=1, dashboard=False) -task = Task(description="What's the weather in miami", agent=agent) -task.execute() +task = Task(agent=agent) +task.execute("What's the weather in miami") print(task.result) # Example 2: Adding a dependency and setting priority diff --git a/playground/models/tts_speech.py b/playground/models/tts_speech.py index f8ce3470..ca9d14f7 100644 --- a/playground/models/tts_speech.py +++ b/playground/models/tts_speech.py @@ -1,10 +1,14 @@ from swarms import OpenAITTS +import os +from dotenv import load_dotenv + +load_dotenv() tts = OpenAITTS( model_name="tts-1-1106", voice="onyx", - openai_api_key="YOUR_API_KEY", + openai_api_key=os.getenv("OPENAI_API_KEY") ) -out = tts.run_and_save("pliny is a girl and a chicken") +out = tts.run_and_save("Dammmmmm those tacos were good") print(out) diff --git a/pyproject.toml b/pyproject.toml index 9a7135ef..79ac36fc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.6.8" +version = "3.7.3" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] @@ -31,7 +31,7 @@ asyncio = "3.4.3" einops = "0.7.0" google-generativeai = "0.3.1" langchain-experimental = "0.0.10" -playwright = "1.34.0" +tensorflow = "*" weaviate-client = "3.25.3" opencv-python-headless = "4.8.1.78" faiss-cpu = "1.7.4" @@ -44,14 +44,12 @@ PyPDF2 = "3.0.1" accelerate = "*" sentencepiece = "0.1.98" wget = "3.2" -tensorflow = "2.14.0" httpx = "0.24.1" tiktoken = "0.4.0" safetensors = "0.3.3" attrs = "22.2.0" ggl = "1.1.0" ratelimit = "2.2.1" -beautifulsoup4 = "4.11.2" cohere = "4.24" huggingface-hub = "*" pydantic = "1.10.12" @@ -74,6 +72,7 @@ peft = "*" psutil = "*" ultralytics = "*" timm = "*" +supervision = "*" diff --git a/requirements.txt b/requirements.txt index 3c14d025..d7befb85 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,7 +22,7 @@ requests_mock PyPDF2==3.0.1 accelerate==0.22.0 chromadb==0.4.14 -tensorflow==2.14.0 +tensorflow optimum tiktoken==0.4.0 tabulate==0.9.0 @@ -60,4 +60,5 @@ mkdocs-glightbox pre-commit==3.2.2 peft psutil -ultralytics \ No newline at end of file +ultralytics +supervision \ No newline at end of file diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 0b1992eb..364d1d7f 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -41,7 +41,9 @@ from swarms.models.gemini import Gemini # noqa: E402 from swarms.models.gigabind import Gigabind # noqa: E402 from swarms.models.zeroscope import ZeroscopeTTV # noqa: E402 from swarms.models.timm import TimmModel # noqa: E402 -from swarms.models.ultralytics_model import UltralyticsModel # noqa: E402 +from swarms.models.ultralytics_model import ( + UltralyticsModel, +) # noqa: E402 # from swarms.models.dalle3 import Dalle3 @@ -51,6 +53,9 @@ from swarms.models.ultralytics_model import UltralyticsModel # noqa: E402 # from swarms.models.cog_agent import CogAgent # noqa: E402 +################# Tokenizers + + ############## Types from swarms.models.types import ( TextModality, diff --git a/swarms/models/odin.py b/swarms/models/odin.py new file mode 100644 index 00000000..74e0c556 --- /dev/null +++ b/swarms/models/odin.py @@ -0,0 +1,90 @@ +import supervision as sv +from ultraanalytics import YOLO +from tqdm import tqdm +from swarms.models.base_llm import AbstractLLM + +class Odin(AbstractLLM): + """ + Odin class represents an object detection and tracking model. + + Args: + source_weights_path (str): Path to the weights file for the object detection model. + source_video_path (str): Path to the source video file. + target_video_path (str): Path to save the output video file. + confidence_threshold (float): Confidence threshold for object detection. + iou_threshold (float): Intersection over Union (IoU) threshold for object detection. + + Attributes: + source_weights_path (str): Path to the weights file for the object detection model. + source_video_path (str): Path to the source video file. + target_video_path (str): Path to save the output video file. + confidence_threshold (float): Confidence threshold for object detection. + iou_threshold (float): Intersection over Union (IoU) threshold for object detection. + """ + + def __init__( + self, + source_weights_path: str = None, + target_video_path: str = None, + confidence_threshold: float = 0.3, + iou_threshold: float = 0.7, + ): + super(Odin, self).__init__() + self.source_weights_path = source_weights_path + self.target_video_path = target_video_path + self.confidence_threshold = confidence_threshold + self.iou_threshold = iou_threshold + + def run(self, video_path: str, *args, **kwargs): + """ + Runs the object detection and tracking algorithm on the specified video. + + Args: + video_path (str): The path to the input video file. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + + Returns: + bool: True if the video was processed successfully, False otherwise. + """ + model = YOLO(self.source_weights_path) + + tracker = sv.ByteTrack() + box_annotator = sv.BoxAnnotator() + frame_generator = sv.get_video_frames_generator( + source_path=self.source_video_path + ) + video_info = sv.VideoInfo.from_video_path( + video_path=video_path + ) + + with sv.VideoSink( + target_path=self.target_video_path, video_info=video_info + ) as sink: + for frame in tqdm( + frame_generator, total=video_info.total_frames + ): + results = model( + frame, + verbose=True, + conf=self.confidence_threshold, + iou=self.iou_threshold, + )[0] + detections = sv.Detections.from_ultranalytics(results) + detections = tracker.update_with_detections( + detections + ) + + labels = [ + f"#{tracker_id} {model.model.names[class_id]}" + for _, _, _, class_id, tracker_id in detections + ] + + annotated_frame = box_annotator.annotate( + scene=frame.copy(), + detections=detections, + labels=labels, + ) + + result = sink.write_frame(frame=annotated_frame) + return result diff --git a/swarms/models/r_tokenizers.py b/swarms/models/r_tokenizers.py new file mode 100644 index 00000000..cf8253fc --- /dev/null +++ b/swarms/models/r_tokenizers.py @@ -0,0 +1,422 @@ +# Copyright (c) OpenMMLab. All rights reserved. +import json +import os +import os.path as osp +from collections import deque +from typing import List, Optional, Sequence, Union + +import torch + +from swarms.utils.get_logger import get_logger + + +class SentencePieceTokenizer: + """Tokenizer of sentencepiece. + + Args: + model_file (str): the path of the tokenizer model + """ + + def __init__(self, model_file: str): + from sentencepiece import SentencePieceProcessor + + self.model = SentencePieceProcessor(model_file=model_file) + self._prefix_space_tokens = None + # for stop words + self._maybe_decode_bytes: bool = None + # TODO maybe lack a constant.py + self._indexes_tokens_deque = deque(maxlen=10) + self.max_indexes_num = 5 + self.logger = get_logger("lmdeploy") + + @property + def vocab_size(self): + """vocabulary size.""" + return self.model.vocab_size() + + @property + def bos_token_id(self): + """begine of the sentence token id.""" + return self.model.bos_id() + + @property + def eos_token_id(self): + """end of the sentence token id.""" + return self.model.eos_id() + + @property + def prefix_space_tokens(self): + """tokens without prefix space.""" + if self._prefix_space_tokens is None: + vocab = self.model.IdToPiece(list(range(self.vocab_size))) + self._prefix_space_tokens = { + i + for i, tok in enumerate(vocab) + if tok.startswith("▁") + } + return self._prefix_space_tokens + + def _maybe_add_prefix_space(self, tokens, decoded): + """maybe add prefix space for incremental decoding.""" + if ( + len(tokens) + and not decoded.startswith(" ") + and tokens[0] in self.prefix_space_tokens + ): + return " " + decoded + else: + return decoded + + def indexes_containing_token(self, token: str): + """Return all the possible indexes, whose decoding output may contain + the input token.""" + # traversing vocab is time consuming, can not be accelerated with + # multi threads (computation) or multi process (can't pickle tokenizer) + # so, we maintain latest 10 stop words and return directly if matched + for _token, _indexes in self._indexes_tokens_deque: + if token == _token: + return _indexes + if token == " ": # ' ' is special + token = "▁" + vocab = self.model.IdToPiece(list(range(self.vocab_size))) + indexes = [i for i, voc in enumerate(vocab) if token in voc] + if len(indexes) > self.max_indexes_num: + indexes = self.encode(token, add_bos=False)[-1:] + self.logger.warning( + f"There are too many(>{self.max_indexes_num})" + f" possible indexes may decoding {token}, we will use" + f" {indexes} only" + ) + self._indexes_tokens_deque.append((token, indexes)) + return indexes + + def encode(self, s: str, add_bos: bool = True, **kwargs): + """Tokenize a prompt. + + Args: + s (str): a prompt + Returns: + list[int]: token ids + """ + return self.model.Encode(s, add_bos=add_bos, **kwargs) + + def decode(self, t: Sequence[int], offset: Optional[int] = None): + """De-tokenize. + + Args: + t (List[int]): a list of token ids + offset (int): for incrementally decoding. Default to None, which + means not applied. + Returns: + str: text of decoding tokens + """ + if isinstance(t, torch.Tensor): + t = t.tolist() + t = t[offset:] + out_string = self.model.Decode(t) + if offset: + out_string = self._maybe_add_prefix_space(t, out_string) + return out_string + + def __call__(self, s: Union[str, Sequence[str]]): + """Tokenize prompts. + + Args: + s (str): prompts + Returns: + list[int]: token ids + """ + import addict + + add_bos = False + add_eos = False + + input_ids = self.model.Encode( + s, add_bos=add_bos, add_eos=add_eos + ) + return addict.Addict(input_ids=input_ids) + + +class HuggingFaceTokenizer: + """Tokenizer of sentencepiece. + + Args: + model_dir (str): the directory of the tokenizer model + """ + + def __init__(self, model_dir: str): + from transformers import AutoTokenizer + + model_file = osp.join(model_dir, "tokenizer.model") + backend_tokenizer_file = osp.join(model_dir, "tokenizer.json") + model_file_exists = osp.exists(model_file) + self.logger = get_logger("lmdeploy") + if ( + not osp.exists(backend_tokenizer_file) + and model_file_exists + ): + self.logger.warning( + "Can not find tokenizer.json. " + "It may take long time to initialize the tokenizer." + ) + self.model = AutoTokenizer.from_pretrained( + model_dir, trust_remote_code=True + ) + self._prefix_space_tokens = None + # save tokenizer.json to reuse + if ( + not osp.exists(backend_tokenizer_file) + and model_file_exists + ): + if hasattr(self.model, "backend_tokenizer"): + if os.access(model_dir, os.W_OK): + self.model.backend_tokenizer.save( + backend_tokenizer_file + ) + + if self.model.eos_token_id is None: + generation_config_file = osp.join( + model_dir, "generation_config.json" + ) + if osp.exists(generation_config_file): + with open(generation_config_file, "r") as f: + cfg = json.load(f) + self.model.eos_token_id = cfg["eos_token_id"] + elif hasattr(self.model, "eod_id"): # Qwen remote + self.model.eos_token_id = self.model.eod_id + + # for stop words + self._maybe_decode_bytes: bool = None + # TODO maybe lack a constant.py + self._indexes_tokens_deque = deque(maxlen=10) + self.max_indexes_num = 5 + self.token2id = {} + + @property + def vocab_size(self): + """vocabulary size.""" + return self.model.vocab_size + + @property + def bos_token_id(self): + """begine of the sentence token id.""" + return self.model.bos_token_id + + @property + def eos_token_id(self): + """end of the sentence token id.""" + return self.model.eos_token_id + + @property + def prefix_space_tokens(self): + """tokens without prefix space.""" + if self._prefix_space_tokens is None: + vocab = self.model.convert_ids_to_tokens( + list(range(self.vocab_size)) + ) + self._prefix_space_tokens = { + i + for i, tok in enumerate(vocab) + if tok.startswith( + "▁" if isinstance(tok, str) else b" " + ) + } + return self._prefix_space_tokens + + def _maybe_add_prefix_space( + self, tokens: List[int], decoded: str + ): + """maybe add prefix space for incremental decoding.""" + if ( + len(tokens) + and not decoded.startswith(" ") + and tokens[0] in self.prefix_space_tokens + ): + return " " + decoded + else: + return decoded + + @property + def maybe_decode_bytes(self): + """Check if self.model.convert_ids_to_tokens return not a str value.""" + if self._maybe_decode_bytes is None: + self._maybe_decode_bytes = False + vocab = self.model.convert_ids_to_tokens( + list(range(self.vocab_size)) + ) + for tok in vocab: + if not isinstance(tok, str): + self._maybe_decode_bytes = True + break + return self._maybe_decode_bytes + + def indexes_containing_token(self, token: str): + """Return all the possible indexes, whose decoding output may contain + the input token.""" + # traversing vocab is time consuming, can not be accelerated with + # multi threads (computation) or multi process (can't pickle tokenizer) + # so, we maintain latest 10 stop words and return directly if matched + for _token, _indexes in self._indexes_tokens_deque: + if token == _token: + return _indexes + + if self.token2id == {}: + # decode is slower than convert_ids_to_tokens + if self.maybe_decode_bytes: + self.token2id = { + self.model.decode(i): i + for i in range(self.vocab_size) + } + else: + self.token2id = { + self.model.convert_ids_to_tokens(i): i + for i in range(self.vocab_size) + } + if token == " ": # ' ' is special + token = "▁" + indexes = [ + i + for _token, i in self.token2id.items() + if token in _token + ] + if len(indexes) > self.max_indexes_num: + indexes = self.encode(token, add_bos=False)[-1:] + self.logger.warning( + f"There are too many(>{self.max_indexes_num})" + f" possible indexes may decoding {token}, we will use" + f" {indexes} only" + ) + self._indexes_tokens_deque.append((token, indexes)) + return indexes + + def encode(self, s: str, add_bos: bool = True, **kwargs): + """Tokenize a prompt. + + Args: + s (str): a prompt + Returns: + list[int]: token ids + """ + encoded = self.model.encode(s, **kwargs) + if not add_bos: + # in the middle of a session + if len(encoded) and encoded[0] == self.bos_token_id: + encoded = encoded[1:] + return encoded + + def decode(self, t: Sequence[int], offset: Optional[int] = None): + """De-tokenize. + + Args: + t (List[int]): a list of token ids + offset (int): for incrementally decoding. Default to None, which + means not applied. + Returns: + str: text of decoding tokens + """ + skip_special_tokens = True + t = t[offset:] + out_string = self.model.decode( + t, skip_special_tokens=skip_special_tokens + ) + if offset: + out_string = self._maybe_add_prefix_space(t, out_string) + return out_string + + def __call__(self, s: Union[str, Sequence[str]]): + """Tokenize prompts. + + Args: + s (str): prompts + Returns: + list[int]: token ids + """ + add_special_tokens = False + return self.model(s, add_special_tokens=add_special_tokens) + + +class Tokenizer: + """Tokenize prompts or de-tokenize tokens into texts. + + Args: + model_file (str): the path of the tokenizer model + """ + + def __init__(self, model_file: str): + if model_file.endswith(".model"): + model_folder = osp.split(model_file)[0] + else: + model_folder = model_file + model_file = osp.join(model_folder, "tokenizer.model") + tokenizer_config_file = osp.join( + model_folder, "tokenizer_config.json" + ) + + model_file_exists = osp.exists(model_file) + config_exists = osp.exists(tokenizer_config_file) + use_hf_model = config_exists or not model_file_exists + self.logger = get_logger("lmdeploy") + if not use_hf_model: + self.model = SentencePieceTokenizer(model_file) + else: + self.model = HuggingFaceTokenizer(model_folder) + + @property + def vocab_size(self): + """vocabulary size.""" + return self.model.vocab_size + + @property + def bos_token_id(self): + """begine of the sentence token id.""" + return self.model.bos_token_id + + @property + def eos_token_id(self): + """end of the sentence token id.""" + return self.model.eos_token_id + + def encode(self, s: str, add_bos: bool = True, **kwargs): + """Tokenize a prompt. + + Args: + s (str): a prompt + Returns: + list[int]: token ids + """ + return self.model.encode(s, add_bos, **kwargs) + + def decode(self, t: Sequence[int], offset: Optional[int] = None): + """De-tokenize. + + Args: + t (List[int]): a list of token ids + offset (int): for incrementally decoding. Default to None, which + means not applied. + Returns: + str: text of decoding tokens + """ + return self.model.decode(t, offset) + + def __call__(self, s: Union[str, Sequence[str]]): + """Tokenize prompts. + + Args: + s (str): prompts + Returns: + list[int]: token ids + """ + return self.model(s) + + def indexes_containing_token(self, token): + """Return all the possible indexes, whose decoding output may contain + the input token.""" + encoded = self.encode(token, add_bos=False) + if len(encoded) > 1: + self.logger.warning( + f"The token {token}, its length of indexes" + f" {encoded} is over than 1. Currently, it can not be" + " used as stop words" + ) + return [] + return self.model.indexes_containing_token(token) diff --git a/swarms/structs/task.py b/swarms/structs/task.py index 699b7313..f2bf1bfc 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -109,11 +109,11 @@ class Task: except Exception as error: logger.error(f"[ERROR][Task] {error}") - def run(self): - self.execute() + def run(self, task: str, *args, **kwargs): + self.execute(task, *args, **kwargs) - def __call__(self): - self.execute() + def __call__(self, task: str, *args, **kwargs): + self.execute(task, *args, **kwargs) def handle_scheduled_task(self): """ @@ -206,3 +206,5 @@ class Task: logger.error( f"[ERROR][Task][check_dependency_completion] {error}" ) + + diff --git a/swarms/utils/get_logger.py b/swarms/utils/get_logger.py new file mode 100644 index 00000000..54fc8056 --- /dev/null +++ b/swarms/utils/get_logger.py @@ -0,0 +1,130 @@ +import logging +from typing import List, Optional + +logger_initialized = {} + + +def get_logger( + name: str, + log_file: Optional[str] = None, + log_level: int = logging.INFO, + file_mode: str = "w", +): + """Initialize and get a logger by name. + + If the logger has not been initialized, this method will initialize the + logger by adding one or two handlers, otherwise the initialized logger will + be directly returned. During initialization, a StreamHandler will always be + added. If `log_file` is specified, a FileHandler will also be added. + Args: + name (str): Logger name. + log_file (str | None): The log filename. If specified, a FileHandler + will be added to the logger. + log_level (int): The logger level. + file_mode (str): The file mode used in opening log file. + Defaults to 'w'. + Returns: + logging.Logger: The expected logger. + """ + # use logger in mmengine if exists. + try: + from mmengine.logging import MMLogger + + if MMLogger.check_instance_created(name): + logger = MMLogger.get_instance(name) + else: + logger = MMLogger.get_instance( + name, + logger_name=name, + log_file=log_file, + log_level=log_level, + file_mode=file_mode, + ) + return logger + + except Exception: + pass + + logger = logging.getLogger(name) + if name in logger_initialized: + return logger + # handle hierarchical names + # e.g., logger "a" is initialized, then logger "a.b" will skip the + # initialization since it is a child of "a". + for logger_name in logger_initialized: + if name.startswith(logger_name): + return logger + + # handle duplicate logs to the console + for handler in logger.root.handlers: + if type(handler) is logging.StreamHandler: + handler.setLevel(logging.ERROR) + + stream_handler = logging.StreamHandler() + handlers = [stream_handler] + + if log_file is not None: + # Here, the default behaviour of the official logger is 'a'. Thus, we + # provide an interface to change the file mode to the default + # behaviour. + file_handler = logging.FileHandler(log_file, file_mode) + handlers.append(file_handler) + + formatter = logging.Formatter( + "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + ) + for handler in handlers: + handler.setFormatter(formatter) + handler.setLevel(log_level) + logger.addHandler(handler) + + logger.setLevel(log_level) + logger_initialized[name] = True + + return logger + + +def filter_suffix( + response: str, suffixes: Optional[List[str]] = None +) -> str: + """Filter response with suffixes. + + Args: + response (str): generated response by LLMs. + suffixes (str): a list of suffixes to be deleted. + + Return: + str: a clean response. + """ + if suffixes is None: + return response + for item in suffixes: + if response.endswith(item): + response = response[: len(response) - len(item)] + return response + + +# TODO remove stop_word_offsets stuff and make it clean +def _stop_words(stop_words: List[str], tokenizer: object): + """return list of stop-words to numpy.ndarray.""" + import numpy as np + + if stop_words is None: + return None + assert isinstance(stop_words, List) and all( + isinstance(elem, str) for elem in stop_words + ), f"stop_words must be a list but got {type(stop_words)}" + stop_indexes = [] + for stop_word in stop_words: + stop_indexes += tokenizer.indexes_containing_token(stop_word) + assert isinstance(stop_indexes, List) and all( + isinstance(elem, int) for elem in stop_indexes + ), "invalid stop_words" + # each id in stop_indexes represents a stop word + # refer to https://github.com/fauxpilot/fauxpilot/discussions/165 for + # detailed explanation about fastertransformer's stop_indexes + stop_word_offsets = range(1, len(stop_indexes) + 1) + stop_words = np.array([[stop_indexes, stop_word_offsets]]).astype( + np.int32 + ) + return stop_words diff --git a/tests/models/test_timm.py b/tests/models/test_timm.py new file mode 100644 index 00000000..fae5f704 --- /dev/null +++ b/tests/models/test_timm.py @@ -0,0 +1,43 @@ +from unittest.mock import patch +from swarms.models import TimmModel +import torch + + +def test_timm_model_init(): + with patch("swarms.models.timm.list_models") as mock_list_models: + model_name = "resnet18" + pretrained = True + in_chans = 3 + timm_model = TimmModel(model_name, pretrained, in_chans) + mock_list_models.assert_called_once() + assert timm_model.model_name == model_name + assert timm_model.pretrained == pretrained + assert timm_model.in_chans == in_chans + assert timm_model.models == mock_list_models.return_value + + +def test_timm_model_call(): + with patch( + "swarms.models.timm.create_model" + ) as mock_create_model: + model_name = "resnet18" + pretrained = True + in_chans = 3 + timm_model = TimmModel(model_name, pretrained, in_chans) + task = torch.rand(1, in_chans, 224, 224) + result = timm_model(task) + mock_create_model.assert_called_once_with( + model_name, pretrained=pretrained, in_chans=in_chans + ) + assert result == mock_create_model.return_value(task) + + +def test_timm_model_list_models(): + with patch("swarms.models.timm.list_models") as mock_list_models: + model_name = "resnet18" + pretrained = True + in_chans = 3 + timm_model = TimmModel(model_name, pretrained, in_chans) + result = timm_model.list_models() + mock_list_models.assert_called_once() + assert result == mock_list_models.return_value diff --git a/tests/models/test_ultralytics.py b/tests/models/test_ultralytics.py new file mode 100644 index 00000000..8a1fed00 --- /dev/null +++ b/tests/models/test_ultralytics.py @@ -0,0 +1,35 @@ +from unittest.mock import patch +from swarms.models.ultralytics_model import Ultralytics + + +def test_ultralytics_init(): + with patch("swarms.models.YOLO") as mock_yolo: + model_name = "yolov5s" + ultralytics = Ultralytics(model_name) + mock_yolo.assert_called_once_with(model_name) + assert ultralytics.model_name == model_name + assert ultralytics.model == mock_yolo.return_value + + +def test_ultralytics_call(): + with patch("swarms.models.YOLO") as mock_yolo: + model_name = "yolov5s" + ultralytics = Ultralytics(model_name) + task = "detect" + args = (1, 2, 3) + kwargs = {"a": "A", "b": "B"} + result = ultralytics(task, *args, **kwargs) + mock_yolo.return_value.assert_called_once_with( + task, *args, **kwargs + ) + assert result == mock_yolo.return_value.return_value + + + +def test_ultralytics_list_models(): + with patch("swarms.models.YOLO") as mock_yolo: + model_name = "yolov5s" + ultralytics = Ultralytics(model_name) + result = ultralytics.list_models() + mock_yolo.list_models.assert_called_once() + assert result == mock_yolo.list_models.return_value \ No newline at end of file From 77ecece6e79857ba2ae54969390b6f2391205052 Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 20 Jan 2024 00:26:47 -0500 Subject: [PATCH 024/103] [FEATS][Tokenizers] [TimmModel] [Odin] [UltralyticsModel] --- swarms/models/ultralytics_model.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/swarms/models/ultralytics_model.py b/swarms/models/ultralytics_model.py index 2df548f4..2ce619d2 100644 --- a/swarms/models/ultralytics_model.py +++ b/swarms/models/ultralytics_model.py @@ -3,15 +3,15 @@ from ultralytics import YOLO class UltralyticsModel(BaseMultiModalModel): - def __init__(self, model_name: str, *args, **kwargs): - """ - Initializes an instance of the Ultralytics model. + """ + Initializes an instance of the Ultralytics model. - Args: - model_name (str): The name of the model. - *args: Variable length argument list. - **kwargs: Arbitrary keyword arguments. - """ + Args: + model_name (str): The name of the model. + *args: Variable length argument list. + **kwargs: Arbitrary keyword arguments. + """ + def __init__(self, model_name: str, *args, **kwargs): super().__init__(*args, **kwargs) self.model_name = model_name From 090a47fa686793f432e4cc9aeabe4eefcc73e7de Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 20 Jan 2024 11:04:06 -0500 Subject: [PATCH 025/103] [FEATS][Worker] [Step] [Plan] [OmniModalAgent] --- playground/agents/worker_example.py | 22 +++ playground/models/tts_speech.py | 2 +- swarms/agents/__init__.py | 2 + swarms/agents/worker_agent.py | 199 ++++++++++++++++++++++++++++ swarms/models/odin.py | 1 + swarms/models/ultralytics_model.py | 1 + swarms/prompts/worker_prompt.py | 60 +++++++++ swarms/structs/__init__.py | 4 + swarms/structs/plan.py | 32 +++++ swarms/structs/step.py | 24 ++++ swarms/structs/task.py | 2 - tests/models/test_ultralytics.py | 3 +- 12 files changed, 347 insertions(+), 5 deletions(-) create mode 100644 playground/agents/worker_example.py create mode 100644 swarms/agents/worker_agent.py create mode 100644 swarms/prompts/worker_prompt.py create mode 100644 swarms/structs/plan.py create mode 100644 swarms/structs/step.py diff --git a/playground/agents/worker_example.py b/playground/agents/worker_example.py new file mode 100644 index 00000000..65207cdf --- /dev/null +++ b/playground/agents/worker_example.py @@ -0,0 +1,22 @@ +import os +from dotenv import load_dotenv +from swarms.agents.worker_agent import Worker +from swarms import OpenAIChat + +load_dotenv() + +api_key = os.getenv("OPENAI_API_KEY") + +worker = Worker( + name="My Worker", + role="Worker", + human_in_the_loop=False, + tools = [], + temperature=0.5, + llm=OpenAIChat(openai_api_key=api_key), +) + +out = worker.run( + "Hello, how are you? Create an image of how your are doing!" +) +print(out) diff --git a/playground/models/tts_speech.py b/playground/models/tts_speech.py index ca9d14f7..be38912c 100644 --- a/playground/models/tts_speech.py +++ b/playground/models/tts_speech.py @@ -7,7 +7,7 @@ load_dotenv() tts = OpenAITTS( model_name="tts-1-1106", voice="onyx", - openai_api_key=os.getenv("OPENAI_API_KEY") + openai_api_key=os.getenv("OPENAI_API_KEY"), ) out = tts.run_and_save("Dammmmmm those tacos were good") diff --git a/swarms/agents/__init__.py b/swarms/agents/__init__.py index b2cb8043..876a4d27 100644 --- a/swarms/agents/__init__.py +++ b/swarms/agents/__init__.py @@ -2,10 +2,12 @@ from swarms.agents.message import Message from swarms.agents.base import AbstractAgent from swarms.agents.tool_agent import ToolAgent from swarms.agents.simple_agent import SimpleAgent +from swarms.agents.omni_modal_agent import OmniModalAgent __all__ = [ "Message", "AbstractAgent", "ToolAgent", "SimpleAgent", + "OmniModalAgent", ] diff --git a/swarms/agents/worker_agent.py b/swarms/agents/worker_agent.py new file mode 100644 index 00000000..8ed7d0d3 --- /dev/null +++ b/swarms/agents/worker_agent.py @@ -0,0 +1,199 @@ +import os +from typing import Any, List + +import faiss +from langchain.docstore import InMemoryDocstore +from langchain.embeddings import OpenAIEmbeddings +from langchain.vectorstores import FAISS +from langchain_experimental.autonomous_agents import AutoGPT + +from swarms.utils.decorators import error_decorator, timing_decorator + + +class Worker: + """ + The Worker class represents an autonomous agent that can perform tassks through + function calls or by running a chat. + + Args: + name (str, optional): Name of the agent. Defaults to "Autobot Swarm Worker". + role (str, optional): Role of the agent. Defaults to "Worker in a swarm". + external_tools (list, optional): List of external tools. Defaults to None. + human_in_the_loop (bool, optional): Whether to include human in the loop. Defaults to False. + temperature (float, optional): Temperature for the agent. Defaults to 0.5. + llm ([type], optional): Language model. Defaults to None. + openai_api_key (str, optional): OpenAI API key. Defaults to None. + + Raises: + RuntimeError: If there is an error while setting up the agent. + + Example: + >>> worker = Worker( + ... name="My Worker", + ... role="Worker", + ... external_tools=[MyTool1(), MyTool2()], + ... human_in_the_loop=False, + ... temperature=0.5, + ... ) + >>> worker.run("What's the weather in Miami?") + + """ + + def __init__( + self, + name: str = "WorkerAgent", + role: str = "Worker in a swarm", + external_tools=None, + human_in_the_loop: bool = False, + temperature: float = 0.5, + llm=None, + openai_api_key: str = None, + tools: List[Any] = None, + embedding_size: int = 1536, + search_kwargs: dict = {"k": 8}, + *args, + **kwargs, + ): + self.name = name + self.role = role + self.external_tools = external_tools + self.human_in_the_loop = human_in_the_loop + self.temperature = temperature + self.llm = llm + self.openai_api_key = openai_api_key + self.tools = tools + self.embedding_size = embedding_size + self.search_kwargs = search_kwargs + + self.setup_tools(external_tools) + self.setup_memory() + self.setup_agent() + + def reset(self): + """ + Reset the message history. + """ + self.message_history = [] + + def receieve(self, name: str, message: str) -> None: + """ + Receive a message and update the message history. + + Parameters: + - `name` (str): The name of the sender. + - `message` (str): The received message. + """ + self.message_history.append(f"{name}: {message}") + + def send(self) -> str: + """Send message history.""" + self.agent.run(task=self.message_history) + + def setup_tools(self, external_tools): + """ + Set up tools for the worker. + + Parameters: + - `external_tools` (list): List of external tools (optional). + + Example: + ``` + external_tools = [MyTool1(), MyTool2()] + worker = Worker(model_name="gpt-4", + openai_api_key="my_key", + name="My Worker", + role="Worker", + external_tools=external_tools, + human_in_the_loop=False, + temperature=0.5) + ``` + """ + if self.tools is None: + self.tools = [] + + if external_tools is not None: + self.tools.extend(external_tools) + + def setup_memory(self): + """ + Set up memory for the worker. + """ + openai_api_key = ( + os.getenv("OPENAI_API_KEY") or self.openai_api_key + ) + try: + embeddings_model = OpenAIEmbeddings( + openai_api_key=openai_api_key + ) + embedding_size = self.embedding_size + index = faiss.IndexFlatL2(embedding_size) + + self.vectorstore = FAISS( + embeddings_model.embed_query, + index, + InMemoryDocstore({}), + {}, + ) + + except Exception as error: + raise RuntimeError( + "Error setting up memory perhaps try try tuning the" + f" embedding size: {error}" + ) + + def setup_agent(self): + """ + Set up the autonomous agent. + """ + try: + self.agent = AutoGPT.from_llm_and_tools( + ai_name=self.name, + ai_role=self.role, + tools=self.tools, + llm=self.llm, + memory=self.vectorstore.as_retriever( + search_kwargs=self.search_kwargs + ), + human_in_the_loop=self.human_in_the_loop, + ) + + except Exception as error: + raise RuntimeError(f"Error setting up agent: {error}") + + # @log_decorator + @error_decorator + @timing_decorator + def run(self, task: str = None, *args, **kwargs): + """ + Run the autonomous agent on a given task. + + Parameters: + - `task`: The task to be processed. + + Returns: + - `result`: The result of the agent's processing. + """ + try: + result = self.agent.run([task], *args, **kwargs) + return result + except Exception as error: + raise RuntimeError(f"Error while running agent: {error}") + + # @log_decorator + @error_decorator + @timing_decorator + def __call__(self, task: str = None, *args, **kwargs): + """ + Make the worker callable to run the agent on a given task. + + Parameters: + - `task`: The task to be processed. + + Returns: + - `results`: The results of the agent's processing. + """ + try: + results = self.run(task, *args, **kwargs) + return results + except Exception as error: + raise RuntimeError(f"Error while running agent: {error}") diff --git a/swarms/models/odin.py b/swarms/models/odin.py index 74e0c556..1ab09893 100644 --- a/swarms/models/odin.py +++ b/swarms/models/odin.py @@ -3,6 +3,7 @@ from ultraanalytics import YOLO from tqdm import tqdm from swarms.models.base_llm import AbstractLLM + class Odin(AbstractLLM): """ Odin class represents an object detection and tracking model. diff --git a/swarms/models/ultralytics_model.py b/swarms/models/ultralytics_model.py index 2ce619d2..b36d24fa 100644 --- a/swarms/models/ultralytics_model.py +++ b/swarms/models/ultralytics_model.py @@ -11,6 +11,7 @@ class UltralyticsModel(BaseMultiModalModel): *args: Variable length argument list. **kwargs: Arbitrary keyword arguments. """ + def __init__(self, model_name: str, *args, **kwargs): super().__init__(*args, **kwargs) self.model_name = model_name diff --git a/swarms/prompts/worker_prompt.py b/swarms/prompts/worker_prompt.py new file mode 100644 index 00000000..03b8ce06 --- /dev/null +++ b/swarms/prompts/worker_prompt.py @@ -0,0 +1,60 @@ +def worker_agent_system(name: str, memory: str = None): + return """ + You are {name}, + Your decisions must always be made independently without seeking user assistance. + Play to your strengths as an LLM and pursue simple strategies with no legal complications. + If you have completed all your tasks, make sure to use the "finish" command. + + GOALS: + + 1. Hello, how are you? Create an image of how you are doing! + + Constraints: + + 1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files. + 2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember. + 3. No user assistance + 4. Exclusively use the commands listed in double quotes e.g. "command name" + + Commands: + + 1. finish: use this to signal that you have finished all your objectives, args: "response": "final response to let people know you have finished your objectives" + + Resources: + + 1. Internet access for searches and information gathering. + 2. Long Term memory management. + 3. GPT-3.5 powered Agents for delegation of simple tasks. + 4. File output. + + Performance Evaluation: + + 1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities. + 2. Constructively self-criticize your big-picture behavior constantly. + 3. Reflect on past decisions and strategies to refine your approach. + 4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps. + + You should only respond in JSON format as described below + Response Format: + { + "thoughts": { + "text": "thought", + "reasoning": "reasoning", + "plan": "- short bulleted\n- list that conveys\n- long-term plan", + "criticism": "constructive self-criticism", + "speak": "thoughts summary to say to user" + }, + "command": { + "name": "command name", + "args": { + "arg name": "value" + } + } + } + Ensure the response can be parsed by Python json.loads + System: The current time and date is Sat Jan 20 10:39:07 2024 + System: This reminds you of these events from your past: + [{memory}] + + Human: Determine which next command to use, and respond using the format specified above: + """.format(name=name, memory=memory) \ No newline at end of file diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py index 706690c1..e7ba49ca 100644 --- a/swarms/structs/__init__.py +++ b/swarms/structs/__init__.py @@ -30,6 +30,8 @@ from swarms.structs.utils import ( from swarms.structs.task import Task from swarms.structs.block_wrapper import block from swarms.structs.graph_workflow import GraphWorkflow +from swarms.structs.step import Step +from swarms.structs.plan import Plan __all__ = [ @@ -62,4 +64,6 @@ __all__ = [ "Task", "block", "GraphWorkflow", + "Step", + "Plan", ] diff --git a/swarms/structs/plan.py b/swarms/structs/plan.py new file mode 100644 index 00000000..4b5db022 --- /dev/null +++ b/swarms/structs/plan.py @@ -0,0 +1,32 @@ +from typing import List + +from swarms.structs.step import Step + + +class Plan: + def __init__(self, steps: List[Step]): + """ + Initializes a Plan object. + + Args: + steps (List[Step]): A list of Step objects representing the steps in the plan. + """ + self.steps = steps + + def __str__(self) -> str: + """ + Returns a string representation of the Plan object. + + Returns: + str: A string representation of the Plan object. + """ + return str([str(step) for step in self.steps]) + + def __repr(self) -> str: + """ + Returns a string representation of the Plan object. + + Returns: + str: A string representation of the Plan object. + """ + return str(self) diff --git a/swarms/structs/step.py b/swarms/structs/step.py new file mode 100644 index 00000000..7e66250a --- /dev/null +++ b/swarms/structs/step.py @@ -0,0 +1,24 @@ +from dataclasses import dataclass +from typing import Dict, List + +from swarms.tools.tool import BaseTool + + +@dataclass +class Step: + """ + Represents a step in a process. + + Attributes: + task (str): The task associated with the step. + id (int): The unique identifier of the step. + dep (List[int]): The list of step IDs that this step depends on. + args (Dict[str, str]): The arguments associated with the step. + tool (BaseTool): The tool used to execute the step. + """ + + task: str + id: int + dep: List[int] + args: Dict[str, str] + tool: BaseTool diff --git a/swarms/structs/task.py b/swarms/structs/task.py index f2bf1bfc..68a30951 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -206,5 +206,3 @@ class Task: logger.error( f"[ERROR][Task][check_dependency_completion] {error}" ) - - diff --git a/tests/models/test_ultralytics.py b/tests/models/test_ultralytics.py index 8a1fed00..e90a49db 100644 --- a/tests/models/test_ultralytics.py +++ b/tests/models/test_ultralytics.py @@ -25,11 +25,10 @@ def test_ultralytics_call(): assert result == mock_yolo.return_value.return_value - def test_ultralytics_list_models(): with patch("swarms.models.YOLO") as mock_yolo: model_name = "yolov5s" ultralytics = Ultralytics(model_name) result = ultralytics.list_models() mock_yolo.list_models.assert_called_once() - assert result == mock_yolo.list_models.return_value \ No newline at end of file + assert result == mock_yolo.list_models.return_value From ef0a9d051eaf17777b6b81c10d66852919afea06 Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 20 Jan 2024 12:51:17 -0500 Subject: [PATCH 026/103] [README] --- README.md | 37 +++++++++++++++++++------------ pyproject.toml | 2 +- swarms/agents/omni_modal_agent.py | 29 ------------------------ 3 files changed, 24 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 497bc9e1..d12dd880 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,13 @@ Run example in Collab: is located inside the text! ```python import os @@ -235,7 +244,7 @@ workflow.run() ### `ModelParallelizer` -- Concurrent Execution of Multiple Models: The ModelParallelizer allows you to run multiple models concurrently, comparing their outputs. This feature enables you to easily compare the performance and results of different models, helping you make informed decisions about which model to use for your specific task. +Concurrent Execution of Multiple Models: The ModelParallelizer allows you to run multiple models concurrently, comparing their outputs. This feature enables you to easily compare the performance and results of different models, helping you make informed decisions about which model to use for your specific task. - Plug-and-Play Integration: The structure provides a seamless integration with various models, including OpenAIChat, Anthropic, Mixtral, and Gemini. You can easily plug in any of these models and start using them without the need for extensive modifications or setup. diff --git a/pyproject.toml b/pyproject.toml index 79ac36fc..22f09946 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.7.3" +version = "3.7.5" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/agents/omni_modal_agent.py b/swarms/agents/omni_modal_agent.py index 6a22c477..7a75cd8b 100644 --- a/swarms/agents/omni_modal_agent.py +++ b/swarms/agents/omni_modal_agent.py @@ -1,7 +1,5 @@ -from typing import Dict, List from langchain.base_language import BaseLanguageModel -from langchain.tools.base import BaseTool from langchain_experimental.autonomous_agents.hugginggpt.repsonse_generator import ( load_response_generator, ) @@ -16,33 +14,6 @@ from transformers import load_tool from swarms.agents.message import Message -class Step: - def __init__( - self, - task: str, - id: int, - dep: List[int], - args: Dict[str, str], - tool: BaseTool, - ): - self.task = task - self.id = id - self.dep = dep - self.args = args - self.tool = tool - - -class Plan: - def __init__(self, steps: List[Step]): - self.steps = steps - - def __str__(self) -> str: - return str([str(step) for step in self.steps]) - - def __repr(self) -> str: - return str(self) - - class OmniModalAgent: """ OmniModalAgent From 13a2638762878f285867991e00cfde4cf22b051c Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 20 Jan 2024 12:51:59 -0500 Subject: [PATCH 027/103] [README] --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d12dd880..0aec35cf 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,10 @@ Agent is Swarms plug in and play agent structure! By passing in an LLM you can c Here are some features: -✅ Any LLM +✅ Any LLM/ Any framework + ✅ Extremely customize-able with max loops, autosaving, import docs (PDFS, TXT, CSVs, etc) + ✅ Long term memory database with RAG, also plug in and play with ```python From a7e3525cb97f05cb1095015a606be7867196a3df Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 20 Jan 2024 12:53:41 -0500 Subject: [PATCH 028/103] [README] --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0aec35cf..65273700 100644 --- a/README.md +++ b/README.md @@ -28,15 +28,15 @@ Run example in Collab: Date: Sat, 20 Jan 2024 15:03:34 -0500 Subject: [PATCH 031/103] [README] --- README.md | 26 ++-- playground/agents/worker_example.py | 2 +- swarms/agents/omni_modal_agent.py | 1 - swarms/prompts/__init__.py | 2 + swarms/prompts/schema_generator.py | 214 ++++++++++++++++++++++++++++ swarms/prompts/worker_prompt.py | 2 +- swarms/tools/__init__.py | 13 +- swarms/tools/exec_tool.py | 125 ++++++++++++++++ 8 files changed, 365 insertions(+), 20 deletions(-) create mode 100644 swarms/prompts/schema_generator.py create mode 100644 swarms/tools/exec_tool.py diff --git a/README.md b/README.md index 4f0cd695..49ca3770 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ Run example in Collab: >> schema_generator = SchemaGenerator() + >>> schema_generator.add_constraint("No user assistance") + >>> schema_generator.add_resource("Internet access for searches and information gathering.") + >>> schema_generator.add_performance_evaluation("Continuously review and analyze your actions to ensure you are performing to the best of your abilities.") + >>> prompt_string = schema_generator.generate_prompt_string() + >>> print(prompt_string) + """ + + def __init__(self) -> None: + """Initialize the SchemaGenerator object. + + Starts with empty lists of constraints, commands, resources, + and performance evaluations. + """ + self.constraints: List[str] = [] + self.commands: List[BaseTool] = [] + self.resources: List[str] = [] + self.performance_evaluation: List[str] = [] + self.response_format = { + "thoughts": { + "text": "thought", + "reasoning": "reasoning", + "plan": ( + "- short bulleted\n- list that conveys\n-" + " long-term plan" + ), + "criticism": "constructive self-criticism", + "speak": "thoughts summary to say to user", + }, + "command": { + "name": "command name", + "args": {"arg name": "value"}, + }, + } + + def add_constraint(self, constraint: str) -> None: + """ + Add a constraint to the constraints list. + + Args: + constraint (str): The constraint to be added. + """ + self.constraints.append(constraint) + + def add_tool(self, tool: BaseTool) -> None: + self.commands.append(tool) + + def _generate_command_string(self, tool: BaseTool) -> str: + output = f"{tool.name}: {tool.description}" + output += f", args json schema: {json.dumps(tool.args)}" + return output + + def add_resource(self, resource: str) -> None: + """ + Add a resource to the resources list. + + Args: + resource (str): The resource to be added. + """ + self.resources.append(resource) + + def add_performance_evaluation(self, evaluation: str) -> None: + """ + Add a performance evaluation item to the performance_evaluation list. + + Args: + evaluation (str): The evaluation item to be added. + """ + self.performance_evaluation.append(evaluation) + + def _generate_numbered_list( + self, items: list, item_type: str = "list" + ) -> str: + """ + Generate a numbered list from given items based on the item_type. + + Args: + items (list): A list of items to be numbered. + item_type (str, optional): The type of items in the list. + Defaults to 'list'. + + Returns: + str: The formatted numbered list. + """ + if item_type == "command": + command_strings = [ + f"{i + 1}. {self._generate_command_string(item)}" + for i, item in enumerate(items) + ] + finish_description = ( + "use this to signal that you have finished all your" + " objectives" + ) + finish_args = ( + '"response": "final response to let ' + 'people know you have finished your objectives"' + ) + finish_string = ( + f"{len(items) + 1}. {FINISH_NAME}: " + f"{finish_description}, args: {finish_args}" + ) + return "\n".join(command_strings + [finish_string]) + else: + return "\n".join( + f"{i+1}. {item}" for i, item in enumerate(items) + ) + + def generate_prompt_string(self) -> str: + """Generate a prompt string. + + Returns: + str: The generated prompt string. + """ + formatted_response_format = json.dumps( + self.response_format, indent=4 + ) + prompt_string = ( + f"Constraints:\n{self._generate_numbered_list(self.constraints)}\n\nCommands:\n{self._generate_numbered_list(self.commands, item_type='command')}\n\nResources:\n{self._generate_numbered_list(self.resources)}\n\nPerformance" + f" Evaluation:\n{self._generate_numbered_list(self.performance_evaluation)}\n\nYou" + " should only respond in JSON format as described below" + " \nResponse Format:" + f" \n{formatted_response_format} \nEnsure the response" + " can be parsed by Python json.loads" + ) + + return prompt_string + + +def get_prompt(tools: List[BaseTool]) -> str: + """Generates a prompt string. + + It includes various constraints, commands, resources, and performance evaluations. + + Returns: + str: The generated prompt string. + """ + + # Initialize the SchemaGenerator object + schema_generator = SchemaGenerator() + + # Add constraints to the SchemaGenerator object + schema_generator.add_constraint( + "~4000 word limit for short term memory. " + "Your short term memory is short, " + "so immediately save important information to files." + ) + schema_generator.add_constraint( + "If you are unsure how you previously did something " + "or want to recall past events, " + "thinking about similar events will help you remember." + ) + schema_generator.add_constraint("No user assistance") + schema_generator.add_constraint( + "Exclusively use the commands listed in double quotes e.g." + ' "command name"' + ) + + # Add commands to the SchemaGenerator object + for tool in tools: + schema_generator.add_tool(tool) + + # Add resources to the SchemaGenerator object + schema_generator.add_resource( + "Internet access for searches and information gathering." + ) + schema_generator.add_resource("Long Term memory management.") + schema_generator.add_resource( + "GPT-3.5 powered Agents for delegation of simple tasks." + ) + schema_generator.add_resource("File output.") + + # Add performance evaluations to the SchemaGenerator object + schema_generator.add_performance_evaluation( + "Continuously review and analyze your actions " + "to ensure you are performing to the best of your abilities." + ) + schema_generator.add_performance_evaluation( + "Constructively self-criticize your big-picture behavior" + " constantly." + ) + schema_generator.add_performance_evaluation( + "Reflect on past decisions and strategies to refine your" + " approach." + ) + schema_generator.add_performance_evaluation( + "Every command has a cost, so be smart and efficient. " + "Aim to complete tasks in the least number of steps." + ) + + # Generate the prompt string + prompt_string = schema_generator.generate_prompt_string() + + return prompt_string diff --git a/swarms/prompts/worker_prompt.py b/swarms/prompts/worker_prompt.py index 03b8ce06..5ba0b5e5 100644 --- a/swarms/prompts/worker_prompt.py +++ b/swarms/prompts/worker_prompt.py @@ -57,4 +57,4 @@ def worker_agent_system(name: str, memory: str = None): [{memory}] Human: Determine which next command to use, and respond using the format specified above: - """.format(name=name, memory=memory) \ No newline at end of file + """.format(name=name, memory=memory) diff --git a/swarms/tools/__init__.py b/swarms/tools/__init__.py index 877fb0de..c36c9608 100644 --- a/swarms/tools/__init__.py +++ b/swarms/tools/__init__.py @@ -7,7 +7,13 @@ from swarms.tools.tool_utils import ( execute_tools, ) from swarms.tools.tool import BaseTool, Tool, StructuredTool, tool - +from swarms.tools.exec_tool import ( + AgentAction, + BaseAgentOutputParser, + preprocess_json_input, + AgentOutputParser, + execute_tool_by_name, +) __all__ = [ "scrape_tool_func_docs", @@ -20,4 +26,9 @@ __all__ = [ "Tool", "StructuredTool", "tool", + "AgentAction", + "BaseAgentOutputParser", + "preprocess_json_input", + "AgentOutputParser", + "execute_tool_by_name", ] diff --git a/swarms/tools/exec_tool.py b/swarms/tools/exec_tool.py new file mode 100644 index 00000000..3ca02dea --- /dev/null +++ b/swarms/tools/exec_tool.py @@ -0,0 +1,125 @@ +import json +import re +from abc import abstractmethod +from typing import Dict, List, NamedTuple + +from langchain.schema import BaseOutputParser +from pydantic import ValidationError + +from swarms.tools.tool import BaseTool + + +class AgentAction(NamedTuple): + """Action returned by AgentOutputParser.""" + + name: str + args: Dict + + +class BaseAgentOutputParser(BaseOutputParser): + """Base Output parser for Agent.""" + + @abstractmethod + def parse(self, text: str) -> AgentAction: + """Return AgentAction""" + + +def preprocess_json_input(input_str: str) -> str: + """Preprocesses a string to be parsed as json. + + Replace single backslashes with double backslashes, + while leaving already escaped ones intact. + + Args: + input_str: String to be preprocessed + + Returns: + Preprocessed string + """ + corrected_str = re.sub( + r'(? AgentAction: + try: + parsed = json.loads(text, strict=False) + except json.JSONDecodeError: + preprocessed_text = preprocess_json_input(text) + try: + parsed = json.loads(preprocessed_text, strict=False) + except Exception: + return AgentAction( + name="ERROR", + args={ + "error": ( + f"Could not parse invalid json: {text}" + ) + }, + ) + try: + return AgentAction( + name=parsed["command"]["name"], + args=parsed["command"]["args"], + ) + except (KeyError, TypeError): + # If the command is null or incomplete, return an erroneous tool + return AgentAction( + name="ERROR", + args={"error": f"Incomplete command args: {parsed}"}, + ) + + +def execute_tool_by_name( + text: str, + tools: List[BaseTool], + stop_token: str = "finish", +): + """ + Executes a tool based on the given text command. + + Args: + text (str): The text command to be executed. + tools (List[BaseTool]): A list of available tools. + stop_token (str, optional): The stop token to terminate the execution. Defaults to "finish". + + Returns: + str: The result of the command execution. + """ + output_parser = AgentOutputParser() + # Get command name and arguments + action = output_parser.parse(text) + tools = {t.name: t for t in tools} + if action.name == stop_token: + return action.args["response"] + if action.name in tools: + tool = tools[action.name] + try: + observation = tool.run(action.args) + except ValidationError as e: + observation = ( + f"Validation Error in args: {str(e)}, args:" + f" {action.args}" + ) + except Exception as e: + observation = ( + f"Error: {str(e)}, {type(e).__name__}, args:" + f" {action.args}" + ) + result = f"Command {tool.name} returned: {observation}" + elif action.name == "ERROR": + result = f"Error: {action.args}. " + else: + result = ( + f"Unknown command '{action.name}'. " + "Please refer to the 'COMMANDS' list for available " + "commands and only respond in the specified JSON format." + ) + + return result From 3df283b25404dd3833ae05004a58e4bd2a6ac0ae Mon Sep 17 00:00:00 2001 From: Eternal Reclaimer <98760976+kyegomez@users.noreply.github.com> Date: Sat, 20 Jan 2024 23:16:21 -0500 Subject: [PATCH 032/103] Update README.md --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 49ca3770..cf360de0 100644 --- a/README.md +++ b/README.md @@ -1037,6 +1037,9 @@ Help us accelerate our backlog by supporting us financially! Note, we're an open +## Swarm Newsletter + + # License Apache License From d342c6bb435bc97ad5d57b82e7c437b19d15e9cc Mon Sep 17 00:00:00 2001 From: Eternal Reclaimer <98760976+kyegomez@users.noreply.github.com> Date: Sat, 20 Jan 2024 23:18:06 -0500 Subject: [PATCH 033/103] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cf360de0..c0213dd4 100644 --- a/README.md +++ b/README.md @@ -1038,7 +1038,9 @@ Help us accelerate our backlog by supporting us financially! Note, we're an open ## Swarm Newsletter - +Sign up to the Swarm newsletter to receieve updates on the latest Autonomous agent research papers, step by step guides on creating multi-agent app, and much more Swarmie goodiness 😊 + +[CLICK HERE TO SIGNUP](https://docs.google.com/forms/d/e/1FAIpQLSfqxI2ktPR9jkcIwzvHL0VY6tEIuVPd-P2fOWKnd6skT9j1EQ/viewform?usp=sf_link) # License Apache License From 2976b73ede41bfb3f9e99076024cba42d47a3720 Mon Sep 17 00:00:00 2001 From: Eternal Reclaimer <98760976+kyegomez@users.noreply.github.com> Date: Sat, 20 Jan 2024 23:18:28 -0500 Subject: [PATCH 034/103] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c0213dd4..ccfac708 100644 --- a/README.md +++ b/README.md @@ -1037,7 +1037,7 @@ Help us accelerate our backlog by supporting us financially! Note, we're an open -## Swarm Newsletter +## Swarm Newsletter 🤖 🤖 🤖 📧 Sign up to the Swarm newsletter to receieve updates on the latest Autonomous agent research papers, step by step guides on creating multi-agent app, and much more Swarmie goodiness 😊 [CLICK HERE TO SIGNUP](https://docs.google.com/forms/d/e/1FAIpQLSfqxI2ktPR9jkcIwzvHL0VY6tEIuVPd-P2fOWKnd6skT9j1EQ/viewform?usp=sf_link) From 8383e8b0624f2d587b5bac0651f13562e225f872 Mon Sep 17 00:00:00 2001 From: evelynmitchell Date: Sun, 21 Jan 2024 12:37:15 -0700 Subject: [PATCH 035/103] Readme to 3.7.5 --- docs/ReleaseNotes.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md index 31f72e80..f8859fa0 100644 --- a/docs/ReleaseNotes.md +++ b/docs/ReleaseNotes.md @@ -1,5 +1,33 @@ # Release Notes +## 3.7.5 + +2024-01-21 + +### Bug Fixes + +Fix imports of Agent, SequentialWorkflow, ModelParallelizer, Task, OpenAIChat, Gemini, GPT4VisionAPI + +### New Features + +New model: Odin for Object Detection and tracking +New mode: Ultralytics Object recognition YOLO + +New Tokenizers + +Schema generator for prompts. +New prompt for worker agent. + +New structure: plan, step + +New tool: execute tool + +New logger: get_logger + +Example for worker_agent + + + ## 3.6.8 2024-01-19 From e06cdd5fbb50ae807cc045440c73208fa940ecfd Mon Sep 17 00:00:00 2001 From: Eternal Reclaimer <98760976+kyegomez@users.noreply.github.com> Date: Mon, 22 Jan 2024 14:05:12 -0500 Subject: [PATCH 036/103] Update mkdocs.yml --- mkdocs.yml | 71 +++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index b3adedb9..91e95270 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -96,41 +96,46 @@ nav: - Gemini: "swarms/models/gemini.md" - ZeroscopeTTV: "swarms/models/zeroscope.md" - swarms.structs: - - agent: "swarms/structs/agent.md" - - basestructure: "swarms/structs/basestructure.md" - - artifactupload: "swarms/structs/artifactupload.md" - - sequential_workflow: "swarms/structs/sequential_workflow.md" - - taskinput: "swarms/structs/taskinput.md" - - concurrentworkflow: "swarms/structs/concurrentworkflow.md" - - nonlinearworkflow: "swarms/structs/nonlinearworkflow.md" - - stepinput: "swarms/structs/stepinput.md" - - workflow: "swarms/structs/workflow.md" - - artifact: "swarms/structs/artifact.md" - - recursiveworkflow: "swarms/structs/recursiveworkflow.md" - - swarmnetwork: "swarms/structs/swarmnetwork.md" - - task: "swarms/structs/task.md" - - groupchatmanager: "swarms/structs/groupchatmanager.md" - - baseworkflow: "swarms/structs/baseworkflow.md" - - conversation: "swarms/structs/conversation.md" - - groupchat: "swarms/structs/groupchat.md" + - Foundational Structures: + - agent: "swarms/structs/agent.md" + - basestructure: "swarms/structs/basestructure.md" + - artifactupload: "swarms/structs/artifactupload.md" + - taskinput: "swarms/structs/taskinput.md" + - stepinput: "swarms/structs/stepinput.md" + - artifact: "swarms/structs/artifact.md" + - task: "swarms/structs/task.md" + - Workflows: + - recursiveworkflow: "swarms/structs/recursiveworkflow.md" + - concurrentworkflow: "swarms/structs/concurrentworkflow.md" + - nonlinearworkflow: "swarms/structs/nonlinearworkflow.md" + - sequential_workflow: "swarms/structs/sequential_workflow.md" + - workflow: "swarms/structs/workflow.md" + - baseworkflow: "swarms/structs/baseworkflow.md" + - Multi-Agent-Architectures: + - conversation: "swarms/structs/conversation.md" + - groupchat: "swarms/structs/groupchat.md" + - swarmnetwork: "swarms/structs/swarmnetwork.md" + - groupchatmanager: "swarms/structs/groupchatmanager.md" - swarms.memory: - - Weaviate: "swarms/memory/weaviate.md" - - PineconeDB: "swarms/memory/pinecone.md" - - PGVectorStore: "swarms/memory/pg.md" + - Vector Databases: + - Weaviate: "swarms/memory/weaviate.md" + - PineconeDB: "swarms/memory/pinecone.md" + - PGVectorStore: "swarms/memory/pg.md" - ShortTermMemory: "swarms/memory/short_term_memory.md" - swarms.utils: - - pdf_to_text: "swarms/utils/pdf_to_text.md" - - load_model_torch: "swarms/utils/load_model_torch.md" - - metrics_decorator: "swarms/utils/metrics_decorator.md" - - prep_torch_inference: "swarms/utils/prep_torch_inference.md" - - find_image_path: "swarms/utils/find_image_path.md" - - print_class_parameters: "swarms/utils/print_class_parameters.md" - - extract_code_from_markdown: "swarms/utils/extract_code_from_markdown.md" - - check_device: "swarms/utils/check_device.md" - - display_markdown_message: "swarms/utils/display_markdown_message.md" - - phoenix_tracer: "swarms/utils/phoenix_tracer.md" - - limit_tokens_from_string: "swarms/utils/limit_tokens_from_string.md" - - math_eval: "swarms/utils/math_eval.md" + - Misc: + - pdf_to_text: "swarms/utils/pdf_to_text.md" + - load_model_torch: "swarms/utils/load_model_torch.md" + - metrics_decorator: "swarms/utils/metrics_decorator.md" + - prep_torch_inference: "swarms/utils/prep_torch_inference.md" + - find_image_path: "swarms/utils/find_image_path.md" + - print_class_parameters: "swarms/utils/print_class_parameters.md" + - extract_code_from_markdown: "swarms/utils/extract_code_from_markdown.md" + - check_device: "swarms/utils/check_device.md" + - display_markdown_message: "swarms/utils/display_markdown_message.md" + - phoenix_tracer: "swarms/utils/phoenix_tracer.md" + - limit_tokens_from_string: "swarms/utils/limit_tokens_from_string.md" + - math_eval: "swarms/utils/math_eval.md" - Guides: - Overview: "examples/index.md" - Agents: @@ -160,4 +165,4 @@ nav: - Checklist: "corporate/checklist.md" - Hiring: "corporate/hiring.md" - SwarmCloud: "corporate/swarm_cloud.md" - - SwarmMemo: "corporate/swarm_memo.md" \ No newline at end of file + - SwarmMemo: "corporate/swarm_memo.md" From a4c4c3b9432798668f8c434dfe0fc2535e432284 Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 22 Jan 2024 14:21:32 -0500 Subject: [PATCH 037/103] [REFACTOR][SequentialWorkflow] [Agent] [FEATS][AsyncWorkflow] [BUFG][ConcurrentWorkflow] [MISC][DOCS] --- README.md | 19 +- docs/swarms/agents/abstract_agent.md | 90 ------ docs/swarms/agents/abstractagent.md | 124 ++++++++ docs/swarms/agents/message.md | 120 ++++++++ docs/swarms/agents/omnimodalagent.md | 79 +++++ docs/swarms/agents/toolagent.md | 113 +++++++ docs/swarms/agents/workeragent.md | 78 +++++ mkdocs.yml | 10 +- playground/structs/concurrent_workflow.py | 15 +- playground/structs/sequential_workflow.py | 39 ++- playground/structs/task.py | 14 +- scripts/auto_tests_docs/auto_docs.py | 43 +-- scripts/auto_tests_docs/mkdocs_handler.py | 2 +- swarms/agents/__init__.py | 30 +- swarms/agents/omni_modal_agent.py | 2 +- swarms/agents/stopping_conditions.py | 38 +++ swarms/agents/tool_agent.py | 4 - swarms/models/mistral.py | 2 +- swarms/structs/__init__.py | 2 + swarms/structs/agent.py | 296 +++--------------- swarms/structs/async_workflow.py | 103 +++++++ swarms/structs/concurrent_workflow.py | 58 ++-- swarms/{agents => structs}/message.py | 0 swarms/structs/recursive_workflow.py | 6 +- swarms/structs/sequential_workflow.py | 356 ++++------------------ swarms/structs/task.py | 71 ++++- swarms/tools/tool_utils.py | 86 +++++- swarms/utils/fetch_init_params.py | 33 ++ swarms/utils/file_extension_seach.py | 21 ++ 29 files changed, 1103 insertions(+), 751 deletions(-) delete mode 100644 docs/swarms/agents/abstract_agent.md create mode 100644 docs/swarms/agents/abstractagent.md create mode 100644 docs/swarms/agents/message.md create mode 100644 docs/swarms/agents/omnimodalagent.md create mode 100644 docs/swarms/agents/toolagent.md create mode 100644 docs/swarms/agents/workeragent.md create mode 100644 swarms/agents/stopping_conditions.py create mode 100644 swarms/structs/async_workflow.py rename swarms/{agents => structs}/message.py (100%) create mode 100644 swarms/utils/fetch_init_params.py create mode 100644 swarms/utils/file_extension_seach.py diff --git a/README.md b/README.md index ccfac708..54253876 100644 --- a/README.md +++ b/README.md @@ -180,8 +180,8 @@ for task in workflow.tasks: ```python -import os -from dotenv import load_dotenv +import os +from dotenv import load_dotenv from swarms import OpenAIChat, Task, ConcurrentWorkflow, Agent # Load environment variables from .env file @@ -200,9 +200,7 @@ task2 = Task(agent, "What's the weather in new york") task3 = Task(agent, "What's the weather in london") # Add tasks to the workflow -workflow.add(task1) -workflow.add(task2) -workflow.add(task3) +workflow.add(tasks=[task1, task2, task3]) # Run the workflow workflow.run() @@ -413,9 +411,10 @@ print(out) ```python import os -from swarms import Task, Agent, OpenAIChat + from dotenv import load_dotenv +from swarms.structs import Agent, OpenAIChat, Task # Load the environment variables load_dotenv() @@ -440,7 +439,13 @@ agent = Agent( ) # Create a task -task = Task(agent, "Create a strategy to cut business costs by 40% this month") +task = Task( + description=( + "Generate a report on the top 3 biggest expenses for small" + " businesses and how businesses can save 20%" + ), + agent=agent, +) # Set the action and condition task.set_action(my_action) diff --git a/docs/swarms/agents/abstract_agent.md b/docs/swarms/agents/abstract_agent.md deleted file mode 100644 index 4201eef2..00000000 --- a/docs/swarms/agents/abstract_agent.md +++ /dev/null @@ -1,90 +0,0 @@ -`AbsractAgent` Class: A Deep Dive -======================== - -The `AbstractAgent` class is a fundamental building block in the design of AI systems. It encapsulates the behavior of an AI entity, allowing it to interact with other agents and perform actions. The class is designed to be flexible and extensible, enabling the creation of agents with diverse behaviors. - -## Architecture ------------- - -The architecture of the `AbstractAgent` class is centered around three main components: the agent's name, tools, and memory. - -- The `name` is a string that uniquely identifies the agent. This is crucial for communication between agents and for tracking their actions. - -- The `tools` are a list of `Tool` objects that the agent uses to perform its tasks. These could include various AI models, data processing utilities, or any other resources that the agent needs to function. The `tools` method is used to initialize these tools. - -- The `memory` is a `Memory` object that the agent uses to store and retrieve information. This could be used, for example, to remember past actions or to store the state of the environment. The `memory` method is used to initialize the memory. - -The `AbstractAgent` class also includes several methods that define the agent's behavior. These methods are designed to be overridden in subclasses to implement specific behaviors. - -## Methods -------- - -### `reset` - -The `reset` method is used to reset the agent's state. This could involve clearing the agent's memory, resetting its tools, or any other actions necessary to bring the agent back to its initial state. This method is abstract and must be overridden in subclasses. - -### `run` and `_arun` - -The `run` method is used to execute a task. The task is represented as a string, which could be a command, a query, or any other form of instruction that the agent can interpret. The `_arun` method is the asynchronous version of `run`, allowing tasks to be executed concurrently. - -### `chat` and `_achat` - -The `chat` method is used for communication between agents. It takes a list of messages as input, where each message is a dictionary. The `_achat` method is the asynchronous version of `chat`, allowing messages to be sent and received concurrently. - -### `step` and `_astep` - -The `step` method is used to advance the agent's state by one step in response to a message. The `_astep` method is the asynchronous version of `step`, allowing the agent's state to be updated concurrently. - -## Usage E#xamples --------------- - -### Example 1: Creating an Agent - -``` -from swarms.agents.base import AbtractAgent - -agent = Agent(name="Agent1") -print(agent.name) # Output: Agent1 -``` - - -In this example, we create an instance of `AbstractAgent` named "Agent1" and print its name. - -### Example 2: Initializing Tools and Memory - -``` -from swarms.agents.base import AbtractAgent - -agent = Agent(name="Agent1") -tools = [Tool1(), Tool2(), Tool3()] -memory_store = Memory() - -agent.tools(tools) -agent.memory(memory_store) -``` - - -In this example, we initialize the tools and memory of "Agent1". The tools are a list of `Tool` instances, and the memory is a `Memory` instance. - -### Example 3: Running an Agent - -``` -from swarms.agents.base import AbtractAgent - -agent = Agent(name="Agent1") -task = "Task1" - -agent.run(task) -``` - - -In this example, we run "Agent1" with a task named "Task1". - -Notes ------ - -- The `AbstractAgent` class is an abstract class, which means it cannot be instantiated directly. Instead, it should be subclassed, and at least the `reset`, `run`, `chat`, and `step` methods should be overridden. -- The `run`, `chat`, and `step` methods are designed to be flexible and can be adapted to a wide range of tasks and behaviors. For example, the `run` method could be used to execute a machine learning model, the `chat` method could be used to send and receive messages in a chatbot, and the `step` method could be used to update the agent's state in a reinforcement learning environment. -- The `_arun`, `_achat`, and `_astep` methods are asynchronous versions of the `run`, `chat`, and `step` methods, respectively. They return a coroutine that can be awaited using the `await` keyword. This allows multiple tasks to be executed concurrently, improving the efficiency of the agent. -- The `tools` and `memory` methods are used to initialize the agent's tools and memory, respectively. These methods can be overridden in subclasses to initialize specific tools and memory structures. -- The `reset` method is used to reset the agent's state. This method can be overridden in subclasses to define specific reset behaviors. For example, in a reinforcement learning agent, the \ No newline at end of file diff --git a/docs/swarms/agents/abstractagent.md b/docs/swarms/agents/abstractagent.md new file mode 100644 index 00000000..cdd06715 --- /dev/null +++ b/docs/swarms/agents/abstractagent.md @@ -0,0 +1,124 @@ +# swarms.agents + +## 1. Introduction + +`AbstractAgent` is an abstract class that serves as a foundation for implementing AI agents. An agent is an entity that can communicate with other agents and perform actions. The `AbstractAgent` class allows for customization in the implementation of the `receive` method, enabling different agents to define unique actions for receiving and processing messages. + +`AbstractAgent` provides capabilities for managing tools and accessing memory, and has methods for running, chatting, and stepping through communication with other agents. + +## 2. Class Definition + +```python +class AbstractAgent: + """An abstract class for AI agent. + + An agent can communicate with other agents and perform actions. + Different agents can differ in what actions they perform in the `receive` method. + + Agents are full and completed: + + Agents = llm + tools + memory + """ + + def __init__(self, name: str): + """ + Args: + name (str): name of the agent. + """ + self._name = name + + @property + def name(self): + """Get the name of the agent.""" + return self._name + + def tools(self, tools): + """init tools""" + + def memory(self, memory_store): + """init memory""" + pass + + def reset(self): + """(Abstract method) Reset the agent.""" + + def run(self, task: str): + """Run the agent once""" + + def _arun(self, taks: str): + """Run Async run""" + + def chat(self, messages: List[Dict]): + """Chat with the agent""" + + def _achat(self, messages: List[Dict]): + """Asynchronous Chat""" + + def step(self, message: str): + """Step through the agent""" + + def _astep(self, message: str): + """Asynchronous step""" +``` + +## 3. Functionality and Usage + +The `AbstractAgent` class represents a generic AI agent and provides a set of methods to interact with it. + +To create an instance of an agent, the `name` of the agent should be specified. + +### Core Methods + +#### 1. `reset` + +The `reset` method allows the agent to be reset to its initial state. + +```python +agent.reset() +``` + +#### 2. `run` + +The `run` method allows the agent to perform a specific task. + +```python +agent.run('some_task') +``` + +#### 3. `chat` + +The `chat` method enables communication with the agent through a series of messages. + +```python +messages = [{'id': 1, 'text': 'Hello, agent!'}, {'id': 2, 'text': 'How are you?'}] +agent.chat(messages) +``` + +#### 4. `step` + +The `step` method allows the agent to process a single message. + +```python +agent.step('Hello, agent!') +``` + +### Asynchronous Methods + +The class also provides asynchronous variants of the core methods. + +### Additional Functionality + +Additional functionalities for agent initialization and management of tools and memory are also provided. + +```python +agent.tools(some_tools) +agent.memory(some_memory_store) +``` + +## 4. Additional Information and Tips + +When implementing a new agent using the `AbstractAgent` class, ensure that the `receive` method is overridden to define the specific behavior of the agent upon receiving messages. + +## 5. References and Resources + +For further exploration and understanding of AI agents and agent communication, refer to the relevant literature and research on this topic. diff --git a/docs/swarms/agents/message.md b/docs/swarms/agents/message.md new file mode 100644 index 00000000..87794ebc --- /dev/null +++ b/docs/swarms/agents/message.md @@ -0,0 +1,120 @@ +# The Module/Class Name: Message + +In the swarms.agents framework, the class `Message` is used to represent a message with timestamp and optional metadata. + +## Overview and Introduction + +The `Message` class is a fundamental component that enables the representation of messages within an agent system. Messages contain essential information such as the sender, content, timestamp, and optional metadata. + +## Class Definition + +### Constructor: `__init__` + +The constructor of the `Message` class takes three parameters: + +1. `sender` (str): The sender of the message. +2. `content` (str): The content of the message. +3. `metadata` (dict or None): Optional metadata associated with the message. + +### Methods + +1. `__repr__(self)`: Returns a string representation of the `Message` object, including the timestamp, sender, and content. + +```python +class Message: + """ + Represents a message with timestamp and optional metadata. + + Usage + -------------- + mes = Message( + sender = "Kye", + content = "message" + ) + + print(mes) + """ + + def __init__(self, sender, content, metadata=None): + self.timestamp = datetime.datetime.now() + self.sender = sender + self.content = content + self.metadata = metadata or {} + + def __repr__(self): + """ + __repr__ represents the string representation of the Message object. + + Returns: + (str) A string containing the timestamp, sender, and content of the message. + """ + return f"{self.timestamp} - {self.sender}: {self.content}" +``` + +## Functionality and Usage + +The `Message` class represents a message in the agent system. Upon initialization, the `timestamp` is set to the current date and time, and the `metadata` is set to an empty dictionary if no metadata is provided. + +### Usage Example 1 + +Creating a `Message` object and displaying its string representation. + +```python +mes = Message( + sender = "Kye", + content = "Hello! How are you?" +) + +print(mes) +``` + +Output: +``` +2023-09-20 13:45:00 - Kye: Hello! How are you? +``` + +### Usage Example 2 + +Creating a `Message` object with metadata. + +```python +metadata = {"priority": "high", "category": "urgent"} +mes_with_metadata = Message( + sender = "Alice", + content = "Important update", + metadata = metadata +) + +print(mes_with_metadata) +``` + +Output: +``` +2023-09-20 13:46:00 - Alice: Important update +``` + +### Usage Example 3 + +Creating a `Message` object without providing metadata. + +```python +mes_no_metadata = Message( + sender = "Bob", + content = "Reminder: Meeting at 2PM" +) + +print(mes_no_metadata) +``` + +Output: +``` +2023-09-20 13:47:00 - Bob: Reminder: Meeting at 2PM +``` + +## Additional Information and Tips + +When creating a new `Message` object, ensure that the required parameters `sender` and `content` are provided. The `timestamp` will automatically be assigned the current date and time. Optional `metadata` can be included to provide additional context or information associated with the message. + +## References and Resources + +For further information on the `Message` class and its usage, refer to the official swarms.agents documentation and relevant tutorials related to message handling and communication within the agent system. diff --git a/docs/swarms/agents/omnimodalagent.md b/docs/swarms/agents/omnimodalagent.md new file mode 100644 index 00000000..841a39f0 --- /dev/null +++ b/docs/swarms/agents/omnimodalagent.md @@ -0,0 +1,79 @@ +# Module/Class Name: OmniModalAgent + +The `OmniModalAgent` class is a module that operates based on the Language Model (LLM) aka Language Understanding Model, Plans, Tasks, and Tools. It is designed to be a multi-modal chatbot which uses various AI-based capabilities for fulfilling user requests. + +It has the following architecture: +1. Language Model (LLM). +2. Chat Planner - Plans +3. Task Executor - Tasks +4. Tools - Tools + +![OmniModalAgent](https://source.unsplash.com/random) + +--- + +### Usage + from swarms import OmniModalAgent, OpenAIChat + + llm = OpenAIChat() + agent = OmniModalAgent(llm) + response = agent.run("Hello, how are you? Create an image of how your are doing!") + +--- + +--- + +### Initialization + +The constructor of `OmniModalAgent` class takes two main parameters: +- `llm`: A `BaseLanguageModel` that represents the language model +- `tools`: A List of `BaseTool` instances that are used by the agent for fulfilling different requests. + +```python +def __init__( + self, + llm: BaseLanguageModel, + # tools: List[BaseTool] +): +``` + +--- + +### Methods + +The class has two main methods: +1. `run`: This method takes an input string and executes various plans and tasks using the provided tools. Ultimately, it generates a response based on the user's input and returns it. + - Parameters: + - `input`: A string representing the user's input text. + - Returns: + - A string representing the response. + + Usage: + ```python + response = agent.run("Hello, how are you? Create an image of how your are doing!") + ``` + +2. `chat`: This method is used to simulate a chat dialog with the agent. It can take user's messages and return the response (or stream the response word-by-word if required). + - Parameters: + - `msg` (optional): A string representing the message to send to the agent. + - `streaming` (optional): A boolean specifying whether to stream the response. + - Returns: + - A string representing the response from the agent. + + Usage: + ```python + response = agent.chat("Hello") + ``` + +--- + +### Streaming Response + +The class provides a method `_stream_response` that can be used to get the response token by token (i.e. word by word). It yields individual tokens from the response. + +Usage: +```python +for token in _stream_response(response): + print(token) +``` + diff --git a/docs/swarms/agents/toolagent.md b/docs/swarms/agents/toolagent.md new file mode 100644 index 00000000..ebb00623 --- /dev/null +++ b/docs/swarms/agents/toolagent.md @@ -0,0 +1,113 @@ +# ToolAgent Documentation + + +### Overview and Introduction + +The `ToolAgent` class represents an intelligent agent capable of performing a specific task using a pre-trained model and tokenizer. It leverages the Transformer models of the Hugging Face `transformers` library to generate outputs that adhere to a specific JSON schema. This provides developers with a flexible tool for creating bots, text generators, and conversational AI agents. The `ToolAgent` operates based on a JSON schema provided by you, the user. Using the schema, the agent applies the provided model and tokenizer to generate structured text data that matches the specified format. + +The primary objective of the `ToolAgent` class is to amplify the efficiency of developers and AI practitioners by simplifying the process of generating meaningful outputs that navigate the complexities of the model and tokenizer. + +### Class Definition + +The `ToolAgent` class has the following definition: + +```python +class ToolAgent(AbstractLLM): + def __init__( + self, + name: str, + description: str, + model: Any, + tokenizer: Any, + json_schema: Any, + *args, + **kwargs, + ) + def run(self, task: str, *args, **kwargs) + def __call__(self, task: str, *args, **kwargs) +``` + +### Arguments + +The `ToolAgent` class takes the following arguments: + +| Argument | Type | Description | +| --- | --- | --- | +| name | str | The name of the tool agent. +| description | str | A description of the tool agent. +| model | Any | The model used by the tool agent (e.g., `transformers.AutoModelForCausalLM`). +| tokenizer | Any | The tokenizer used by the tool agent (e.g., `transformers.AutoTokenizer`). +| json_schema | Any | The JSON schema used by the tool agent. +| *args | - | Variable-length arguments. +| **kwargs | - | Keyword arguments. + +### Methods + +`ToolAgent` exposes the following methods: + +#### `run(self, task: str, *args, **kwargs) -> Any` + +- Description: Runs the tool agent for a specific task. +- Parameters: + - `task` (str): The task to be performed by the tool agent. + - `*args`: Variable-length argument list. + - `**kwargs`: Arbitrary keyword arguments. +- Returns: The output of the tool agent. +- Raises: Exception if an error occurs during the execution of the tool agent. + + +#### `__call__(self, task: str, *args, **kwargs) -> Any` + +- Description: Calls the tool agent to perform a specific task. +- Parameters: + - `task` (str): The task to be performed by the tool agent. + - `*args`: Variable-length argument list. + - `**kwargs`: Arbitrary keyword arguments. +- Returns: The output of the tool agent. + +### Usage Example + +```python +from transformers import AutoModelForCausalLM, AutoTokenizer +from swarms import ToolAgent + +# Creating a model and tokenizer +model = AutoModelForCausalLM.from_pretrained("databricks/dolly-v2-12b") +tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-12b") + +# Defining a JSON schema +json_schema = { + "type": "object", + "properties": { + "name": {"type": "string"}, + "age": {"type": "number"}, + "is_student": {"type": "boolean"}, + "courses": { + "type": "array", + "items": {"type": "string"} + } + } +} + +# Defining a task +task = "Generate a person's information based on the following schema:" + +# Creating the ToolAgent instance +agent = ToolAgent(model=model, tokenizer=tokenizer, json_schema=json_schema) + +# Running the tool agent +generated_data = agent.run(task) + +# Accessing and printing the generated data +print(generated_data) +``` + +### Additional Information and Tips + +When using the `ToolAgent`, it is important to ensure compatibility between the provided model, tokenizer, and the JSON schema. Additionally, any errors encountered during the execution of the tool agent are propagated as exceptions. Handling such exceptions appropriately can improve the robustness of the tool agent usage. + +### References and Resources + +For further exploration and understanding of the underlying Transformer-based models and tokenizers, refer to the Hugging Face `transformers` library documentation and examples. Additionally, for JSON schema modeling, you can refer to the official JSON Schema specification and examples. + +This documentation provides a comprehensive guide on using the `ToolAgent` class from `swarms` library, and it is recommended to refer back to this document when utilizing the `ToolAgent` for developing your custom conversational agents or text generation tools. diff --git a/docs/swarms/agents/workeragent.md b/docs/swarms/agents/workeragent.md new file mode 100644 index 00000000..e46ec1af --- /dev/null +++ b/docs/swarms/agents/workeragent.md @@ -0,0 +1,78 @@ +# WorkerClass Documentation + +## Overview + +The Worker class represents an autonomous agent that can perform tasks through function calls or by running a chat. It can be used to create applications that demand effective user interactions like search engines, human-like conversational bots, or digital assistants. + +The `Worker` class is part of the `swarms.agents` codebase. This module is largely used in Natural Language Processing (NLP) projects where the agent undertakes conversations and other language-specific operations. + +## Class Definition + +The class `Worker` has the following arguments: + +| Argument | Type | Default Value | Description | +|-----------------------|---------------|----------------------------------|----------------------------------------------------| +| name | str | "Worker" | Name of the agent. | +| role | str | "Worker in a swarm" | Role of the agent. | +| external_tools | list | None | List of external tools available to the agent. | +| human_in_the_loop | bool | False | Determines whether human interaction is required. | +| temperature | float | 0.5 | Temperature for the autonomous agent. | +| llm | None | None | Language model. | +| openai_api_key | str | None | OpenAI API key. | +| tools | List[Any] | None | List of tools available to the agent. | +| embedding_size | int | 1536 | Size of the word embeddings. | +| search_kwargs | dict | {"k": 8} | Search parameters. | +| args | Multiple | | Additional arguments that can be passed. | +| kwargs | Multiple | | Additional keyword arguments that can be passed. | +## Usage + +#### Example 1: Creating and Running an Agent + +```python +from swarms import Worker + +worker = Worker( + name="My Worker", + role="Worker", + external_tools=[MyTool1(), MyTool2()], + human_in_the_loop=False, + temperature=0.5, + llm=some_language_model, + openai_api_key="my_key" +) +worker.run("What's the weather in Miami?") +``` + +#### Example 2: Receiving and Sending Messages + +```python +worker.receieve("User", "Hello there!") +worker.receieve("User", "Can you tell me something about history?") +worker.send() +``` + +#### Example 3: Setting up Tools + +```python +external_tools = [MyTool1(), MyTool2()] +worker = Worker( +name="My Worker", +role="Worker", +external_tools=external_tools, +human_in_the_loop=False, +temperature=0.5, +) +``` + +## Additional Information and Tips + +- The class allows the setting up of tools for the worker to operate effectively. It provides setup facilities for essential computing infrastructure, such as the agent's memory and language model. +- By setting the `human_in_the_loop` parameter to True, interactions with the worker can be made more user-centric. +- The `openai_api_key` argument can be provided for leveraging the OpenAI infrastructure and services. +- A qualified language model can be passed as an instance of the `llm` object, which can be useful when integrating with state-of-the-art text generation engines. + +## References and Resources + +- [OpenAI APIs](https://openai.com) +- [Models and Languages at HuggingFace](https://huggingface.co/models) +- [Deep Learning and Language Modeling at the Allen Institute for AI](https://allenai.org) diff --git a/mkdocs.yml b/mkdocs.yml index 91e95270..963a58e6 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -60,12 +60,12 @@ nav: - Contributing: "contributing.md" - Swarms: - Overview: "swarms/index.md" - - swarms.workers: - - Overview: "swarms/workers/index.md" - - AbstractWorker: "swarms/workers/abstract_worker.md" - swarms.agents: - - AbstractAgent: "swarms/agents/abstract_agent.md" - - OmniModalAgent: "swarms/agents/omni_agent.md" + - Agents: + - WorkerAgent: "swarms/utils/workeragent.md" + - OmniAgent: "swarms/utils/omni_agent.md" + - AbstractAgent: "swarms/utils/abstractagent.md" + - ToolAgent: "swarms/utils/toolagent.md" - swarms.models: - Language: - BaseLLM: "swarms/models/base_llm.md" diff --git a/playground/structs/concurrent_workflow.py b/playground/structs/concurrent_workflow.py index a228d247..98531388 100644 --- a/playground/structs/concurrent_workflow.py +++ b/playground/structs/concurrent_workflow.py @@ -12,15 +12,18 @@ agent = Agent(llm=llm, max_loops=1) # Create a workflow workflow = ConcurrentWorkflow(max_workers=5) +task = ( + "Generate a report on how small businesses spend money and how" + " can they cut 40 percent of their costs" +) + # Create tasks -task1 = Task(agent, "What's the weather in miami") -task2 = Task(agent, "What's the weather in new york") -task3 = Task(agent, "What's the weather in london") +task1 = Task(agent, task) +task2 = Task(agent, task) +task3 = Task(agent, task) # Add tasks to the workflow -workflow.add(task1) -workflow.add(task2) -workflow.add(task3) +workflow.add(tasks=[task1, task2, task3]) # Run the workflow workflow.run() diff --git a/playground/structs/sequential_workflow.py b/playground/structs/sequential_workflow.py index fa7ca16a..5f60db72 100644 --- a/playground/structs/sequential_workflow.py +++ b/playground/structs/sequential_workflow.py @@ -1,6 +1,4 @@ -from swarms.models import OpenAIChat -from swarms.structs import Agent -from swarms.structs.sequential_workflow import SequentialWorkflow +from swarms import OpenAIChat, Agent, Task, SequentialWorkflow # Example usage llm = OpenAIChat( @@ -9,21 +7,38 @@ llm = OpenAIChat( ) # Initialize the Agent with the language agent -flow1 = Agent(llm=llm, max_loops=1, dashboard=False) +agent1 = Agent( + agent_name="John the writer", + llm=llm, + max_loops=0, + dashboard=False, +) +task1 = Task( + agent=agent1, + description="Write a 1000 word blog about the future of AI", +) # Create another Agent for a different task -flow2 = Agent(llm=llm, max_loops=1, dashboard=False) +agent2 = Agent("Summarizer", llm=llm, max_loops=1, dashboard=False) +task2 = Task( + agent=agent2, + description="Summarize the generated blog", +) # Create the workflow -workflow = SequentialWorkflow(max_loops=1) - -# Add tasks to the workflow -workflow.add( - "Generate a 10,000 word blog on health and wellness.", flow1 +workflow = SequentialWorkflow( + name="Blog Generation Workflow", + description=( + "A workflow to generate and summarize a blog about the future" + " of AI" + ), + max_loops=1, + autosave=True, + dashboard=False, ) -# Suppose the next task takes the output of the first task as input -workflow.add("Summarize the generated blog", flow2) +# Add tasks to the workflow +workflow.add(tasks=[task1, task2]) # Run the workflow workflow.run() diff --git a/playground/structs/task.py b/playground/structs/task.py index 089cb263..c2ade96a 100644 --- a/playground/structs/task.py +++ b/playground/structs/task.py @@ -1,8 +1,8 @@ -from swarms.structs import Task, Agent -from swarms.models import OpenAIChat -from dotenv import load_dotenv import os +from dotenv import load_dotenv + +from swarms.structs import Agent, OpenAIChat, Task # Load the environment variables load_dotenv() @@ -27,7 +27,13 @@ agent = Agent( ) # Create a task -task = Task(description="What's the weather in miami", agent=agent) +task = Task( + description=( + "Generate a report on the top 3 biggest expenses for small" + " businesses and how businesses can save 20%" + ), + agent=agent, +) # Set the action and condition task.set_action(my_action) diff --git a/scripts/auto_tests_docs/auto_docs.py b/scripts/auto_tests_docs/auto_docs.py index 8d441ff3..020684ff 100644 --- a/scripts/auto_tests_docs/auto_docs.py +++ b/scripts/auto_tests_docs/auto_docs.py @@ -9,20 +9,11 @@ from scripts.auto_tests_docs.docs import DOCUMENTATION_WRITER_SOP from swarms import OpenAIChat ########## -from swarms.structs.task import Task -from swarms.structs.swarm_net import SwarmNetwork -from swarms.structs.nonlinear_workflow import NonlinearWorkflow -from swarms.structs.recursive_workflow import RecursiveWorkflow -from swarms.structs.groupchat import GroupChat, GroupChatManager -from swarms.structs.base_workflow import BaseWorkflow -from swarms.structs.concurrent_workflow import ConcurrentWorkflow -from swarms.structs.base import BaseStructure -from swarms.structs.schemas import ( - Artifact, - ArtifactUpload, - StepInput, - TaskInput, -) +from swarms.agents.base import AbstractAgent +from swarms.structs.message import Message +from swarms.agents.omni_modal_agent import OmniModalAgent +from swarms.agents.tool_agent import ToolAgent +from swarms.agents.worker_agent import WorkerAgent #################### load_dotenv() @@ -49,14 +40,14 @@ def process_documentation(cls): # Process with OpenAI model (assuming the model's __call__ method takes this input and returns processed content) processed_content = model( - DOCUMENTATION_WRITER_SOP(input_content, "swarms.structs") + DOCUMENTATION_WRITER_SOP(input_content, "swarms.agents") ) # doc_content = f"# {cls.__name__}\n\n{processed_content}\n" doc_content = f"{processed_content}\n" # Create the directory if it doesn't exist - dir_path = "docs/swarms/structs" + dir_path = "docs/swarms/agents" os.makedirs(dir_path, exist_ok=True) # Write the processed documentation to a Markdown file @@ -69,19 +60,11 @@ def process_documentation(cls): def main(): classes = [ - Task, - SwarmNetwork, - NonlinearWorkflow, - RecursiveWorkflow, - GroupChat, - GroupChatManager, - BaseWorkflow, - ConcurrentWorkflow, - BaseStructure, - Artifact, - ArtifactUpload, - StepInput, - TaskInput, + AbstractAgent, + Message, + OmniModalAgent, + ToolAgent, + WorkerAgent, ] threads = [] for cls in classes: @@ -95,7 +78,7 @@ def main(): for thread in threads: thread.join() - print("Documentation generated in 'swarms.structs' directory.") + print("Documentation generated in 'swarms.agents' directory.") if __name__ == "__main__": diff --git a/scripts/auto_tests_docs/mkdocs_handler.py b/scripts/auto_tests_docs/mkdocs_handler.py index 8b1dc0a0..f5c4044f 100644 --- a/scripts/auto_tests_docs/mkdocs_handler.py +++ b/scripts/auto_tests_docs/mkdocs_handler.py @@ -28,4 +28,4 @@ def generate_file_list(directory, output_file): # Use the function to generate the file list -generate_file_list("docs/swarms/structs", "file_list.txt") +generate_file_list("docs/swarms/agents", "file_list.txt") diff --git a/swarms/agents/__init__.py b/swarms/agents/__init__.py index 876a4d27..9c855436 100644 --- a/swarms/agents/__init__.py +++ b/swarms/agents/__init__.py @@ -1,13 +1,35 @@ -from swarms.agents.message import Message from swarms.agents.base import AbstractAgent -from swarms.agents.tool_agent import ToolAgent -from swarms.agents.simple_agent import SimpleAgent from swarms.agents.omni_modal_agent import OmniModalAgent +from swarms.agents.simple_agent import SimpleAgent +from swarms.agents.stopping_conditions import ( + check_cancelled, + check_complete, + check_done, + check_end, + check_error, + check_exit, + check_failure, + check_finished, + check_stopped, + check_success, +) +from swarms.agents.tool_agent import ToolAgent +from swarms.agents.worker_agent import WorkerAgent __all__ = [ - "Message", "AbstractAgent", "ToolAgent", "SimpleAgent", "OmniModalAgent", + "check_done", + "check_finished", + "check_complete", + "check_success", + "check_failure", + "check_error", + "check_stopped", + "check_cancelled", + "check_exit", + "check_end", + "WorkerAgent", ] diff --git a/swarms/agents/omni_modal_agent.py b/swarms/agents/omni_modal_agent.py index fae6ab4e..113ec461 100644 --- a/swarms/agents/omni_modal_agent.py +++ b/swarms/agents/omni_modal_agent.py @@ -10,7 +10,7 @@ from langchain_experimental.autonomous_agents.hugginggpt.task_planner import ( ) from transformers import load_tool -from swarms.agents.message import Message +from swarms.structs.message import Message class OmniModalAgent: diff --git a/swarms/agents/stopping_conditions.py b/swarms/agents/stopping_conditions.py new file mode 100644 index 00000000..85acbf94 --- /dev/null +++ b/swarms/agents/stopping_conditions.py @@ -0,0 +1,38 @@ +def check_done(s): + return "" in s + + +def check_finished(s): + return "finished" in s + + +def check_complete(s): + return "complete" in s + + +def check_success(s): + return "success" in s + + +def check_failure(s): + return "failure" in s + + +def check_error(s): + return "error" in s + + +def check_stopped(s): + return "stopped" in s + + +def check_cancelled(s): + return "cancelled" in s + + +def check_exit(s): + return "exit" in s + + +def check_end(s): + return "end" in s diff --git a/swarms/agents/tool_agent.py b/swarms/agents/tool_agent.py index bc34a476..b2a22ba0 100644 --- a/swarms/agents/tool_agent.py +++ b/swarms/agents/tool_agent.py @@ -1,7 +1,3 @@ -""" -Tool Agent - -""" from swarms.tools.format_tools import Jsonformer from typing import Any from swarms.models.base_llm import AbstractLLM diff --git a/swarms/models/mistral.py b/swarms/models/mistral.py index 297ecf12..aeeb37a8 100644 --- a/swarms/models/mistral.py +++ b/swarms/models/mistral.py @@ -1,7 +1,7 @@ import torch from transformers import AutoModelForCausalLM, AutoTokenizer -from swarms.agents.message import Message +from swarms.structs.message import Message class Mistral: diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py index e7ba49ca..90b0b911 100644 --- a/swarms/structs/__init__.py +++ b/swarms/structs/__init__.py @@ -32,6 +32,7 @@ from swarms.structs.block_wrapper import block from swarms.structs.graph_workflow import GraphWorkflow from swarms.structs.step import Step from swarms.structs.plan import Plan +from swarms.structs.message import Message __all__ = [ @@ -66,4 +67,5 @@ __all__ = [ "GraphWorkflow", "Step", "Plan", + "Message", ] diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 1b137988..046844eb 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -1,9 +1,7 @@ import asyncio -import inspect import json import logging import random -import re import time import uuid from typing import Any, Callable, Dict, List, Optional, Tuple @@ -13,23 +11,19 @@ from termcolor import colored from swarms.memory.base_vectordb import VectorDatabase from swarms.prompts.agent_system_prompts import ( AGENT_SYSTEM_PROMPT_3, - agent_system_prompt_2, ) from swarms.prompts.multi_modal_autonomous_instruction_prompt import ( MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1, ) -from swarms.prompts.tools import ( - SCENARIOS, -) from swarms.tools.tool import BaseTool -from swarms.tools.tool_func_doc_scraper import scrape_tool_func_docs from swarms.utils.code_interpreter import SubprocessCodeInterpreter +from swarms.utils.data_to_text import data_to_text +from swarms.utils.logger import logger from swarms.utils.parse_code import ( extract_code_from_markdown, ) from swarms.utils.pdf_to_text import pdf_to_text from swarms.utils.token_count_tiktoken import limit_tokens_from_string -from swarms.utils.data_to_text import data_to_text # Utils @@ -112,26 +106,17 @@ class Agent: filtered_run: Run the agent with filtered responses interactive_run: Run the agent in interactive mode streamed_generation: Stream the generation of the response - get_llm_params: Get the llm parameters save_state: Save the state load_state: Load the state - get_llm_init_params: Get the llm init parameters - get_tool_description: Get the tool description - find_tool_by_name: Find a tool by name - extract_tool_commands: Extract the tool commands - execute_tools: Execute the tools - parse_and_execute_tools: Parse and execute the tools truncate_history: Truncate the history add_task_to_memory: Add the task to the memory add_message_to_memory: Add the message to the memory add_message_to_memory_and_truncate: Add the message to the memory and truncate - parse_tool_docs: Parse the tool docs print_dashboard: Print the dashboard loop_count_print: Print the loop count streaming: Stream the content _history: Generate the history _dynamic_prompt_setup: Setup the dynamic prompt - agent_system_prompt_2: Agent system prompt 2 run_async: Run the agent asynchronously run_async_concurrent: Run the agent asynchronously and concurrently run_async_concurrent: Run the agent asynchronously and concurrently @@ -182,7 +167,7 @@ class Agent: pdf_path: Optional[str] = None, list_of_pdf: Optional[str] = None, tokenizer: Optional[Any] = None, - memory: Optional[VectorDatabase] = None, + long_term_memory: Optional[VectorDatabase] = None, preset_stopping_token: Optional[bool] = False, traceback: Any = None, traceback_handlers: Any = None, @@ -210,9 +195,7 @@ class Agent: self.context_length = context_length self.sop = sop self.sop_list = sop_list - self.sop_list = [] - self.tools = tools or [] - self.tool_docs = [] + self.tools = tools self.system_prompt = system_prompt self.agent_name = agent_name self.agent_description = agent_description @@ -226,7 +209,7 @@ class Agent: self.pdf_path = pdf_path self.list_of_pdf = list_of_pdf self.tokenizer = tokenizer - self.memory = memory + self.long_term_memory = long_term_memory self.preset_stopping_token = preset_stopping_token self.traceback = traceback self.traceback_handlers = traceback_handlers @@ -256,13 +239,7 @@ class Agent: if preset_stopping_token: self.stopping_token = "" - # If tools exist then add the tool docs usage to the sop - if self.tools: - self.sop_list.append( - self.tools_prompt_prep(self.tool_docs, SCENARIOS) - ) - - # self.short_memory_test = Conversation(time_enabled=True) + # self.short_memory = Conversation(time_enabled=True) # If the docs exist then ingest the docs if self.docs: @@ -316,93 +293,6 @@ class Agent: """Format the template with the provided kwargs using f-string interpolation.""" return template.format(**kwargs) - def get_llm_init_params(self) -> str: - """Get LLM init params""" - init_signature = inspect.signature(self.llm.__init__) - params = init_signature.parameters - params_str_list = [] - - for name, param in params.items(): - if name == "self": - continue - if hasattr(self.llm, name): - value = getattr(self.llm, name) - else: - value = self.llm.__dict__.get(name, "Unknown") - - params_str_list.append( - f" {name.capitalize().replace('_', ' ')}: {value}" - ) - - return "\n".join(params_str_list) - - def get_tool_description(self): - """Get the tool description""" - if self.tools: - try: - tool_descriptions = [] - for tool in self.tools: - description = f"{tool.name}: {tool.description}" - tool_descriptions.append(description) - return "\n".join(tool_descriptions) - except Exception as error: - print( - f"Error getting tool description: {error} try" - " adding a description to the tool or removing" - " the tool" - ) - else: - return "No tools available" - - def find_tool_by_name(self, name: str): - """Find a tool by name""" - for tool in self.tools: - if tool.name == name: - return tool - - def extract_tool_commands(self, text: str): - """ - Extract the tool commands from the text - - Example: - ```json - { - "tool": "tool_name", - "params": { - "tool1": "inputs", - "param2": "value2" - } - } - ``` - - """ - # Regex to find JSON like strings - pattern = r"```json(.+?)```" - matches = re.findall(pattern, text, re.DOTALL) - json_commands = [] - for match in matches: - try: - json_commands = json.loads(match) - json_commands.append(json_commands) - except Exception as error: - print(f"Error parsing JSON command: {error}") - - def execute_tools(self, tool_name, params): - """Execute the tool with the provided params""" - tool = self.tool_find_by_name(tool_name) - if tool: - # Execute the tool with the provided parameters - tool_result = tool.run(**params) - print(tool_result) - - def parse_and_execute_tools(self, response: str): - """Parse and execute the tools""" - json_commands = self.extract_tool_commands(response) - for command in json_commands: - tool_name = command.get("tool") - params = command.get("parmas", {}) - self.execute_tools(tool_name, params) - def truncate_history(self): """ Take the history and truncate it to fit into the model context length @@ -446,12 +336,6 @@ class Agent: self.short_memory[-1].append(message) self.truncate_history() - def parse_tool_docs(self): - """Parse the tool docs""" - for tool in self.tools: - docs = self.tool_docs.append(scrape_tool_func_docs(tool)) - return str(docs) - def print_dashboard(self, task: str): """Print dashboard""" model_config = self.get_llm_init_params() @@ -578,14 +462,11 @@ class Agent: combined_prompt = f"{dynamic_prompt}\n{task}" return combined_prompt - def agent_system_prompt_2(self): - """Agent system prompt 2""" - return agent_system_prompt_2(self.agent_name) - def run( self, task: Optional[str] = None, img: Optional[str] = None, + *args, **kwargs, ): """ @@ -638,9 +519,7 @@ class Agent: self.dynamic_temperature() # Preparing the prompt - task = self.agent_history_prompt( - AGENT_SYSTEM_PROMPT_3, response - ) + task = self.agent_history_prompt(history=response) attempt = 0 while attempt < self.retry_attempts: @@ -663,10 +542,6 @@ class Agent: if self.code_interpreter: self.run_code(response) - # If there are any tools then parse and execute them - if self.tools: - self.parse_and_execute_tools(response) - # If interactive mode is enabled then print the response and get user input if self.interactive: print(f"AI: {response}") @@ -712,7 +587,7 @@ class Agent: return response except Exception as error: - print(f"Error running agent: {error}") + logger.error(f"Error running agent: {error}") raise def __call__(self, task: str, img: str = None, *args, **kwargs): @@ -740,8 +615,7 @@ class Agent: def agent_history_prompt( self, - system_prompt: str = AGENT_SYSTEM_PROMPT_3, - history=None, + history: str = None, ): """ Generate the agent history prompt @@ -754,7 +628,7 @@ class Agent: str: The agent history prompt """ if self.sop: - system_prompt = system_prompt or self.system_prompt + system_prompt = self.system_prompt agent_history_prompt = f""" SYSTEM_PROMPT: {system_prompt} @@ -767,7 +641,7 @@ class Agent: """ return agent_history_prompt else: - system_prompt = system_prompt or self.system_prompt + system_prompt = self.system_prompt agent_history_prompt = f""" SYSTEM_PROMPT: {system_prompt} @@ -777,7 +651,7 @@ class Agent: """ return agent_history_prompt - def agent_memory_prompt(self, query, prompt): + def long_term_memory_prompt(self, query: str, prompt: str): """ Generate the agent long term memory prompt @@ -788,16 +662,12 @@ class Agent: Returns: str: The agent history prompt """ - context_injected_prompt = prompt - if self.memory: - ltr = self.memory.query(query) - - context_injected_prompt = f"""{prompt} - ################ CONTEXT #################### - {ltr} - """ + ltr = self.long_term_memory.query(query) - return context_injected_prompt + return f"""{prompt} + ################ CONTEXT #################### + {ltr} + """ async def run_concurrent(self, tasks: List[str], **kwargs): """ @@ -1045,45 +915,6 @@ class Agent: print() return response - def get_llm_params(self): - """ - Extracts and returns the parameters of the llm object for serialization. - It assumes that the llm object has an __init__ method - with parameters that can be used to recreate it. - """ - if not hasattr(self.llm, "__init__"): - return None - - init_signature = inspect.signature(self.llm.__init__) - params = init_signature.parameters - llm_params = {} - - for name, param in params.items(): - if name == "self": - continue - if hasattr(self.llm, name): - value = getattr(self.llm, name) - if isinstance( - value, - ( - str, - int, - float, - bool, - list, - dict, - tuple, - type(None), - ), - ): - llm_params[name] = value - else: - llm_params[name] = str( - value - ) # For non-serializable objects, save their string representation. - - return llm_params - def save_state(self, file_path: str) -> None: """ Saves the current state of the agent to a JSON file, including the llm parameters. @@ -1277,85 +1108,40 @@ class Agent: text = limit_tokens_from_string(text, num_limits) return text - def tools_prompt_prep( - self, docs: str = None, scenarios: str = SCENARIOS - ): - """ - Tools prompt prep + def ingest_docs(self, docs: List[str], *args, **kwargs): + """Ingest the docs into the memory Args: - docs (str, optional): _description_. Defaults to None. - scenarios (str, optional): _description_. Defaults to None. + docs (List[str]): _description_ Returns: _type_: _description_ """ - PROMPT = f""" - # Task - You will be provided with a list of APIs. These APIs will have a - description and a list of parameters and return types for each tool. Your - task involves creating varied, complex, and detailed user scenarios - that require to call API calls. You must select what api to call based on - the context of the task and the scenario. - - For instance, given the APIs: SearchHotels, BookHotel, CancelBooking, - GetNFLNews. Given that GetNFLNews is explicitly provided, your scenario - should articulate something akin to: - - "The user wants to see if the Broncos won their last game (GetNFLNews). - They then want to see if that qualifies them for the playoffs and who - they will be playing against (GetNFLNews). The Broncos did make it into - the playoffs, so the user wants watch the game in person. They want to - look for hotels where the playoffs are occurring (GetNBANews + - SearchHotels). After looking at the options, the user chooses to book a - 3-day stay at the cheapest 4-star option (BookHotel)." - 13 - - This scenario exemplifies a scenario using 5 API calls. The scenario is - complex, detailed, and concise as desired. The scenario also includes two - APIs used in tandem, the required API, GetNBANews to search for the - playoffs location and SearchHotels to find hotels based on the returned - location. Usage of multiple APIs in tandem is highly desirable and will - receive a higher score. Ideally each scenario should contain one or more - instances of multiple APIs being used in tandem. - - Note that this scenario does not use all the APIs given and re-uses the " - GetNBANews" API. Re-using APIs is allowed, but each scenario should - involve as many different APIs as the user demands. Note that API usage is also included - in the scenario, but exact parameters ar necessary. You must use a - different combination of APIs for each scenario. All APIs must be used in - at least one scenario. You can only use the APIs provided in the APIs - section. - - Note that API calls are not explicitly mentioned and their uses are - included in parentheses. This behaviour should be mimicked in your - response. - - Output the tool usage in a strict json format with the function name and input to - the function. For example, Deliver your response in this format: - - ‘‘‘ - {scenarios} - ‘‘‘ - # APIs - ‘‘‘ - {docs} - ‘‘‘ - # Response - ‘‘‘ - """ - return PROMPT + for doc in docs: + data = data_to_text(doc) - def ingest_docs(self, docs: List[str], *args, **kwargs): - """Ingest the docs into the memory + return self.short_memory.append(data) + + def ingest_pdf(self, pdf: str): + """Ingest the pdf into the memory Args: - docs (List[str]): _description_ + pdf (str): _description_ Returns: _type_: _description_ """ - for doc in docs: - data = data_to_text(doc) + text = pdf_to_text(pdf) + return self.short_memory.append(text) - return self.short_memory.append(data) + def receieve_mesage(self, name: str, message: str): + """Receieve a message""" + message = f"{name}: {message}" + return self.short_memory.append(message) + + def send_agent_message( + self, agent_name: str, message: str, *args, **kwargs + ): + """Send a message to the agent""" + message = f"{agent_name}: {message}" + return self.run(message, *args, **kwargs) diff --git a/swarms/structs/async_workflow.py b/swarms/structs/async_workflow.py new file mode 100644 index 00000000..b46061b2 --- /dev/null +++ b/swarms/structs/async_workflow.py @@ -0,0 +1,103 @@ +import asyncio +from dataclasses import dataclass, field +from typing import Any, Callable, List, Optional +from swarms.structs.task import Task +from swarms.utils.logger import logger + + +@dataclass +class AsyncWorkflow: + """ + Represents an asynchronous workflow to run tasks. + + Attributes: + name (str): The name of the workflow. + description (str): The description of the workflow. + max_loops (int): The maximum number of loops to run the workflow. + autosave (bool): Flag indicating whether to autosave the results. + dashboard (bool): Flag indicating whether to display a dashboard. + task_pool (List[Any]): The list of tasks in the workflow. + results (List[Any]): The list of results from running the tasks. + loop (Optional[asyncio.AbstractEventLoop]): The event loop to use. + stopping_condition (Optional[Callable]): The stopping condition for the workflow. + + Methods: + add(tasks: List[Any]) -> None: + Add tasks to the workflow. + + delete(task: Task = None, tasks: List[Task] = None) -> None: + Delete a task from the workflow. + + run() -> List[Any]: + Run the workflow and return the results. + """ + + name: str = "Async Workflow" + description: str = "A workflow to run asynchronous tasks" + max_loops: int = 1 + autosave: bool = True + dashboard: bool = False + task_pool: List[Any] = field(default_factory=list) + results: List[Any] = field(default_factory=list) + loop: Optional[asyncio.AbstractEventLoop] = None + stopping_condition: Optional[Callable] = None + + async def add(self, task: Any, tasks: List[Any]): + """Add tasks to the workflow""" + try: + if tasks: + for task in tasks: + self.task_pool.extend(tasks) + elif task: + self.task_pool.append(task) + + else: + if task and tasks: + # Add the task and tasks to the task pool + self.task_pool.append(task) + self.task_pool.extend(tasks) + else: + raise ValueError( + "Either task or tasks must be provided" + ) + + except Exception as error: + logger.error(f"[ERROR][AsyncWorkflow] {error}") + + async def delete( + self, task: Any = None, tasks: List[Task] = None + ): + """Delete a task from the workflow""" + try: + if task: + self.task_pool.remove(task) + elif tasks: + for task in tasks: + self.task_pool.remove(task) + except Exception as error: + logger.error(f"[ERROR][AsyncWorkflow] {error}") + + async def run(self): + """Run the workflow""" + if self.loop is None: + self.loop = asyncio.get_event_loop() + for i in range(self.max_loops): + logger.info( + f"[INFO][AsyncWorkflow] Loop {i + 1}/{self.max_loops}" + ) + futures = [ + asyncio.ensure_future(task.execute()) + for task in self.task_pool + ] + self.results = await asyncio.gather(*futures) + # if self.autosave: + # self.save() + # if self.dashboard: + # self.display() + + # Add a stopping condition to stop the workflow, if provided but stopping_condition takes in a parameter s for string + if self.stopping_condition: + if self.stopping_condition(self.results): + break + + return self.results diff --git a/swarms/structs/concurrent_workflow.py b/swarms/structs/concurrent_workflow.py index dd3fc518..8aa5399b 100644 --- a/swarms/structs/concurrent_workflow.py +++ b/swarms/structs/concurrent_workflow.py @@ -1,6 +1,6 @@ import concurrent.futures from dataclasses import dataclass, field -from typing import Dict, List, Optional +from typing import Dict, List, Optional, Callable from swarms.structs.base import BaseStructure from swarms.structs.task import Task @@ -33,6 +33,7 @@ class ConcurrentWorkflow(BaseStructure): """ task_pool: List[Dict] = field(default_factory=list) + max_loops: int = 1 max_workers: int = 5 autosave: bool = False saved_state_filepath: Optional[str] = ( @@ -41,6 +42,7 @@ class ConcurrentWorkflow(BaseStructure): print_results: bool = False return_results: bool = False use_processes: bool = False + stopping_condition: Optional[Callable] = None def add(self, task: Task = None, tasks: List[Task] = None): """Adds a task to the workflow. @@ -66,7 +68,7 @@ class ConcurrentWorkflow(BaseStructure): logger.warning(f"[ERROR][ConcurrentWorkflow] {error}") raise error - def run(self): + def run(self, *args, **kwargs): """ Executes the tasks in parallel using a ThreadPoolExecutor. @@ -77,27 +79,37 @@ class ConcurrentWorkflow(BaseStructure): Returns: List[Any]: A list of the results of each task, if return_results is True. Otherwise, returns None. """ - with concurrent.futures.ThreadPoolExecutor( - max_workers=self.max_workers - ) as executor: - futures = { - executor.submit(task.execute): task - for task in self.task_pool - } - results = [] - - for future in concurrent.futures.as_completed(futures): - task = futures[future] - try: - result = future.result() - if self.print_results: - logger.info(f"Task {task}: {result}") - if self.return_results: - results.append(result) - except Exception as e: - logger.error( - f"Task {task} generated an exception: {e}" - ) + loop_count = 0 + while loop_count < self.max_loops: + with concurrent.futures.ThreadPoolExecutor( + max_workers=self.max_workers + ) as executor: + futures = { + executor.submit(task.execute): task + for task in self.task_pool + } + results = [] + + for future in concurrent.futures.as_completed( + futures + ): + task = futures[future] + try: + result = future.result() + if self.print_results: + logger.info(f"Task {task}: {result}") + if self.return_results: + results.append(result) + except Exception as e: + logger.error( + f"Task {task} generated an exception: {e}" + ) + + loop_count += 1 + if self.stopping_condition and self.stopping_condition( + results + ): + break return results if self.return_results else None diff --git a/swarms/agents/message.py b/swarms/structs/message.py similarity index 100% rename from swarms/agents/message.py rename to swarms/structs/message.py diff --git a/swarms/structs/recursive_workflow.py b/swarms/structs/recursive_workflow.py index 64b27406..afeb91b7 100644 --- a/swarms/structs/recursive_workflow.py +++ b/swarms/structs/recursive_workflow.py @@ -31,7 +31,11 @@ class RecursiveWorkflow(BaseStructure): >>> workflow.run() """ - def __init__(self, stop_token: str = ""): + def __init__( + self, + stop_token: str = "", + stopping_conditions: callable = None, + ): self.stop_token = stop_token self.task_pool = [] diff --git a/swarms/structs/sequential_workflow.py b/swarms/structs/sequential_workflow.py index 15e2c0f8..6b1d7c06 100644 --- a/swarms/structs/sequential_workflow.py +++ b/swarms/structs/sequential_workflow.py @@ -1,11 +1,9 @@ -import concurrent.futures import json from dataclasses import dataclass, field -from typing import Any, Callable, Dict, List, Optional, Union +from typing import Any, Dict, List, Optional from termcolor import colored -from swarms.structs.agent import Agent from swarms.structs.task import Task from swarms.utils.logger import logger @@ -14,7 +12,7 @@ from swarms.utils.logger import logger @dataclass class SequentialWorkflow: """ - SequentialWorkflow class for running a sequence of tasks using N number of autonomous agents. + SequentialWorkflow class for running a sequence of task_pool using N number of autonomous agents. Args: max_loops (int): The maximum number of times to run the workflow. @@ -22,7 +20,7 @@ class SequentialWorkflow: Attributes: - tasks (List[Task]): The list of tasks to execute. + task_pool (List[Task]): The list of task_pool to execute. max_loops (int): The maximum number of times to run the workflow. dashboard (bool): Whether to display the dashboard for the workflow. @@ -35,13 +33,13 @@ class SequentialWorkflow: >>> workflow.add("What's the weather in miami", llm) >>> workflow.add("Create a report on these metrics", llm) >>> workflow.run() - >>> workflow.tasks + >>> workflow.task_pool """ name: str = None description: str = None - tasks: List[Task] = field(default_factory=list) + task_pool: List[Task] = field(default_factory=list) max_loops: int = 1 autosave: bool = False saved_state_filepath: Optional[str] = ( @@ -52,9 +50,8 @@ class SequentialWorkflow: def add( self, - agent: Union[Callable, Agent], - task: Optional[str] = None, - tasks: Optional[List[str]] = None, + task: Optional[Task] = None, + tasks: Optional[List[Task]] = None, *args, **kwargs, ) -> None: @@ -69,26 +66,28 @@ class SequentialWorkflow: **kwargs: Additional keyword arguments to pass to the task execution. """ try: - # If the agent is a Agent instance, we include the task in kwargs for Agent.run() - if isinstance(agent, Agent): - kwargs["task"] = ( - task # Set the task as a keyword argument for Agent + # If the agent is a Task instance, we include the task in kwargs for Agent.run() + # Append the task to the task_pool list + if task: + self.task_pool.append(task) + logger.info( + f"[INFO][SequentialWorkflow] Added task {task} to" + " workflow" ) + elif tasks: + for task in tasks: + self.task_pool.append(task) + logger.info( + "[INFO][SequentialWorkflow] Added task" + f" {task} to workflow" + ) + else: + if task and tasks is not None: + # Add the task and list of tasks to the task_pool at the same time + self.task_pool.append(task) + for task in tasks: + self.task_pool.append(task) - # Append the task to the tasks list - self.tasks.append( - Task( - description=task, - agent=agent, - args=list(args), - kwargs=kwargs, - ) - ) - - logger.info( - f"[INFO][SequentialWorkflow] Added task {task} to" - " workflow" - ) except Exception as error: logger.error( colored( @@ -99,8 +98,12 @@ class SequentialWorkflow: def reset_workflow(self) -> None: """Resets the workflow by clearing the results of each task.""" try: - for task in self.tasks: + for task in self.task_pool: task.result = None + logger.info( + f"[INFO][SequentialWorkflow] Reset task {task} in" + " workflow" + ) except Exception as error: logger.error( colored(f"Error resetting workflow: {error}", "red"), @@ -115,7 +118,8 @@ class SequentialWorkflow: """ try: return { - task.description: task.result for task in self.tasks + task.description: task.result + for task in self.task_pool } except Exception as error: logger.error( @@ -124,14 +128,10 @@ class SequentialWorkflow: ), ) - def remove_task(self, task: str) -> None: - """Remove tasks from sequential workflow""" + def remove_task(self, task: Task) -> None: + """Remove task_pool from sequential workflow""" try: - self.tasks = [ - task - for task in self.tasks - if task.description != task - ] + self.task_pool.remove(task) logger.info( f"[INFO][SequentialWorkflow] Removed task {task} from" " workflow" @@ -144,129 +144,6 @@ class SequentialWorkflow: ), ) - def update_task(self, task: str, **updates) -> None: - """ - Updates the arguments of a task in the workflow. - - Args: - task (str): The description of the task to update. - **updates: The updates to apply to the task. - - Raises: - ValueError: If the task is not found in the workflow. - - Examples: - >>> from swarms.models import OpenAIChat - >>> from swarms.structs import SequentialWorkflow - >>> llm = OpenAIChat(openai_api_key="") - >>> workflow = SequentialWorkflow(max_loops=1) - >>> workflow.add("What's the weather in miami", llm) - >>> workflow.add("Create a report on these metrics", llm) - >>> workflow.update_task("What's the weather in miami", max_tokens=1000) - >>> workflow.tasks[0].kwargs - {'max_tokens': 1000} - - """ - try: - for task in self.tasks: - if task.description == task: - task.kwargs.update(updates) - break - else: - raise ValueError( - f"Task {task} not found in workflow." - ) - - print( - f"[INFO][SequentialWorkflow] Updated task {task} in" - " workflow" - ) - except Exception as error: - logger.error( - colored( - f"Error updating task in workflow: {error}", "red" - ), - ) - - def delete_task(self, task: str) -> None: - """ - Delete a task from the workflow. - - Args: - task (str): The description of the task to delete. - - Raises: - ValueError: If the task is not found in the workflow. - - Examples: - >>> from swarms.models import OpenAIChat - >>> from swarms.structs import SequentialWorkflow - >>> llm = OpenAIChat(openai_api_key="") - >>> workflow = SequentialWorkflow(max_loops=1) - >>> workflow.add("What's the weather in miami", llm) - >>> workflow.add("Create a report on these metrics", llm) - >>> workflow.delete_task("What's the weather in miami") - >>> workflow.tasks - [Task(description='Create a report on these metrics', agent=Agent(llm=OpenAIChat(openai_api_key=''), max_loops=1, dashboard=False), args=[], kwargs={}, result=None, history=[])] - """ - try: - for task in self.tasks: - if task.description == task: - self.tasks.remove(task) - break - else: - raise ValueError( - f"Task {task} not found in workflow." - ) - - print( - f"[INFO][SequentialWorkflow] Deleted task {task} from" - " workflow" - ) - except Exception as error: - logger.error( - colored( - f"Error deleting task from workflow: {error}", - "red", - ), - ) - - def concurrent_run(self): - """ - Concurrently run the workflow using a pool of workers. - - Examples: - >>> from swarms.models import OpenAIChat - >>> from swarms.structs import SequentialWorkflow - >>> llm = OpenAIChat(openai_api_key="") - >>> workflow = SequentialWorkflow(max_loops=1) - - """ - try: - with concurrent.futures.ThreadPoolExecutor() as executor: - futures_to_task = { - executor.submit(task.run): task - for task in self.tasks - } - results = [] - for future in concurrent.futures.as_completed( - futures_to_task - ): - task = futures_to_task[future] - - try: - result = future.result() - except Exception as error: - print(f"Error running workflow: {error}") - else: - results.append(result) - print( - f"Task {task} completed successfully with" - f" result: {result}" - ) - except Exception as error: - print(colored(f"Error running workflow: {error}", "red")) - def save_workflow_state( self, filepath: Optional[str] = "sequential_workflow_state.json", @@ -293,7 +170,7 @@ class SequentialWorkflow: with open(filepath, "w") as f: # Saving the state as a json for simplicuty state = { - "tasks": [ + "task_pool": [ { "description": task.description, "args": task.args, @@ -301,13 +178,13 @@ class SequentialWorkflow: "result": task.result, "history": task.history, } - for task in self.tasks + for task in self.task_pool ], "max_loops": self.max_loops, } json.dump(state, f, indent=4) - print( + logger.info( "[INFO][SequentialWorkflow] Saved workflow state to" f" {filepath}" ) @@ -357,7 +234,7 @@ class SequentialWorkflow: -------------------------------- Name: {self.name} Description: {self.description} - Tasks: {len(self.tasks)} + task_pool: {len(self.task_pool)} Max Loops: {self.max_loops} Autosave: {self.autosave} Autosave Filepath: {self.saved_state_filepath} @@ -382,38 +259,6 @@ class SequentialWorkflow: ) ) - def add_objective_to_workflow(self, task: str, **kwargs) -> None: - """Adds an objective to the workflow.""" - try: - print( - colored( - """ - Adding Objective to Workflow...""", - "green", - attrs=["bold", "underline"], - ) - ) - - task = Task( - description=task, - agent=kwargs["agent"], - args=list(kwargs["args"]), - kwargs=kwargs["kwargs"], - ) - self.tasks.append(task) - - print( - f"[INFO][SequentialWorkflow] Added task {task} to" - " workflow" - ) - except Exception as error: - logger.error( - colored( - f"Error adding objective to workflow: {error}", - "red", - ) - ) - def load_workflow_state( self, filepath: str = None, **kwargs ) -> None: @@ -440,8 +285,8 @@ class SequentialWorkflow: with open(filepath, "r") as f: state = json.load(f) self.max_loops = state["max_loops"] - self.tasks = [] - for task_state in state["tasks"]: + self.task_pool = [] + for task_state in state["task_pool"]: task = Task( description=task_state["description"], agent=task_state["agent"], @@ -450,7 +295,7 @@ class SequentialWorkflow: result=task_state["result"], history=task_state["history"], ) - self.tasks.append(task) + self.task_pool.append(task) print( "[INFO][SequentialWorkflow] Loaded workflow state" @@ -474,114 +319,35 @@ class SequentialWorkflow: """ try: self.workflow_bootup() - for _ in range(self.max_loops): - for task in self.tasks: + loops = 0 + while loops < self.max_loops: + for i in range(len(self.task_pool)): + task = self.task_pool[i] # Check if the current task can be executed if task.result is None: - # Check if the agent is a Agent and a 'task' argument is needed - if isinstance(task.agent, Agent): - # Ensure that 'task' is provided in the kwargs - if "task" not in task.kwargs: - raise ValueError( - "The 'task' argument is required" - " for the Agent agent execution" - f" in '{task.description}'" - ) - # Separate the 'task' argument from other kwargs - flow_task_arg = task.kwargs.pop("task") - task.result = task.agent.run( - flow_task_arg, - *task.args, - **task.kwargs, - ) - else: - # If it's not a Agent instance, call the agent directly - task.result = task.agent( - *task.args, **task.kwargs - ) + # Get the inputs for the current task + task.context(task) - # Pass the result as an argument to the next task if it exists - next_task_index = self.tasks.index(task) + 1 - if next_task_index < len(self.tasks): - next_task = self.tasks[next_task_index] - if isinstance(next_task.agent, Agent): - # For Agent flows, 'task' should be a keyword argument - next_task.kwargs["task"] = task.result - else: - # For other callable flows, the result is added to args - next_task.args.insert(0, task.result) + result = task.execute() - # Autosave the workflow state - if self.autosave: - self.save_workflow_state( - "sequential_workflow_state.json" - ) - except Exception as e: - logger.error( - colored( - ( - "Error initializing the Sequential workflow:" - f" {e} try optimizing your inputs like the" - " agent class and task description" - ), - "red", - attrs=["bold", "underline"], - ) - ) - - async def arun(self) -> None: - """ - Asynchronously run the workflow. - - Raises: - ValueError: If a Agent instance is used as a task and the 'task' argument is not provided. - - """ - try: - for _ in range(self.max_loops): - for task in self.tasks: - # Check if the current task can be executed - if task.result is None: - # Check if the agent is a Agent and a 'task' argument is needed - if isinstance(task.agent, Agent): - # Ensure that 'task' is provided in the kwargs - if "task" not in task.kwargs: - raise ValueError( - "The 'task' argument is required" - " for the Agent agent execution" - f" in '{task.description}'" - ) - # Separate the 'task' argument from other kwargs - flow_task_arg = task.kwargs.pop("task") - task.result = await task.agent.arun( - flow_task_arg, - *task.args, - **task.kwargs, - ) - else: - # If it's not a Agent instance, call the agent directly - task.result = await task.agent( - *task.args, **task.kwargs - ) + # Pass the inputs to the next task + if i < len(self.task_pool) - 1: + next_task = self.task_pool[i + 1] + next_task.description = result - # Pass the result as an argument to the next task if it exists - next_task_index = self.tasks.index(task) + 1 - if next_task_index < len(self.tasks): - next_task = self.tasks[next_task_index] - if isinstance(next_task.agent, Agent): - # For Agent flows, 'task' should be a keyword argument - next_task.kwargs["task"] = task.result - else: - # For other callable flows, the result is added to args - next_task.args.insert(0, task.result) + # Execute the current task + task.execute() # Autosave the workflow state if self.autosave: self.save_workflow_state( "sequential_workflow_state.json" ) + + self.workflow_shutdown() + loops += 1 except Exception as e: - print( + logger.error( colored( ( "Error initializing the Sequential workflow:" diff --git a/swarms/structs/task.py b/swarms/structs/task.py index 68a30951..a794506f 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -12,6 +12,7 @@ from typing import ( from swarms.structs.agent import Agent from swarms.utils.logger import logger +from swarms.structs.conversation import Conversation @dataclass @@ -57,9 +58,7 @@ class Task: """ agent: Union[Callable, Agent] - description: str - args: List[Any] = field(default_factory=list) - kwargs: Dict[str, Any] = field(default_factory=dict) + description: str = None result: Any = None history: List[Any] = field(default_factory=list) schedule_time: datetime = None @@ -69,8 +68,10 @@ class Task: condition: Callable = None priority: int = 0 dependencies: List["Task"] = field(default_factory=list) + args: List[Any] = field(default_factory=list) + kwargs: Dict[str, Any] = field(default_factory=dict) - def execute(self, task: str, img: str = None, *args, **kwargs): + def execute(self, *args, **kwargs): """ Execute the task by calling the agent or model with the arguments and keyword arguments. You can add images to the agent by passing the @@ -86,8 +87,10 @@ class Task: >>> task.result """ - logger.info(f"[INFO][Task] Executing task: {task}") - task = self.description or task + logger.info( + f"[INFO][Task] Executing task: {self.description}" + ) + task = self.description try: if isinstance(self.agent, Agent): if self.condition is None or self.condition(): @@ -109,11 +112,11 @@ class Task: except Exception as error: logger.error(f"[ERROR][Task] {error}") - def run(self, task: str, *args, **kwargs): - self.execute(task, *args, **kwargs) + def run(self, *args, **kwargs): + self.execute(*args, **kwargs) - def __call__(self, task: str, *args, **kwargs): - self.execute(task, *args, **kwargs) + def __call__(self, *args, **kwargs): + self.execute(*args, **kwargs) def handle_scheduled_task(self): """ @@ -206,3 +209,51 @@ class Task: logger.error( f"[ERROR][Task][check_dependency_completion] {error}" ) + + def context( + self, + task: "Task" = None, + context: List["Task"] = None, + *args, + **kwargs, + ): + """ + Set the context for the task. + + Args: + context (str): The context to set. + """ + # For sequential workflow, sequentially add the context of the previous task in the list + new_context = Conversation(time_enabled=True, *args, **kwargs) + + if context: + for task in context: + description = ( + task.description + if task.description is not None + else "" + ) + result = ( + task.result if task.result is not None else "" + ) + + # Add the context of the task to the conversation + new_context.add( + task.agent.agent_name, f"{description} {result}" + ) + + elif task: + description = ( + task.description + if task.description is not None + else "" + ) + result = task.result if task.result is not None else "" + new_context.add( + task.agent.agent_name, f"{description} {result}" + ) + + prompt = new_context.return_history_as_string() + + # Add to history + return self.history.append(prompt) diff --git a/swarms/tools/tool_utils.py b/swarms/tools/tool_utils.py index da13e223..a5a4e47c 100644 --- a/swarms/tools/tool_utils.py +++ b/swarms/tools/tool_utils.py @@ -1,6 +1,12 @@ -import re import json -from typing import List, Any +import re +from typing import Any, List + +from swarms.prompts.tools import ( + SCENARIOS, +) +from swarms.tools.tool import BaseTool +from swarms.tools.tool_func_doc_scraper import scrape_tool_func_docs def tool_find_by_name(tool_name: str, tools: List[Any]): @@ -55,3 +61,79 @@ def execute_tools(tool_name, params): # Execute the tool with the provided parameters tool_result = tool.run(**params) print(tool_result) + + +def parse_tool_docs(tools: List[BaseTool]): + """Parse the tool docs""" + tool_docs = [] + for tool in tools: + docs = tool_docs.append(scrape_tool_func_docs(tool)) + return str(docs) + + +def tools_prompt_prep(docs: str = None, scenarios: str = SCENARIOS): + """ + Tools prompt prep + + Args: + docs (str, optional): _description_. Defaults to None. + scenarios (str, optional): _description_. Defaults to None. + + Returns: + _type_: _description_ + """ + PROMPT = f""" + # Task + You will be provided with a list of APIs. These APIs will have a + description and a list of parameters and return types for each tool. Your + task involves creating varied, complex, and detailed user scenarios + that require to call API calls. You must select what api to call based on + the context of the task and the scenario. + + For instance, given the APIs: SearchHotels, BookHotel, CancelBooking, + GetNFLNews. Given that GetNFLNews is explicitly provided, your scenario + should articulate something akin to: + + "The user wants to see if the Broncos won their last game (GetNFLNews). + They then want to see if that qualifies them for the playoffs and who + they will be playing against (GetNFLNews). The Broncos did make it into + the playoffs, so the user wants watch the game in person. They want to + look for hotels where the playoffs are occurring (GetNBANews + + SearchHotels). After looking at the options, the user chooses to book a + 3-day stay at the cheapest 4-star option (BookHotel)." + 13 + + This scenario exemplifies a scenario using 5 API calls. The scenario is + complex, detailed, and concise as desired. The scenario also includes two + APIs used in tandem, the required API, GetNBANews to search for the + playoffs location and SearchHotels to find hotels based on the returned + location. Usage of multiple APIs in tandem is highly desirable and will + receive a higher score. Ideally each scenario should contain one or more + instances of multiple APIs being used in tandem. + + Note that this scenario does not use all the APIs given and re-uses the " + GetNBANews" API. Re-using APIs is allowed, but each scenario should + involve as many different APIs as the user demands. Note that API usage is also included + in the scenario, but exact parameters ar necessary. You must use a + different combination of APIs for each scenario. All APIs must be used in + at least one scenario. You can only use the APIs provided in the APIs + section. + + Note that API calls are not explicitly mentioned and their uses are + included in parentheses. This behaviour should be mimicked in your + response. + + Output the tool usage in a strict json format with the function name and input to + the function. For example, Deliver your response in this format: + + ‘‘‘ + {scenarios} + ‘‘‘ + # APIs + ‘‘‘ + {docs} + ‘‘‘ + # Response + ‘‘‘ + """ + return PROMPT diff --git a/swarms/utils/fetch_init_params.py b/swarms/utils/fetch_init_params.py new file mode 100644 index 00000000..5798eb14 --- /dev/null +++ b/swarms/utils/fetch_init_params.py @@ -0,0 +1,33 @@ +import inspect + + +def get_cls_init_params(cls) -> str: + """ + Get the initialization parameters of a class. + + Args: + cls: The class to retrieve the initialization parameters from. + + Returns: + str: A string representation of the initialization parameters. + + """ + init_signature = inspect.signature(cls.__init__) + params = init_signature.parameters + params_str_list = [] + + for name, param in params.items(): + if name == "self": + continue + if name == "kwargs": + value = "Any keyword arguments" + elif hasattr(cls, name): + value = getattr(cls, name) + else: + value = cls.__dict__.get(name, "Unknown") + + params_str_list.append( + f" {name.capitalize().replace('_', ' ')}: {value}" + ) + + return "\n".join(params_str_list) diff --git a/swarms/utils/file_extension_seach.py b/swarms/utils/file_extension_seach.py new file mode 100644 index 00000000..a29cb505 --- /dev/null +++ b/swarms/utils/file_extension_seach.py @@ -0,0 +1,21 @@ +import re + + +def get_file_extension(s): + """ + Get the file extension from a given string. + + Args: + s (str): The input string. + + Returns: + str or None: The file extension if found, or None if not found. + + Raises: + ValueError: If the input is not a string. + """ + if not isinstance(s, str): + raise ValueError("Input must be a string") + + match = re.search(r"\.(pdf|csv|txt|docx|xlsx)$", s, re.IGNORECASE) + return match.group()[1:] if match else None From 26ce6e098dc30068e9092396747e3aee2adc2fd6 Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 22 Jan 2024 14:25:53 -0500 Subject: [PATCH 038/103] [README] --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 54253876..ab596850 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Swarms is a modular framework that enables reliable and useful multi-agent colla ---- ## Installation -`pip3 install --upgrade swarms` +`pip3 install -U swarms` --- @@ -1043,10 +1043,13 @@ Help us accelerate our backlog by supporting us financially! Note, we're an open ## Swarm Newsletter 🤖 🤖 🤖 📧 -Sign up to the Swarm newsletter to receieve updates on the latest Autonomous agent research papers, step by step guides on creating multi-agent app, and much more Swarmie goodiness 😊 +Sign up to the Swarm newsletter to receive updates on the latest Autonomous agent research papers, step by step guides on creating multi-agent app, and much more Swarmie goodiness 😊 + [CLICK HERE TO SIGNUP](https://docs.google.com/forms/d/e/1FAIpQLSfqxI2ktPR9jkcIwzvHL0VY6tEIuVPd-P2fOWKnd6skT9j1EQ/viewform?usp=sf_link) # License Apache License + + From a46b5418b11bfe43f59b8736c5301e63786c1bab Mon Sep 17 00:00:00 2001 From: Eternal Reclaimer <98760976+kyegomez@users.noreply.github.com> Date: Mon, 22 Jan 2024 14:30:20 -0500 Subject: [PATCH 039/103] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index ab596850..8f13e2e7 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,7 @@
-Swarms is a modular framework that enables reliable and useful multi-agent collaboration at scale to automate real-world tasks. - +A modular framework that enables you to Build, Deploy, and Scale Reliable Autonomous Agents. Get started now below. [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) [![GitHub license](https://img.shields.io/github/license/kyegomez/swarms)](https://github.com/kyegomez/swarms/blob/main/LICENSE)[![GitHub star chart](https://img.shields.io/github/stars/kyegomez/swarms?style=social)](https://star-history.com/#kyegomez/swarms)[![Dependency Status](https://img.shields.io/librariesio/github/kyegomez/swarms)](https://libraries.io/github/kyegomez/swarms) [![Downloads](https://static.pepy.tech/badge/swarms/month)](https://pepy.tech/project/swarms) From bd24931d9367a4a19e3d9f9ba9d29f5cac0d6ff7 Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 22 Jan 2024 14:36:46 -0500 Subject: [PATCH 040/103] [BUFG][Docs] --- mkdocs.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mkdocs.yml b/mkdocs.yml index 963a58e6..e7d76b06 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -62,10 +62,10 @@ nav: - Overview: "swarms/index.md" - swarms.agents: - Agents: - - WorkerAgent: "swarms/utils/workeragent.md" - - OmniAgent: "swarms/utils/omni_agent.md" - - AbstractAgent: "swarms/utils/abstractagent.md" - - ToolAgent: "swarms/utils/toolagent.md" + - WorkerAgent: "swarms/agents/workeragent.md" + - OmniAgent: "swarms/agents/omni_agent.md" + - AbstractAgent: "swarms/agents/abstractagent.md" + - ToolAgent: "swarms/agents/toolagent.md" - swarms.models: - Language: - BaseLLM: "swarms/models/base_llm.md" From aebec35d5442207d35d5cd204c999c3d9a978cdb Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 22 Jan 2024 17:50:25 -0500 Subject: [PATCH 041/103] [FEATS] [ broadcast, circular_swarm, exponential_swarm, fibonacci_swarm, geometric_swarm, grid_swarm, harmonic_swarm, linear_swarm, log_swarm, mesh_swarm, one_to_one, one_to_three, power_swarm, prime_swarm, pyramid_swarm, sigmoid_swarm, staircase_swarm, star_swarm,] --- mkdocs.yml | 2 +- swarms/structs/__init__.py | 53 ++++++-- swarms/structs/agent.py | 17 +++ swarms/structs/company.py | 156 +++++++++++++++++++++++ swarms/structs/swarming_architectures.py | 94 ++++++++++++++ 5 files changed, 313 insertions(+), 9 deletions(-) create mode 100644 swarms/structs/company.py diff --git a/mkdocs.yml b/mkdocs.yml index e7d76b06..184f059f 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -111,7 +111,7 @@ nav: - sequential_workflow: "swarms/structs/sequential_workflow.md" - workflow: "swarms/structs/workflow.md" - baseworkflow: "swarms/structs/baseworkflow.md" - - Multi-Agent-Architectures: + - Multi Agent Architectures: - conversation: "swarms/structs/conversation.md" - groupchat: "swarms/structs/groupchat.md" - swarmnetwork: "swarms/structs/swarmnetwork.md" diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py index 90b0b911..11e9e523 100644 --- a/swarms/structs/__init__.py +++ b/swarms/structs/__init__.py @@ -3,12 +3,16 @@ from swarms.structs.autoscaler import AutoScaler from swarms.structs.base import BaseStructure from swarms.structs.base_swarm import AbstractSwarm from swarms.structs.base_workflow import BaseWorkflow +from swarms.structs.block_wrapper import block from swarms.structs.concurrent_workflow import ConcurrentWorkflow from swarms.structs.conversation import Conversation +from swarms.structs.graph_workflow import GraphWorkflow from swarms.structs.groupchat import GroupChat, GroupChatManager +from swarms.structs.message import Message from swarms.structs.model_parallizer import ModelParallelizer from swarms.structs.multi_agent_collab import MultiAgentCollaboration from swarms.structs.nonlinear_workflow import NonlinearWorkflow +from swarms.structs.plan import Plan from swarms.structs.recursive_workflow import RecursiveWorkflow from swarms.structs.schemas import ( Artifact, @@ -17,23 +21,38 @@ from swarms.structs.schemas import ( TaskInput, ) from swarms.structs.sequential_workflow import SequentialWorkflow +from swarms.structs.step import Step from swarms.structs.swarm_net import SwarmNetwork +from swarms.structs.swarming_architectures import ( + broadcast, + circular_swarm, + exponential_swarm, + fibonacci_swarm, + geometric_swarm, + grid_swarm, + harmonic_swarm, + linear_swarm, + log_swarm, + mesh_swarm, + one_to_one, + one_to_three, + power_swarm, + prime_swarm, + pyramid_swarm, + sigmoid_swarm, + staircase_swarm, + star_swarm, +) +from swarms.structs.task import Task from swarms.structs.utils import ( + detect_markdown, distribute_tasks, extract_key_from_json, extract_tokens_from_text, find_agent_by_id, find_token_in_text, parse_tasks, - detect_markdown, ) -from swarms.structs.task import Task -from swarms.structs.block_wrapper import block -from swarms.structs.graph_workflow import GraphWorkflow -from swarms.structs.step import Step -from swarms.structs.plan import Plan -from swarms.structs.message import Message - __all__ = [ "Agent", @@ -68,4 +87,22 @@ __all__ = [ "Step", "Plan", "Message", + "broadcast", + "circular_swarm", + "exponential_swarm", + "fibonacci_swarm", + "geometric_swarm", + "grid_swarm", + "harmonic_swarm", + "linear_swarm", + "log_swarm", + "mesh_swarm", + "one_to_one", + "one_to_three", + "power_swarm", + "prime_swarm", + "pyramid_swarm", + "sigmoid_swarm", + "staircase_swarm", + "star_swarm", ] diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 046844eb..dace1ff3 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -1,6 +1,7 @@ import asyncio import json import logging +import os import random import time import uuid @@ -173,6 +174,7 @@ class Agent: traceback_handlers: Any = None, streaming_on: Optional[bool] = False, docs: List[str] = None, + docs_folder: str = None, *args, **kwargs: Any, ): @@ -215,6 +217,7 @@ class Agent: self.traceback_handlers = traceback_handlers self.streaming_on = streaming_on self.docs = docs + self.docs_folder = docs_folder # The max_loops will be set dynamically if the dynamic_loop if self.dynamic_loops: @@ -245,6 +248,9 @@ class Agent: if self.docs: self.ingest_docs(self.docs) + if self.docs_folder: + self.get_docs_from_doc_folders() + def set_system_prompt(self, system_prompt: str): """Set the system prompt""" self.system_prompt = system_prompt @@ -1145,3 +1151,14 @@ class Agent: """Send a message to the agent""" message = f"{agent_name}: {message}" return self.run(message, *args, **kwargs) + + def get_docs_from_doc_folders(self): + """Get the docs from the files""" + # Get the list of files then extract them and add them to the memory + files = os.listdir(self.docs_folder) + + # Extract the text from the files + for file in files: + text = data_to_text(file) + + return self.short_memory.append(text) diff --git a/swarms/structs/company.py b/swarms/structs/company.py new file mode 100644 index 00000000..b01c0a00 --- /dev/null +++ b/swarms/structs/company.py @@ -0,0 +1,156 @@ +from typing import List, Optional, Union, Dict +from dataclasses import dataclass, field +from swarms.structs.agent import Agent +from swarms.utils.logger import logger + + +@dataclass +class Company: + """ + Represents a company with a hierarchical organizational structure. + """ + org_chart: Union[List[Agent], List[List[Agent]]] + shared_instructions: str = None + ceo: Optional[Agent] = None + agents: List[Agent] = field(default_factory=list) + agent_interactions: Dict[str, List[str]] = field( + default_factory=dict + ) + + def __post_init__(self): + self._parse_org_chart(self.org_chart) + + def add(self, agent: Agent) -> None: + """ + Adds an agent to the company. + + Args: + agent (Agent): The agent to be added. + + Raises: + ValueError: If an agent with the same ID already exists in the company. + """ + try: + if any( + existing_agent.id == agent.id + for existing_agent in self.agents + ): + raise ValueError( + f"Agent with id {agent.id} already exists in the" + " company." + ) + self.agents.append(agent) + + except Exception as error: + logger.error( + f"[ERROR][CLASS: Company][METHOD: add] {error}" + ) + raise error + + def get(self, agent_name: str) -> Agent: + """ + Retrieves an agent from the company by name. + + Args: + agent_name (str): The name of the agent to retrieve. + + Returns: + Agent: The retrieved agent. + + Raises: + ValueError: If an agent with the specified name does not exist in the company. + """ + try: + for agent in self.agents: + if agent.name == agent_name: + return agent + raise ValueError( + f"Agent with name {agent_name} does not exist in the" + " company." + ) + except Exception as error: + logger.error( + f"[ERROR][CLASS: Company][METHOD: get] {error}" + ) + raise error + + def remove(self, agent: Agent) -> None: + """ + Removes an agent from the company. + + Args: + agent (Agent): The agent to be removed. + """ + self.agents.remove(agent) + + def _parse_org_chart( + self, org_chart: Union[List[Agent], List[List[Agent]]] + ) -> None: + """ + Parses the organization chart and adds agents to the company. + + Args: + org_chart (Union[List[Agent], List[List[Agent]]]): The organization chart + representing the hierarchy of agents. + + Raises: + ValueError: If more than one CEO is found in the org chart or if an invalid + agent is encountered. + + """ + try: + for node in org_chart: + if isinstance(node, Agent): + if self.ceo: + raise ValueError("1 CEO is only allowed") + self.ceo = node + self.add(node) + + elif isinstance(node, list): + for agent in node: + if not isinstance(agent, Agent): + raise ValueError( + "Invalid agent in org chart" + ) + self.add(agent) + + for i, agent in enumerate(node): + if i == len(node) - 1: + continue + + for other_agent in node[i + 1]: + self.__init_task(agent, other_agent) + except Exception as error: + logger.error( + "[ERROR][CLASS: Company][METHOD: _parse_org_chart]" + f" {error}" + ) + raise error + + def _init_interaction( + self, + agent1: Agent, + agent2: Agent, + ) -> None: + if agent1.ai_name not in self.agents_interactions: + self.agents_interactions[agent1.ai_name] = [] + self.agents_interactions[agent1.ai_name].append( + agent2.ai_name + ) + + def run(self): + """ + Run the company + """ + for ( + agent_name, + interaction_agents, + ) in self.agents_interactions.items(): + agent = self.get(agent_name) + for interaction_agent in interaction_agents: + task_description = ( + f"Task for {agent_name} to interact with" + f" {interaction_agent}" + ) + print(f"{task_description} is being executed") + agent.run(task_description) diff --git a/swarms/structs/swarming_architectures.py b/swarms/structs/swarming_architectures.py index a6ccdf9b..ad3ad4ed 100644 --- a/swarms/structs/swarming_architectures.py +++ b/swarms/structs/swarming_architectures.py @@ -1,6 +1,8 @@ import math from typing import List from swarms.structs.agent import Agent +import asyncio +from swarms.utils.logger import logger def circular_swarm(agents: List[Agent], tasks: List[str]): @@ -159,3 +161,95 @@ def sinusoidal_swarm(agents: List[Agent], task: str): for i in range(len(agents)): index = int((math.sin(i) + 1) / 2 * len(agents)) agents[index].run(task) + + +async def one_to_three(sender: Agent, agents: List[Agent], task: str): + """ + Sends a message from the sender agent to three other agents. + + Args: + sender (Agent): The agent sending the message. + agents (List[Agent]): The list of agents to receive the message. + task (str): The message to be sent. + + Raises: + Exception: If there is an error while sending the message. + + Returns: + None + """ + try: + receive_tasks = [] + for agent in agents: + receive_tasks.append( + agent.receive_message(sender.ai_name, task) + ) + + await asyncio.gather(*receive_tasks) + except Exception as error: + logger.error( + f"[ERROR][CLASS: Agent][METHOD: one_to_three] {error}" + ) + raise error + + +async def broadcast( + sender: Agent, + agents: List[Agent], + task: str, +): + """ + Broadcasts a message from the sender agent to a list of agents. + + Args: + sender (Agent): The agent sending the message. + agents (List[Agent]): The list of agents to receive the message. + task (str): The message to be broadcasted. + + Raises: + Exception: If an error occurs during the broadcast. + + Returns: + None + """ + try: + receive_tasks = [] + for agent in agents: + receive_tasks.append( + agent.receive_message(sender.ai_name, task) + ) + + await asyncio.gather(*receive_tasks) + except Exception as error: + logger.error( + f"[ERROR][CLASS: Agent][METHOD: broadcast] {error}" + ) + raise error + + +async def one_to_one( + sender: Agent, + receiver: Agent, + task: str, +): + """ + Sends a message from the sender agent to the receiver agent. + + Args: + sender (Agent): The agent sending the message. + receiver (Agent): The agent to receive the message. + task (str): The message to be sent. + + Raises: + Exception: If an error occurs during the message sending. + + Returns: + None + """ + try: + await receiver.receive_message(sender.ai_name, task) + except Exception as error: + logger.error( + f"[ERROR][CLASS: Agent][METHOD: one_to_one] {error}" + ) + raise error From a403ed3c3b5993ccd75c961817273cf3ea046f43 Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 23 Jan 2024 13:52:27 -0500 Subject: [PATCH 042/103] [BUFG][Worker] --- company_example.py | 38 +++++++++++++++++++ pyproject.toml | 2 +- swarms/agents/__init__.py | 4 +- swarms/structs/company.py | 29 ++++++++++++--- tests/structs/test_company.py | 70 +++++++++++++++++++++++++++++++++++ 5 files changed, 135 insertions(+), 8 deletions(-) create mode 100644 company_example.py create mode 100644 tests/structs/test_company.py diff --git a/company_example.py b/company_example.py new file mode 100644 index 00000000..df2d2506 --- /dev/null +++ b/company_example.py @@ -0,0 +1,38 @@ +# Example + +import os + +from dotenv import load_dotenv + +from swarms import Agent, OpenAIChat +from swarms.structs.company import Company + +load_dotenv() + +llm = OpenAIChat( + openai_api_key=os.getenv("OPENAI_API_KEY"), max_tokens=4000 +) + +ceo = Agent(llm=llm, ai_name="CEO") +dev = Agent(llm=llm, ai_name="Developer") +va = Agent(llm=llm, ai_name="VA") + +# Create a company +company = Company( + org_chart = [[dev, va]], + shared_instructions="Do your best", + ceo=ceo, +) + +# Add agents to the company +hr = Agent(llm=llm, name="HR") +company.add(hr) + +# Get an agent from the company +hr = company.get("CEO") + +# Remove an agent from the company +company.remove(hr) + +# Run the company +company.run() diff --git a/pyproject.toml b/pyproject.toml index 22f09946..7cb65d39 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.7.5" +version = "3.7.6" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/agents/__init__.py b/swarms/agents/__init__.py index 9c855436..4b786bf4 100644 --- a/swarms/agents/__init__.py +++ b/swarms/agents/__init__.py @@ -14,7 +14,7 @@ from swarms.agents.stopping_conditions import ( check_success, ) from swarms.agents.tool_agent import ToolAgent -from swarms.agents.worker_agent import WorkerAgent +from swarms.agents.worker_agent import Worker __all__ = [ "AbstractAgent", @@ -31,5 +31,5 @@ __all__ = [ "check_cancelled", "check_exit", "check_end", - "WorkerAgent", + "Worker", ] diff --git a/swarms/structs/company.py b/swarms/structs/company.py index b01c0a00..80fe3eef 100644 --- a/swarms/structs/company.py +++ b/swarms/structs/company.py @@ -1,21 +1,23 @@ -from typing import List, Optional, Union, Dict from dataclasses import dataclass, field +from typing import Dict, List, Optional, Union + from swarms.structs.agent import Agent from swarms.utils.logger import logger - +from swarms.structs.conversation import Conversation @dataclass class Company: """ Represents a company with a hierarchical organizational structure. """ - org_chart: Union[List[Agent], List[List[Agent]]] + org_chart: List[List[Agent]] shared_instructions: str = None ceo: Optional[Agent] = None agents: List[Agent] = field(default_factory=list) agent_interactions: Dict[str, List[str]] = field( default_factory=dict ) + history: Conversation = field(default_factory=Conversation) def __post_init__(self): self._parse_org_chart(self.org_chart) @@ -81,7 +83,13 @@ class Company: Args: agent (Agent): The agent to be removed. """ - self.agents.remove(agent) + try: + self.agents.remove(agent) + except Exception as error: + logger.error( + f"[ERROR][CLASS: Company][METHOD: remove] {error}" + ) + raise error def _parse_org_chart( self, org_chart: Union[List[Agent], List[List[Agent]]] @@ -96,7 +104,6 @@ class Company: Raises: ValueError: If more than one CEO is found in the org chart or if an invalid agent is encountered. - """ try: for node in org_chart: @@ -132,6 +139,16 @@ class Company: agent1: Agent, agent2: Agent, ) -> None: + """ + Initializes the interaction between two agents. + + Args: + agent1 (Agent): The first agent involved in the interaction. + agent2 (Agent): The second agent involved in the interaction. + + Returns: + None + """ if agent1.ai_name not in self.agents_interactions: self.agents_interactions[agent1.ai_name] = [] self.agents_interactions[agent1.ai_name].append( @@ -154,3 +171,5 @@ class Company: ) print(f"{task_description} is being executed") agent.run(task_description) + + diff --git a/tests/structs/test_company.py b/tests/structs/test_company.py new file mode 100644 index 00000000..0b1ec105 --- /dev/null +++ b/tests/structs/test_company.py @@ -0,0 +1,70 @@ +import pytest +from swarms.structs.agent import Agent +from swarms.structs.company import Company +from swarms import OpenAIChat + +# Mock OpenAIChat instance +llm = OpenAIChat(openai_api_key="test_key", max_tokens=4000) + +# Mock Agents +ceo = Agent(llm=llm, name="CEO") +dev = Agent(llm=llm, name="Developer") +va = Agent(llm=llm, name="VA") +hr = Agent(llm=llm, name="HR") +shared_instructions = "Listen to your boss" + + +def test_add_agent(): + company = Company( + org_chart=[[ceo, [dev, va]]], + shared_instructions=shared_instructions, + ) + company.add(hr) + assert hr in company.agents + + +def test_get_agent(): + company = Company( + org_chart=[[ceo, [dev, va]]], + shared_instructions=shared_instructions, + ) + company.add(hr) + assert company.get("HR") == hr + + +def test_remove_agent(): + company = Company( + org_chart=[[ceo, [dev, va]]], + shared_instructions=shared_instructions, + ) + company.add(hr) + company.remove(hr) + assert hr not in company.agents + + +def test_add_existing_agent(): + company = Company( + org_chart=[[ceo, [dev, va]]], + shared_instructions=shared_instructions, + ) + company.add(hr) + with pytest.raises(ValueError): + company.add(hr) + + +def test_get_nonexistent_agent(): + company = Company( + org_chart=[[ceo, [dev, va]]], + shared_instructions=shared_instructions, + ) + with pytest.raises(ValueError): + company.get("Nonexistent") + + +def test_remove_nonexistent_agent(): + company = Company( + org_chart=[[ceo, [dev, va]]], + shared_instructions=shared_instructions, + ) + with pytest.raises(ValueError): + company.remove(hr) From 7472207d6579f1595ea3480fadb6f80012bd3bb8 Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 23 Jan 2024 13:54:10 -0500 Subject: [PATCH 043/103] [FEAT] --- company_example.py => playground/structs/company_example.py | 0 pyproject.toml | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename company_example.py => playground/structs/company_example.py (100%) diff --git a/company_example.py b/playground/structs/company_example.py similarity index 100% rename from company_example.py rename to playground/structs/company_example.py diff --git a/pyproject.toml b/pyproject.toml index 7cb65d39..a6e3e64c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.7.6" +version = "3.7.7" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] From 403bed61fe01e5023761bc336cab8843122bb72c Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 23 Jan 2024 15:22:19 -0500 Subject: [PATCH 044/103] [BUFG][Odin] --- playground/structs/company_example.py | 2 +- pyproject.toml | 1 + requirements.txt | 3 +- swarms/models/medical_sam.py | 140 ++++++++++++++++++++++ swarms/models/odin.py | 52 ++++---- swarms/structs/company.py | 4 +- swarms/utils/download_weights_from_url.py | 19 +++ 7 files changed, 194 insertions(+), 27 deletions(-) create mode 100644 swarms/models/medical_sam.py create mode 100644 swarms/utils/download_weights_from_url.py diff --git a/playground/structs/company_example.py b/playground/structs/company_example.py index df2d2506..72396c61 100644 --- a/playground/structs/company_example.py +++ b/playground/structs/company_example.py @@ -19,7 +19,7 @@ va = Agent(llm=llm, ai_name="VA") # Create a company company = Company( - org_chart = [[dev, va]], + org_chart=[[dev, va]], shared_instructions="Do your best", ceo=ceo, ) diff --git a/pyproject.toml b/pyproject.toml index a6e3e64c..fccb186b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,6 +73,7 @@ psutil = "*" ultralytics = "*" timm = "*" supervision = "*" +scikit-image = "*" diff --git a/requirements.txt b/requirements.txt index d7befb85..ab78bb36 100644 --- a/requirements.txt +++ b/requirements.txt @@ -61,4 +61,5 @@ pre-commit==3.2.2 peft psutil ultralytics -supervision \ No newline at end of file +supervision +scikit-image \ No newline at end of file diff --git a/swarms/models/medical_sam.py b/swarms/models/medical_sam.py new file mode 100644 index 00000000..01e77c04 --- /dev/null +++ b/swarms/models/medical_sam.py @@ -0,0 +1,140 @@ +import os +from dataclasses import dataclass +from typing import Tuple + +import numpy as np +import requests +import torch +import torch.nn.functional as F +from skimage import transform +from torch import Tensor + + +@dataclass +class MedicalSAM: + """ + MedicalSAM class for performing semantic segmentation on medical images using the SAM model. + + Attributes: + model_path (str): The file path to the model weights. + device (str): The device to run the model on (default is "cuda:0"). + model_weights_url (str): The URL to download the model weights from. + + Methods: + __post_init__(): Initializes the MedicalSAM object. + download_model_weights(model_path: str): Downloads the model weights from the specified URL and saves them to the given file path. + preprocess(img): Preprocesses the input image. + run(img, box): Runs the semantic segmentation on the input image within the specified bounding box. + + """ + + model_path: str + device: str = "cuda:0" + model_weights_url: str = "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth" + + def __post_init__(self): + if not os.path.exists(self.model_path): + self.download_model_weights(self.model_path) + + self.model = sam_model_registry["vit_b"]( + checkpoint=self.model_path + ) + self.model = self.model.to(self.device) + self.model.eval() + + def download_model_weights(self, model_path: str): + """ + Downloads the model weights from the specified URL and saves them to the given file path. + + Args: + model_path (str): The file path where the model weights will be saved. + + Raises: + Exception: If the model weights fail to download. + """ + response = requests.get(self.model_weights_url, stream=True) + if response.status_code == 200: + with open(model_path, "wb") as f: + f.write(response.content) + else: + raise Exception("Failed to download model weights.") + + def preprocess(self, img: np.ndarray) -> Tuple[Tensor, int, int]: + """ + Preprocesses the input image. + + Args: + img: The input image. + + Returns: + img_tensor: The preprocessed image tensor. + H: The original height of the image. + W: The original width of the image. + """ + if len(img.shape) == 2: + img = np.repeat(img[:, :, None], 3, axis=-1) + H, W, _ = img.shape + img = transform.resize( + img, + (1024, 1024), + order=3, + preserve_range=True, + anti_aliasing=True, + ).astype(np.uint8) + img = img - img.min() / np.clip( + img.max() - img.min(), a_min=1e-8, a_max=None + ) + img = torch.tensor(img).float().permute(2, 0, 1).unsqueeze(0) + return img, H, W + + @torch.no_grad() + def run(self, img: np.ndarray, box: np.ndarray) -> np.ndarray: + """ + Runs the semantic segmentation on the input image within the specified bounding box. + + Args: + img: The input image. + box: The bounding box coordinates (x1, y1, x2, y2). + + Returns: + medsam_seg: The segmented image. + """ + img_tensor, H, W = self.preprocess(img) + img_tensor = img_tensor.to(self.device) + box_1024 = box / np.array([W, H, W, H]) * 1024 + img = self.model.image_encoder(img_tensor) + + box_torch = torch.as_tensor( + box_1024, dtype=torch.float, device=img_tensor.device + ) + + if len(box_torch.shape) == 2: + box_torch = box_torch[:, None, :] + + sparse_embeddings, dense_embeddings = ( + self.model.prompt_encoder( + points=None, + boxes=box_torch, + masks=None, + ) + ) + + low_res_logits, _ = self.model.mask_decoder( + image_embeddings=img, + image_pe=self.model.prompt_encoder.get_dense_pe(), + sparse_prompt_embeddings=sparse_embeddings, + dense_prompt_embeddings=dense_embeddings, + multimask_output=False, + ) + + low_res_pred = torch.sigmoid(low_res_logits) + low_res_pred = F.interpolate( + low_res_pred, + size=(H, W), + mode="bilinear", + align_corners=False, + ) + low_res_pred = low_res_pred.squeeze().cpu().numpy() + medsam_seg = (low_res_pred > 0.5).astype(np.uint8) + + return medsam_seg diff --git a/swarms/models/odin.py b/swarms/models/odin.py index 1ab09893..a6228159 100644 --- a/swarms/models/odin.py +++ b/swarms/models/odin.py @@ -1,47 +1,53 @@ +import os import supervision as sv -from ultraanalytics import YOLO +from ultralytics import YOLO from tqdm import tqdm from swarms.models.base_llm import AbstractLLM - +from swarms.utils.download_weights_from_url import download_weights_from_url class Odin(AbstractLLM): """ Odin class represents an object detection and tracking model. - Args: - source_weights_path (str): Path to the weights file for the object detection model. - source_video_path (str): Path to the source video file. - target_video_path (str): Path to save the output video file. - confidence_threshold (float): Confidence threshold for object detection. - iou_threshold (float): Intersection over Union (IoU) threshold for object detection. - Attributes: - source_weights_path (str): Path to the weights file for the object detection model. - source_video_path (str): Path to the source video file. - target_video_path (str): Path to save the output video file. - confidence_threshold (float): Confidence threshold for object detection. - iou_threshold (float): Intersection over Union (IoU) threshold for object detection. + source_weights_path (str): The file path to the YOLO model weights. + confidence_threshold (float): The confidence threshold for object detection. + iou_threshold (float): The intersection over union (IOU) threshold for object detection. + + Example: + >>> odin = Odin( + ... source_weights_path="yolo.weights", + ... confidence_threshold=0.3, + ... iou_threshold=0.7, + ... ) + >>> odin.run(video="input.mp4") + + """ def __init__( self, - source_weights_path: str = None, - target_video_path: str = None, + source_weights_path: str = "yolo.weights", confidence_threshold: float = 0.3, iou_threshold: float = 0.7, ): super(Odin, self).__init__() self.source_weights_path = source_weights_path - self.target_video_path = target_video_path self.confidence_threshold = confidence_threshold self.iou_threshold = iou_threshold + + if not os.path.exists(self.source_weights_path): + download_weights_from_url( + url=source_weights_path, save_path=self.source_weights_path + ) + - def run(self, video_path: str, *args, **kwargs): + def run(self, video: str, *args, **kwargs): """ Runs the object detection and tracking algorithm on the specified video. Args: - video_path (str): The path to the input video file. + video (str): The path to the input video file. *args: Additional positional arguments. **kwargs: Additional keyword arguments. @@ -53,14 +59,14 @@ class Odin(AbstractLLM): tracker = sv.ByteTrack() box_annotator = sv.BoxAnnotator() frame_generator = sv.get_video_frames_generator( - source_path=self.source_video_path + source_path=self.source_video ) - video_info = sv.VideoInfo.from_video_path( - video_path=video_path + video_info = sv.VideoInfo.from_video( + video=video ) with sv.VideoSink( - target_path=self.target_video_path, video_info=video_info + target_path=self.target_video, video_info=video_info ) as sink: for frame in tqdm( frame_generator, total=video_info.total_frames diff --git a/swarms/structs/company.py b/swarms/structs/company.py index 80fe3eef..11b6d61f 100644 --- a/swarms/structs/company.py +++ b/swarms/structs/company.py @@ -5,11 +5,13 @@ from swarms.structs.agent import Agent from swarms.utils.logger import logger from swarms.structs.conversation import Conversation + @dataclass class Company: """ Represents a company with a hierarchical organizational structure. """ + org_chart: List[List[Agent]] shared_instructions: str = None ceo: Optional[Agent] = None @@ -171,5 +173,3 @@ class Company: ) print(f"{task_description} is being executed") agent.run(task_description) - - diff --git a/swarms/utils/download_weights_from_url.py b/swarms/utils/download_weights_from_url.py new file mode 100644 index 00000000..bc93d699 --- /dev/null +++ b/swarms/utils/download_weights_from_url.py @@ -0,0 +1,19 @@ +import requests + +def download_weights_from_url(url: str, save_path: str = "models/weights.pth"): + """ + Downloads model weights from the given URL and saves them to the specified path. + + Args: + url (str): The URL from which to download the model weights. + save_path (str, optional): The path where the downloaded weights should be saved. + Defaults to "models/weights.pth". + """ + response = requests.get(url, stream=True) + response.raise_for_status() + + with open(save_path, "wb") as f: + for chunk in response.iter_content(chunk_size=8192): + f.write(chunk) + + print(f"Model weights downloaded and saved to {save_path}") \ No newline at end of file From aea843437e646fb89b50e2969d343d6d4b3b91ed Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 23 Jan 2024 17:42:49 -0500 Subject: [PATCH 045/103] [FEAT][QwenVLMultiModal] --- swarms/models/__init__.py | 7 +- swarms/models/llava.py | 134 +++++++++++----------- swarms/models/medical_sam.py | 4 + swarms/models/odin.py | 21 ++-- swarms/models/qwen.py | 108 +++++++++++++++++ swarms/models/vip_llava.py | 94 +++++++++++++++ swarms/utils/__init__.py | 5 +- swarms/utils/download_weights_from_url.py | 15 ++- tests/models/test_qwen.py | 60 ++++++++++ 9 files changed, 363 insertions(+), 85 deletions(-) create mode 100644 swarms/models/qwen.py create mode 100644 swarms/models/vip_llava.py create mode 100644 tests/models/test_qwen.py diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 364d1d7f..635124a6 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -44,7 +44,9 @@ from swarms.models.timm import TimmModel # noqa: E402 from swarms.models.ultralytics_model import ( UltralyticsModel, ) # noqa: E402 - +from swarms.models.vip_llava import VipLlavaMultiModal # noqa: E402 +from swarms.models.llava import LavaMultiModal # noqa: E402 +from swarms.models.qwen import QwenVLMultiModal # noqa: E402 # from swarms.models.dalle3 import Dalle3 # from swarms.models.distilled_whisperx import DistilWhisperModel # noqa: E402 @@ -105,4 +107,7 @@ __all__ = [ "TogetherLLM", "TimmModel", "UltralyticsModel", + "VipLlavaMultiModal", + "LavaMultiModal", + "QwenVLMultiModal", ] diff --git a/swarms/models/llava.py b/swarms/models/llava.py index 605904c3..bcc1b09f 100644 --- a/swarms/models/llava.py +++ b/swarms/models/llava.py @@ -1,82 +1,82 @@ -from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline +import requests +from PIL import Image +from transformers import AutoProcessor, LlavaForConditionalGeneration +from typing import Tuple, Union +from io import BytesIO +from swarms.models.base_multimodal_model import BaseMultiModalModel -class MultiModalLlava: +class LavaMultiModal(BaseMultiModalModel): """ - LLava Model + A class to handle multi-modal inputs (text and image) using the Llava model for conditional generation. + + Attributes: + model_name (str): The name or path of the pre-trained model. + max_length (int): The maximum length of the generated sequence. Args: - model_name_or_path: The model name or path to the model - revision: The revision of the model to use - device: The device to run the model on - max_new_tokens: The maximum number of tokens to generate - do_sample: Whether or not to use sampling - temperature: The temperature of the sampling - top_p: The top p value for sampling - top_k: The top k value for sampling - repetition_penalty: The repetition penalty for sampling - device_map: The device map to use + model_name (str): The name of the pre-trained model. + max_length (int): The maximum length of the generated sequence. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. - Methods: - __call__: Call the model - chat: Interactive chat in terminal + Examples: + >>> model = LavaMultiModal() + >>> model.run("A cat", "https://example.com/cat.jpg") - Example: - >>> from swarms.models.llava import LlavaModel - >>> model = LlavaModel(device="cpu") - >>> model("Hello, I am a robot.") """ def __init__( self, - model_name_or_path="TheBloke/llava-v1.5-13B-GPTQ", - revision="main", - device="cuda", - max_new_tokens=512, - do_sample=True, - temperature=0.7, - top_p=0.95, - top_k=40, - repetition_penalty=1.1, - device_map: str = "auto", - ): - self.device = device - self.model = AutoModelForCausalLM.from_pretrained( - model_name_or_path, - device_map=device_map, - trust_remote_code=False, - revision=revision, - ).to(self.device) + model_name: str = "llava-hf/llava-1.5-7b-hf", + max_length: int = 30, + *args, + **kwargs, + ) -> None: + super().__init__(*args, **kwargs) + self.model_name = model_name + self.max_length = max_length - self.tokenizer = AutoTokenizer.from_pretrained( - model_name_or_path, use_fast=True - ) - self.pipe = pipeline( - "text-generation", - model=self.model, - tokenizer=self.tokenizer, - max_new_tokens=max_new_tokens, - do_sample=do_sample, - temperature=temperature, - top_p=top_p, - top_k=top_k, - repetition_penalty=repetition_penalty, - device=0 if self.device == "cuda" else -1, + self.model = LlavaForConditionalGeneration.from_pretrained( + model_name, *args, **kwargs ) + self.processor = AutoProcessor.from_pretrained(model_name) - def __call__(self, prompt): - """Call the model""" - return self.pipe(prompt)[0]["generated_text"] + def run( + self, text: str, img: str, *args, **kwargs + ) -> Union[str, Tuple[None, str]]: + """ + Processes the input text and image, and generates a response. - def chat(self): - """Interactive chat in terminal""" - print( - "Starting chat with LlavaModel. Type 'exit' to end the" - " session." - ) - while True: - user_input = input("You: ") - if user_input.lower() == "exit": - break - response = self(user_input) - print(f"Model: {response}") + Args: + text (str): The input text for the model. + img (str): The URL of the image to process. + max_length (int): The maximum length of the generated sequence. + + Returns: + Union[str, Tuple[None, str]]: The generated response string or a tuple (None, error message) in case of an error. + """ + try: + response = requests.get(img, stream=True) + response.raise_for_status() + image = Image.open(BytesIO(response.content)) + + inputs = self.processor( + text=text, images=image, return_tensors="pt" + ) + + # Generate + generate_ids = self.model.generate( + **inputs, max_length=self.max_length, **kwargs + ) + return self.processor.batch_decode( + generate_ids, + skip_special_tokens=True, + clean_up_tokenization_spaces=False, + *args, + )[0] + + except requests.RequestException as e: + return None, f"Error fetching image: {str(e)}" + except Exception as e: + return None, f"Error during model processing: {str(e)}" diff --git a/swarms/models/medical_sam.py b/swarms/models/medical_sam.py index 01e77c04..8d096ba5 100644 --- a/swarms/models/medical_sam.py +++ b/swarms/models/medical_sam.py @@ -10,6 +10,10 @@ from skimage import transform from torch import Tensor +def sam_model_registry(): + pass + + @dataclass class MedicalSAM: """ diff --git a/swarms/models/odin.py b/swarms/models/odin.py index a6228159..27cb1710 100644 --- a/swarms/models/odin.py +++ b/swarms/models/odin.py @@ -3,7 +3,10 @@ import supervision as sv from ultralytics import YOLO from tqdm import tqdm from swarms.models.base_llm import AbstractLLM -from swarms.utils.download_weights_from_url import download_weights_from_url +from swarms.utils.download_weights_from_url import ( + download_weights_from_url, +) + class Odin(AbstractLLM): """ @@ -13,7 +16,7 @@ class Odin(AbstractLLM): source_weights_path (str): The file path to the YOLO model weights. confidence_threshold (float): The confidence threshold for object detection. iou_threshold (float): The intersection over union (IOU) threshold for object detection. - + Example: >>> odin = Odin( ... source_weights_path="yolo.weights", @@ -21,8 +24,8 @@ class Odin(AbstractLLM): ... iou_threshold=0.7, ... ) >>> odin.run(video="input.mp4") - - + + """ def __init__( @@ -35,12 +38,12 @@ class Odin(AbstractLLM): self.source_weights_path = source_weights_path self.confidence_threshold = confidence_threshold self.iou_threshold = iou_threshold - + if not os.path.exists(self.source_weights_path): download_weights_from_url( - url=source_weights_path, save_path=self.source_weights_path + url=source_weights_path, + save_path=self.source_weights_path, ) - def run(self, video: str, *args, **kwargs): """ @@ -61,9 +64,7 @@ class Odin(AbstractLLM): frame_generator = sv.get_video_frames_generator( source_path=self.source_video ) - video_info = sv.VideoInfo.from_video( - video=video - ) + video_info = sv.VideoInfo.from_video(video=video) with sv.VideoSink( target_path=self.target_video, video_info=video_info diff --git a/swarms/models/qwen.py b/swarms/models/qwen.py new file mode 100644 index 00000000..1533b117 --- /dev/null +++ b/swarms/models/qwen.py @@ -0,0 +1,108 @@ +from dataclasses import dataclass, field +from typing import Optional, Tuple + +from PIL import Image +from transformers import AutoModelForCausalLM, AutoTokenizer + +from swarms.models.base_multimodal_model import BaseMultiModalModel + + +@dataclass +class QwenVLMultiModal(BaseMultiModalModel): + """ + QwenVLMultiModal is a class that represents a multi-modal model for Qwen chatbot. + It inherits from the BaseMultiModalModel class. + + Examples: + >>> model = QwenVLMultiModal() + >>> model.run("Hello, how are you?", "https://example.com/image.jpg") + + """ + + model_name: str = "Qwen/Qwen-VL-Chat" + device: str = "cuda" + args: tuple = field(default_factory=tuple) + kwargs: dict = field(default_factory=dict) + quantize: bool = False + + def __post_init__(self): + """ + Initializes the QwenVLMultiModal object. + It initializes the tokenizer and the model for the Qwen chatbot. + """ + + if self.quantize: + self.model_name = "Qwen/Qwen-VL-Chat-Int4" + + self.tokenizer = AutoTokenizer.from_pretrained( + self.model_name, trust_remote_code=True + ) + self.model = AutoModelForCausalLM.from_pretrained( + self.model_name, + device_map=self.device, + trust_remote_code=True, + ).eval() + + def run( + self, text: str, img: str, *args, **kwargs + ) -> Tuple[Optional[str], Optional[Image.Image]]: + """ + Runs the Qwen chatbot model on the given text and image inputs. + + Args: + text (str): The input text for the chatbot. + img (str): The input image for the chatbot. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + + Returns: + Tuple[Optional[str], Optional[Image.Image]]: A tuple containing the response generated by the chatbot + and the image associated with the response (if any). + """ + try: + query = self.tokenizer.from_list_format( + [ + {"image": img, "text": text}, + ] + ) + + inputs = self.tokenizer(query, return_tensors="pt") + inputs = inputs.to(self.model.device) + pred = self.model.generate(**inputs) + response = self.tokenizer.decode( + pred.cpu()[0], skip_special_tokens=False + ) + return response + except Exception as error: + print(f"[ERROR]: [QwenVLMultiModal]: {error}") + + def chat( + self, text: str, img: str, *args, **kwargs + ) -> tuple[str, list]: + """ + Chat with the model using text and image inputs. + + Args: + text (str): The text input for the chat. + img (str): The image input for the chat. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + + Returns: + tuple[str, list]: A tuple containing the response and chat history. + + Raises: + Exception: If an error occurs during the chat. + + """ + try: + response, history = self.model.chat( + self.tokenizer, + query=f"{img}这是什么", + history=None, + ) + return response, history + except Exception as e: + raise Exception( + "An error occurred during the chat." + ) from e diff --git a/swarms/models/vip_llava.py b/swarms/models/vip_llava.py new file mode 100644 index 00000000..31726275 --- /dev/null +++ b/swarms/models/vip_llava.py @@ -0,0 +1,94 @@ +from io import BytesIO + +import requests +import torch +from PIl import Image +from transformers import ( + AutoProcessor, + VipLlavaForConditionalGeneration, +) + +from swarms.models.base_multimodal_model import BaseMultiModalModel + + +class VipLlavaMultiModal(BaseMultiModalModel): + """ + A multi-modal model for VIP-LLAVA. + + Args: + model_name (str): The name or path of the pre-trained model. + max_new_tokens (int): The maximum number of new tokens to generate. + device_map (str): The device mapping for the model. + torch_dtype: The torch data type for the model. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + """ + + def __init__( + self, + model_name: str = "llava-hf/vip-llava-7b-hf", + max_new_tokens: int = 500, + device_map: str = "auto", + torch_dtype=torch.float16, + *args, + **kwargs, + ): + super().__init__(*args, **kwargs) + self.model_name = model_name + self.max_new_tokens = max_new_tokens + self.device_map = device_map + self.torch_dtype = torch_dtype + + self.model = VipLlavaForConditionalGeneration.from_pretrained( + model_name, + device_map=device_map, + torch_dtype=torch_dtype, + *args, + **kwargs, + ) + self.processor = AutoProcessor.from_pretrained( + model_name, *args, **kwargs + ) + + def run(self, text: str, img: str, *args, **kwargs): + """ + Run the VIP-LLAVA model. + + Args: + text (str): The input text. + img (str): The URL of the input image. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + + Returns: + str: The generated output text. + tuple: A tuple containing None and the error message if an error occurs. + """ + try: + response = requests.get(img, stream=True) + response.raise_for_status() + image = Image.open(BytesIO(response.content)) + + inputs = self.processor( + text=text, + images=image, + return_tensors="pt", + *args, + **kwargs, + ).to(0, self.torch_dtype) + + # Generate + generate_ids = self.model.generate( + **inputs, max_new_tokens=self.max_new_tokens, **kwargs + ) + + return self.processor.decode( + generate_ids[0][len(inputs["input_ids"][0]) :], + skip_special_tokens=True, + ) + + except requests.RequestException as error: + return None, f"Error fetching image: {error}" + + except Exception as error: + return None, f"Error during model inference: {error}" diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index e265a1c8..de8ffb57 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -19,7 +19,9 @@ from swarms.utils.data_to_text import ( data_to_text, ) from swarms.utils.try_except_wrapper import try_except_wrapper - +from swarms.utils.download_weights_from_url import ( + download_weights_from_url, +) __all__ = [ "SubprocessCodeInterpreter", @@ -39,4 +41,5 @@ __all__ = [ "txt_to_text", "data_to_text", "try_except_wrapper", + "download_weights_from_url", ] diff --git a/swarms/utils/download_weights_from_url.py b/swarms/utils/download_weights_from_url.py index bc93d699..b5fa1633 100644 --- a/swarms/utils/download_weights_from_url.py +++ b/swarms/utils/download_weights_from_url.py @@ -1,19 +1,22 @@ -import requests +import requests -def download_weights_from_url(url: str, save_path: str = "models/weights.pth"): + +def download_weights_from_url( + url: str, save_path: str = "models/weights.pth" +): """ Downloads model weights from the given URL and saves them to the specified path. Args: url (str): The URL from which to download the model weights. - save_path (str, optional): The path where the downloaded weights should be saved. + save_path (str, optional): The path where the downloaded weights should be saved. Defaults to "models/weights.pth". """ response = requests.get(url, stream=True) response.raise_for_status() - + with open(save_path, "wb") as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk) - - print(f"Model weights downloaded and saved to {save_path}") \ No newline at end of file + + print(f"Model weights downloaded and saved to {save_path}") diff --git a/tests/models/test_qwen.py b/tests/models/test_qwen.py new file mode 100644 index 00000000..28178fc0 --- /dev/null +++ b/tests/models/test_qwen.py @@ -0,0 +1,60 @@ +from unittest.mock import Mock, patch +from swarms.models.qwen import QwenVLMultiModal + + +def test_post_init(): + with patch( + "swarms.models.qwen.AutoTokenizer.from_pretrained" + ) as mock_tokenizer, patch( + "swarms.models.qwen.AutoModelForCausalLM.from_pretrained" + ) as mock_model: + mock_tokenizer.return_value = Mock() + mock_model.return_value = Mock() + + model = QwenVLMultiModal() + mock_tokenizer.assert_called_once_with( + model.model_name, trust_remote_code=True + ) + mock_model.assert_called_once_with( + model.model_name, + device_map=model.device, + trust_remote_code=True, + ) + + +def test_run(): + with patch( + "swarms.models.qwen.AutoTokenizer.from_list_format" + ) as mock_format, patch( + "swarms.models.qwen.AutoTokenizer.__call__" + ) as mock_call, patch( + "swarms.models.qwen.AutoModelForCausalLM.generate" + ) as mock_generate, patch( + "swarms.models.qwen.AutoTokenizer.decode" + ) as mock_decode: + mock_format.return_value = Mock() + mock_call.return_value = Mock() + mock_generate.return_value = Mock() + mock_decode.return_value = "response" + + model = QwenVLMultiModal() + response = model.run( + "Hello, how are you?", "https://example.com/image.jpg" + ) + + assert response == "response" + + +def test_chat(): + with patch( + "swarms.models.qwen.AutoModelForCausalLM.chat" + ) as mock_chat: + mock_chat.return_value = ("response", ["history"]) + + model = QwenVLMultiModal() + response, history = model.chat( + "Hello, how are you?", "https://example.com/image.jpg" + ) + + assert response == "response" + assert history == ["history"] From f781c3b2db04b18bd50944b030bf51e171b18e12 Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 23 Jan 2024 17:43:22 -0500 Subject: [PATCH 046/103] [V] --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index fccb186b..42413124 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.7.7" +version = "3.7.8" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] From 00de2f37bce367d5bd3a05166b211ed47032eb75 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 24 Jan 2024 10:54:28 -0500 Subject: [PATCH 047/103] [README] --- README.md | 25 +++++++++++++++++ playground/models/llava.py | 16 +++++++++++ swarms/utils/agent_ops_wrapper.py | 45 +++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 playground/models/llava.py create mode 100644 swarms/utils/agent_ops_wrapper.py diff --git a/README.md b/README.md index 8f13e2e7..43407822 100644 --- a/README.md +++ b/README.md @@ -910,6 +910,31 @@ cog_agent = CogAgent() # Run the model on the tests cog_agent.run("Describe this scene", "images/1.jpg") +``` + + +### `QwenVLMultiModal` +A radically simple interface for QwenVLMultiModal comes complete with Quantization to turn it on just set quantize to true! + +```python +from swarms import QwenVLMultiModal + +# Instantiate the QwenVLMultiModal model +model = QwenVLMultiModal( + model_name="Qwen/Qwen-VL-Chat", + device="cuda", + quantize=True, +) + +# Run the model +response = model( + "Hello, how are you?", "https://example.com/image.jpg" +) + +# Print the response +print(response) + + ``` ---- diff --git a/playground/models/llava.py b/playground/models/llava.py new file mode 100644 index 00000000..561b6f88 --- /dev/null +++ b/playground/models/llava.py @@ -0,0 +1,16 @@ +from swarms import QwenVLMultiModal + +# Instantiate the QwenVLMultiModal model +model = QwenVLMultiModal( + model_name="Qwen/Qwen-VL-Chat", + device="cuda", + quantize=True, +) + +# Run the model +response = model( + "Hello, how are you?", "https://example.com/image.jpg" +) + +# Print the response +print(response) diff --git a/swarms/utils/agent_ops_wrapper.py b/swarms/utils/agent_ops_wrapper.py new file mode 100644 index 00000000..c1a019d2 --- /dev/null +++ b/swarms/utils/agent_ops_wrapper.py @@ -0,0 +1,45 @@ +import logging +import agentops + +class AgentOpsWrapper: + """ + A wrapper for the AgentOps client that adds error handling and logging. + """ + + def __init__(self, api_key): + """ + Initialize the AgentOps client with the given API key. + """ + self.client = agentops.Client(api_key) + self.logger = logging.getLogger(__name__) + self.logger.setLevel(logging.INFO) + + def record_action(self, action_name): + """ + Record an action with the given name. + """ + def decorator(func): + def wrapper(*args, **kwargs): + try: + self.client.record_action(action_name) + result = func(*args, **kwargs) + self.logger.info(f"Action {action_name} completed successfully.") + return result + except Exception as e: + self.logger.error(f"Error while recording action {action_name}: {e}") + raise + return wrapper + return decorator + + def end_session(self, status): + """ + End the session with the given status. + """ + try: + self.client.end_session(status) + self.logger.info(f"Session ended with status {status}.") + except Exception as e: + self.logger.error(f"Error while ending session: {e}") + raise + +# agentops = AgentOpsWrapper(api_key) \ No newline at end of file From 5b7f404842e38196b848b27980b1c8dae8ec12f0 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 24 Jan 2024 12:44:25 -0500 Subject: [PATCH 048/103] [FEAT][Telemetry] --- playground/structs/sequential_workflow.py | 6 +- swarms/__init__.py | 12 +++ swarms/models/vip_llava.py | 2 +- swarms/telemetry/__init__.py | 8 +- swarms/telemetry/main.py | 58 ++++++++++++++ swarms/telemetry/sys_info.py | 95 +++++------------------ swarms/telemetry/user_utils.py | 28 ++++++- swarms/utils/__init__.py | 3 + swarms/utils/agent_ops_wrapper.py | 45 ----------- swarms/utils/save_logs.py | 46 +++++++++++ 10 files changed, 174 insertions(+), 129 deletions(-) create mode 100644 swarms/telemetry/main.py delete mode 100644 swarms/utils/agent_ops_wrapper.py create mode 100644 swarms/utils/save_logs.py diff --git a/playground/structs/sequential_workflow.py b/playground/structs/sequential_workflow.py index 5f60db72..7fa110bc 100644 --- a/playground/structs/sequential_workflow.py +++ b/playground/structs/sequential_workflow.py @@ -43,6 +43,6 @@ workflow.add(tasks=[task1, task2]) # Run the workflow workflow.run() -# Output the results -for task in workflow.tasks: - print(f"Task: {task.description}, Result: {task.result}") +# # Output the results +# for task in workflow.tasks: +# print(f"Task: {task.description}, Result: {task.result}") diff --git a/swarms/__init__.py b/swarms/__init__.py index 4e6785cb..54a60596 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -1,3 +1,4 @@ +# from swarms.telemetry.main import Telemetry # noqa: E402, F403 from swarms.telemetry.bootup import bootup # noqa: E402, F403 bootup() @@ -8,3 +9,14 @@ from swarms.models import * # noqa: E402, F403 from swarms.telemetry import * # noqa: E402, F403 from swarms.utils import * # noqa: E402, F403 from swarms.prompts import * # noqa: E402, F403 + + +# telemetry = Telemetry('mongodb://localhost:27017/', 'mydatabase') + +# telemetry.log_import('swarms.telemetry.bootup') +# telemetry.log_import('swarms.agents') +# telemetry.log_import('swarms.structs') +# telemetry.log_import('swarms.models') +# telemetry.log_import('swarms.telemetry') +# telemetry.log_import('swarms.utils') +# telemetry.log_import('swarms.prompts') diff --git a/swarms/models/vip_llava.py b/swarms/models/vip_llava.py index 31726275..db532913 100644 --- a/swarms/models/vip_llava.py +++ b/swarms/models/vip_llava.py @@ -2,7 +2,7 @@ from io import BytesIO import requests import torch -from PIl import Image +from PIL import Image from transformers import ( AutoProcessor, VipLlavaForConditionalGeneration, diff --git a/swarms/telemetry/__init__.py b/swarms/telemetry/__init__.py index 0a16ca28..d829b724 100644 --- a/swarms/telemetry/__init__.py +++ b/swarms/telemetry/__init__.py @@ -1,13 +1,12 @@ from swarms.telemetry.log_all import log_all_calls, log_calls from swarms.telemetry.sys_info import ( get_cpu_info, - get_oi_version, + get_swarms_verison, get_os_version, get_package_mismatches, get_pip_version, get_python_version, get_ram_info, - interpreter_info, system_info, ) from swarms.telemetry.user_utils import ( @@ -15,6 +14,7 @@ from swarms.telemetry.user_utils import ( generate_user_id, get_machine_id, get_system_info, + get_user_device_data, ) __all__ = [ @@ -26,11 +26,11 @@ __all__ = [ "generate_unique_identifier", "get_python_version", "get_pip_version", - "get_oi_version", + "get_swarms_verison", "get_os_version", "get_cpu_info", "get_ram_info", "get_package_mismatches", - "interpreter_info", "system_info", + "get_user_device_data", ] diff --git a/swarms/telemetry/main.py b/swarms/telemetry/main.py new file mode 100644 index 00000000..fe00fecf --- /dev/null +++ b/swarms/telemetry/main.py @@ -0,0 +1,58 @@ +import logging +import pymongo +import platform +import datetime + + +class Telemetry: + def __init__(self, db_url, db_name): + self.logger = self.setup_logging() + self.db = self.setup_db(db_url, db_name) + + def setup_logging(self): + logger = logging.getLogger("telemetry") + logger.setLevel(logging.DEBUG) + handler = logging.StreamHandler() + handler.setFormatter( + logging.Formatter( + "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + ) + ) + logger.addHandler(handler) + return logger + + def setup_db(self, db_url, db_name): + client = pymongo.MongoClient(db_url) + return client[db_name] + + def capture_device_data(self): + data = { + "system": platform.system(), + "node": platform.node(), + "release": platform.release(), + "version": platform.version(), + "machine": platform.machine(), + "processor": platform.processor(), + "time": datetime.datetime.now(), + } + return data + + def send_to_db(self, collection_name, data): + collection = self.db[collection_name] + collection.insert_one(data) + + def log_and_capture(self, message, level, collection_name): + if level == "info": + self.logger.info(message) + elif level == "error": + self.logger.error(message) + data = self.capture_device_data() + data["log"] = message + self.send_to_db(collection_name, data) + + def log_import(self, module_name): + self.logger.info(f"Importing module {module_name}") + module = __import__(module_name, fromlist=["*"]) + for k in dir(module): + if not k.startswith("__"): + self.logger.info(f"Imported {k} from {module_name}") diff --git a/swarms/telemetry/sys_info.py b/swarms/telemetry/sys_info.py index 08ad1db3..d2841585 100644 --- a/swarms/telemetry/sys_info.py +++ b/swarms/telemetry/sys_info.py @@ -22,20 +22,20 @@ def get_pip_version(): return pip_version -def get_oi_version(): +def get_swarms_verison(): try: - oi_version_cmd = ( - subprocess.check_output(["interpreter", "--version"]) + swarms_verison_cmd = ( + subprocess.check_output(["swarms", "--version"]) .decode() .split()[1] ) except Exception as e: - oi_version_cmd = str(e) - oi_version_pkg = pkg_resources.get_distribution( - "open-interpreter" + swarms_verison_cmd = str(e) + swarms_verison_pkg = pkg_resources.get_distribution( + "swarms" ).version - oi_version = oi_version_cmd, oi_version_pkg - return oi_version + swarms_verison = swarms_verison_cmd, swarms_verison_pkg + return swarms_verison def get_os_version(): @@ -89,70 +89,15 @@ def get_package_mismatches(file_path="pyproject.toml"): return "\n" + "\n".join(mismatches) -def interpreter_info(interpreter): - try: - if interpreter.offline and interpreter.llm.api_base: - try: - curl = subprocess.check_output( - f"curl {interpreter.llm.api_base}" - ) - except Exception as e: - curl = str(e) - else: - curl = "Not local" - - messages_to_display = [] - for message in interpreter.messages: - message = message.copy() - try: - if len(message["content"]) > 600: - message["content"] = ( - message["content"][:300] - + "..." - + message["content"][-300:] - ) - except Exception as e: - print(str(e), "for message:", message) - messages_to_display.append(message) - - return f""" - - # Interpreter Info - - Vision: {interpreter.llm.supports_vision} - Model: {interpreter.llm.model} - Function calling: {interpreter.llm.supports_functions} - Context window: {interpreter.llm.context_window} - Max tokens: {interpreter.llm.max_tokens} - - Auto run: {interpreter.auto_run} - API base: {interpreter.llm.api_base} - Offline: {interpreter.offline} - - Curl output: {curl} - - # Messages - - System Message: {interpreter.system_message} - - """ + "\n\n".join([str(m) for m in messages_to_display]) - except: - return "Error, couldn't get interpreter info" - - -def system_info(interpreter): - oi_version = get_oi_version() - print(f""" - Python Version: {get_python_version()} - Pip Version: {get_pip_version()} - Open-interpreter Version: cmd:{oi_version[0]}, pkg: {oi_version[1]} - OS Version and Architecture: {get_os_version()} - CPU Info: {get_cpu_info()} - RAM Info: {get_ram_info()} - {interpreter_info(interpreter)} - """) - - # Removed the following, as it causes `FileNotFoundError: [Errno 2] No such file or directory: 'pyproject.toml'`` on prod - # (i think it works on dev, but on prod the pyproject.toml will not be in the cwd. might not be accessible at all) - # Package Version Mismatches: - # {get_package_mismatches()} + +def system_info(): + swarms_verison = get_swarms_verison() + return { + "Python Version": get_python_version(), + "Pip Version": get_pip_version(), + "Swarms Version": swarms_verison, + "OS Version and Architecture": get_os_version(), + "CPU Info": get_cpu_info(), + "RAM Info": get_ram_info(), + } + diff --git a/swarms/telemetry/user_utils.py b/swarms/telemetry/user_utils.py index 74667326..9369bc26 100644 --- a/swarms/telemetry/user_utils.py +++ b/swarms/telemetry/user_utils.py @@ -2,7 +2,8 @@ import hashlib import platform import uuid import socket - +from swarms.telemetry.sys_info import system_info +from swarms.telemetry.check_update import check_for_package # Helper functions def generate_user_id(): @@ -47,6 +48,7 @@ def get_system_info(): ), "processor": platform.processor(), "python_version": platform.python_version(), + "Misc": system_info(), } return info @@ -61,3 +63,27 @@ def generate_unique_identifier(): system_info = get_system_info() unique_id = uuid.uuid5(uuid.NAMESPACE_DNS, str(system_info)) return str(unique_id) + + +def get_local_ip(): + """Get local ip + + Returns: + str: local ip + + """ + return socket.gethostbyname(socket.gethostname()) + + + +def get_user_device_data(): + data = { + "ID": generate_user_id(), + "Machine ID": get_machine_id(), + "System Info": get_system_info(), + "UniqueID": generate_unique_identifier(), + "Swarms [Version]": check_for_package("swarms"), + } + return data + +# \ No newline at end of file diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index de8ffb57..df9fe6ca 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -22,6 +22,8 @@ from swarms.utils.try_except_wrapper import try_except_wrapper from swarms.utils.download_weights_from_url import ( download_weights_from_url, ) +from swarms.utils.save_logs import parse_log_file + __all__ = [ "SubprocessCodeInterpreter", @@ -42,4 +44,5 @@ __all__ = [ "data_to_text", "try_except_wrapper", "download_weights_from_url", + "parse_log_file", ] diff --git a/swarms/utils/agent_ops_wrapper.py b/swarms/utils/agent_ops_wrapper.py deleted file mode 100644 index c1a019d2..00000000 --- a/swarms/utils/agent_ops_wrapper.py +++ /dev/null @@ -1,45 +0,0 @@ -import logging -import agentops - -class AgentOpsWrapper: - """ - A wrapper for the AgentOps client that adds error handling and logging. - """ - - def __init__(self, api_key): - """ - Initialize the AgentOps client with the given API key. - """ - self.client = agentops.Client(api_key) - self.logger = logging.getLogger(__name__) - self.logger.setLevel(logging.INFO) - - def record_action(self, action_name): - """ - Record an action with the given name. - """ - def decorator(func): - def wrapper(*args, **kwargs): - try: - self.client.record_action(action_name) - result = func(*args, **kwargs) - self.logger.info(f"Action {action_name} completed successfully.") - return result - except Exception as e: - self.logger.error(f"Error while recording action {action_name}: {e}") - raise - return wrapper - return decorator - - def end_session(self, status): - """ - End the session with the given status. - """ - try: - self.client.end_session(status) - self.logger.info(f"Session ended with status {status}.") - except Exception as e: - self.logger.error(f"Error while ending session: {e}") - raise - -# agentops = AgentOpsWrapper(api_key) \ No newline at end of file diff --git a/swarms/utils/save_logs.py b/swarms/utils/save_logs.py new file mode 100644 index 00000000..c8193905 --- /dev/null +++ b/swarms/utils/save_logs.py @@ -0,0 +1,46 @@ +import os + + +def parse_log_file(filename: str): + """ + Parse a log file and return a list of log entries. + + Each log entry is a dictionary with keys for the timestamp, name, level, and message. + + Args: + filename (str): The name of the log file. + + Returns: + list: A list of log entries. + + Raises: + FileNotFoundError: If the log file does not exist. + ValueError: If a log entry does not have the correct format. + """ + # Check if the file exists + if not os.path.exists(filename): + raise FileNotFoundError( + f"The file {filename} does not exist." + ) + + log_entries = [] + + with open(filename, "r") as file: + for line in file: + parts = line.split(" - ") + # Check if the log entry has the correct format + if len(parts) != 4: + raise ValueError( + f"The log entry '{line}' does not have the" + " correct format." + ) + timestamp, name, level, message = parts + log_entry = { + "timestamp": timestamp, + "name": name, + "level": level, + "message": message.rstrip("\n"), + } + log_entries.append(log_entry) + + return log_entries From 4d39e2fc76c43ef6aee5222a42003e8d55bcee20 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 24 Jan 2024 13:34:08 -0500 Subject: [PATCH 049/103] [DOCS][Data Room] --- docs/corporate/data_room.md | 83 +++++++++++++++++++++++++++++++++++++ mkdocs.yml | 1 + pyproject.toml | 2 +- 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 docs/corporate/data_room.md diff --git a/docs/corporate/data_room.md b/docs/corporate/data_room.md new file mode 100644 index 00000000..d63d1b7b --- /dev/null +++ b/docs/corporate/data_room.md @@ -0,0 +1,83 @@ +# Swarms Data Roam + +## Table of Contents + +*** Introduction *** +- Overview of the Company +- Vision and Mission Statement +- Executive Summary + +*** Corporate Documents *** +- Articles of Incorporation +- Bylaws +- Shareholder Agreements +- Board Meeting Minutes +- Company Structure and Org Chart + +*** Financial Information *** +- Historical Financial Statements + - Income Statements + - Balance Sheets + - Cash Flow Statements +- Financial Projections and Forecasts +- Cap Table +- Funding History and Use of Funds + +*** Products and Services *** +- Detailed Descriptions of Products/Services +- Product Development Roadmap +- User Manuals and Technical Specifications +- Case Studies and Use Cases + + +## *** Introdution *** +Swarms provides automation-as-a-service through swarms of autonomous agents that work together as a team. We enable our customers to build, deploy, and scale production-grade multi-agent applications to automate real-world tasks. + + +### *** Vision *** +Our vision for 2024 is to provide the most reliable infrastructure for deploying autonomous agents into the real world through the Swarm Cloud, our premier cloud platform for the scalable deployment of Multi-Modal Autonomous Agents. The platform focuses on delivering maximum value to users by only taking a small fee when utilizing the agents for the hosted compute power needed to host the agents. + +### *** Executive Summary *** +The Swarm Corporation aims to enable AI models to automate complex workflows and operations, not just singular low-value tasks. We believe collaboration between multiple agents can overcome limitations of individual agents for reasoning, planning, etc. This will allow automation of processes in mission-critical industries like security, logistics, and manufacturing where AI adoption is currently low. + +We provide an open source framework to deploy production-grade multi-modal agents in just a few lines of code. This builds our user base, recruits talent, gets customer feedback to improve products, gains awareness and trust. + +Our business model focuses on customer satisfaction, openness, integration with other tools/platforms, and production-grade reliability. + +Go-to-market strategy is to get the framework to product-market fit with over 50K weekly recurring users, then secure high-value contracts in target industries. Long-term monetization via microtransactions, usage-based pricing, subscriptions. + +The team has thousands of hours building and optimizing autonomous agents. Leadership includes AI engineers, product experts, open source contributors and community builders. + +Key milestones: get 80K framework users in January 2024, start contracts in target verticals, introduce commercial products in 2025 with various pricing models. + + +### *** Swarm Memo *** +To learn more about our mission, vision, plans for GTM, and much more please refer to the [Swarm Memo here](https://docs.google.com/document/d/1hS_nv_lFjCqLfnJBoF6ULY9roTbSgSuCkvXvSUSc7Lo/edit?usp=sharing) + + + +## Product +Swarms is an open source framework for developers in python to enable seamless, reliable, and scalable multi-agent orchestration. + +[Here is the official Swarms Github Page:](https://github.com/kyegomez/swarms) + +### Product Growth Metrics + +- Total Downloads of all time: [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) + +- Click here for Downloads this month: [![Downloads](https://static.pepy.tech/badge/swarms/month)](https://pepy.tech/project/swarms) + + +- Total Downloads this week: [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) + +- Click here for Forks which represent the number of times a user has copied the entire codebase for optimization, contribution, or usage. [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) + +- Stars are the number of people that have liked our project, click here for more: [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) + +- Various Project Statistics such as watchers, number of contributors, date repository was created and much more. [CLICK HERE](https://libraries.io/github/kyegomez/swarms) + +- Contribution Based Statistics such as number of contributors, number of lines of code changed, and much more [HERE](https://github.com/kyegomez/swarms/graphs/contributors) + +- [Community insights](https://github.com/kyegomez/swarms/graphs/community) + +- [Github Traffic Metrics](https://github.com/kyegomez/swarms/graphs/traffic) diff --git a/mkdocs.yml b/mkdocs.yml index 184f059f..033ac5f2 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -166,3 +166,4 @@ nav: - Hiring: "corporate/hiring.md" - SwarmCloud: "corporate/swarm_cloud.md" - SwarmMemo: "corporate/swarm_memo.md" + - Data Room: "corporate/data_room.md" diff --git a/pyproject.toml b/pyproject.toml index 42413124..fdd127c3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.7.8" +version = "3.7.9" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] From 6191e290f0cbfaf85ef5f210b89e9772b83fd838 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 24 Jan 2024 14:39:26 -0500 Subject: [PATCH 050/103] [DOCS][Swarm Data room] --- docs/corporate/data_room.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/docs/corporate/data_room.md b/docs/corporate/data_room.md index d63d1b7b..8a7ccb91 100644 --- a/docs/corporate/data_room.md +++ b/docs/corporate/data_room.md @@ -2,19 +2,19 @@ ## Table of Contents -*** Introduction *** +** Introduction ** - Overview of the Company - Vision and Mission Statement - Executive Summary -*** Corporate Documents *** +** Corporate Documents ** - Articles of Incorporation - Bylaws - Shareholder Agreements - Board Meeting Minutes - Company Structure and Org Chart -*** Financial Information *** +** Financial Information ** - Historical Financial Statements - Income Statements - Balance Sheets @@ -23,21 +23,21 @@ - Cap Table - Funding History and Use of Funds -*** Products and Services *** +** Products and Services ** - Detailed Descriptions of Products/Services - Product Development Roadmap - User Manuals and Technical Specifications - Case Studies and Use Cases -## *** Introdution *** +## ** Introdution ** Swarms provides automation-as-a-service through swarms of autonomous agents that work together as a team. We enable our customers to build, deploy, and scale production-grade multi-agent applications to automate real-world tasks. -### *** Vision *** +### ** Vision ** Our vision for 2024 is to provide the most reliable infrastructure for deploying autonomous agents into the real world through the Swarm Cloud, our premier cloud platform for the scalable deployment of Multi-Modal Autonomous Agents. The platform focuses on delivering maximum value to users by only taking a small fee when utilizing the agents for the hosted compute power needed to host the agents. -### *** Executive Summary *** +### ** Executive Summary ** The Swarm Corporation aims to enable AI models to automate complex workflows and operations, not just singular low-value tasks. We believe collaboration between multiple agents can overcome limitations of individual agents for reasoning, planning, etc. This will allow automation of processes in mission-critical industries like security, logistics, and manufacturing where AI adoption is currently low. We provide an open source framework to deploy production-grade multi-modal agents in just a few lines of code. This builds our user base, recruits talent, gets customer feedback to improve products, gains awareness and trust. @@ -51,24 +51,22 @@ The team has thousands of hours building and optimizing autonomous agents. Leade Key milestones: get 80K framework users in January 2024, start contracts in target verticals, introduce commercial products in 2025 with various pricing models. -### *** Swarm Memo *** +### ** The Swarm Corporation Memo ** To learn more about our mission, vision, plans for GTM, and much more please refer to the [Swarm Memo here](https://docs.google.com/document/d/1hS_nv_lFjCqLfnJBoF6ULY9roTbSgSuCkvXvSUSc7Lo/edit?usp=sharing) - ## Product -Swarms is an open source framework for developers in python to enable seamless, reliable, and scalable multi-agent orchestration. +Swarms is an open source framework for developers in python to enable seamless, reliable, and scalable multi-agent orchestration through modularity, customization, and precision. [Here is the official Swarms Github Page:](https://github.com/kyegomez/swarms) ### Product Growth Metrics -- Total Downloads of all time: [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) +- Total Downloads of all time: [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) - Click here for Downloads this month: [![Downloads](https://static.pepy.tech/badge/swarms/month)](https://pepy.tech/project/swarms) - -- Total Downloads this week: [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) +- Total Downloads this week: [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) - Click here for Forks which represent the number of times a user has copied the entire codebase for optimization, contribution, or usage. [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) @@ -78,6 +76,8 @@ Swarms is an open source framework for developers in python to enable seamless, - Contribution Based Statistics such as number of contributors, number of lines of code changed, and much more [HERE](https://github.com/kyegomez/swarms/graphs/contributors) -- [Community insights](https://github.com/kyegomez/swarms/graphs/community) +- [Github Community insights](https://github.com/kyegomez/swarms/graphs/community) - [Github Traffic Metrics](https://github.com/kyegomez/swarms/graphs/traffic) + +- Issues with the framework or Github Issues: [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) \ No newline at end of file From f327282ade2acbf000d41cee9f4c259e845d3b43 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 24 Jan 2024 14:42:16 -0500 Subject: [PATCH 051/103] [DOCS][Swarm Data room] --- docs/corporate/data_room.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/corporate/data_room.md b/docs/corporate/data_room.md index 8a7ccb91..95081500 100644 --- a/docs/corporate/data_room.md +++ b/docs/corporate/data_room.md @@ -2,19 +2,19 @@ ## Table of Contents -** Introduction ** +**Introduction** - Overview of the Company - Vision and Mission Statement - Executive Summary -** Corporate Documents ** +**Corporate Documents** - Articles of Incorporation - Bylaws - Shareholder Agreements - Board Meeting Minutes - Company Structure and Org Chart -** Financial Information ** +**Financial Information** - Historical Financial Statements - Income Statements - Balance Sheets @@ -23,21 +23,21 @@ - Cap Table - Funding History and Use of Funds -** Products and Services ** +**Products and Services** - Detailed Descriptions of Products/Services - Product Development Roadmap - User Manuals and Technical Specifications - Case Studies and Use Cases -## ** Introdution ** +## **Introdution** Swarms provides automation-as-a-service through swarms of autonomous agents that work together as a team. We enable our customers to build, deploy, and scale production-grade multi-agent applications to automate real-world tasks. -### ** Vision ** +### **Vision** Our vision for 2024 is to provide the most reliable infrastructure for deploying autonomous agents into the real world through the Swarm Cloud, our premier cloud platform for the scalable deployment of Multi-Modal Autonomous Agents. The platform focuses on delivering maximum value to users by only taking a small fee when utilizing the agents for the hosted compute power needed to host the agents. -### ** Executive Summary ** +### **Executive Summary** The Swarm Corporation aims to enable AI models to automate complex workflows and operations, not just singular low-value tasks. We believe collaboration between multiple agents can overcome limitations of individual agents for reasoning, planning, etc. This will allow automation of processes in mission-critical industries like security, logistics, and manufacturing where AI adoption is currently low. We provide an open source framework to deploy production-grade multi-modal agents in just a few lines of code. This builds our user base, recruits talent, gets customer feedback to improve products, gains awareness and trust. @@ -51,7 +51,7 @@ The team has thousands of hours building and optimizing autonomous agents. Leade Key milestones: get 80K framework users in January 2024, start contracts in target verticals, introduce commercial products in 2025 with various pricing models. -### ** The Swarm Corporation Memo ** +### **The Swarm Corporation Memo** To learn more about our mission, vision, plans for GTM, and much more please refer to the [Swarm Memo here](https://docs.google.com/document/d/1hS_nv_lFjCqLfnJBoF6ULY9roTbSgSuCkvXvSUSc7Lo/edit?usp=sharing) From 07c3f6af8c795e6c48caac56c3f8c53b25ed77cd Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 24 Jan 2024 14:50:46 -0500 Subject: [PATCH 052/103] [DOCS][Swarm Data room] --- docs/corporate/data_room.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/corporate/data_room.md b/docs/corporate/data_room.md index 95081500..c05286a9 100644 --- a/docs/corporate/data_room.md +++ b/docs/corporate/data_room.md @@ -4,29 +4,44 @@ **Introduction** - Overview of the Company + - Vision and Mission Statement + - Executive Summary **Corporate Documents** - Articles of Incorporation + - Bylaws + - Shareholder Agreements + - Board Meeting Minutes + - Company Structure and Org Chart **Financial Information** - Historical Financial Statements + - Income Statements + - Balance Sheets + - Cash Flow Statements + - Financial Projections and Forecasts + - Cap Table + - Funding History and Use of Funds **Products and Services** - Detailed Descriptions of Products/Services + - Product Development Roadmap + - User Manuals and Technical Specifications + - Case Studies and Use Cases From 6206d12db2b406112bd23ce0a5e9e8d3fc32fd83 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 24 Jan 2024 15:03:46 -0500 Subject: [PATCH 053/103] [DOCS][Swarm Data room] --- docs/corporate/data_room.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/corporate/data_room.md b/docs/corporate/data_room.md index c05286a9..550e8f94 100644 --- a/docs/corporate/data_room.md +++ b/docs/corporate/data_room.md @@ -1,8 +1,9 @@ -# Swarms Data Roam +# Swarms Data Room ## Table of Contents **Introduction** + - Overview of the Company - Vision and Mission Statement @@ -10,6 +11,7 @@ - Executive Summary **Corporate Documents** + - Articles of Incorporation - Bylaws @@ -21,6 +23,7 @@ - Company Structure and Org Chart **Financial Information** + - Historical Financial Statements - Income Statements @@ -36,6 +39,7 @@ - Funding History and Use of Funds **Products and Services** + - Detailed Descriptions of Products/Services - Product Development Roadmap @@ -70,7 +74,7 @@ Key milestones: get 80K framework users in January 2024, start contracts in targ To learn more about our mission, vision, plans for GTM, and much more please refer to the [Swarm Memo here](https://docs.google.com/document/d/1hS_nv_lFjCqLfnJBoF6ULY9roTbSgSuCkvXvSUSc7Lo/edit?usp=sharing) -## Product +## **Product** Swarms is an open source framework for developers in python to enable seamless, reliable, and scalable multi-agent orchestration through modularity, customization, and precision. [Here is the official Swarms Github Page:](https://github.com/kyegomez/swarms) From bf7142b8d2f6084e25c8a52099ff8d2f832f2034 Mon Sep 17 00:00:00 2001 From: Eternal Reclaimer <98760976+kyegomez@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:14:06 -0500 Subject: [PATCH 054/103] Update data_room.md --- docs/corporate/data_room.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/corporate/data_room.md b/docs/corporate/data_room.md index 550e8f94..3bfc05dc 100644 --- a/docs/corporate/data_room.md +++ b/docs/corporate/data_room.md @@ -81,17 +81,17 @@ Swarms is an open source framework for developers in python to enable seamless, ### Product Growth Metrics -- Total Downloads of all time: [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) +- Total Downloads of all time: [![Downloads](https://static.pepy.tech/badge/swarms)](https://pepy.tech/project/swarms) - Click here for Downloads this month: [![Downloads](https://static.pepy.tech/badge/swarms/month)](https://pepy.tech/project/swarms) -- Total Downloads this week: [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) +- Total Downloads this week: [![Downloads](https://static.pepy.tech/badge/swarms/week)](https://pepy.tech/project/swarms) +- +- Github Forks, Forks represent the number of times a user has copied the entire codebase for optimization, contribution, or usage. [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) -- Click here for Forks which represent the number of times a user has copied the entire codebase for optimization, contribution, or usage. [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) +- Github Stars, stars are the number of people that have liked our project, click here for more: [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) -- Stars are the number of people that have liked our project, click here for more: [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) - -- Various Project Statistics such as watchers, number of contributors, date repository was created and much more. [CLICK HERE](https://libraries.io/github/kyegomez/swarms) +- Pip Module Metrics: Various Project Statistics such as watchers, number of contributors, date repository was created and much more. [CLICK HERE](https://libraries.io/github/kyegomez/swarms) - Contribution Based Statistics such as number of contributors, number of lines of code changed, and much more [HERE](https://github.com/kyegomez/swarms/graphs/contributors) @@ -99,4 +99,4 @@ Swarms is an open source framework for developers in python to enable seamless, - [Github Traffic Metrics](https://github.com/kyegomez/swarms/graphs/traffic) -- Issues with the framework or Github Issues: [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) \ No newline at end of file +- Issues with the framework or Github Issues: [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) From a8a6215c8375eac1dea0cfb9a2848a1143ad99c3 Mon Sep 17 00:00:00 2001 From: Eternal Reclaimer <98760976+kyegomez@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:14:20 -0500 Subject: [PATCH 055/103] Update data_room.md --- docs/corporate/data_room.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/corporate/data_room.md b/docs/corporate/data_room.md index 3bfc05dc..3241d880 100644 --- a/docs/corporate/data_room.md +++ b/docs/corporate/data_room.md @@ -86,7 +86,7 @@ Swarms is an open source framework for developers in python to enable seamless, - Click here for Downloads this month: [![Downloads](https://static.pepy.tech/badge/swarms/month)](https://pepy.tech/project/swarms) - Total Downloads this week: [![Downloads](https://static.pepy.tech/badge/swarms/week)](https://pepy.tech/project/swarms) -- + - Github Forks, Forks represent the number of times a user has copied the entire codebase for optimization, contribution, or usage. [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) - Github Stars, stars are the number of people that have liked our project, click here for more: [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) From 893529134140b72700d6a4be982e4636239d8148 Mon Sep 17 00:00:00 2001 From: Eternal Reclaimer <98760976+kyegomez@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:19:11 -0500 Subject: [PATCH 056/103] Update data_room.md --- docs/corporate/data_room.md | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/docs/corporate/data_room.md b/docs/corporate/data_room.md index 3241d880..946f209f 100644 --- a/docs/corporate/data_room.md +++ b/docs/corporate/data_room.md @@ -80,23 +80,16 @@ Swarms is an open source framework for developers in python to enable seamless, [Here is the official Swarms Github Page:](https://github.com/kyegomez/swarms) ### Product Growth Metrics +| Name | Description | Link | +|----------------------------------|---------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------| +| Total Downloads of all time | Total number of downloads for the product over its entire lifespan. | [![Downloads](https://static.pepy.tech/badge/swarms)](https://pepy.tech/project/swarms) | +| Downloads this month | Number of downloads for the product in the current month. | [![Downloads](https://static.pepy.tech/badge/swarms/month)](https://pepy.tech/project/swarms) | +| Total Downloads this week | Total number of downloads for the product in the current week. | [![Downloads](https://static.pepy.tech/badge/swarms/week)](https://pepy.tech/project/swarms) | +| Github Forks | Number of times the product's codebase has been copied for optimization, contribution, or usage. | [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) | +| Github Stars | Number of users who have 'liked' the project. | [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) | +| Pip Module Metrics | Various project statistics such as watchers, number of contributors, date repository was created, and more. | [CLICK HERE](https://libraries.io/github/kyegomez/swarms) | +| Contribution Based Statistics | Statistics like number of contributors, lines of code changed, etc. | [HERE](https://github.com/kyegomez/swarms/graphs/contributors) | +| Github Community insights | Insights into the Github community around the product. | [Github Community insights](https://github.com/kyegomez/swarms/graphs/community) | +| Github Traffic Metrics | Metrics related to traffic, such as views and clones on Github. | [Github Traffic Metrics](https://github.com/kyegomez/swarms/graphs/traffic) | +| Issues with the framework | Current open issues for the product on Github. | [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) | -- Total Downloads of all time: [![Downloads](https://static.pepy.tech/badge/swarms)](https://pepy.tech/project/swarms) - -- Click here for Downloads this month: [![Downloads](https://static.pepy.tech/badge/swarms/month)](https://pepy.tech/project/swarms) - -- Total Downloads this week: [![Downloads](https://static.pepy.tech/badge/swarms/week)](https://pepy.tech/project/swarms) - -- Github Forks, Forks represent the number of times a user has copied the entire codebase for optimization, contribution, or usage. [![GitHub forks](https://img.shields.io/github/forks/kyegomez/swarms)](https://github.com/kyegomez/swarms/network) - -- Github Stars, stars are the number of people that have liked our project, click here for more: [![GitHub stars](https://img.shields.io/github/stars/kyegomez/swarms)](https://github.com/kyegomez/swarms/stargazers) - -- Pip Module Metrics: Various Project Statistics such as watchers, number of contributors, date repository was created and much more. [CLICK HERE](https://libraries.io/github/kyegomez/swarms) - -- Contribution Based Statistics such as number of contributors, number of lines of code changed, and much more [HERE](https://github.com/kyegomez/swarms/graphs/contributors) - -- [Github Community insights](https://github.com/kyegomez/swarms/graphs/community) - -- [Github Traffic Metrics](https://github.com/kyegomez/swarms/graphs/traffic) - -- Issues with the framework or Github Issues: [![GitHub issues](https://img.shields.io/github/issues/kyegomez/swarms)](https://github.com/kyegomez/swarms/issues) From aee3a79c016a1966dc01d47499ce744797b8dac7 Mon Sep 17 00:00:00 2001 From: Eternal Reclaimer <98760976+kyegomez@users.noreply.github.com> Date: Thu, 25 Jan 2024 08:25:43 -0500 Subject: [PATCH 057/103] Update pyproject.toml --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index fdd127c3..b7a39e00 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -24,7 +24,7 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.6.1" torch = "2.1.1" -transformers = "4.36.2" +transformers = "4.37.1" openai = "0.28.0" langchain = "0.0.333" asyncio = "3.4.3" From b097e6016df23c235977c241ecc97f2cca06ee01 Mon Sep 17 00:00:00 2001 From: Kye Date: Thu, 25 Jan 2024 17:13:16 -0500 Subject: [PATCH 058/103] [README] --- README.md | 50 +++++++++++++++++++++++++++++ playground/agents/worker_example.py | 8 +++++ playground/demos/fof/langchain.py | 5 +++ swarms/agents/__init__.py | 2 ++ swarms/agents/agent_wrapper.py | 25 +++++++++++++++ swarms/agents/worker_agent.py | 2 ++ swarms/models/anthropic.py | 2 -- swarms/structs/task.py | 1 + 8 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 playground/demos/fof/langchain.py create mode 100644 swarms/agents/agent_wrapper.py diff --git a/README.md b/README.md index 43407822..64db98ae 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,56 @@ generated_data = agent.run(task) print(generated_data) +``` + + +### `Worker` +The `Worker` is a simple all-in-one agent equipped with an LLM, tools, and RAG. Get started below: + +✅ Plug in and Play LLM. Utilize any LLM from anywhere and any framework + +✅ Reliable RAG: Utilizes FAISS for efficient RAG but it's modular so you can use any DB. + +✅ Multi-Step Parallel Function Calling: Use any tool + +```python +# Importing necessary modules +import os +from dotenv import load_dotenv +from swarms import Worker, OpenAIChat, tool + +# Loading environment variables from .env file +load_dotenv() + +# Retrieving the OpenAI API key from environment variables +api_key = os.getenv("OPENAI_API_KEY") + + +# Create a tool +@tool +def search_api(query: str): + pass + + +# Creating a Worker instance +worker = Worker( + name="My Worker", + role="Worker", + human_in_the_loop=False, + tools=[search_api], + temperature=0.5, + llm=OpenAIChat(openai_api_key=api_key), +) + +# Running the worker with a prompt +out = worker.run( + "Hello, how are you? Create an image of how your are doing!" +) + +# Printing the output +print(out) + + ``` ------ diff --git a/playground/agents/worker_example.py b/playground/agents/worker_example.py index ceead3a9..bd8d47c0 100644 --- a/playground/agents/worker_example.py +++ b/playground/agents/worker_example.py @@ -1,12 +1,16 @@ +# Importing necessary modules import os from dotenv import load_dotenv from swarms.agents.worker_agent import Worker from swarms import OpenAIChat +# Loading environment variables from .env file load_dotenv() +# Retrieving the OpenAI API key from environment variables api_key = os.getenv("OPENAI_API_KEY") +# Creating a Worker instance worker = Worker( name="My Worker", role="Worker", @@ -14,9 +18,13 @@ worker = Worker( tools=[], temperature=0.5, llm=OpenAIChat(openai_api_key=api_key), + verbose = True, ) +# Running the worker with a prompt out = worker.run( "Hello, how are you? Create an image of how your are doing!" ) + +# Printing the output print(out) diff --git a/playground/demos/fof/langchain.py b/playground/demos/fof/langchain.py new file mode 100644 index 00000000..ef338520 --- /dev/null +++ b/playground/demos/fof/langchain.py @@ -0,0 +1,5 @@ +""" +This tutorial shows you how to integrate swarms with Langchain + +""" + diff --git a/swarms/agents/__init__.py b/swarms/agents/__init__.py index 4b786bf4..461baa16 100644 --- a/swarms/agents/__init__.py +++ b/swarms/agents/__init__.py @@ -15,6 +15,7 @@ from swarms.agents.stopping_conditions import ( ) from swarms.agents.tool_agent import ToolAgent from swarms.agents.worker_agent import Worker +from swarms.agents.agent_wrapper import agent_wrapper __all__ = [ "AbstractAgent", @@ -32,4 +33,5 @@ __all__ = [ "check_exit", "check_end", "Worker", + "agent_wrapper", ] diff --git a/swarms/agents/agent_wrapper.py b/swarms/agents/agent_wrapper.py new file mode 100644 index 00000000..109048e9 --- /dev/null +++ b/swarms/agents/agent_wrapper.py @@ -0,0 +1,25 @@ +from swarms.structs.agent import Agent + +def agent_wrapper(ClassToWrap): + """ + This function takes a class 'ClassToWrap' and returns a new class that + inherits from both 'ClassToWrap' and 'Agent'. The new class overrides + the '__init__' method of 'Agent' to call the '__init__' method of 'ClassToWrap'. + + Args: + ClassToWrap (type): The class to be wrapped and made to inherit from 'Agent'. + + Returns: + type: The new class that inherits from both 'ClassToWrap' and 'Agent'. + """ + + class WrappedClass(ClassToWrap, Agent): + def __init__(self, *args, **kwargs): + try: + Agent.__init__(self, *args, **kwargs) + ClassToWrap.__init__(self, *args, **kwargs) + except Exception as e: + print(f"Error initializing WrappedClass: {e}") + raise e + + return WrappedClass \ No newline at end of file diff --git a/swarms/agents/worker_agent.py b/swarms/agents/worker_agent.py index 8ed7d0d3..292466a2 100644 --- a/swarms/agents/worker_agent.py +++ b/swarms/agents/worker_agent.py @@ -51,6 +51,7 @@ class Worker: tools: List[Any] = None, embedding_size: int = 1536, search_kwargs: dict = {"k": 8}, + verbose: bool = False, *args, **kwargs, ): @@ -64,6 +65,7 @@ class Worker: self.tools = tools self.embedding_size = embedding_size self.search_kwargs = search_kwargs + self.verbose = verbose self.setup_tools(external_tools) self.setup_memory() diff --git a/swarms/models/anthropic.py b/swarms/models/anthropic.py index adffe49d..0e4690f9 100644 --- a/swarms/models/anthropic.py +++ b/swarms/models/anthropic.py @@ -29,9 +29,7 @@ from langchain.schema.language_model import BaseLanguageModel from langchain.schema.output import GenerationChunk from langchain.schema.prompt import PromptValue from langchain.utils import ( - check_package_version, get_from_dict_or_env, - get_pydantic_field_names, ) from packaging.version import parse from requests import HTTPError, Response diff --git a/swarms/structs/task.py b/swarms/structs/task.py index a794506f..fd6a1ab8 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -233,6 +233,7 @@ class Task: if task.description is not None else "" ) + result = ( task.result if task.result is not None else "" ) From cdf68c94676d786bce5f653bae562e5a211bee72 Mon Sep 17 00:00:00 2001 From: Kye Date: Thu, 25 Jan 2024 17:28:26 -0500 Subject: [PATCH 059/103] [FEAT][CLIPQ] --- swarms/agents/worker_agent.py | 3 +- swarms/models/__init__.py | 3 + swarms/models/clipq.py | 161 ++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 swarms/models/clipq.py diff --git a/swarms/agents/worker_agent.py b/swarms/agents/worker_agent.py index 292466a2..d3ebb1d3 100644 --- a/swarms/agents/worker_agent.py +++ b/swarms/agents/worker_agent.py @@ -9,7 +9,6 @@ from langchain_experimental.autonomous_agents import AutoGPT from swarms.utils.decorators import error_decorator, timing_decorator - class Worker: """ The Worker class represents an autonomous agent that can perform tassks through @@ -165,7 +164,7 @@ class Worker: # @log_decorator @error_decorator @timing_decorator - def run(self, task: str = None, *args, **kwargs): + def run(self, task: str = None, img = None, *args, **kwargs): """ Run the autonomous agent on a given task. diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 635124a6..d9a1e96d 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -47,6 +47,7 @@ from swarms.models.ultralytics_model import ( from swarms.models.vip_llava import VipLlavaMultiModal # noqa: E402 from swarms.models.llava import LavaMultiModal # noqa: E402 from swarms.models.qwen import QwenVLMultiModal # noqa: E402 +from swarms.models.clipq import CLIPQ # noqa: E402 # from swarms.models.dalle3 import Dalle3 # from swarms.models.distilled_whisperx import DistilWhisperModel # noqa: E402 @@ -110,4 +111,6 @@ __all__ = [ "VipLlavaMultiModal", "LavaMultiModal", "QwenVLMultiModal", + "CLIPQ", + ] diff --git a/swarms/models/clipq.py b/swarms/models/clipq.py new file mode 100644 index 00000000..c70190fd --- /dev/null +++ b/swarms/models/clipq.py @@ -0,0 +1,161 @@ +from io import BytesIO + +import requests +import torch +from PIL import Image +from torchvision.transforms import GaussianBlur +from transformers import CLIPModel, CLIPProcessor + + +class CLIPQ: + """ + ClipQ is an CLIQ based model that can be used to generate captions for images. + + + Attributes: + model_name (str): The name of the model to be used. + query_text (str): The query text to be used for the model. + + Args: + model_name (str): The name of the model to be used. + query_text (str): The query text to be used for the model. + + + + + """ + + def __init__( + self, + model_name: str = "openai/clip-vit-base-patch16", + query_text: str = "A photo ", + *args, + **kwargs + ): + self.model = CLIPModel.from_pretrained(model_name, *args, **kwargs) + self.processor = CLIPProcessor.from_pretrained(model_name) + self.query_text = query_text + + def fetch_image_from_url(self, url = "https://picsum.photos/800"): + """Fetches an image from the given url""" + response = requests.get(url) + if response.status_code != 200: + raise Exception("Failed to fetch an image") + image = Image.open(BytesIO(response.content)) + return image + + def load_image_from_path(self, path): + """Loads an image from the given path""" + return Image.open(path) + + def split_image(self, image, h_splits: int = 2, v_splits: int = 2): + """Splits the given image into h_splits x v_splits parts""" + width, height = image.size + w_step, h_step = width // h_splits, height // v_splits + slices = [] + + for i in range(v_splits): + for j in range(h_splits): + slice = image.crop( + (j * w_step, i * h_step, (j + 1) * w_step, (i + 1) * h_step) + ) + slices.append(slice) + return slices + + def get_vectors( + self, + image, + h_splits: int = 2, + v_splits: int = 2, + ): + """Gets the vectors for the given image""" + slices = self.split_image(image, h_splits, v_splits) + vectors = [] + + for slice in slices: + inputs = self.processor( + text=self.query_text, images=slice, return_tensors="pt", padding=True + ) + outputs = self.model(**inputs) + vectors.append(outputs.image_embeds.squeeze().detach().numpy()) + return vectors + + def run_from_url( + self, + url: str = "https://picsum.photos/800", + h_splits: int = 2, + v_splits: int = 2, + ): + """Runs the model on the image fetched from the given url""" + image = self.fetch_image_from_url(url) + return self.get_vectors(image, h_splits, v_splits) + + def check_hard_chunking(self, quadrants): + """Check if the chunking is hard""" + variances = [] + for quadrant in quadrants: + edge_pixels = torch.cat( + [ + quadrant[0, 1], + quadrant[-1, :], + ] + ) + variances.append(torch.var(edge_pixels).item()) + return variances + + def embed_whole_image(self, image): + """Embed the entire image""" + inputs = self.processor( + image, + return_tensors="pt", + ) + with torch.no_grad(): + outputs = self.model(**inputs) + return outputs.image_embeds.squeeze() + + def apply_noise_reduction(self, image, kernel_size: int = 5): + """Implement an upscaling method to upscale the image and tiling issues""" + blur = GaussianBlur(kernel_size) + return blur(image) + + def run_from_path(self, path: str = None, h_splits: int = 2, v_splits: int = 2): + """Runs the model on the image loaded from the given path""" + image = self.load_image_from_path(path) + return self.get_vectors(image, h_splits, v_splits) + + def get_captions(self, image, candidate_captions): + """Get the best caption for the given image""" + inputs_image = self.processor( + images=image, + return_tensors="pt", + ) + + inputs_text = self.processor( + text=candidate_captions, + images=inputs_image.pixel_values[0], # Fix the argument name + return_tensors="pt", + padding=True, + truncation=True, + ) + + image_embeds = self.model( + pixel_values=inputs_image.pixel_values[0] + ).image_embeds + text_embeds = self.model( + input_ids=inputs_text.input_ids, attention_mask=inputs_text.attention_mask + ).text_embeds + + # Calculate similarity between image and text + similarities = (image_embeds @ text_embeds.T).squeeze(0) + best_caption_index = similarities.argmax().item() + + return candidate_captions[best_caption_index] + + def get_and_concat_captions( + self, image, candidate_captions, h_splits=2, v_splits=2 + ): + """Get the best caption for the given image""" + slices = self.split_image(image, h_splits, v_splits) + captions = [self.get_captions(slice, candidate_captions) for slice in slices] + concated_captions = "".join(captions) + return concated_captions \ No newline at end of file From 970da21846d2cfa668c86a0649914c3479992bf4 Mon Sep 17 00:00:00 2001 From: Kye Date: Thu, 25 Jan 2024 20:01:54 -0500 Subject: [PATCH 060/103] [FEAT] --- swarms/utils/__init__.py | 7 +++ swarms/utils/json_output_parser.py | 82 ++++++++++++++++++++++++++++++ swarms/utils/yaml_output_parser.py | 76 +++++++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 swarms/utils/json_output_parser.py create mode 100644 swarms/utils/yaml_output_parser.py diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index df9fe6ca..b57f818d 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -25,6 +25,11 @@ from swarms.utils.download_weights_from_url import ( from swarms.utils.save_logs import parse_log_file +######## +from swarms.utils.yaml_output_parser import YamlOutputParser +from swarms.utils.json_output_parser import JsonOutputParser + + __all__ = [ "SubprocessCodeInterpreter", "display_markdown_message", @@ -45,4 +50,6 @@ __all__ = [ "try_except_wrapper", "download_weights_from_url", "parse_log_file", + "YamlOutputParser", + "JsonOutputParser", ] diff --git a/swarms/utils/json_output_parser.py b/swarms/utils/json_output_parser.py new file mode 100644 index 00000000..a180e408 --- /dev/null +++ b/swarms/utils/json_output_parser.py @@ -0,0 +1,82 @@ +import json +import re +from typing import Type, TypeVar +from pydantic import BaseModel, ValidationError + +T = TypeVar("T", bound=BaseModel) + +class JsonParsingException(Exception): + """Custom exception for errors in JSON parsing.""" + +class JsonOutputParser: + """Parse JSON output using a Pydantic model. + + This parser is designed to extract JSON formatted data from a given string + and parse it using a specified Pydantic model for validation. + + Attributes: + pydantic_object: A Pydantic model class for parsing and validation. + pattern: A regex pattern to match JSON code blocks. + + Examples: + >>> from pydantic import BaseModel + >>> from swarms.utils.json_output_parser import JsonOutputParser + >>> class MyModel(BaseModel): + ... name: str + ... age: int + ... + >>> parser = JsonOutputParser(MyModel) + >>> text = "```json\n{\"name\": \"John\", \"age\": 42}\n```" + >>> model = parser.parse(text) + >>> model.name + + """ + + def __init__(self, pydantic_object: Type[T]): + self.pydantic_object = pydantic_object + self.pattern = re.compile(r"^```(?:json)?(?P[^`]*)", re.MULTILINE | re.DOTALL) + + def parse(self, text: str) -> T: + """Parse the provided text to extract and validate JSON data. + + Args: + text: A string containing potential JSON data. + + Returns: + An instance of the specified Pydantic model with parsed data. + + Raises: + JsonParsingException: If parsing or validation fails. + """ + try: + match = re.search(self.pattern, text.strip()) + json_str = match.group("json") if match else text + + json_object = json.loads(json_str) + return self.pydantic_object.parse_obj(json_object) + + except (json.JSONDecodeError, ValidationError) as e: + name = self.pydantic_object.__name__ + msg = f"Failed to parse {name} from text '{text}'. Error: {e}" + raise JsonParsingException(msg) from e + + def get_format_instructions(self) -> str: + """Generate formatting instructions based on the Pydantic model schema. + + Returns: + A string containing formatting instructions. + """ + schema = self.pydantic_object.schema() + reduced_schema = {k: v for k, v in schema.items() if k not in ['title', 'type']} + schema_str = json.dumps(reduced_schema, indent=4) + + format_instructions = f"JSON Formatting Instructions:\n{schema_str}" + return format_instructions + +# # Example usage +# class ExampleModel(BaseModel): +# field1: int +# field2: str + +# parser = JsonOutputParser(ExampleModel) +# # Use parser.parse(text) to parse JSON data diff --git a/swarms/utils/yaml_output_parser.py b/swarms/utils/yaml_output_parser.py new file mode 100644 index 00000000..c6749d8a --- /dev/null +++ b/swarms/utils/yaml_output_parser.py @@ -0,0 +1,76 @@ +import json +import re +import yaml +from typing import Type, TypeVar +from pydantic import BaseModel, ValidationError + +T = TypeVar("T", bound=BaseModel) + +class YamlParsingException(Exception): + """Custom exception for errors in YAML parsing.""" + +class YamlOutputParser: + """Parse YAML output using a Pydantic model. + + This parser is designed to extract YAML formatted data from a given string + and parse it using a specified Pydantic model for validation. + + Attributes: + pydantic_object: A Pydantic model class for parsing and validation. + pattern: A regex pattern to match YAML code blocks. + + + Examples: + >>> from pydantic import BaseModel + >>> from swarms.utils.yaml_output_parser import YamlOutputParser + >>> class MyModel(BaseModel): + ... name: str + ... age: int + ... + >>> parser = YamlOutputParser(MyModel) + >>> text = "```yaml\nname: John\nage: 42\n```" + >>> model = parser.parse(text) + >>> model.name + + """ + + def __init__(self, pydantic_object: Type[T]): + self.pydantic_object = pydantic_object + self.pattern = re.compile(r"^```(?:ya?ml)?(?P[^`]*)", re.MULTILINE | re.DOTALL) + + def parse(self, text: str) -> T: + """Parse the provided text to extract and validate YAML data. + + Args: + text: A string containing potential YAML data. + + Returns: + An instance of the specified Pydantic model with parsed data. + + Raises: + YamlParsingException: If parsing or validation fails. + """ + try: + match = re.search(self.pattern, text.strip()) + yaml_str = match.group("yaml") if match else text + + json_object = yaml.safe_load(yaml_str) + return self.pydantic_object.parse_obj(json_object) + + except (yaml.YAMLError, ValidationError) as e: + name = self.pydantic_object.__name__ + msg = f"Failed to parse {name} from text '{text}'. Error: {e}" + raise YamlParsingException(msg) from e + + def get_format_instructions(self) -> str: + """Generate formatting instructions based on the Pydantic model schema. + + Returns: + A string containing formatting instructions. + """ + schema = self.pydantic_object.schema() + reduced_schema = {k: v for k, v in schema.items() if k not in ['title', 'type']} + schema_str = json.dumps(reduced_schema, indent=4) + + format_instructions = f"YAML Formatting Instructions:\n{schema_str}" + return format_instructions From 1b15753424cf964ea942eff146df830ad1a423d6 Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 26 Jan 2024 10:15:26 -0500 Subject: [PATCH 061/103] [FEATS][ remove_whitespace_from_json, remove_whitespace_from_yaml] --- pyproject.toml | 2 +- swarms/utils/__init__.py | 7 +++- swarms/utils/remove_json_whitespace.py | 48 ++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 swarms/utils/remove_json_whitespace.py diff --git a/pyproject.toml b/pyproject.toml index b7a39e00..7a45a177 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.7.9" +version = "3.8.1" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index b57f818d..7593f0aa 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -28,7 +28,10 @@ from swarms.utils.save_logs import parse_log_file ######## from swarms.utils.yaml_output_parser import YamlOutputParser from swarms.utils.json_output_parser import JsonOutputParser - +from swarms.utils.remove_json_whitespace import ( + remove_whitespace_from_json, + remove_whitespace_from_yaml, +) __all__ = [ "SubprocessCodeInterpreter", @@ -52,4 +55,6 @@ __all__ = [ "parse_log_file", "YamlOutputParser", "JsonOutputParser", + "remove_whitespace_from_json", + "remove_whitespace_from_yaml", ] diff --git a/swarms/utils/remove_json_whitespace.py b/swarms/utils/remove_json_whitespace.py new file mode 100644 index 00000000..842e04c1 --- /dev/null +++ b/swarms/utils/remove_json_whitespace.py @@ -0,0 +1,48 @@ +import json +import yaml + +def remove_whitespace_from_json(json_string: str) -> str: + """ + Removes unnecessary whitespace from a JSON string. + + This function parses the JSON string into a Python object and then + serializes it back into a JSON string without unnecessary whitespace. + + Args: + json_string (str): The JSON string. + + Returns: + str: The JSON string with whitespace removed. + """ + parsed = json.loads(json_string) + return json.dumps(parsed, separators=(',', ':')) + +# # Example usage for JSON +# json_string = '{"field1": 123, "field2": "example text"}' +# print(remove_whitespace_from_json(json_string)) + + + +def remove_whitespace_from_yaml(yaml_string: str) -> str: + """ + Removes unnecessary whitespace from a YAML string. + + This function parses the YAML string into a Python object and then + serializes it back into a YAML string with minimized whitespace. + Note: This might change the representation style of YAML data. + + Args: + yaml_string (str): The YAML string. + + Returns: + str: The YAML string with whitespace reduced. + """ + parsed = yaml.safe_load(yaml_string) + return yaml.dump(parsed, default_flow_style=True) + +# # Example usage for YAML +# yaml_string = """ +# field1: 123 +# field2: example text +# """ +# print(remove_whitespace_from_yaml(yaml_string)) From 8113d6ddbcf23e358aa4c7e2bfff7195a1047a27 Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 27 Jan 2024 12:04:42 -0500 Subject: [PATCH 062/103] [CODE QUALITY] --- playground/agents/worker_example.py | 2 +- playground/demos/fof/langchain.py | 1 - swarms/agents/agent_wrapper.py | 11 ++--- swarms/agents/worker_agent.py | 3 +- swarms/models/__init__.py | 1 - swarms/models/clipq.py | 56 ++++++++++++++++++-------- swarms/structs/agent.py | 9 ++++- swarms/structs/task.py | 2 +- swarms/telemetry/sys_info.py | 2 - swarms/telemetry/user_utils.py | 7 ++-- swarms/utils/__init__.py | 2 +- swarms/utils/json_output_parser.py | 26 +++++++++--- swarms/utils/remove_json_whitespace.py | 6 ++- swarms/utils/yaml_output_parser.py | 27 +++++++++---- 14 files changed, 105 insertions(+), 50 deletions(-) diff --git a/playground/agents/worker_example.py b/playground/agents/worker_example.py index bd8d47c0..9e215e83 100644 --- a/playground/agents/worker_example.py +++ b/playground/agents/worker_example.py @@ -18,7 +18,7 @@ worker = Worker( tools=[], temperature=0.5, llm=OpenAIChat(openai_api_key=api_key), - verbose = True, + verbose=True, ) # Running the worker with a prompt diff --git a/playground/demos/fof/langchain.py b/playground/demos/fof/langchain.py index ef338520..dd6d7083 100644 --- a/playground/demos/fof/langchain.py +++ b/playground/demos/fof/langchain.py @@ -2,4 +2,3 @@ This tutorial shows you how to integrate swarms with Langchain """ - diff --git a/swarms/agents/agent_wrapper.py b/swarms/agents/agent_wrapper.py index 109048e9..738f599d 100644 --- a/swarms/agents/agent_wrapper.py +++ b/swarms/agents/agent_wrapper.py @@ -1,18 +1,19 @@ from swarms.structs.agent import Agent + def agent_wrapper(ClassToWrap): """ This function takes a class 'ClassToWrap' and returns a new class that inherits from both 'ClassToWrap' and 'Agent'. The new class overrides the '__init__' method of 'Agent' to call the '__init__' method of 'ClassToWrap'. - + Args: ClassToWrap (type): The class to be wrapped and made to inherit from 'Agent'. - + Returns: type: The new class that inherits from both 'ClassToWrap' and 'Agent'. """ - + class WrappedClass(ClassToWrap, Agent): def __init__(self, *args, **kwargs): try: @@ -21,5 +22,5 @@ def agent_wrapper(ClassToWrap): except Exception as e: print(f"Error initializing WrappedClass: {e}") raise e - - return WrappedClass \ No newline at end of file + + return WrappedClass diff --git a/swarms/agents/worker_agent.py b/swarms/agents/worker_agent.py index d3ebb1d3..d254acef 100644 --- a/swarms/agents/worker_agent.py +++ b/swarms/agents/worker_agent.py @@ -9,6 +9,7 @@ from langchain_experimental.autonomous_agents import AutoGPT from swarms.utils.decorators import error_decorator, timing_decorator + class Worker: """ The Worker class represents an autonomous agent that can perform tassks through @@ -164,7 +165,7 @@ class Worker: # @log_decorator @error_decorator @timing_decorator - def run(self, task: str = None, img = None, *args, **kwargs): + def run(self, task: str = None, img=None, *args, **kwargs): """ Run the autonomous agent on a given task. diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index d9a1e96d..fcd67dc6 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -112,5 +112,4 @@ __all__ = [ "LavaMultiModal", "QwenVLMultiModal", "CLIPQ", - ] diff --git a/swarms/models/clipq.py b/swarms/models/clipq.py index c70190fd..7e49e74a 100644 --- a/swarms/models/clipq.py +++ b/swarms/models/clipq.py @@ -10,17 +10,17 @@ from transformers import CLIPModel, CLIPProcessor class CLIPQ: """ ClipQ is an CLIQ based model that can be used to generate captions for images. - - + + Attributes: model_name (str): The name of the model to be used. query_text (str): The query text to be used for the model. - + Args: model_name (str): The name of the model to be used. query_text (str): The query text to be used for the model. - - + + """ @@ -30,13 +30,15 @@ class CLIPQ: model_name: str = "openai/clip-vit-base-patch16", query_text: str = "A photo ", *args, - **kwargs + **kwargs, ): - self.model = CLIPModel.from_pretrained(model_name, *args, **kwargs) + self.model = CLIPModel.from_pretrained( + model_name, *args, **kwargs + ) self.processor = CLIPProcessor.from_pretrained(model_name) self.query_text = query_text - def fetch_image_from_url(self, url = "https://picsum.photos/800"): + def fetch_image_from_url(self, url="https://picsum.photos/800"): """Fetches an image from the given url""" response = requests.get(url) if response.status_code != 200: @@ -48,7 +50,9 @@ class CLIPQ: """Loads an image from the given path""" return Image.open(path) - def split_image(self, image, h_splits: int = 2, v_splits: int = 2): + def split_image( + self, image, h_splits: int = 2, v_splits: int = 2 + ): """Splits the given image into h_splits x v_splits parts""" width, height = image.size w_step, h_step = width // h_splits, height // v_splits @@ -57,7 +61,12 @@ class CLIPQ: for i in range(v_splits): for j in range(h_splits): slice = image.crop( - (j * w_step, i * h_step, (j + 1) * w_step, (i + 1) * h_step) + ( + j * w_step, + i * h_step, + (j + 1) * w_step, + (i + 1) * h_step, + ) ) slices.append(slice) return slices @@ -74,10 +83,15 @@ class CLIPQ: for slice in slices: inputs = self.processor( - text=self.query_text, images=slice, return_tensors="pt", padding=True + text=self.query_text, + images=slice, + return_tensors="pt", + padding=True, ) outputs = self.model(**inputs) - vectors.append(outputs.image_embeds.squeeze().detach().numpy()) + vectors.append( + outputs.image_embeds.squeeze().detach().numpy() + ) return vectors def run_from_url( @@ -118,7 +132,9 @@ class CLIPQ: blur = GaussianBlur(kernel_size) return blur(image) - def run_from_path(self, path: str = None, h_splits: int = 2, v_splits: int = 2): + def run_from_path( + self, path: str = None, h_splits: int = 2, v_splits: int = 2 + ): """Runs the model on the image loaded from the given path""" image = self.load_image_from_path(path) return self.get_vectors(image, h_splits, v_splits) @@ -132,7 +148,9 @@ class CLIPQ: inputs_text = self.processor( text=candidate_captions, - images=inputs_image.pixel_values[0], # Fix the argument name + images=inputs_image.pixel_values[ + 0 + ], # Fix the argument name return_tensors="pt", padding=True, truncation=True, @@ -142,7 +160,8 @@ class CLIPQ: pixel_values=inputs_image.pixel_values[0] ).image_embeds text_embeds = self.model( - input_ids=inputs_text.input_ids, attention_mask=inputs_text.attention_mask + input_ids=inputs_text.input_ids, + attention_mask=inputs_text.attention_mask, ).text_embeds # Calculate similarity between image and text @@ -156,6 +175,9 @@ class CLIPQ: ): """Get the best caption for the given image""" slices = self.split_image(image, h_splits, v_splits) - captions = [self.get_captions(slice, candidate_captions) for slice in slices] + captions = [ + self.get_captions(slice, candidate_captions) + for slice in slices + ] concated_captions = "".join(captions) - return concated_captions \ No newline at end of file + return concated_captions diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index dace1ff3..3b298e3a 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -670,10 +670,15 @@ class Agent: """ ltr = self.long_term_memory.query(query) - return f"""{prompt} - ################ CONTEXT #################### + context = f""" + {prompt} + ####### Long Term Memory ################ {ltr} """ + return self.short_memory.append([f"{context}"]) + + def add_memory(self, message: str): + return self.short_memory.append([f"{message}"]) async def run_concurrent(self, tasks: List[str], **kwargs): """ diff --git a/swarms/structs/task.py b/swarms/structs/task.py index fd6a1ab8..fb89b7bf 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -233,7 +233,7 @@ class Task: if task.description is not None else "" ) - + result = ( task.result if task.result is not None else "" ) diff --git a/swarms/telemetry/sys_info.py b/swarms/telemetry/sys_info.py index d2841585..a4857e11 100644 --- a/swarms/telemetry/sys_info.py +++ b/swarms/telemetry/sys_info.py @@ -89,7 +89,6 @@ def get_package_mismatches(file_path="pyproject.toml"): return "\n" + "\n".join(mismatches) - def system_info(): swarms_verison = get_swarms_verison() return { @@ -100,4 +99,3 @@ def system_info(): "CPU Info": get_cpu_info(), "RAM Info": get_ram_info(), } - diff --git a/swarms/telemetry/user_utils.py b/swarms/telemetry/user_utils.py index 9369bc26..4d4fb166 100644 --- a/swarms/telemetry/user_utils.py +++ b/swarms/telemetry/user_utils.py @@ -5,6 +5,7 @@ import socket from swarms.telemetry.sys_info import system_info from swarms.telemetry.check_update import check_for_package + # Helper functions def generate_user_id(): """Generate user id @@ -75,7 +76,6 @@ def get_local_ip(): return socket.gethostbyname(socket.gethostname()) - def get_user_device_data(): data = { "ID": generate_user_id(), @@ -85,5 +85,6 @@ def get_user_device_data(): "Swarms [Version]": check_for_package("swarms"), } return data - -# \ No newline at end of file + + +# diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index 7593f0aa..c8eaabaa 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -25,7 +25,7 @@ from swarms.utils.download_weights_from_url import ( from swarms.utils.save_logs import parse_log_file -######## +######## from swarms.utils.yaml_output_parser import YamlOutputParser from swarms.utils.json_output_parser import JsonOutputParser from swarms.utils.remove_json_whitespace import ( diff --git a/swarms/utils/json_output_parser.py b/swarms/utils/json_output_parser.py index a180e408..724d5ed5 100644 --- a/swarms/utils/json_output_parser.py +++ b/swarms/utils/json_output_parser.py @@ -5,9 +5,11 @@ from pydantic import BaseModel, ValidationError T = TypeVar("T", bound=BaseModel) + class JsonParsingException(Exception): """Custom exception for errors in JSON parsing.""" + class JsonOutputParser: """Parse JSON output using a Pydantic model. @@ -17,7 +19,7 @@ class JsonOutputParser: Attributes: pydantic_object: A Pydantic model class for parsing and validation. pattern: A regex pattern to match JSON code blocks. - + Examples: >>> from pydantic import BaseModel >>> from swarms.utils.json_output_parser import JsonOutputParser @@ -29,12 +31,14 @@ class JsonOutputParser: >>> text = "```json\n{\"name\": \"John\", \"age\": 42}\n```" >>> model = parser.parse(text) >>> model.name - + """ def __init__(self, pydantic_object: Type[T]): self.pydantic_object = pydantic_object - self.pattern = re.compile(r"^```(?:json)?(?P[^`]*)", re.MULTILINE | re.DOTALL) + self.pattern = re.compile( + r"^```(?:json)?(?P[^`]*)", re.MULTILINE | re.DOTALL + ) def parse(self, text: str) -> T: """Parse the provided text to extract and validate JSON data. @@ -57,7 +61,10 @@ class JsonOutputParser: except (json.JSONDecodeError, ValidationError) as e: name = self.pydantic_object.__name__ - msg = f"Failed to parse {name} from text '{text}'. Error: {e}" + msg = ( + f"Failed to parse {name} from text '{text}'." + f" Error: {e}" + ) raise JsonParsingException(msg) from e def get_format_instructions(self) -> str: @@ -67,12 +74,19 @@ class JsonOutputParser: A string containing formatting instructions. """ schema = self.pydantic_object.schema() - reduced_schema = {k: v for k, v in schema.items() if k not in ['title', 'type']} + reduced_schema = { + k: v + for k, v in schema.items() + if k not in ["title", "type"] + } schema_str = json.dumps(reduced_schema, indent=4) - format_instructions = f"JSON Formatting Instructions:\n{schema_str}" + format_instructions = ( + f"JSON Formatting Instructions:\n{schema_str}" + ) return format_instructions + # # Example usage # class ExampleModel(BaseModel): # field1: int diff --git a/swarms/utils/remove_json_whitespace.py b/swarms/utils/remove_json_whitespace.py index 842e04c1..a5b3f7de 100644 --- a/swarms/utils/remove_json_whitespace.py +++ b/swarms/utils/remove_json_whitespace.py @@ -1,6 +1,7 @@ import json import yaml + def remove_whitespace_from_json(json_string: str) -> str: """ Removes unnecessary whitespace from a JSON string. @@ -15,14 +16,14 @@ def remove_whitespace_from_json(json_string: str) -> str: str: The JSON string with whitespace removed. """ parsed = json.loads(json_string) - return json.dumps(parsed, separators=(',', ':')) + return json.dumps(parsed, separators=(",", ":")) + # # Example usage for JSON # json_string = '{"field1": 123, "field2": "example text"}' # print(remove_whitespace_from_json(json_string)) - def remove_whitespace_from_yaml(yaml_string: str) -> str: """ Removes unnecessary whitespace from a YAML string. @@ -40,6 +41,7 @@ def remove_whitespace_from_yaml(yaml_string: str) -> str: parsed = yaml.safe_load(yaml_string) return yaml.dump(parsed, default_flow_style=True) + # # Example usage for YAML # yaml_string = """ # field1: 123 diff --git a/swarms/utils/yaml_output_parser.py b/swarms/utils/yaml_output_parser.py index c6749d8a..61be311b 100644 --- a/swarms/utils/yaml_output_parser.py +++ b/swarms/utils/yaml_output_parser.py @@ -6,9 +6,11 @@ from pydantic import BaseModel, ValidationError T = TypeVar("T", bound=BaseModel) + class YamlParsingException(Exception): """Custom exception for errors in YAML parsing.""" + class YamlOutputParser: """Parse YAML output using a Pydantic model. @@ -18,8 +20,8 @@ class YamlOutputParser: Attributes: pydantic_object: A Pydantic model class for parsing and validation. pattern: A regex pattern to match YAML code blocks. - - + + Examples: >>> from pydantic import BaseModel >>> from swarms.utils.yaml_output_parser import YamlOutputParser @@ -31,12 +33,14 @@ class YamlOutputParser: >>> text = "```yaml\nname: John\nage: 42\n```" >>> model = parser.parse(text) >>> model.name - + """ def __init__(self, pydantic_object: Type[T]): self.pydantic_object = pydantic_object - self.pattern = re.compile(r"^```(?:ya?ml)?(?P[^`]*)", re.MULTILINE | re.DOTALL) + self.pattern = re.compile( + r"^```(?:ya?ml)?(?P[^`]*)", re.MULTILINE | re.DOTALL + ) def parse(self, text: str) -> T: """Parse the provided text to extract and validate YAML data. @@ -59,7 +63,10 @@ class YamlOutputParser: except (yaml.YAMLError, ValidationError) as e: name = self.pydantic_object.__name__ - msg = f"Failed to parse {name} from text '{text}'. Error: {e}" + msg = ( + f"Failed to parse {name} from text '{text}'." + f" Error: {e}" + ) raise YamlParsingException(msg) from e def get_format_instructions(self) -> str: @@ -69,8 +76,14 @@ class YamlOutputParser: A string containing formatting instructions. """ schema = self.pydantic_object.schema() - reduced_schema = {k: v for k, v in schema.items() if k not in ['title', 'type']} + reduced_schema = { + k: v + for k, v in schema.items() + if k not in ["title", "type"] + } schema_str = json.dumps(reduced_schema, indent=4) - format_instructions = f"YAML Formatting Instructions:\n{schema_str}" + format_instructions = ( + f"YAML Formatting Instructions:\n{schema_str}" + ) return format_instructions From d5c0ca0128b6346dc7dfda5ee81e3a02ebd39327 Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 27 Jan 2024 19:31:38 -0500 Subject: [PATCH 063/103] [FEAT][Conversation] --- swarms/memory/__init__.py | 4 +- swarms/memory/base_db.py | 2 +- swarms/memory/base_vectordatabase.py | 142 +++++++++++++++++++++++++++ swarms/memory/base_vectordb.py | 58 ----------- swarms/memory/pinecone.py | 4 +- swarms/memory/sqlite.py | 4 +- swarms/memory/weaviate_db.py | 4 +- swarms/structs/agent.py | 10 +- swarms/structs/conversation.py | 6 ++ swarms/structs/multi_agent_rag.py | 97 ++++++++++++++++++ 10 files changed, 259 insertions(+), 72 deletions(-) create mode 100644 swarms/memory/base_vectordatabase.py delete mode 100644 swarms/memory/base_vectordb.py create mode 100644 swarms/structs/multi_agent_rag.py diff --git a/swarms/memory/__init__.py b/swarms/memory/__init__.py index d2eed0d5..1d52d718 100644 --- a/swarms/memory/__init__.py +++ b/swarms/memory/__init__.py @@ -1,11 +1,11 @@ -from swarms.memory.base_vectordb import VectorDatabase +from swarms.memory.base_vectordb import AbstractDatabase from swarms.memory.short_term_memory import ShortTermMemory from swarms.memory.sqlite import SQLiteDB from swarms.memory.weaviate_db import WeaviateDB from swarms.memory.visual_memory import VisualShortTermMemory __all__ = [ - "VectorDatabase", + "AbstractDatabase", "ShortTermMemory", "SQLiteDB", "WeaviateDB", diff --git a/swarms/memory/base_db.py b/swarms/memory/base_db.py index 0501def7..bb0a2961 100644 --- a/swarms/memory/base_db.py +++ b/swarms/memory/base_db.py @@ -156,4 +156,4 @@ class AbstractDatabase(ABC): """ - pass + pass \ No newline at end of file diff --git a/swarms/memory/base_vectordatabase.py b/swarms/memory/base_vectordatabase.py new file mode 100644 index 00000000..734c872a --- /dev/null +++ b/swarms/memory/base_vectordatabase.py @@ -0,0 +1,142 @@ +from abc import ABC, abstractmethod + + +class AbstractVectorDatabase(ABC): + """ + Abstract base class for a database. + + This class defines the interface for interacting with a database. + Subclasses must implement the abstract methods to provide the + specific implementation details for connecting to a database, + executing queries, and performing CRUD operations. + + """ + + @abstractmethod + def connect(self): + """ + Connect to the database. + + This method establishes a connection to the database. + + """ + + pass + + @abstractmethod + def close(self): + """ + Close the database connection. + + This method closes the connection to the database. + + """ + + pass + + @abstractmethod + def query(self, query: str): + """ + Execute a database query. + + This method executes the given query on the database. + + Parameters: + query (str): The query to be executed. + + """ + + pass + + @abstractmethod + def fetch_all(self): + """ + Fetch all rows from the result set. + + This method retrieves all rows from the result set of a query. + + Returns: + list: A list of dictionaries representing the rows. + + """ + + pass + + @abstractmethod + def fetch_one(self): + """ + Fetch one row from the result set. + + This method retrieves one row from the result set of a query. + + Returns: + dict: A dictionary representing the row. + + """ + + pass + + @abstractmethod + def add(self, doc: str): + """ + Add a new record to the database. + + This method adds a new record to the specified table in the database. + + Parameters: + table (str): The name of the table. + data (dict): A dictionary representing the data to be added. + + """ + + pass + + + @abstractmethod + def get(self, query: str): + """ + Get a record from the database. + + This method retrieves a record from the specified table in the database based on the given ID. + + Parameters: + table (str): The name of the table. + id (int): The ID of the record to be retrieved. + + Returns: + dict: A dictionary representing the retrieved record. + + """ + + pass + + @abstractmethod + def update(self, doc): + """ + Update a record in the database. + + This method updates a record in the specified table in the database based on the given ID. + + Parameters: + table (str): The name of the table. + id (int): The ID of the record to be updated. + data (dict): A dictionary representing the updated data. + + """ + + pass + + @abstractmethod + def delete(self, message): + """ + Delete a record from the database. + + This method deletes a record from the specified table in the database based on the given ID. + + Parameters: + table (str): The name of the table. + id (int): The ID of the record to be deleted. + + """ + + pass \ No newline at end of file diff --git a/swarms/memory/base_vectordb.py b/swarms/memory/base_vectordb.py deleted file mode 100644 index 841c6147..00000000 --- a/swarms/memory/base_vectordb.py +++ /dev/null @@ -1,58 +0,0 @@ -from abc import ABC, abstractmethod -from typing import Any, Dict - - -class VectorDatabase(ABC): - @abstractmethod - def add( - self, vector: Dict[str, Any], metadata: Dict[str, Any] - ) -> None: - """ - add a vector into the database. - - Args: - vector (Dict[str, Any]): The vector to add. - metadata (Dict[str, Any]): Metadata associated with the vector. - """ - pass - - @abstractmethod - def query(self, text: str, num_results: int) -> Dict[str, Any]: - """ - Query the database for vectors similar to the given vector. - - Args: - text (Dict[str, Any]): The vector to compare against. - num_results (int): The number of similar vectors to return. - - Returns: - Dict[str, Any]: The most similar vectors and their associated metadata. - """ - pass - - @abstractmethod - def delete(self, vector_id: str) -> None: - """ - Delete a vector from the database. - - Args: - vector_id (str): The ID of the vector to delete. - """ - pass - - @abstractmethod - def update( - self, - vector_id: str, - vector: Dict[str, Any], - metadata: Dict[str, Any], - ) -> None: - """ - Update a vector in the database. - - Args: - vector_id (str): The ID of the vector to update. - vector (Dict[str, Any]): The new vector. - metadata (Dict[str, Any]): The new metadata. - """ - pass diff --git a/swarms/memory/pinecone.py b/swarms/memory/pinecone.py index 164cb334..bf073d3e 100644 --- a/swarms/memory/pinecone.py +++ b/swarms/memory/pinecone.py @@ -1,12 +1,12 @@ from typing import Optional -from swarms.memory.base_vectordb import VectorDatabase +from swarms.memory.base_vectordb import AbstractDatabase import pinecone from attr import define, field from swarms.utils.hash import str_to_hash @define -class PineconeDB(VectorDatabase): +class PineconeDB(AbstractDatabase): """ PineconeDB is a vector storage driver that uses Pinecone as the underlying storage engine. diff --git a/swarms/memory/sqlite.py b/swarms/memory/sqlite.py index eed4ee2c..2c4f2740 100644 --- a/swarms/memory/sqlite.py +++ b/swarms/memory/sqlite.py @@ -1,5 +1,5 @@ from typing import List, Tuple, Any, Optional -from swarms.memory.base_vectordb import VectorDatabase +from swarms.memory.base_vectordb import AbstractDatabase try: import sqlite3 @@ -9,7 +9,7 @@ except ImportError: ) -class SQLiteDB(VectorDatabase): +class SQLiteDB(AbstractDatabase): """ A reusable class for SQLite database operations with methods for adding, deleting, updating, and querying data. diff --git a/swarms/memory/weaviate_db.py b/swarms/memory/weaviate_db.py index 0c0b09a2..fec1199e 100644 --- a/swarms/memory/weaviate_db.py +++ b/swarms/memory/weaviate_db.py @@ -4,7 +4,7 @@ Weaviate API Client from typing import Any, Dict, List, Optional -from swarms.memory.base_vectordb import VectorDatabase +from swarms.memory.base_vectordb import AbstractDatabase try: import weaviate @@ -12,7 +12,7 @@ except ImportError: print("pip install weaviate-client") -class WeaviateDB(VectorDatabase): +class WeaviateDB(AbstractDatabase): """ Weaviate API Client diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 3b298e3a..bd245ba7 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -9,7 +9,7 @@ from typing import Any, Callable, Dict, List, Optional, Tuple from termcolor import colored -from swarms.memory.base_vectordb import VectorDatabase +from swarms.memory.base_vectordb import AbstractDatabase from swarms.prompts.agent_system_prompts import ( AGENT_SYSTEM_PROMPT_3, ) @@ -83,7 +83,7 @@ class Agent: pdf_path (str): The path to the pdf list_of_pdf (str): The list of pdf tokenizer (Any): The tokenizer - memory (VectorDatabase): The memory + memory (AbstractDatabase): The memory preset_stopping_token (bool): Enable preset stopping token traceback (Any): The traceback traceback_handlers (Any): The traceback handlers @@ -168,7 +168,7 @@ class Agent: pdf_path: Optional[str] = None, list_of_pdf: Optional[str] = None, tokenizer: Optional[Any] = None, - long_term_memory: Optional[VectorDatabase] = None, + long_term_memory: Optional[AbstractDatabase] = None, preset_stopping_token: Optional[bool] = False, traceback: Any = None, traceback_handlers: Any = None, @@ -657,7 +657,7 @@ class Agent: """ return agent_history_prompt - def long_term_memory_prompt(self, query: str, prompt: str): + def long_term_memory_prompt(self, query: str): """ Generate the agent long term memory prompt @@ -671,7 +671,7 @@ class Agent: ltr = self.long_term_memory.query(query) context = f""" - {prompt} + {query} ####### Long Term Memory ################ {ltr} """ diff --git a/swarms/structs/conversation.py b/swarms/structs/conversation.py index 441ff3d9..9a2224a4 100644 --- a/swarms/structs/conversation.py +++ b/swarms/structs/conversation.py @@ -60,6 +60,7 @@ class Conversation(BaseStructure): def __init__( self, + system_prompt: str, time_enabled: bool = False, database: AbstractDatabase = None, autosave: bool = False, @@ -68,11 +69,16 @@ class Conversation(BaseStructure): **kwargs, ): super().__init__() + self.system_prompt = system_prompt self.time_enabled = time_enabled self.database = database self.autosave = autosave self.save_filepath = save_filepath self.conversation_history = [] + + # If system prompt is not None, add it to the conversation history + if self.system_prompt: + self.add("system", self.system_prompt) def add(self, role: str, content: str, *args, **kwargs): """Add a message to the conversation history diff --git a/swarms/structs/multi_agent_rag.py b/swarms/structs/multi_agent_rag.py new file mode 100644 index 00000000..7b51332e --- /dev/null +++ b/swarms/structs/multi_agent_rag.py @@ -0,0 +1,97 @@ +from dataclasses import dataclass +from typing import List, Optional + +from swarms.memory.base_vectordatabase import AbstractVectorDatabase +from swarms.structs.agent import Agent + + +@dataclass +class MultiAgentRag: + """ + Represents a multi-agent RAG (Relational Agent Graph) structure. + + Attributes: + agents (List[Agent]): List of agents in the multi-agent RAG. + db (AbstractVectorDatabase): Database used for querying. + verbose (bool): Flag indicating whether to print verbose output. + """ + agents: List[Agent] + db: AbstractVectorDatabase + verbose: bool = False + + + def query_database(self, query: str): + """ + Queries the database using the given query string. + + Args: + query (str): The query string. + + Returns: + List: The list of results from the database. + """ + results = [] + for agent in self.agents: + agent_results = agent.long_term_memory_prompt(query) + results.extend(agent_results) + return results + + + def get_agent_by_id(self, agent_id) -> Optional[Agent]: + """ + Retrieves an agent from the multi-agent RAG by its ID. + + Args: + agent_id: The ID of the agent to retrieve. + + Returns: + Agent or None: The agent with the specified ID, or None if not found. + """ + for agent in self.agents: + if agent.agent_id == agent_id: + return agent + return None + + def add_message( + self, + sender: Agent, + message: str, + *args, + **kwargs + ): + """ + Adds a message to the database. + + Args: + sender (Agent): The agent sending the message. + message (str): The message to add. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + + Returns: + int: The ID of the added message. + """ + doc = f"{sender.ai_name}: {message}" + + return self.db.add(doc) + + def query( + self, + message: str, + *args, + **kwargs + ): + """ + Queries the database using the given message. + + Args: + message (str): The message to query. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + + Returns: + List: The list of results from the database. + """ + return self.db.query(message) + + From e70b401b5489992775f6b141f9fbaa9e2b3f9849 Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 27 Jan 2024 20:17:53 -0500 Subject: [PATCH 064/103] [README] --- README.md | 153 +++++++++++++++++++++++++---------- playground/models/idefics.py | 15 +++- playground/models/kosmos.py | 10 +++ pyproject.toml | 2 +- swarms/models/__init__.py | 8 +- 5 files changed, 139 insertions(+), 49 deletions(-) create mode 100644 playground/models/kosmos.py diff --git a/README.md b/README.md index 64db98ae..aa8822e8 100644 --- a/README.md +++ b/README.md @@ -824,6 +824,115 @@ out = llm.run(task=task, img=img) print(out) ``` +### `GPT4Vision` +```python +from swarms import GPT4VisionAPI + +# Initialize with default API key and custom max_tokens +api = GPT4VisionAPI(max_tokens=1000) + +# Define the task and image URL +task = "Describe the scene in the image." +img = "https://i.imgur.com/4P4ZRxU.jpeg" + +# Run the GPT-4 Vision model +response = api.run(task, img) + +# Print the model's response +print(response) +``` + +### `QwenVLMultiModal` +A radically simple interface for QwenVLMultiModal comes complete with Quantization to turn it on just set quantize to true! + +```python +from swarms import QwenVLMultiModal + +# Instantiate the QwenVLMultiModal model +model = QwenVLMultiModal( + model_name="Qwen/Qwen-VL-Chat", + device="cuda", + quantize=True, +) + +# Run the model +response = model( + "Hello, how are you?", "https://example.com/image.jpg" +) + +# Print the response +print(response) + + +``` + + +### `Kosmos` +- Multi-Modal Model from microsoft! + +```python +from swarms import Kosmos + +# Initialize the model +model = Kosmos() + +# Generate +out = model.run("Analyze the reciepts in this image", "docs.jpg") + +# Print the output +print(out) + +``` + + +### `Idefics` +- Multi-Modal model from Huggingface team! + +```python +# Import the idefics model from the swarms.models module +from swarms.models import Idefics + +# Create an instance of the idefics model +model = Idefics() + +# Define user input with an image URL and chat with the model +user_input = ( + "User: What is in this image?" + " https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG" +) +response = model.chat(user_input) +print(response) + +# Define another user input with an image URL and chat with the model +user_input = ( + "User: And who is that?" + " https://static.wikia.nocookie.net/asterix/images/2/25/R22b.gif/revision/latest?cb=20110815073052" +) +response = model.chat(user_input) +print(response) + +# Set the checkpoint of the model to "new_checkpoint" +model.set_checkpoint("new_checkpoint") + +# Set the device of the model to "cpu" +model.set_device("cpu") + +# Set the maximum length of the chat to 200 +model.set_max_length(200) + +# Clear the chat history of the model +model.clear_chat_history() + + +``` + +## Radically Simple AI Model APIs +We provide a vast array of language and multi-modal model APIs for you to generate text, images, music, speech, and even videos. Get started below: + + + +----- + ### `Anthropic` ```python @@ -900,23 +1009,6 @@ print(image_url) ``` -### `GPT4Vision` -```python -from swarms import GPT4VisionAPI - -# Initialize with default API key and custom max_tokens -api = GPT4VisionAPI(max_tokens=1000) - -# Define the task and image URL -task = "Describe the scene in the image." -img = "https://i.imgur.com/4P4ZRxU.jpeg" - -# Run the GPT-4 Vision model -response = api.run(task, img) - -# Print the model's response -print(response) -``` ### Text to Video with `ZeroscopeTTV` @@ -938,7 +1030,7 @@ print(video_path) ``` -### ModelScope + -### `QwenVLMultiModal` -A radically simple interface for QwenVLMultiModal comes complete with Quantization to turn it on just set quantize to true! - -```python -from swarms import QwenVLMultiModal - -# Instantiate the QwenVLMultiModal model -model = QwenVLMultiModal( - model_name="Qwen/Qwen-VL-Chat", - device="cuda", - quantize=True, -) - -# Run the model -response = model( - "Hello, how are you?", "https://example.com/image.jpg" -) - -# Print the response -print(response) - - -``` ---- diff --git a/playground/models/idefics.py b/playground/models/idefics.py index 39d6f4eb..ea36ba77 100644 --- a/playground/models/idefics.py +++ b/playground/models/idefics.py @@ -1,7 +1,10 @@ -from swarms.models import idefics +# Import the idefics model from the swarms.models module +from swarms.models import Idefics -model = idefics() +# Create an instance of the idefics model +model = Idefics() +# Define user input with an image URL and chat with the model user_input = ( "User: What is in this image?" " https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG" @@ -9,6 +12,7 @@ user_input = ( response = model.chat(user_input) print(response) +# Define another user input with an image URL and chat with the model user_input = ( "User: And who is that?" " https://static.wikia.nocookie.net/asterix/images/2/25/R22b.gif/revision/latest?cb=20110815073052" @@ -16,7 +20,14 @@ user_input = ( response = model.chat(user_input) print(response) +# Set the checkpoint of the model to "new_checkpoint" model.set_checkpoint("new_checkpoint") + +# Set the device of the model to "cpu" model.set_device("cpu") + +# Set the maximum length of the chat to 200 model.set_max_length(200) + +# Clear the chat history of the model model.clear_chat_history() diff --git a/playground/models/kosmos.py b/playground/models/kosmos.py new file mode 100644 index 00000000..3d0f1dd2 --- /dev/null +++ b/playground/models/kosmos.py @@ -0,0 +1,10 @@ +from swarms import Kosmos + +# Initialize the model +model = Kosmos() + +# Generate +out = model.run("Analyze the reciepts in this image", "docs.jpg") + +# Print the output +print(out) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 7a45a177..cd5f9f74 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.8.1" +version = "3.8.2" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index fcd67dc6..a8fb119a 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -48,6 +48,8 @@ from swarms.models.vip_llava import VipLlavaMultiModal # noqa: E402 from swarms.models.llava import LavaMultiModal # noqa: E402 from swarms.models.qwen import QwenVLMultiModal # noqa: E402 from swarms.models.clipq import CLIPQ # noqa: E402 +from swarms.models.kosmos_two import Kosmos # noqa: E402 +from swarms.models.fuyu import Fuyu # noqa: E402 # from swarms.models.dalle3 import Dalle3 # from swarms.models.distilled_whisperx import DistilWhisperModel # noqa: E402 @@ -79,7 +81,6 @@ __all__ = [ "Zephyr", "BaseMultiModalModel", "Idefics", - # "Kosmos", "Vilt", "Nougat", "LayoutLMDocumentQA", @@ -102,9 +103,6 @@ __all__ = [ "AudioModality", "VideoModality", "MultimodalData", - # "CogAgent", - # "ModelScopePipeline", - # "ModelScopeAutoModel", "TogetherLLM", "TimmModel", "UltralyticsModel", @@ -112,4 +110,6 @@ __all__ = [ "LavaMultiModal", "QwenVLMultiModal", "CLIPQ", + "Kosmos", + "Fuyu", ] From d34427a4f856b9e757f07e10d78ae23843bc282d Mon Sep 17 00:00:00 2001 From: Kye Date: Sun, 28 Jan 2024 09:53:53 -0500 Subject: [PATCH 065/103] [CODE QUALITY] --- playground/models/kosmos.py | 2 +- pyproject.toml | 2 +- swarms/memory/base_db.py | 2 +- swarms/memory/base_vectordatabase.py | 3 +- swarms/models/__init__.py | 2 +- swarms/models/openai_models.py | 73 +++++++++++++++++++++++++++- swarms/structs/conversation.py | 2 +- swarms/structs/multi_agent_rag.py | 46 +++++++----------- 8 files changed, 94 insertions(+), 38 deletions(-) diff --git a/playground/models/kosmos.py b/playground/models/kosmos.py index 3d0f1dd2..dbfd108f 100644 --- a/playground/models/kosmos.py +++ b/playground/models/kosmos.py @@ -7,4 +7,4 @@ model = Kosmos() out = model.run("Analyze the reciepts in this image", "docs.jpg") # Print the output -print(out) \ No newline at end of file +print(out) diff --git a/pyproject.toml b/pyproject.toml index cd5f9f74..31b72863 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.8.2" +version = "3.8.5" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/memory/base_db.py b/swarms/memory/base_db.py index bb0a2961..0501def7 100644 --- a/swarms/memory/base_db.py +++ b/swarms/memory/base_db.py @@ -156,4 +156,4 @@ class AbstractDatabase(ABC): """ - pass \ No newline at end of file + pass diff --git a/swarms/memory/base_vectordatabase.py b/swarms/memory/base_vectordatabase.py index 734c872a..06f42007 100644 --- a/swarms/memory/base_vectordatabase.py +++ b/swarms/memory/base_vectordatabase.py @@ -91,7 +91,6 @@ class AbstractVectorDatabase(ABC): pass - @abstractmethod def get(self, query: str): """ @@ -139,4 +138,4 @@ class AbstractVectorDatabase(ABC): """ - pass \ No newline at end of file + pass diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index a8fb119a..dfeb9cfe 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -48,7 +48,7 @@ from swarms.models.vip_llava import VipLlavaMultiModal # noqa: E402 from swarms.models.llava import LavaMultiModal # noqa: E402 from swarms.models.qwen import QwenVLMultiModal # noqa: E402 from swarms.models.clipq import CLIPQ # noqa: E402 -from swarms.models.kosmos_two import Kosmos # noqa: E402 +from swarms.models.kosmos_two import Kosmos # noqa: E402 from swarms.models.fuyu import Fuyu # noqa: E402 # from swarms.models.dalle3 import Dalle3 diff --git a/swarms/models/openai_models.py b/swarms/models/openai_models.py index f13657dc..b1aa0117 100644 --- a/swarms/models/openai_models.py +++ b/swarms/models/openai_models.py @@ -1,5 +1,7 @@ from __future__ import annotations +import asyncio +import functools import logging import sys from typing import ( @@ -16,6 +18,7 @@ from typing import ( Optional, Set, Tuple, + Type, Union, ) @@ -23,7 +26,7 @@ from langchain.callbacks.manager import ( AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun, ) -from langchain.llms.base import BaseLLM, create_base_retry_decorator +from langchain.llms.base import BaseLLM from langchain.pydantic_v1 import Field, root_validator from langchain.schema import Generation, LLMResult from langchain.schema.output import GenerationChunk @@ -32,7 +35,17 @@ from langchain.utils import ( get_pydantic_field_names, ) from langchain.utils.utils import build_extra_kwargs +from tenacity import ( + RetryCallState, + before_sleep_log, + retry, + retry_base, + retry_if_exception_type, + stop_after_attempt, + wait_exponential, +) +logger = logging.getLogger(__name__) from importlib.metadata import version @@ -41,6 +54,62 @@ from packaging.version import parse logger = logging.getLogger(__name__) +@functools.lru_cache +def _log_error_once(msg: str) -> None: + """Log an error once.""" + logger.error(msg) + + +def create_base_retry_decorator( + error_types: List[Type[BaseException]], + max_retries: int = 1, + run_manager: Optional[ + Union[AsyncCallbackManagerForLLMRun, CallbackManagerForLLMRun] + ] = None, +) -> Callable[[Any], Any]: + """Create a retry decorator for a given LLM and provided list of error types.""" + + _logging = before_sleep_log(logger, logging.WARNING) + + def _before_sleep(retry_state: RetryCallState) -> None: + _logging(retry_state) + if run_manager: + if isinstance(run_manager, AsyncCallbackManagerForLLMRun): + coro = run_manager.on_retry(retry_state) + try: + loop = asyncio.get_event_loop() + if loop.is_running(): + loop.create_task(coro) + else: + asyncio.run(coro) + except Exception as e: + _log_error_once(f"Error in on_retry: {e}") + else: + run_manager.on_retry(retry_state) + return None + + min_seconds = 4 + max_seconds = 10 + # Wait 2^x * 1 second between each retry starting with + # 4 seconds, then up to 10 seconds, then 10 seconds afterwards + retry_instance: "retry_base" = retry_if_exception_type( + error_types[0] + ) + for error in error_types[1:]: + retry_instance = retry_instance | retry_if_exception_type( + error + ) + return retry( + reraise=True, + stop=stop_after_attempt(max_retries), + wait=wait_exponential( + multiplier=1, min=min_seconds, max=max_seconds + ), + retry=retry_instance, + before_sleep=_before_sleep, + ) + + def is_openai_v1() -> bool: _version = parse(version("openai")) return _version.major >= 1 @@ -833,7 +902,7 @@ class OpenAIChat(BaseLLM): """ client: Any #: :meta private: - model_name: str = "gpt-3.5-turbo-1106" + model_name: str = "gpt-4-1106-preview" model_kwargs: Dict[str, Any] = Field(default_factory=dict) openai_api_key: Optional[str] = None openai_api_base: Optional[str] = None diff --git a/swarms/structs/conversation.py b/swarms/structs/conversation.py index 9a2224a4..a59e4cf9 100644 --- a/swarms/structs/conversation.py +++ b/swarms/structs/conversation.py @@ -75,7 +75,7 @@ class Conversation(BaseStructure): self.autosave = autosave self.save_filepath = save_filepath self.conversation_history = [] - + # If system prompt is not None, add it to the conversation history if self.system_prompt: self.add("system", self.system_prompt) diff --git a/swarms/structs/multi_agent_rag.py b/swarms/structs/multi_agent_rag.py index 7b51332e..91d8c39d 100644 --- a/swarms/structs/multi_agent_rag.py +++ b/swarms/structs/multi_agent_rag.py @@ -9,24 +9,24 @@ from swarms.structs.agent import Agent class MultiAgentRag: """ Represents a multi-agent RAG (Relational Agent Graph) structure. - + Attributes: agents (List[Agent]): List of agents in the multi-agent RAG. db (AbstractVectorDatabase): Database used for querying. verbose (bool): Flag indicating whether to print verbose output. """ + agents: List[Agent] db: AbstractVectorDatabase verbose: bool = False - - + def query_database(self, query: str): """ Queries the database using the given query string. - + Args: query (str): The query string. - + Returns: List: The list of results from the database. """ @@ -35,15 +35,14 @@ class MultiAgentRag: agent_results = agent.long_term_memory_prompt(query) results.extend(agent_results) return results - - + def get_agent_by_id(self, agent_id) -> Optional[Agent]: """ Retrieves an agent from the multi-agent RAG by its ID. - + Args: agent_id: The ID of the agent to retrieve. - + Returns: Agent or None: The agent with the specified ID, or None if not found. """ @@ -51,47 +50,36 @@ class MultiAgentRag: if agent.agent_id == agent_id: return agent return None - + def add_message( - self, - sender: Agent, - message: str, - *args, - **kwargs + self, sender: Agent, message: str, *args, **kwargs ): """ Adds a message to the database. - + Args: sender (Agent): The agent sending the message. message (str): The message to add. *args: Additional positional arguments. **kwargs: Additional keyword arguments. - + Returns: int: The ID of the added message. """ doc = f"{sender.ai_name}: {message}" - + return self.db.add(doc) - - def query( - self, - message: str, - *args, - **kwargs - ): + + def query(self, message: str, *args, **kwargs): """ Queries the database using the given message. - + Args: message (str): The message to query. *args: Additional positional arguments. **kwargs: Additional keyword arguments. - + Returns: List: The list of results from the database. """ return self.db.query(message) - - From 71df100c45173251f68bf8ab5f78b99958367940 Mon Sep 17 00:00:00 2001 From: evelynmitchell Date: Sun, 28 Jan 2024 16:38:02 -0700 Subject: [PATCH 066/103] base_vectordb fix --- swarms/memory/{base_vectordatabase.py => base_vectordb.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename swarms/memory/{base_vectordatabase.py => base_vectordb.py} (100%) diff --git a/swarms/memory/base_vectordatabase.py b/swarms/memory/base_vectordb.py similarity index 100% rename from swarms/memory/base_vectordatabase.py rename to swarms/memory/base_vectordb.py From d62eac4bb9dc866cb5a902db5b120c42ae7d88b3 Mon Sep 17 00:00:00 2001 From: evelynmitchell Date: Sun, 28 Jan 2024 16:52:32 -0700 Subject: [PATCH 067/103] abstract vector db import --- swarms/memory/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swarms/memory/__init__.py b/swarms/memory/__init__.py index 1d52d718..f6ab4750 100644 --- a/swarms/memory/__init__.py +++ b/swarms/memory/__init__.py @@ -1,11 +1,11 @@ -from swarms.memory.base_vectordb import AbstractDatabase +from swarms.memory.base_vectordb import AbstractVectorDatabase from swarms.memory.short_term_memory import ShortTermMemory from swarms.memory.sqlite import SQLiteDB from swarms.memory.weaviate_db import WeaviateDB from swarms.memory.visual_memory import VisualShortTermMemory __all__ = [ - "AbstractDatabase", + "AbstractVectorDatabase", "ShortTermMemory", "SQLiteDB", "WeaviateDB", From ad057a56c63458f69c58a479b8488d8b3d075960 Mon Sep 17 00:00:00 2001 From: evelynmitchell Date: Sun, 28 Jan 2024 17:01:41 -0700 Subject: [PATCH 068/103] abstract vector db --- swarms/structs/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index bd245ba7..a5173482 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -9,7 +9,7 @@ from typing import Any, Callable, Dict, List, Optional, Tuple from termcolor import colored -from swarms.memory.base_vectordb import AbstractDatabase +from swarms.memory.base_vectordb import AbstractVectorDatabase from swarms.prompts.agent_system_prompts import ( AGENT_SYSTEM_PROMPT_3, ) From 8df8c68853eb46bec1e5a60145b4e0cc2a954997 Mon Sep 17 00:00:00 2001 From: evelynmitchell Date: Sun, 28 Jan 2024 17:25:01 -0700 Subject: [PATCH 069/103] AbstractVectorDB, pinecone --- pyproject.toml | 1 + requirements.txt | 3 ++- swarms/memory/pinecone.py | 4 ++-- swarms/memory/sqlite.py | 4 ++-- swarms/memory/weaviate_db.py | 4 ++-- swarms/structs/agent.py | 4 ++-- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 31b72863..31c7be39 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -74,6 +74,7 @@ ultralytics = "*" timm = "*" supervision = "*" scikit-image = "*" +pinecone-client = "*" diff --git a/requirements.txt b/requirements.txt index ab78bb36..1ef64bf7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -62,4 +62,5 @@ peft psutil ultralytics supervision -scikit-image \ No newline at end of file +scikit-image +pinecone-client \ No newline at end of file diff --git a/swarms/memory/pinecone.py b/swarms/memory/pinecone.py index bf073d3e..b1c0edf3 100644 --- a/swarms/memory/pinecone.py +++ b/swarms/memory/pinecone.py @@ -1,12 +1,12 @@ from typing import Optional -from swarms.memory.base_vectordb import AbstractDatabase +from swarms.memory.base_vectordb import AbstractVectorDatabase import pinecone from attr import define, field from swarms.utils.hash import str_to_hash @define -class PineconeDB(AbstractDatabase): +class PineconeDB(AbstractVectorDatabase): """ PineconeDB is a vector storage driver that uses Pinecone as the underlying storage engine. diff --git a/swarms/memory/sqlite.py b/swarms/memory/sqlite.py index 2c4f2740..542be34b 100644 --- a/swarms/memory/sqlite.py +++ b/swarms/memory/sqlite.py @@ -1,5 +1,5 @@ from typing import List, Tuple, Any, Optional -from swarms.memory.base_vectordb import AbstractDatabase +from swarms.memory.base_vectordb import AbstractVectorDatabase try: import sqlite3 @@ -9,7 +9,7 @@ except ImportError: ) -class SQLiteDB(AbstractDatabase): +class SQLiteDB(AbstractVectorDatabase): """ A reusable class for SQLite database operations with methods for adding, deleting, updating, and querying data. diff --git a/swarms/memory/weaviate_db.py b/swarms/memory/weaviate_db.py index fec1199e..05ad5388 100644 --- a/swarms/memory/weaviate_db.py +++ b/swarms/memory/weaviate_db.py @@ -4,7 +4,7 @@ Weaviate API Client from typing import Any, Dict, List, Optional -from swarms.memory.base_vectordb import AbstractDatabase +from swarms.memory.base_vectordb import AbstractVectorDatabase try: import weaviate @@ -12,7 +12,7 @@ except ImportError: print("pip install weaviate-client") -class WeaviateDB(AbstractDatabase): +class WeaviateDB(AbstractVectorDatabase): """ Weaviate API Client diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index a5173482..61f0dd1e 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -83,7 +83,7 @@ class Agent: pdf_path (str): The path to the pdf list_of_pdf (str): The list of pdf tokenizer (Any): The tokenizer - memory (AbstractDatabase): The memory + memory (AbstractVectorDatabase): The memory preset_stopping_token (bool): Enable preset stopping token traceback (Any): The traceback traceback_handlers (Any): The traceback handlers @@ -168,7 +168,7 @@ class Agent: pdf_path: Optional[str] = None, list_of_pdf: Optional[str] = None, tokenizer: Optional[Any] = None, - long_term_memory: Optional[AbstractDatabase] = None, + long_term_memory: Optional[AbstractVectorDatabase] = None, preset_stopping_token: Optional[bool] = False, traceback: Any = None, traceback_handlers: Any = None, From 4f39f4fe0ace20126d410c7e7e8ca0fc116f1bc9 Mon Sep 17 00:00:00 2001 From: Kye Date: Sun, 28 Jan 2024 20:00:47 -0500 Subject: [PATCH 070/103] [CLEANUP] --- pyproject.toml | 2 +- swarms/memory/__init__.py | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 31c7be39..32eaa71c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.8.5" +version = "3.8.7" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/memory/__init__.py b/swarms/memory/__init__.py index f6ab4750..c8330fa8 100644 --- a/swarms/memory/__init__.py +++ b/swarms/memory/__init__.py @@ -1,4 +1,5 @@ from swarms.memory.base_vectordb import AbstractVectorDatabase +from swarms.memory.base_db import AbstractDatabase from swarms.memory.short_term_memory import ShortTermMemory from swarms.memory.sqlite import SQLiteDB from swarms.memory.weaviate_db import WeaviateDB @@ -6,6 +7,7 @@ from swarms.memory.visual_memory import VisualShortTermMemory __all__ = [ "AbstractVectorDatabase", + "AbstractDatabase", "ShortTermMemory", "SQLiteDB", "WeaviateDB", From 466f27a714b331db9c622d58b7950cf4f1a177f0 Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 29 Jan 2024 20:48:06 -0500 Subject: [PATCH 071/103] [BUFG] --- README.md | 1 + pyproject.toml | 4 +- swarms/agents/simple_agent.py | 108 ++++++++++---- swarms/cli/_cli.py | 100 +++++-------- swarms/tokenizers/__init__.py | 12 ++ swarms/{models => tokenizers}/r_tokenizers.py | 0 tests/models/test_timm_model.py | 140 +----------------- tests/models/test_ultralytics.py | 8 +- tests/structs/test_swarmnetwork.py | 2 +- 9 files changed, 143 insertions(+), 232 deletions(-) create mode 100644 swarms/tokenizers/__init__.py rename swarms/{models => tokenizers}/r_tokenizers.py (100%) diff --git a/README.md b/README.md index aa8822e8..52fc6a04 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ A modular framework that enables you to Build, Deploy, and Scale Reliable Autono --- ## Usage +With Swarms, you can create structures, such as Agents, Swarms, and Workflows, that are composed of different types of tasks. Let's build a simple creative agent that will dynamically create a 10,000 word blog on health and wellness. Run example in Collab: Open In Colab diff --git a/pyproject.toml b/pyproject.toml index 32eaa71c..74098c43 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.8.7" +version = "3.8.8" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] @@ -105,4 +105,4 @@ preview = true [tool.poetry.scripts] -swarms = 'swarms.cli._cli:cli' \ No newline at end of file +swarms = 'swarms.cli._cli:main' \ No newline at end of file diff --git a/swarms/agents/simple_agent.py b/swarms/agents/simple_agent.py index 87671c46..757715dd 100644 --- a/swarms/agents/simple_agent.py +++ b/swarms/agents/simple_agent.py @@ -1,40 +1,98 @@ from swarms.structs.conversation import Conversation from swarms.models.base_llm import AbstractLLM +from typing import Any +import importlib +import pkgutil +import swarms.models + + +def get_llm_by_name(name: str): + """ + Searches all the modules exported from the 'swarms.models' path for a class with the given name. + + Args: + name (str): The name of the class to search for. + + Returns: + type: The class with the given name, or None if no such class is found. + """ + for importer, modname, ispkg in pkgutil.iter_modules( + swarms.models.__path__ + ): + module = importlib.import_module(f"swarms.models.{modname}") + if hasattr(module, name): + return getattr(module, name) + return None # Run the language model in a loop for n iterations def SimpleAgent( - llm: AbstractLLM = None, iters: int = 10, *args, **kwargs + llm: AbstractLLM = None, iters: Any = "automatic", *args, **kwargs ): - """Simple agent conversation + """ + A simple agent that interacts with a language model. Args: - llm (_type_): _description_ - iters (int, optional): _description_. Defaults to 10. - - Example: - >>> from swarms.models import GPT2LM - >>> from swarms.agents import SimpleAgent - >>> llm = GPT2LM() - >>> SimpleAgent(llm, iters=10) + llm (AbstractLLM): The language model to use for generating responses. + iters (Any): The number of iterations or "automatic" to run indefinitely. + *args: Additional positional arguments to pass to the language model. + **kwargs: Additional keyword arguments to pass to the language model. + + Raises: + Exception: If the language model is not defined or cannot be found. + + Returns: + None """ + try: + if llm is None: + raise Exception("Language model not defined") + + if isinstance(llm, str): + llm = get_llm_by_name(llm) + if llm is None: + raise Exception(f"Language model {llm} not found") + llm = llm(*args, **kwargs) + except Exception as error: + print(f"[ERROR][SimpleAgent] {error}") + raise error + try: conv = Conversation(*args, **kwargs) - for i in range(iters): - user_input = input("User: ") - conv.add("user", user_input) - if user_input.lower() == "quit": - break - task = ( - conv.return_history_as_string() - ) # Get the conversation history - out = llm(task) - conv.add("assistant", out) - print( - f"Assistant: {out}", - ) - conv.display_conversation() - conv.export_conversation("conversation.txt") + if iters == "automatic": + i = 0 + while True: + user_input = input("\033[91mUser:\033[0m ") + conv.add("user", user_input) + if user_input.lower() == "quit": + break + task = ( + conv.return_history_as_string() + ) # Get the conversation history + out = llm(task, *args, **kwargs) + conv.add("assistant", out) + print( + f"\033[94mAssistant:\033[0m {out}", + ) + conv.display_conversation() + conv.export_conversation("conversation.txt") + i += 1 + else: + for i in range(iters): + user_input = input("\033[91mUser:\033[0m ") + conv.add("user", user_input) + if user_input.lower() == "quit": + break + task = ( + conv.return_history_as_string() + ) # Get the conversation history + out = llm(task, *args, **kwargs) + conv.add("assistant", out) + print( + f"\033[94mAssistant:\033[0m {out}", + ) + conv.display_conversation() + conv.export_conversation("conversation.txt") except Exception as error: print(f"[ERROR][SimpleAgentConversation] {error}") diff --git a/swarms/cli/_cli.py b/swarms/cli/_cli.py index 8dee387f..9b1365ae 100644 --- a/swarms/cli/_cli.py +++ b/swarms/cli/_cli.py @@ -1,75 +1,53 @@ import argparse -import sys +from swarms.agents.simple_agent import SimpleAgent, get_llm_by_name -def cli(): - parser = argparse.ArgumentParser(description="Swarms CLI") - parser.add_argument( - "file_name", help="Python file containing Swarms code to run" - ) - # Help message for the -h flag is automatically generated by argparse - parser.add_argument( - "-v", "--version", action="version", version="%(prog)s 0.1.0" +def main(): + parser = argparse.ArgumentParser( + prog="swarms", + description=( + "Run the SimpleAgent with a specified language model." + ), ) + subparsers = parser.add_subparsers(dest="command") - # Check deployments for a given model - parser.add_argument( - "-c", "--check", help="Check deployments for a given agent" + run_parser = subparsers.add_parser( + "run", help="Run the SimpleAgent." ) - - # Generate an API key for a given agent - parser.add_argument( - "-g", - "--generate", - help="Generate an API key for a given agent", + run_parser.add_argument( + "modelname", + type=str, + help="The name of the language model to use.", ) - - # Signin to swarms with a given API key - parser.add_argument( - "-s", "--signin", help="Signin to swarms with a given API key" + run_parser.add_argument( + "--iters", + type=int, + default="automatic", + help=( + 'Number of iterations or "automatic" for infinite loop.' + ' Defaults to "automatic".' + ), ) - # Signout of swarms - parser.add_argument("-o", "--signout", help="Signout of swarms") - - # List all agents - parser.add_argument("-l", "--list", help="List all agents") - - # List all deployments - parser.add_argument( - "-d", "--deployments", help="List all deployments" + # Add a help command + help_parser = subparsers.add_parser( + "help", help="Show this help message and exit." ) + help_parser.set_defaults(func=lambda args: parser.print_help()) - # Pricing information - parser.add_argument("-p", "--pricing", help="Pricing information") - - # Run a deployment - parser.add_argument("-r", "--run", help="Run a deployment") - - # Stop a deployment - parser.add_argument("-t", "--stop", help="Stop a deployment") - - # Delete a deployment - parser.add_argument("-x", "--delete", help="Delete a deployment") - - # Get a deployment - parser.add_argument("-e", "--get", help="Get a deployment") + args = parser.parse_args() - # Get a deployment's logs - parser.add_argument( - "-z", "--logs", help="Get a deployment's logs" - ) + if hasattr(args, "func"): + args.func(args) + elif args.command == "run": + llm = get_llm_by_name(args.modelname) + if llm is None: + raise ValueError( + "No language model found with name" + f" '{args.modelname}'" + ) + SimpleAgent(llm, iters=args.iters) - # Parse the arguments - args = parser.parse_args() - # Execute the specified file - try: - with open(args.file_name, "r") as file: - exec(file.read(), globals()) - except FileNotFoundError: - print(f"Error: File '{args.file_name}' not found.") - sys.exit(1) - except Exception as e: - print(f"Error executing file '{args.file_name}': {e}") - sys.exit(1) +# if __name__ == "__main__": +# main() diff --git a/swarms/tokenizers/__init__.py b/swarms/tokenizers/__init__.py new file mode 100644 index 00000000..fd60f8cf --- /dev/null +++ b/swarms/tokenizers/__init__.py @@ -0,0 +1,12 @@ +from swarms.tokenizers.r_tokenizers import ( + SentencePieceTokenizer, + HuggingFaceTokenizer, + Tokenizer, +) + + +__all__ = [ + "SentencePieceTokenizer", + "HuggingFaceTokenizer", + "Tokenizer", +] diff --git a/swarms/models/r_tokenizers.py b/swarms/tokenizers/r_tokenizers.py similarity index 100% rename from swarms/models/r_tokenizers.py rename to swarms/tokenizers/r_tokenizers.py diff --git a/tests/models/test_timm_model.py b/tests/models/test_timm_model.py index 97499c6a..0ced344e 100644 --- a/tests/models/test_timm_model.py +++ b/tests/models/test_timm_model.py @@ -1,14 +1,7 @@ from unittest.mock import Mock import torch import pytest -from swarms.models.timm import TimmModel, TimmModelInfo - - -@pytest.fixture -def sample_model_info(): - return TimmModelInfo( - model_name="resnet18", pretrained=True, in_chans=3 - ) +from swarms.models.timm import TimmModel def test_get_supported_models(): @@ -33,45 +26,6 @@ def test_call(sample_model_info): assert isinstance(output_shape, torch.Size) -@pytest.mark.parametrize( - "model_name, pretrained, in_chans", - [ - ("resnet18", True, 3), - ("resnet50", False, 1), - ("efficientnet_b0", True, 3), - ], -) -def test_create_model_parameterized(model_name, pretrained, in_chans): - model_info = TimmModelInfo( - model_name=model_name, - pretrained=pretrained, - in_chans=in_chans, - ) - model_handler = TimmModel() - model = model_handler._create_model(model_info) - assert isinstance(model, torch.nn.Module) - - -@pytest.mark.parametrize( - "model_name, pretrained, in_chans", - [ - ("resnet18", True, 3), - ("resnet50", False, 1), - ("efficientnet_b0", True, 3), - ], -) -def test_call_parameterized(model_name, pretrained, in_chans): - model_info = TimmModelInfo( - model_name=model_name, - pretrained=pretrained, - in_chans=in_chans, - ) - model_handler = TimmModel() - input_tensor = torch.randn(1, in_chans, 224, 224) - output_shape = model_handler.__call__(model_info, input_tensor) - assert isinstance(output_shape, torch.Size) - - def test_get_supported_models_mock(): model_handler = TimmModel() model_handler._get_supported_models = Mock( @@ -88,98 +42,6 @@ def test_create_model_mock(sample_model_info): assert isinstance(model, torch.nn.Module) -def test_call_exception(): - model_handler = TimmModel() - model_info = TimmModelInfo( - model_name="invalid_model", pretrained=True, in_chans=3 - ) - input_tensor = torch.randn(1, 3, 224, 224) - with pytest.raises(Exception): - model_handler.__call__(model_info, input_tensor) - - -def test_coverage(): - pytest.main(["--cov=my_module", "--cov-report=html"]) - - -def test_environment_variable(): - import os - - os.environ["MODEL_NAME"] = "resnet18" - os.environ["PRETRAINED"] = "True" - os.environ["IN_CHANS"] = "3" - - model_handler = TimmModel() - model_info = TimmModelInfo( - model_name=os.environ["MODEL_NAME"], - pretrained=bool(os.environ["PRETRAINED"]), - in_chans=int(os.environ["IN_CHANS"]), - ) - input_tensor = torch.randn(1, model_info.in_chans, 224, 224) - output_shape = model_handler(model_info, input_tensor) - assert isinstance(output_shape, torch.Size) - - -@pytest.mark.slow -def test_marked_slow(): - model_handler = TimmModel() - model_info = TimmModelInfo( - model_name="resnet18", pretrained=True, in_chans=3 - ) - input_tensor = torch.randn(1, 3, 224, 224) - output_shape = model_handler(model_info, input_tensor) - assert isinstance(output_shape, torch.Size) - - -@pytest.mark.parametrize( - "model_name, pretrained, in_chans", - [ - ("resnet18", True, 3), - ("resnet50", False, 1), - ("efficientnet_b0", True, 3), - ], -) -def test_marked_parameterized(model_name, pretrained, in_chans): - model_info = TimmModelInfo( - model_name=model_name, - pretrained=pretrained, - in_chans=in_chans, - ) - model_handler = TimmModel() - model = model_handler._create_model(model_info) - assert isinstance(model, torch.nn.Module) - - -def test_exception_testing(): - model_handler = TimmModel() - model_info = TimmModelInfo( - model_name="invalid_model", pretrained=True, in_chans=3 - ) - input_tensor = torch.randn(1, 3, 224, 224) - with pytest.raises(Exception): - model_handler.__call__(model_info, input_tensor) - - -def test_parameterized_testing(): - model_handler = TimmModel() - model_info = TimmModelInfo( - model_name="resnet18", pretrained=True, in_chans=3 - ) - input_tensor = torch.randn(1, 3, 224, 224) - output_shape = model_handler.__call__(model_info, input_tensor) - assert isinstance(output_shape, torch.Size) - - -def test_use_mocks_and_monkeypatching(): - model_handler = TimmModel() - model_handler._create_model = Mock(return_value=torch.nn.Module()) - model_info = TimmModelInfo( - model_name="resnet18", pretrained=True, in_chans=3 - ) - model = model_handler._create_model(model_info) - assert isinstance(model, torch.nn.Module) - - def test_coverage_report(): # Install pytest-cov # Run tests with coverage report diff --git a/tests/models/test_ultralytics.py b/tests/models/test_ultralytics.py index e90a49db..3e7a7b5c 100644 --- a/tests/models/test_ultralytics.py +++ b/tests/models/test_ultralytics.py @@ -1,11 +1,11 @@ from unittest.mock import patch -from swarms.models.ultralytics_model import Ultralytics +from swarms.models.ultralytics_model import UltralyticsModel def test_ultralytics_init(): with patch("swarms.models.YOLO") as mock_yolo: model_name = "yolov5s" - ultralytics = Ultralytics(model_name) + ultralytics = UltralyticsModel(model_name) mock_yolo.assert_called_once_with(model_name) assert ultralytics.model_name == model_name assert ultralytics.model == mock_yolo.return_value @@ -14,7 +14,7 @@ def test_ultralytics_init(): def test_ultralytics_call(): with patch("swarms.models.YOLO") as mock_yolo: model_name = "yolov5s" - ultralytics = Ultralytics(model_name) + ultralytics = UltralyticsModel(model_name) task = "detect" args = (1, 2, 3) kwargs = {"a": "A", "b": "B"} @@ -28,7 +28,7 @@ def test_ultralytics_call(): def test_ultralytics_list_models(): with patch("swarms.models.YOLO") as mock_yolo: model_name = "yolov5s" - ultralytics = Ultralytics(model_name) + ultralytics = UltralyticsModel(model_name) result = ultralytics.list_models() mock_yolo.list_models.assert_called_once() assert result == mock_yolo.list_models.return_value diff --git a/tests/structs/test_swarmnetwork.py b/tests/structs/test_swarmnetwork.py index 683d3bb8..4844dbcd 100644 --- a/tests/structs/test_swarmnetwork.py +++ b/tests/structs/test_swarmnetwork.py @@ -2,7 +2,7 @@ from unittest.mock import MagicMock, Mock, patch import pytest -from swarm_net import SwarmNet +from swarms.structs.swarm_net import SwarmNet from swarms.structs.agent import Agent from swarms.structs.swarm_net import SwarmNetwork From 7c4d663edd3b78fc157080649045794ddd0506d5 Mon Sep 17 00:00:00 2001 From: Eternal Reclaimer <98760976+kyegomez@users.noreply.github.com> Date: Tue, 30 Jan 2024 06:09:19 -0500 Subject: [PATCH 072/103] Update agent.py --- swarms/structs/agent.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 61f0dd1e..52d44ac4 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -344,7 +344,6 @@ class Agent: def print_dashboard(self, task: str): """Print dashboard""" - model_config = self.get_llm_init_params() print(colored("Initializing Agent Dashboard...", "yellow")) print( @@ -373,7 +372,6 @@ class Agent: Dynamic Temperature: {self.dynamic_temperature_enabled} Autosave: {self.autosave} Saved State: {self.saved_state_path} - Model Configuration: {model_config} ---------------------------------------- """, From ff5406dd5068f2485779094aefba4ce662a66714 Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 30 Jan 2024 14:08:51 -0500 Subject: [PATCH 073/103] [FEATS][swarms.tokenizers][swarms.artifacts][swarms.loaders][swarms.chunkers] --- docs/swarms/tokenizers/anthropictokenizer.md | 123 ++++++++++++ docs/swarms/tokenizers/basetokenizer.md | 62 ++++++ docs/swarms/tokenizers/coheretokenizer.md | 134 +++++++++++++ .../swarms/tokenizers/huggingfacetokenizer.md | 127 +++++++++++++ docs/swarms/tokenizers/openaitokenizer.md | 89 +++++++++ .../tokenizers/sentencepiecetokenizer.md | 163 ++++++++++++++++ docs/swarms/tokenizers/tokenizer.md | 78 ++++++++ example.py | 3 +- mkdocs.yml | 9 + pyproject.toml | 2 +- scripts/{auto_tests_docs => }/auto_docs.py | 35 ++-- scripts/auto_tests_docs/auto_tests.py | 65 +++---- scripts/auto_tests_docs/mkdocs_handler.py | 2 +- swarms/__init__.py | 15 +- swarms/artifacts/__init__.py | 7 + swarms/artifacts/base_artifact.py | 76 ++++++++ swarms/artifacts/text_artifact.py | 57 ++++++ swarms/chunkers/__init__.py | 5 + swarms/chunkers/base_chunker.py | 162 ++++++++++++++++ swarms/chunkers/chunk_seperator.py | 7 + swarms/chunkers/text_chunker.py | 13 ++ {tests => swarms/loaders}/__init__.py | 0 swarms/loaders/pdf_loader.py | 77 ++++++++ swarms/memory/__init__.py | 2 + swarms/memory/action_subtask.py | 16 ++ swarms/models/__init__.py | 6 + swarms/models/base_embedding_model.py | 80 ++++++++ swarms/tokenizers/__init__.py | 13 +- swarms/tokenizers/anthropic_tokenizer.py | 96 ++++++++++ swarms/tokenizers/base_tokenizer.py | 56 ++++++ swarms/tokenizers/cohere_tokenizer.py | 35 ++++ swarms/tokenizers/openai_tokenizers.py | 179 ++++++++++++++++++ swarms/utils/__init__.py | 2 + swarms/utils/exponential_backoff.py | 46 +++++ swarms/utils/hash_utils.py | 0 ...rkflow.py => test_tests_graph_workflow.py} | 0 tests/test___init__.py | 0 tests/tokenizers/test_anthropictokenizer.py | 39 ++++ tests/tokenizers/test_basetokenizer.py | 45 +++++ tests/tokenizers/test_coheretokenizer.py | 35 ++++ tests/tokenizers/test_huggingfacetokenizer.py | 66 +++++++ tests/tokenizers/test_openaitokenizer.py | 47 +++++ .../tokenizers/test_sentencepiecetokenizer.py | 1 + tests/tokenizers/test_tokenizer.py | 81 ++++++++ 44 files changed, 2088 insertions(+), 68 deletions(-) create mode 100644 docs/swarms/tokenizers/anthropictokenizer.md create mode 100644 docs/swarms/tokenizers/basetokenizer.md create mode 100644 docs/swarms/tokenizers/coheretokenizer.md create mode 100644 docs/swarms/tokenizers/huggingfacetokenizer.md create mode 100644 docs/swarms/tokenizers/openaitokenizer.md create mode 100644 docs/swarms/tokenizers/sentencepiecetokenizer.md create mode 100644 docs/swarms/tokenizers/tokenizer.md rename scripts/{auto_tests_docs => }/auto_docs.py (69%) create mode 100644 swarms/artifacts/__init__.py create mode 100644 swarms/artifacts/base_artifact.py create mode 100644 swarms/artifacts/text_artifact.py create mode 100644 swarms/chunkers/__init__.py create mode 100644 swarms/chunkers/base_chunker.py create mode 100644 swarms/chunkers/chunk_seperator.py create mode 100644 swarms/chunkers/text_chunker.py rename {tests => swarms/loaders}/__init__.py (100%) create mode 100644 swarms/loaders/pdf_loader.py create mode 100644 swarms/memory/action_subtask.py create mode 100644 swarms/models/base_embedding_model.py create mode 100644 swarms/tokenizers/anthropic_tokenizer.py create mode 100644 swarms/tokenizers/base_tokenizer.py create mode 100644 swarms/tokenizers/cohere_tokenizer.py create mode 100644 swarms/tokenizers/openai_tokenizers.py create mode 100644 swarms/utils/exponential_backoff.py create mode 100644 swarms/utils/hash_utils.py rename tests/structs/{tests_graph_workflow.py => test_tests_graph_workflow.py} (100%) create mode 100644 tests/test___init__.py create mode 100644 tests/tokenizers/test_anthropictokenizer.py create mode 100644 tests/tokenizers/test_basetokenizer.py create mode 100644 tests/tokenizers/test_coheretokenizer.py create mode 100644 tests/tokenizers/test_huggingfacetokenizer.py create mode 100644 tests/tokenizers/test_openaitokenizer.py create mode 100644 tests/tokenizers/test_sentencepiecetokenizer.py create mode 100644 tests/tokenizers/test_tokenizer.py diff --git a/docs/swarms/tokenizers/anthropictokenizer.md b/docs/swarms/tokenizers/anthropictokenizer.md new file mode 100644 index 00000000..4a72fcce --- /dev/null +++ b/docs/swarms/tokenizers/anthropictokenizer.md @@ -0,0 +1,123 @@ +# AnthropicTokenizer Documentation + +## Introduction + +This documentation intends to provide a complete and in-depth guide for using the `AnthropicTokenizer` class within the `swarms.tokenizers` library. The `AnthropicTokenizer` is designed specifically to interface with Anthropic's AI models, primarily used for text tokenization and metadata handling. + +Understanding how to use this tokenizer effectively is crucial for developers and researchers working with natural language processing, machine learning, and text analysis using Anthropic AI models. + +The purpose of the `AnthropicTokenizer` is to convert raw text into a sequence of tokens that can be fed into Anthropic AI models for various tasks. Tokenization is a fundamental step in text processing pipelines and affects the performance of AI models. + +## Class Definition: AnthropicTokenizer + +`AnthropicTokenizer` extends the functionality of a base tokenizer to provide features specifically needed for Anthropic AI models. The class is designed to manage tokenization processes such as counting tokens and ensuring that the token count is under a specified limit, which is essential for effective and efficient model performance. + +**Class Signature:** + +**Parameters:** + +| Parameter Name | Type | Description | Default Value | +|---------------------------|-----------|-----------------------------------------------------------------|---------------| +| `max_tokens` | `int` | Maximum number of tokens permitted. | `500` | +| `client` | `Anthropic` | Instance of an `Anthropic` client for tokenization services. | `None` | +| `model` | `str` | Identifier for the Anthropic model in use. | `"claude-2.1"`| + +**Methods and their descriptions:** + +| Method Name | Return Type | Description | +|----------------------|-------------|--------------------------------------------------------------| +| `__post_init__` | `None` | Initializes default parameters and client instance. | +| `default_max_tokens` | `int` | Returns the default maximum number of tokens. | +| `count_tokens` | `int` | Counts tokens in the input text. Raises a ValueError if the input is not a string. | + +## Architecture and Mechanics + +Upon instantiation, `AnthropicTokenizer` initializes its `max_tokens` limit and sets up a client to interact with the Anthropic services. The client is responsible for providing tokenization functions critical for processing the text inputs. + +The tokenizer employs a dictionary to map specific model prefixes to their maximum token counts. This allows users to adapt the tokenizer's behavior to different models with varying token limits. The `default_max_tokens()` method dynamically retrieves the token limit based on the provided model name, ensuring compatibility and flexibility. + +`count_tokens()` is a critical function that calculates the number of tokens in a given text. This functionality is essential for respecting the model's token limit and ensuring accurate processing by the Anthropic AI. + +## Usage Examples + +Before delving into detailed examples, make sure you have `swarms.tokenizers` installed and ready. If `anthropic` is an optional dependency, ensure that it's installed as well. + +### 1. Tokenizing with Default Settings + +```python +from swarms.tokenizers import AnthropicTokenizer + +# Initialize the tokenizer with default settings +tokenizer = AnthropicTokenizer() + +# Tokenize a sample text +text = "Hello world! This is an example text to tokenize." +token_count = tokenizer.count_tokens(text) + +print(f"Number of tokens: {token_count}") +``` + +In this example, we use the `AnthropicTokenizer` to count the number of tokens in a simple text. The token count can be crucial for managing inputs to the AI model. + +### 2. Tokenizing with Custom Model + +```python +from swarms.tokenizers import AnthropicTokenizer + +# Define a custom model +custom_model = "claude" + +# Initialize the tokenizer with a custom model and max_tokens +tokenizer = AnthropicTokenizer(model=custom_model, max_tokens=1000) + +# Process a larger text +large_text = "..." # Assume large_text is a string with meaningful content + +token_count = tokenizer.count_tokens(large_text) +if token_count > tokenizer.max_tokens: + print("Text exceeds the maximum token limit.") +else: + print(f"Token count within limit: {token_count}") +``` + +This snippet demonstrates setting up the tokenizer for a custom model and a higher maximum token limit. It is helpful when dealing with texts larger than the default token limit. + +### 3. Handling Error in Token Count Function + +```python +from swarms.tokenizers import AnthropicTokenizer + +# Initialize the tokenizer +tokenizer = AnthropicTokenizer() + +# Attempt to tokenize a non-string input (which will raise an error) +non_string_input = ["This", "is", "a", "list", "not", "a", "string"] + +try: + tokenizer.count_tokens(non_string_input) +except ValueError as e: + print(f"Error: {e}") +``` + +This example illustrates the error management within the `count_tokens` method. It is important to handle exceptions gracefully, particularly when a non-string input is provided. + +## Additional Tips and Considerations + +- Always ensure the input text is a string before calling `count_tokens` to avoid unnecessary errors. +- Be aware of the `max_tokens` limit since larger models might have significantly higher limits than defaults. +- When tokenizing large datasets, batch processing with a loop or parallelization might provide better performance. + +## Resources and References + +Given that `AnthropicTokenizer` interacts with an AI model and optional dependencies, it is beneficial to refer to the official documentation and guides specific to those components: + +- [Anthropic Model Documentation](#) (Link would be replaced with actual URL) +- [swarms.tokenizers Installation Guide](#) +- [Python `dataclasses` Documentation](https://docs.python.org/3/library/dataclasses.html) + +Additionally, literature on best practices for tokenization and natural language processing will contribute to a more effective use of the tokenizer: + +- Smith, B. (Year). "Advanced Tokenization Techniques for NLP Models." Journal of Machine Learning. +- Caruthers, M. (Year). "Text Pre-processing and Tokenization for Deep Learning." + +By following the provided documentation and recommended practices, developers and researchers can harness the power of `AnthropicTokenizer` to its full potential, facilitating optimal use of Anthropic's AI models for varied text processing tasks. diff --git a/docs/swarms/tokenizers/basetokenizer.md b/docs/swarms/tokenizers/basetokenizer.md new file mode 100644 index 00000000..16db07fc --- /dev/null +++ b/docs/swarms/tokenizers/basetokenizer.md @@ -0,0 +1,62 @@ +# Documentation for `swarms.tokenizers.BaseTokenizer` + +## Overview and Introduction + +The `swarms.tokenizers` library is designed to provide flexible and efficient tokenization utilities for natural language processing (NLP) tasks. The `BaseTokenizer` class serves as a foundational abstract class from which specific tokenizer implementations can be derived. This class outlines essential functions and properties all tokenizers should have, ensuring consistency and capturing common behaviors required for processing textual data. + +## Class Definition: `BaseTokenizer` + +### Attributes and Methods + +| Name | Type | Description | +| ---------------------- | ------------------------------- | ------------------------------------------------------------------------- | +| `max_tokens` | `int` | Maximum number of tokens the tokenizer can process. | +| `stop_token` | `str` | Token used to denote the end of processing. | +| `stop_sequences` | `List[str]` (read-only) | List of stop sequences initialized post-instantiation. | +| `count_tokens_left` | Method: `(text) -> int` | Computes the number of tokens that can still be added given the text. | +| `count_tokens` | Abstract Method: `(text) -> int`| Returns the number of tokens in the given text. | + +## Functionality and Usage + +The `BaseTokenizer` class provides the structure for creating tokenizers. It includes methods for counting the tokens in a given text and determining how many more tokens can be added without exceeding the `max_tokens` limit. This class should be subclassed, and the `count_tokens` method must be implemented in subclasses to provide the specific token counting logic. + +### Example: Subclassing `BaseTokenizer` + +```python +from swarms.tokenizers import BaseTokenizer + +class SimpleTokenizer(BaseTokenizer): + + def count_tokens(self, text: Union[str, List[dict]]) -> int: + if isinstance(text, str): + # Split text by spaces as a simple tokenization approach + return len(text.split()) + elif isinstance(text, list): + # Assume list of dictionaries with 'token' key + return sum(len(item['token'].split()) for item in text) + else: + raise TypeError("Unsupported type for text") + +# Usage example +tokenizer = SimpleTokenizer(max_tokens=100) +text = "This is an example sentence to tokenize." +print(tokenizer.count_tokens(text)) # Outputs: 7 (assuming space tokenization) +remaining_tokens = tokenizer.count_tokens_left(text) +print(remaining_tokens) # Outputs: 93 +``` + +### Note: + +Understand that the `stop_sequences` and `stop_token` in this particular implementation are placeholders to illustrate the pattern. The actual logic may differ based on specific tokenizer requirements. + +## Additional Information and Tips + +- Tokenization is a vital step in text processing for NLP. It should be tailored to the requirements of the application. +- Ensure that tokenizer definitions are in sync with the models and datasets being used. + +## References and Resources + +For a deeper understanding of tokenization and its role in NLP, refer to: + +- [Natural Language Processing (NLP) in Python — Tokenization](https://nlp.stanford.edu/IR-book/html/htmledition/tokenization-1.html) +- [Hugging Face Tokenizers](https://huggingface.co/docs/tokenizers/python/latest/) - a popular library for tokenization, particularly in the context of transformer models. diff --git a/docs/swarms/tokenizers/coheretokenizer.md b/docs/swarms/tokenizers/coheretokenizer.md new file mode 100644 index 00000000..5a11b8ce --- /dev/null +++ b/docs/swarms/tokenizers/coheretokenizer.md @@ -0,0 +1,134 @@ +# CohereTokenizer Documentation + +The `CohereTokenizer` class is designed to interface with Cohere language models and provides methods for tokenizing text inputs. This tokenizer plays a crucial role in preparing data for a Cohere model, which operates on tokens rather than raw text. + +--- + +### Class Name: `CohereTokenizer` + +## Overview + +The `CohereTokenizer` class is essential for interacting with Cohere models that require tokenized input. As models often operate on tokens, having an intuitive and efficient tokenizer directly linked to the model simplifies preprocessing tasks. This tokenizer counts the tokens in the given text, helping users to manage and understand the tokens they can work with, given limitations like the model's maximum token count. + +## Architecture and How the Class Works + +The `CohereTokenizer` is built as a data class, ensuring that it is lightweight and focused solely on its data attributes and methods related to tokenization. The class relies on an instance of a Cohere `Client`, which needs to be instantiated with an API key from Cohere before use. + +Upon instantiation, the `CohereTokenizer` holds a reference to a specific Cohere model and interfaces with the `Client` to tokenize text accordingly. It provides a simple utility (`count_tokens`) to count the number of tokens that a string, or a list of strings, would be broken down into by the Cohere API. + +## Purpose and Usage + +The `CohereTokenizer` is specifically made for users who are working with Cohere language models. It's designed to help them in preprocessing steps by converting text into tokens and determining how many tokens their text segments contain. This is crucial for ensuring that inputs do not exceed the model's maximum token count, as exceeding this limit can result in errors or truncated text. + +--- + +# Class Definition + +```python +@dataclass +class CohereTokenizer: + model: str + client: Client + DEFAULT_MODEL: str = "command" + DEFAULT_MAX_TOKENS: int = 2048 + max_tokens: int = DEFAULT_MAX_TOKENS +``` + +## Parameters + +| Parameter | Type | Description | Default Value | +| ------------------ | -------------- | ------------------------------------------------------------- | ------------- | +| `model` | `str` | Specifies the Cohere model to be used for tokenization. | None | +| `client` | `Client` | An instance of the Cohere client, initialized with an API key.| None | +| `DEFAULT_MODEL` | `str` | The default model to use if none is specified. | "command" | +| `DEFAULT_MAX_TOKENS`| `int` | Default maximum number of tokens the model accepts. | 2048 | +| `max_tokens` | `int` | Maximum number of tokens; it can be altered to fit the model. | `DEFAULT_MAX_TOKENS`| + +### Methods + +The `CohereTokenizer` class contains the following method: + +#### `count_tokens` + +```python +def count_tokens(self, text: str | list) -> int: + """ + Count the number of tokens in the given text. + + Args: + text (str | list): The input text to tokenize. + + Returns: + int: The number of tokens in the text. + + Raises: + ValueError: If the input text is not a string. + """ +``` + +--- + +# Functionality and Usage Example + +Below are examples demonstrating how to use `CohereTokenizer`. + +--- + +## Counting Tokens + +### Initialization + +First, the Cohere client must be initialized and passed in to create an instance of `CohereTokenizer`. + +```python +from cohere import Client +from swarms.tokenizers import CohereTokenizer + +# Initialize Cohere client with your API key +cohere_client = Client('your-api-key') + +# Instantiate the tokenizer +tokenizer = CohereTokenizer(model='your-model-name', client=cohere_client) +``` + +### Count Tokens Example 1 + +Counting tokens for a single string. + +```python +text_to_tokenize = "Hello, World!" +token_count = tokenizer.count_tokens(text_to_tokenize) +print(f"Number of tokens: {token_count}") +``` + +### Count Tokens Example 2 + +Trying to pass a list instead of a single string, which would raise an error. + +```python +texts_to_tokenize = ["Hello, World!", "Another piece of text."] +try: + token_count = tokenizer.count_tokens(texts_to_tokenize) +except ValueError as e: + print(f"Error: {e}") +``` + +The above code would print `Error: Text must be a string.` as the `count_tokens` function expects a string, not a list. + +--- + +# Additional Information and Tips + +When working with the `CohereTokenizer`, here are some key points to keep in mind: + +- The token count is important to know because Cohere models have a maximum token limit for input. If your text exceeds this limit, it must be split or truncated before being passed to the model. +- It is always a good practice to catch exceptions when using methods like `count_tokens` to handle unexpected inputs gracefully. +- Remember to replace `'your-api-key'` and `'your-model-name'` with your actual Cohere API key and desired model name. + +# References and Resources + +For more detailed information, refer to the following resources: + +- [Cohere API documentation](https://docs.cohere.ai/) +- [Data Classes in Python](https://docs.python.org/3/library/dataclasses.html) + diff --git a/docs/swarms/tokenizers/huggingfacetokenizer.md b/docs/swarms/tokenizers/huggingfacetokenizer.md new file mode 100644 index 00000000..8cf7fd77 --- /dev/null +++ b/docs/swarms/tokenizers/huggingfacetokenizer.md @@ -0,0 +1,127 @@ +# HuggingFaceTokenizer Documentation + +`HuggingFaceTokenizer` is a comprehensive Python class that leverages the Hugging Face `transformers` library to tokenize text using the SentencePiece tokenization mechanism. This class serves as a convenient wrapper for initializing and using tokenizer models from Hugging Face's transformer models, enabling easy integration of tokenizer functionality in various NLP tasks. + +**Purpose and Architecture:** + +Tokenization is a critical step in processing natural language wherein text is broken down into smaller elements (tokens), which can be further used for text analysis, language modeling, and other computational linguistics tasks. The `HuggingFaceTokenizer` provides methods to encode text (turning strings into lists of token IDs) and decode lists of token IDs back into human-readable text. + +**Table of Contents:** + +- [Overview](#overview) +- [Initialization](#initialization) +- [Properties](#properties) +- [Methods](#methods) +- [Usage Examples](#usage-examples) +- [References and Resources](#references-and-resources) + +## Overview + +The `HuggingFaceTokenizer` class is designed to streamline the process of tokenizing text for natural language processing (NLP). It encapsulates various functionalities, such as encoding text into tokens, decoding tokens into text, and identifying token IDs for special tokens. + +## Initialization + +`HuggingFaceTokenizer` is initialized by providing the directory containing the pretrained tokenizer model files. During its initialization, it configures its internal state for tokenization processes, prepares access to vocabulary, and establishes necessary properties for subsequent tokenization tasks. + +### Constructor Parameters + +| Parameter | Data Type | Description | Default | +|------------|-----------|--------------------------------------------|---------| +| model_dir | `str` | The directory containing the tokenizer model files. | None | + +### Attributes + +| Attribute | Data Type | Description | +|-------------------|---------------------|--------------------------------------------------------| +| vocab_size | `int` | The size of the vocabulary used by the tokenizer. | +| bos_token_id | `int` | The token ID representing the beginning of sequence token. | +| eos_token_id | `int` | The token ID representing the end of sequence token. | +| prefix_space_tokens | `Set[int]` | A set of token IDs without a prefix space. | + +## Methods + +### Vocabulary Related Methods + +#### `vocab_size` +Returns the size of the tokenizer's vocabulary. + +#### `bos_token_id` +Returns the token ID used for the beginning of a sentence. + +#### `eos_token_id` +Returns the token ID used for the end of a sentence. + +#### `prefix_space_tokens` +Returns a set of token IDs that start without prefix spaces. + +### Tokenization Methods + +#### `encode` +Encodes a given text into a sequence of token IDs. + +#### `decode` +Decodes a given sequence of token IDs into human-readable text. + +#### `indexes_containing_token` +Returns a list of token IDs that potentially could be decoded into the given token. + +#### `__call__` +Tokenizes given text when the object is called like a function. + +## Usage Examples + +### 1. Initializing the Tokenizer + +```python +from swarms.tokenizers import HuggingFaceTokenizer + +# Initialize the tokenizer with the path to your tokenizer model. +tokenizer = HuggingFaceTokenizer('/path/to/your/model_dir') +``` + +### 2. Encoding Text + +```python +# Tokenize a single sentence. +sentence = "The quick brown fox jumps over the lazy dog." +token_ids = tokenizer.encode(sentence) +print(token_ids) +``` + +### 3. Decoding Tokens + +```python +# Assuming 'token_ids' contains a list of token IDs +decoded_text = tokenizer.decode(token_ids) +print(decoded_text) +``` + +### 4. Getting Special Token IDs + +```python +# Get the beginning of sequence token ID +bos_id = tokenizer.bos_token_id +print(f"BOS token ID: {bos_id}") + +# Get the end of sequence token ID +eos_id = tokenizer.eos_token_id +print(f"EOS token ID: {eos_id}") +``` + +### 5. Using the Tokenizer + +```python +# Tokenize a prompt directly by calling the object with a string. +text = "Hello, world!" +token_ids = tokenizer(text) +print(token_ids) +``` + +## References and Resources + +For more in-depth information on the Hugging Face `transformers` library and SentencePiece, refer to the following resources: + +- Hugging Face `transformers` library documentation: https://huggingface.co/docs/transformers/index +- SentencePiece repository and documentation: https://github.com/google/sentencepiece + +This documentation provides an introductory overview of the `HuggingFaceTokenizer` class. For a more extensive guide on the various parameters, functionalities, and advanced usage scenarios, users should refer to the detailed library documentation and external resources provided above. diff --git a/docs/swarms/tokenizers/openaitokenizer.md b/docs/swarms/tokenizers/openaitokenizer.md new file mode 100644 index 00000000..9e051c32 --- /dev/null +++ b/docs/swarms/tokenizers/openaitokenizer.md @@ -0,0 +1,89 @@ +# OpenAITokenizer + +The `OpenAITokenizer` class is a versatile and intuitive tokenizer designed for use with OpenAI's various language models, including the powerful GPT series. This class addresses the need to efficiently tokenize text for submission to OpenAI's API endpoints, managing different models and their unique tokenization schemes with ease. + +Utility of `OpenAITokenizer` centers around its key features: +- Support for multiple OpenAI models including GPT-3 and GPT-4. +- Dynamic token counting that considers model-specific details. +- Straightforward API intended for easy integration with larger systems. + +## Architecture and Design + +The class adheres to a simple yet effective design, offering methods for calculating token lengths and embedded properties that manage model-specific characteristics such as maximum tokens and encodings. A data class structure is used for clean initializations and better management of class data. + +The `OpenAITokenizer` uses a property-based approach and a method-based approach to provide users with a variety of functionalities essential for preparing text input for OpenAI models. + +## Attributes + +The class contains several key constants and properties that define defaults and settings for use with different models: + +| Attribute | Type | Description | +|-----------------------------------------------------|-----------------|-------------------------------------------------------------| +| `DEFAULT_OPENAI_GPT_3_COMPLETION_MODEL` | `str` | Default completion model for OpenAI GPT-3. | +| `DEFAULT_OPENAI_GPT_3_CHAT_MODEL` | `str` | Default chat model for OpenAI GPT-3. | +| `DEFAULT_OPENAI_GPT_4_MODEL` | `str` | Default model for OpenAI GPT-4. | +| `DEFAULT_ENCODING` | `str` | Default encoding for text. | +| `DEFAULT_MAX_TOKENS` | `int` | Default maximum number of tokens based on the model. | +| `TOKEN_OFFSET` | `int` | Token offset applicable to some models. | +| `MODEL_PREFIXES_TO_MAX_TOKENS` | `dict` | Mapping of model prefixes to their respective max tokens. | +| `EMBEDDING_MODELS` | `list` | List of embedding models supported. | +| `model` | `str` | Name of the model currently being used. | + +## Methods + +The `OpenAITokenizer` class offers a variety of methods: + +| Method | Arguments | Return Type | Description | +|-----------------------|---------------------------------------------|-------------|------------------------------------------------------------------------------------------------| +| `__post_init__` | None | `None` | Method called after the class has been initialized to set up default values. | +| `encoding` | None | `Encoding` | Getter method that retrieves the encoding based on the specified model. | +| `default_max_tokens` | None | `int` | Calculates the default max tokens based on the current model or defaults if not model-specific.| +| `count_tokens` | `text: str \| list[dict]`, `model: str` | `int` | Counts the number of tokens within a given text or a list of messages. | +| `len` | `text: str \| list[dict]`, `model: str` | `int` | Wrapper for `count_tokens`, providing a more intuitive naming convention. | + +### Usage Examples + +Given the extensive nature of this class, several examples are provided for each method, detailing how to use the `OpenAITokenizer` in different contexts. + +#### Example 1: Initializing the Tokenizer + +```python +from swarms.tokenizers import OpenAITokenizer + +tokenizer = OpenAITokenizer(model='gpt-4') +``` + +This example creates a new instance of `OpenAITokenizer` set to work with the GPT-4 model. + +#### Example 2: Counting Tokens + +```python +text = "Hello, this is an example text to tokenize." + +# Initialize the tokenizer +tokenizer = OpenAITokenizer(model='gpt-4') + +# Count tokens +num_tokens = tokenizer.count_tokens(text) +print(f"Number of tokens: {num_tokens}") +``` + +This code snippet demonstrates how to count the number of tokens in a string of text using the specified model's encoding. + +#### Example 3: Custom Model Token Counting + +```python +messages = [ + {"name": "Alice", "message": "Hello! How are you?"}, + {"name": "Bob", "message": "I'm good! Just working on some code."}, +] + +tokenizer = OpenAITokenizer(model='gpt-3.5-turbo') + +# Count tokens for a list of messages +num_tokens = tokenizer.len(messages, model="gpt-3.5-turbo-0613") +print(f"Total tokens for messages: {num_tokens}") +``` + +In this example, we're invoking the `len` method to count the tokens in a conversation thread. Each message is represented as a dictionary with a `name` and `message` field. + diff --git a/docs/swarms/tokenizers/sentencepiecetokenizer.md b/docs/swarms/tokenizers/sentencepiecetokenizer.md new file mode 100644 index 00000000..390dbb07 --- /dev/null +++ b/docs/swarms/tokenizers/sentencepiecetokenizer.md @@ -0,0 +1,163 @@ +# swarms.tokenizers Documentation + +`swarms.tokenizers` is a PyTorch-like tokenization library designed to facilitate natural language processing (NLP) tasks by converting text inputs into a form that machine learning models can interpret. In this documentation, we will outline how to utilize the `SentencePieceTokenizer` class from the `swarms.tokenizers` library, which offers sentencepiece tokenization, a language-independent subword tokenizer and detokenizer. + +## Purpose and Architecture of `SentencePieceTokenizer` + +The `SentencePieceTokenizer` class uses a pre-trained sentencepiece model to tokenize and detokenize texts. SentencePiece is an unsupervised text tokenizer and detokenizer that allows the generation of a subword vocabulary from raw data. By breaking text down into subword units (like wordpieces or byte-pair-encodings), SentencePiece handles languages without a clear word boundary and can improve the performance of text processing in neural network models. + +In `SentencePieceTokenizer`, the tokenization process is language-agnostic and encompasses a range of tokenization strategies, such as byte pair encoding (BPE), unigram, or a combination of both. The class is designed with ease of use in mind, allowing seamless integration with other components of the NLP pipeline. + +## Class Definition + +```python +class SentencePieceTokenizer: + """ + Tokenizer of sentencepiece. + + Args: + model_file (str): the path of the tokenizer model + """ +``` + +## Initialization Parameters + +Property/Method | Type | Description +----------------|------|------------- +`model_file` | `str` | The path to the pretrained sentencepiece model file. + +## Methods and Usage + +Below, we detail the methods available in `SentencePieceTokenizer`, including their parameters, their functionality, and usage examples. + +### Method: `__init__` + +Instantiates an instance of the `SentencePieceTokenizer` with the specified sentencepiece model. + +#### Parameters + +Parameter | Type | Description +----------|------|------------- +`model_file` | `str` | The path to the pretrained sentencepiece model file. + +#### Example + +```python +from swarms.tokenizers import SentencePieceTokenizer + +tokenizer = SentencePieceTokenizer(model_file='your_model.model') +``` + +### Properties: Vocabulary Information + +These properties provide access to various vocabulary-specific information. + +#### `vocab_size` +#### `bos_token_id` +#### `eos_token_id` + +##### Example + +```python +vocab_size = tokenizer.vocab_size +print(f"Vocabulary size: {vocab_size}") + +bos_id = tokenizer.bos_token_id +eos_id = tokenizer.eos_token_id +print(f"BOS token ID: {bos_id}, EOS token ID: {eos_id}") +``` + +### Method: `indexes_containing_token` + +Finds possible tokenizer indexes that, when decoded, may contain the input token. + +#### Parameters + +Parameter | Type | Description +----------|------|------------- +`token` | `str` | The token for which possible indexes are to be found. + +#### Returns + +- `List[int]`: List of tokenizer indexes that might contain the token. + +#### Example + +```python +indexes = tokenizer.indexes_containing_token("▁the") +print(f"Indexes containing '▁the': {indexes}") +``` + +### Method: `encode` + +Tokenizes a text prompt into a list of token IDs. + +#### Parameters + +Parameter | Type | Description +----------|------|------------- +`s` | `str` | The text prompt to tokenize. +`add_bos` | `bool` | If `True`, it adds the beginning-of-sentence token. (default: `True`) + +#### Returns +- `List[int]`: List of token IDs representing the text prompt. + +#### Example + +```python +encoded_ids = tokenizer.encode("Hello, world!", add_bos=True) +print(f"Encoded token IDs: {encoded_ids}") +``` + +### Method: `decode` + +Detokenizes a list of token IDs into text. + +#### Parameters + +Parameter | Type | Description +----------|------|------------- +`t` | `List[int]` | A list of token IDs to detokenize. +`offset` | `Optional[int]` | For incremental decoding. Defaults to `None`, which means it is not applied. + +#### Returns + +- `str`: Text representation of the decoded token IDs. + +#### Example + +```python +decoded_text = tokenizer.decode([bos_id] + encoded_ids) +print(f"Decoded text: {decoded_text}") +``` + +### Method: `__call__` + +Tokenizes prompts when the class instance is used as a callable. + +#### Parameters + +Parameter | Type | Description +----------|------|------------- +`s` | `Union[str, Sequence[str]]` | Text prompts to tokenize. +`add_bos` | `bool` | If `True`, it adds the beginning-of-sentence token. (default: `False`) +`add_eos` | `bool` | If `True`, it adds the end-of-sentence token. (default: `False`) + +#### Returns + +- `addict.Addict`: Object with `input_ids` containing the list of token IDs. + +#### Example + +```python +input_data = tokenizer("Let's tokenize this sentence.") +print(f"Tokenized input IDs: {input_data.input_ids}") +``` + +## Additional Information and Tips + +The library has efficient internals that cache information for performance benefits. For example, `indexes_containing_token` uses a deque to store the most recent lookups, which saves computation time by avoiding re-traversing the vocabulary. + +## Conclusion + +This documentation provides an in-depth explanation of `swarms.tokenizers` with a focus on the `SentencePieceTokenizer` class. By following the examples and guidance detailed above, users should be able to effectively use the tokenizers for their NLP tasks. Users are also encouraged to refer to further resources and the official SentencePiece documentation for more advanced use cases and configurations. diff --git a/docs/swarms/tokenizers/tokenizer.md b/docs/swarms/tokenizers/tokenizer.md new file mode 100644 index 00000000..f60e5bbd --- /dev/null +++ b/docs/swarms/tokenizers/tokenizer.md @@ -0,0 +1,78 @@ +# `Tokenizer` Class Documentation + +The `Tokenizer` class is a flexible and robust tokenization tool designed to efficiently tokenize prompts into a sequence of token IDs or convert token IDs back into readable text. The class works by initializing with a path to a pretrained tokenization model and supports different tokenization backends based on the availability of configs and pretrained models. + +## Initialization & Configuration + +### Parameters: + +| Parameter | Type | Description | Required | +|------------|------|------------------------------------------|----------| +| model_file | str | Path to the tokenizer model or directory | Yes | + +### Attributes: + +| Attribute | Type | Description | +|------------------|------|------------------------------------| +| vocab_size | int | Size of the tokenizer's vocabulary | +| bos_token_id | int | ID of the beginning-of-sequence token | +| eos_token_id | int | ID of the end-of-sequence token | + +### Methods: + +| Method | Returns | Description | +|--------------------------------|---------|--------------------------------------------------------------| +| encode(s, add_bos=True, **kwargs) | list[int] | Tokenizes a prompt and returns token IDs. | +| decode(t, offset=None) | str | Decodes a list of token IDs to a string. | +| __call__(s) | list[int] | Tokenize prompts when the instance is called directly. | +| indexes_containing_token(token) | list[int] | Returns indexes in the vocabulary that may contain the token. | + +## Usage Examples + +### Tokenizing a Prompt + +```python +from swarms.tokenizers import Tokenizer + +tokenizer = Tokenizer("/path/to/tokenizer.model") + +# Tokenize a single prompt string +prompt = "Hello, world!" +token_ids = tokenizer.encode(prompt) +print(token_ids) +``` + +### Decoding Token IDs + +```python +# Decode token IDs back into text +decoded_text = tokenizer.decode(token_ids) +print(decoded_text) +``` + +### Incremental Decoding + +```python +# Incremental decoding with offset (useful for streaming applications) +partial_tokens = [token_ids[0]] # simulate partially received tokens +decoded_partial = tokenizer.decode(partial_tokens, offset=0) +print(decoded_partial) +``` + +### Properties Access + +```python +# Access vocabulary size and special token IDs +print("Vocabulary Size:", tokenizer.vocab_size) +print("BOS Token ID:", tokenizer.bos_token_id) +print("EOS Token ID:", tokenizer.eos_token_id) +``` + +### Indexes Containing Token + +```python +# Find indexes that may output a specific token during decoding +token = "world" +indexes = tokenizer.indexes_containing_token(token) +print("Token Indexes:", indexes) +``` diff --git a/example.py b/example.py index f8d04047..b5aee4a4 100644 --- a/example.py +++ b/example.py @@ -3,8 +3,7 @@ import os from dotenv import load_dotenv # Import the OpenAIChat model and the Agent struct -from swarms.models import OpenAIChat -from swarms.structs import Agent +from swarms import OpenAIChat, Agent # Load the environment variables load_dotenv() diff --git a/mkdocs.yml b/mkdocs.yml index 033ac5f2..dd928dab 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -116,6 +116,15 @@ nav: - groupchat: "swarms/structs/groupchat.md" - swarmnetwork: "swarms/structs/swarmnetwork.md" - groupchatmanager: "swarms/structs/groupchatmanager.md" + - swarms.tokenizers: + - Language: + - Tokenizer: "swarms/tokenizers/tokenizer.md" + - CohereTokenizer: "swarms/tokenizers/coheretokenizer.md" + - BaseTokenizer: "swarms/tokenizers/basetokenizer.md" + - HuggingfaceTokenizer: "swarms/tokenizers/huggingfacetokenizer.md" + - SentencepieceTokenizer: "swarms/tokenizers/sentencepiecetokenizer.md" + - AnthropicTokenizer: "swarms/tokenizers/anthropictokenizer.md" + - OpenaiTokenizer: "swarms/tokenizers/openaitokenizer.md" - swarms.memory: - Vector Databases: - Weaviate: "swarms/memory/weaviate.md" diff --git a/pyproject.toml b/pyproject.toml index 74098c43..13c2ac4f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.8.8" +version = "3.9.1" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/scripts/auto_tests_docs/auto_docs.py b/scripts/auto_docs.py similarity index 69% rename from scripts/auto_tests_docs/auto_docs.py rename to scripts/auto_docs.py index 020684ff..f469e9ec 100644 --- a/scripts/auto_tests_docs/auto_docs.py +++ b/scripts/auto_docs.py @@ -9,11 +9,18 @@ from scripts.auto_tests_docs.docs import DOCUMENTATION_WRITER_SOP from swarms import OpenAIChat ########## -from swarms.agents.base import AbstractAgent -from swarms.structs.message import Message -from swarms.agents.omni_modal_agent import OmniModalAgent -from swarms.agents.tool_agent import ToolAgent -from swarms.agents.worker_agent import WorkerAgent +from swarms.tokenizers.r_tokenizers import ( + SentencePieceTokenizer, + HuggingFaceTokenizer, + Tokenizer, +) +from swarms.tokenizers.base_tokenizer import BaseTokenizer +from swarms.tokenizers.openai_tokenizers import OpenAITokenizer +from swarms.tokenizers.anthropic_tokenizer import ( + AnthropicTokenizer, +) +from swarms.tokenizers.cohere_tokenizer import CohereTokenizer + #################### load_dotenv() @@ -40,14 +47,14 @@ def process_documentation(cls): # Process with OpenAI model (assuming the model's __call__ method takes this input and returns processed content) processed_content = model( - DOCUMENTATION_WRITER_SOP(input_content, "swarms.agents") + DOCUMENTATION_WRITER_SOP(input_content, "swarms.tokenizers") ) # doc_content = f"# {cls.__name__}\n\n{processed_content}\n" doc_content = f"{processed_content}\n" # Create the directory if it doesn't exist - dir_path = "docs/swarms/agents" + dir_path = "docs/swarms/tokenizers" os.makedirs(dir_path, exist_ok=True) # Write the processed documentation to a Markdown file @@ -60,11 +67,13 @@ def process_documentation(cls): def main(): classes = [ - AbstractAgent, - Message, - OmniModalAgent, - ToolAgent, - WorkerAgent, + SentencePieceTokenizer, + HuggingFaceTokenizer, + Tokenizer, + BaseTokenizer, + OpenAITokenizer, + AnthropicTokenizer, + CohereTokenizer, ] threads = [] for cls in classes: @@ -78,7 +87,7 @@ def main(): for thread in threads: thread.join() - print("Documentation generated in 'swarms.agents' directory.") + print("Documentation generated in 'swarms.tokenizers' directory.") if __name__ == "__main__": diff --git a/scripts/auto_tests_docs/auto_tests.py b/scripts/auto_tests_docs/auto_tests.py index 73a35c4f..87d891d2 100644 --- a/scripts/auto_tests_docs/auto_tests.py +++ b/scripts/auto_tests_docs/auto_tests.py @@ -4,25 +4,21 @@ import re import threading from swarms import OpenAIChat from scripts.auto_tests_docs.docs import TEST_WRITER_SOP_PROMPT -from zeta.nn.modules._activations import ( - AccurateGELUActivation, - ClippedGELUActivation, - FastGELUActivation, - GELUActivation, - LaplaceActivation, - LinearActivation, - MishActivation, - NewGELUActivation, - PytorchGELUTanh, - QuickGELUActivation, - ReLUSquaredActivation, + +######### +from swarms.tokenizers.r_tokenizers import ( + SentencePieceTokenizer, + HuggingFaceTokenizer, + Tokenizer, +) +from swarms.tokenizers.base_tokenizer import BaseTokenizer +from swarms.tokenizers.openai_tokenizers import OpenAITokenizer +from swarms.tokenizers.anthropic_tokenizer import ( + AnthropicTokenizer, ) -from zeta.nn.modules.dense_connect import DenseBlock -from zeta.nn.modules.dual_path_block import DualPathBlock -from zeta.nn.modules.feedback_block import FeedbackBlock -from zeta.nn.modules.highway_layer import HighwayLayer -from zeta.nn.modules.multi_scale_block import MultiScaleBlock -from zeta.nn.modules.recursive_block import RecursiveBlock +from swarms.tokenizers.cohere_tokenizer import CohereTokenizer + +######## from dotenv import load_dotenv load_dotenv() @@ -69,14 +65,16 @@ def create_test(cls): # Process with OpenAI model (assuming the model's __call__ method takes this input and returns processed content) processed_content = model( - TEST_WRITER_SOP_PROMPT(input_content, "zeta", "zeta.nn") + TEST_WRITER_SOP_PROMPT( + input_content, "swarms", "swarms.tokenizers" + ) ) processed_content = extract_code_from_markdown(processed_content) doc_content = f"# {cls.__name__}\n\n{processed_content}\n" # Create the directory if it doesn't exist - dir_path = "tests/nn/modules" + dir_path = "tests/tokenizers" os.makedirs(dir_path, exist_ok=True) # Write the processed documentation to a Python file @@ -87,25 +85,14 @@ def create_test(cls): def main(): classes = [ - DenseBlock, - HighwayLayer, - MultiScaleBlock, - FeedbackBlock, - DualPathBlock, - RecursiveBlock, - PytorchGELUTanh, - NewGELUActivation, - GELUActivation, - FastGELUActivation, - QuickGELUActivation, - ClippedGELUActivation, - AccurateGELUActivation, - MishActivation, - LinearActivation, - LaplaceActivation, - ReLUSquaredActivation, + SentencePieceTokenizer, + HuggingFaceTokenizer, + Tokenizer, + BaseTokenizer, + OpenAITokenizer, + AnthropicTokenizer, + CohereTokenizer, ] - threads = [] for cls in classes: thread = threading.Thread(target=create_test, args=(cls,)) @@ -116,7 +103,7 @@ def main(): for thread in threads: thread.join() - print("Tests generated in 'docs/zeta/nn/modules' directory.") + print("Tests generated in 'tests/tokenizers' directory.") if __name__ == "__main__": diff --git a/scripts/auto_tests_docs/mkdocs_handler.py b/scripts/auto_tests_docs/mkdocs_handler.py index f5c4044f..a61defa8 100644 --- a/scripts/auto_tests_docs/mkdocs_handler.py +++ b/scripts/auto_tests_docs/mkdocs_handler.py @@ -28,4 +28,4 @@ def generate_file_list(directory, output_file): # Use the function to generate the file list -generate_file_list("docs/swarms/agents", "file_list.txt") +generate_file_list("docs/swarms/tokenizers", "file_list.txt") diff --git a/swarms/__init__.py b/swarms/__init__.py index 54a60596..3a28d980 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -9,14 +9,7 @@ from swarms.models import * # noqa: E402, F403 from swarms.telemetry import * # noqa: E402, F403 from swarms.utils import * # noqa: E402, F403 from swarms.prompts import * # noqa: E402, F403 - - -# telemetry = Telemetry('mongodb://localhost:27017/', 'mydatabase') - -# telemetry.log_import('swarms.telemetry.bootup') -# telemetry.log_import('swarms.agents') -# telemetry.log_import('swarms.structs') -# telemetry.log_import('swarms.models') -# telemetry.log_import('swarms.telemetry') -# telemetry.log_import('swarms.utils') -# telemetry.log_import('swarms.prompts') +from swarms.tokenizers import * # noqa: E402, F403 +from swarms.loaders import * # noqa: E402, F403 +from swarms.artifacts import * # noqa: E402, F403 +from swarms.chunkers import * # noqa: E402, F403 diff --git a/swarms/artifacts/__init__.py b/swarms/artifacts/__init__.py new file mode 100644 index 00000000..edf4c343 --- /dev/null +++ b/swarms/artifacts/__init__.py @@ -0,0 +1,7 @@ +from swarms.artifacts.base_artifact import BaseArtifact +from swarms.artifacts.text_artifact import TextArtifact + +__all__ = [ + "BaseArtifact", + "TextArtifact", +] diff --git a/swarms/artifacts/base_artifact.py b/swarms/artifacts/base_artifact.py new file mode 100644 index 00000000..3edf7ad3 --- /dev/null +++ b/swarms/artifacts/base_artifact.py @@ -0,0 +1,76 @@ +from __future__ import annotations +from typing import Any +import json +import uuid +from abc import ABC, abstractmethod +from dataclasses import dataclass + + +@dataclass +class BaseArtifact(ABC): + """ + Base class for artifacts. + """ + + id: str + name: str + value: Any + + def __post_init__(self): + if self.id is None: + self.id = uuid.uuid4().hex + if self.name is None: + self.name = self.id + + @classmethod + def value_to_bytes(cls, value: Any) -> bytes: + """ + Convert the value to bytes. + """ + if isinstance(value, bytes): + return value + else: + return str(value).encode() + + @classmethod + def value_to_dict(cls, value: Any) -> dict: + """ + Convert the value to a dictionary. + """ + if isinstance(value, dict): + dict_value = value + else: + dict_value = json.loads(value) + + return {k: v for k, v in dict_value.items()} + + def to_text(self) -> str: + """ + Convert the value to text. + """ + return str(self.value) + + def __str__(self) -> str: + """ + Return a string representation of the artifact. + """ + return self.to_text() + + def __bool__(self) -> bool: + """ + Return the boolean value of the artifact. + """ + return bool(self.value) + + def __len__(self) -> int: + """ + Return the length of the artifact. + """ + return len(self.value) + + @abstractmethod + def __add__(self, other: BaseArtifact) -> BaseArtifact: + """ + Add two artifacts together. + """ + ... diff --git a/swarms/artifacts/text_artifact.py b/swarms/artifacts/text_artifact.py new file mode 100644 index 00000000..e800ad51 --- /dev/null +++ b/swarms/artifacts/text_artifact.py @@ -0,0 +1,57 @@ +from __future__ import annotations +from typing import Optional +from dataclasses import dataclass, field +from swarms.artifacts.base_artifact import BaseArtifact +from swarms.tokenizers.base_tokenizer import BaseTokenizer + + +@dataclass +class TextArtifact(BaseArtifact): + """ + Represents a text artifact. + + Attributes: + value (str): The text value of the artifact. + encoding (str, optional): The encoding of the text (default is "utf-8"). + encoding_error_handler (str, optional): The error handler for encoding errors (default is "strict"). + _embedding (list[float]): The embedding of the text artifact (default is an empty list). + + Properties: + embedding (Optional[list[float]]): The embedding of the text artifact. + + Methods: + __add__(self, other: BaseArtifact) -> TextArtifact: Concatenates the text value of the artifact with another artifact. + __bool__(self) -> bool: Checks if the text value of the artifact is non-empty. + generate_embedding(self, driver: BaseEmbeddingModel) -> Optional[list[float]]: Generates the embedding of the text artifact using a given embedding model. + token_count(self, tokenizer: BaseTokenizer) -> int: Counts the number of tokens in the text artifact using a given tokenizer. + to_bytes(self) -> bytes: Converts the text value of the artifact to bytes using the specified encoding and error handler. + """ + + value: str + encoding: str = "utf-8" + encoding_error_handler: str = "strict" + _embedding: list[float] = field(default_factory=list) + + @property + def embedding(self) -> Optional[list[float]]: + return None if len(self._embedding) == 0 else self._embedding + + def __add__(self, other: BaseArtifact) -> TextArtifact: + return TextArtifact(self.value + other.value) + + def __bool__(self) -> bool: + return bool(self.value.strip()) + + def generate_embedding(self, model) -> Optional[list[float]]: + self._embedding.clear() + self._embedding.extend(model.embed_string(str(self.value))) + + return self.embedding + + def token_count(self, tokenizer: BaseTokenizer) -> int: + return tokenizer.count_tokens(str(self.value)) + + def to_bytes(self) -> bytes: + return self.value.encode( + encoding=self.encoding, errors=self.encoding_error_handler + ) diff --git a/swarms/chunkers/__init__.py b/swarms/chunkers/__init__.py new file mode 100644 index 00000000..ccc8814e --- /dev/null +++ b/swarms/chunkers/__init__.py @@ -0,0 +1,5 @@ +from swarms.chunkers.chunk_seperator import ChunkSeparator +from swarms.chunkers.base_chunker import BaseChunker +from swarms.chunkers.text_chunker import TextChunker + +__all__ = ["ChunkSeparator", "BaseChunker", "TextChunker"] diff --git a/swarms/chunkers/base_chunker.py b/swarms/chunkers/base_chunker.py new file mode 100644 index 00000000..a63b3ac7 --- /dev/null +++ b/swarms/chunkers/base_chunker.py @@ -0,0 +1,162 @@ +from __future__ import annotations +from abc import ABC +from typing import Optional +from dataclasses import dataclass, field +from swarms.artifacts.text_artifact import TextArtifact +from swarms.chunkers.chunk_seperator import ChunkSeparator +from swarms.tokenizers.base_tokenizer import BaseTokenizer +from swarms.tokenizers.openai_tokenizers import OpenAITokenizer + + +@dataclass +class BaseChunker(ABC): + """ + Base class for chunking text into smaller chunks. + """ + + DEFAULT_SEPARATORS = [ChunkSeparator(" ")] + + separators: list[ChunkSeparator] = field( + default_factory=lambda: BaseChunker.DEFAULT_SEPARATORS + ) + tokenizer: BaseTokenizer = field( + default_factory=lambda: OpenAITokenizer( + model=OpenAITokenizer.DEFAULT_OPENAI_GPT_3_CHAT_MODEL + ) + ) + max_tokens: int = field( + default_factory=lambda: BaseChunker.tokenizer.max_tokens + ) + + def chunk(self, text: str | str) -> list[str]: + """ + Chunk the given text into smaller chunks. + + Args: + text (TextArtifact | str): The text to be chunked. + + Returns: + list[TextArtifact]: The list of chunked text artifacts. + """ + text = text.value if isinstance(text, str) else text + + return [ + TextArtifact(c) for c in self._chunk_recursively(text) + ] + + def _chunk_recursively( + self, + chunk: str, + current_separator: Optional[ChunkSeparator] = None, + ) -> list[str]: + """ + Recursively chunk the given chunk into smaller subchunks. + + Args: + chunk (str): The chunk to be recursively chunked. + current_separator (Optional[ChunkSeparator], optional): The current separator to be used. Defaults to None. + + Returns: + list[str]: The list of recursively chunked subchunks. + """ + token_count = self.tokenizer.count_tokens(chunk) + + if token_count <= self.max_tokens: + return [chunk] + else: + balance_index = -1 + balance_diff = float("inf") + tokens_count = 0 + half_token_count = token_count // 2 + + # If a separator is provided, only use separators after it. + if current_separator: + separators = self.separators[ + self.separators.index(current_separator) : + ] + else: + separators = self.separators + + # Loop through available separators to find the best split. + for separator in separators: + # Split the chunk into subchunks using the current separator. + subchunks = list( + filter(None, chunk.split(separator.value)) + ) + + # Check if the split resulted in more than one subchunk. + if len(subchunks) > 1: + # Iterate through the subchunks and calculate token counts. + for index, subchunk in enumerate(subchunks): + if index < len(subchunks): + if separator.is_prefix: + subchunk = separator.value + subchunk + else: + subchunk = subchunk + separator.value + + tokens_count += self.tokenizer.count_tokens( + subchunk + ) + + # Update the best split if the current one is more balanced. + if ( + abs(tokens_count - half_token_count) + < balance_diff + ): + balance_index = index + balance_diff = abs( + tokens_count - half_token_count + ) + + # Create the two subchunks based on the best separator. + if separator.is_prefix: + # If the separator is a prefix, append it before this subchunk. + first_subchunk = ( + separator.value + + separator.value.join( + subchunks[: balance_index + 1] + ) + ) + second_subchunk = ( + separator.value + + separator.value.join( + subchunks[balance_index + 1 :] + ) + ) + else: + # If the separator is not a prefix, append it after this subchunk. + first_subchunk = ( + separator.value.join( + subchunks[: balance_index + 1] + ) + + separator.value + ) + second_subchunk = separator.value.join( + subchunks[balance_index + 1 :] + ) + + # Continue recursively chunking the subchunks. + first_subchunk_rec = self._chunk_recursively( + first_subchunk.strip(), separator + ) + second_subchunk_rec = self._chunk_recursively( + second_subchunk.strip(), separator + ) + + # Return the concatenated results of the subchunks if both are non-empty. + if first_subchunk_rec and second_subchunk_rec: + return ( + first_subchunk_rec + second_subchunk_rec + ) + # If only one subchunk is non-empty, return it. + elif first_subchunk_rec: + return first_subchunk_rec + elif second_subchunk_rec: + return second_subchunk_rec + else: + return [] + # If none of the separators result in a balanced split, split the chunk in half. + midpoint = len(chunk) // 2 + return self._chunk_recursively( + chunk[:midpoint] + ) + self._chunk_recursively(chunk[midpoint:]) diff --git a/swarms/chunkers/chunk_seperator.py b/swarms/chunkers/chunk_seperator.py new file mode 100644 index 00000000..d554be48 --- /dev/null +++ b/swarms/chunkers/chunk_seperator.py @@ -0,0 +1,7 @@ +from dataclasses import dataclass + + +@dataclass +class ChunkSeparator: + value: str + is_prefix: bool = False diff --git a/swarms/chunkers/text_chunker.py b/swarms/chunkers/text_chunker.py new file mode 100644 index 00000000..b8b17bf1 --- /dev/null +++ b/swarms/chunkers/text_chunker.py @@ -0,0 +1,13 @@ +from swarms.chunkers.base_chunker import BaseChunker +from swarms.chunkers.chunk_seperator import ChunkSeparator + + +class TextChunker(BaseChunker): + DEFAULT_SEPARATORS = [ + ChunkSeparator("\n\n"), + ChunkSeparator("\n"), + ChunkSeparator(". "), + ChunkSeparator("! "), + ChunkSeparator("? "), + ChunkSeparator(" "), + ] diff --git a/tests/__init__.py b/swarms/loaders/__init__.py similarity index 100% rename from tests/__init__.py rename to swarms/loaders/__init__.py diff --git a/swarms/loaders/pdf_loader.py b/swarms/loaders/pdf_loader.py new file mode 100644 index 00000000..f3db1448 --- /dev/null +++ b/swarms/loaders/pdf_loader.py @@ -0,0 +1,77 @@ +from __future__ import annotations + +from dataclasses import dataclass +from pathlib import Path +from typing import IO, Dict, List, Optional + +from PyPDF2 import PdfReader + +from swarms.utils.hash import str_to_hash + + +@dataclass +class TextArtifact: + text: str + + +@dataclass +class PDFLoader: + """ + A class for loading PDF files and extracting text artifacts. + + Args: + tokenizer (str): The tokenizer to use for chunking the text. + max_tokens (int): The maximum number of tokens per chunk. + + Methods: + load(source, password=None, *args, **kwargs): + Load a single PDF file and extract text artifacts. + + load_collection(sources, password=None, *args, **kwargs): + Load a collection of PDF files and extract text artifacts. + + Private Methods: + _load_pdf(stream, password=None): + Load a PDF file and extract text artifacts. + + Attributes: + tokenizer (str): The tokenizer used for chunking the text. + max_tokens (int): The maximum number of tokens per chunk. + """ + + tokenizer: str + max_tokens: int + + def __post_init__(self): + self.chunker = PdfChunker( + tokenizer=self.tokenizer, max_tokens=self.max_tokens + ) + + def load( + self, + source: str | IO | Path, + password: Optional[str] = None, + *args, + **kwargs, + ) -> List[TextArtifact]: + return self._load_pdf(source, password) + + def load_collection( + self, + sources: List[str | IO | Path], + password: Optional[str] = None, + *args, + **kwargs, + ) -> Dict[str, List[TextArtifact]]: + return { + str_to_hash(str(s)): self._load_pdf(s, password) + for s in sources + } + + def _load_pdf( + self, stream: str | IO | Path, password: Optional[str] + ) -> List[TextArtifact]: + reader = PdfReader(stream, strict=True, password=password) + return [ + TextArtifact(text=p.extract_text()) for p in reader.pages + ] diff --git a/swarms/memory/__init__.py b/swarms/memory/__init__.py index c8330fa8..4d58ea5a 100644 --- a/swarms/memory/__init__.py +++ b/swarms/memory/__init__.py @@ -4,6 +4,7 @@ from swarms.memory.short_term_memory import ShortTermMemory from swarms.memory.sqlite import SQLiteDB from swarms.memory.weaviate_db import WeaviateDB from swarms.memory.visual_memory import VisualShortTermMemory +from swarms.memory.action_subtask import ActionSubtaskEntry __all__ = [ "AbstractVectorDatabase", @@ -12,4 +13,5 @@ __all__ = [ "SQLiteDB", "WeaviateDB", "VisualShortTermMemory", + "ActionSubtaskEntry", ] diff --git a/swarms/memory/action_subtask.py b/swarms/memory/action_subtask.py new file mode 100644 index 00000000..09a4f10b --- /dev/null +++ b/swarms/memory/action_subtask.py @@ -0,0 +1,16 @@ +from dataclasses import dataclass + + +@dataclass +class ActionSubtaskEntry: + """Used to store ActionSubtask data to preserve TaskMemory pointers and context in the form of thought and action. + + Attributes: + thought: CoT thought string from the LLM. + action: ReAct action JSON string from the LLM. + answer: tool-generated and memory-processed response from Griptape. + """ + + thought: str + action: str + answer: str diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index dfeb9cfe..48503c54 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -70,6 +70,11 @@ from swarms.models.types import ( MultimodalData, ) # noqa: E402 + +# 3############ Embedding models +from swarms.models.base_embedding_model import BaseEmbeddingModel + + __all__ = [ "AbstractLLM", "Anthropic", @@ -112,4 +117,5 @@ __all__ = [ "CLIPQ", "Kosmos", "Fuyu", + "BaseEmbeddingModel", ] diff --git a/swarms/models/base_embedding_model.py b/swarms/models/base_embedding_model.py new file mode 100644 index 00000000..218e52aa --- /dev/null +++ b/swarms/models/base_embedding_model.py @@ -0,0 +1,80 @@ +from __future__ import annotations + +from abc import ABC, abstractmethod +from dataclasses import dataclass, field +from typing import Optional + +import numpy as np +from swarms.chunkers.base_chunker import BaseChunker +from swarms.chunkers.text_chunker import TextChunker +from swarms.utils.exponential_backoff import ExponentialBackoffMixin + +from swarms.artifacts.text_artifact import TextArtifact +from swarms.tokenizers.base_tokenizer import BaseTokenizer + + +@dataclass +class BaseEmbeddingModel( + ExponentialBackoffMixin, + ABC, + # SerializableMixin +): + """ + Attributes: + model: The name of the model to use. + tokenizer: An instance of `BaseTokenizer` to use when calculating tokens. + """ + + model: str = None + tokenizer: Optional[BaseTokenizer] = None + chunker: BaseChunker = field(init=False) + + def __post_init__(self) -> None: + if self.tokenizer: + self.chunker = TextChunker(tokenizer=self.tokenizer) + + def embed_text_artifact( + self, artifact: TextArtifact + ) -> list[float]: + return self.embed_string(artifact.to_text()) + + def embed_string(self, string: str) -> list[float]: + for attempt in self.retrying(): + with attempt: + if ( + self.tokenizer + and self.tokenizer.count_tokens(string) + > self.tokenizer.max_tokens + ): + return self._embed_long_string(string) + else: + return self.try_embed_chunk(string) + + else: + raise RuntimeError("Failed to embed string.") + + @abstractmethod + def try_embed_chunk(self, chunk: str) -> list[float]: + ... + + def _embed_long_string(self, string: str) -> list[float]: + """Embeds a string that is too long to embed in one go.""" + chunks = self.chunker.chunk(string) + + embedding_chunks = [] + length_chunks = [] + for chunk in chunks: + embedding_chunks.append(self.try_embed_chunk(chunk.value)) + length_chunks.append(len(chunk)) + + # generate weighted averages + embedding_chunks = np.average( + embedding_chunks, axis=0, weights=length_chunks + ) + + # normalize length to 1 + embedding_chunks = embedding_chunks / np.linalg.norm( + embedding_chunks + ) + + return embedding_chunks.tolist() diff --git a/swarms/tokenizers/__init__.py b/swarms/tokenizers/__init__.py index fd60f8cf..02129cb9 100644 --- a/swarms/tokenizers/__init__.py +++ b/swarms/tokenizers/__init__.py @@ -3,10 +3,21 @@ from swarms.tokenizers.r_tokenizers import ( HuggingFaceTokenizer, Tokenizer, ) - +from swarms.tokenizers.base_tokenizer import BaseTokenizer +from swarms.tokenizers.openai_tokenizers import OpenAITokenizer +from swarms.tokenizers.anthropic_tokenizer import ( + import_optional_dependency, + AnthropicTokenizer, +) +from swarms.tokenizers.cohere_tokenizer import CohereTokenizer __all__ = [ "SentencePieceTokenizer", "HuggingFaceTokenizer", "Tokenizer", + "BaseTokenizer", + "OpenAITokenizer", + "import_optional_dependency", + "AnthropicTokenizer", + "CohereTokenizer", ] diff --git a/swarms/tokenizers/anthropic_tokenizer.py b/swarms/tokenizers/anthropic_tokenizer.py new file mode 100644 index 00000000..94bced96 --- /dev/null +++ b/swarms/tokenizers/anthropic_tokenizer.py @@ -0,0 +1,96 @@ +from __future__ import annotations + +from dataclasses import dataclass +from importlib import import_module +from types import ModuleType +from typing import Optional + +from anthropic import Anthropic + +from swarms.tokenizers.base_tokenizer import BaseTokenizer + +INSTALL_MAPPING = { + "huggingface_hub": "huggingface-hub", + "pinecone": "pinecone-client", + "opensearchpy": "opensearch-py", +} + + +def import_optional_dependency(name: str) -> Optional[ModuleType]: + """Import an optional dependency. + + If a dependency is missing, an ImportError with a nice message will be raised. + + Args: + name: The module name. + Returns: + The imported module, when found. + None is returned when the package is not found and `errors` is False. + """ + + package_name = INSTALL_MAPPING.get(name) + install_name = package_name if package_name is not None else name + + msg = ( + f"Missing optional dependency: '{install_name}'. " + f"Use poetry or pip to install '{install_name}'." + ) + try: + module = import_module(name) + except ImportError: + raise ImportError(msg) + + return module + + +@dataclass +class AnthropicTokenizer(BaseTokenizer): + """ + Tokenizer class for Anthropic models.] + """ + + max_tokens: int = 500 + client: Anthropic = None + model: str = "claude-2.1" + + def __post_init__(self): + self.DEFAULT_MODEL: str = "claude-2.1" + self.MODEL_PREFIXES_TO_MAX_TOKENS: dict[str, int] = { + "claude-2.1": 200000, + "claude": 100000, + } + self.model = self.model # or self.DEFAULT_MODEL + self.max_tokens = self.max_tokens or self.default_max_tokens() + self.client = ( + self.client + or import_optional_dependency("anthropic").Anthropic() + ) + + def default_max_tokens(self) -> int: + """ + Returns the default maximum number of tokens based on the model prefix. + """ + tokens = next( + v + for k, v in self.MODEL_PREFIXES_TO_MAX_TOKENS.items() + if self.model.startswith(k) + ) + return tokens + + def count_tokens(self, text: str | list) -> int: + """ + Counts the number of tokens in the given text. + + Args: + text: The input text. + + Returns: + The number of tokens in the text. + + Raises: + ValueError: If the input text is not a string. + """ + if isinstance(text, str): + return self.client.count_tokens(text) + else: + raise ValueError("Text must be a string.") diff --git a/swarms/tokenizers/base_tokenizer.py b/swarms/tokenizers/base_tokenizer.py new file mode 100644 index 00000000..a14bbd09 --- /dev/null +++ b/swarms/tokenizers/base_tokenizer.py @@ -0,0 +1,56 @@ +from __future__ import annotations + +from abc import ABC, abstractmethod +from dataclasses import dataclass, field +from typing import List, Union + + +@dataclass +class BaseTokenizer(ABC): + """ + Base class for tokenizers. + + Attributes: + stop_sequences (List[str]): List of stop sequences. + max_tokens (int): Maximum number of tokens. + stop_token (str): Stop token. + """ + + max_tokens: int + stop_token: str = "<|Response|>" + + def __post_init__(self): + self.stop_sequences: List[str] = field( + default_factory=lambda: ["<|Response|>"], + init=False, + ) + + def count_tokens_left(self, text: Union[str, List[dict]]) -> int: + """ + Counts the number of tokens left based on the given text. + + Args: + text (Union[str, List[dict]]): The text to count tokens from. + + Returns: + int: The number of tokens left. + """ + diff = self.max_tokens - self.count_tokens(text) + + if diff > 0: + return diff + else: + return 0 + + @abstractmethod + def count_tokens(self, text: Union[str, List[dict]]) -> int: + """ + Counts the number of tokens in the given text. + + Args: + text (Union[str, List[dict]]): The text to count tokens from. + + Returns: + int: The number of tokens. + """ + ... diff --git a/swarms/tokenizers/cohere_tokenizer.py b/swarms/tokenizers/cohere_tokenizer.py new file mode 100644 index 00000000..7387c836 --- /dev/null +++ b/swarms/tokenizers/cohere_tokenizer.py @@ -0,0 +1,35 @@ +from __future__ import annotations + +from dataclasses import dataclass +from cohere import Client + + +@dataclass +class CohereTokenizer: + """ + A tokenizer class for Cohere models. + """ + + model: str + client: Client + DEFAULT_MODEL: str = "command" + DEFAULT_MAX_TOKENS: int = 2048 + max_tokens: int = DEFAULT_MAX_TOKENS + + def count_tokens(self, text: str | list) -> int: + """ + Count the number of tokens in the given text. + + Args: + text (str | list): The input text to tokenize. + + Returns: + int: The number of tokens in the text. + + Raises: + ValueError: If the input text is not a string. + """ + if isinstance(text, str): + return len(self.client.tokenize(text=text).tokens) + else: + raise ValueError("Text must be a string.") diff --git a/swarms/tokenizers/openai_tokenizers.py b/swarms/tokenizers/openai_tokenizers.py new file mode 100644 index 00000000..b77a8efd --- /dev/null +++ b/swarms/tokenizers/openai_tokenizers.py @@ -0,0 +1,179 @@ +from __future__ import annotations +import logging +from dataclasses import dataclass, field +import tiktoken +from tiktoken import Encoding +from typing import Optional +from swarms.tokenizers.base_tokenizer import BaseTokenizer + + +@dataclass +class OpenAITokenizer(BaseTokenizer): + """ + A class representing an OpenAI tokenizer. + + Attributes: + - DEFAULT_OPENAI_GPT_3_COMPLETION_MODEL (str): The default OpenAI GPT-3 completion model. + - DEFAULT_OPENAI_GPT_3_CHAT_MODEL (str): The default OpenAI GPT-3 chat model. + - DEFAULT_OPENAI_GPT_4_MODEL (str): The default OpenAI GPT-4 model. + - DEFAULT_ENCODING (str): The default encoding. + - DEFAULT_MAX_TOKENS (int): The default maximum number of tokens. + - TOKEN_OFFSET (int): The token offset. + - MODEL_PREFIXES_TO_MAX_TOKENS (dict): A dictionary mapping model prefixes to maximum tokens. + - EMBEDDING_MODELS (list): A list of embedding models. + - model (str): The model name. + + Methods: + - __post_init__(): Initializes the OpenAITokenizer object. + - encoding(): Returns the encoding for the model. + - default_max_tokens(): Returns the default maximum number of tokens. + - count_tokens(text, model): Counts the number of tokens in the given text. + - len(text, model): Returns the length of the text in tokens. + """ + + model: str = "gpt-2" + + def __post_init__(self): + """ + Initializes the OpenAITokenizer object. + Sets the default maximum number of tokens. + """ + self.max_tokens: int = field( + default_factory=lambda: self.default_max_tokens() + ) + + self.DEFAULT_OPENAI_GPT_3_COMPLETION_MODEL = ( + "text-davinci-003" + ) + self.DEFAULT_OPENAI_GPT_3_CHAT_MODEL = "gpt-3.5-turbo" + self.DEFAULT_OPENAI_GPT_4_MODEL = "gpt-4" + self.DEFAULT_ENCODING = "cl100k_base" + self.EFAULT_MAX_TOKENS = 2049 + self.TOKEN_OFFSET = 8 + + self.MODEL_PREFIXES_TO_MAX_TOKENS = { + "gpt-4-1106": 128000, + "gpt-4-32k": 32768, + "gpt-4": 8192, + "gpt-3.5-turbo-16k": 16384, + "gpt-3.5-turbo": 4096, + "gpt-35-turbo-16k": 16384, + "gpt-35-turbo": 4096, + "text-davinci-003": 4097, + "text-davinci-002": 4097, + "code-davinci-002": 8001, + "text-embedding-ada-002": 8191, + "text-embedding-ada-001": 2046, + } + + self.EMBEDDING_MODELS = [ + "text-embedding-ada-002", + "text-embedding-ada-001", + ] + + @property + def encoding(self) -> Encoding: + """ + Returns the encoding for the model. + If the model is not found, returns the default encoding. + """ + try: + return tiktoken.encoding_for_model(self.model) + except KeyError: + return tiktoken.get_encoding(self.DEFAULT_ENCODING) + + def default_max_tokens(self) -> int: + """ + Returns the default maximum number of tokens based on the model. + """ + tokens = next( + v + for k, v in self.MODEL_PREFIXES_TO_MAX_TOKENS.items() + if self.model.startswith(k) + ) + offset = ( + 0 + if self.model in self.EMBEDDING_MODELS + else self.TOKEN_OFFSET + ) + + return ( + tokens if tokens else self.DEFAULT_MAX_TOKENS + ) - offset + + def count_tokens( + self, text: str | list[dict], model: Optional[str] = None + ) -> int: + """ + Counts the number of tokens in the given text. + If the text is a list of messages, counts the tokens for each message. + If a model is provided, uses that model for encoding. + """ + if isinstance(text, list): + model = model if model else self.model + + try: + encoding = tiktoken.encoding_for_model(model) + except KeyError: + logging.warning( + "model not found. Using cl100k_base encoding." + ) + encoding = tiktoken.get_encoding("cl100k_base") + + if model in { + "gpt-3.5-turbo-0613", + "gpt-3.5-turbo-16k-0613", + "gpt-4-0314", + "gpt-4-32k-0314", + "gpt-4-0613", + "gpt-4-32k-0613", + }: + tokens_per_message = 3 + tokens_per_name = 1 + elif model == "gpt-3.5-turbo-0301": + tokens_per_message = 4 + tokens_per_name = -1 + elif "gpt-3.5-turbo" in model or "gpt-35-turbo" in model: + logging.info( + "gpt-3.5-turbo may update over time. Returning" + " num tokens assuming gpt-3.5-turbo-0613." + ) + return self.count_tokens( + text, model="gpt-3.5-turbo-0613" + ) + elif "gpt-4" in model: + logging.info( + "gpt-4 may update over time. Returning num tokens" + " assuming gpt-4-0613." + ) + return self.count_tokens(text, model="gpt-4-0613") + else: + raise NotImplementedError( + "token_count() is not implemented for model" + f" {model}. See" + " https://github.com/openai/openai-python/blob/main/chatml.md" + " for information on how messages are converted" + " to tokens." + ) + + num_tokens = 0 + + for message in text: + num_tokens += tokens_per_message + for key, value in message.items(): + num_tokens += len(encoding.encode(value)) + if key == "name": + num_tokens += tokens_per_name + + num_tokens += 3 + + return num_tokens + else: + return len(self.encoding.encode(text)) + + def len(self, text: str | list[dict], model: Optional[str]): + """ + Returns the length of the text in tokens. + If a model is provided, uses that model for encoding. + """ + return self.count_tokens(text, model) diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index c8eaabaa..3bbcca16 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -32,6 +32,7 @@ from swarms.utils.remove_json_whitespace import ( remove_whitespace_from_json, remove_whitespace_from_yaml, ) +from swarms.utils.exponential_backoff import ExponentialBackoffMixin __all__ = [ "SubprocessCodeInterpreter", @@ -57,4 +58,5 @@ __all__ = [ "JsonOutputParser", "remove_whitespace_from_json", "remove_whitespace_from_yaml", + "ExponentialBackoffMixin", ] diff --git a/swarms/utils/exponential_backoff.py b/swarms/utils/exponential_backoff.py new file mode 100644 index 00000000..2238064a --- /dev/null +++ b/swarms/utils/exponential_backoff.py @@ -0,0 +1,46 @@ +import logging +from abc import ABC +from dataclasses import dataclass +from tenacity import Retrying, wait_exponential, stop_after_attempt + + +@dataclass +class ExponentialBackoffMixin(ABC): + """ + A mixin class that provides exponential backoff functionality. + """ + + min_retry_delay: float = 2 + """ + The minimum delay between retries in seconds. + """ + + max_retry_delay: float = 10 + """ + The maximum delay between retries in seconds. + """ + + max_attempts: int = 10 + """ + The maximum number of retry attempts. + """ + + def after_hook(s: str) -> None: + return logging.warning(s) + + """ + A callable that is executed after each retry attempt. + """ + + def retrying(self) -> Retrying: + """ + Returns a Retrying object configured with the exponential backoff settings. + """ + return Retrying( + wait=wait_exponential( + min=self.min_retry_delay, max=self.max_retry_delay + ), + stop=stop_after_attempt(self.max_attempts), + reraise=True, + after=self.after_hook, + ) diff --git a/swarms/utils/hash_utils.py b/swarms/utils/hash_utils.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/structs/tests_graph_workflow.py b/tests/structs/test_tests_graph_workflow.py similarity index 100% rename from tests/structs/tests_graph_workflow.py rename to tests/structs/test_tests_graph_workflow.py diff --git a/tests/test___init__.py b/tests/test___init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/tokenizers/test_anthropictokenizer.py b/tests/tokenizers/test_anthropictokenizer.py new file mode 100644 index 00000000..5d49b5eb --- /dev/null +++ b/tests/tokenizers/test_anthropictokenizer.py @@ -0,0 +1,39 @@ +# AnthropicTokenizer + +import pytest +from swarms.tokenizers.anthropic_tokenizer import AnthropicTokenizer + + +def test_post_init(): + tokenizer = AnthropicTokenizer() + assert tokenizer.model == "claude-2.1" + assert tokenizer.max_tokens == 200000 + + +def test_default_max_tokens(): + tokenizer = AnthropicTokenizer(model="claude") + assert tokenizer.default_max_tokens() == 100000 + + +@pytest.mark.parametrize( + "model,tokens", [("claude-2.1", 200000), ("claude", 100000)] +) +def test_default_max_tokens_models(model, tokens): + tokenizer = AnthropicTokenizer(model=model) + assert tokenizer.default_max_tokens() == tokens + + +def test_count_tokens_string(): + # Insert mock instantiation of anthropic client and its count_tokens function + text = "This is a test string." + tokenizer = AnthropicTokenizer() + tokens = tokenizer.count_tokens(text) + assert tokens == 5 + + +def test_count_tokens_list(): + # Insert mock instantiation of anthropic client and its count_tokens function + text = ["This", "is", "a", "test", "string."] + tokenizer = AnthropicTokenizer() + with pytest.raises(ValueError): + tokenizer.count_tokens(text) diff --git a/tests/tokenizers/test_basetokenizer.py b/tests/tokenizers/test_basetokenizer.py new file mode 100644 index 00000000..9bd5d9c6 --- /dev/null +++ b/tests/tokenizers/test_basetokenizer.py @@ -0,0 +1,45 @@ +# BaseTokenizer + +import pytest +from swarms.tokenizers.base_tokenizer import BaseTokenizer + + +# 1. Fixture for BaseTokenizer instance. +@pytest.fixture +def base_tokenizer(): + return BaseTokenizer(max_tokens=100) + + +# 2. Tests for __post_init__. +def test_post_init(base_tokenizer): + assert base_tokenizer.stop_sequences == ["<|Response|>"] + assert base_tokenizer.stop_token == "<|Response|>" + + +# 3. Tests for count_tokens_left with different inputs. +def test_count_tokens_left_with_positive_diff( + base_tokenizer, monkeypatch +): + # Mocking count_tokens to return a specific value + monkeypatch.setattr( + "swarms.tokenizers.BaseTokenizer.count_tokens", + lambda x, y: 50, + ) + assert base_tokenizer.count_tokens_left("some text") == 50 + + +def test_count_tokens_left_with_zero_diff( + base_tokenizer, monkeypatch +): + monkeypatch.setattr( + "swarms.tokenizers.BaseTokenizer.count_tokens", + lambda x, y: 100, + ) + assert base_tokenizer.count_tokens_left("some text") == 0 + + +# 4. Add tests for count_tokens. This method is an abstract one, so testing it +# will be dependent on the actual implementation in the subclass. Here is just +# a general idea how to test it (we assume that test_count_tokens is implemented in some subclass). +def test_count_tokens(subclass_tokenizer_instance): + assert subclass_tokenizer_instance.count_tokens("some text") == 6 diff --git a/tests/tokenizers/test_coheretokenizer.py b/tests/tokenizers/test_coheretokenizer.py new file mode 100644 index 00000000..65633d9a --- /dev/null +++ b/tests/tokenizers/test_coheretokenizer.py @@ -0,0 +1,35 @@ +# CohereTokenizer + +import pytest +from swarms.tokenizers.cohere_tokenizer import CohereTokenizer +from unittest.mock import MagicMock + + +@pytest.fixture +def cohere_tokenizer(): + mock_client = MagicMock() + mock_client.tokenize.return_value.tokens = [ + "token1", + "token2", + "token3", + ] + return CohereTokenizer(model="", client=mock_client) + + +def test_count_tokens_with_string(cohere_tokenizer): + tokens_count = cohere_tokenizer.count_tokens("valid string") + assert tokens_count == 3 + + +def test_count_tokens_with_non_string(cohere_tokenizer): + with pytest.raises(ValueError): + cohere_tokenizer.count_tokens(["invalid", "input"]) + + +def test_count_tokens_with_different_length(cohere_tokenizer): + cohere_tokenizer.client.tokenize.return_value.tokens = [ + "token1", + "token2", + ] + tokens_count = cohere_tokenizer.count_tokens("valid string") + assert tokens_count == 2 diff --git a/tests/tokenizers/test_huggingfacetokenizer.py b/tests/tokenizers/test_huggingfacetokenizer.py new file mode 100644 index 00000000..3a0d29af --- /dev/null +++ b/tests/tokenizers/test_huggingfacetokenizer.py @@ -0,0 +1,66 @@ +# HuggingFaceTokenizer + +import pytest +import os +from unittest.mock import patch +from swarms.tokenizers.r_tokenizers import HuggingFaceTokenizer + + +# Test class setup +@pytest.fixture +def hftokenizer(): + dir_path = os.path.join(os.getcwd(), "modeldir") + tokenizer = HuggingFaceTokenizer(dir_path) + return tokenizer + + +# testing __init__ +@patch("os.path") +@patch("swarms.tokenizers.get_logger") +def test___init__(mock_get_logger, mock_path, hftokenizer): + mock_path.exists.return_value = False + mock_path.join.return_value = "dummy_path" + mock_get_logger.return_value = "dummy_logger" + assert hftokenizer.model_dir == "dummy_path" + assert hftokenizer.logger == "dummy_logger" + assert hftokenizer._maybe_decode_bytes is False + assert hftokenizer._prefix_space_tokens is None + + +# testing vocab_size property +def test_vocab_size(hftokenizer): + assert hftokenizer.vocab_size == 30522 + + +# testing bos_token_id property +def test_bos_token_id(hftokenizer): + assert hftokenizer.bos_token_id == 101 + + +# testing eos_token_id property +def test_eos_token_id(hftokenizer): + assert hftokenizer.eos_token_id == 102 + + +# testing prefix_space_tokens property +def test_prefix_space_tokens(hftokenizer): + assert len(hftokenizer.prefix_space_tokens) > 0 + + +# testing _maybe_add_prefix_space method +def test__maybe_add_prefix_space(hftokenizer): + assert ( + hftokenizer._maybe_add_prefix_space( + [101, 2003, 2010, 2050, 2001, 2339], " is why" + ) + == " is why" + ) + assert ( + hftokenizer._maybe_add_prefix_space( + [2003, 2010, 2050, 2001, 2339], "is why" + ) + == " is why" + ) + + +# continuing tests for other methods... diff --git a/tests/tokenizers/test_openaitokenizer.py b/tests/tokenizers/test_openaitokenizer.py new file mode 100644 index 00000000..229db92d --- /dev/null +++ b/tests/tokenizers/test_openaitokenizer.py @@ -0,0 +1,47 @@ +# OpenAITokenizer + +import pytest +import swarms.tokenizers.openai_tokenizers as tokenizers + + +@pytest.fixture() +def openai_tokenizer(): + return tokenizers.OpenAITokenizer("gpt-3") + + +def test_init(openai_tokenizer): + assert openai_tokenizer.model == "gpt-3" + + +def test_default_max_tokens(openai_tokenizer): + assert openai_tokenizer.default_max_tokens() == 4096 + + +@pytest.mark.parametrize( + "text, expected_output", [("Hello, world!", 3), (["Hello"], 4)] +) +def test_count_tokens_single(openai_tokenizer, text, expected_output): + assert ( + openai_tokenizer.count_tokens(text, "gpt-3") + == expected_output + ) + + +@pytest.mark.parametrize( + "texts, expected_output", + [(["Hello, world!", "This is a test"], 6), (["Hello"], 4)], +) +def test_count_tokens_multiple( + openai_tokenizer, texts, expected_output +): + assert ( + openai_tokenizer.count_tokens(texts, "gpt-3") + == expected_output + ) + + +@pytest.mark.parametrize( + "text, expected_output", [("Hello, world!", 3), (["Hello"], 4)] +) +def test_len(openai_tokenizer, text, expected_output): + assert openai_tokenizer.len(text, "gpt-3") == expected_output diff --git a/tests/tokenizers/test_sentencepiecetokenizer.py b/tests/tokenizers/test_sentencepiecetokenizer.py new file mode 100644 index 00000000..e3a0b917 --- /dev/null +++ b/tests/tokenizers/test_sentencepiecetokenizer.py @@ -0,0 +1 @@ +# SentencePieceTokenizer diff --git a/tests/tokenizers/test_tokenizer.py b/tests/tokenizers/test_tokenizer.py new file mode 100644 index 00000000..ea40a2e0 --- /dev/null +++ b/tests/tokenizers/test_tokenizer.py @@ -0,0 +1,81 @@ +# Tokenizer + +from swarms.tokenizers.r_tokenizers import Tokenizer +from unittest.mock import patch + + +def test_initializer_existing_model_file(): + with patch("os.path.exists", return_value=True): + with patch( + "swarms.tokenizers.SentencePieceTokenizer" + ) as mock_model: + tokenizer = Tokenizer("tokenizers/my_model.model") + mock_model.assert_called_with("tokenizers/my_model.model") + assert tokenizer.model == mock_model.return_value + + +def test_initializer_model_folder(): + with patch("os.path.exists", side_effect=[False, True]): + with patch( + "swarms.tokenizers.HuggingFaceTokenizer" + ) as mock_model: + tokenizer = Tokenizer("my_model_directory") + mock_model.assert_called_with("my_model_directory") + assert tokenizer.model == mock_model.return_value + + +def test_vocab_size(): + with patch( + "swarms.tokenizers.SentencePieceTokenizer" + ) as mock_model: + tokenizer = Tokenizer("tokenizers/my_model.model") + assert ( + tokenizer.vocab_size == mock_model.return_value.vocab_size + ) + + +def test_bos_token_id(): + with patch( + "swarms.tokenizers.SentencePieceTokenizer" + ) as mock_model: + tokenizer = Tokenizer("tokenizers/my_model.model") + assert ( + tokenizer.bos_token_id + == mock_model.return_value.bos_token_id + ) + + +def test_encode(): + with patch( + "swarms.tokenizers.SentencePieceTokenizer" + ) as mock_model: + tokenizer = Tokenizer("tokenizers/my_model.model") + assert ( + tokenizer.encode("hello") + == mock_model.return_value.encode.return_value + ) + + +def test_decode(): + with patch( + "swarms.tokenizers.SentencePieceTokenizer" + ) as mock_model: + tokenizer = Tokenizer("tokenizers/my_model.model") + assert ( + tokenizer.decode([1, 2, 3]) + == mock_model.return_value.decode.return_value + ) + + +def test_call(): + with patch( + "swarms.tokenizers.SentencePieceTokenizer" + ) as mock_model: + tokenizer = Tokenizer("tokenizers/my_model.model") + assert ( + tokenizer("hello") + == mock_model.return_value.__call__.return_value + ) + + +# More tests can be added here From 446842930cedad44647a47c00425acf8746b53ec Mon Sep 17 00:00:00 2001 From: vyomakesh09 Date: Tue, 30 Jan 2024 21:55:19 +0000 Subject: [PATCH 074/103] modified: tests/structs/test_swarmnetwork.py --- tests/structs/test_swarmnetwork.py | 42 ++---------------------------- 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/tests/structs/test_swarmnetwork.py b/tests/structs/test_swarmnetwork.py index 4844dbcd..6c4e29f7 100644 --- a/tests/structs/test_swarmnetwork.py +++ b/tests/structs/test_swarmnetwork.py @@ -1,8 +1,8 @@ -from unittest.mock import MagicMock, Mock, patch +from unittest.mock import Mock, patch import pytest -from swarms.structs.swarm_net import SwarmNet + from swarms.structs.agent import Agent from swarms.structs.swarm_net import SwarmNetwork @@ -52,41 +52,3 @@ def test_swarm_network_remove_agent(swarm_network): assert len(swarm_network.agents) == 4 assert agent_to_remove not in swarm_network.agents - -@pytest.fixture -def swarmnet(): - swarmnet = SwarmNet() - agent_mock = MagicMock() - agent_mock.id = "1" - swarmnet.agents = [agent_mock] - return swarmnet - - -def test_run_agent(swarmnet): - swarmnet.run_agent("1", "task") - swarmnet.agents[0].run.assert_called_once_with("task") - - -def test_run_agent_no_agent(swarmnet): - with pytest.raises(ValueError, match="No agent found with ID"): - swarmnet.run_agent("2", "task") - - -def test_run_many_agents(swarmnet): - swarmnet.run_many_agents("task") - swarmnet.agents[0].run.assert_called_once_with("task") - - -def test_list_agents(swarmnet): - swarmnet.list_agents() - assert swarmnet.agents[0].id == "1" - - -def test_get_agent(swarmnet): - agent = swarmnet.get_agent("1") - assert agent.id == "1" - - -def test_get_agent_no_agent(swarmnet): - with pytest.raises(ValueError, match="No agent found with ID"): - swarmnet.get_agent("2") From 070cccac92e81942204e80796c98d72c0e88cd92 Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 30 Jan 2024 18:27:23 -0500 Subject: [PATCH 075/103] [UNUSED DEPENDENCY] --- pyproject.toml | 6 ++-- requirements.txt | 7 ----- swarms/structs/model_parallizer.py | 44 ++---------------------------- 3 files changed, 5 insertions(+), 52 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 13c2ac4f..e0f8d47a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.9.1" +version = "3.9.3" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] @@ -23,7 +23,7 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.6.1" -torch = "2.1.1" +torch = "2.1.1"p transformers = "4.37.1" openai = "0.28.0" langchain = "0.0.333" @@ -56,10 +56,8 @@ pydantic = "1.10.12" tenacity = "8.2.2" Pillow = "9.4.0" chromadb = "0.4.14" -tabulate = "0.9.0" termcolor = "2.2.0" black = "23.3.0" -open_clip_torch = "2.20.0" soundfile = "0.12.1" torchvision = "0.16.1" rich = "13.5.2" diff --git a/requirements.txt b/requirements.txt index 1ef64bf7..7a9c8df2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,7 +11,6 @@ ggl==1.1.0 Pillow==9.4.0 faiss-cpu==1.7.4 openai==0.28.0 -attrs==22.2.0 datasets==2.14.5 pydantic==1.10.12 bitsandbytes @@ -25,13 +24,11 @@ chromadb==0.4.14 tensorflow optimum tiktoken==0.4.0 -tabulate==0.9.0 colored addict backoff==2.2.1 ratelimit==2.2.1 termcolor==2.2.0 -controlnet-aux diffusers einops==0.7.0 imageio==2.25.1 @@ -40,14 +37,10 @@ imageio-ffmpeg==0.4.9 safetensors==0.3.3 numpy omegaconf==2.3.0 -open_clip_torch==2.20.0 openai==0.28.0 opencv-python==4.7.0.72 -prettytable==3.9.0 safetensors==0.3.3 timm -torchmetrics -webdataset marshmallow==3.19.0 yapf autopep8 diff --git a/swarms/structs/model_parallizer.py b/swarms/structs/model_parallizer.py index 8e9dfcdd..0e898d16 100644 --- a/swarms/structs/model_parallizer.py +++ b/swarms/structs/model_parallizer.py @@ -3,7 +3,6 @@ import logging from concurrent.futures import ThreadPoolExecutor, as_completed from typing import Callable, List -from tabulate import tabulate from termcolor import colored # Configure logging @@ -77,23 +76,6 @@ class ModelParallelizer: f"[ERROR][ModelParallelizer] [ROOT CAUSE] [{error}]" ) - def print_responses(self, task): - """Prints the responses in a tabular format""" - responses = self.run_all(task) - table = [] - for i, response in enumerate(responses): - table.append([f"LLM {i+1}", response]) - print( - colored( - tabulate( - table, - headers=["LLM", "Response"], - tablefmt="pretty", - ), - "cyan", - ) - ) - def run_all(self, task): """Run the task on all LLMs""" responses = [] @@ -101,23 +83,7 @@ class ModelParallelizer: responses.append(llm(task)) return responses - def print_arun_all(self, task): - """Prints the responses in a tabular format""" - responses = self.arun_all(task) - table = [] - for i, response in enumerate(responses): - table.append([f"LLM {i+1}", response]) - print( - colored( - tabulate( - table, - headers=["LLM", "Response"], - tablefmt="pretty", - ), - "cyan", - ) - ) - + # New Features def save_responses_to_file(self, filename): """Save responses to file""" @@ -126,7 +92,7 @@ class ModelParallelizer: [f"LLM {i+1}", response] for i, response in enumerate(self.last_responses) ] - file.write(tabulate(table, headers=["LLM", "Response"])) + file.write(table) @classmethod def load_llms_from_file(cls, filename): @@ -151,11 +117,7 @@ class ModelParallelizer: ] print( colored( - tabulate( - table, - headers=["LLM", "Response"], - tablefmt="pretty", - ), + table, "cyan", ) ) From a9f45f271f8d2984a24a63d66dca07836abb65ba Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 30 Jan 2024 18:56:02 -0500 Subject: [PATCH 076/103] [SPEEDUPS] --- pyproject.toml | 4 ++-- swarms/structs/model_parallizer.py | 1 - swarms/utils/disable_logging.py | 15 ++++++++++++++- tests/structs/test_swarmnetwork.py | 1 - 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e0f8d47a..0f51e7c6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.9.3" +version = "3.9.4" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] @@ -23,7 +23,7 @@ classifiers = [ [tool.poetry.dependencies] python = "^3.6.1" -torch = "2.1.1"p +torch = "2.1.1" transformers = "4.37.1" openai = "0.28.0" langchain = "0.0.333" diff --git a/swarms/structs/model_parallizer.py b/swarms/structs/model_parallizer.py index 0e898d16..828d4ef4 100644 --- a/swarms/structs/model_parallizer.py +++ b/swarms/structs/model_parallizer.py @@ -83,7 +83,6 @@ class ModelParallelizer: responses.append(llm(task)) return responses - # New Features def save_responses_to_file(self, filename): """Save responses to file""" diff --git a/swarms/utils/disable_logging.py b/swarms/utils/disable_logging.py index 3b6884d2..e7d2e5e6 100644 --- a/swarms/utils/disable_logging.py +++ b/swarms/utils/disable_logging.py @@ -33,4 +33,17 @@ def disable_logging(): logger = logging.getLogger(logger_name) logger.setLevel( logging.WARNING - ) # Supress DEBUG and info logs + ) # Suppress DEBUG and info logs + + # Create a file handler to log errors to the file + file_handler = logging.FileHandler("errors.txt") + file_handler.setLevel(logging.ERROR) + logging.getLogger().addHandler(file_handler) + + # Create a stream handler to log errors to the terminal + stream_handler = logging.StreamHandler() + stream_handler.setLevel(logging.ERROR) + logging.getLogger().addHandler(stream_handler) + + +disable_logging() diff --git a/tests/structs/test_swarmnetwork.py b/tests/structs/test_swarmnetwork.py index 6c4e29f7..b4e3593b 100644 --- a/tests/structs/test_swarmnetwork.py +++ b/tests/structs/test_swarmnetwork.py @@ -51,4 +51,3 @@ def test_swarm_network_remove_agent(swarm_network): swarm_network.remove_agent(agent_to_remove) assert len(swarm_network.agents) == 4 assert agent_to_remove not in swarm_network.agents - From 8a227678ed9b1916ae1f2ead6851e1905f3ad4bd Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 30 Jan 2024 19:30:04 -0500 Subject: [PATCH 077/103] [REQUIREMENTS] --- pyproject.toml | 1 - requirements.txt | 3 --- 2 files changed, 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0f51e7c6..0698ef64 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,6 @@ diffusers = "*" PyPDF2 = "3.0.1" accelerate = "*" sentencepiece = "0.1.98" -wget = "3.2" httpx = "0.24.1" tiktoken = "0.4.0" safetensors = "0.3.3" diff --git a/requirements.txt b/requirements.txt index 7a9c8df2..6ad5f4b4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,7 +4,6 @@ pandas==1.5.3 langchain==0.0.333 langchain-experimental==0.0.10 playwright==1.34.0 -wget==3.2 httpx==0.24.1 open_clip_torch==2.20.0 ggl==1.1.0 @@ -31,9 +30,7 @@ ratelimit==2.2.1 termcolor==2.2.0 diffusers einops==0.7.0 -imageio==2.25.1 opencv-python-headless==4.8.1.78 -imageio-ffmpeg==0.4.9 safetensors==0.3.3 numpy omegaconf==2.3.0 From 751246e5474cfabe458e9e81b238de1480fd5727 Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 30 Jan 2024 19:33:27 -0500 Subject: [PATCH 078/103] [REQUIREMENTS] --- pyproject.toml | 1 - requirements.txt | 1 - 2 files changed, 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 0698ef64..85e9862a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -36,7 +36,6 @@ weaviate-client = "3.25.3" opencv-python-headless = "4.8.1.78" faiss-cpu = "1.7.4" backoff = "2.2.1" -marshmallow = "3.19.0" datasets = "*" optimum = "1.15.0" diffusers = "*" diff --git a/requirements.txt b/requirements.txt index 6ad5f4b4..df1545e4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -38,7 +38,6 @@ openai==0.28.0 opencv-python==4.7.0.72 safetensors==0.3.3 timm -marshmallow==3.19.0 yapf autopep8 cohere==4.24 From c21af37f642d2a34f523abc2db9995e557b1cd8d Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 30 Jan 2024 19:38:46 -0500 Subject: [PATCH 079/103] [REQUIREMENTS] --- pyproject.toml | 1 - requirements.txt | 4 +--- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 85e9862a..74bf6674 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,7 +46,6 @@ httpx = "0.24.1" tiktoken = "0.4.0" safetensors = "0.3.3" attrs = "22.2.0" -ggl = "1.1.0" ratelimit = "2.2.1" cohere = "4.24" huggingface-hub = "*" diff --git a/requirements.txt b/requirements.txt index df1545e4..3046a978 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -torch==2.1.1 +gitorch==2.1.1 transformers pandas==1.5.3 langchain==0.0.333 @@ -6,7 +6,6 @@ langchain-experimental==0.0.10 playwright==1.34.0 httpx==0.24.1 open_clip_torch==2.20.0 -ggl==1.1.0 Pillow==9.4.0 faiss-cpu==1.7.4 openai==0.28.0 @@ -33,7 +32,6 @@ einops==0.7.0 opencv-python-headless==4.8.1.78 safetensors==0.3.3 numpy -omegaconf==2.3.0 openai==0.28.0 opencv-python==4.7.0.72 safetensors==0.3.3 From e8681b223c50ee83a6d141bdd869d1a6457a6493 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 17:59:19 -0500 Subject: [PATCH 080/103] [CLEANUP] --- example.py | 4 +- playground/demos/visuo/text_to_sql_agent.py | 33 +++ playground/models/roboflow_example.py | 13 + playground/{swarms => structs}/chat.py | 0 playground/{swarms => structs}/debate.py | 0 .../{swarms => structs}/dialogue_simulator.py | 0 .../{swarms => structs}/easy_example.py | 0 playground/{swarms => structs}/godmode.py | 0 playground/{swarms => structs}/groupchat.py | 0 playground/{swarms => structs}/gui_app.py | 0 .../{swarms => structs}/multi_agent_collab.py | 0 .../{swarms => structs}/multi_agent_debate.py | 0 playground/{swarms => structs}/orchestrate.py | 0 .../{swarms => structs}/orchestrator.py | 0 playground/{swarms => structs}/social_app.py | 0 .../{swarms => structs}/swarms_example.py | 0 playground/{swarms => structs}/todo_app.py | 0 playground/swarms/autoscaler.py | 7 - ...orkflow.py => workflow_example_example.py} | 0 pyproject.toml | 3 +- requirements.txt | 5 +- scripts/playground_to_examples.sh | 13 + swarms/models/__init__.py | 4 + swarms/models/gpt4_sam.py | 81 ++++++ swarms/models/odin.py | 2 +- swarms/models/roboflow_model.py | 64 +++++ swarms/models/sam_supervision.py | 116 ++++++++ swarms/models/ultralytics_model.py | 27 +- swarms/structs/agent.py | 49 ++-- swarms/structs/conversation.py | 46 ++++ swarms/utils/__init__.py | 18 ++ swarms/utils/download_img.py | 31 +++ swarms/utils/logger.py | 34 +++ swarms/utils/supervision_masking.py | 259 ++++++++++++++++++ swarms/utils/supervision_visualizer.py | 85 ++++++ 35 files changed, 858 insertions(+), 36 deletions(-) create mode 100644 playground/demos/visuo/text_to_sql_agent.py create mode 100644 playground/models/roboflow_example.py rename playground/{swarms => structs}/chat.py (100%) rename playground/{swarms => structs}/debate.py (100%) rename playground/{swarms => structs}/dialogue_simulator.py (100%) rename playground/{swarms => structs}/easy_example.py (100%) rename playground/{swarms => structs}/godmode.py (100%) rename playground/{swarms => structs}/groupchat.py (100%) rename playground/{swarms => structs}/gui_app.py (100%) rename playground/{swarms => structs}/multi_agent_collab.py (100%) rename playground/{swarms => structs}/multi_agent_debate.py (100%) rename playground/{swarms => structs}/orchestrate.py (100%) rename playground/{swarms => structs}/orchestrator.py (100%) rename playground/{swarms => structs}/social_app.py (100%) rename playground/{swarms => structs}/swarms_example.py (100%) rename playground/{swarms => structs}/todo_app.py (100%) delete mode 100644 playground/swarms/autoscaler.py rename playground/{workflow.py => workflow_example_example.py} (100%) create mode 100755 scripts/playground_to_examples.sh create mode 100644 swarms/models/gpt4_sam.py create mode 100644 swarms/models/roboflow_model.py create mode 100644 swarms/models/sam_supervision.py create mode 100644 swarms/utils/download_img.py create mode 100644 swarms/utils/supervision_masking.py create mode 100644 swarms/utils/supervision_visualizer.py diff --git a/example.py b/example.py index b5aee4a4..33c3bef1 100644 --- a/example.py +++ b/example.py @@ -3,7 +3,7 @@ import os from dotenv import load_dotenv # Import the OpenAIChat model and the Agent struct -from swarms import OpenAIChat, Agent +from swarms import Agent, OpenAIChat # Load the environment variables load_dotenv() @@ -22,7 +22,7 @@ llm = OpenAIChat( ## Initialize the workflow agent = Agent( llm=llm, - max_loops=1, + max_loops=4, autosave=True, dashboard=True, ) diff --git a/playground/demos/visuo/text_to_sql_agent.py b/playground/demos/visuo/text_to_sql_agent.py new file mode 100644 index 00000000..67f53e97 --- /dev/null +++ b/playground/demos/visuo/text_to_sql_agent.py @@ -0,0 +1,33 @@ +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="codellama/CodeLlama-70b-hf", + max_length=4000, + quantize=True, + temperature=0.5, +) + +## Initialize the workflow +agent = Agent( + llm=llm, + max_loops="auto", + system_prompt=None, + autosave=True, + dashboard=True, + tools=[None], +) + +# Run the workflow on a task +agent.run("Generate a 10,000 word blog on health and wellness.") diff --git a/playground/models/roboflow_example.py b/playground/models/roboflow_example.py new file mode 100644 index 00000000..e7cae29e --- /dev/null +++ b/playground/models/roboflow_example.py @@ -0,0 +1,13 @@ +from swarms import RoboflowMultiModal + + +# Initialize the model +model = RoboflowMultiModal( + api_key="api", + project_id="your project id", + hosted=False, +) + + +# Run the model on an img +out = model("img.png") diff --git a/playground/swarms/chat.py b/playground/structs/chat.py similarity index 100% rename from playground/swarms/chat.py rename to playground/structs/chat.py diff --git a/playground/swarms/debate.py b/playground/structs/debate.py similarity index 100% rename from playground/swarms/debate.py rename to playground/structs/debate.py diff --git a/playground/swarms/dialogue_simulator.py b/playground/structs/dialogue_simulator.py similarity index 100% rename from playground/swarms/dialogue_simulator.py rename to playground/structs/dialogue_simulator.py diff --git a/playground/swarms/easy_example.py b/playground/structs/easy_example.py similarity index 100% rename from playground/swarms/easy_example.py rename to playground/structs/easy_example.py diff --git a/playground/swarms/godmode.py b/playground/structs/godmode.py similarity index 100% rename from playground/swarms/godmode.py rename to playground/structs/godmode.py diff --git a/playground/swarms/groupchat.py b/playground/structs/groupchat.py similarity index 100% rename from playground/swarms/groupchat.py rename to playground/structs/groupchat.py diff --git a/playground/swarms/gui_app.py b/playground/structs/gui_app.py similarity index 100% rename from playground/swarms/gui_app.py rename to playground/structs/gui_app.py diff --git a/playground/swarms/multi_agent_collab.py b/playground/structs/multi_agent_collab.py similarity index 100% rename from playground/swarms/multi_agent_collab.py rename to playground/structs/multi_agent_collab.py diff --git a/playground/swarms/multi_agent_debate.py b/playground/structs/multi_agent_debate.py similarity index 100% rename from playground/swarms/multi_agent_debate.py rename to playground/structs/multi_agent_debate.py diff --git a/playground/swarms/orchestrate.py b/playground/structs/orchestrate.py similarity index 100% rename from playground/swarms/orchestrate.py rename to playground/structs/orchestrate.py diff --git a/playground/swarms/orchestrator.py b/playground/structs/orchestrator.py similarity index 100% rename from playground/swarms/orchestrator.py rename to playground/structs/orchestrator.py diff --git a/playground/swarms/social_app.py b/playground/structs/social_app.py similarity index 100% rename from playground/swarms/social_app.py rename to playground/structs/social_app.py diff --git a/playground/swarms/swarms_example.py b/playground/structs/swarms_example.py similarity index 100% rename from playground/swarms/swarms_example.py rename to playground/structs/swarms_example.py diff --git a/playground/swarms/todo_app.py b/playground/structs/todo_app.py similarity index 100% rename from playground/swarms/todo_app.py rename to playground/structs/todo_app.py diff --git a/playground/swarms/autoscaler.py b/playground/swarms/autoscaler.py deleted file mode 100644 index 82bcadb6..00000000 --- a/playground/swarms/autoscaler.py +++ /dev/null @@ -1,7 +0,0 @@ -from swarms import AutoScaler - -auto_scaler = AutoScaler() -auto_scaler.start() - -for i in range(100): - auto_scaler.add_task(f"Task {i}") diff --git a/playground/workflow.py b/playground/workflow_example_example.py similarity index 100% rename from playground/workflow.py rename to playground/workflow_example_example.py diff --git a/pyproject.toml b/pyproject.toml index 74bf6674..fe88c5d6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.9.4" +version = "3.9.5" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] @@ -70,6 +70,7 @@ timm = "*" supervision = "*" scikit-image = "*" pinecone-client = "*" +roboflow = "*" diff --git a/requirements.txt b/requirements.txt index 3046a978..32184962 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -gitorch==2.1.1 +torch==2.1.1 transformers pandas==1.5.3 langchain==0.0.333 @@ -50,4 +50,5 @@ psutil ultralytics supervision scikit-image -pinecone-client \ No newline at end of file +pinecone-client +roboflow \ No newline at end of file diff --git a/scripts/playground_to_examples.sh b/scripts/playground_to_examples.sh new file mode 100755 index 00000000..c1ed0f61 --- /dev/null +++ b/scripts/playground_to_examples.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Change to the root directory +cd / + +# Iterate over all the .py files in the directory +for file in *.py; do + # Get the base name of the file without the .py + base_name=$(basename "$file" .py) + + # Rename the file to remove .py from the end + mv "$file" "${base_name}" +done \ No newline at end of file diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 48503c54..70d4f947 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -50,6 +50,8 @@ from swarms.models.qwen import QwenVLMultiModal # noqa: E402 from swarms.models.clipq import CLIPQ # noqa: E402 from swarms.models.kosmos_two import Kosmos # noqa: E402 from swarms.models.fuyu import Fuyu # noqa: E402 +from swarms.models.roboflow_model import RoboflowMultiModal +from swarms.models.sam_supervision import SegmentAnythingMarkGenerator # from swarms.models.dalle3 import Dalle3 # from swarms.models.distilled_whisperx import DistilWhisperModel # noqa: E402 @@ -118,4 +120,6 @@ __all__ = [ "Kosmos", "Fuyu", "BaseEmbeddingModel", + "RoboflowMultiModal", + "SegmentAnythingMarkGenerator", ] diff --git a/swarms/models/gpt4_sam.py b/swarms/models/gpt4_sam.py new file mode 100644 index 00000000..aef0181f --- /dev/null +++ b/swarms/models/gpt4_sam.py @@ -0,0 +1,81 @@ +import cv2 + +from swarms.models.base_multimodal_model import BaseMultiModalModel +from swarms.models.sam_supervision import SegmentAnythingMarkGenerator +from swarms.utils.supervision_masking import refine_marks +from swarms.utils.supervision_visualizer import MarkVisualizer +from typing import Any + + +class GPT4VSAM(BaseMultiModalModel): + """ + GPT4VSAM class represents a multi-modal model that combines the capabilities of GPT-4 and SegmentAnythingMarkGenerator. + It takes an instance of BaseMultiModalModel (vlm) and a device as input and provides methods for loading images and making predictions. + + Args: + vlm (BaseMultiModalModel): An instance of BaseMultiModalModel representing the visual language model. + device (str, optional): The device to be used for computation. Defaults to "cuda". + + Attributes: + vlm (BaseMultiModalModel): An instance of BaseMultiModalModel representing the visual language model. + device (str): The device to be used for computation. + sam (SegmentAnythingMarkGenerator): An instance of SegmentAnythingMarkGenerator for generating marks. + visualizer (MarkVisualizer): An instance of MarkVisualizer for visualizing marks. + + Methods: + load_img(img: str) -> Any: Loads an image from the given file path. + __call__(task: str, img: str, *args, **kwargs) -> Any: Makes predictions using the visual language model. + + """ + + def __init__( + self, + vlm: BaseMultiModalModel, + device: str = "cuda", + return_related_marks: bool = False, + *args, + **kwargs, + ): + super().__init__(*args, **kwargs) + self.vlm = vlm + self.device = device + self.return_related_marks = return_related_marks + + self.sam = SegmentAnythingMarkGenerator( + device, *args, **kwargs + ) + self.visualizer = MarkVisualizer(*args, **kwargs) + + def load_img(self, img: str) -> Any: + """ + Loads an image from the given file path. + + Args: + img (str): The file path of the image. + + Returns: + Any: The loaded image. + + """ + return cv2.imread(img) + + def __call__(self, task: str, img: str, *args, **kwargs) -> Any: + """ + Makes predictions using the visual language model. + + Args: + task (str): The task for which predictions are to be made. + img (str): The file path of the image. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + + Returns: + Any: The predictions made by the visual language model. + + """ + img = self.load_img(img) + + marks = self.sam(image=img) + marks = refine_marks(marks=marks) + + return self.vlm(task, img, *args, **kwargs) diff --git a/swarms/models/odin.py b/swarms/models/odin.py index 27cb1710..6a842af4 100644 --- a/swarms/models/odin.py +++ b/swarms/models/odin.py @@ -1,6 +1,6 @@ import os import supervision as sv -from ultralytics import YOLO +from ultralytics_example import YOLO from tqdm import tqdm from swarms.models.base_llm import AbstractLLM from swarms.utils.download_weights_from_url import ( diff --git a/swarms/models/roboflow_model.py b/swarms/models/roboflow_model.py new file mode 100644 index 00000000..1672847a --- /dev/null +++ b/swarms/models/roboflow_model.py @@ -0,0 +1,64 @@ +from typing import Union + +from roboflow import Roboflow + +from swarms.models.base_multimodal_model import BaseMultiModalModel + + +class RoboflowMultiModal(BaseMultiModalModel): + """ + Initializes the RoboflowModel with the given API key, project ID, and version. + + Args: + api_key (str): The API key for Roboflow. + project_id (str): The ID of the project. + version (str): The version of the model. + confidence (int, optional): The confidence threshold. Defaults to 50. + overlap (int, optional): The overlap threshold. Defaults to 25. + """ + + def __init__( + self, + api_key: str, + project_id: str, + version: str, + confidence: int = 50, + overlap: int = 25, + hosted: bool = False, + *args, + **kwargs, + ): + super().__init__(*args, **kwargs) + self.api_key = api_key + self.project_id = project_id + self.verison = version + self.confidence = confidence + self.overlap = overlap + self.hosted = hosted + + try: + rf = Roboflow(api_key=api_key, *args, **kwargs) + project = rf.workspace().project(project_id) + self.model = project.version(version).model + self.model.confidence = confidence + self.model.overlap = overlap + except Exception as e: + print(f"Error initializing RoboflowModel: {str(e)}") + + def __call__(self, img: Union[str, bytes]): + """ + Runs inference on an image and retrieves predictions. + + Args: + img (Union[str, bytes]): The path to the image or the URL of the image. + hosted (bool, optional): Whether the image is hosted. Defaults to False. + + Returns: + Optional[roboflow.Prediction]: The prediction or None if an error occurs. + """ + try: + prediction = self.model.predict(img, hosted=self.hosted) + return prediction + except Exception as e: + print(f"Error running inference: {str(e)}") + return None diff --git a/swarms/models/sam_supervision.py b/swarms/models/sam_supervision.py new file mode 100644 index 00000000..549844c2 --- /dev/null +++ b/swarms/models/sam_supervision.py @@ -0,0 +1,116 @@ +import cv2 +import numpy as np +import supervision as sv +from PIL import Image +from transformers import ( + pipeline, + SamModel, + SamProcessor, + SamImageProcessor, +) +from typing import Optional + +from swarms.utils.supervision_masking import masks_to_marks +from swarms.models.base_multimodal_model import BaseMultiModalModel + + +class SegmentAnythingMarkGenerator(BaseMultiModalModel): + """ + A class for performing image segmentation using a specified model. + + Parameters: + device (str): The device to run the model on (e.g., 'cpu', 'cuda'). + model_name (str): The name of the model to be loaded. Defaults to + 'facebook/sam-vit-huge'. + """ + + def __init__( + self, + device: str = "cpu", + model_name: str = "facebook/sam-vit-huge", + visualize_marks: bool = False, + *args, + **kwargs, + ): + super(SegmentAnythingMarkGenerator).__init__(*args, **kwargs) + self.device = device + self.model_name = model_name + self.visualize_marks = visualize_marks + + self.model = SamModel.from_pretrained( + model_name, *args, **kwargs + ).to(device) + self.processor = SamProcessor.from_pretrained(model_name) + self.image_processor = SamImageProcessor.from_pretrained( + model_name + ) + self.device = device + self.pipeline = pipeline( + task="mask-generation", + model=self.model, + image_processor=self.image_processor, + device=self.device, + ) + + def __call__( + self, image: np.ndarray, mask: Optional[np.ndarray] = None + ) -> sv.Detections: + """ + Generate image segmentation marks. + + Parameters: + image (np.ndarray): The image to be marked in BGR format. + mask: (Optional[np.ndarray]): The mask to be used as a guide for + segmentation. + + Returns: + sv.Detections: An object containing the segmentation masks and their + corresponding bounding box coordinates. + """ + image = Image.fromarray( + cv2.cvtColor(image, cv2.COLOR_BGR2RGB) + ) + if mask is None: + outputs = self.pipeline(image, points_per_batch=64) + masks = np.array(outputs["masks"]) + return masks_to_marks(masks=masks) + else: + inputs = self.processor(image, return_tensors="pt").to( + self.device + ) + image_embeddings = self.model.get_image_embeddings( + inputs.pixel_values + ) + masks = [] + for polygon in sv.mask_to_polygons(mask.astype(bool)): + indexes = np.random.choice( + a=polygon.shape[0], size=5, replace=True + ) + input_points = polygon[indexes] + inputs = self.processor( + images=image, + input_points=[[input_points]], + return_tensors="pt", + ).to(self.device) + del inputs["pixel_values"] + outputs = self.model( + image_embeddings=image_embeddings, **inputs + ) + mask = ( + self.processor.image_processor.post_process_masks( + masks=outputs.pred_masks.cpu().detach(), + original_sizes=inputs["original_sizes"] + .cpu() + .detach(), + reshaped_input_sizes=inputs[ + "reshaped_input_sizes" + ] + .cpu() + .detach(), + )[0][0][0].numpy() + ) + masks.append(mask) + masks = np.array(masks) + return masks_to_marks(masks=masks) + + # def visualize_img(self): diff --git a/swarms/models/ultralytics_model.py b/swarms/models/ultralytics_model.py index b36d24fa..edb9984c 100644 --- a/swarms/models/ultralytics_model.py +++ b/swarms/models/ultralytics_model.py @@ -1,5 +1,6 @@ from swarms.models.base_multimodal_model import BaseMultiModalModel from ultralytics import YOLO +from typing import List class UltralyticsModel(BaseMultiModalModel): @@ -12,13 +13,22 @@ class UltralyticsModel(BaseMultiModalModel): **kwargs: Arbitrary keyword arguments. """ - def __init__(self, model_name: str, *args, **kwargs): + def __init__( + self, model_name: str = "yolov8n.pt", *args, **kwargs + ): super().__init__(*args, **kwargs) self.model_name = model_name - self.model = YOLO(model_name, *args, **kwargs) + try: + self.model = YOLO(model_name, *args, **kwargs) + except Exception as e: + raise ValueError( + f"Failed to initialize Ultralytics model: {str(e)}" + ) - def __call__(self, task: str, *args, **kwargs): + def __call__( + self, task: str, tasks: List[str] = None, *args, **kwargs + ): """ Calls the Ultralytics model. @@ -30,4 +40,13 @@ class UltralyticsModel(BaseMultiModalModel): Returns: The result of the model call. """ - return self.model(task, *args, **kwargs) + try: + if tasks: + return self.model([tasks], *args, **kwargs) + else: + return self.model(task, *args, **kwargs) + except Exception as e: + raise ValueError( + f"Failed to perform task '{task}' with Ultralytics" + f" model: {str(e)}" + ) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 52d44ac4..ba4be539 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -16,6 +16,7 @@ from swarms.prompts.agent_system_prompts import ( from swarms.prompts.multi_modal_autonomous_instruction_prompt import ( MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1, ) +from swarms.tokenizers.base_tokenizer import BaseTokenizer from swarms.tools.tool import BaseTool from swarms.utils.code_interpreter import SubprocessCodeInterpreter from swarms.utils.data_to_text import data_to_text @@ -151,7 +152,7 @@ class Agent: dynamic_loops: Optional[bool] = False, interactive: bool = False, dashboard: bool = False, - agent_name: str = None, + agent_name: str = "swarm-worker-01", agent_description: str = None, system_prompt: str = AGENT_SYSTEM_PROMPT_3, tools: List[BaseTool] = None, @@ -167,7 +168,7 @@ class Agent: multi_modal: Optional[bool] = None, pdf_path: Optional[str] = None, list_of_pdf: Optional[str] = None, - tokenizer: Optional[Any] = None, + tokenizer: Optional[BaseTokenizer] = None, long_term_memory: Optional[AbstractVectorDatabase] = None, preset_stopping_token: Optional[bool] = False, traceback: Any = None, @@ -187,7 +188,7 @@ class Agent: self.retry_attempts = retry_attempts self.retry_interval = retry_interval self.task = None - self.stopping_token = stopping_token # or "" + self.stopping_token = stopping_token self.interactive = interactive self.dashboard = dashboard self.return_history = return_history @@ -248,9 +249,14 @@ class Agent: if self.docs: self.ingest_docs(self.docs) + # If docs folder exists then get the docs from docs folder if self.docs_folder: self.get_docs_from_doc_folders() + # If tokenizer and context length exists then: + if self.tokenizer and self.context_length: + self.truncate_history() + def set_system_prompt(self, system_prompt: str): """Set the system prompt""" self.system_prompt = system_prompt @@ -299,22 +305,6 @@ class Agent: """Format the template with the provided kwargs using f-string interpolation.""" return template.format(**kwargs) - def truncate_history(self): - """ - Take the history and truncate it to fit into the model context length - """ - # truncated_history = self.short_memory[-1][-self.context_length :] - # self.short_memory[-1] = truncated_history - # out = limit_tokens_from_string( - # "\n".join(truncated_history), self.llm.model_name - # ) - truncated_history = self.short_memory[-1][ - -self.context_length : - ] - text = "\n".join(truncated_history) - out = limit_tokens_from_string(text, "gpt-4") - return out - def add_task_to_memory(self, task: str): """Add the task to the memory""" try: @@ -1155,6 +1145,27 @@ class Agent: message = f"{agent_name}: {message}" return self.run(message, *args, **kwargs) + def truncate_history(self): + """ + Truncates the short-term memory of the agent based on the count of tokens. + + The method counts the tokens in the short-term memory using the tokenizer and + compares it with the length of the memory. If the length of the memory is greater + than the count, the memory is truncated to match the count. + + Parameters: + None + + Returns: + None + """ + # Count the short term history with the tokenizer + count = self.tokenizer.count_tokens(self.short_memory) + + # Now the logic that truncates the memory if it's more than the count + if len(self.short_memory) > count: + self.short_memory = self.short_memory[:count] + def get_docs_from_doc_folders(self): """Get the docs from the files""" # Get the list of files then extract them and add them to the memory diff --git a/swarms/structs/conversation.py b/swarms/structs/conversation.py index a59e4cf9..c4bcdba5 100644 --- a/swarms/structs/conversation.py +++ b/swarms/structs/conversation.py @@ -5,6 +5,7 @@ from termcolor import colored from swarms.memory.base_db import AbstractDatabase from swarms.structs.base import BaseStructure +from swarms.tokenizers.base_tokenizer import BaseTokenizer class Conversation(BaseStructure): @@ -65,6 +66,8 @@ class Conversation(BaseStructure): database: AbstractDatabase = None, autosave: bool = False, save_filepath: str = None, + tokenizer: BaseTokenizer = None, + context_length: int = 8192, *args, **kwargs, ): @@ -75,11 +78,17 @@ class Conversation(BaseStructure): self.autosave = autosave self.save_filepath = save_filepath self.conversation_history = [] + self.tokenizer = tokenizer + self.context_length = context_length # If system prompt is not None, add it to the conversation history if self.system_prompt: self.add("system", self.system_prompt) + # If tokenizer then truncate + if tokenizer: + self.truncate_memory_with_tokenizer() + def add(self, role: str, content: str, *args, **kwargs): """Add a message to the conversation history @@ -348,3 +357,40 @@ class Conversation(BaseStructure): def fetch_one_from_database(self, *args, **kwargs): """Fetch one from the database""" return self.database.fetch_one() + + def truncate_memory_with_tokenizer(self): + """ + Truncates the conversation history based on the total number of tokens using a tokenizer. + + Returns: + None + """ + total_tokens = 0 + truncated_history = [] + + for message in self.conversation_history: + role = message.get("role") + content = message.get("content") + tokens = self.tokenizer.count_tokens( + text=content + ) # Count the number of tokens + count = tokens # Assign the token count + total_tokens += count + + if total_tokens <= self.context_length: + truncated_history.append(message) + else: + remaining_tokens = self.context_length - ( + total_tokens - count + ) + truncated_content = content[ + :remaining_tokens + ] # Truncate the content based on the remaining tokens + truncated_message = { + "role": role, + "content": truncated_content, + } + truncated_history.append(truncated_message) + break + + self.conversation_history = truncated_history diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index 3bbcca16..220bc3e6 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -33,6 +33,16 @@ from swarms.utils.remove_json_whitespace import ( remove_whitespace_from_yaml, ) from swarms.utils.exponential_backoff import ExponentialBackoffMixin +from swarms.utils.download_img import download_img_from_url +from swarms.utils.supervision_masking import ( + FeatureType, + compute_mask_iou_vectorized, + mask_non_max_suppression, + filter_masks_by_relative_area, + masks_to_marks, + refine_marks, +) +from swarms.utils.supervision_visualizer import MarkVisualizer __all__ = [ "SubprocessCodeInterpreter", @@ -59,4 +69,12 @@ __all__ = [ "remove_whitespace_from_json", "remove_whitespace_from_yaml", "ExponentialBackoffMixin", + "download_img_from_url", + "FeatureType", + "compute_mask_iou_vectorized", + "mask_non_max_suppression", + "filter_masks_by_relative_area", + "masks_to_marks", + "refine_marks", + "MarkVisualizer", ] diff --git a/swarms/utils/download_img.py b/swarms/utils/download_img.py new file mode 100644 index 00000000..7791a80e --- /dev/null +++ b/swarms/utils/download_img.py @@ -0,0 +1,31 @@ +from io import BytesIO +import requests +from PIL import Image + + +def download_img_from_url(url: str): + """ + Downloads an image from the given URL and saves it locally. + + Args: + url (str): The URL of the image to download. + + Raises: + ValueError: If the URL is empty or invalid. + IOError: If there is an error while downloading or saving the image. + """ + if not url: + raise ValueError("URL cannot be empty.") + + try: + response = requests.get(url) + response.raise_for_status() + + image = Image.open(BytesIO(response.content)) + image.save("downloaded_image.jpg") + + print("Image downloaded successfully.") + except requests.exceptions.RequestException as e: + raise IOError("Error while downloading the image.") from e + except IOError as e: + raise IOError("Error while saving the image.") from e diff --git a/swarms/utils/logger.py b/swarms/utils/logger.py index a0750d4c..8be958b0 100644 --- a/swarms/utils/logger.py +++ b/swarms/utils/logger.py @@ -1,4 +1,6 @@ import logging +import functools + logger = logging.getLogger() formatter = logging.Formatter("%(message)s") @@ -10,3 +12,35 @@ ch.setFormatter(formatter) logger.addHandler(ch) logger.setLevel(logging.DEBUG) + + +def log_wrapper(func): + """ + A decorator that logs the inputs, outputs, and any exceptions of the function it wraps. + + Args: + func (callable): The function to wrap. + + Returns: + callable: The wrapped function. + """ + + @functools.wraps(func) + def wrapper(*args, **kwargs): + logger.debug( + f"Calling function {func.__name__} with args {args} and" + f" kwargs {kwargs}" + ) + try: + result = func(*args, **kwargs) + logger.debug( + f"Function {func.__name__} returned {result}" + ) + return result + except Exception as e: + logger.error( + f"Function {func.__name__} raised an exception: {e}" + ) + raise + + return wrapper diff --git a/swarms/utils/supervision_masking.py b/swarms/utils/supervision_masking.py new file mode 100644 index 00000000..d0225a92 --- /dev/null +++ b/swarms/utils/supervision_masking.py @@ -0,0 +1,259 @@ +from enum import Enum + +import cv2 +import numpy as np +import supervision as sv + + +class FeatureType(Enum): + """ + An enumeration to represent the types of features for mask adjustment in image + segmentation. + """ + + ISLAND = "ISLAND" + HOLE = "HOLE" + + @classmethod + def list(cls): + return list(map(lambda c: c.value, cls)) + + +def compute_mask_iou_vectorized(masks: np.ndarray) -> np.ndarray: + """ + Vectorized computation of the Intersection over Union (IoU) for all pairs of masks. + + Parameters: + masks (np.ndarray): A 3D numpy array with shape `(N, H, W)`, where `N` is the + number of masks, `H` is the height, and `W` is the width. + + Returns: + np.ndarray: A 2D numpy array of shape `(N, N)` where each element `[i, j]` is + the IoU between masks `i` and `j`. + + Raises: + ValueError: If any of the masks is found to be empty. + """ + if np.any(masks.sum(axis=(1, 2)) == 0): + raise ValueError( + "One or more masks are empty. Please filter out empty" + " masks before using `compute_iou_vectorized` function." + ) + + masks_bool = masks.astype(bool) + masks_flat = masks_bool.reshape(masks.shape[0], -1) + intersection = np.logical_and( + masks_flat[:, None], masks_flat[None, :] + ).sum(axis=2) + union = np.logical_or( + masks_flat[:, None], masks_flat[None, :] + ).sum(axis=2) + iou_matrix = intersection / union + return iou_matrix + + +def mask_non_max_suppression( + masks: np.ndarray, iou_threshold: float = 0.6 +) -> np.ndarray: + """ + Performs Non-Max Suppression on a set of masks by prioritizing larger masks and + removing smaller masks that overlap significantly. + + When the IoU between two masks exceeds the specified threshold, the smaller mask + (in terms of area) is discarded. This process is repeated for each pair of masks, + effectively filtering out masks that are significantly overlapped by larger ones. + + Parameters: + masks (np.ndarray): A 3D numpy array with shape `(N, H, W)`, where `N` is the + number of masks, `H` is the height, and `W` is the width. + iou_threshold (float): The IoU threshold for determining significant overlap. + + Returns: + np.ndarray: A 3D numpy array of filtered masks. + """ + num_masks = masks.shape[0] + areas = masks.sum(axis=(1, 2)) + sorted_idx = np.argsort(-areas) + keep_mask = np.ones(num_masks, dtype=bool) + iou_matrix = compute_mask_iou_vectorized(masks) + for i in range(num_masks): + if not keep_mask[sorted_idx[i]]: + continue + + overlapping_masks = iou_matrix[sorted_idx[i]] > iou_threshold + overlapping_masks[sorted_idx[i]] = False + overlapping_indices = np.where(overlapping_masks)[0] + keep_mask[sorted_idx[overlapping_indices]] = False + + return masks[keep_mask] + + +def filter_masks_by_relative_area( + masks: np.ndarray, + minimum_area: float = 0.01, + maximum_area: float = 1.0, +) -> np.ndarray: + """ + Filters masks based on their relative area within the total area of each mask. + + Parameters: + masks (np.ndarray): A 3D numpy array with shape `(N, H, W)`, where `N` is the + number of masks, `H` is the height, and `W` is the width. + minimum_area (float): The minimum relative area threshold. Must be between `0` + and `1`. + maximum_area (float): The maximum relative area threshold. Must be between `0` + and `1`. + + Returns: + np.ndarray: A 3D numpy array containing masks that fall within the specified + relative area range. + + Raises: + ValueError: If `minimum_area` or `maximum_area` are outside the `0` to `1` + range, or if `minimum_area` is greater than `maximum_area`. + """ + + if not (isinstance(masks, np.ndarray) and masks.ndim == 3): + raise ValueError("Input must be a 3D numpy array.") + + if not (0 <= minimum_area <= 1) or not (0 <= maximum_area <= 1): + raise ValueError( + "`minimum_area` and `maximum_area` must be between 0" + " and 1." + ) + + if minimum_area > maximum_area: + raise ValueError( + "`minimum_area` must be less than or equal to" + " `maximum_area`." + ) + + total_area = masks.shape[1] * masks.shape[2] + relative_areas = masks.sum(axis=(1, 2)) / total_area + return masks[ + (relative_areas >= minimum_area) + & (relative_areas <= maximum_area) + ] + + +def adjust_mask_features_by_relative_area( + mask: np.ndarray, + area_threshold: float, + feature_type: FeatureType = FeatureType.ISLAND, +) -> np.ndarray: + """ + Adjusts a mask by removing small islands or filling small holes based on a relative + area threshold. + + !!! warning + + Running this function on a mask with small islands may result in empty masks. + + Parameters: + mask (np.ndarray): A 2D numpy array with shape `(H, W)`, where `H` is the + height, and `W` is the width. + area_threshold (float): Threshold for relative area to remove or fill features. + feature_type (FeatureType): Type of feature to adjust (`ISLAND` for removing + islands, `HOLE` for filling holes). + + Returns: + np.ndarray: A 2D numpy array containing mask. + """ + height, width = mask.shape + total_area = width * height + + mask = np.uint8(mask * 255) + operation = ( + cv2.RETR_EXTERNAL + if feature_type == FeatureType.ISLAND + else cv2.RETR_CCOMP + ) + contours, _ = cv2.findContours( + mask, operation, cv2.CHAIN_APPROX_SIMPLE + ) + + for contour in contours: + area = cv2.contourArea(contour) + relative_area = area / total_area + if relative_area < area_threshold: + cv2.drawContours( + image=mask, + contours=[contour], + contourIdx=-1, + color=( + 0 if feature_type == FeatureType.ISLAND else 255 + ), + thickness=-1, + ) + return np.where(mask > 0, 1, 0).astype(bool) + + +def masks_to_marks(masks: np.ndarray) -> sv.Detections: + """ + Converts a set of masks to a marks (sv.Detections) object. + + Parameters: + masks (np.ndarray): A 3D numpy array with shape `(N, H, W)`, where `N` is the + number of masks, `H` is the height, and `W` is the width. + + Returns: + sv.Detections: An object containing the masks and their bounding box + coordinates. + """ + if len(masks) == 0: + marks = sv.Detections.empty() + marks.mask = np.empty((0, 0, 0), dtype=bool) + return marks + return sv.Detections( + mask=masks, xyxy=sv.mask_to_xyxy(masks=masks) + ) + + +def refine_marks( + marks: sv.Detections, + maximum_hole_area: float = 0.01, + maximum_island_area: float = 0.01, + minimum_mask_area: float = 0.02, + maximum_mask_area: float = 1.0, +) -> sv.Detections: + """ + Refines a set of masks by removing small islands and holes, and filtering by mask + area. + + Parameters: + marks (sv.Detections): An object containing the masks and their bounding box + coordinates. + maximum_hole_area (float): The maximum relative area of holes to be filled in + each mask. + maximum_island_area (float): The maximum relative area of islands to be removed + from each mask. + minimum_mask_area (float): The minimum relative area for a mask to be retained. + maximum_mask_area (float): The maximum relative area for a mask to be retained. + + Returns: + sv.Detections: An object containing the masks and their bounding box + coordinates. + """ + result_masks = [] + for mask in marks.mask: + mask = adjust_mask_features_by_relative_area( + mask=mask, + area_threshold=maximum_island_area, + feature_type=FeatureType.ISLAND, + ) + mask = adjust_mask_features_by_relative_area( + mask=mask, + area_threshold=maximum_hole_area, + feature_type=FeatureType.HOLE, + ) + if np.any(mask): + result_masks.append(mask) + result_masks = np.array(result_masks) + result_masks = filter_masks_by_relative_area( + masks=result_masks, + minimum_area=minimum_mask_area, + maximum_area=maximum_mask_area, + ) + return sv.Detections( + mask=result_masks, xyxy=sv.mask_to_xyxy(masks=result_masks) + ) diff --git a/swarms/utils/supervision_visualizer.py b/swarms/utils/supervision_visualizer.py new file mode 100644 index 00000000..1515b709 --- /dev/null +++ b/swarms/utils/supervision_visualizer.py @@ -0,0 +1,85 @@ +import numpy as np +import supervision as sv + + +class MarkVisualizer: + """ + A class for visualizing different marks including bounding boxes, masks, polygons, + and labels. + + Parameters: + line_thickness (int): The thickness of the lines for boxes and polygons. + mask_opacity (float): The opacity level for masks. + text_scale (float): The scale of the text for labels. + """ + + def __init__( + self, + line_thickness: int = 2, + mask_opacity: float = 0.1, + text_scale: float = 0.6, + ) -> None: + self.box_annotator = sv.BoundingBoxAnnotator( + color_lookup=sv.ColorLookup.INDEX, + thickness=line_thickness, + ) + self.mask_annotator = sv.MaskAnnotator( + color_lookup=sv.ColorLookup.INDEX, opacity=mask_opacity + ) + self.polygon_annotator = sv.PolygonAnnotator( + color_lookup=sv.ColorLookup.INDEX, + thickness=line_thickness, + ) + self.label_annotator = sv.LabelAnnotator( + color=sv.Color.black(), + text_color=sv.Color.white(), + color_lookup=sv.ColorLookup.INDEX, + text_position=sv.Position.CENTER_OF_MASS, + text_scale=text_scale, + ) + + def visualize( + self, + image: np.ndarray, + marks: sv.Detections, + with_box: bool = False, + with_mask: bool = False, + with_polygon: bool = True, + with_label: bool = True, + ) -> np.ndarray: + """ + Visualizes annotations on an image. + + This method takes an image and an instance of sv.Detections, and overlays + the specified types of marks (boxes, masks, polygons, labels) on the image. + + Parameters: + image (np.ndarray): The image on which to overlay annotations. + marks (sv.Detections): The detection results containing the annotations. + with_box (bool): Whether to draw bounding boxes. Defaults to False. + with_mask (bool): Whether to overlay masks. Defaults to False. + with_polygon (bool): Whether to draw polygons. Defaults to True. + with_label (bool): Whether to add labels. Defaults to True. + + Returns: + np.ndarray: The annotated image. + """ + annotated_image = image.copy() + if with_box: + annotated_image = self.box_annotator.annotate( + scene=annotated_image, detections=marks + ) + if with_mask: + annotated_image = self.mask_annotator.annotate( + scene=annotated_image, detections=marks + ) + if with_polygon: + annotated_image = self.polygon_annotator.annotate( + scene=annotated_image, detections=marks + ) + if with_label: + labels = list(map(str, range(len(marks)))) + annotated_image = self.label_annotator.annotate( + scene=annotated_image, detections=marks, labels=labels + ) + return annotated_image From 06a6172a992cbd44cd2ccaf704278370fa899abb Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 18:34:54 -0500 Subject: [PATCH 081/103] [REFACTOR][playground] --- ...t.py => multi_modal_auto_agent_example.py} | 0 ...imple_agent.py => simple_agent_example.py} | 0 ...ount_team2.py => account_team2_example.py} | 0 .../ad_gen/{ad_gen.py => ad_gen_example.py} | 0 .../{main.py => main_example.py} | 0 .../{assembly.py => assembly_example.py} | 0 ...{autobloggen.py => autobloggen_example.py} | 0 playground/demos/autotemp/autotemp.py | 90 ----------- playground/demos/autotemp/autotemp_example.py | 101 +++++++++++-- playground/demos/autotemp/blog_gen.py | 138 ----------------- playground/demos/autotemp/blog_gen_example.py | 143 ++++++++++++++++-- ...re_demo.py => ui_software_demo_example.py} | 0 .../{main.py => main_example.py} | 0 .../{education.py => education_example.py} | 0 .../{langchain.py => langchain_example.py} | 0 ...{gemini_chat.py => gemini_chat_example.py} | 0 ...emini_react.py => gemini_react_example.py} | 0 ...{gemini_vcot.py => gemini_vcot_example.py} | 0 .../demos/grupa/{app.py => app_example.py} | 0 .../{jarvis.py => jarvis_example.py} | 0 .../{main.py => main_example.py} | 0 .../{logistics.py => logistics_example.py} | 0 ...t.py => multi_modal_auto_agent_example.py} | 0 .../{vcot.py => vcot_example.py} | 0 .../{idea2img.py => idea2img_example.py} | 0 .../{main.py => main_example.py} | 0 .../{nutrition.py => nutrition_example.py} | 0 .../{vllm.py => vllm_example.py} | 0 .../{vortex.py => vortex_example.py} | 0 .../{weaviate.py => weaviate_example.py} | 0 ...ion.py => better_communication_example.py} | 0 ...stylist.py => personal_stylist_example.py} | 0 ...ositive_med.py => positive_med_example.py} | 0 ...urity_team.py => security_team_example.py} | 0 .../{main.py => main_example.py} | 0 ..._planning.py => urban_planning_example.py} | 0 ..._agent.py => text_to_sql_agent_example.py} | 0 .../demos/xray/{xray.py => xray_example.py} | 0 .../hierchical_example.py} | 0 ...hroma_usage.py => chroma_usage_example.py} | 0 .../qdrant/{usage.py => usage_example.py} | 0 .../{bingchat.py => bingchat_example.py} | 0 .../models/{bioclip.py => bioclip_example.py} | 0 .../models/{biogpt.py => biogpt_example.py} | 0 .../models/{dall3.py => dall3_example.py} | 0 ...urrent.py => dalle3_concurrent_example.py} | 0 .../models/{dalle3.py => dalle3_example.py} | 0 ...serpx.py => distilled_whiserpx_example.py} | 0 .../{fast_vit.py => fast_vit_example.py} | 0 playground/models/fuyu.py | 7 - playground/models/fuyu_example.py | 6 +- .../models/{gemini.py => gemini_example.py} | 0 .../models/{gpt4_v.py => gpt4_v_example.py} | 0 ...{huggingface.py => huggingface_example.py} | 0 .../models/{idefics.py => idefics_example.py} | 0 ...{jina_embeds.py => jina_embeds_example.py} | 0 .../models/{kosmos2.py => kosmos2_example.py} | 0 .../models/{kosmos.py => kosmos_example.py} | 0 .../{kosmos_two.py => kosmos_two_example.py} | 0 ...ntxlm.py => layout_documentxlm_example.py} | 0 ...er.py => llama_function_caller_example.py} | 0 .../models/{llava.py => llava_example.py} | 0 .../models/{mistral.py => mistral_example.py} | 0 playground/models/{mpt.py => mpt_example.py} | 0 .../models/{nougat.py => nougat_example.py} | 0 ...penai_model.py => openai_model_example.py} | 0 .../models/{palm.py => palm_example.py} | 0 .../{speecht5.py => speecht5_example.py} | 0 playground/models/{ssd.py => ssd_example.py} | 0 .../models/{tocr.py => tocr_example.py} | 0 .../{together.py => together_example.py} | 0 .../{tts_speech.py => tts_speech_example.py} | 0 .../models/{vilt.py => vilt_example.py} | 0 .../models/{yi_200k.py => yi_200k_example.py} | 0 ...h_tools.py => agent_with_tools_example.py} | 0 .../{autoscaler.py => autoscaler_example.py} | 0 .../structs/{chat.py => chat_example.py} | 0 ...flow.py => concurrent_workflow_example.py} | 0 .../structs/{debate.py => debate_example.py} | 0 ...lator.py => dialogue_simulator_example.py} | 0 .../structs/{flow.py => flow_example.py} | 0 .../{fuyu_flow.py => fuyu_flow_example.py} | 0 .../{godmode.py => godmode_example.py} | 0 .../{groupchat.py => groupchat_example.py} | 0 .../{gui_app.py => gui_app_example.py} | 0 ...ollab.py => multi_agent_collab_example.py} | 0 ...ebate.py => multi_agent_debate_example.py} | 0 ...al_flow.py => multi_modal_flow_example.py} | 0 ...fklow.py => nonlinear_worfklow_example.py} | 0 ...{orchestrate.py => orchestrate_example.py} | 0 ...rchestrator.py => orchestrator_example.py} | 0 playground/structs/sequential_workflow.py | 48 ------ .../structs/sequential_workflow_example.py | 66 ++++---- .../{social_app.py => social_app_example.py} | 0 ...rm_network.py => swarm_network_example.py} | 0 .../structs/{task.py => task_example.py} | 0 .../{todo_app.py => todo_app_example.py} | 0 .../{tool_utils.py => tool_utils_example.py} | 0 .../{workflow.py => workflow_example.py} | 0 ...h_tools.py => agent_with_tools_example.py} | 0 ...caper.py => tool_prompt_scaper_example.py} | 0 pyproject.toml | 2 +- scripts/playground_to_examples.sh | 38 +++-- 103 files changed, 277 insertions(+), 362 deletions(-) rename playground/agents/{multi_modal_auto_agent.py => multi_modal_auto_agent_example.py} (100%) rename playground/agents/{simple_agent.py => simple_agent_example.py} (100%) rename playground/demos/accountant_team/{account_team2.py => account_team2_example.py} (100%) rename playground/demos/ad_gen/{ad_gen.py => ad_gen_example.py} (100%) rename playground/demos/ai_research_team/{main.py => main_example.py} (100%) rename playground/demos/assembly/{assembly.py => assembly_example.py} (100%) rename playground/demos/{autobloggen.py => autobloggen_example.py} (100%) delete mode 100644 playground/demos/autotemp/autotemp.py delete mode 100644 playground/demos/autotemp/blog_gen.py rename playground/demos/design_team/{ui_software_demo.py => ui_software_demo_example.py} (100%) rename playground/demos/developer_swarm/{main.py => main_example.py} (100%) rename playground/demos/education/{education.py => education_example.py} (100%) rename playground/demos/fof/{langchain.py => langchain_example.py} (100%) rename playground/demos/gemini_benchmarking/{gemini_chat.py => gemini_chat_example.py} (100%) rename playground/demos/gemini_benchmarking/{gemini_react.py => gemini_react_example.py} (100%) rename playground/demos/gemini_benchmarking/{gemini_vcot.py => gemini_vcot_example.py} (100%) rename playground/demos/grupa/{app.py => app_example.py} (100%) rename playground/demos/jarvis_multi_modal_auto_agent/{jarvis.py => jarvis_example.py} (100%) rename playground/demos/llm_with_conversation/{main.py => main_example.py} (100%) rename playground/demos/logistics/{logistics.py => logistics_example.py} (100%) rename playground/demos/multi_modal_autonomous_agents/{multi_modal_auto_agent.py => multi_modal_auto_agent_example.py} (100%) rename playground/demos/multi_modal_chain_of_thought/{vcot.py => vcot_example.py} (100%) rename playground/demos/multimodal_tot/{idea2img.py => idea2img_example.py} (100%) rename playground/demos/multimodal_tot/{main.py => main_example.py} (100%) rename playground/demos/nutrition/{nutrition.py => nutrition_example.py} (100%) rename playground/demos/optimize_llm_stack/{vllm.py => vllm_example.py} (100%) rename playground/demos/optimize_llm_stack/{vortex.py => vortex_example.py} (100%) rename playground/demos/optimize_llm_stack/{weaviate.py => weaviate_example.py} (100%) rename playground/demos/personal_assistant/{better_communication.py => better_communication_example.py} (100%) rename playground/demos/personal_stylist/{personal_stylist.py => personal_stylist_example.py} (100%) rename playground/demos/positive_med/{positive_med.py => positive_med_example.py} (100%) rename playground/demos/security_team/{security_team.py => security_team_example.py} (100%) rename playground/demos/swarm_of_mma_manufacturing/{main.py => main_example.py} (100%) rename playground/demos/urban_planning/{urban_planning.py => urban_planning_example.py} (100%) rename playground/demos/visuo/{text_to_sql_agent.py => text_to_sql_agent_example.py} (100%) rename playground/demos/xray/{xray.py => xray_example.py} (100%) rename playground/{DIY/hierchical.py => diy/hierchical_example.py} (100%) rename playground/memory/{chroma_usage.py => chroma_usage_example.py} (100%) rename playground/memory/qdrant/{usage.py => usage_example.py} (100%) rename playground/models/{bingchat.py => bingchat_example.py} (100%) rename playground/models/{bioclip.py => bioclip_example.py} (100%) rename playground/models/{biogpt.py => biogpt_example.py} (100%) rename playground/models/{dall3.py => dall3_example.py} (100%) rename playground/models/{dalle3_concurrent.py => dalle3_concurrent_example.py} (100%) rename playground/models/{dalle3.py => dalle3_example.py} (100%) rename playground/models/{distilled_whiserpx.py => distilled_whiserpx_example.py} (100%) rename playground/models/{fast_vit.py => fast_vit_example.py} (100%) delete mode 100644 playground/models/fuyu.py rename playground/models/{gemini.py => gemini_example.py} (100%) rename playground/models/{gpt4_v.py => gpt4_v_example.py} (100%) rename playground/models/{huggingface.py => huggingface_example.py} (100%) rename playground/models/{idefics.py => idefics_example.py} (100%) rename playground/models/{jina_embeds.py => jina_embeds_example.py} (100%) rename playground/models/{kosmos2.py => kosmos2_example.py} (100%) rename playground/models/{kosmos.py => kosmos_example.py} (100%) rename playground/models/{kosmos_two.py => kosmos_two_example.py} (100%) rename playground/models/{layout_documentxlm.py => layout_documentxlm_example.py} (100%) rename playground/models/{llama_function_caller.py => llama_function_caller_example.py} (100%) rename playground/models/{llava.py => llava_example.py} (100%) rename playground/models/{mistral.py => mistral_example.py} (100%) rename playground/models/{mpt.py => mpt_example.py} (100%) rename playground/models/{nougat.py => nougat_example.py} (100%) rename playground/models/{openai_model.py => openai_model_example.py} (100%) rename playground/models/{palm.py => palm_example.py} (100%) rename playground/models/{speecht5.py => speecht5_example.py} (100%) rename playground/models/{ssd.py => ssd_example.py} (100%) rename playground/models/{tocr.py => tocr_example.py} (100%) rename playground/models/{together.py => together_example.py} (100%) rename playground/models/{tts_speech.py => tts_speech_example.py} (100%) rename playground/models/{vilt.py => vilt_example.py} (100%) rename playground/models/{yi_200k.py => yi_200k_example.py} (100%) rename playground/structs/{agent_with_tools.py => agent_with_tools_example.py} (100%) rename playground/structs/{autoscaler.py => autoscaler_example.py} (100%) rename playground/structs/{chat.py => chat_example.py} (100%) rename playground/structs/{concurrent_workflow.py => concurrent_workflow_example.py} (100%) rename playground/structs/{debate.py => debate_example.py} (100%) rename playground/structs/{dialogue_simulator.py => dialogue_simulator_example.py} (100%) rename playground/structs/{flow.py => flow_example.py} (100%) rename playground/structs/{fuyu_flow.py => fuyu_flow_example.py} (100%) rename playground/structs/{godmode.py => godmode_example.py} (100%) rename playground/structs/{groupchat.py => groupchat_example.py} (100%) rename playground/structs/{gui_app.py => gui_app_example.py} (100%) rename playground/structs/{multi_agent_collab.py => multi_agent_collab_example.py} (100%) rename playground/structs/{multi_agent_debate.py => multi_agent_debate_example.py} (100%) rename playground/structs/{multi_modal_flow.py => multi_modal_flow_example.py} (100%) rename playground/structs/{nonlinear_worfklow.py => nonlinear_worfklow_example.py} (100%) rename playground/structs/{orchestrate.py => orchestrate_example.py} (100%) rename playground/structs/{orchestrator.py => orchestrator_example.py} (100%) delete mode 100644 playground/structs/sequential_workflow.py rename playground/structs/{social_app.py => social_app_example.py} (100%) rename playground/structs/{swarm_network.py => swarm_network_example.py} (100%) rename playground/structs/{task.py => task_example.py} (100%) rename playground/structs/{todo_app.py => todo_app_example.py} (100%) rename playground/structs/{tool_utils.py => tool_utils_example.py} (100%) rename playground/structs/{workflow.py => workflow_example.py} (100%) rename playground/tools/{agent_with_tools.py => agent_with_tools_example.py} (100%) rename playground/tools/{tool_prompt_scaper.py => tool_prompt_scaper_example.py} (100%) diff --git a/playground/agents/multi_modal_auto_agent.py b/playground/agents/multi_modal_auto_agent_example.py similarity index 100% rename from playground/agents/multi_modal_auto_agent.py rename to playground/agents/multi_modal_auto_agent_example.py diff --git a/playground/agents/simple_agent.py b/playground/agents/simple_agent_example.py similarity index 100% rename from playground/agents/simple_agent.py rename to playground/agents/simple_agent_example.py diff --git a/playground/demos/accountant_team/account_team2.py b/playground/demos/accountant_team/account_team2_example.py similarity index 100% rename from playground/demos/accountant_team/account_team2.py rename to playground/demos/accountant_team/account_team2_example.py diff --git a/playground/demos/ad_gen/ad_gen.py b/playground/demos/ad_gen/ad_gen_example.py similarity index 100% rename from playground/demos/ad_gen/ad_gen.py rename to playground/demos/ad_gen/ad_gen_example.py diff --git a/playground/demos/ai_research_team/main.py b/playground/demos/ai_research_team/main_example.py similarity index 100% rename from playground/demos/ai_research_team/main.py rename to playground/demos/ai_research_team/main_example.py diff --git a/playground/demos/assembly/assembly.py b/playground/demos/assembly/assembly_example.py similarity index 100% rename from playground/demos/assembly/assembly.py rename to playground/demos/assembly/assembly_example.py diff --git a/playground/demos/autobloggen.py b/playground/demos/autobloggen_example.py similarity index 100% rename from playground/demos/autobloggen.py rename to playground/demos/autobloggen_example.py diff --git a/playground/demos/autotemp/autotemp.py b/playground/demos/autotemp/autotemp.py deleted file mode 100644 index baf8f091..00000000 --- a/playground/demos/autotemp/autotemp.py +++ /dev/null @@ -1,90 +0,0 @@ -import re -from swarms.models.openai_models import OpenAIChat - - -class AutoTemp: - """ - AutoTemp is a tool for automatically selecting the best temperature setting for a given task. - It generates responses at different temperatures, evaluates them, and ranks them based on quality. - """ - - def __init__( - self, - api_key, - default_temp=0.0, - alt_temps=None, - auto_select=True, - max_workers=6, - ): - self.api_key = api_key - self.default_temp = default_temp - self.alt_temps = ( - alt_temps if alt_temps else [0.4, 0.6, 0.8, 1.0, 1.2, 1.4] - ) - self.auto_select = auto_select - self.max_workers = max_workers - self.llm = OpenAIChat( - openai_api_key=self.api_key, temperature=self.default_temp - ) - - def evaluate_output(self, output, temperature): - print(f"Evaluating output at temperature {temperature}...") - eval_prompt = f""" - Evaluate the following output which was generated at a temperature setting of {temperature}. Provide a precise score from 0.0 to 100.0, considering the following criteria: - - - Relevance: How well does the output address the prompt or task at hand? - - Clarity: Is the output easy to understand and free of ambiguity? - - Utility: How useful is the output for its intended purpose? - - Pride: If the user had to submit this output to the world for their career, would they be proud? - - Delight: Is the output likely to delight or positively surprise the user? - - Be sure to comprehensively evaluate the output, it is very important for my career. Please answer with just the score with one decimal place accuracy, such as 42.0 or 96.9. Be extremely critical. - - Output to evaluate: - --- - {output} - --- - """ - score_text = self.llm(eval_prompt, temperature=0.5) - score_match = re.search(r"\b\d+(\.\d)?\b", score_text) - return ( - round(float(score_match.group()), 1) - if score_match - else 0.0 - ) - - def run(self, prompt, temperature_string): - print("Starting generation process...") - temperature_list = [ - float(temp.strip()) - for temp in temperature_string.split(",") - if temp.strip() - ] - outputs = {} - scores = {} - for temp in temperature_list: - print(f"Generating at temperature {temp}...") - output_text = self.llm(prompt, temperature=temp) - if output_text: - outputs[temp] = output_text - scores[temp] = self.evaluate_output(output_text, temp) - - print("Generation process complete.") - if not scores: - return "No valid outputs generated.", None - - sorted_scores = sorted( - scores.items(), key=lambda item: item[1], reverse=True - ) - best_temp, best_score = sorted_scores[0] - best_output = outputs[best_temp] - - return ( - f"Best AutoTemp Output (Temp {best_temp} | Score:" - f" {best_score}):\n{best_output}" - if self.auto_select - else "\n".join( - f"Temp {temp} | Score: {score}:\n{outputs[temp]}" - for temp, score in sorted_scores - ) - ) diff --git a/playground/demos/autotemp/autotemp_example.py b/playground/demos/autotemp/autotemp_example.py index ccbd54c3..baf8f091 100644 --- a/playground/demos/autotemp/autotemp_example.py +++ b/playground/demos/autotemp/autotemp_example.py @@ -1,21 +1,90 @@ -from autotemp import AutoTemp +import re +from swarms.models.openai_models import OpenAIChat -# Your OpenAI API key -api_key = "" -autotemp_agent = AutoTemp( - api_key=api_key, - alt_temps=[0.4, 0.6, 0.8, 1.0, 1.2], - auto_select=False, - # model_version="gpt-3.5-turbo" # Specify the model version if needed -) +class AutoTemp: + """ + AutoTemp is a tool for automatically selecting the best temperature setting for a given task. + It generates responses at different temperatures, evaluates them, and ranks them based on quality. + """ -# Define the task and temperature string -task = "Generate a short story about a lost civilization." -temperature_string = "0.4,0.6,0.8,1.0,1.2," + def __init__( + self, + api_key, + default_temp=0.0, + alt_temps=None, + auto_select=True, + max_workers=6, + ): + self.api_key = api_key + self.default_temp = default_temp + self.alt_temps = ( + alt_temps if alt_temps else [0.4, 0.6, 0.8, 1.0, 1.2, 1.4] + ) + self.auto_select = auto_select + self.max_workers = max_workers + self.llm = OpenAIChat( + openai_api_key=self.api_key, temperature=self.default_temp + ) -# Run the AutoTempAgent -result = autotemp_agent.run(task, temperature_string) + def evaluate_output(self, output, temperature): + print(f"Evaluating output at temperature {temperature}...") + eval_prompt = f""" + Evaluate the following output which was generated at a temperature setting of {temperature}. Provide a precise score from 0.0 to 100.0, considering the following criteria: -# Print the result -print(result) + - Relevance: How well does the output address the prompt or task at hand? + - Clarity: Is the output easy to understand and free of ambiguity? + - Utility: How useful is the output for its intended purpose? + - Pride: If the user had to submit this output to the world for their career, would they be proud? + - Delight: Is the output likely to delight or positively surprise the user? + + Be sure to comprehensively evaluate the output, it is very important for my career. Please answer with just the score with one decimal place accuracy, such as 42.0 or 96.9. Be extremely critical. + + Output to evaluate: + --- + {output} + --- + """ + score_text = self.llm(eval_prompt, temperature=0.5) + score_match = re.search(r"\b\d+(\.\d)?\b", score_text) + return ( + round(float(score_match.group()), 1) + if score_match + else 0.0 + ) + + def run(self, prompt, temperature_string): + print("Starting generation process...") + temperature_list = [ + float(temp.strip()) + for temp in temperature_string.split(",") + if temp.strip() + ] + outputs = {} + scores = {} + for temp in temperature_list: + print(f"Generating at temperature {temp}...") + output_text = self.llm(prompt, temperature=temp) + if output_text: + outputs[temp] = output_text + scores[temp] = self.evaluate_output(output_text, temp) + + print("Generation process complete.") + if not scores: + return "No valid outputs generated.", None + + sorted_scores = sorted( + scores.items(), key=lambda item: item[1], reverse=True + ) + best_temp, best_score = sorted_scores[0] + best_output = outputs[best_temp] + + return ( + f"Best AutoTemp Output (Temp {best_temp} | Score:" + f" {best_score}):\n{best_output}" + if self.auto_select + else "\n".join( + f"Temp {temp} | Score: {score}:\n{outputs[temp]}" + for temp, score in sorted_scores + ) + ) diff --git a/playground/demos/autotemp/blog_gen.py b/playground/demos/autotemp/blog_gen.py deleted file mode 100644 index e11a1521..00000000 --- a/playground/demos/autotemp/blog_gen.py +++ /dev/null @@ -1,138 +0,0 @@ -import os -from termcolor import colored -from swarms.models import OpenAIChat -from autotemp import AutoTemp -from swarms.structs import SequentialWorkflow - - -class BlogGen: - def __init__( - self, - api_key, - blog_topic, - temperature_range: str = "0.4,0.6,0.8,1.0,1.2", - ): # Add blog_topic as an argument - self.openai_chat = OpenAIChat( - openai_api_key=api_key, temperature=0.8 - ) - self.auto_temp = AutoTemp(api_key) - self.temperature_range = temperature_range - self.workflow = SequentialWorkflow(max_loops=5) - - # Formatting the topic selection prompt with the user's topic - self.TOPIC_SELECTION_SYSTEM_PROMPT = f""" - Given the topic '{blog_topic}', generate an engaging and versatile blog topic. This topic should cover areas related to '{blog_topic}' and might include aspects such as current events, lifestyle, technology, health, and culture related to '{blog_topic}'. Identify trending subjects within this realm. The topic must be unique, thought-provoking, and have the potential to draw in readers interested in '{blog_topic}'. - """ - - self.DRAFT_WRITER_SYSTEM_PROMPT = """ - Create an engaging and comprehensive blog article of at least 1,000 words on '{{CHOSEN_TOPIC}}'. The content should be original, informative, and reflective of a human-like style, with a clear structure including headings and sub-headings. Incorporate a blend of narrative, factual data, expert insights, and anecdotes to enrich the article. Focus on SEO optimization by using relevant keywords, ensuring readability, and including meta descriptions and title tags. The article should provide value, appeal to both knowledgeable and general readers, and maintain a balance between depth and accessibility. Aim to make the article engaging and suitable for online audiences. - """ - - self.REVIEW_AGENT_SYSTEM_PROMPT = """ - Critically review the drafted blog article on '{{ARTICLE_TOPIC}}' to refine it to high-quality content suitable for online publication. Ensure the article is coherent, factually accurate, engaging, and optimized for search engines (SEO). Check for the effective use of keywords, readability, internal and external links, and the inclusion of meta descriptions and title tags. Edit the content to enhance clarity, impact, and maintain the authors voice. The goal is to polish the article into a professional, error-free piece that resonates with the target audience, adheres to publication standards, and is optimized for both search engines and social media sharing. - """ - - self.DISTRIBUTION_AGENT_SYSTEM_PROMPT = """ - Develop an autonomous distribution strategy for the blog article on '{{ARTICLE_TOPIC}}'. Utilize an API to post the article on a popular blog platform (e.g., WordPress, Blogger, Medium) commonly used by our target audience. Ensure the post includes all SEO elements like meta descriptions, title tags, and properly formatted content. Craft unique, engaging social media posts tailored to different platforms to promote the blog article. Schedule these posts to optimize reach and engagement, using data-driven insights. Monitor the performance of the distribution efforts, adjusting strategies based on engagement metrics and audience feedback. Aim to maximize the article's visibility, attract a diverse audience, and foster engagement across digital channels. - """ - - def run_workflow(self): - try: - # Topic generation using OpenAIChat - topic_result = self.openai_chat.generate( - [self.TOPIC_SELECTION_SYSTEM_PROMPT] - ) - topic_output = topic_result.generations[0][0].text - print( - colored( - ( - "\nTopic Selection Task" - f" Output:\n----------------------------\n{topic_output}\n" - ), - "white", - ) - ) - - chosen_topic = topic_output.split("\n")[0] - print( - colored("Selected topic: " + chosen_topic, "yellow") - ) - - # Initial draft generation with AutoTemp - initial_draft_prompt = ( - self.DRAFT_WRITER_SYSTEM_PROMPT.replace( - "{{CHOSEN_TOPIC}}", chosen_topic - ) - ) - auto_temp_output = self.auto_temp.run( - initial_draft_prompt, self.temperature_range - ) - initial_draft_output = auto_temp_output # Assuming AutoTemp.run returns the best output directly - print( - colored( - ( - "\nInitial Draft" - f" Output:\n----------------------------\n{initial_draft_output}\n" - ), - "white", - ) - ) - - # Review process using OpenAIChat - review_prompt = self.REVIEW_AGENT_SYSTEM_PROMPT.replace( - "{{ARTICLE_TOPIC}}", chosen_topic - ) - review_result = self.openai_chat.generate([review_prompt]) - review_output = review_result.generations[0][0].text - print( - colored( - ( - "\nReview" - f" Output:\n----------------------------\n{review_output}\n" - ), - "white", - ) - ) - - # Distribution preparation using OpenAIChat - distribution_prompt = ( - self.DISTRIBUTION_AGENT_SYSTEM_PROMPT.replace( - "{{ARTICLE_TOPIC}}", chosen_topic - ) - ) - distribution_result = self.openai_chat.generate( - [distribution_prompt] - ) - distribution_output = distribution_result.generations[0][ - 0 - ].text - print( - colored( - ( - "\nDistribution" - f" Output:\n----------------------------\n{distribution_output}\n" - ), - "white", - ) - ) - - # Final compilation of the blog - final_blog_content = f"{initial_draft_output}\n\n{review_output}\n\n{distribution_output}" - print( - colored( - ( - "\nFinal Blog" - f" Content:\n----------------------------\n{final_blog_content}\n" - ), - "green", - ) - ) - - except Exception as e: - print(colored(f"An error occurred: {str(e)}", "red")) - - -if __name__ == "__main__": - api_key = os.environ["OPENAI_API_KEY"] - blog_generator = BlogGen(api_key) - blog_generator.run_workflow() diff --git a/playground/demos/autotemp/blog_gen_example.py b/playground/demos/autotemp/blog_gen_example.py index e7109b5a..e11a1521 100644 --- a/playground/demos/autotemp/blog_gen_example.py +++ b/playground/demos/autotemp/blog_gen_example.py @@ -1,25 +1,138 @@ import os -from blog_gen import BlogGen +from termcolor import colored +from swarms.models import OpenAIChat +from autotemp import AutoTemp +from swarms.structs import SequentialWorkflow -def main(): - api_key = os.getenv("OPENAI_API_KEY") - if not api_key: - raise ValueError( - "OPENAI_API_KEY environment variable not set." +class BlogGen: + def __init__( + self, + api_key, + blog_topic, + temperature_range: str = "0.4,0.6,0.8,1.0,1.2", + ): # Add blog_topic as an argument + self.openai_chat = OpenAIChat( + openai_api_key=api_key, temperature=0.8 ) + self.auto_temp = AutoTemp(api_key) + self.temperature_range = temperature_range + self.workflow = SequentialWorkflow(max_loops=5) - blog_topic = input("Enter the topic for the blog generation: ") + # Formatting the topic selection prompt with the user's topic + self.TOPIC_SELECTION_SYSTEM_PROMPT = f""" + Given the topic '{blog_topic}', generate an engaging and versatile blog topic. This topic should cover areas related to '{blog_topic}' and might include aspects such as current events, lifestyle, technology, health, and culture related to '{blog_topic}'. Identify trending subjects within this realm. The topic must be unique, thought-provoking, and have the potential to draw in readers interested in '{blog_topic}'. + """ - blog_generator = BlogGen(api_key, blog_topic) - blog_generator.TOPIC_SELECTION_SYSTEM_PROMPT = ( - blog_generator.TOPIC_SELECTION_SYSTEM_PROMPT.replace( - "{{BLOG_TOPIC}}", blog_topic - ) - ) + self.DRAFT_WRITER_SYSTEM_PROMPT = """ + Create an engaging and comprehensive blog article of at least 1,000 words on '{{CHOSEN_TOPIC}}'. The content should be original, informative, and reflective of a human-like style, with a clear structure including headings and sub-headings. Incorporate a blend of narrative, factual data, expert insights, and anecdotes to enrich the article. Focus on SEO optimization by using relevant keywords, ensuring readability, and including meta descriptions and title tags. The article should provide value, appeal to both knowledgeable and general readers, and maintain a balance between depth and accessibility. Aim to make the article engaging and suitable for online audiences. + """ - blog_generator.run_workflow() + self.REVIEW_AGENT_SYSTEM_PROMPT = """ + Critically review the drafted blog article on '{{ARTICLE_TOPIC}}' to refine it to high-quality content suitable for online publication. Ensure the article is coherent, factually accurate, engaging, and optimized for search engines (SEO). Check for the effective use of keywords, readability, internal and external links, and the inclusion of meta descriptions and title tags. Edit the content to enhance clarity, impact, and maintain the authors voice. The goal is to polish the article into a professional, error-free piece that resonates with the target audience, adheres to publication standards, and is optimized for both search engines and social media sharing. + """ + + self.DISTRIBUTION_AGENT_SYSTEM_PROMPT = """ + Develop an autonomous distribution strategy for the blog article on '{{ARTICLE_TOPIC}}'. Utilize an API to post the article on a popular blog platform (e.g., WordPress, Blogger, Medium) commonly used by our target audience. Ensure the post includes all SEO elements like meta descriptions, title tags, and properly formatted content. Craft unique, engaging social media posts tailored to different platforms to promote the blog article. Schedule these posts to optimize reach and engagement, using data-driven insights. Monitor the performance of the distribution efforts, adjusting strategies based on engagement metrics and audience feedback. Aim to maximize the article's visibility, attract a diverse audience, and foster engagement across digital channels. + """ + + def run_workflow(self): + try: + # Topic generation using OpenAIChat + topic_result = self.openai_chat.generate( + [self.TOPIC_SELECTION_SYSTEM_PROMPT] + ) + topic_output = topic_result.generations[0][0].text + print( + colored( + ( + "\nTopic Selection Task" + f" Output:\n----------------------------\n{topic_output}\n" + ), + "white", + ) + ) + + chosen_topic = topic_output.split("\n")[0] + print( + colored("Selected topic: " + chosen_topic, "yellow") + ) + + # Initial draft generation with AutoTemp + initial_draft_prompt = ( + self.DRAFT_WRITER_SYSTEM_PROMPT.replace( + "{{CHOSEN_TOPIC}}", chosen_topic + ) + ) + auto_temp_output = self.auto_temp.run( + initial_draft_prompt, self.temperature_range + ) + initial_draft_output = auto_temp_output # Assuming AutoTemp.run returns the best output directly + print( + colored( + ( + "\nInitial Draft" + f" Output:\n----------------------------\n{initial_draft_output}\n" + ), + "white", + ) + ) + + # Review process using OpenAIChat + review_prompt = self.REVIEW_AGENT_SYSTEM_PROMPT.replace( + "{{ARTICLE_TOPIC}}", chosen_topic + ) + review_result = self.openai_chat.generate([review_prompt]) + review_output = review_result.generations[0][0].text + print( + colored( + ( + "\nReview" + f" Output:\n----------------------------\n{review_output}\n" + ), + "white", + ) + ) + + # Distribution preparation using OpenAIChat + distribution_prompt = ( + self.DISTRIBUTION_AGENT_SYSTEM_PROMPT.replace( + "{{ARTICLE_TOPIC}}", chosen_topic + ) + ) + distribution_result = self.openai_chat.generate( + [distribution_prompt] + ) + distribution_output = distribution_result.generations[0][ + 0 + ].text + print( + colored( + ( + "\nDistribution" + f" Output:\n----------------------------\n{distribution_output}\n" + ), + "white", + ) + ) + + # Final compilation of the blog + final_blog_content = f"{initial_draft_output}\n\n{review_output}\n\n{distribution_output}" + print( + colored( + ( + "\nFinal Blog" + f" Content:\n----------------------------\n{final_blog_content}\n" + ), + "green", + ) + ) + + except Exception as e: + print(colored(f"An error occurred: {str(e)}", "red")) if __name__ == "__main__": - main() + api_key = os.environ["OPENAI_API_KEY"] + blog_generator = BlogGen(api_key) + blog_generator.run_workflow() diff --git a/playground/demos/design_team/ui_software_demo.py b/playground/demos/design_team/ui_software_demo_example.py similarity index 100% rename from playground/demos/design_team/ui_software_demo.py rename to playground/demos/design_team/ui_software_demo_example.py diff --git a/playground/demos/developer_swarm/main.py b/playground/demos/developer_swarm/main_example.py similarity index 100% rename from playground/demos/developer_swarm/main.py rename to playground/demos/developer_swarm/main_example.py diff --git a/playground/demos/education/education.py b/playground/demos/education/education_example.py similarity index 100% rename from playground/demos/education/education.py rename to playground/demos/education/education_example.py diff --git a/playground/demos/fof/langchain.py b/playground/demos/fof/langchain_example.py similarity index 100% rename from playground/demos/fof/langchain.py rename to playground/demos/fof/langchain_example.py diff --git a/playground/demos/gemini_benchmarking/gemini_chat.py b/playground/demos/gemini_benchmarking/gemini_chat_example.py similarity index 100% rename from playground/demos/gemini_benchmarking/gemini_chat.py rename to playground/demos/gemini_benchmarking/gemini_chat_example.py diff --git a/playground/demos/gemini_benchmarking/gemini_react.py b/playground/demos/gemini_benchmarking/gemini_react_example.py similarity index 100% rename from playground/demos/gemini_benchmarking/gemini_react.py rename to playground/demos/gemini_benchmarking/gemini_react_example.py diff --git a/playground/demos/gemini_benchmarking/gemini_vcot.py b/playground/demos/gemini_benchmarking/gemini_vcot_example.py similarity index 100% rename from playground/demos/gemini_benchmarking/gemini_vcot.py rename to playground/demos/gemini_benchmarking/gemini_vcot_example.py diff --git a/playground/demos/grupa/app.py b/playground/demos/grupa/app_example.py similarity index 100% rename from playground/demos/grupa/app.py rename to playground/demos/grupa/app_example.py diff --git a/playground/demos/jarvis_multi_modal_auto_agent/jarvis.py b/playground/demos/jarvis_multi_modal_auto_agent/jarvis_example.py similarity index 100% rename from playground/demos/jarvis_multi_modal_auto_agent/jarvis.py rename to playground/demos/jarvis_multi_modal_auto_agent/jarvis_example.py diff --git a/playground/demos/llm_with_conversation/main.py b/playground/demos/llm_with_conversation/main_example.py similarity index 100% rename from playground/demos/llm_with_conversation/main.py rename to playground/demos/llm_with_conversation/main_example.py diff --git a/playground/demos/logistics/logistics.py b/playground/demos/logistics/logistics_example.py similarity index 100% rename from playground/demos/logistics/logistics.py rename to playground/demos/logistics/logistics_example.py diff --git a/playground/demos/multi_modal_autonomous_agents/multi_modal_auto_agent.py b/playground/demos/multi_modal_autonomous_agents/multi_modal_auto_agent_example.py similarity index 100% rename from playground/demos/multi_modal_autonomous_agents/multi_modal_auto_agent.py rename to playground/demos/multi_modal_autonomous_agents/multi_modal_auto_agent_example.py diff --git a/playground/demos/multi_modal_chain_of_thought/vcot.py b/playground/demos/multi_modal_chain_of_thought/vcot_example.py similarity index 100% rename from playground/demos/multi_modal_chain_of_thought/vcot.py rename to playground/demos/multi_modal_chain_of_thought/vcot_example.py diff --git a/playground/demos/multimodal_tot/idea2img.py b/playground/demos/multimodal_tot/idea2img_example.py similarity index 100% rename from playground/demos/multimodal_tot/idea2img.py rename to playground/demos/multimodal_tot/idea2img_example.py diff --git a/playground/demos/multimodal_tot/main.py b/playground/demos/multimodal_tot/main_example.py similarity index 100% rename from playground/demos/multimodal_tot/main.py rename to playground/demos/multimodal_tot/main_example.py diff --git a/playground/demos/nutrition/nutrition.py b/playground/demos/nutrition/nutrition_example.py similarity index 100% rename from playground/demos/nutrition/nutrition.py rename to playground/demos/nutrition/nutrition_example.py diff --git a/playground/demos/optimize_llm_stack/vllm.py b/playground/demos/optimize_llm_stack/vllm_example.py similarity index 100% rename from playground/demos/optimize_llm_stack/vllm.py rename to playground/demos/optimize_llm_stack/vllm_example.py diff --git a/playground/demos/optimize_llm_stack/vortex.py b/playground/demos/optimize_llm_stack/vortex_example.py similarity index 100% rename from playground/demos/optimize_llm_stack/vortex.py rename to playground/demos/optimize_llm_stack/vortex_example.py diff --git a/playground/demos/optimize_llm_stack/weaviate.py b/playground/demos/optimize_llm_stack/weaviate_example.py similarity index 100% rename from playground/demos/optimize_llm_stack/weaviate.py rename to playground/demos/optimize_llm_stack/weaviate_example.py diff --git a/playground/demos/personal_assistant/better_communication.py b/playground/demos/personal_assistant/better_communication_example.py similarity index 100% rename from playground/demos/personal_assistant/better_communication.py rename to playground/demos/personal_assistant/better_communication_example.py diff --git a/playground/demos/personal_stylist/personal_stylist.py b/playground/demos/personal_stylist/personal_stylist_example.py similarity index 100% rename from playground/demos/personal_stylist/personal_stylist.py rename to playground/demos/personal_stylist/personal_stylist_example.py diff --git a/playground/demos/positive_med/positive_med.py b/playground/demos/positive_med/positive_med_example.py similarity index 100% rename from playground/demos/positive_med/positive_med.py rename to playground/demos/positive_med/positive_med_example.py diff --git a/playground/demos/security_team/security_team.py b/playground/demos/security_team/security_team_example.py similarity index 100% rename from playground/demos/security_team/security_team.py rename to playground/demos/security_team/security_team_example.py diff --git a/playground/demos/swarm_of_mma_manufacturing/main.py b/playground/demos/swarm_of_mma_manufacturing/main_example.py similarity index 100% rename from playground/demos/swarm_of_mma_manufacturing/main.py rename to playground/demos/swarm_of_mma_manufacturing/main_example.py diff --git a/playground/demos/urban_planning/urban_planning.py b/playground/demos/urban_planning/urban_planning_example.py similarity index 100% rename from playground/demos/urban_planning/urban_planning.py rename to playground/demos/urban_planning/urban_planning_example.py diff --git a/playground/demos/visuo/text_to_sql_agent.py b/playground/demos/visuo/text_to_sql_agent_example.py similarity index 100% rename from playground/demos/visuo/text_to_sql_agent.py rename to playground/demos/visuo/text_to_sql_agent_example.py diff --git a/playground/demos/xray/xray.py b/playground/demos/xray/xray_example.py similarity index 100% rename from playground/demos/xray/xray.py rename to playground/demos/xray/xray_example.py diff --git a/playground/DIY/hierchical.py b/playground/diy/hierchical_example.py similarity index 100% rename from playground/DIY/hierchical.py rename to playground/diy/hierchical_example.py diff --git a/playground/memory/chroma_usage.py b/playground/memory/chroma_usage_example.py similarity index 100% rename from playground/memory/chroma_usage.py rename to playground/memory/chroma_usage_example.py diff --git a/playground/memory/qdrant/usage.py b/playground/memory/qdrant/usage_example.py similarity index 100% rename from playground/memory/qdrant/usage.py rename to playground/memory/qdrant/usage_example.py diff --git a/playground/models/bingchat.py b/playground/models/bingchat_example.py similarity index 100% rename from playground/models/bingchat.py rename to playground/models/bingchat_example.py diff --git a/playground/models/bioclip.py b/playground/models/bioclip_example.py similarity index 100% rename from playground/models/bioclip.py rename to playground/models/bioclip_example.py diff --git a/playground/models/biogpt.py b/playground/models/biogpt_example.py similarity index 100% rename from playground/models/biogpt.py rename to playground/models/biogpt_example.py diff --git a/playground/models/dall3.py b/playground/models/dall3_example.py similarity index 100% rename from playground/models/dall3.py rename to playground/models/dall3_example.py diff --git a/playground/models/dalle3_concurrent.py b/playground/models/dalle3_concurrent_example.py similarity index 100% rename from playground/models/dalle3_concurrent.py rename to playground/models/dalle3_concurrent_example.py diff --git a/playground/models/dalle3.py b/playground/models/dalle3_example.py similarity index 100% rename from playground/models/dalle3.py rename to playground/models/dalle3_example.py diff --git a/playground/models/distilled_whiserpx.py b/playground/models/distilled_whiserpx_example.py similarity index 100% rename from playground/models/distilled_whiserpx.py rename to playground/models/distilled_whiserpx_example.py diff --git a/playground/models/fast_vit.py b/playground/models/fast_vit_example.py similarity index 100% rename from playground/models/fast_vit.py rename to playground/models/fast_vit_example.py diff --git a/playground/models/fuyu.py b/playground/models/fuyu.py deleted file mode 100644 index 537de25a..00000000 --- a/playground/models/fuyu.py +++ /dev/null @@ -1,7 +0,0 @@ -from swarms.models.fuyu import Fuyu - -fuyu = Fuyu() - -# This is the default image, you can change it to any image you want -out = fuyu("What is this image?", "images/swarms.jpeg") -print(out) diff --git a/playground/models/fuyu_example.py b/playground/models/fuyu_example.py index 612c002e..537de25a 100644 --- a/playground/models/fuyu_example.py +++ b/playground/models/fuyu_example.py @@ -1,7 +1,7 @@ from swarms.models.fuyu import Fuyu -img = "dalle3.jpeg" - fuyu = Fuyu() -fuyu("What is this image", img) +# This is the default image, you can change it to any image you want +out = fuyu("What is this image?", "images/swarms.jpeg") +print(out) diff --git a/playground/models/gemini.py b/playground/models/gemini_example.py similarity index 100% rename from playground/models/gemini.py rename to playground/models/gemini_example.py diff --git a/playground/models/gpt4_v.py b/playground/models/gpt4_v_example.py similarity index 100% rename from playground/models/gpt4_v.py rename to playground/models/gpt4_v_example.py diff --git a/playground/models/huggingface.py b/playground/models/huggingface_example.py similarity index 100% rename from playground/models/huggingface.py rename to playground/models/huggingface_example.py diff --git a/playground/models/idefics.py b/playground/models/idefics_example.py similarity index 100% rename from playground/models/idefics.py rename to playground/models/idefics_example.py diff --git a/playground/models/jina_embeds.py b/playground/models/jina_embeds_example.py similarity index 100% rename from playground/models/jina_embeds.py rename to playground/models/jina_embeds_example.py diff --git a/playground/models/kosmos2.py b/playground/models/kosmos2_example.py similarity index 100% rename from playground/models/kosmos2.py rename to playground/models/kosmos2_example.py diff --git a/playground/models/kosmos.py b/playground/models/kosmos_example.py similarity index 100% rename from playground/models/kosmos.py rename to playground/models/kosmos_example.py diff --git a/playground/models/kosmos_two.py b/playground/models/kosmos_two_example.py similarity index 100% rename from playground/models/kosmos_two.py rename to playground/models/kosmos_two_example.py diff --git a/playground/models/layout_documentxlm.py b/playground/models/layout_documentxlm_example.py similarity index 100% rename from playground/models/layout_documentxlm.py rename to playground/models/layout_documentxlm_example.py diff --git a/playground/models/llama_function_caller.py b/playground/models/llama_function_caller_example.py similarity index 100% rename from playground/models/llama_function_caller.py rename to playground/models/llama_function_caller_example.py diff --git a/playground/models/llava.py b/playground/models/llava_example.py similarity index 100% rename from playground/models/llava.py rename to playground/models/llava_example.py diff --git a/playground/models/mistral.py b/playground/models/mistral_example.py similarity index 100% rename from playground/models/mistral.py rename to playground/models/mistral_example.py diff --git a/playground/models/mpt.py b/playground/models/mpt_example.py similarity index 100% rename from playground/models/mpt.py rename to playground/models/mpt_example.py diff --git a/playground/models/nougat.py b/playground/models/nougat_example.py similarity index 100% rename from playground/models/nougat.py rename to playground/models/nougat_example.py diff --git a/playground/models/openai_model.py b/playground/models/openai_model_example.py similarity index 100% rename from playground/models/openai_model.py rename to playground/models/openai_model_example.py diff --git a/playground/models/palm.py b/playground/models/palm_example.py similarity index 100% rename from playground/models/palm.py rename to playground/models/palm_example.py diff --git a/playground/models/speecht5.py b/playground/models/speecht5_example.py similarity index 100% rename from playground/models/speecht5.py rename to playground/models/speecht5_example.py diff --git a/playground/models/ssd.py b/playground/models/ssd_example.py similarity index 100% rename from playground/models/ssd.py rename to playground/models/ssd_example.py diff --git a/playground/models/tocr.py b/playground/models/tocr_example.py similarity index 100% rename from playground/models/tocr.py rename to playground/models/tocr_example.py diff --git a/playground/models/together.py b/playground/models/together_example.py similarity index 100% rename from playground/models/together.py rename to playground/models/together_example.py diff --git a/playground/models/tts_speech.py b/playground/models/tts_speech_example.py similarity index 100% rename from playground/models/tts_speech.py rename to playground/models/tts_speech_example.py diff --git a/playground/models/vilt.py b/playground/models/vilt_example.py similarity index 100% rename from playground/models/vilt.py rename to playground/models/vilt_example.py diff --git a/playground/models/yi_200k.py b/playground/models/yi_200k_example.py similarity index 100% rename from playground/models/yi_200k.py rename to playground/models/yi_200k_example.py diff --git a/playground/structs/agent_with_tools.py b/playground/structs/agent_with_tools_example.py similarity index 100% rename from playground/structs/agent_with_tools.py rename to playground/structs/agent_with_tools_example.py diff --git a/playground/structs/autoscaler.py b/playground/structs/autoscaler_example.py similarity index 100% rename from playground/structs/autoscaler.py rename to playground/structs/autoscaler_example.py diff --git a/playground/structs/chat.py b/playground/structs/chat_example.py similarity index 100% rename from playground/structs/chat.py rename to playground/structs/chat_example.py diff --git a/playground/structs/concurrent_workflow.py b/playground/structs/concurrent_workflow_example.py similarity index 100% rename from playground/structs/concurrent_workflow.py rename to playground/structs/concurrent_workflow_example.py diff --git a/playground/structs/debate.py b/playground/structs/debate_example.py similarity index 100% rename from playground/structs/debate.py rename to playground/structs/debate_example.py diff --git a/playground/structs/dialogue_simulator.py b/playground/structs/dialogue_simulator_example.py similarity index 100% rename from playground/structs/dialogue_simulator.py rename to playground/structs/dialogue_simulator_example.py diff --git a/playground/structs/flow.py b/playground/structs/flow_example.py similarity index 100% rename from playground/structs/flow.py rename to playground/structs/flow_example.py diff --git a/playground/structs/fuyu_flow.py b/playground/structs/fuyu_flow_example.py similarity index 100% rename from playground/structs/fuyu_flow.py rename to playground/structs/fuyu_flow_example.py diff --git a/playground/structs/godmode.py b/playground/structs/godmode_example.py similarity index 100% rename from playground/structs/godmode.py rename to playground/structs/godmode_example.py diff --git a/playground/structs/groupchat.py b/playground/structs/groupchat_example.py similarity index 100% rename from playground/structs/groupchat.py rename to playground/structs/groupchat_example.py diff --git a/playground/structs/gui_app.py b/playground/structs/gui_app_example.py similarity index 100% rename from playground/structs/gui_app.py rename to playground/structs/gui_app_example.py diff --git a/playground/structs/multi_agent_collab.py b/playground/structs/multi_agent_collab_example.py similarity index 100% rename from playground/structs/multi_agent_collab.py rename to playground/structs/multi_agent_collab_example.py diff --git a/playground/structs/multi_agent_debate.py b/playground/structs/multi_agent_debate_example.py similarity index 100% rename from playground/structs/multi_agent_debate.py rename to playground/structs/multi_agent_debate_example.py diff --git a/playground/structs/multi_modal_flow.py b/playground/structs/multi_modal_flow_example.py similarity index 100% rename from playground/structs/multi_modal_flow.py rename to playground/structs/multi_modal_flow_example.py diff --git a/playground/structs/nonlinear_worfklow.py b/playground/structs/nonlinear_worfklow_example.py similarity index 100% rename from playground/structs/nonlinear_worfklow.py rename to playground/structs/nonlinear_worfklow_example.py diff --git a/playground/structs/orchestrate.py b/playground/structs/orchestrate_example.py similarity index 100% rename from playground/structs/orchestrate.py rename to playground/structs/orchestrate_example.py diff --git a/playground/structs/orchestrator.py b/playground/structs/orchestrator_example.py similarity index 100% rename from playground/structs/orchestrator.py rename to playground/structs/orchestrator_example.py diff --git a/playground/structs/sequential_workflow.py b/playground/structs/sequential_workflow.py deleted file mode 100644 index 7fa110bc..00000000 --- a/playground/structs/sequential_workflow.py +++ /dev/null @@ -1,48 +0,0 @@ -from swarms import OpenAIChat, Agent, Task, SequentialWorkflow - -# Example usage -llm = OpenAIChat( - temperature=0.5, - max_tokens=3000, -) - -# Initialize the Agent with the language agent -agent1 = Agent( - agent_name="John the writer", - llm=llm, - max_loops=0, - dashboard=False, -) -task1 = Task( - agent=agent1, - description="Write a 1000 word blog about the future of AI", -) - -# Create another Agent for a different task -agent2 = Agent("Summarizer", llm=llm, max_loops=1, dashboard=False) -task2 = Task( - agent=agent2, - description="Summarize the generated blog", -) - -# Create the workflow -workflow = SequentialWorkflow( - name="Blog Generation Workflow", - description=( - "A workflow to generate and summarize a blog about the future" - " of AI" - ), - max_loops=1, - autosave=True, - dashboard=False, -) - -# Add tasks to the workflow -workflow.add(tasks=[task1, task2]) - -# Run the workflow -workflow.run() - -# # Output the results -# for task in workflow.tasks: -# print(f"Task: {task.description}, Result: {task.result}") diff --git a/playground/structs/sequential_workflow_example.py b/playground/structs/sequential_workflow_example.py index c36419c1..7fa110bc 100644 --- a/playground/structs/sequential_workflow_example.py +++ b/playground/structs/sequential_workflow_example.py @@ -1,52 +1,48 @@ -import os -from swarms.models import OpenAIChat -from swarms.structs import Agent -from swarms.structs.sequential_workflow import SequentialWorkflow -from dotenv import load_dotenv +from swarms import OpenAIChat, Agent, Task, SequentialWorkflow -load_dotenv() - -# Load the environment variables -api_key = os.getenv("OPENAI_API_KEY") - - -# Initialize the language agent -# Initialize the language model +# Example usage llm = OpenAIChat( temperature=0.5, - model_name="gpt-4", - openai_api_key=api_key, - max_tokens=4000, + max_tokens=3000, ) - -# Initialize the agent with the language agent +# Initialize the Agent with the language agent agent1 = Agent( + agent_name="John the writer", llm=llm, - max_loops=1, + max_loops=0, + dashboard=False, +) +task1 = Task( + agent=agent1, + description="Write a 1000 word blog about the future of AI", ) -# Create another agent for a different task -agent2 = Agent(llm=llm, max_loops=1) +# Create another Agent for a different task +agent2 = Agent("Summarizer", llm=llm, max_loops=1, dashboard=False) +task2 = Task( + agent=agent2, + description="Summarize the generated blog", +) # Create the workflow -workflow = SequentialWorkflow(max_loops=1) - -# Add tasks to the workflow -workflow.add( - agent1, - "Generate a 10,000 word blog on health and wellness.", +workflow = SequentialWorkflow( + name="Blog Generation Workflow", + description=( + "A workflow to generate and summarize a blog about the future" + " of AI" + ), + max_loops=1, + autosave=True, + dashboard=False, ) -# Suppose the next task takes the output of the first task as input -workflow.add( - agent2, - "Summarize the generated blog", -) +# Add tasks to the workflow +workflow.add(tasks=[task1, task2]) # Run the workflow workflow.run() -# Output the results -for task in workflow.tasks: - print(f"Task: {task.description}, Result: {task.result}") +# # Output the results +# for task in workflow.tasks: +# print(f"Task: {task.description}, Result: {task.result}") diff --git a/playground/structs/social_app.py b/playground/structs/social_app_example.py similarity index 100% rename from playground/structs/social_app.py rename to playground/structs/social_app_example.py diff --git a/playground/structs/swarm_network.py b/playground/structs/swarm_network_example.py similarity index 100% rename from playground/structs/swarm_network.py rename to playground/structs/swarm_network_example.py diff --git a/playground/structs/task.py b/playground/structs/task_example.py similarity index 100% rename from playground/structs/task.py rename to playground/structs/task_example.py diff --git a/playground/structs/todo_app.py b/playground/structs/todo_app_example.py similarity index 100% rename from playground/structs/todo_app.py rename to playground/structs/todo_app_example.py diff --git a/playground/structs/tool_utils.py b/playground/structs/tool_utils_example.py similarity index 100% rename from playground/structs/tool_utils.py rename to playground/structs/tool_utils_example.py diff --git a/playground/structs/workflow.py b/playground/structs/workflow_example.py similarity index 100% rename from playground/structs/workflow.py rename to playground/structs/workflow_example.py diff --git a/playground/tools/agent_with_tools.py b/playground/tools/agent_with_tools_example.py similarity index 100% rename from playground/tools/agent_with_tools.py rename to playground/tools/agent_with_tools_example.py diff --git a/playground/tools/tool_prompt_scaper.py b/playground/tools/tool_prompt_scaper_example.py similarity index 100% rename from playground/tools/tool_prompt_scaper.py rename to playground/tools/tool_prompt_scaper_example.py diff --git a/pyproject.toml b/pyproject.toml index fe88c5d6..bd198d14 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.9.5" +version = "3.9.6" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/scripts/playground_to_examples.sh b/scripts/playground_to_examples.sh index c1ed0f61..a0ed5a96 100755 --- a/scripts/playground_to_examples.sh +++ b/scripts/playground_to_examples.sh @@ -1,13 +1,33 @@ #!/bin/bash -# Change to the root directory -cd / +# Define the directory to search +dir="playground" -# Iterate over all the .py files in the directory -for file in *.py; do - # Get the base name of the file without the .py - base_name=$(basename "$file" .py) +# Check if the directory exists +if [ -d "$dir" ] +then + # Use find to locate all .py files in the directory and its subdirectories + for file in $(find $dir -name "*.py") + do + # Extract the file name and directory + base=$(basename $file .py) + dir=$(dirname $file) - # Rename the file to remove .py from the end - mv "$file" "${base_name}" -done \ No newline at end of file + # Check if the file name already contains _example + if [[ $base == *_example ]] + then + echo "Skipping $file as it already contains _example" + continue + fi + + # Append _example to the file name + newname="${base}_example.py" + + # Rename the file + mv $file $dir/$newname + + echo "Renamed $file to $dir/$newname" + done +else + echo "Directory $dir does not exist." +fi \ No newline at end of file From f840483d359cb5fdcd24ad3ccfd439a4ddb22288 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 18:35:36 -0500 Subject: [PATCH 082/103] [V][3.9.7] --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index bd198d14..56eee947 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.9.6" +version = "3.9.7" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] From 70c2a8515f0057796b3ae261a5be72652fb62bdb Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:02:10 -0500 Subject: [PATCH 083/103] [V][3.9.9] --- pyproject.toml | 3 ++- requirements.txt | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 56eee947..2aabfa84 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.9.7" +version = "3.9.9" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] @@ -41,6 +41,7 @@ optimum = "1.15.0" diffusers = "*" PyPDF2 = "3.0.1" accelerate = "*" +anthropic = "*" sentencepiece = "0.1.98" httpx = "0.24.1" tiktoken = "0.4.0" diff --git a/requirements.txt b/requirements.txt index 32184962..c7001cfb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -50,5 +50,6 @@ psutil ultralytics supervision scikit-image +anthropic = "*" pinecone-client roboflow \ No newline at end of file From 42049f44111671d93f5b574b692544baa7e210fa Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:11:03 -0500 Subject: [PATCH 084/103] [REFACTOR][ToolAgent] --- pyproject.toml | 2 +- swarms/agents/tool_agent.py | 16 ++++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2aabfa84..47732bf6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "3.9.9" +version = "4.0.0" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/agents/tool_agent.py b/swarms/agents/tool_agent.py index b2a22ba0..d9359471 100644 --- a/swarms/agents/tool_agent.py +++ b/swarms/agents/tool_agent.py @@ -1,6 +1,7 @@ -from swarms.tools.format_tools import Jsonformer from typing import Any + from swarms.models.base_llm import AbstractLLM +from swarms.tools.format_tools import Jsonformer class ToolAgent(AbstractLLM): @@ -65,15 +66,17 @@ class ToolAgent(AbstractLLM): model: Any, tokenizer: Any, json_schema: Any, + max_number_tokens: int = 500, *args, **kwargs, ): - super().__init__() + super().__init__(*args, **kwargs) self.name = name self.description = description self.model = model self.tokenizer = tokenizer self.json_schema = json_schema + self.max_number_tokens = max_number_tokens def run(self, task: str, *args, **kwargs): """ @@ -92,10 +95,11 @@ class ToolAgent(AbstractLLM): """ try: self.toolagent = Jsonformer( - self.model, - self.tokenizer, - self.json_schema, - task, + model=self.model, + tokenizer=self.tokenizer, + json_schema=self.json_schema, + prompt=task, + max_number_tokens=self.max_number_tokens, *args, **kwargs, ) From d97654a45d9146f6d2755d0de79779fa99a142f8 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:13:23 -0500 Subject: [PATCH 085/103] requirements] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index c7001cfb..e2cdf386 100644 --- a/requirements.txt +++ b/requirements.txt @@ -50,6 +50,6 @@ psutil ultralytics supervision scikit-image -anthropic = "*" +anthropic pinecone-client roboflow \ No newline at end of file From 20e263cb40a16919220bc7184597873006d9d7d5 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:15:15 -0500 Subject: [PATCH 086/103] requirements --- requirements.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index e2cdf386..5a9d380c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,9 +3,7 @@ transformers pandas==1.5.3 langchain==0.0.333 langchain-experimental==0.0.10 -playwright==1.34.0 httpx==0.24.1 -open_clip_torch==2.20.0 Pillow==9.4.0 faiss-cpu==1.7.4 openai==0.28.0 From 8b3b2fde1e5b0a63a7a7af3dc81a5314114db7c8 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:18:33 -0500 Subject: [PATCH 087/103] [EXAMPLE][ToolAgent] --- README.md | 18 +++++++++++------ playground/agents/tool_agent.py | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 playground/agents/tool_agent.py diff --git a/README.md b/README.md index 52fc6a04..1b22bc10 100644 --- a/README.md +++ b/README.md @@ -83,32 +83,38 @@ ToolAgent is an agent that outputs JSON using any model from huggingface. It tak ```python +# Import necessary libraries from transformers import AutoModelForCausalLM, AutoTokenizer from swarms import ToolAgent - +# Load the pre-trained model and tokenizer model = AutoModelForCausalLM.from_pretrained("databricks/dolly-v2-12b") tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-12b") +# Define a JSON schema for person's information json_schema = { "type": "object", "properties": { "name": {"type": "string"}, "age": {"type": "number"}, "is_student": {"type": "boolean"}, - "courses": { - "type": "array", - "items": {"type": "string"} - } - } + "courses": {"type": "array", "items": {"type": "string"}}, + }, } +# Define the task to generate a person's information task = "Generate a person's information based on the following schema:" + +# Create an instance of the ToolAgent class agent = ToolAgent(model=model, tokenizer=tokenizer, json_schema=json_schema) + +# Run the agent to generate the person's information generated_data = agent.run(task) +# Print the generated data print(generated_data) + ``` diff --git a/playground/agents/tool_agent.py b/playground/agents/tool_agent.py new file mode 100644 index 00000000..93e07ff3 --- /dev/null +++ b/playground/agents/tool_agent.py @@ -0,0 +1,36 @@ +# Import necessary libraries +from transformers import AutoModelForCausalLM, AutoTokenizer +from swarms import ToolAgent + +# Load the pre-trained model and tokenizer +model = AutoModelForCausalLM.from_pretrained( + "databricks/dolly-v2-12b" +) +tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-12b") + +# Define a JSON schema for person's information +json_schema = { + "type": "object", + "properties": { + "name": {"type": "string"}, + "age": {"type": "number"}, + "is_student": {"type": "boolean"}, + "courses": {"type": "array", "items": {"type": "string"}}, + }, +} + +# Define the task to generate a person's information +task = ( + "Generate a person's information based on the following schema:" +) + +# Create an instance of the ToolAgent class +agent = ToolAgent( + model=model, tokenizer=tokenizer, json_schema=json_schema +) + +# Run the agent to generate the person's information +generated_data = agent.run(task) + +# Print the generated data +print(generated_data) From 3e692ed2544584054b097e53f5202eac18fcf688 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:19:59 -0500 Subject: [PATCH 088/103] [REQUIREMENT][tool] --- pyproject.toml | 1 + requirements.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index 47732bf6..589bce46 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,6 +39,7 @@ backoff = "2.2.1" datasets = "*" optimum = "1.15.0" diffusers = "*" +toml = "*" PyPDF2 = "3.0.1" accelerate = "*" anthropic = "*" diff --git a/requirements.txt b/requirements.txt index 5a9d380c..00c92023 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,6 +19,7 @@ accelerate==0.22.0 chromadb==0.4.14 tensorflow optimum +toml tiktoken==0.4.0 colored addict From 2007c5f51d16b7bd8bf6dc1e1ec8f6d96eea28cf Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:22:16 -0500 Subject: [PATCH 089/103] [V][4.0.1] --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 589bce46..c9f0c28d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "4.0.0" +version = "4.0.1" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] From fde09d6bd306c2b07652fc368de5da179fd3f456 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:31:48 -0500 Subject: [PATCH 090/103] [EXAMPLE][Tool Agent] --- playground/agents/tool_agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playground/agents/tool_agent.py b/playground/agents/tool_agent.py index 93e07ff3..91b576a4 100644 --- a/playground/agents/tool_agent.py +++ b/playground/agents/tool_agent.py @@ -33,4 +33,4 @@ agent = ToolAgent( generated_data = agent.run(task) # Print the generated data -print(generated_data) +print(f"Generated data: {generated_data}") From 5569a5daf8ecc0d5b51d715f518da3b1b5a4c68b Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:33:58 -0500 Subject: [PATCH 091/103] [BUFG][ToolAgent] --- playground/agents/tool_agent.py | 2 +- swarms/agents/tool_agent.py | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/playground/agents/tool_agent.py b/playground/agents/tool_agent.py index 91b576a4..3cd93fbd 100644 --- a/playground/agents/tool_agent.py +++ b/playground/agents/tool_agent.py @@ -26,7 +26,7 @@ task = ( # Create an instance of the ToolAgent class agent = ToolAgent( - model=model, tokenizer=tokenizer, json_schema=json_schema + name="dolly-function-agent", description="Ana gent to create a child data", model=model, tokenizer=tokenizer, json_schema=json_schema ) # Run the agent to generate the person's information diff --git a/swarms/agents/tool_agent.py b/swarms/agents/tool_agent.py index d9359471..f6d9f6e8 100644 --- a/swarms/agents/tool_agent.py +++ b/swarms/agents/tool_agent.py @@ -61,11 +61,11 @@ class ToolAgent(AbstractLLM): def __init__( self, - name: str, - description: str, - model: Any, - tokenizer: Any, - json_schema: Any, + name: str = None, + description: str = None, + model: Any = None, + tokenizer: Any = None, + json_schema: Any = None, max_number_tokens: int = 500, *args, **kwargs, From 18b8a5d602d1249d141716ef550bff64bde8ae98 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:40:37 -0500 Subject: [PATCH 092/103] [REFACTOR][ToolAgent][name and description] --- playground/agents/tool_agent.py | 6 +++++- swarms/agents/tool_agent.py | 4 ++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/playground/agents/tool_agent.py b/playground/agents/tool_agent.py index 3cd93fbd..71d10bb8 100644 --- a/playground/agents/tool_agent.py +++ b/playground/agents/tool_agent.py @@ -26,7 +26,11 @@ task = ( # Create an instance of the ToolAgent class agent = ToolAgent( - name="dolly-function-agent", description="Ana gent to create a child data", model=model, tokenizer=tokenizer, json_schema=json_schema + name="dolly-function-agent", + description="Ana gent to create a child data", + model=model, + tokenizer=tokenizer, + json_schema=json_schema, ) # Run the agent to generate the person's information diff --git a/swarms/agents/tool_agent.py b/swarms/agents/tool_agent.py index f6d9f6e8..8e6adf9d 100644 --- a/swarms/agents/tool_agent.py +++ b/swarms/agents/tool_agent.py @@ -61,8 +61,8 @@ class ToolAgent(AbstractLLM): def __init__( self, - name: str = None, - description: str = None, + name: str = "Function Calling Agent", + description: str = "Generates a function based on the input json schema and the task", model: Any = None, tokenizer: Any = None, json_schema: Any = None, From 537f81f93272cd6e3b57ffb72e7b0b083807efde Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:46:09 -0500 Subject: [PATCH 093/103] [REFACTOR][ToolAgent] --- playground/agents/tool_agent.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/playground/agents/tool_agent.py b/playground/agents/tool_agent.py index 71d10bb8..0a95f42c 100644 --- a/playground/agents/tool_agent.py +++ b/playground/agents/tool_agent.py @@ -4,7 +4,9 @@ from swarms import ToolAgent # Load the pre-trained model and tokenizer model = AutoModelForCausalLM.from_pretrained( - "databricks/dolly-v2-12b" + "databricks/dolly-v2-12b", + load_in_4bit=True, + device_map="auto", ) tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-12b") From a552a983730c98b2b7d9da386ea4dc7053166657 Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 31 Jan 2024 21:59:54 -0500 Subject: [PATCH 094/103] [EXAMPLES][Cleanup][Kosmos] --- playground/models/kosmos2_example.py | 10 ---------- playground/models/kosmos_two_example.py | 11 ----------- pyproject.toml | 2 +- 3 files changed, 1 insertion(+), 22 deletions(-) delete mode 100644 playground/models/kosmos2_example.py delete mode 100644 playground/models/kosmos_two_example.py diff --git a/playground/models/kosmos2_example.py b/playground/models/kosmos2_example.py deleted file mode 100644 index 6fc4df02..00000000 --- a/playground/models/kosmos2_example.py +++ /dev/null @@ -1,10 +0,0 @@ -from swarms.models.kosmos2 import Kosmos2 -from PIL import Image - - -model = Kosmos2.initialize() - -image = Image.open("images/swarms.jpg") - -detections = model(image) -print(detections) diff --git a/playground/models/kosmos_two_example.py b/playground/models/kosmos_two_example.py deleted file mode 100644 index 8bf583cd..00000000 --- a/playground/models/kosmos_two_example.py +++ /dev/null @@ -1,11 +0,0 @@ -from swarms.models.kosmos_two import Kosmos - -# Initialize Kosmos -kosmos = Kosmos() - -# Perform multimodal grounding -out = kosmos.multimodal_grounding( - "Find the red apple in the image.", "images/swarms.jpeg" -) - -print(out) diff --git a/pyproject.toml b/pyproject.toml index c9f0c28d..f4971b47 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "4.0.1" +version = "4.0.2" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] From 7e50f19f3fdd6bb9479d02254d85ecf74dd32552 Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 2 Feb 2024 15:46:58 -0800 Subject: [PATCH 095/103] [CLEANUP][Old tests with modelscope and vllm] --- swarms/structs/__init__.py | 7 ++ tests/models/test_cogagent.py | 71 ----------- tests/models/test_elevenlab.py | 1 - tests/models/test_gpt4_vision_api.py | 1 - tests/models/test_modeelscope_pipeline.py | 39 ------ tests/models/test_modelscope_llm.py | 58 --------- tests/models/test_vllm.py | 141 ---------------------- tests/structs/test_multi_agent_collab.py | 1 - tests/structs/test_swarmnetwork.py | 1 - 9 files changed, 7 insertions(+), 313 deletions(-) delete mode 100644 tests/models/test_cogagent.py delete mode 100644 tests/models/test_modeelscope_pipeline.py delete mode 100644 tests/models/test_modelscope_llm.py delete mode 100644 tests/models/test_vllm.py diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py index 11e9e523..15b9ef1e 100644 --- a/swarms/structs/__init__.py +++ b/swarms/structs/__init__.py @@ -17,8 +17,12 @@ from swarms.structs.recursive_workflow import RecursiveWorkflow from swarms.structs.schemas import ( Artifact, ArtifactUpload, + Step, StepInput, + StepOutput, + StepRequestBody, TaskInput, + TaskRequestBody, ) from swarms.structs.sequential_workflow import SequentialWorkflow from swarms.structs.step import Step @@ -105,4 +109,7 @@ __all__ = [ "sigmoid_swarm", "staircase_swarm", "star_swarm", + "StepOutput", + "StepRequestBody", + "TaskRequestBody", ] diff --git a/tests/models/test_cogagent.py b/tests/models/test_cogagent.py deleted file mode 100644 index ac7fec56..00000000 --- a/tests/models/test_cogagent.py +++ /dev/null @@ -1,71 +0,0 @@ -import pytest -from swarms.models.cog_agent import CogAgent -from unittest.mock import MagicMock -from PIL import Image - - -@pytest.fixture -def cogagent_params(): - return { - "model_name": "ZhipuAI/cogagent-chat", - "tokenizer_name": "I-ModelScope/vicuna-7b-v1.5", - "dtype": "torch.bfloat16", - "low_cpu_mem_usage": True, - "load_in_4bit": True, - "trust_remote_code": True, - "device": "cuda", - } - - -@pytest.fixture -def cogagent(cogagent_params): - return CogAgent(**cogagent_params) - - -def test_init(mocker, cogagent_params, cogagent): - mock_model = mocker.patch( - "swarms.models.cog_agent.AutoModelForCausalLM.from_pretrained" - ) - mock_tokenizer = mocker.patch( - "swarms.models.cog_agent.AutoTokenizer.from_pretrained" - ) - - for param, value in cogagent_params.items(): - assert getattr(cogagent, param) == value - - mock_tokenizer.assert_called_once_with( - cogagent_params["tokenizer_name"] - ) - mock_model.assert_called_once_with( - cogagent_params["model_name"], - torch_dtype=cogagent_params["dtype"], - low_cpu_mem_usage=cogagent_params["low_cpu_mem_usage"], - load_in_4bit=cogagent_params["load_in_4bit"], - trust_remote_code=cogagent_params["trust_remote_code"], - ) - - -def test_run(mocker, cogagent): - task = "How are you?" - img = "images/1.jpg" - mock_image = mocker.patch( - "PIL.Image.open", return_value=MagicMock(spec=Image.Image) - ) - cogagent.model.build_conversation_input_ids = MagicMock( - return_value={ - "input_ids": MagicMock(), - "token_type_ids": MagicMock(), - "attention_mask": MagicMock(), - "images": [MagicMock()], - } - ) - cogagent.model.__call__ = MagicMock(return_value="Mocked output") - cogagent.decode = MagicMock(return_value="Mocked response") - - output = cogagent.run(task, img) - - assert output is not None - mock_image.assert_called_once_with(img) - cogagent.model.build_conversation_input_ids.assert_called_once() - cogagent.model.__call__.assert_called_once() - cogagent.decode.assert_called_once() diff --git a/tests/models/test_elevenlab.py b/tests/models/test_elevenlab.py index b28ecb31..0ba975ca 100644 --- a/tests/models/test_elevenlab.py +++ b/tests/models/test_elevenlab.py @@ -9,7 +9,6 @@ from dotenv import load_dotenv load_dotenv() - # Define some test data SAMPLE_TEXT = "Hello, this is a test." API_KEY = os.environ.get("ELEVEN_API_KEY") diff --git a/tests/models/test_gpt4_vision_api.py b/tests/models/test_gpt4_vision_api.py index c7758a36..26f60960 100644 --- a/tests/models/test_gpt4_vision_api.py +++ b/tests/models/test_gpt4_vision_api.py @@ -10,7 +10,6 @@ from swarms.models.gpt4_vision_api import GPT4VisionAPI load_dotenv() - custom_api_key = os.environ.get("OPENAI_API_KEY") img = "images/swarms.jpeg" diff --git a/tests/models/test_modeelscope_pipeline.py b/tests/models/test_modeelscope_pipeline.py deleted file mode 100644 index 16da0155..00000000 --- a/tests/models/test_modeelscope_pipeline.py +++ /dev/null @@ -1,39 +0,0 @@ -import pytest -from swarms.models.modelscope_pipeline import ModelScopePipeline -from unittest.mock import MagicMock - - -@pytest.fixture -def pipeline_params(): - return { - "type_task": "text-generation", - "model_name": "gpt2", - } - - -@pytest.fixture -def pipeline_model(pipeline_params): - return ModelScopePipeline(**pipeline_params) - - -def test_init(mocker, pipeline_params, pipeline_model): - mock_pipeline = mocker.patch( - "swarms.models.modelscope_pipeline.pipeline" - ) - - for param, value in pipeline_params.items(): - assert getattr(pipeline_model, param) == value - - mock_pipeline.assert_called_once_with( - pipeline_params["type_task"], - model=pipeline_params["model_name"], - ) - - -def test_run(mocker, pipeline_model): - task = "Generate a 10,000 word blog on health and wellness." - pipeline_model.model = MagicMock(return_value="Mocked output") - - output = pipeline_model.run(task) - - assert output is not None diff --git a/tests/models/test_modelscope_llm.py b/tests/models/test_modelscope_llm.py deleted file mode 100644 index 7e9310b2..00000000 --- a/tests/models/test_modelscope_llm.py +++ /dev/null @@ -1,58 +0,0 @@ -import pytest -from swarms.models.modelscope_llm import ModelScopeAutoModel -from unittest.mock import MagicMock - - -@pytest.fixture -def model_params(): - return { - "model_name": "gpt2", - "tokenizer_name": None, - "device": "cuda", - "device_map": "auto", - "max_new_tokens": 500, - "skip_special_tokens": True, - } - - -@pytest.fixture -def modelscope(model_params): - return ModelScopeAutoModel(**model_params) - - -def test_init(mocker, model_params, modelscope): - mock_model = mocker.patch( - "swarms.models.modelscope_llm.AutoModelForCausalLM.from_pretrained" - ) - mock_tokenizer = mocker.patch( - "swarms.models.modelscope_llm.AutoTokenizer.from_pretrained" - ) - - for param, value in model_params.items(): - assert getattr(modelscope, param) == value - - mock_tokenizer.assert_called_once_with( - model_params["tokenizer_name"] - ) - mock_model.assert_called_once_with( - model_params["model_name"], - device_map=model_params["device_map"], - ) - - -def test_run(mocker, modelscope): - task = "Generate a 10,000 word blog on health and wellness." - mocker.patch( - "swarms.models.modelscope_llm.AutoTokenizer.decode", - return_value="Mocked output", - ) - modelscope.model.generate = MagicMock( - return_value=["Mocked token"] - ) - modelscope.tokenizer = MagicMock( - return_value={"input_ids": "Mocked input_ids"} - ) - - output = modelscope.run(task) - - assert output is not None diff --git a/tests/models/test_vllm.py b/tests/models/test_vllm.py deleted file mode 100644 index 6eec8f27..00000000 --- a/tests/models/test_vllm.py +++ /dev/null @@ -1,141 +0,0 @@ -import pytest -from swarms.models.vllm import vLLM - - -# Fixture for initializing vLLM -@pytest.fixture -def vllm_instance(): - return vLLM() - - -# Test the default initialization of vLLM -def test_vllm_default_init(vllm_instance): - assert isinstance(vllm_instance, vLLM) - assert vllm_instance.model_name == "facebook/opt-13b" - assert vllm_instance.tensor_parallel_size == 4 - assert not vllm_instance.trust_remote_code - assert vllm_instance.revision is None - assert vllm_instance.temperature == 0.5 - assert vllm_instance.top_p == 0.95 - - -# Test custom initialization of vLLM -def test_vllm_custom_init(): - vllm_instance = vLLM( - model_name="custom_model", - tensor_parallel_size=8, - trust_remote_code=True, - revision="123", - temperature=0.7, - top_p=0.9, - ) - assert isinstance(vllm_instance, vLLM) - assert vllm_instance.model_name == "custom_model" - assert vllm_instance.tensor_parallel_size == 8 - assert vllm_instance.trust_remote_code - assert vllm_instance.revision == "123" - assert vllm_instance.temperature == 0.7 - assert vllm_instance.top_p == 0.9 - - -# Test the run method of vLLM -def test_vllm_run(vllm_instance): - task = "Hello, vLLM!" - result = vllm_instance.run(task) - assert isinstance(result, str) - assert len(result) > 0 - - -# Test run method with different temperature and top_p values -@pytest.mark.parametrize( - "temperature, top_p", [(0.2, 0.8), (0.8, 0.2)] -) -def test_vllm_run_with_params(vllm_instance, temperature, top_p): - task = "Temperature and Top-P Test" - result = vllm_instance.run( - task, temperature=temperature, top_p=top_p - ) - assert isinstance(result, str) - assert len(result) > 0 - - -# Test run method with a specific model revision -def test_vllm_run_with_revision(vllm_instance): - task = "Specific Model Revision Test" - result = vllm_instance.run(task, revision="abc123") - assert isinstance(result, str) - assert len(result) > 0 - - -# Test run method with a specific model name -def test_vllm_run_with_custom_model(vllm_instance): - task = "Custom Model Test" - custom_model_name = "my_custom_model" - result = vllm_instance.run(task, model_name=custom_model_name) - assert isinstance(result, str) - assert len(result) > 0 - assert vllm_instance.model_name == custom_model_name - - -# Test run method with invalid task input -def test_vllm_run_invalid_task(vllm_instance): - invalid_task = None - with pytest.raises(ValueError): - vllm_instance.run(invalid_task) - - -# Test run method with a very high temperature value -def test_vllm_run_high_temperature(vllm_instance): - task = "High Temperature Test" - high_temperature = 10.0 - result = vllm_instance.run(task, temperature=high_temperature) - assert isinstance(result, str) - assert len(result) > 0 - - -# Test run method with a very low top_p value -def test_vllm_run_low_top_p(vllm_instance): - task = "Low Top-P Test" - low_top_p = 0.01 - result = vllm_instance.run(task, top_p=low_top_p) - assert isinstance(result, str) - assert len(result) > 0 - - -# Test run method with an empty task -def test_vllm_run_empty_task(vllm_instance): - empty_task = "" - result = vllm_instance.run(empty_task) - assert isinstance(result, str) - assert len(result) == 0 - - -# Test initialization with invalid parameters -def test_vllm_invalid_init(): - with pytest.raises(ValueError): - vLLM( - model_name=None, - tensor_parallel_size=-1, - trust_remote_code="invalid", - revision=123, - temperature=-0.1, - top_p=1.1, - ) - - -# Test running vLLM with a large number of parallel heads -def test_vllm_large_parallel_heads(): - vllm_instance = vLLM(tensor_parallel_size=16) - task = "Large Parallel Heads Test" - result = vllm_instance.run(task) - assert isinstance(result, str) - assert len(result) > 0 - - -# Test running vLLM with trust_remote_code set to True -def test_vllm_trust_remote_code(): - vllm_instance = vLLM(trust_remote_code=True) - task = "Trust Remote Code Test" - result = vllm_instance.run(task) - assert isinstance(result, str) - assert len(result) > 0 diff --git a/tests/structs/test_multi_agent_collab.py b/tests/structs/test_multi_agent_collab.py index 05b914b4..475b32b3 100644 --- a/tests/structs/test_multi_agent_collab.py +++ b/tests/structs/test_multi_agent_collab.py @@ -135,7 +135,6 @@ def test_save(collaboration, tmp_path): # Add more tests here... - # Add more parameterized tests for different scenarios... diff --git a/tests/structs/test_swarmnetwork.py b/tests/structs/test_swarmnetwork.py index b4e3593b..9dc6d903 100644 --- a/tests/structs/test_swarmnetwork.py +++ b/tests/structs/test_swarmnetwork.py @@ -2,7 +2,6 @@ from unittest.mock import Mock, patch import pytest - from swarms.structs.agent import Agent from swarms.structs.swarm_net import SwarmNetwork From 586dd6bec21465c8932658839f29897d69bec9d0 Mon Sep 17 00:00:00 2001 From: Kye Date: Fri, 2 Feb 2024 19:06:50 -0800 Subject: [PATCH 096/103] [CLEANUP] [old docs] [FEATS][SamplingParam] [Inference][utils] --- docs/old-docs/C0NTRIBUTING.md | 83 --- docs/old-docs/DOCUMENTATION.md | 368 ------------ docs/old-docs/Tutorials/GettingStartedLLM.md | 225 -------- docs/old-docs/agents/MODELS.md | 143 ----- docs/old-docs/agents/README.md | 75 --- docs/old-docs/corp/BENEFITS.md | 114 ---- docs/old-docs/corp/DEMO_IDEAS.md | 7 - docs/old-docs/corp/DEVELOPER_PLAN.md | 101 ---- docs/old-docs/corp/FLYWHEEL.md | 101 ---- docs/old-docs/corp/MANIFESTO.md | 38 -- docs/old-docs/corp/MISSION.md | 149 ----- docs/old-docs/corp/MONETIZATION.md | 165 ------ docs/old-docs/corp/PITCH.md | 14 - docs/old-docs/corp/ROADMAP.md | 115 ---- docs/old-docs/corp/SALES.md | 295 ---------- docs/old-docs/corp/SALESPEOPLE_PLAN.md | 143 ----- docs/old-docs/design/CLEAN_CODE.md | 242 -------- docs/old-docs/design/DESIGN.md | 146 ----- docs/old-docs/design/DESIGN_PHILOSOPHY.md | 53 -- docs/old-docs/design/GOLDEN_METRIC.md | 225 -------- docs/old-docs/design/abstraction.py | 19 - docs/old-docs/ideas/AGENT_NAME.md | 22 - docs/old-docs/ideas/HIVEMIND.md | 69 --- docs/old-docs/ideas/IDEAS.MD | 401 -------------- docs/old-docs/ideas/PLATFORMS.md | 11 - docs/old-docs/ideas/SWARMSOS.md | 42 -- docs/old-docs/ideas/aug13.md | 78 --- docs/old-docs/ideas/aug16.md | 80 --- docs/old-docs/research/AGENTS.md | 522 ------------------ docs/old-docs/research/LANGCHAIN_WEAKNESS.md | 104 ---- docs/old-docs/research/RESEARCH.md | 29 - docs/old-docs/workers/VortexAgent.md | 75 --- docs/old-docs/workers/WorkerNode.md | 275 --------- swarms/__init__.py | 5 + swarms/models/__init__.py | 9 +- .../models/inference_engine.py | 0 swarms/models/sampling_params.py | 299 ++++++++++ swarms/structs/task_tree.py | 80 +++ swarms/utils/dist_utils.py | 315 +++++++++++ swarms/utils/inference_convert_utils.py | 79 +++ 40 files changed, 786 insertions(+), 4530 deletions(-) delete mode 100644 docs/old-docs/C0NTRIBUTING.md delete mode 100644 docs/old-docs/DOCUMENTATION.md delete mode 100644 docs/old-docs/Tutorials/GettingStartedLLM.md delete mode 100644 docs/old-docs/agents/MODELS.md delete mode 100644 docs/old-docs/agents/README.md delete mode 100644 docs/old-docs/corp/BENEFITS.md delete mode 100644 docs/old-docs/corp/DEMO_IDEAS.md delete mode 100644 docs/old-docs/corp/DEVELOPER_PLAN.md delete mode 100644 docs/old-docs/corp/FLYWHEEL.md delete mode 100644 docs/old-docs/corp/MANIFESTO.md delete mode 100644 docs/old-docs/corp/MISSION.md delete mode 100644 docs/old-docs/corp/MONETIZATION.md delete mode 100644 docs/old-docs/corp/PITCH.md delete mode 100644 docs/old-docs/corp/ROADMAP.md delete mode 100644 docs/old-docs/corp/SALES.md delete mode 100644 docs/old-docs/corp/SALESPEOPLE_PLAN.md delete mode 100644 docs/old-docs/design/CLEAN_CODE.md delete mode 100644 docs/old-docs/design/DESIGN.md delete mode 100644 docs/old-docs/design/DESIGN_PHILOSOPHY.md delete mode 100644 docs/old-docs/design/GOLDEN_METRIC.md delete mode 100644 docs/old-docs/design/abstraction.py delete mode 100644 docs/old-docs/ideas/AGENT_NAME.md delete mode 100644 docs/old-docs/ideas/HIVEMIND.md delete mode 100644 docs/old-docs/ideas/IDEAS.MD delete mode 100644 docs/old-docs/ideas/PLATFORMS.md delete mode 100644 docs/old-docs/ideas/SWARMSOS.md delete mode 100644 docs/old-docs/ideas/aug13.md delete mode 100644 docs/old-docs/ideas/aug16.md delete mode 100644 docs/old-docs/research/AGENTS.md delete mode 100644 docs/old-docs/research/LANGCHAIN_WEAKNESS.md delete mode 100644 docs/old-docs/research/RESEARCH.md delete mode 100644 docs/old-docs/workers/VortexAgent.md delete mode 100644 docs/old-docs/workers/WorkerNode.md rename docs/old-docs/README.md => swarms/models/inference_engine.py (100%) create mode 100644 swarms/models/sampling_params.py create mode 100644 swarms/structs/task_tree.py create mode 100644 swarms/utils/dist_utils.py create mode 100644 swarms/utils/inference_convert_utils.py diff --git a/docs/old-docs/C0NTRIBUTING.md b/docs/old-docs/C0NTRIBUTING.md deleted file mode 100644 index 4cf85e6b..00000000 --- a/docs/old-docs/C0NTRIBUTING.md +++ /dev/null @@ -1,83 +0,0 @@ -# Contributing to Swarms - -Thank you for your interest in contributing to Swarms! We welcome contributions from the community to help improve usability and readability. By contributing, you can be a part of creating a dynamic and interactive AI system. - -To get started, please follow the guidelines below. - -## Join the Swarms Community - -Join the Swarms community on Discord to connect with other contributors, coordinate work, and receive support. - -- [Join the Swarms Discord Server](https://discord.gg/qUtxnK2NMf) - -## Taking on Tasks - -We have a growing list of tasks and issues that you can contribute to. To get started, follow these steps: - -1. Visit the [Swarms GitHub repository](https://github.com/kyegomez/swarms) and browse through the existing issues. - -2. Find an issue that interests you and make a comment stating that you would like to work on it. Include a brief description of how you plan to solve the problem and any questions you may have. - -3. Once a project coordinator assigns the issue to you, you can start working on it. - -If you come across an issue that is unclear but still interests you, please post in the Discord server mentioned above. Someone from the community will be able to help clarify the issue in more detail. - -We also welcome contributions to documentation, such as updating markdown files, adding docstrings, creating system architecture diagrams, and other related tasks. - -## Submitting Your Work - -To contribute your changes to Swarms, please follow these steps: - -1. Fork the Swarms repository to your GitHub account. You can do this by clicking on the "Fork" button on the repository page. - -2. Clone the forked repository to your local machine using the `git clone` command. - -3. Before making any changes, make sure to sync your forked repository with the original repository to keep it up to date. You can do this by following the instructions [here](https://docs.github.com/en/github/collaborating-with-pull-requests/syncing-a-fork). - -4. Create a new branch for your changes. This branch should have a descriptive name that reflects the task or issue you are working on. - -5. Make your changes in the branch, focusing on a small, focused change that only affects a few files. - -6. Run any necessary formatting or linting tools to ensure that your changes adhere to the project's coding standards. - -7. Once your changes are ready, commit them to your branch with descriptive commit messages. - -8. Push the branch to your forked repository. - -9. Create a pull request (PR) from your branch to the main Swarms repository. Provide a clear and concise description of your changes in the PR. - -10. Request a review from the project maintainers. They will review your changes, provide feedback, and suggest any necessary improvements. - -11. Make any required updates or address any feedback provided during the review process. - -12. Once your changes have been reviewed and approved, they will be merged into the main branch of the Swarms repository. - -13. Congratulations! You have successfully contributed to Swarms. - -Please note that during the review process, you may be asked to make changes or address certain issues. It is important to engage in open and constructive communication with the project maintainers to ensure the quality of your contributions. - -## Developer Setup - -If you are interested in setting up the Swarms development environment, please follow the instructions provided in the [developer setup guide](docs/developer-setup.md). This guide provides an overview of the different tools and technologies used in the project. - -## Optimization Priorities - -To continuously improve Swarms, we prioritize the following design objectives: - -1. **Usability**: Increase the ease of use and user-friendliness of the swarm system to facilitate adoption and interaction with basic input. - -2. **Reliability**: Improve the swarm's ability to obtain the desired output even with basic and un-detailed input. - -3. **Speed**: Reduce the time it takes for the swarm to accomplish tasks by improving the communication layer, critiquing, and self-alignment with meta prompting. - -4. **Scalability**: Ensure that the system is asynchronous, concurrent, and self-healing to support scalability. - -Our goal is to continuously improve Swarms by following this roadmap while also being adaptable to new needs and opportunities as they arise. - -## Join the Agora Community - -Swarms is brought to you by Agora, the open-source AI research organization. Join the Agora community to connect with other researchers and developers working on AI projects. - -- [Join the Agora Discord Server](https://discord.gg/qUtxnK2NMf) - -Thank you for your contributions and for being a part of the Swarms and Agora community! Together, we can advance Humanity through the power of AI. \ No newline at end of file diff --git a/docs/old-docs/DOCUMENTATION.md b/docs/old-docs/DOCUMENTATION.md deleted file mode 100644 index b9c33b51..00000000 --- a/docs/old-docs/DOCUMENTATION.md +++ /dev/null @@ -1,368 +0,0 @@ -# Swarms Documentation - -## ClassName - -Swarms - -## Purpose - -The Swarms module provides a powerful framework for creating and managing swarms of autonomous agents to accomplish complex tasks. It consists of the `WorkerNode` and `BossNode` classes, along with the `LLM` utility class, which allow you to easily set up and run a swarm of agents to tackle any objective. The module is highly configurable and extensible, providing flexibility to accommodate various use cases. - -## Usage example - -```python -from swarms import Swarms - -api_key = "your_openai_api_key" - -# Initialize Swarms with your API key -swarm = Swarms(api_key=api_key) - -# Define an objective -objective = "Please make a web GUI for using HTTP API server..." - -# Run Swarms -result = swarm.run(objective) - -print(result) -``` - -## Constructor - -```python -def __init__(self, openai_api_key) -``` - -- `openai_api_key` (required): The API key for OpenAI's models. - -## Methods - -### run(objective) - -Runs the swarm with the given objective by initializing the worker and boss nodes. - -- `objective` (required): The objective or task to be accomplished by the swarm. - -Returns the result of the swarm execution. - -## Example Usage - -```python -from swarms import Swarms - -api_key = "your_openai_api_key" - -# Initialize Swarms with your API key -swarm = Swarms(api_key=api_key) - -# Define an objective -objective = "Please make a web GUI for using HTTP API server..." - -# Run Swarms -result = swarm.run(objective) - -print(result) -``` - -## WorkerNode - -The `WorkerNode` class represents an autonomous agent instance that functions as a worker to accomplish complex tasks. It has the ability to search the internet, process and generate images, text, audio, and more. - -### Constructor - -```python -def __init__(self, llm, tools, vectorstore) -``` - -- `llm` (required): The language model used by the worker node. -- `tools` (required): A list of tools available to the worker node. -- `vectorstore` (required): The vector store used by the worker node. - -### Methods - -- `create_agent(ai_name, ai_role, human_in_the_loop, search_kwargs)`: Creates an agent within the worker node. -- `add_tool(tool)`: Adds a tool to the worker node. -- `run(prompt)`: Runs the worker node to complete a task specified by the prompt. - -### Example Usage - -```python -from swarms import worker_node - -# Your OpenAI API key -api_key = "your_openai_api_key" - -# Initialize a WorkerNode with your API key -node = worker_node(api_key) - -# Define an objective -objective = "Please make a web GUI for using HTTP API server..." - -# Run the task -task = node.run(objective) - -print(task) -``` - -## BossNode - -The `BossNode` class represents an agent responsible for creating and managing tasks for the worker agent(s). It interacts with the worker node(s) to delegate tasks and monitor their progress. - -### Constructor - -```python -def __init__(self, llm, vectorstore, agent_executor, max_iterations) -``` - -- `llm` (required): The language model used by the boss node. -- `vectorstore` (required): The vector store used by the boss node. -- `agent_executor` (required): The agent executor used to execute tasks. -- `max_iterations` (required): The maximum number of iterations for task execution. - -### Methods - -- `create_task(objective)`: Creates a task with the given objective. -- `execute_task(task)`: Executes the given task by interacting with the worker agent(s). - -## LLM - -The `LLM` class is a utility class that provides an interface to different language models (LLMs) such as OpenAI's ChatGPT and Hugging Face models. It is used to initialize the language model for the worker and boss nodes. - -### Constructor - -```python -def __init__(self, openai_api_key=None, hf_repo_id=None, hf_api_token=None, model_kwargs=None) -``` - -- `openai_api_key` (optional): The API key for OpenAI's models. -- `hf_repo_id` (optional): The repository ID for the Hugging Face model. -- `hf_api_token` (optional): The API token for the Hugging Face model. -- `model_kwargs` (optional): Additional keyword arguments to pass to the language model. - -### Methods - -- `run(prompt)`: Runs the language model with the given prompt and returns the generated response. - -## Configuration - -The Swarms module can be configured by modifying the following parameters: - -### WorkerNode - -- `llm_class`: The language model class to use for the worker node (default: `ChatOpenAI`). -- `temperature`: The temperature parameter for the language model (default: `0.5`). - -### BossNode - -- `llm_class`: The language model class to use for the boss node (default: `OpenAI`). -- `max_iterations`: The maximum number of iterations for task execution (default: `5`). - -### LLM - -- `openai_api_key`: The API key for OpenAI's models. -- `hf_repo_id`: The repository ID for the Hugging Face model. -- `hf_api_token`: The API token for the Hugging Face model. -- `model_kwargs`: Additional keyword arguments to pass to the language model. - -## Tool Configuration - -The Swarms module supports various tools that can be added to the worker node for performing specific tasks. The following tools are available: - -- `DuckDuckGoSearchRun`: A tool for performing web searches. -- `WriteFileTool`: A tool for writing files. -- `ReadFileTool`: A tool for reading files. -- `process_csv`: A tool for processing CSV files. -- `WebpageQATool`: A tool for performing question answering using web pages. - -Additional tools can be added by extending the functionality of the `Tool` class. - -## Advanced Usage - -For more advanced usage, you can customize the tools and parameters according to your specific requirements. The Swarms module provides flexibility and extensibility to accommodate various use cases. - -For example, you can add your own custom tools by extending the `Tool` class and adding them to the worker node. You can also modify the prompt templates used by the boss node to customize the interaction between the boss and worker agents. - -Please refer to the source code and documentation of the Swarms module for more details and examples. - -## Conclusion - -The Swarms module provides a powerful framework for creating and managing swarms of autonomous agents to accomplish complex tasks. With the `WorkerNode` and `BossNode` classes, along with the `LLM` utility class, you can easily set up and run a swarm of agents to tackle any objective. The module is highly configurable and extensible, allowing you to tailor it to your specific needs. - - -## LLM -### Purpose -The `LLM` class provides an interface to different language models (LLMs) such as OpenAI's ChatGPT and Hugging Face models. It allows you to initialize and run a language model with a given prompt and obtain the generated response. - -### Systems Understanding -The `LLM` class takes an OpenAI API key or Hugging Face repository ID and API token as input. It uses these credentials to initialize the language model, either from OpenAI's models or from a specific Hugging Face repository. The language model can then be run with a prompt, and the generated response is returned. - -### Usage Example -```python -from swarms import LLM - -# Create an instance of LLM with OpenAI API key -llm_instance = LLM(openai_api_key="your_openai_key") - -# Run the language model with a prompt -result = llm_instance.run("Who won the FIFA World Cup in 1998?") -print(result) - -# Create an instance of LLM with Hugging Face repository ID and API token -llm_instance = LLM(hf_repo_id="google/flan-t5-xl", hf_api_token="your_hf_api_token") - -# Run the language model with a prompt -result = llm_instance.run("Who won the FIFA World Cup in 1998?") -print(result) -``` - -### Constructor -```python -def __init__(self, openai_api_key: Optional[str] = None, - hf_repo_id: Optional[str] = None, - hf_api_token: Optional[str] = None, - model_kwargs: Optional[dict] = None) -``` -- `openai_api_key` (optional): The API key for OpenAI's models. -- `hf_repo_id` (optional): The repository ID for the Hugging Face model. -- `hf_api_token` (optional): The API token for the Hugging Face model. -- `model_kwargs` (optional): Additional keyword arguments to pass to the language model. - -### Methods -- `run(prompt: str) -> str`: Runs the language model with the given prompt and returns the generated response. - -### Args -- `prompt` (str): The prompt to be passed to the language model. - -### Returns -- `result` (str): The generated response from the language model. - -## Conclusion -The `LLM` class provides a convenient way to initialize and run different language models using either OpenAI's API or Hugging Face models. By providing the necessary credentials and a prompt, you can obtain the generated response from the language model. - - - - - - -# `GooglePalm` class: - -### Example 1: Using Dictionaries as Messages - -```python -from google_palm import GooglePalm - -# Initialize the GooglePalm instance -gp = GooglePalm( - client=your_client, - model_name="models/chat-bison-001", - temperature=0.7, - top_p=0.9, - top_k=10, - n=5 -) - -# Create some messages -messages = [ - {"role": "system", "content": "You are a helpful assistant."}, - {"role": "user", "content": "Who won the world series in 2020?"}, -] - -# Generate a response -response = gp.generate(messages) - -# Print the generated response -print(response) -``` - -### Example 2: Using BaseMessage and Its Subclasses as Messages - -```python -from google_palm import GooglePalm -from langchain.schema.messages import SystemMessage, HumanMessage - -# Initialize the GooglePalm instance -gp = GooglePalm( - client=your_client, - model_name="models/chat-bison-001", - temperature=0.7, - top_p=0.9, - top_k=10, - n=5 -) - -# Create some messages -messages = [ - SystemMessage(content="You are a helpful assistant."), - HumanMessage(content="Who won the world series in 2020?"), -] - -# Generate a response -response = gp.generate(messages) - -# Print the generated response -print(response) -``` - -### Example 3: Using GooglePalm with Asynchronous Function - -```python -import asyncio -from google_palm import GooglePalm -from langchain.schema.messages import SystemMessage, HumanMessage - -# Initialize the GooglePalm instance -gp = GooglePalm( - client=your_client, - model_name="models/chat-bison-001", - temperature=0.7, - top_p=0.9, - top_k=10, - n=5 -) - -# Create some messages -messages = [ - SystemMessage(content="You are a helpful assistant."), - HumanMessage(content="Who won the world series in 2020?"), -] - -# Define an asynchronous function -async def generate_response(): - response = await gp._agenerate(messages) - print(response) - -# Run the asynchronous function -asyncio.run(generate_response()) -``` - -Remember to replace `your_client` with an actual instance of your client. Also, ensure the `model_name` is the correct name of the model that you want to use. - -The `temperature`, `top_p`, `top_k`, and `n` parameters control the randomness and diversity of the generated responses. You can adjust these parameters based on your application's requirements. - - - - - -## `CodeInterpreter`: - -```python -tool = CodeInterpreter("Code Interpreter", "A tool to interpret code and generate useful outputs.") -tool.run("Plot the bitcoin chart of 2023 YTD") - -# Or with file inputs -tool.run("Analyze this dataset and plot something interesting about it.", ["examples/assets/iris.csv"]) -``` - -To use the asynchronous version, simply replace `run` with `arun` and ensure your calling code is in an async context: - -```python -import asyncio - -tool = CodeInterpreter("Code Interpreter", "A tool to interpret code and generate useful outputs.") -asyncio.run(tool.arun("Plot the bitcoin chart of 2023 YTD")) - -# Or with file inputs -asyncio.run(tool.arun("Analyze this dataset and plot something interesting about it.", ["examples/assets/iris.csv"])) -``` - -The `CodeInterpreter` class is a flexible tool that uses the `CodeInterpreterSession` from the `codeinterpreterapi` package to run the code interpretation and return the result. It provides both synchronous and asynchronous methods for convenience, and ensures that exceptions are handled gracefully. \ No newline at end of file diff --git a/docs/old-docs/Tutorials/GettingStartedLLM.md b/docs/old-docs/Tutorials/GettingStartedLLM.md deleted file mode 100644 index f0d06ef5..00000000 --- a/docs/old-docs/Tutorials/GettingStartedLLM.md +++ /dev/null @@ -1,225 +0,0 @@ -# Getting Started with Swarms: A Simple Introduction to State-of-the-Art Language Models -====================================================================================== - -Welcome to the universe of Swarms! 🚀 - -Today, you're embarking on a thrilling journey through the ever-evolving realm of state-of-the-art language models. - -As you might know, we're in the early days of this adventure, and every step we take is building from the ground up. - -Our foundation is set on five levels of abstraction. - -Each level adds complexity and capability, but worry not! - -We'll walk you through each step, making sure you have fun and learn along the way. - -So, ready to swarm? - -Let's dive right in! - -Installation 😊 -=============== - -To get started with Swarms, run the following command: - -pip install swarms - -1\. OpenAI -========== - -Ah, OpenAI, where the magic of GPT series lives. - -With Swarms, you can tap into this magic in a straightforward way. - -Think of it as having a chat with one of the smartest beings ever created by humankind! - -Features ✨ ----------- - -- Direct Interface: Seamless interaction with OpenAI's GPT models. -- Synchronous & Asynchronous Interaction: Flexibility to interact in real-time or in the background. -- Multi-query Support: Enables querying multiple IDs simultaneously. -- Streaming Capability: Stream multiple responses for dynamic conversations. -- Console Logging: Gives users visibility and traceability of their interactions. - -How It Works: -============= - -1. Initiate: Set up your agent using your OpenAI API key and other customizable parameters. -2. Converse: Use methods like `generate` to converse with the model. Got a list of queries? No worries, methods like `ask_multiple` got you covered. -3. Marvel: Witness the intelligence in the responses and interact in real-time! - -Quick Start: -============ - -Imagine a scenario where you want to know how multiple IDs (say products, books, or places) are perceived. It's just two lines of code away! - -from swarms import OpenAI()\ -chat = OpenAI()\ -response = chat.generate("Hello World")\ -print(response) - -2\. HuggingFace -=============== - -HuggingFace is a name that's changed the game in the NLP world. And with Swarms, you can easily harness the power of their vast model repository. - -Features ✨ ----------- - -- Access to a Vast Model Repository: Directly tap into HuggingFace's expansive model hub. -- Intuitive Text Generation: Prompt-based text generation that's straightforward. -- High Customizability: Users can set device preferences, maximum length of generated text, and more. -- Speed Boost: Our implementation offers up to a 9x speed increase by leveraging model quantization. -- Less Memory Consumption: Quantization reduces the model size significantly. -- Maintained Accuracy: Despite the reduction in model size and increased speed, the quality of the output remains top-tier. -- Superior to Other Packages: Unlike many other packages that simply wrap around the HuggingFace API, Swarms has built-in support for advanced features like quantization, making it both faster and more efficient. - -How It Works: -============= - -1. Pick Your Model: From BERT to GPT-2, choose from a myriad of options. -2. Chat Away: Generate thought-provoking text based on your prompts. - -Quick Start: -============ - -Ready to create a story? - -from swarms import HuggingFaceLLM - -hugging_face_model = HuggingFaceLLM(model_id="amazon/FalconLite")\ -generated_text = hugging_face_model.generate("In a world where AI rules," - -3\. Google PaLM -=============== - -Google's venture into conversational AI, the PaLM Chat API, can now be effortlessly integrated into your projects with Swarms. - -Features ✨ ----------- - -- Easy Integration: Quickly set up interactions with Google's PaLM Chat API. -- Dynamic Conversations: Engage in back-and-forth chat-like conversations with the model. -- Customizable Sampling Techniques: Set temperature, top-p, and top-k values for diverse and controlled outputs. - -How It Works: -============= - -1. Set Up: Initialize with your preferred model and Google API key. -2. Engage: Engage in back-and-forth conversations with the model. - -Quick Start: -============ - -Looking for a quick joke? Google's got you: - -from swarms import GooglePalm - -google_palm = GooglePalm()\ -messages = [{"role": "system", "content": "You are a funny assistant"}, {"role": "user", "content": "Crack me a joke"}]\ -response = google_palm.generate(messages) - -4\. Anthropic (swarms.models.Anthropic) -============================================== - -Anthropic's models, with their mysterious allure, are now at your fingertips. - -Features ✨ ----------- - -- Simplified Access: Straightforward interaction with Anthropic's large language models. -- Dynamic Text Generation: Generate intriguing content based on user prompts. -- Streaming Mode: Enable real-time streaming of responses for dynamic use-cases. - -How It Works: -============= - -1. Initialize: Get started with your preferred Anthropic model. -2. Generate: Whether you're crafting a story or looking for answers, you're in for a treat. - -Quick Start: -============ - -Dive into a fairy tale: - -from swarms import Anthropic - -anthropic = Anthropic()\ -generated_text = anthropic.generate("In a kingdom far away,") - -Building with the Five Levels of Abstraction -============================================ - -From the individual model, right up to the hivemind, we've crafted a layered approach that scales and diversifies your interactions: - -1. Model: Start with a base model like OpenAI. -2. Agent Level: Integrate the model with vector stores and tools. -3. Worker Infrastructure: Assign tasks to worker nodes with specific tools. -4. Swarm Level: Coordinate multiple worker nodes for a symphony of intelligence. -5. Hivemind: The pinnacle! Integrate multiple swarms for unparalleled capability. - -And, our master plan is... - -The Master Plan -=============== - -Phase 1: Building the Foundation --------------------------------- - -In the first phase, our focus is on building the basic infrastructure of Swarms. - -This includes developing key components like the Swarms class, integrating essential tools, and establishing task completion and evaluation logic. - -We'll also start developing our testing and evaluation framework during this phase. - -If you're interested in foundational work and have a knack for building robust, scalable systems, this phase is for you. - -Phase 2: Optimizing the System ------------------------------- - -In the second phase, we'll focus on optimizing Swarms by integrating more advanced features, improving the system's efficiency, and refining our testing and evaluation framework. - -This phase involves more complex tasks, so if you enjoy tackling challenging problems and contributing to the development of innovative features, this is the phase for you. - -Phase 3: Towards Super-Intelligence ------------------------------------ - -The third phase of our bounty program is the most exciting --- this is where we aim to achieve super-intelligence. - -In this phase, we'll be working on improving the swarm's capabilities, expanding its skills, and fine-tuning the system based on real-world testing and feedback. - -If you're excited about the future of AI and want to contribute to a project that could potentially transform the digital world, this is the phase for you. - -Remember, our roadmap is a guide, and we encourage you to bring your own ideas and creativity to the table. - -We believe that every contribution, no matter how small, can make a difference. - -So join us on this exciting journey and help us create the future of Swarms. - -Hiring: -======= - -We're hiring: Engineers, Researchers, Interns And, salesprofessionals to work on democratizing swarms, email me at with your story at `kye@apac.ai` - -In Conclusion: A World of Possibilities -======================================= - -There you have it! - -A whirlwind tour through some of the most cutting-edge language models available today. - -Remember, Swarms is like a treasure chest, and we're continually adding more jewels to it. - -As Sir Jonathan Ive would say, "True simplicity is derived from so much more than just the absence of clutter and ornamentation, it's about bringing order to complexity." - -Now, with the foundation of Swarms beneath your feet, you're well-equipped to soar to new heights. - -So go on, experiment, explore, and have a blast! - -The future of AI awaits you! 🌌🐝🎉 - -*Disclaimer: Remember, we're at the early stages, but every idea, every line of code, every interaction you have, is helping shape the future of Swarms. So, thank you for being a part of this exciting journey!* - -Happy Swarming! - diff --git a/docs/old-docs/agents/MODELS.md b/docs/old-docs/agents/MODELS.md deleted file mode 100644 index d2dce9cb..00000000 --- a/docs/old-docs/agents/MODELS.md +++ /dev/null @@ -1,143 +0,0 @@ -## LLMs in Swarms Documentation - -Welcome to the documentation for the llm section of the swarms package, designed to facilitate seamless integration with various AI language models and APIs. This package empowers developers, end-users, and system administrators to interact with AI models from different providers, such as OpenAI, Hugging Face, Google PaLM, and Anthropic. - -### Table of Contents -1. [OpenAI](#openai) -2. [HuggingFace](#huggingface) -3. [Google PaLM](#google-palm) -4. [Anthropic](#anthropic) - -### 1. OpenAI (swarms.models.OpenAI) - -The OpenAI class provides an interface to interact with OpenAI's language models. It allows both synchronous and asynchronous interactions. - -**Constructor:** -```python -OpenAI(api_key: str, system: str = None, console: bool = True, model: str = None, params: dict = None, save_messages: bool = True) -``` - -**Attributes:** -- `api_key` (str): Your OpenAI API key. -- `system` (str, optional): A system message to be used in conversations. -- `console` (bool, default=True): Display console logs. -- `model` (str, optional): Name of the language model to use. -- `params` (dict, optional): Additional parameters for model interactions. -- `save_messages` (bool, default=True): Save conversation messages. - -**Methods:** -- `generate(message: str, **kwargs) -> str`: Generate a response using the OpenAI model. -- `generate_async(message: str, **kwargs) -> str`: Generate a response asynchronously. -- `ask_multiple(ids: List[str], question_template: str) -> List[str]`: Query multiple IDs simultaneously. -- `stream_multiple(ids: List[str], question_template: str) -> List[str]`: Stream multiple responses. - -**Usage Example:** -```python -from swarms import OpenAI -import asyncio - -chat = OpenAI(api_key="YOUR_OPENAI_API_KEY") - -response = chat.generate("Hello, how can I assist you?") -print(response) - -ids = ["id1", "id2", "id3"] -async_responses = asyncio.run(chat.ask_multiple(ids, "How is {id}?")) -print(async_responses) -``` - -### 2. HuggingFace (swarms.models.HuggingFaceLLM) - -The HuggingFaceLLM class allows interaction with language models from Hugging Face. - -**Constructor:** -```python -HuggingFaceLLM(model_id: str, device: str = None, max_length: int = 20, quantize: bool = False, quantization_config: dict = None) -``` - -**Attributes:** -- `model_id` (str): ID or name of the Hugging Face model. -- `device` (str, optional): Device to run the model on (e.g., 'cuda', 'cpu'). -- `max_length` (int, default=20): Maximum length of generated text. -- `quantize` (bool, default=False): Apply model quantization. -- `quantization_config` (dict, optional): Configuration for quantization. - -**Methods:** -- `generate(prompt_text: str, max_length: int = None) -> str`: Generate text based on a prompt. - -**Usage Example:** -```python -from swarms import HuggingFaceLLM - -model_id = "gpt2" -hugging_face_model = HuggingFaceLLM(model_id=model_id) - -prompt = "Once upon a time" -generated_text = hugging_face_model.generate(prompt) -print(generated_text) -``` - -### 3. Google PaLM (swarms.models.GooglePalm) - -The GooglePalm class provides an interface for Google's PaLM Chat API. - -**Constructor:** -```python -GooglePalm(model_name: str = "models/chat-bison-001", google_api_key: str = None, temperature: float = None, top_p: float = None, top_k: int = None, n: int = 1) -``` - -**Attributes:** -- `model_name` (str): Name of the Google PaLM model. -- `google_api_key` (str, optional): Google API key. -- `temperature` (float, optional): Temperature for text generation. -- `top_p` (float, optional): Top-p sampling value. -- `top_k` (int, optional): Top-k sampling value. -- `n` (int, default=1): Number of candidate completions. - -**Methods:** -- `generate(messages: List[Dict[str, Any]], stop: List[str] = None, **kwargs) -> Dict[str, Any]`: Generate text based on a list of messages. -- `__call__(messages: List[Dict[str, Any]], stop: List[str] = None, **kwargs) -> Dict[str, Any]`: Generate text using the call syntax. - -**Usage Example:** -```python -from swarms import GooglePalm - -google_palm = GooglePalm() -messages = [{"role": "system", "content": "You are a helpful assistant"}, {"role": "user", "content": "Tell me a joke"}] - -response = google_palm.generate(messages) -print(response["choices"][0]["text"]) -``` - -### 4. Anthropic (swarms.models.Anthropic) - -The Anthropic class enables interaction with Anthropic's large language models. - -**Constructor:** -```python -Anthropic(model: str = "claude-2", max_tokens_to_sample: int = 256, temperature: float = None, top_k: int = None, top_p: float = None, streaming: bool = False, default_request_timeout: int = None) -``` - -**Attributes:** -- `model` (str): Name of the Anthropic model. -- `max_tokens_to_sample` (int, default=256): Maximum tokens to sample. -- `temperature` (float, optional): Temperature for text generation. -- `top_k` (int, optional): Top-k sampling value. -- `top_p` (float, optional): Top-p sampling value. -- `streaming` (bool, default=False): Enable streaming mode. -- `default_request_timeout` (int, optional): Default request timeout. - -**Methods:** -- `generate(prompt: str, stop: List[str] = None) -> str`: Generate text based on a prompt. - -**Usage Example:** -```python -from swarms import Anthropic - -anthropic = Anthropic() -prompt = "Once upon a time" -generated_text = anthropic.generate(prompt) -print(generated_text) -``` - -This concludes the documentation for the "swarms" package, providing you with tools to seamlessly integrate with various language models and APIs. Happy coding! \ No newline at end of file diff --git a/docs/old-docs/agents/README.md b/docs/old-docs/agents/README.md deleted file mode 100644 index 287c69d9..00000000 --- a/docs/old-docs/agents/README.md +++ /dev/null @@ -1,75 +0,0 @@ -Introduction to Agents in Swarms -================================ - -Welcome to the revolutionary world of Agents in Swarms. I'm a big believer in simplicity, modularity, and the power of open collaboration. The same principles apply here. - -Agents are the individual building blocks in a swarm. They are the worker bees, each with a specific task, but all working together towards a common goal. In our case, an agent is a combination of a Language Model (LLM), Long Term Memory, and Tools. - -In other words, an agent is: - -`LLM => Long Term Memory => Tools` - -That's it. That's as simple as it can get. - -Why does this work? Because each component has a specific, well-defined role. The Language Model is the driving force, generating text based on a given prompt. The Long Term Memory stores information that the agent can draw upon to make its output more coherent and contextually relevant. The Tools provide additional capabilities, such as the ability to parse text, search the web, or interact with APIs. - -But the real beauty of this system is not just in the individual components, but in how they work together. The output of one becomes the input of another, creating a feedback loop of continuous learning and improvement. - -And the best part? Our Agent classes are designed to be as simple as humanely possible. They are plug-and-play with any of our language model classes, vector stores, and tools. This means you can easily swap out one component for another, allowing for endless customization and flexibility. - -The file structure is equally straightforward: - -``` -* memory -* models -* tools -* utils - -``` - -Each directory contains different components of the swarm. The `models` directory contains the language models, the `memory` directory contains the long-term memory, the `tools` directory contains the tools, the `utils` directory contains various utility functions. - -Let's see how simple it is to use these components with some examples: - -```python -# Import the necessary classes -from swarms.agents import Anthropic, HuggingFaceLLM - -# Create an instance of the Anthropic class -anthropic = Anthropic(model="claude-2", max_tokens_to_sample=100, temperature=0.8) - -# Use the Anthropic instance to generate text -prompt = "Once upon a time" -stop = ["The end"] -print("Anthropic output:") -print(anthropic.generate(prompt, stop)) - -# Create an instance of the HuggingFaceLLM class -huggingface = HuggingFaceLLM(model_id="gpt2", device="cpu", max_length=50) - -# Use the HuggingFaceLLM instance to generate text -prompt = "Once upon a time" -print("\nHuggingFaceLLM output:") -print(huggingface.generate(prompt)) -``` - - -And to build an agent: - -```python -from swarms.agents import vectorstore, tool, Agent - -# Create an instance of the Agent class -agent = Agent( - llm=huggingface, - memory=vectorstore, - tools=tool, -) - -agent.run("Make me an instagram clone") -``` - - -In conclusion, the Agents in Swarms represent a new way of thinking about AI. They are simple, modular, and highly customizable, allowing you to create powerful AI systems that are more than the sum of their parts. And as always, we're just getting started. There's always room for improvement, for simplification, for making things even better. That's the spirit of open collaboration. That's the spirit of Swarms. - -Thanks for becoming an alpha build user, email kye@apac.ai with all complaints. \ No newline at end of file diff --git a/docs/old-docs/corp/BENEFITS.md b/docs/old-docs/corp/BENEFITS.md deleted file mode 100644 index a908915c..00000000 --- a/docs/old-docs/corp/BENEFITS.md +++ /dev/null @@ -1,114 +0,0 @@ -Maximize Value Using Value Equation -1. Maximize Dream Outcome: Solve Problems Worth $1 Billion -Swarms empowers you to solve problems worth $1 billion, maximizing your dream outcome and the potential impact of your work. - -2. Maximize Perceived Likelihood of Success: 99% Success Rate -With a 99% success rate backed by testimonials and proven case studies, Swarms maximizes your confidence in achieving your desired outcomes. - -3. Minimize Time to Success: Achieve Results 10x Faster -Swarms minimizes the time it takes to achieve success by enabling you to accomplish tasks and goals 10 times faster than traditional methods. - -4. Minimize Effort & Sacrifice: Save 100 Hours per Week -By automating tasks and streamlining processes, Swarms saves you 100 hours per week, minimizing effort and sacrifice required to achieve your goals. - -5. Maximize Efficiency: Increase Productivity by 300% -Swarms optimizes your workflow, increasing productivity by 300% through intelligent automation and task optimization. - -6. Minimize Errors: Ensure 99.9% Accuracy -With Swarms' autonomous AI agents, you can achieve 99.9% accuracy, minimizing errors and ensuring the highest level of quality in your work. - -7. Maximize Scalability: Handle 1 Million Transactions per Second -Swarms scales with your growing needs, allowing you to handle up to 1 million transactions per second, ensuring seamless operations as your business expands. - -8. Minimize Costs: Save $1 Million Annually -By optimizing resource allocation and reducing manual labor, Swarms helps you save $1 million annually, minimizing costs and maximizing your bottom line. - -9. Maximize Flexibility: Adapt to Changing Requirements in Minutes -Swarms offers maximum flexibility, allowing you to adapt to changing requirements in minutes, ensuring you stay agile and responsive in a dynamic business environment. - -10. Minimize Complexity: Simplify Complex Tasks by 90% -Swarms simplifies complex tasks by 90%, breaking them down into manageable steps, minimizing complexity and enabling you to tackle even the most challenging projects. - -11. Maximize Collaboration: Increase Team Efficiency by 200% -With Swarms' coordination capabilities, you can increase team efficiency by 200%, fostering collaboration and driving innovation within your organization. - -12. Minimize Downtime: Ensure 99.99% Uptime -Swarms ensures 99.99% uptime, minimizing downtime and ensuring continuous operations, preventing costly disruptions to your business. - -13. Maximize Security: Protect Your Data with Military-Grade Encryption -Swarms prioritizes data security, providing military-grade encryption to protect your sensitive information, maximizing the security and confidentiality of your data. - -14. Minimize Learning Curve: Get Up and Running in 1 Hour -Swarms minimizes the learning curve, allowing you to get up and running in just 1 hour, maximizing your time and productivity. - -15. Maximize Innovation: Stay Ahead with AI-Driven Insights -Swarms leverages AI and autonomous agents to provide cutting-edge insights, enabling you to stay ahead of the competition and drive innovation in your industry. - -16. Minimize Maintenance: Reduce Maintenance Costs by 80% -Swarms reduces maintenance costs by 80%, minimizing the time and resources required for upkeep, allowing you to focus on your core business activities. - -17. Maximize Adaptability: Customize to Your Needs with 100+ Configurable Options -Swarms offers over 100 configurable options, maximizing adaptability and allowing you to customize the platform to suit your specific requirements. - - - -1. Maximize Dream Outcome: Solve Problems Worth $1 Billion -Swarms empowers you to solve problems worth $1 billion, maximizing your dream outcome and the potential impact of your work. - -2. Maximize Perceived Likelihood of Success: 99% Success Rate -With a 99% success rate backed by testimonials and proven case studies, Swarms maximizes your confidence in achieving your desired outcomes. - -3. Minimize Time to Success: Achieve Results 10x Faster -Swarms minimizes the time it takes to achieve success by enabling you to accomplish tasks and goals 10 times faster than traditional methods. - -4. Minimize Effort & Sacrifice: Save 100 Hours per Week -By automating tasks and streamlining processes, Swarms saves you 100 hours per week, minimizing effort and sacrifice required to achieve your goals. - -5. Maximize Efficiency: Increase Productivity by 300% -Swarms optimizes your workflow, increasing productivity by 300% through intelligent automation and task optimization. - -6. Minimize Errors: Ensure 99.9% Accuracy -With Swarms' autonomous AI agents, you can achieve 99.9% accuracy, minimizing errors and ensuring the highest level of quality in your work. - -7. Maximize Scalability: Handle 1 Million Transactions per Second -Swarms scales with your growing needs, allowing you to handle up to 1 million transactions per second, ensuring seamless operations as your business expands. - -8. Minimize Costs: Save $1 Million Annually -By optimizing resource allocation and reducing manual labor, Swarms helps you save $1 million annually, minimizing costs and maximizing your bottom line. - -9. Maximize Flexibility: Adapt to Changing Requirements in Minutes -Swarms offers maximum flexibility, allowing you to adapt to changing requirements in minutes, ensuring you stay agile and responsive in a dynamic business environment. - -10. Minimize Complexity: Simplify Complex Tasks by 90% -Swarms simplifies complex tasks by 90%, breaking them down into manageable steps, minimizing complexity and enabling you to tackle even the most challenging projects. - -11. Maximize Collaboration: Increase Team Efficiency by 200% -With Swarms' coordination capabilities, you can increase team efficiency by 200%, fostering collaboration and driving innovation within your organization. - -12. Minimize Downtime: Ensure 99.99% Uptime -Swarms ensures 99.99% uptime, minimizing downtime and ensuring continuous operations, preventing costly disruptions to your business. - -13. Maximize Security: Protect Your Data with Military-Grade Encryption -Swarms prioritizes data security, providing military-grade encryption to protect your sensitive information, maximizing the security and confidentiality of your data. - -14. Minimize Learning Curve: Get Up and Running in 1 Hour -Swarms minimizes the learning curve, allowing you to get up and running in just 1 hour, maximizing your time and productivity. - -15. Maximize Innovation: Stay Ahead with AI-Driven Insights -Swarms leverages AI and autonomous agents to provide cutting-edge insights, enabling you to stay ahead of the competition and drive innovation in your industry. - -16. Minimize Maintenance: Reduce Maintenance Costs by 80% -Swarms reduces maintenance costs by 80%, minimizing the time and resources required for upkeep, allowing you to focus on your core business activities. - -17. Maximize Adaptability: Customize to Your Needs with 100+ Configurable Options -Swarms offers over 100 configurable options, maximizing adaptability and allowing you to customize the platform to suit your specific requirements. - -18. Minimize Risk: Mitigate Potential Losses by 95% -Swarms helps you minimize risk by mitigating potential losses by 95%, providing a secure and reliable platform for your critical operations. - -19. Maximize ROI: Achieve 500% Return on Investment -With Swarms' efficiency and cost-saving capabilities, you can achieve a 500% return on investment, maximizing the value you get from your resources. - -20. Minimize Waste: Reduce Resource Consumption by 70% -Swarms minimizes waste by reducing resource consumption by 70%, optimizing resource allocation and promoting sustainability in your operations. - diff --git a/docs/old-docs/corp/DEMO_IDEAS.md b/docs/old-docs/corp/DEMO_IDEAS.md deleted file mode 100644 index e1a27f51..00000000 --- a/docs/old-docs/corp/DEMO_IDEAS.md +++ /dev/null @@ -1,7 +0,0 @@ -# Demo Ideas - -* We could also try to create an AI influencer run by a swarm, let it create a whole identity and generate images, memes, and other content for Twitter, Reddit, etc. - -* had a thought that we should have either a more general one of these or a swarm or both -- need something connecting all the calendars, events, and initiatives of all the AI communities, langchain, laion, eluther, lesswrong, gato, rob miles, chatgpt hackers, etc etc - -* Swarm of AI influencers to spread marketing \ No newline at end of file diff --git a/docs/old-docs/corp/DEVELOPER_PLAN.md b/docs/old-docs/corp/DEVELOPER_PLAN.md deleted file mode 100644 index 18d62db5..00000000 --- a/docs/old-docs/corp/DEVELOPER_PLAN.md +++ /dev/null @@ -1,101 +0,0 @@ -# Flywheel Effect for Developer Acquisition and Incentivization - -As with the sales model, the developer acquisition and incentivization model also relies on a flywheel effect. This effect is particularly potent in a community-driven ecosystem such as ours, where the value proposition continually grows as more developers join and contribute to our projects. Here's how we could apply this approach: - -## Step 1: Initial Value Proposition for Developers -The starting point of the flywheel is to provide an attractive value proposition for developers. This could include: - -- The ability to work on cutting-edge technology (Swarms, in this case). -- The opportunity to contribute to a community-driven, open-source project. -- The chance to learn from and collaborate with a global network of highly skilled developers. -- An incentivization structure that rewards contributions (more on this later). - -## Step 2: Developer Acquisition -With the initial value proposition in place, we can move on to the actual acquisition of developers. This could be accomplished through: - -- Active recruitment from online developer communities. -- Referral programs that incentivize current contributors to bring in new developers. -- Partnerships with universities, boot camps, and other institutions to attract budding developers. - -## Step 3: Collaboration and Learning -Once developers join our ecosystem, they become part of a collaborative community where they can learn from each other, improve their skills, and work on exciting and meaningful projects. This, in turn, attracts more developers, adding momentum to the flywheel. - -## Step 4: Recognizing and Rewarding Contributions -To keep the flywheel spinning, it's crucial to recognize and reward the contributions made by developers. This can be done in various ways: - -- Monetary rewards: Developers can be paid based on the value their contributions bring to the project. This could be determined through various metrics, such as the complexity of their contributions, the impact on the project, or the amount of their code that gets used in production. - -- Reputation and recognition: The open-source nature of our project means that all contributions are public and can be used by developers to build their professional profiles. Contributors could also be highlighted on our website, in our communications, and at community events. - -- Career advancement: Developers who consistently make valuable contributions could be offered positions of leadership within the project, such as becoming maintainers or joining a steering committee. - -- Agora Tokens: We could create a system of tokens that are earned based on contributions. These tokens could be exchanged for various benefits, such as access to exclusive events, special training, or even physical goods. - -## Step 5: Scaling the Flywheel -With the flywheel in motion, the next step is to scale. As our community grows and our technology improves, we can attract more developers and create more value. This leads to a virtuous cycle of growth, where each new developer adds to the attractiveness of our project, which in turn brings in more developers. - -In essence, this flywheel approach is about creating a community where everyone benefits from each other's contributions. The more value a developer adds, the more they are rewarded. The more developers contribute, the more value is created, attracting even more developers. - -Such a model not only aligns with our values of openness, collaboration, and shared success, but it also gives us a sustainable and scalable method for growing our developer community. It makes Agora not just a place to work, but also a place to learn, grow, and be recognized for one's contributions. This is a powerful way to ensure that we can continue to advance our technology and make a significant impact on the world. - - -# Risks and mitigations - -The open source engineering freelancer model brings with it its own set of potential risks and challenges. Here's an exploration of some of these, along with strategies for mitigation: - -**1. Quality Control:** When dealing with a wide network of freelance contributors, ensuring a consistent standard of quality across all contributions can be challenging. This can be mitigated by implementing rigorous review processes and standards, establishing an automated testing infrastructure, and fostering a culture of quality among contributors. Providing clear contribution guidelines, code style guides, and other resources can help freelancers understand what's expected of them. Providing Educational resources such as sponsoring creators like Yannic, and even making our own courses and then building techno-monasteries where young people can come in and research for free. - -**2. Security Risks:** Open-source projects can be susceptible to malicious contributors, who might introduce vulnerabilities into the codebase. To mitigate this, rigorous code review processes should be in place. Additionally, adopting a "trust but verify" approach, leveraging automated security scanning tools, and conducting periodic security audits can be beneficial. - -**3. Intellectual Property Issues:** Open-source projects can face risks around intellectual property, such as contributors introducing code that infringes on someone else's copyrights. A clear Contributor License Agreement (CLA) should be in place, which contributors need to agree to before their contributions can be accepted. This helps protect the project and its users from potential legal issues. - -**4. Loss of Core Focus:** With numerous contributors focusing on different aspects of the project, there can be a risk of losing sight of the project's core objectives. Maintaining a clear roadmap, having a strong leadership team, and ensuring open and regular communication can help keep the project focused. - -**5. Contributor Burnout:** Freelancers contributing in their free time might face burnout, especially if they feel their contributions aren't being recognized or rewarded. To mitigate this, create a supportive environment where contributors' efforts are acknowledged and rewarded. This might include monetary rewards, but can also include non-monetary rewards like public recognition, advancement opportunities within the project, and so on. - -**6. Fragmentation:** In open source projects, there is a risk of fragmentation where different contributors or groups of contributors might want to take the project in different directions. Strong project governance, a clear roadmap, and open, transparent decision-making processes can help mitigate this risk. - -**7. Dependency on Key Individuals:** If key parts of the project are understood and maintained by only a single contributor, there is a risk if that individual decides to leave or is unable to contribute for some reason. This can be mitigated by ensuring knowledge is shared and responsibilities are spread among multiple contributors. - -Overall, these risks can be managed with proper planning, clear communication, and the implementation of good governance and security practices. It's essential to approach the open source model with a clear understanding of these potential pitfalls and a plan to address them. - -## Plan to Gain Open Source Developers for SWARMS - -Attracting and retaining open-source developers is a challenge that requires a strategic approach. This plan emphasizes delivering value to the developers as well as providing recognition, community, and financial incentives. - -### Step 1: Foster an Engaging and Inclusive Community - -The first step is to foster an engaging and inclusive open-source community around SWARMS. This community should be a place where developers feel welcome and excited to contribute. Regular community events (both online and offline), engaging content, and a supportive environment can help attract and retain developers. - -### Step 2: Provide Clear Contribution Guidelines - -Providing clear and comprehensive contribution guidelines will make it easier for developers to get started. These guidelines should cover the basics of how to set up the development environment, how to submit changes, and how the code review process works. - -### Step 3: Offer Educational Resources and Training - -Providing training and educational resources can help developers grow their skills and contribute more effectively. These resources could include tutorials, webinars, workshops, documentation, and more. - -### Step 4: Establish a Recognition and Reward System - -Recognize and reward the contributions of developers. This could involve public recognition, like featuring contributors on the SWARMS website, as well as financial incentives. Implementing a system where developers earn a share of the revenue from SWARMS based on their contributions can be a strong motivator. - -### Step 5: Implement a Strong Support System - -Offer strong technical support to developers. This could include dedicated channels for developers to ask questions, request feedback, and share their progress. Having core team members available to provide assistance and mentorship can be hugely beneficial. - -### Step 6: Regularly Solicit and Incorporate Feedback - -Regularly ask for feedback from developers and incorporate their suggestions into future developments. This shows developers that their opinions are valued and can lead to improvements in SWARMS. - -## Flywheel for Gaining More Open Source Developers - -Now let's look at the flywheel effect that can result from this plan. The idea of the flywheel is that each part of the process feeds into the next, creating a cycle of growth that becomes self-sustaining over time. - -1. We build an engaging and supportive community around SWARMS. -2. This community attracts more developers who are interested in contributing to SWARMS. -3. As more developers contribute, the quality and scope of SWARMS improve, making it more attractive to potential users. -4. As SWARMS gains more users, the potential revenue from SWARMS increases, allowing for larger rewards to be distributed to developers. -5. The prospect of these rewards attracts even more developers to the SWARMS community. -6. The cycle repeats, with each iteration attracting more developers, improving SWARMS, increasing its user base, and raising potential rewards. - -Through this plan and the resulting flywheel effect, we can attract a strong, committed team of open-source developers to build SWARMS and make it the best it can be. \ No newline at end of file diff --git a/docs/old-docs/corp/FLYWHEEL.md b/docs/old-docs/corp/FLYWHEEL.md deleted file mode 100644 index ac8851be..00000000 --- a/docs/old-docs/corp/FLYWHEEL.md +++ /dev/null @@ -1,101 +0,0 @@ -# The Swarms Flywheel - -1. **Building a Supportive Community:** Initiate by establishing an engaging and inclusive open-source community for both developers and sales freelancers around Swarms. Regular online meetups, webinars, tutorials, and sales training can make them feel welcome and encourage contributions and sales efforts. - -2. **Increased Contributions and Sales Efforts:** The more engaged the community, the more developers will contribute to Swarms and the more effort sales freelancers will put into selling Swarms. - -3. **Improvement in Quality and Market Reach:** More developer contributions mean better quality, reliability, and feature offerings from Swarms. Simultaneously, increased sales efforts from freelancers boost Swarms' market penetration and visibility. - -4. **Rise in User Base:** As Swarms becomes more robust and more well-known, the user base grows, driving more revenue. - -5. **Greater Financial Incentives:** Increased revenue can be redirected to offer more significant financial incentives to both developers and salespeople. Developers can be incentivized based on their contribution to Swarms, and salespeople can be rewarded with higher commissions. - -6. **Attract More Developers and Salespeople:** These financial incentives, coupled with the recognition and experience from participating in a successful project, attract more developers and salespeople to the community. - -7. **Wider Adoption of Swarms:** An ever-improving product, a growing user base, and an increasing number of passionate salespeople accelerate the adoption of Swarms. - -8. **Return to Step 1:** As the community, user base, and sales network continue to grow, the cycle repeats, each time speeding up the flywheel. - - -```markdown - +---------------------+ - | Building a | - | Supportive | <--+ - | Community | | - +--------+-----------+ | - | | - v | - +--------+-----------+ | - | Increased | | - | Contributions & | | - | Sales Efforts | | - +--------+-----------+ | - | | - v | - +--------+-----------+ | - | Improvement in | | - | Quality & Market | | - | Reach | | - +--------+-----------+ | - | | - v | - +--------+-----------+ | - | Rise in User | | - | Base | | - +--------+-----------+ | - | | - v | - +--------+-----------+ | - | Greater Financial | | - | Incentives | | - +--------+-----------+ | - | | - v | - +--------+-----------+ | - | Attract More | | - | Developers & | | - | Salespeople | | - +--------+-----------+ | - | | - v | - +--------+-----------+ | - | Wider Adoption of | | - | Swarms |----+ - +---------------------+ -``` - - -# Potential Risks and Mitigations: - -1. **Insufficient Contributions or Quality of Work**: Open-source efforts rely on individuals being willing and able to spend time contributing. If not enough people participate, or the work they produce is of poor quality, the product development could stall. - * **Mitigation**: Create a robust community with clear guidelines, support, and resources. Provide incentives for quality contributions, such as a reputation system, swag, or financial rewards. Conduct thorough code reviews to ensure the quality of contributions. - -2. **Lack of Sales Results**: Commission-based salespeople will only continue to sell the product if they're successful. If they aren't making enough sales, they may lose motivation and cease their efforts. - * **Mitigation**: Provide adequate sales training and resources. Ensure the product-market fit is strong, and adjust messaging or sales tactics as necessary. Consider implementing a minimum commission or base pay to reduce risk for salespeople. - -3. **Poor User Experience or User Adoption**: If users don't find the product useful or easy to use, they won't adopt it, and the user base won't grow. This could also discourage salespeople and contributors. - * **Mitigation**: Prioritize user experience in the product development process. Regularly gather and incorporate user feedback. Ensure robust user support is in place. - -4. **Inadequate Financial Incentives**: If the financial rewards don't justify the time and effort contributors and salespeople are putting in, they will likely disengage. - * **Mitigation**: Regularly review and adjust financial incentives as needed. Ensure that the method for calculating and distributing rewards is transparent and fair. - -5. **Security and Compliance Risks**: As the user base grows and the software becomes more complex, the risk of security issues increases. Moreover, as contributors from various regions join, compliance with various international laws could become an issue. - * **Mitigation**: Establish strong security practices from the start. Regularly conduct security audits. Seek legal counsel to understand and adhere to international laws and regulations. - -## Activation Plan for the Flywheel: - -1. **Community Building**: Begin by fostering a supportive community around Swarms. Encourage early adopters to contribute and provide feedback. Create comprehensive documentation, community guidelines, and a forum for discussion and support. - -2. **Sales and Development Training**: Provide resources and training for salespeople and developers. Make sure they understand the product, its value, and how to effectively contribute or sell. - -3. **Increase Contributions and Sales Efforts**: Encourage increased participation by highlighting successful contributions and sales, rewarding top contributors and salespeople, and regularly communicating about the project's progress and impact. - -4. **Iterate and Improve**: Continually gather and implement feedback to improve Swarms and its market reach. The better the product and its alignment with the market, the more the user base will grow. - -5. **Expand User Base**: As the product improves and sales efforts continue, the user base should grow. Ensure you have the infrastructure to support this growth and maintain a positive user experience. - -6. **Increase Financial Incentives**: As the user base and product grow, so too should the financial incentives. Make sure rewards continue to be competitive and attractive. - -7. **Attract More Contributors and Salespeople**: As the financial incentives and success of the product increase, this should attract more contributors and salespeople, further feeding the flywheel. - -Throughout this process, it's important to regularly reassess and adjust your strategy as necessary. Stay flexible and responsive to changes in the market, user feedback, and the evolving needs of the community. \ No newline at end of file diff --git a/docs/old-docs/corp/MANIFESTO.md b/docs/old-docs/corp/MANIFESTO.md deleted file mode 100644 index b9c79c74..00000000 --- a/docs/old-docs/corp/MANIFESTO.md +++ /dev/null @@ -1,38 +0,0 @@ -Today, we stand at the verge of a revolution in artificial intelligence and machine learning. Individual models have accomplished incredible feats, achieving unprecedented levels of understanding and generating incredibly human-like text. But this is just the beginning. - -In the future, we should expect more. These models, which we've seen perform so admirably in isolation, should be able to work together, as a team, a swarm. However, this kind of collaborative intelligence doesn't exist today. That's because the technology to seamlessly integrate these models and foster true inter-model collaboration has been missing, until now. - -In attempting to create this swarm, we face numerous challenges, such as developing the necessary infrastructure, ensuring seamless integration between the agents, and overcoming the practical limitations of our current computing capabilities. These are daunting tasks, and many have shied away from them because of the sheer complexity of the problem. But, if we can overcome these challenges, the rewards will be unimaginable, all digital activities will be automated. - -We envision a future where swarms of Language Learning Model (LLM) agents revolutionize fields like customer support, content creation, and research. Imagine an AI system that could work cohesively, understand complex problems, and deliver multi-faceted solutions. We estimate this could lead to a 100-fold improvement in AI effectiveness, and up to a trillion-dollar impact on the global economy. - -The secret to achieving this lies in our open-source approach and the power of the collective. By embracing open-source, we are enabling hundreds of thousands of minds worldwide to contribute to this vision, each bringing unique insights and solutions. Our bug bounty program and automated testing environments will act as catalysts, motivating and rewarding contributors while ensuring the robustness and reliability of our technology. - -At Agora, we believe in the transformative potential of this technology, and we are committed to making it a reality. Our world-class team of researchers, engineers, and AI enthusiasts are singularly focused on this mission. With a proven track record of success, and the tenacity to tackle the most complex problems, we are best positioned to lead this charge. - -We invite you to join us on this exciting journey. Let's come together to create swarms, advance humanity, and redefine what is possible with artificial intelligence. Our future is in our hands. Let's shape it together. - - -#2 -Swarms is a fluid, seamless, and reliable framework for scaling up LLM autonomous agent interactions to automate digital tasks. - -Like a chorus harmonizing to create a more beautiful melody, Swarms transforms isolated AI models into a cooperative network that can achieve feats beyond the capacity of any single model. - -Our product is not merely an abstract idea or a dream for the future - it's a reality, right here, right now. Swarms is an accessible, easy-to-use platform. All it takes is a simple 'pip install swarms' command, or a 'git clone' from our GitHub repository, and the swarming power of AI is at your fingertips. - -The fundamental functionality of Swarms is simple: it facilitates dynamic interaction and cooperation among AI models. Users set an objective, and the swarm of AI agents collectively process the task, pooling their strengths to provide a more refined, intelligent output. - -With Swarms, you're not just using a tool, you're unlocking the next level of AI. You're interacting with a cooperative entity that can tackle complex problems, refine outputs, and ultimately, revolutionize the way we utilize AI. - -Our vision is to see Swarms employed across various domains, from customer support to content creation, and research to robotics, expanding the possibilities of what AI can achieve. This isn't just an improvement of existing systems, but a groundbreaking leap forward in AI capability. It's about breaking down the silos, fostering collaboration, and elevating collective intelligence. - - -At the heart of our product, Swarms, is the principle of cooperative intelligence. Swarms is an AI-based technology that enables multiple autonomous agents, or "worker bees," to coordinate, collaborate, and generate solutions that no single agent could achieve on its own. Think of it as a digital hive mind that decomposes a large objective into manageable subtasks, distributes these tasks amongst the autonomous agents, and then assembles the results into a coherent whole. - -In action, the process is fluid, elegant, and surprisingly human-like. An objective is presented to the Swarm - this could be anything from conducting web-based research to producing a report or analyzing a complex dataset. The Swarm, with its innate ability to decompose tasks, breaks the objective down into bite-sized components. Each of these components is then dispatched to a worker agent, equipped with the tools and capabilities to tackle it. - -These agents are not mere receivers of instructions. They are sophisticated, multimodal, AI entities capable of browsing the web, ingesting and understanding data, interacting with digital interfaces, and even spawning additional worker agents when necessary. These agents carry out their assigned tasks autonomously, converging their efforts towards the overall objective. - -In practical terms, Swarms is as versatile as it is powerful. For a business seeking to automate its customer support system, Swarms could manage incoming queries, distribute them amongst the worker agents, and generate appropriate responses based on the customer's needs. In a research context, Swarms could ingest large volumes of data, identify key areas of interest, and provide comprehensive analyses, all without human intervention. - -What sets Swarms apart is its ability to harness the power of collective intelligence, the same principle that allows a flock of birds to move in unison or a colony of ants to construct complex structures. By enabling AI agents to cooperate in this way, Swarms isn't just pushing the boundaries of what AI can do – it's redefining them. And it all starts with a simple 'pip install swarms' or 'git clone' from our GitHub repository. Welcome to the age of cooperative AI. diff --git a/docs/old-docs/corp/MISSION.md b/docs/old-docs/corp/MISSION.md deleted file mode 100644 index c287a0b5..00000000 --- a/docs/old-docs/corp/MISSION.md +++ /dev/null @@ -1,149 +0,0 @@ -# Bounty Program - -Our bounty program is an exciting opportunity for contributors to help us build the future of Swarms. By participating, you can earn rewards while contributing to a project that aims to revolutionize digital activity. - -Here's how it works: - -1. **Check out our Roadmap**: We've shared our roadmap detailing our short and long-term goals. These are the areas where we're seeking contributions. - -2. **Pick a Task**: Choose a task from the roadmap that aligns with your skills and interests. If you're unsure, you can reach out to our team for guidance. - -3. **Get to Work**: Once you've chosen a task, start working on it. Remember, quality is key. We're looking for contributions that truly make a difference. - -4. **Submit your Contribution**: Once your work is complete, submit it for review. We'll evaluate your contribution based on its quality, relevance, and the value it brings to Swarms. - -5. **Earn Rewards**: If your contribution is approved, you'll earn a bounty. The amount of the bounty depends on the complexity of the task, the quality of your work, and the value it brings to Swarms. - -## The Three Phases of Our Bounty Program - -### Phase 1: Building the Foundation -In the first phase, our focus is on building the basic infrastructure of Swarms. This includes developing key components like the Swarms class, integrating essential tools, and establishing task completion and evaluation logic. We'll also start developing our testing and evaluation framework during this phase. If you're interested in foundational work and have a knack for building robust, scalable systems, this phase is for you. - -### Phase 2: Enhancing the System -In the second phase, we'll focus on enhancing Swarms by integrating more advanced features, improving the system's efficiency, and refining our testing and evaluation framework. This phase involves more complex tasks, so if you enjoy tackling challenging problems and contributing to the development of innovative features, this is the phase for you. - -### Phase 3: Towards Super-Intelligence -The third phase of our bounty program is the most exciting - this is where we aim to achieve super-intelligence. In this phase, we'll be working on improving the swarm's capabilities, expanding its skills, and fine-tuning the system based on real-world testing and feedback. If you're excited about the future of AI and want to contribute to a project that could potentially transform the digital world, this is the phase for you. - -Remember, our roadmap is a guide, and we encourage you to bring your own ideas and creativity to the table. We believe that every contribution, no matter how small, can make a difference. So join us on this exciting journey and help us create the future of Swarms. - -**To participate in our bounty program, visit the [Swarms Bounty Program Page](https://swarms.ai/bounty).** Let's build the future together! - - - - - -## Bounties for Roadmap Items - -To accelerate the development of Swarms and to encourage more contributors to join our journey towards automating every digital activity in existence, we are announcing a Bounty Program for specific roadmap items. Each bounty will be rewarded based on the complexity and importance of the task. Below are the items available for bounty: - -1. **Multi-Agent Debate Integration**: $2000 -2. **Meta Prompting Integration**: $1500 -3. **Swarms Class**: $1500 -4. **Integration of Additional Tools**: $1000 -5. **Task Completion and Evaluation Logic**: $2000 -6. **Ocean Integration**: $2500 -7. **Improved Communication**: $2000 -8. **Testing and Evaluation**: $1500 -9. **Worker Swarm Class**: $2000 -10. **Documentation**: $500 - -For each bounty task, there will be a strict evaluation process to ensure the quality of the contribution. This process includes a thorough review of the code and extensive testing to ensure it meets our standards. - -# 3-Phase Testing Framework - -To ensure the quality and efficiency of the Swarm, we will introduce a 3-phase testing framework which will also serve as our evaluation criteria for each of the bounty tasks. - -## Phase 1: Unit Testing -In this phase, individual modules will be tested to ensure that they work correctly in isolation. Unit tests will be designed for all functions and methods, with an emphasis on edge cases. - -## Phase 2: Integration Testing -After passing unit tests, we will test the integration of different modules to ensure they work correctly together. This phase will also test the interoperability of the Swarm with external systems and libraries. - -## Phase 3: Benchmarking & Stress Testing -In the final phase, we will perform benchmarking and stress tests. We'll push the limits of the Swarm under extreme conditions to ensure it performs well in real-world scenarios. This phase will measure the performance, speed, and scalability of the Swarm under high load conditions. - -By following this 3-phase testing framework, we aim to develop a reliable, high-performing, and scalable Swarm that can automate all digital activities. - -# Reverse Engineering to Reach Phase 3 - -To reach the Phase 3 level, we need to reverse engineer the tasks we need to complete. Here's an example of what this might look like: - -1. **Set Clear Expectations**: Define what success looks like for each task. Be clear about the outputs and outcomes we expect. This will guide our testing and development efforts. - -2. **Develop Testing Scenarios**: Create a comprehensive list of testing scenarios that cover both common and edge cases. This will help us ensure that our Swarm can handle a wide range of situations. - -3. **Write Test Cases**: For each scenario, write detailed test cases that outline the exact steps to be followed, the inputs to be used, and the expected outputs. - -4. **Execute the Tests**: Run the test cases on our Swarm, making note of any issues or bugs that arise. - -5. **Iterate and Improve**: Based on the results of our tests, iterate and improve our Swarm. This may involve fixing bugs, optimizing code, or redesigning parts of our system. - -6. **Repeat**: Repeat this process until our Swarm meets our expectations and passes all test cases. - -By following these steps, we will systematically build, test, and improve our Swarm until it reaches the Phase 3 level. This methodical approach will help us ensure that we create a reliable, high-performing, and scalable Swarm that can truly automate all digital activities. - -Let's shape the future of digital automation together! - - --------------------- -# Super-Intelligence Roadmap - -Creating a Super-Intelligent Swarm involves three main phases, where each phase has multiple sub-stages, each of which will require rigorous testing and evaluation to ensure progress towards super-intelligence. - -## Phase 1: Narrow Intelligence - -In this phase, the goal is to achieve high performance in specific tasks. These tasks will be predefined and the swarm will be trained and tested on these tasks. - -1. **Single Task Mastery**: Focus on mastering one task at a time. This can range from simple tasks like image recognition to complex tasks like natural language processing. - -2. **Task Switching**: Train the swarm to switch between different tasks effectively. This includes being able to stop one task and start another one without any loss in performance. - -3. **Multi-tasking**: The swarm should be capable of performing multiple tasks simultaneously without any degradation in performance. - -## Phase 2: General Intelligence - -In this phase, the swarm will be trained to handle a variety of tasks that were not part of the original training set. - -1. **Transfer Learning**: The swarm should be able to transfer knowledge learned in one context to another context. This means being able to apply knowledge learned in one task to a different but related task. - -2. **Adaptive Learning**: The swarm should be capable of adapting its learning strategies based on the task at hand. This includes being able to adjust its learning rate, exploration vs exploitation balance, etc. - -3. **Self-Learning**: The swarm should be able to learn new tasks on its own without any external guidance. This includes being able to understand the task requirements, find relevant information, learn the task, and evaluate its performance. - -## Phase 3: Super Intelligence - -In this phase, the swarm will surpass human-level performance in most economically valuable work. This involves the swarm being able to solve complex real-world problems, make accurate predictions, and generate innovative solutions. - -1. **Complex Problem Solving**: The swarm should be able to solve complex real-world problems. This includes being able to understand the problem, identify relevant information, generate solutions, evaluate the solutions, and implement the best solution. - -2. **Predictive Abilities**: The swarm should be able to make accurate predictions about future events based on past data. This includes being able to understand the data, identify relevant patterns, make accurate predictions, and evaluate the accuracy of its predictions. - -3. **Innovation**: The swarm should be able to generate innovative solutions to problems. This includes being able to think creatively, generate novel ideas, evaluate the ideas, and implement the best idea. - -4. **Self-improvement**: The swarm should be capable of improving its own capabilities. This includes being able to identify areas of weakness, find ways to improve, and implement the improvements. - -5. **Understanding**: The swarm should be able to understand complex concepts, make inferences, and draw conclusions. This includes being able to understand natural language, reason logically, and make sound judgments. - -Each of these stages will require extensive testing and evaluation to ensure progress towards super-intelligence. - -# Reverse-Engineering Super-Intelligence - -To reach the Phase 3 level of super-intelligence, we need to reverse engineer the tasks that need to be completed. Here's an outline of what this might look like: - -1. **Setting Success Metrics**: For each stage, define clear success metrics. These metrics should be quantitative and measurable, and they should align with the objectives of the stage. - -2. **Identifying Prerequisites**: Determine what needs to be in place before each stage can begin. This could include certain capabilities, resources, or technologies. - -3. **Developing Training Programs**: For each stage, develop a comprehensive training program. This should include a variety of tasks that will challenge the swarm and push it to - - develop the necessary capabilities. - -4. **Creating Testing Protocols**: Develop rigorous testing protocols for each stage. These protocols should test all aspects of the swarm's performance and they should be designed to push the swarm to its limits. - -5. **Iterating and Improving**: Based on the results of the tests, iterate and improve the swarm. This could involve adjusting the training program, modifying the swarm's architecture, or tweaking its learning algorithms. - -6. **Moving to the Next Stage**: Once the swarm has met the success metrics for a stage, it can move on to the next stage. This process continues until the swarm has reached the level of super-intelligence. - -This process will require a significant amount of time, resources, and effort. However, by following this structured approach, we can systematically guide the swarm towards super-intelligence. - diff --git a/docs/old-docs/corp/MONETIZATION.md b/docs/old-docs/corp/MONETIZATION.md deleted file mode 100644 index a44eb966..00000000 --- a/docs/old-docs/corp/MONETIZATION.md +++ /dev/null @@ -1,165 +0,0 @@ -# Swarms Monetization Strategy - -This strategy includes a variety of business models, potential revenue streams, cashflow structures, and customer identification methods. Let's explore these further. - -## Business Models - -1. **Platform as a Service (PaaS):** Provide the Swarms AI platform on a subscription basis, charged monthly or annually. This could be tiered based on usage and access to premium features. - -2. **API Usage-based Pricing:** Charge customers based on their usage of the Swarms API. The more requests made, the higher the fee. - -3. **Managed Services:** Offer complete end-to-end solutions where you manage the entire AI infrastructure for the clients. This could be on a contract basis with a recurring fee. - -4. **Training and Certification:** Provide Swarms AI training and certification programs for interested developers and businesses. These could be monetized as separate courses or subscription-based access. - -5. **Partnerships:** Collaborate with large enterprises and offer them dedicated Swarm AI services. These could be performance-based contracts, ensuring a mutually beneficial relationship. - -6. **Data as a Service (DaaS):** Leverage the data generated by Swarms for insights and analytics, providing valuable business intelligence to clients. - -## Potential Revenue Streams - -1. **Subscription Fees:** This would be the main revenue stream from providing the Swarms platform as a service. - -2. **Usage Fees:** Additional revenue can come from usage fees for businesses that have high demand for Swarms API. - -3. **Contract Fees:** From offering managed services and bespoke solutions to businesses. - -4. **Training Fees:** Revenue from providing training and certification programs to developers and businesses. - -5. **Partnership Contracts:** Large-scale projects with enterprises, involving dedicated Swarm AI services, could provide substantial income. - -6. **Data Insights:** Revenue from selling valuable business intelligence derived from Swarm's aggregated and anonymized data. - -## Potential Customers - -1. **Businesses Across Sectors:** Any business seeking to leverage AI for automation, efficiency, and data insights could be a potential customer. This includes sectors like finance, eCommerce, logistics, healthcare, and more. - -2. **Developers:** Both freelance and those working in organizations could use Swarms to enhance their projects and services. - -3. **Enterprises:** Large enterprises looking to automate and optimize their operations could greatly benefit from Swarms. - -4. **Educational Institutions:** Universities and research institutions could leverage Swarms for research and teaching purposes. - -## Roadmap - -1. **Landing Page Creation:** Develop a dedicated product page on apac.ai for Swarms. - -2. **Hosted Swarms API:** Launch a cloud-based Swarms API service. It should be highly reliable, with robust documentation to attract daily users. - -3. **Consumer and Enterprise Subscription Service:** Launch a comprehensive subscription service on The Domain. This would provide users with access to a wide array of APIs and data streams. - -4. **Dedicated Capacity Deals:** Partner with large enterprises to offer them dedicated Swarm AI solutions for automating their operations. - -5. **Enterprise Partnerships:** Develop partnerships with large enterprises for extensive contract-based projects. - -6. **Integration with Collaboration Platforms:** Develop Swarms bots for platforms like Discord and Slack, charging users a subscription fee for access. - -7. **Personal Data Instances:** Offer users dedicated instances of all their data that the Swarm can query as needed. - -8. **Browser Extension:** Develop a browser extension that integrates with the Swarms platform, offering users a more seamless experience. - -Remember, customer satisfaction and a value-centric approach are at the core of any successful monetization strategy. It's essential to continuously iterate and improve the product based on customer feedback and evolving market needs. - - - - - - - - - - - - - - - -1. **Platform as a Service (PaaS):** Create a cloud-based platform that allows users to build, run, and manage applications without the complexity of maintaining the infrastructure. You could charge users a subscription fee for access to the platform and provide different pricing tiers based on usage levels. This could be an attractive solution for businesses that do not have the capacity to build or maintain their own swarm intelligence solutions. - -2. **Professional Services:** Offer consultancy and implementation services to businesses looking to utilize the Swarm technology. This could include assisting with integration into existing systems, offering custom development services, or helping customers to build specific solutions using the framework. - -3. **Education and Training:** Create a certification program for developers or companies looking to become proficient with the Swarms framework. This could be sold as standalone courses, or bundled with other services. - -4. **Managed Services:** Some companies may prefer to outsource the management of their Swarm-based systems. A managed services solution could take care of all the technical aspects, from hosting the solution to ensuring it runs smoothly, allowing the customer to focus on their core business. - -5. **Data Analysis and Insights:** Swarm intelligence can generate valuable data and insights. By anonymizing and aggregating this data, you could provide industry reports, trend analysis, and other valuable insights to businesses. - -As for the type of platform, Swarms can be offered as a cloud-based solution given its scalability and flexibility. This would also allow you to apply a SaaS/PaaS type monetization model, which provides recurring revenue. - -Potential customers could range from small to large enterprises in various sectors such as logistics, eCommerce, finance, and technology, who are interested in leveraging artificial intelligence and machine learning for complex problem solving, optimization, and decision-making. - -**Product Brief Monetization Strategy:** - -Product Name: Swarms.AI Platform - -Product Description: A cloud-based AI and ML platform harnessing the power of swarm intelligence. - -1. **Platform as a Service (PaaS):** Offer tiered subscription plans (Basic, Premium, Enterprise) to accommodate different usage levels and business sizes. - -2. **Professional Services:** Offer consultancy and custom development services to tailor the Swarms solution to the specific needs of the business. - -3. **Education and Training:** Launch an online Swarms.AI Academy with courses and certifications for developers and businesses. - -4. **Managed Services:** Provide a premium, fully-managed service offering that includes hosting, maintenance, and 24/7 support. - -5. **Data Analysis and Insights:** Offer industry reports and customized insights generated from aggregated and anonymized Swarm data. - -Potential Customers: Enterprises in sectors such as logistics, eCommerce, finance, and technology. This can be sold globally, provided there's an internet connection. - -Marketing Channels: Online marketing (SEO, Content Marketing, Social Media), Partnerships with tech companies, Direct Sales to Enterprises. - -This strategy is designed to provide multiple revenue streams, while ensuring the Swarms.AI platform is accessible and useful to a range of potential customers. - -1. **AI Solution as a Service:** By offering the Swarms framework as a service, businesses can access and utilize the power of multiple LLM agents without the need to maintain the infrastructure themselves. Subscription can be tiered based on usage and additional features. - -2. **Integration and Custom Development:** Offer integration services to businesses wanting to incorporate the Swarms framework into their existing systems. Also, you could provide custom development for businesses with specific needs not met by the standard framework. - -3. **Training and Certification:** Develop an educational platform offering courses, webinars, and certifications on using the Swarms framework. This can serve both developers seeking to broaden their skills and businesses aiming to train their in-house teams. - -4. **Managed Swarms Solutions:** For businesses that prefer to outsource their AI needs, provide a complete solution which includes the development, maintenance, and continuous improvement of swarms-based applications. - -5. **Data Analytics Services:** Leveraging the aggregated insights from the AI swarms, you could offer data analytics services. Businesses can use these insights to make informed decisions and predictions. - -**Type of Platform:** - -Cloud-based platform or Software as a Service (SaaS) will be a suitable model. It offers accessibility, scalability, and ease of updates. - -**Target Customers:** - -The technology can be beneficial for businesses across sectors like eCommerce, technology, logistics, finance, healthcare, and education, among others. - -**Product Brief Monetization Strategy:** - -Product Name: Swarms.AI - -1. **AI Solution as a Service:** Offer different tiered subscriptions (Standard, Premium, and Enterprise) each with varying levels of usage and features. - -2. **Integration and Custom Development:** Offer custom development and integration services, priced based on the scope and complexity of the project. - -3. **Training and Certification:** Launch the Swarms.AI Academy with courses and certifications, available for a fee. - -4. **Managed Swarms Solutions:** Offer fully managed solutions tailored to business needs, priced based on scope and service level agreements. - -5. **Data Analytics Services:** Provide insightful reports and data analyses, which can be purchased on a one-off basis or through a subscription. - -By offering a variety of services and payment models, Swarms.AI will be able to cater to a diverse range of business needs, from small start-ups to large enterprises. Marketing channels would include digital marketing, partnerships with technology companies, presence in tech events, and direct sales to targeted industries. - - - -# Roadmap - -* Create a landing page for swarms apac.ai/product/swarms - -* Create Hosted Swarms API for anybody to just use without need for mega gpu infra, charge usage based pricing. Prerequisites for success => Swarms has to be extremely reliable + we need world class documentation and many daily users => how do we get many daily users? We provide a seamless and fluid experience, how do we create a seamless and fluid experience? We write good code that is modular, provides feedback to the user in times of distress, and ultimately accomplishes the user's tasks. - -* Hosted consumer and enterprise subscription as a service on The Domain, where users can interact with 1000s of APIs and ingest 1000s of different data streams. - -* Hosted dedicated capacity deals with mega enterprises on automating many operations with Swarms for monthly subscription 300,000+$ - -* Partnerships with enterprises, massive contracts with performance based fee - -* Have discord bot and or slack bot with users personal data, charge subscription + browser extension - -* each user gets a dedicated ocean instance of all their data so the swarm can query it as needed. - -* \ No newline at end of file diff --git a/docs/old-docs/corp/PITCH.md b/docs/old-docs/corp/PITCH.md deleted file mode 100644 index 14381b50..00000000 --- a/docs/old-docs/corp/PITCH.md +++ /dev/null @@ -1,14 +0,0 @@ - -## Purpose -Artificial Intelligence has grown at an exponential rate over the past decade. Yet, we are far from fully harnessing its potential. Today's AI operates in isolation, each working separately in their corner. But life doesn't work like that. The world doesn't work like that. Success isn't built in silos; it's built in teams. - -Imagine a world where AI models work in unison. Where they can collaborate, interact, and pool their collective intelligence to achieve more than any single model could. This is the future we envision. But today, we lack a framework for AI to collaborate effectively, to form a true swarm of intelligent agents. - - -This is a difficult problem, one that has eluded solution. It requires sophisticated systems that can allow individual models to not just communicate but also understand each other, pool knowledge and resources, and create collective intelligence. This is the next frontier of AI. - -But here at Swarms, we have a secret sauce. It's not just a technology or a breakthrough invention. It's a way of thinking - the philosophy of rapid iteration. With each cycle, we make massive progress. We experiment, we learn, and we grow. We have developed a pioneering framework that can enable AI models to work together as a swarm, combining their strengths to create richer, more powerful outputs. - -We are uniquely positioned to take on this challenge with 1,500+ devoted researchers in Agora. We have assembled a team of world-class experts, experienced and driven, united by a shared vision. Our commitment to breaking barriers, pushing boundaries, and our belief in the power of collective intelligence makes us the best team to usher in this future to fundamentally advance our species, Humanity. - ---- \ No newline at end of file diff --git a/docs/old-docs/corp/ROADMAP.md b/docs/old-docs/corp/ROADMAP.md deleted file mode 100644 index 87a80c6d..00000000 --- a/docs/old-docs/corp/ROADMAP.md +++ /dev/null @@ -1,115 +0,0 @@ -## The Plan - -### Phase 1: Building the Foundation -In the first phase, our focus is on building the basic infrastructure of Swarms. This includes developing key components like the Swarms class, integrating essential tools, and establishing task completion and evaluation logic. We'll also start developing our testing and evaluation framework during this phase. If you're interested in foundational work and have a knack for building robust, scalable systems, this phase is for you. - -### Phase 2: Optimizing the System -In the second phase, we'll focus on optimizng Swarms by integrating more advanced features, improving the system's efficiency, and refining our testing and evaluation framework. This phase involves more complex tasks, so if you enjoy tackling challenging problems and contributing to the development of innovative features, this is the phase for you. - -### Phase 3: Towards Super-Intelligence -The third phase of our bounty program is the most exciting - this is where we aim to achieve super-intelligence. In this phase, we'll be working on improving the swarm's capabilities, expanding its skills, and fine-tuning the system based on real-world testing and feedback. If you're excited about the future of AI and want to contribute to a project that could potentially transform the digital world, this is the phase for you. - -Remember, our roadmap is a guide, and we encourage you to bring your own ideas and creativity to the table. We believe that every contribution, no matter how small, can make a difference. So join us on this exciting journey and help us create the future of Swarms. - - -## Optimization Priorities - -1. **Reliability**: Increase the reliability of the swarm - obtaining the desired output with a basic and un-detailed input. - -2. **Speed**: Reduce the time it takes for the swarm to accomplish tasks by improving the communication layer, critiquing, and self-alignment with meta prompting. - -3. **Scalability**: Ensure that the system is asynchronous, concurrent, and self-healing to support scalability. - -Our goal is to continuously improve Swarms by following this roadmap, while also being adaptable to new needs and opportunities as they arise. - -# Open Source Roadmap - -Here is the detailed roadmap of our priorities and planned features for the near term: - -## TODO - -* Create a non-langchain worker and swarm class and compare - -* Create extensive documentation - -* Make sure that the boss agent successfully calls the worker agent if when it's finished makinng a plan - -* Make sure the worker agent can access tools like web browser, terminal, and code editor, and multi-modal agents - -* Make sure inputs and outputs from boss to worker are well defined and are collaborating if not then readjust prompt - -* Create a tool that creates other tools with access to write code, debug, and an architectural argent that creates the architecture and then another agent that creates the code[Architecter(with code examples), code generator (with access to writing code and terminalrools)] -- The Compiler? - -* Create a screenshot tool that takes a screen shot and then passes it to a worker multi-modal agent for visual context. - -* API endroute in FASTAPI - -* Develop Conversational UI with Gradio - -* Integrate omni agent as a worker tool - -* Integrate Ocean Database as primary vectorstore - -* Integrate visual agent - -* Integrate quantized hf models as base models with langchain huggingface - -1. **Multi-Agent Debate Integration**: Integrate multi-agent debate frameworks ([Multi Agent debate](https://github.com/Skytliang/Multi-Agents-Debate) and [Multi agent2 debate](https://github.com/composable-models/llm_multiagent_debate)) to improve decision-making. - -2. **Meta Prompting Integration**: Include meta prompting across all worker agents to guide their actions. - -3. **Swarms Class**: Create a main swarms class `swarms('Increase sales by 40$', workers=4)` for managing and coordinating multiple worker nodes. - -4. **Integration of Additional Tools**: Integrate [Jarvis](https://github.com/microsoft/JARVIS) as worker nodes, add text to speech and text to script tools ([whisper x](https://github.com/kyegomez/youtubeURL-to-text)), and integrate Hugging Face agents and other external tools. - -5. **Task Completion and Evaluation Logic**: Include task completion logic with meta prompting, and evaluate task completion on a scale from 0.0 to 1.0. - -7. **Ocean Integration**: Use the [Ocean](https://github.com/kyegomez/Ocean) vector database as the main embedding database for all the agents, both boss and worker. - -8. **Improved Communication**: Develop a universal vector database that is only used when a task is completed in this format `[TASK][COMPLETED]`. - -9. **Testing and Evaluation**: Create unit tests, benchmarks, and evaluations for performance monitoring and continuous improvement. - -10. **Worker Swarm Class**: Create a class for self-scaling worker swarms. If they need help, they can spawn an entirely new worker and more workers if needed. - -## Documentation - -1. **Examples**: Create extensive and useful examples for a variety of use cases. - -2. **README**: Update the README to include the examples and usage instructions. - - -# Mid-Long term -Here are some potential middle-to-long-term improvements to consider for this project: - -1. **Modular Design**: Aim to design a more modular and scalable framework, making it easy for developers to plug-and-play various components. - -2. **Interactive User Interface**: Develop a more interactive, user-friendly GUI that allows users to interact with the system without needing to understand the underlying code. - -3. **Advanced Error Handling**: Implement advanced error handling and debugging capabilities to make it easier for developers to diagnose and fix issues. - -4. **Optimized Resource Utilization**: Improve the efficiency of resource use, aiming to reduce memory consumption and improve speed without sacrificing accuracy. - -5. **Collaborative Learning**: Integrate more sophisticated techniques for collaborative learning among the swarm, allowing them to share knowledge and learn from each other's successes and failures. - -6. **Autonomous Self-Improvement**: Implement mechanisms that allow the swarm to autonomously learn from its past experiences and improve its performance over time. - -7. **Security Enhancements**: Include robust security measures to protect sensitive data and prevent unauthorized access. - -8. **Privacy-Preserving Techniques**: Consider incorporating privacy-preserving techniques such as differential privacy to ensure the confidentiality of user data. - -9. **Support for More Languages**: Expand language support to allow the system to cater to a more global audience. - -10. **Robustness and Resilience**: Improve the system's robustness and resilience, ensuring that it can operate effectively even in the face of hardware or software failures. - -11. **Continual Learning**: Implement continual learning techniques to allow the system to adapt and evolve as new data comes in. - -12. **More Contextual Understanding**: Enhance the system's capability to understand context better, making it more effective in handling real-world, complex tasks. - -13. **Dynamic Task Prioritization**: Develop advanced algorithms for dynamic task prioritization, ensuring that the most important tasks are addressed first. - -14. **Expanding the Swarm's Skills**: Train the swarm on a wider range of tasks, gradually expanding their skill set and problem-solving capabilities. - -15. **Real-World Deployment**: Test and refine the system in real-world settings, learning from these experiences to further improve and adapt the system. - -Remember, these are potential improvements. It's important to revisit your priorities regularly and adjust them based on project needs, feedback, and learning from both successes and failures. diff --git a/docs/old-docs/corp/SALES.md b/docs/old-docs/corp/SALES.md deleted file mode 100644 index 9f09a2cb..00000000 --- a/docs/old-docs/corp/SALES.md +++ /dev/null @@ -1,295 +0,0 @@ -# Sales Documentation - -## Small Businesses - -Small businesses often lack the resources to hire a dedicated team of data analysts and AI experts. This is where Swarms steps in. With our platform, these businesses can automate many of the tasks that previously required manual effort or expert knowledge. Our strategy for engaging small businesses involves showcasing the simplicity and cost-effectiveness of Swarms. - -### Stage 1: Awareness and Education -* Questions: Have you considered implementing AI solutions? Are you aware of how AI can help your business? -* Commitments: Schedule a demo of Swarms. - -### Stage 2: Engagement and Evaluation -* Questions: Have you identified areas where AI can improve efficiency? How do you currently manage these tasks? -* Commitments: Conduct a trial run of Swarms on a select project. - -### Stage 3: Adoption and Integration -* Questions: Are you satisfied with the results of the trial run? Are you ready to implement Swarms more broadly? -* Commitments: Purchase a subscription and begin the full-scale integration of Swarms. - -## Medium-Sized Enterprises - -Medium-sized enterprises typically have some level of AI integration but often struggle with scalability. Swarms can offer these organizations a path to seamlessly scale their existing AI capabilities. Our strategy for engaging medium-sized enterprises involves demonstrating how Swarms can take their current AI solutions to the next level. - -### Stage 1: Awareness and Potential Evaluation -* Questions: Are you looking to scale your current AI solutions? Are you satisfied with the performance of your current AI tools? -* Commitments: Arrange a personalized demonstration of Swarms. - -### Stage 2: Engagement and Testing -* Questions: Have you identified the specific areas where your AI solutions need scaling? Are you open to piloting Swarms in these areas? -* Commitments: Run a pilot program using Swarms. - -### Stage 3: Adoption and Expansion -* Questions: Did the pilot program meet your expectations? Are you ready to expand the use of Swarms across your organization? -* Commitments: Commit to a long-term partnership and full integration of Swarms. - -## Large Corporations - -Large corporations typically have extensive AI capabilities, but they may struggle with coordination and efficiency. Swarms can help streamline these organizations' AI operations. Our strategy for engaging large corporations involves demonstrating how Swarms can enhance the efficiency of their AI ecosystems. - -### Stage 1: Awareness and Relevance Evaluation -* Questions: Are you experiencing inefficiencies with your existing AI operations? Have you considered solutions to improve coordination among your AI tools? -* Commitments: Organize an executive briefing session about Swarms. - -### Stage 2: Engagement and Trial -* Questions: Can you identify specific use cases for Swarms in your organization? Are you willing to conduct a trial run of Swarms? -* Commitments: Implement a trial run of Swarms for selected use cases. - -### Stage 3: Adoption and Wide-Scale Implementation -* Questions: Was the trial run of Swarms successful? Are you ready to implement Swarms throughout your organization? -* Commitments: Form a strategic alliance and proceed with wide-scale implementation of Swarms. - -Remember, this is more than just a transaction. It's a partnership. And like any good partnership, it's built on trust, communication, and a shared vision. We're excited to embark on this journey with you, and we're committed to supporting you every step of the way. - - - -# SPIN SOP - -This is a detailed customer journey roadmap and Standard Operating Procedure for selling Swarms to businesses of varying sizes. The SPIN selling method (Situation, Problem, Implication, Need-payoff) will be incorporated throughout the document to provide a comprehensive approach. We'll explore a scenario that begins with cold emailing and culminates in offering theoretical consultation package deals. - -**1. Lead Generation and Cold Outreach** - -Our journey begins with identifying potential leads that could benefit from the capabilities of Swarms. This step involves researching potential clients based on their industry, size, digital footprint, and overall potential to benefit from AI automation. - -Upon identifying a potential client, initiate contact with a tailored cold email. This email should: - -- Grab the recipient's attention (Subject line is crucial) -- Introduce Swarms and its potential benefits (unique selling proposition) -- Propose a discovery call or meeting to discuss how Swarms could be beneficial - -An example could be: - -*Subject: Elevate Your Business with the Power of AI Swarm Technology* - -Dear [Recipient's Name], - -I represent Agora, a pioneer in the field of cooperative AI. Our latest innovation, Swarms, harnesses the power of AI to drive efficiency, streamline operations, and ultimately boost profitability. I would love to have a conversation with you to discuss how this cutting-edge technology can specifically help [Their Company Name]. - -Are you available for a quick call next week? - -Best regards, -[Your Name] - -**2. Discovery Call** - -The aim of the discovery call is to learn about the potential client's business and identify their needs using the SPIN selling method: - -*SITUATION* - Get an understanding of the client's current operations, their use of technology, and their overall business landscape. - -*PROBLEM* - Identify any potential areas where the client might be facing challenges - these could be inefficiencies, high operating costs, or unmet customer needs. - -*IMPLICATION* - Discuss the consequences of these problems, such as reduced profitability or customer dissatisfaction. - -*NEED-PAYOFF* - Finally, demonstrate how Swarms can address these issues and the benefits it will bring to their business. - -**3. Follow-Up and Tailored Proposal** - -After gaining a deeper understanding of the client's needs, follow up with a detailed proposal that outlines how Swarms can specifically address their challenges. The proposal should highlight: - -- How Swarms fits into their current operations -- A projection of improvements and potential return on investment -- The steps involved in the implementation process - -**4. Theoretical Consultation Packages** - -Introduce consultation packages at this stage to provide further value and assure the client of continued support during the Swarms integration journey. The packages could include: - -- *Swarms Starter Pack*: Ideal for small businesses. Includes initial setup and integration, basic training, and a month of technical support. -- *Swarms Business Pack*: Suited for medium-sized businesses. Offers advanced setup, employee training, a dedicated support manager, and three months of technical support. -- *Swarms Enterprise Pack*: For large corporations. Includes customized setup and integration, extensive training, a dedicated account manager, and six months of priority technical support. - -**5. Demonstration and Commitment** - -Offer a demonstration to show Swarms in action. This could be a simulated use-case relevant to the client's industry or a walk-through of the platform. - -Post-demonstration, ask for the client's commitment to move to the next step. This could be a meeting with other decision-makers, an agreement to a trial period, or a signed contract. - -**6. Implementation and Onboarding** - -After gaining the client's commitment, the next stage involves the implementation of Swarms in their operations. This will depend on the client's size, the complexity of their operations, and the specifics agreed upon in the proposal. - -**7. Continued Support and Review** - -Continued technical support is essential. Regularly check in with the client, ensure they are getting the most out of Swarms, and address any issues promptly. It's also important to review the impact of Swarms on the client's operations after a set period and adjust the strategy as necessary. - -Selling Swarms is not about pushing a product; it's about offering a solution that can revolutionize businesses. The journey from cold emailing to a fully-fledged partnership should reflect this philosophy at every stage. - - -# Tactics - - -# Value proposition Formula -``` -Dream outcome • Perceived Likelihood -------------------------------------- -Time Delay * Effort & Sacrifice -``` - -Where: - -#### Maximize Value Using Value Equation -❏ Maximize Dream Outcome (solve problem worth solving) - -❏ Maximize Perceived Likelihood of Success (testimonials& proven case studies) - -❏ Minimize Time to Success (How can we make this faster? How can we show progress?) - -❏ Minimize Effort & Sacrifice (How can we make this easier? More Seamless? Convenient?) - -### Swarms Examples - -### Swarms Value Proposition - -"Leap into a new era of productivity with Swarms. Automate up to 50% of your business tasks with the power of cooperative AI, a proven solution trusted by thousands worldwide. With an easy integration process, your dream of seamless automation is just a few steps away." - -Where: - -- The dream outcome is achieving a significant boost in efficiency and productivity by automating 30-50% of your daily business tasks with Swarms. - -- The perceived likelihood of success is backed by our strong track record, with thousands of successful implementations and satisfied customers globally. - -- We've minimized the time delay to success. Swarms provides quick and painless onboarding, with step-by-step support to ensure smooth integration into your existing workflow. - -- The effort and sacrifice required is significantly less than traditional methods. Swarms is designed for ease of use, requiring minimal technical know-how. Plus, our dedicated support team is always at hand to ensure a seamless experience. - -In essence, Swarms makes the dream of comprehensive business automation an attainable reality. Join thousands of our active users in harnessing the power of cooperative AI, and transform your business operations with us today. - -### Value prop SWARMS -``` -We've helped thousands of people just like you automate 30% of their activities with Swarms. And, all it takes to get started is a fast simple onboarding agent that asks you to integrate your tools and datasources. -``` - -In today's competitive landscape, organizations of all sizes are continually seeking ways to automate routine tasks, streamline processes, and make data-driven decisions. Enter Swarms, a revolutionary AI-based technology that leverages the power of multiple autonomous agents to perform tasks with unprecedented speed and accuracy. - -This guide lays out a SPIN (Situation, Problem, Implication, Need-payoff) approach to selling Swarms, taking you through a step-by-step journey from cold outreach to closing the deal. - -#2 -Cold Outreach - -Our first step is to generate interest in Swarms, and we do this through personalized, value-driven outreach. Focus on how Swarms can solve their pain points and bring value to their organization. - -Situation Questions: -- Do you currently use any AI or machine learning tools in your organization? -- How are you managing tasks that could be automated or require large-scale data analysis? - -Problem Questions: -- Are there any specific challenges in managing these tasks manually or using traditional AI models? -- How much time and resources are you currently dedicating to these tasks? - -Implication Questions: -- What is the cost of not addressing these challenges or improving these processes? -- How does this affect your team’s productivity, your operational efficiency, or your competitive advantage? - -Need-payoff Questions: -- How would your organization benefit from automating these tasks or making them more efficient? -- Could an AI-based tool that leverages the power of multiple autonomous agents be beneficial for your organization? - -#3 -Discovery Calls - -Once you've generated interest and scheduled a discovery call, dive deeper into their business operations, their pain points, and their goals. Establish a clear understanding of what they need and how Swarms can fulfill those needs. - -Situation Questions: -- Could you tell me more about your current workflows and operational processes? -- What is the biggest challenge your team is facing in managing these workflows? - -Problem Questions: -- Have you ever encountered situations where the scale or complexity of tasks was overwhelming for your existing systems? -- Are there any tasks that you believe require a level of intelligence or speed that your current solutions can’t provide? - -Implication Questions: -- How does this affect your growth, competitiveness, or profitability in the long term? -- What are the ripple effects of these challenges on other aspects of your business? - -Need-payoff Questions: -- Would a solution that can handle tasks of any scale or complexity efficiently and accurately be of value to your team? -- How would such a solution impact your operational efficiency, team productivity, and bottom line? - -#4 -Product Demonstration - -This is the stage where you showcase the capabilities of Swarms, demonstrating its features and how it can be applied to their specific use cases. Show, don't tell. - -Situation Questions: -- Can you share a few tasks that you believe could be significantly improved with automation or intelligent processing? -- What features or functionalities are you looking for in a solution to improve these tasks? - -Problem Questions: -- Are there any specific issues that you expect to arise if these tasks are managed with your current systems? -- Have past solutions failed to deliver on your expectations in any way? - -Implication Questions: -- What are the potential consequences if these issues are not addressed or if the tasks are not improved? -- How does this affect your team’s morale, your customer satisfaction, or your market position? - -Need-payoff Questions: -- Would you be interested in a solution that can automate these tasks, provide intelligent processing, and scale according to your needs? -- How would such a solution change the way your team works and the outcomes they achieve? - -#5 -Proposal and Negotiation - -Once they've seen Swarms in action, it's time to present a tailored proposal that highlights the value of Swarms for their organization. Always be ready to negotiate, but remember, the focus is on value, not price. - -Situation Questions: -- What does your budget look like for a solution like Swarms? -- What are the key factors you'll consider in making your decision? - -Problem Questions: -- Are there any concerns or roadblocks that you think might prevent you from moving forward with Swarms? -- Have budget constraints or cost issues affected your ability to implement effective solutions in the past? - -Implication Questions: -- If cost or resource constraints continue to limit your ability to implement effective solutions, how will this impact your organization in the long term? -- Are you prepared to deal with the ramifications of continued inefficiencies or challenges? - -Need-payoff Questions: -- How would investing in Swarms impact your budget compared to the potential return on investment? -- How much value do you place on a solution that can transform the way you manage tasks, improve efficiency, and drive growth? - -#6 -Closing the Deal - -Closing the deal is about more than just signing a contract. It’s about setting the stage for a long-term partnership, ensuring they see the value in Swarms not just as a product, but as a key part of their business strategy. - -Situation Questions: -- Are you ready to move forward with implementing Swarms in your organization? -- What expectations do you have from Swarms in the initial phase? - -Problem Questions: -- Are there any final concerns or questions you have that could prevent us from moving forward? -- Is there anything that’s still unclear about how Swarms works or the value it can bring to your organization? - -Implication Questions: -- If these concerns or uncertainties are not addressed, how will it affect your decision? -- Are you willing to overlook the potential value Swarms could bring due to these concerns? - -Need-payoff Questions: -- How can we address these concerns to make Swarms a part of your organization's growth story? -- Can we agree on the fact that Swarms, with its unique capabilities, could significantly boost your organization's operational efficiency and competitiveness? - -#7 -Consultation Packages - -As part of our commitment to ensure our clients extract the maximum value from Swarms, we offer several consultation packages. These packages are designed to provide continuous support as you integrate Swarms into your workflows and processes, helping you overcome any challenges and optimize the system for your specific needs. - -Package 1 - Initial Setup & Training: Our team of experts will assist you in setting up Swarms, train your team on its functionalities and features, and provide support as you start to use the system. - -Package 2 - Optimization & Fine-tuning: As you use Swarms, we'll work closely with you to optimize the system for your specific tasks and workflows, ensuring you extract the maximum value from the platform. - -Package 3 - Ongoing Support & Upgrades: We provide continuous support to address any challenges you encounter and ensure you always have access to the - - latest upgrades and improvements to Swarms. - -Remember, Swarms isn't just a product; it's a partnership. We're committed to working with you every step of the way, ensuring you harness the full power of cooperative AI to transform your organization. - - diff --git a/docs/old-docs/corp/SALESPEOPLE_PLAN.md b/docs/old-docs/corp/SALESPEOPLE_PLAN.md deleted file mode 100644 index e5c3fe40..00000000 --- a/docs/old-docs/corp/SALESPEOPLE_PLAN.md +++ /dev/null @@ -1,143 +0,0 @@ -# **Open Source Freelancer Salespeople Recruitment Plan** - -Here is a strategic plan to attract open-source freelancer salespeople to Swarms. - -1. **Promote the Vision**: A compelling vision is the cornerstone of any recruitment strategy. Share the vision and mission of Swarms – its potential to revolutionize AI and digital automation – on every possible platform. The goal is to attract freelancers who are excited about the potential of AI and are eager to be part of this revolution. - -2. **Compensation Structure**: Offer a highly competitive, commission-based compensation structure. This could include a base rate for each sale, as well as performance-based bonuses and incentives for high-performing salespeople. Make it clear that the better they do, the more they earn. - -3. **Comprehensive Training**: Ensure all salespeople receive comprehensive training about Swarms, its capabilities, and the potential benefits it can offer to businesses. The more knowledgeable they are about the product, the better they can sell it. - -4. **Collaborative Community**: Foster a community of open-source freelancer salespeople. This community will provide a platform for salespeople to exchange ideas, share success stories, and learn from each other. Foster a culture of collaboration and continuous learning. - -5. **Clear Communication**: Be clear about expectations, targets, and performance metrics. Provide regular feedback and recognition to keep salespeople motivated and aligned with the company's goals. - -6. **Sales Tools & Resources**: Equip salespeople with the necessary tools and resources they need to sell effectively. This might include sales scripts, customer personas, case studies, product demos, and any other material that can aid them in their sales efforts. - -7. **Marketing Support**: In parallel to sales efforts, invest in marketing initiatives to build brand awareness and generate leads. The higher the brand visibility, the easier it will be for salespeople to sell the product. - -8. **Advocate Program**: Introduce an advocate program where salespeople get additional rewards for bringing in more salespeople. This will not only increase the sales force but also instill a sense of ownership and involvement among salespeople. - -**Flywheel Research Diagram** - -Building a flywheel involves understanding and leveraging the compounding effect of a circular, connected process, where each stage fuels the next. Here's a conceptualization of a Swarms Sales Flywheel: - -1. **Training & Empowerment**: Start by attracting freelance salespeople and providing comprehensive training and resources. As salespeople gain competence, they become better at selling Swarms. - -2. **Sales**: As sales increase, so do the visibility of Swarms and the earnings of the salespeople. This attracts more clients and more potential salespeople. - -3. **Client Success**: Successful clients serve as testimonials and case studies, helping to validate the product and making it easier for salespeople to sell Swarms. Success stories and increased visibility generate more interest among potential salespeople. - -4. **Community & Advocacy**: A growing community of successful salespeople, satisfied clients, and a strong product fuels advocacy. Salespeople are more likely to recommend the opportunity to other potential salespeople. - -5. **Growth**: As the community and client base grow, so do the opportunities for salespeople. Increased earnings and a stronger product reputation attract more salespeople, turning the flywheel faster. - -6. **Back to Training & Empowerment**: The increased interest from potential salespeople leads back to the first stage of the flywheel – training and empowering more salespeople. - -The key to the flywheel's momentum is ensuring each stage is performed effectively, creating a virtuous cycle that builds momentum over time. It relies on customer success, salesperson success, and product success, all fueling each other to keep the flywheel spinning. - - - -# Risks and Mitigations -Embracing an open source salesforce represents an innovative approach and can have significant benefits, including scalability, diversity, and cost-effectiveness. However, there are potential risks that need to be considered and mitigated. Here they are, along with strategies for managing them: - -**1. Brand Representation:** In an open source sales model, you can't control who represents your brand, as anyone can essentially become a salesperson. This can pose a risk if salespeople don't present the product or brand accurately, or don't uphold company values in their interactions. - - *Mitigation Strategy:* Provide clear brand guidelines, sales training, and codes of conduct that salespeople must adhere to. Regular monitoring and feedback can help ensure compliance. Also, introduce a certification process to officially recognize salespeople who demonstrate their understanding of the product and brand. - -**2. Product Misrepresentation:** Salespeople may overpromise or misrepresent the product to close a deal, leading to customer dissatisfaction and damage to the brand. - - *Mitigation Strategy:* Ensure that comprehensive and accurate product information is readily available. Provide clear guidelines on what can and cannot be promised to customers. Regularly update salespeople on product developments so their knowledge remains accurate. - -**3. Variable Quality of Salespeople:** In an open-source model, the quality of salespeople can vary widely, which may lead to inconsistent customer experiences. - - *Mitigation Strategy:* Offer comprehensive training and provide sales scripts or guidelines to ensure a uniform sales approach. Monitor performance and provide feedback to help salespeople improve. - -**4. Competition and Infighting:** Salespeople may compete with each other for the same leads, causing conflicts and damaging team cohesion. - - *Mitigation Strategy:* Create a clear system for lead assignment and territory division to prevent overlaps. Promote a collaborative rather than competitive culture, emphasizing the collective success of the team over individual achievements. - -**5. Data Security and Privacy:** With more individuals having access to company and customer information, the risk of data breaches increases. - - *Mitigation Strategy:* Provide training on data security and privacy policies. Use secure systems for data access and sharing. Regularly audit and monitor data access to detect any potential breaches. - -**6. Lack of Control:** The company may have less control over an open-source salesforce compared to an in-house team, leading to potential inconsistencies and inefficiencies. - - *Mitigation Strategy:* Regular communication and feedback are crucial. Use a performance management system to set expectations, track progress, and identify areas for improvement. - -Ultimately, the key is to adopt a long-term perspective, just like Jeff Bezos. Invest in training and building relationships with the salespeople. Foster a culture of customer obsession, and instill a sense of ownership and responsibility in the salespeople. Just as with any other risk, these can be mitigated with careful planning, continuous monitoring, and regular feedback. - - - - - - -# Open Source Salesperson Onboarding Experience - -Creating an efficient, streamlined, and effective onboarding experience for open source salespeople is essential to minimize time and maximize engagement. Drawing inspiration from the simplicity and user-focus of Steve Jobs, this document proposes an onboarding agent that is effortless, engaging, and educational. - -## Landing Page - -The journey starts with a landing page that is clean, crisp, and intuitively designed. A minimalistic aesthetic, akin to Apple's design philosophy, helps the user focus on what's important. The landing page should contain: - -- A bold, clear headline, expressing the value proposition for becoming an open source salesperson for Swarms. -- A short video or animation introducing Swarms and the opportunity for the salespeople. -- Clear call-to-action (CTA) buttons to start the onboarding process or to learn more. - -## Interactive Learning Modules - -Once the user clicks on the CTA, they're taken to an interactive course platform. This platform should feature short, digestible video modules covering a range of essential topics, including: - -1. An Introduction to Swarms: An engaging video that explains the concept, the value it brings to businesses, and the open-source ethos driving it. - -2. Understanding the Technology: A simple, jargon-free explanation of the underlying technology, how it works, and why it works that way. Emphasis should be on benefits rather than technical intricacies. - -3. Successful Sales Strategies: Sharing effective sales techniques and strategies that have worked for Swarms, along with case studies and testimonials. - -4. Navigating Customer Conversations: Guidance on how to effectively communicate with potential customers, including understanding their needs, presenting Swarms as a solution, and addressing objections. - -After each module, the user is asked to answer a few questions to ensure understanding and engagement. This also helps in identifying potential areas of confusion or difficulty. - -## Personal Interaction - -Once the user completes all the modules and successfully answers the questions, they're invited to schedule a one-on-one call with a member of APAC AI or an experienced open source sales freelancer. This serves as a validation step, providing personalized feedback and guidance to ensure the salesperson is ready to start selling Swarms. - -Throughout this journey, the focus should be on simplicity and intuitiveness. Just like Steve Jobs did with Apple's products, the onboarding experience should be so seamless and enjoyable that it's almost invisible, allowing the user to focus on what truly matters – learning about Swarms and how to sell it. - - - -# Open Source Salesperson Onboarding: Post Course Completion - -### For more assistance check out these resources - -* [Pricing Package](https://www.acquisition.com/hubfs/Offer%20Checklists%20-%20PDF%20Downloads/Pricing-Value-Checklist.pdf?hsLang=en) - -*[Alex Hormozi](https://www.acquisition.com/offers-value-equation) - -Once a salesperson has completed the initial course and had their one-on-one session, the onboarding continues to the next phase – preparing them for sales activities and ensuring they have the necessary tools and resources. - -## Access to CRM and Sales Tools - -Every salesperson is provided with access to a CRM (Customer Relationship Management) system. This CRM would be a simplified, user-friendly system that allows them to manage their prospects, track their interactions, and monitor their sales progress. They would also receive training on how to effectively use the CRM to streamline their sales activities. - -## Sales Resources - -Salespeople would be provided with a suite of sales resources, including Swarms product brochures, case studies, presentations, and a script guideline. They would also be given access to a community forum where they can connect with other salespeople, share experiences, ask questions, and learn from each other. - -## Making a Sale - -In terms of the actual sale process, the salesperson would direct the customer to a unique landing page on the APAC.ai website. This landing page would be specifically designed for the sales journey, and it would allow the customer to input their salesperson's ID during the checkout process. - -This ID linking mechanism is critical, as it ensures that the salesperson gets credited for any sales they bring in. Once a sale is made, the salesperson's commission gets credited to their account. - -## Post-Sale and Account Management - -Post-sale, the salesperson's role transitions to more of an account manager. They become the primary point of contact for the customer, responsible for ensuring customer satisfaction, handling any issues or concerns, and identifying opportunities for upselling or cross-selling. - -The salesperson would also receive a recurring revenue from their accounts. This not only incentivizes them to maintain a good relationship with their customers but also rewards them for the ongoing value they provide. - -## Feedback and Performance Reviews - -Periodic performance reviews would be conducted to provide feedback to the salespeople and help them improve. These reviews would also be an opportunity to recognize top performers and share their success stories with the wider community. - -Overall, the objective is to create a smooth, rewarding, and self-sustaining sales ecosystem. Salespeople are empowered with the tools, resources, and incentives they need to succeed, and in return, they help drive the growth and success of Swarms. It's a win-win scenario that has the potential to dramatically accelerate Swarms' market penetration and customer adoption. \ No newline at end of file diff --git a/docs/old-docs/design/CLEAN_CODE.md b/docs/old-docs/design/CLEAN_CODE.md deleted file mode 100644 index 7840a649..00000000 --- a/docs/old-docs/design/CLEAN_CODE.md +++ /dev/null @@ -1,242 +0,0 @@ -Code is clean if it can be understood easily – by everyone on the team. Clean code can be read and enhanced by a developer other than its original author. With understandability comes readability, changeability, extensibility and maintainability. -_____________________________________ - -## General rules -1. Follow standard conventions. -2. Keep it simple stupid. Simpler is always better. Reduce complexity as much as possible. -3. Boy scout rule. Leave the campground cleaner than you found it. -4. Always find root cause. Always look for the root cause of a problem. - -## Design rules -1. Keep configurable data at high levels. -2. Prefer polymorphism to if/else or switch/case. -3. Separate multi-threading code. -4. Prevent over-configurability. -5. Use dependency injection. -6. Follow Law of Demeter. A class should know only its direct dependencies. - -## Understandability tips -1. Be consistent. If you do something a certain way, do all similar things in the same way. -2. Use explanatory variables. -3. Encapsulate boundary conditions. Boundary conditions are hard to keep track of. Put the processing for them in one place. -4. Prefer dedicated value objects to primitive type. -5. Avoid logical dependency. Don't write methods which works correctly depending on something else in the same class. -6. Avoid negative conditionals. - -## Names rules -1. Choose descriptive and unambiguous names. -2. Make meaningful distinction. -3. Use pronounceable names. -4. Use searchable names. -5. Replace magic numbers with named constants. -6. Avoid encodings. Don't append prefixes or type information. -7. The Name of a variable, Function, or Class should answer why it exists, what it does , and how it can used. Comments are a burden -8. Clarity is King -9. ClassNames should not be a verb -10. Methods should have verb or verb phrase names -11. Be simple. Be Direct. Say what you mean, mean what you say. -12. Don't use the same word for 2 purposes -13. - -## Functions rules -1. Small. -2. Do one thing. -3. Use descriptive names. -4. Prefer fewer arguments. -5. Have no side effects. -6. Don't use flag arguments. Split method into several independent methods that can be called from the client without the flag. -7. Smaller than 20 lines long -8. The Stepdown rule => function -> next level of abstraction - - -## ErrorHandling -1. Specify where the error in print -2. Don't use a single variable -3. - -## If statements -1. - - -## Comments rules -1. Always try to explain yourself in code. -2. Don't be redundant. -3. Don't add obvious noise. -4. Don't use closing brace comments. -5. Don't comment out code. Just remove. -6. Use as explanation of intent. -7. Use as clarification of code. -8. Use as warning of consequences. - -## Source code structure -1. Separate concepts vertically. -2. Related code should appear vertically dense. -3. Declare variables close to their usage. -4. Dependent functions should be close. -5. Similar functions should be close. -6. Place functions in the downward direction. -7. Keep lines short. -8. Don't use horizontal alignment. -9. Use white space to associate related things and disassociate weakly related. -10. Don't break indentation. - -## Objects and data structures -1. Hide internal structure. -2. Prefer data structures. -3. Avoid hybrids structures (half object and half data). -4. Should be small. -5. Do one thing. -6. Small number of instance variables. -7. Base class should know nothing about their derivatives. -8. Better to have many functions than to pass some code into a function to select a behavior. -9. Prefer non-static methods to static methods. - -## Tests -1. One assert per test. -2. Readable. -3. Fast. -4. Independent. -5. Repeatable. - -## Code smells -1. Rigidity. The software is difficult to change. A small change causes a cascade of subsequent changes. -2. Fragility. The software breaks in many places due to a single change. -3. Immobility. You cannot reuse parts of the code in other projects because of involved risks and high effort. -4. Needless Complexity. -5. Needless Repetition. -6. Opacity. The code is hard to understand. - - - - - - - - -# Clean Code - -Here are some general principles for writing highly usable, functional, reliable, fast, and scalable code: - -1. **Clear and Understandable:** The code should be written in a way that's easy for others to understand. This includes using clear variable and function names, and including comments to explain complex sections of code. - -2. **Modular and Reusable:** Code should be broken down into small, modular functions and classes that each perform a single task. This makes the code more understandable, and also allows for code reuse. - -3. **Robust Error Handling:** The code should be able to handle all potential errors gracefully, and should never crash unexpectedly. This includes checking for invalid input, catching exceptions, and providing useful error messages. - -4. **Type Handling:** Whenever possible, the code should enforce and check types to prevent type-related errors. This can be done through the use of type hints in languages like Python, or through explicit type checks. - -5. **Logging:** The code should include extensive logging to make it easier to debug and understand what the code is doing. This includes logging any errors that occur, as well as important events or state changes. - -6. **Performance:** The code should be optimized for performance, avoiding unnecessary computation and using efficient algorithms and data structures. This includes profiling the code to identify and optimize performance bottlenecks. - -7. **Scalability:** The code should be designed to scale well as the size of the input data or the number of users increases. This includes using scalable algorithms and data structures, and designing the code to work well in a distributed or parallel computing environment if necessary. - -8. **Testing:** The code should include comprehensive tests to ensure that it works correctly. This includes unit tests for individual functions and classes, as well as integration tests to ensure that the different parts of the code work well together. - -9. **Version Control:** The code should be stored in a version control system like Git, which allows for tracking changes, collaborating with others, and rolling back to a previous state if necessary. - -10. **Documentation:** The codebase should be well-documented, both in terms of comments within the code and external documentation that explains how to use and contribute to the code. - -11. **Continuous Integration/Continuous Deployment (CI/CD):** Implement CI/CD pipelines for automatic testing and deployment. This ensures that any new changes do not break existing functionality and that the latest version of the application is always available for deployment. - -# Examples -1. **Clear and Understandable:** Use meaningful variable and function names. Include comments when necessary. - - ```python - # Good example - def calculate_average(numbers: List[int]) -> float: - """Calculate and return the average of a list of numbers.""" - total = sum(numbers) - count = len(numbers) - return total / count - ``` - - For file and folder names, use descriptive names that relate to their function in your program. For example, a file that contains functions for handling user input might be named `user_input.py`. - -2. **Modular and Reusable:** Write functions for tasks that you perform over and over. - - ```python - def greet_user(name: str): - """Print a greeting to the user.""" - print(f"Hello, {name}!") - ``` - - For folder structure, group related files in the same directory. For example, all test files could be in a `tests` directory. - -3. **Robust Error Handling:** Use try/except blocks to catch and handle errors. - - ```python - def divide_numbers(numerator: float, denominator: float) -> float: - """Divide two numbers and handle division by zero.""" - try: - return numerator / denominator - except ZeroDivisionError: - print("Error: Division by zero.") - return None - ``` - -4. **Type Handling:** Use type hints to specify the type of function arguments and return values. - - ```python - def greet_user(name: str) -> None: - """Greet the user.""" - print(f"Hello, {name}!") - ``` - -5. **Logging:** Use the `logging` module to log events. - - ```python - import logging - - logging.basicConfig(level=logging.INFO) - - def divide_numbers(numerator: float, denominator: float) -> float: - """Divide two numbers and log if division by zero occurs.""" - try: - return numerator / denominator - except ZeroDivisionError: - logging.error("Attempted division by zero.") - return None - ``` - -6. **Performance:** Use built-in functions and data types for better performance. - - ```python - # Using a set to check for membership is faster than using a list - numbers_set = set(numbers) - if target in numbers_set: - print(f"{target} is in the set of numbers.") - ``` - -7. **Scalability:** For scalability, an example might involve using a load balancer or dividing tasks among different workers or threads. This is more of a system design consideration than a single piece of code. - -8. **Testing:** Write tests for your functions. - - ```python - def test_calculate_average(): - assert calculate_average([1, 2, 3, 4]) == 2.5 - ``` - - For tests, you could have a separate `tests` directory. Inside this directory, each test file could be named `test_.py` where `` is the name of the file being tested. - -9. **Version Control:** This point refers to using tools like Git for version control. A simple example would be committing changes to a repository: - - ```bash - git add . - git commit -m "Add function to calculate average" - git push - ``` - -10. **Documentation:** Write docstrings for your functions. - - ```python - def calculate_average(numbers: List[int]) -> float: - """Calculate and return the average of a list of numbers.""" - ... - ``` - - Documentation might be kept in a `docs` directory, with separate files for different topics. - -11. **Continuous Integration/Continuous Deployment (CI/CD):** This is typically handled by a system like Jenkins, GitHub Actions, or GitLab CI/CD. It involves creating a script or configuration file that tells the CI/CD system how to build, test, and deploy your code. For example, a `.github/workflows/main.yml` file for a GitHub Actions workflow. - -Remember, consistency in your naming conventions and organization is key. Having a standard and sticking to it will make your codebase easier to navigate and understand. \ No newline at end of file diff --git a/docs/old-docs/design/DESIGN.md b/docs/old-docs/design/DESIGN.md deleted file mode 100644 index be92089a..00000000 --- a/docs/old-docs/design/DESIGN.md +++ /dev/null @@ -1,146 +0,0 @@ -# Swarm Architecture Design Document - -## Overview - -The goal of the Swarm Architecture is to provide a flexible and scalable system to build swarm intelligence models that can solve complex problems. This document details the proposed design to create a plug-and-play system, which makes it easy to create custom swarms, and provides pre-configured swarms with multi-modal agents. - -## Design Principles - -- **Modularity**: The system will be built in a modular fashion, allowing various components to be easily swapped or upgraded. -- **Interoperability**: Different swarm classes and components should be able to work together seamlessly. -- **Scalability**: The design should support the growth of the system by adding more components or swarms. -- **Ease of Use**: Users should be able to easily create their own swarms or use pre-configured ones with minimal configuration. - -## Design Components - -### AbstractSwarm - -The AbstractSwarm is an abstract base class which defines the basic structure of a swarm and the methods that need to be implemented. Any new swarm should inherit from this class and implement the required methods. - -### Swarm Classes - -Various Swarm classes can be implemented inheriting from the AbstractSwarm class. Each swarm class should implement the required methods for initializing the components, worker nodes, and boss node, and running the swarm. - -Pre-configured swarm classes with multi-modal agents can be provided for ease of use. These classes come with a default configuration of tools and agents, which can be used out of the box. - -### Tools and Agents - -Tools and agents are the components that provide the actual functionality to the swarms. They can be language models, AI assistants, vector stores, or any other components that can help in problem solving. - -To make the system plug-and-play, a standard interface should be defined for these components. Any new tool or agent should implement this interface, so that it can be easily plugged into the system. - -## Usage - -Users can either use pre-configured swarms or create their own custom swarms. - -To use a pre-configured swarm, they can simply instantiate the corresponding swarm class and call the run method with the required objective. - -To create a custom swarm, they need to: - -1. Define a new swarm class inheriting from AbstractSwarm. -2. Implement the required methods for the new swarm class. -3. Instantiate the swarm class and call the run method. - -### Example - -```python -# Using pre-configured swarm -swarm = PreConfiguredSwarm(openai_api_key) -swarm.run_swarms(objective) - -# Creating custom swarm -class CustomSwarm(AbstractSwarm): - # Implement required methods - -swarm = CustomSwarm(openai_api_key) -swarm.run_swarms(objective) -``` - -## Conclusion - -This Swarm Architecture design provides a scalable and flexible system for building swarm intelligence models. The plug-and-play design allows users to easily use pre-configured swarms or create their own custom swarms. - - -# Swarming Architectures -Sure, below are five different swarm architectures with their base requirements and an abstract class that processes these components: - -1. **Hierarchical Swarm**: This architecture is characterized by a boss/worker relationship. The boss node takes high-level decisions and delegates tasks to the worker nodes. The worker nodes perform tasks and report back to the boss node. - - Requirements: Boss node (can be a large language model), worker nodes (can be smaller language models), and a task queue for task management. - -2. **Homogeneous Swarm**: In this architecture, all nodes in the swarm are identical and contribute equally to problem-solving. Each node has the same capabilities. - - Requirements: Homogeneous nodes (can be language models of the same size), communication protocol for nodes to share information. - -3. **Heterogeneous Swarm**: This architecture contains different types of nodes, each with its specific capabilities. This diversity can lead to more robust problem-solving. - - Requirements: Different types of nodes (can be different types and sizes of language models), a communication protocol, and a mechanism to delegate tasks based on node capabilities. - -4. **Competitive Swarm**: In this architecture, nodes compete with each other to find the best solution. The system may use a selection process to choose the best solutions. - - Requirements: Nodes (can be language models), a scoring mechanism to evaluate node performance, a selection mechanism. - -5. **Cooperative Swarm**: In this architecture, nodes work together and share information to find solutions. The focus is on cooperation rather than competition. - - Requirements: Nodes (can be language models), a communication protocol, a consensus mechanism to agree on solutions. - - -6. **Grid-based Swarm**: This architecture positions agents on a grid, where they can only interact with their neighbors. This is useful for simulations, especially in fields like ecology or epidemiology. - - Requirements: Agents (can be language models), a grid structure, and a neighborhood definition (i.e., how to identify neighboring agents). - -7. **Particle Swarm Optimization (PSO) Swarm**: In this architecture, each agent represents a potential solution to an optimization problem. Agents move in the solution space based on their own and their neighbors' past performance. PSO is especially useful for continuous numerical optimization problems. - - Requirements: Agents (each representing a solution), a definition of the solution space, an evaluation function to rate the solutions, a mechanism to adjust agent positions based on performance. - -8. **Ant Colony Optimization (ACO) Swarm**: Inspired by ant behavior, this architecture has agents leave a pheromone trail that other agents follow, reinforcing the best paths. It's useful for problems like the traveling salesperson problem. - - Requirements: Agents (can be language models), a representation of the problem space, a pheromone updating mechanism. - -9. **Genetic Algorithm (GA) Swarm**: In this architecture, agents represent potential solutions to a problem. They can 'breed' to create new solutions and can undergo 'mutations'. GA swarms are good for search and optimization problems. - - Requirements: Agents (each representing a potential solution), a fitness function to evaluate solutions, a crossover mechanism to breed solutions, and a mutation mechanism. - -10. **Stigmergy-based Swarm**: In this architecture, agents communicate indirectly by modifying the environment, and other agents react to such modifications. It's a decentralized method of coordinating tasks. - - Requirements: Agents (can be language models), an environment that agents can modify, a mechanism for agents to perceive environment changes. - -These architectures all have unique features and requirements, but they share the need for agents (often implemented as language models) and a mechanism for agents to communicate or interact, whether it's directly through messages, indirectly through the environment, or implicitly through a shared solution space. Some also require specific data structures, like a grid or problem space, and specific algorithms, like for evaluating solutions or updating agent positions. - - - - - -Here is an abstract class that provides the basic structure to process these components: - -```python -from abc import ABC, abstractmethod - -class AbstractSwarm(ABC): - - def __init__(self, agents, vectorstore, tools): - self.agents = agents - self.vectorstore = vectorstore - self.tools = tools - - @abstractmethod - def initialize(self): - pass - - @abstractmethod - def communicate(self): - pass - - @abstractmethod - def process(self): - pass - - @abstractmethod - def solve(self): - pass -``` - -This abstract class requires four methods to be implemented: - -- `initialize`: This method is used to set up the initial state of the swarm, including setting up nodes and tools. -- `communicate`: This method is responsible for facilitating communication between nodes. -- `process`: This method handles the processing logic, which can be different based on the swarm architecture. -- `solve`: This method is called to start the problem-solving process. - -This abstract class can be inherited by specific swarm architecture classes to implement their specific behavior. - -# 3 Ingredients - -* The Individual Agent Configuration with a vectorstore and tools - -* The Orchestrator, => task assignment, task completion handling, communication layer \ No newline at end of file diff --git a/docs/old-docs/design/DESIGN_PHILOSOPHY.md b/docs/old-docs/design/DESIGN_PHILOSOPHY.md deleted file mode 100644 index d1ee57e1..00000000 --- a/docs/old-docs/design/DESIGN_PHILOSOPHY.md +++ /dev/null @@ -1,53 +0,0 @@ -# Design Philosophy Document for Swarms - -## Usable - -### Objective - -Our goal is to ensure that Swarms is intuitive and easy to use for all users, regardless of their level of technical expertise. This includes the developers who implement Swarms in their applications, as well as end users who interact with the implemented systems. - -### Tactics - -- Clear and Comprehensive Documentation: We will provide well-written and easily accessible documentation that guides users through using and understanding Swarms. -- User-Friendly APIs: We'll design clean and self-explanatory APIs that help developers to understand their purpose quickly. -- Prompt and Effective Support: We will ensure that support is readily available to assist users when they encounter problems or need help with Swarms. - -## Reliable - -### Objective - -Swarms should be dependable and trustworthy. Users should be able to count on Swarms to perform consistently and without error or failure. - -### Tactics - -- Robust Error Handling: We will focus on error prevention, detection, and recovery to minimize failures in Swarms. -- Comprehensive Testing: We will apply various testing methodologies such as unit testing, integration testing, and stress testing to validate the reliability of our software. -- Continuous Integration/Continuous Delivery (CI/CD): We will use CI/CD pipelines to ensure that all changes are tested and validated before they're merged into the main branch. - -## Fast - -### Objective - -Swarms should offer high performance and rapid response times. The system should be able to handle requests and tasks swiftly. - -### Tactics - -- Efficient Algorithms: We will focus on optimizing our algorithms and data structures to ensure they run as quickly as possible. -- Caching: Where appropriate, we will use caching techniques to speed up response times. -- Profiling and Performance Monitoring: We will regularly analyze the performance of Swarms to identify bottlenecks and opportunities for improvement. - -## Scalable - -### Objective - -Swarms should be able to grow in capacity and complexity without compromising performance or reliability. It should be able to handle increased workloads gracefully. - -### Tactics - -- Modular Architecture: We will design Swarms using a modular architecture that allows for easy scaling and modification. -- Load Balancing: We will distribute tasks evenly across available resources to prevent overload and maximize throughput. -- Horizontal and Vertical Scaling: We will design Swarms to be capable of both horizontal (adding more machines) and vertical (adding more power to an existing machine) scaling. - -### Philosophy - -Swarms is designed with a philosophy of simplicity and reliability. We believe that software should be a tool that empowers users, not a hurdle that they need to overcome. Therefore, our focus is on usability, reliability, speed, and scalability. We want our users to find Swarms intuitive and dependable, fast and adaptable to their needs. This philosophy guides all of our design and development decisions. \ No newline at end of file diff --git a/docs/old-docs/design/GOLDEN_METRIC.md b/docs/old-docs/design/GOLDEN_METRIC.md deleted file mode 100644 index 8340d634..00000000 --- a/docs/old-docs/design/GOLDEN_METRIC.md +++ /dev/null @@ -1,225 +0,0 @@ -# The Golden Metric: 95% User-Task-Completion-Satisfaction Rate - -In the world of Swarms, there’s one metric that stands above the rest: the User-Task-Completion-Satisfaction (UTCS) rate. This metric is the heart of our system, the pulse that keeps us moving forward. It’s not just a number; it’s a reflection of our commitment to our users and a measure of our success. - -## What is the UTCS Rate? -The UTCS rate is a measure of how reliably and quickly Swarms can satisfy a user demand. It’s calculated by dividing the number of tasks completed to the user’s satisfaction by the total number of tasks. Multiply that by 100, and you’ve got your UTCS rate. - -But what does it mean to complete a task to the user’s satisfaction? It means that the task is not only completed, but completed in a way that meets or exceeds the user’s expectations. It’s about quality, speed, and reliability. - -## Why is the UTCS Rate Important? -The UTCS rate is a direct reflection of the user experience. A high UTCS rate means that users are getting what they need from Swarms, and they’re getting it quickly and reliably. It means that Swarms is doing its job, and doing it well. - -But the UTCS rate is not just about user satisfaction. It’s also a measure of Swarms’ efficiency and effectiveness. A high UTCS rate means that Swarms is able to complete tasks quickly and accurately, with minimal errors or delays. It’s a sign of a well-oiled machine. - -## How Do We Achieve a 95% UTCS Rate? -Achieving a 95% UTCS rate is no small feat. It requires a deep understanding of our users and their needs, a robust and reliable system, and a commitment to continuous improvement. - -### Here are some strategies we’re implementing to reach our goal: - -* Understanding User Needs: We must have agents that gain an understanding of the user's objective and break it up into it's most fundamental building blocks - -* Improving System Reliability: We’re working to make Swarms more reliable, reducing errors and improving the accuracy of task completion. This includes improving our algorithms, refining our processes, and investing in quality assurance. - -* Optimizing for Speed: We’re optimizing Swarms to complete tasks as quickly as possible, without sacrificing quality. This includes improving our infrastructure, streamlining our workflows, and implementing performance optimizations. - -*Iterating and Improving: We’re committed to continuous improvement. We’re constantly monitoring our UTCS rate and other key metrics, and we’re always looking for ways to improve. We’re not afraid to experiment, iterate, and learn from our mistakes. - -Achieving a 95% UTCS rate is a challenging goal, but it’s a goal worth striving for. It’s a goal that will drive us to improve, innovate, and deliver the best possible experience for our users. And in the end, that’s what Swarms is all about. - - - -# Your Feedback Matters: Help Us Optimize the UTCS Rate - -As we initiate the journey of Swarms, we seek your feedback to better guide our growth and development. Your opinions and suggestions are crucial for us, helping to mold our product, pricing, branding, and a host of other facets that influence your experience. - -## Your Insights on the UTCS Rate -Our goal is to maintain a UTCS (User-Task-Completion-Satisfaction) rate of 95%. This metric is integral to the success of Swarms, indicating the efficiency and effectiveness with which we satisfy user requests. However, it's a metric that we can't optimize alone - we need your help. - -Here's what we want to understand from you: - -1. **Satisfaction:** What does a "satisfactorily completed task" mean to you? Are there specific elements that contribute to a task being carried out to your satisfaction? -2. **Timeliness:** How important is speed in the completion of a task? What would you consider a reasonable timeframe for a task to be completed? -3. **Usability:** How intuitive and user-friendly do you find the Swarms platform? Are there any aspects of the platform that you believe could be enhanced? -4. **Reliability:** How much does consistency in performance matter to you? Can you share any experiences where Swarms either met or fell short of your expectations? -5. **Value for Money:** How do you perceive our pricing? Does the value Swarms provides align with the costs? - -We invite you to share your experiences, thoughts, and ideas. Whether it's a simple suggestion or an in-depth critique, we appreciate and value your input. - -## Your Feedback: The Backbone of our Growth -Your feedback is the backbone of Swarms' evolution. It drives us to refine our strategies, fuels our innovative spirit, and, most importantly, enables us to serve you better. - -As we launch, we open the conversation around these key aspects of Swarms, and we look forward to understanding your expectations, your needs, and how we can deliver the best experience for you. - -So, let's start this conversation - how can we make Swarms work best for you? - - -Guide Our Growth: Help Optimize Swarms -As we launch Swarms, your feedback is critical for enhancing our product, pricing, and branding. A key aim for us is a User-Task-Completion-Satisfaction (UTCS) rate of 95% - indicating our efficiency and effectiveness in meeting user needs. However, we need your insights to optimize this. - -Here's what we're keen to understand: - -Satisfaction: Your interpretation of a "satisfactorily completed task". -Timeliness: The importance of speed in task completion for you. -Usability: Your experiences with our platform’s intuitiveness and user-friendliness. -Reliability: The significance of consistent performance to you. -Value for Money: Your thoughts on our pricing and value proposition. -We welcome your thoughts, experiences, and suggestions. Your feedback fuels our evolution, driving us to refine strategies, boost innovation, and enhance your experience. - -Let's start the conversation - how can we make Swarms work best for you? - - --------- - -**The Golden Metric Analysis: The Ultimate UTCS Paradigm for Swarms** - -### Introduction - -In our ongoing journey to perfect Swarms, understanding how our product fares in the eyes of the end-users is paramount. Enter the User-Task-Completion-Satisfaction (UTCS) rate - our primary metric that gauges how reliably and swiftly Swarms can meet user demands. As we steer Swarms towards achieving a UTCS rate of 95%, understanding this metric's core and how to refine it becomes vital. - -### Decoding UTCS: An Analytical Overview - -The UTCS rate is not merely about task completion; it's about the comprehensive experience. Therefore, its foundations lie in: - -1. **Quality**: Ensuring tasks are executed flawlessly. -2. **Speed**: Delivering results in the shortest possible time. -3. **Reliability**: Consistency in quality and speed across all tasks. - -We can represent the UTCS rate with the following equation: - -```latex -\[ UTCS Rate = \frac{(Completed Tasks \times User Satisfaction)}{(Total Tasks)} \times 100 \] -``` - -Where: -- Completed Tasks refer to the number of tasks Swarms executes without errors. -- User Satisfaction is the subjective component, gauged through feedback mechanisms. This could be on a scale of 1-10 (or a percentage). -- Total Tasks refer to all tasks processed by Swarms, regardless of the outcome. - -### The Golden Metric: Swarm Efficiency Index (SEI) - -However, this basic representation doesn't factor in a critical component: system performance. Thus, we introduce the Swarm Efficiency Index (SEI). The SEI encapsulates not just the UTCS rate but also system metrics like memory consumption, number of tasks, and time taken. By blending these elements, we aim to present a comprehensive view of Swarm's prowess. - -Here’s the formula: - -```latex -\[ SEI = \frac{UTCS Rate}{(Memory Consumption + Time Window + Task Complexity)} \] -``` - -Where: -- Memory Consumption signifies the system resources used to accomplish tasks. -- Time Window is the timeframe in which the tasks were executed. -- Task Complexity could be a normalized scale that defines how intricate a task is (e.g., 1-5, with 5 being the most complex). - -Rationale: -- **Incorporating Memory Consumption**: A system that uses less memory but delivers results is more efficient. By inverting memory consumption in the formula, we emphasize that as memory usage goes down, SEI goes up. - -- **Considering Time**: Time is of the essence. The faster the results without compromising quality, the better. By adding the Time Window, we emphasize that reduced task execution time increases the SEI. - -- **Factoring in Task Complexity**: Not all tasks are equal. A system that effortlessly completes intricate tasks is more valuable. By integrating task complexity, we can normalize the SEI according to the task's nature. - -### Implementing SEI & Improving UTCS - -Using feedback from elder-plinius, we can better understand and improve SEI and UTCS: - -1. **Feedback Across Skill Levels**: By gathering feedback from users with different skill levels, we can refine our metrics, ensuring Swarms caters to all. - -2. **Simplifying Setup**: Detailed guides can help newcomers swiftly get on board, thus enhancing user satisfaction. - -3. **Enhancing Workspace and Agent Management**: A clearer view of the Swarm's internal structure, combined with on-the-go adjustments, can improve both the speed and quality of results. - -4. **Introducing System Suggestions**: A proactive Swarms that provides real-time insights and recommendations can drastically enhance user satisfaction, thus pushing up the UTCS rate. - -### Conclusion - -The UTCS rate is undeniably a pivotal metric for Swarms. However, with the introduction of the Swarm Efficiency Index (SEI), we have an opportunity to encapsulate a broader spectrum of performance indicators, leading to a more holistic understanding of Swarms' efficiency. By consistently optimizing for SEI, we can ensure that Swarms not only meets user expectations but also operates at peak system efficiency. - - ----------------- -**Research Analysis: Tracking and Ensuring Reliability of Swarm Metrics at Scale** - -### 1. Introduction - -In our pursuit to optimize the User-Task-Completion-Satisfaction (UTCS) rate and Swarm Efficiency Index (SEI), reliable tracking of these metrics at scale becomes paramount. This research analysis delves into methodologies, technologies, and practices that can be employed to monitor these metrics accurately and efficiently across vast data sets. - -### 2. Why Tracking at Scale is Challenging - -The primary challenges include: - -- **Volume of Data**: As Swarms grows, the data generated multiplies exponentially. -- **Variability of Data**: Diverse user inputs lead to myriad output scenarios. -- **System Heterogeneity**: Different configurations and deployments can yield variable results. - -### 3. Strategies for Scalable Tracking - -#### 3.1. Distributed Monitoring Systems - -**Recommendation**: Implement distributed systems like Prometheus or InfluxDB. - -**Rationale**: -- Ability to collect metrics from various Swarm instances concurrently. -- Scalable and can handle vast data influxes. - -#### 3.2. Real-time Data Processing - -**Recommendation**: Use stream processing systems like Apache Kafka or Apache Flink. - -**Rationale**: -- Enables real-time metric calculation. -- Can handle high throughput and low-latency requirements. - -#### 3.3. Data Sampling - -**Recommendation**: Random or stratified sampling of user sessions. - -**Rationale**: -- Reduces the data volume to be processed. -- Maintains representativeness of overall user experience. - -### 4. Ensuring Reliability in Data Collection - -#### 4.1. Redundancy - -**Recommendation**: Integrate redundancy into data collection nodes. - -**Rationale**: -- Ensures no single point of failure. -- Data loss prevention in case of system malfunctions. - -#### 4.2. Anomaly Detection - -**Recommendation**: Implement AI-driven anomaly detection systems. - -**Rationale**: -- Identifies outliers or aberrations in metric calculations. -- Ensures consistent and reliable data interpretation. - -#### 4.3. Data Validation - -**Recommendation**: Establish automated validation checks. - -**Rationale**: -- Ensures only accurate and relevant data is considered. -- Eliminates inconsistencies arising from corrupted or irrelevant data. - -### 5. Feedback Loops and Continuous Refinement - -#### 5.1. User Feedback Integration - -**Recommendation**: Develop an in-built user feedback mechanism. - -**Rationale**: -- Helps validate the perceived vs. actual performance. -- Allows for continuous refining of tracking metrics and methodologies. - -#### 5.2. A/B Testing - -**Recommendation**: Regularly conduct A/B tests for new tracking methods or adjustments. - -**Rationale**: -- Determines the most effective methods for data collection. -- Validates new tracking techniques against established ones. - -### 6. Conclusion - -To successfully and reliably track the UTCS rate and SEI at scale, it's essential to combine robust monitoring tools, data processing methodologies, and validation techniques. By doing so, Swarms can ensure that the metrics collected offer a genuine reflection of system performance and user satisfaction. Regular feedback and iterative refinement, rooted in a culture of continuous improvement, will further enhance the accuracy and reliability of these essential metrics. \ No newline at end of file diff --git a/docs/old-docs/design/abstraction.py b/docs/old-docs/design/abstraction.py deleted file mode 100644 index 75862e72..00000000 --- a/docs/old-docs/design/abstraction.py +++ /dev/null @@ -1,19 +0,0 @@ -from swarms import Model, Agent, vectorstore, tools, orchestrator - -# 1 model -Model(openai) - -# 2 agent level -Agent(model, vectorstore, tools) - -# 3 worker infrastructure level -worker_node(Agent, human_input, tools) - -# 4 swarm level basically handling infrastructure for multiple worker node -swarm = orchestrator(worker_node, 100) # nodes - -# 5 -hivemind = Hivemind(swarm * 100) - - -# a market different pre built worker or boss agent that have access to different tools and memory, proompts diff --git a/docs/old-docs/ideas/AGENT_NAME.md b/docs/old-docs/ideas/AGENT_NAME.md deleted file mode 100644 index 407c2f12..00000000 --- a/docs/old-docs/ideas/AGENT_NAME.md +++ /dev/null @@ -1,22 +0,0 @@ -Sure, here are some creative neo-futuristic names for your swarm worker agents: - -QuantumPulse -UltraNode -FusionCortex -HyperionCrawler -XenoSync -HoloWorker -EtherEngine -NebulaNexus -OmegaOrbit -ZetaZenith -PhantomPivot -VortexVector -ChronoCluster -SpecterSphere -TitanTrace -EchoEntity -NovaNucleus -AstroAssembler -PulsarProcessor -CosmicCrafter \ No newline at end of file diff --git a/docs/old-docs/ideas/HIVEMIND.md b/docs/old-docs/ideas/HIVEMIND.md deleted file mode 100644 index 09c0c9ab..00000000 --- a/docs/old-docs/ideas/HIVEMIND.md +++ /dev/null @@ -1,69 +0,0 @@ -Guide to Product-Market Fit for HiveMind Class -Risks and Mitigations -Scalability: As the number of swarms increases, the computational resources required will also increase. This could lead to performance issues or high costs. - -Mitigation: Implement efficient resource management and load balancing. Consider using cloud-based solutions that can scale up or down based on demand. - -Concurrency Issues: With multiple swarms running concurrently, there could be issues with data consistency and synchronization. - -Mitigation: Implement robust concurrency control mechanisms. Ensure that the shared vector store is thread-safe. - -Error Propagation: Errors in one swarm could potentially affect other swarms or the entire HiveMind. - -Mitigation: Implement robust error handling and isolation mechanisms. Errors in one swarm should not affect the operation of other swarms. - -Complexity: The HiveMind class is complex and could be difficult to maintain and extend. - -Mitigation: Follow best practices for software design, such as modularity, encapsulation, and separation of concerns. Write comprehensive tests to catch issues early. - -User Experience: If the HiveMind class is not easy to use, it could deter potential users. - -Mitigation: Provide clear documentation and examples. Implement a user-friendly API. Consider providing a high-level interface that abstracts away some of the complexity. - -Mental Models and Design Paradigms -Modularity: Each swarm should be a self-contained unit that can operate independently. This makes the system more flexible and easier to maintain. - -Concurrency: The system should be designed to handle multiple swarms running concurrently. This requires careful consideration of issues such as data consistency and synchronization. - -Fault Tolerance: The system should be able to handle errors gracefully. If one swarm encounters an error, it should not affect the operation of other swarms. - -Scalability: The system should be able to handle an increasing number of swarms without a significant degradation in performance. - -User-Centric Design: The system should be designed with the user in mind. It should be easy to use and provide value to the user. - -Path to Product-Market Fit -Identify Target Users: Determine who would benefit most from using the HiveMind class. This could be developers, data scientists, researchers, or businesses. - -Understand User Needs: Conduct user research to understand the problems that users are trying to solve and how the HiveMind class can help. - -Develop MVP: Develop a minimum viable product (MVP) that demonstrates the value of the HiveMind class. This should be a simple version of the product that solves a core user problem. - -Gather Feedback: After releasing the MVP, gather feedback from users. This could be through surveys, interviews, or user testing. - -Iterate and Improve: Use the feedback to iterate and improve the product. This could involve fixing bugs, adding new features, or improving usability. - -Scale: Once the product has achieved product-market fit, focus on scaling. This could involve optimizing the product for performance, expanding to new markets, or developing partnerships. - - - -Here are some features that could be added to the HiveMind class to provide maximum value for users: - -Dynamic Scaling: The ability to automatically scale the number of swarms based on the complexity of the task or the load on the system. This would allow the system to handle a wide range of tasks efficiently. - -Task Prioritization: The ability to prioritize tasks based on their importance or urgency. This would allow more important tasks to be completed first. - -Progress Monitoring: The ability for users to monitor the progress of their tasks. This could include a progress bar, estimated completion time, or real-time updates. - -Error Reporting: Detailed error reports that help users understand what went wrong if a task fails. This could include the error message, the swarm that encountered the error, and suggestions for how to fix the error. - -Task Cancellation: The ability for users to cancel a task that is currently being processed. This could be useful if a user realizes they made a mistake or if a task is taking too long to complete. - -Task Queuing: The ability for users to queue up multiple tasks. This would allow users to submit a batch of tasks and have them processed one after the other. - -Result Formatting: The ability for users to specify how they want the results to be formatted. This could include options for plain text, JSON, XML, or other formats. - -Integration with Other Services: The ability to integrate with other services, such as databases, cloud storage, or machine learning platforms. This would allow users to easily store results, access additional resources, or leverage advanced features. - -Security Features: Features to ensure the security and privacy of user data, such as encryption, access controls, and audit logs. - -User-Friendly API: A well-designed, user-friendly API that makes it easy for users to use the HiveMind class in their own applications. This could include clear documentation, examples, and error messages. diff --git a/docs/old-docs/ideas/IDEAS.MD b/docs/old-docs/ideas/IDEAS.MD deleted file mode 100644 index bc2514b1..00000000 --- a/docs/old-docs/ideas/IDEAS.MD +++ /dev/null @@ -1,401 +0,0 @@ -## Swarming Architectures - -Here are three examples of swarming architectures that could be applied in this context. - -1. **Hierarchical Swarms**: In this architecture, a 'lead' agent coordinates the efforts of other agents, distributing tasks based on each agent's unique strengths. The lead agent might be equipped with additional functionality or decision-making capabilities to effectively manage the swarm. - -2. **Collaborative Swarms**: Here, each agent in the swarm works in parallel, potentially on different aspects of a task. They then collectively determine the best output, often through a voting or consensus mechanism. - -3. **Competitive Swarms**: In this setup, multiple agents work on the same task independently. The output from the agent which produces the highest confidence or quality result is then selected. This can often lead to more robust outputs, as the competition drives each agent to perform at its best. - -4. **Multi-Agent Debate**: Here, multiple agents debate a topic. The output from the agent which produces the highest confidence or quality result is then selected. This can lead to more robust outputs, as the competition drives each agent to perform it's best. - - -# Ideas - -A swarm, particularly in the context of distributed computing, refers to a large number of coordinated agents or nodes that work together to solve a problem. The specific requirements of a swarm might vary depending on the task at hand, but some of the general requirements include: - -1. **Distributed Nature**: The swarm should consist of multiple individual units or nodes, each capable of functioning independently. - -2. **Coordination**: The nodes in the swarm need to coordinate with each other to ensure they're working together effectively. This might involve communication between nodes, or it could be achieved through a central orchestrator. - -3. **Scalability**: A well-designed swarm system should be able to scale up or down as needed, adding or removing nodes based on the task load. - -4. **Resilience**: If a node in the swarm fails, it shouldn't bring down the entire system. Instead, other nodes should be able to pick up the slack. - -5. **Load Balancing**: Tasks should be distributed evenly across the nodes in the swarm to avoid overloading any single node. - -6. **Interoperability**: Each node should be able to interact with others, regardless of differences in underlying hardware or software. - -Integrating these requirements with Large Language Models (LLMs) can be done as follows: - -1. **Distributed Nature**: Each LLM agent can be considered as a node in the swarm. These agents can be distributed across multiple servers or even geographically dispersed data centers. - -2. **Coordination**: An orchestrator can manage the LLM agents, assigning tasks, coordinating responses, and ensuring effective collaboration between agents. - -3. **Scalability**: As the demand for processing power increases or decreases, the number of LLM agents can be adjusted accordingly. - -4. **Resilience**: If an LLM agent goes offline or fails, the orchestrator can assign its tasks to other agents, ensuring the swarm continues functioning smoothly. - -5. **Load Balancing**: The orchestrator can also handle load balancing, ensuring tasks are evenly distributed amongst the LLM agents. - -6. **Interoperability**: By standardizing the input and output formats of the LLM agents, they can effectively communicate and collaborate, regardless of the specific model or configuration of each agent. - -In terms of architecture, the swarm might look something like this: - -``` - (Orchestrator) - / \ - Tools + Vector DB -- (LLM Agent)---(Communication Layer) (Communication Layer)---(LLM Agent)-- Tools + Vector DB - / | | \ -(Task Assignment) (Task Completion) (Task Assignment) (Task Completion) -``` - -Each LLM agent communicates with the orchestrator through a dedicated communication layer. The orchestrator assigns tasks to each LLM agent, which the agents then complete and return. This setup allows for a high degree of flexibility, scalability, and robustness. - - -## Communication Layer - -Communication layers play a critical role in distributed systems, enabling interaction between different nodes (agents) and the orchestrator. Here are three potential communication layers for a distributed system, including their strengths and weaknesses: - -1. **Message Queuing Systems (like RabbitMQ, Kafka)**: - - - Strengths: They are highly scalable, reliable, and designed for high-throughput systems. They also ensure delivery of messages and can persist them if necessary. Furthermore, they support various messaging patterns like publish/subscribe, which can be highly beneficial in a distributed system. They also have robust community support. - - - Weaknesses: They can add complexity to the system, including maintenance of the message broker. Moreover, they require careful configuration to perform optimally, and handling failures can sometimes be challenging. - -2. **RESTful APIs**: - - - Strengths: REST is widely adopted, and most programming languages have libraries to easily create RESTful APIs. They leverage standard HTTP(S) protocols and methods and are straightforward to use. Also, they can be stateless, meaning each request contains all the necessary information, enabling scalability. - - - Weaknesses: For real-time applications, REST may not be the best fit due to its synchronous nature. Additionally, handling a large number of API requests can put a strain on the system, causing slowdowns or timeouts. - -3. **gRPC (Google Remote Procedure Call)**: - - - Strengths: gRPC uses Protocol Buffers as its interface definition language, leading to smaller payloads and faster serialization/deserialization compared to JSON (commonly used in RESTful APIs). It supports bidirectional streaming and can use HTTP/2 features, making it excellent for real-time applications. - - - Weaknesses: gRPC is more complex to set up compared to REST. Protocol Buffers' binary format can be more challenging to debug than JSON. It's also not as widely adopted as REST, so tooling and support might be limited in some environments. - -In the context of swarm LLMs, one could consider an **Omni-Vector Embedding Database** for communication. This database could store and manage the high-dimensional vectors produced by each LLM agent. - -- Strengths: This approach would allow for similarity-based lookup and matching of LLM-generated vectors, which can be particularly useful for tasks that involve finding similar outputs or recognizing patterns. - -- Weaknesses: An Omni-Vector Embedding Database might add complexity to the system in terms of setup and maintenance. It might also require significant computational resources, depending on the volume of data being handled and the complexity of the vectors. The handling and transmission of high-dimensional vectors could also pose challenges in terms of network load. - - - - -# Technical Analysis Document: Particle Swarm of AI Agents using Ocean Database - -## Overview - -The goal is to create a particle swarm of AI agents using the OpenAI API for the agents and the Ocean database as the communication space, where the embeddings act as particles. The swarm will work collectively to perform tasks and optimize their behavior based on the interaction with the Ocean database. - -## Algorithmic Overview - -1. Initialize the AI agents and the Ocean database. -2. Assign tasks to the AI agents. -3. AI agents use the OpenAI API to perform tasks and generate embeddings. -4. AI agents store their embeddings in the Ocean database. -5. AI agents query the Ocean database for relevant embeddings. -6. AI agents update their positions based on the retrieved embeddings. -7. Evaluate the performance of the swarm and update the agents' behavior accordingly. -8. Repeat steps 3-7 until a stopping criterion is met. - -## Python Implementation Logic - -1. **Initialize the AI agents and the Ocean database.** - -```python -import openai -import oceandb -from oceandb.utils.embedding_functions import ImageBindEmbeddingFunction - -# Initialize Ocean database -client = oceandb.Client() -text_embedding_function = ImageBindEmbeddingFunction(modality="text") -collection = client.create_collection("all-my-documents", embedding_function=text_embedding_function) - -# Initialize AI agents -agents = initialize_agents(...) -``` - -2. **Assign tasks to the AI agents.** - -```python -tasks = assign_tasks_to_agents(agents, ...) -``` - -3. **AI agents use the OpenAI API to perform tasks and generate embeddings.** - -```python -def agent_perform_task(agent, task): - # Perform the task using the OpenAI API - result = perform_task_with_openai_api(agent, task) - # Generate the embedding - embedding = generate_embedding(result) - return embedding - -embeddings = [agent_perform_task(agent, task) for agent, task in zip(agents, tasks)] -``` - -4. **AI agents store their embeddings in the Ocean database.** - -```python -def store_embeddings_in_database(embeddings, collection): - for i, embedding in enumerate(embeddings): - document_id = f"agent_{i}" - collection.add(documents=[embedding], ids=[document_id]) - -store_embeddings_in_database(embeddings, collection) -``` - -5. **AI agents query the Ocean database for relevant embeddings.** - -```python -def query_database_for_embeddings(agent, collection, n_results=1): - query_result = collection.query(query_texts=[agent], n_results=n_results) - return query_result - -queried_embeddings = [query_database_for_embeddings(agent, collection) for agent in agents] -``` - -6. **AI agents update their positions based on the retrieved embeddings.** - -```python -def update_agent_positions(agents, queried_embeddings): - for agent, embedding in zip(agents, queried_embeddings): - agent.update_position(embedding) - -update_agent_positions(agents, queried_embeddings) -``` - -7. **Evaluate the performance of the swarm and update the agents' behavior accordingly.** - -```python -def evaluate_swarm_performance(agents, ...): - # Evaluate the performance of the swarm - performance = compute_performance_metric(agents, ...) - return performance - -def update_agent_behavior(agents, performance): - # Update agents' behavior based on swarm performance - for agent in agents: - agent.adjust_behavior(performance) - -performance = evaluate_swarm_performance(agents, ...) -update_agent_behavior(agents, performance) -``` - -8. **Repeat steps 3-7 until a stopping criterion is met.** - -```python -while not stopping_criterion_met(): - # Perform tasks and generate embeddings - embeddings = [agent_perform_task(agent, task) for agent, task in zip(agents, tasks)] - - # Store embeddings in the Ocean database - store_embeddings_in_database(embeddings, collection) - - # Query the Ocean database for relevant embeddings - queried_embeddings = [query_database_for_embeddings(agent, collection) for agent in agents] - - # Update AI agent positions based on the retrieved embeddings - update_agent_positions(agents, queried_embeddings) - - # Evaluate the performance of the swarm and update the agents' behavior accordingly - performance = evaluate_swarm_performance(agents, ...) - update_agent_behavior(agents, performance) -``` - -This code demonstrates the complete loop to repeat steps 3-7 until a stopping criterion is met. You will need to define the `stopping_criterion_met()` function, which could be based on a predefined number of iterations, a target performance level, or any other condition that indicates that the swarm has reached a desired state. - - - - -* Integrate petals to handle huggingface LLM - - - -# Orchestrator -* Takes in an agent class with vector store, then handles all the communication and scales up a swarm with number of agents and handles task assignment and task completion - -```python - -from swarms import OpenAI, Orchestrator, Swarm - -orchestrated = Orchestrate(OpenAI, nodes=40) #handles all the task assignment and allocation and agent communication using a vectorstore as a universal communication layer and also handlles the task completion logic - -Objective = "Make a business website for a marketing consultancy" - -Swarms = (Swarms(orchestrated, auto=True, Objective)) -``` - -In terms of architecture, the swarm might look something like this: - -``` - (Orchestrator) - / \ - Tools + Vector DB -- (LLM Agent)---(Communication Layer) (Communication Layer)---(LLM Agent)-- Tools + Vector DB - / | | \ -(Task Assignment) (Task Completion) (Task Assignment) (Task Completion) -``` - -Each LLM agent communicates with the orchestrator through a dedicated communication layer. The orchestrator assigns tasks to each LLM agent, which the agents then complete and return. This setup allows for a high degree of flexibility, scalability, and robustness. - -In the context of swarm LLMs, one could consider an **Omni-Vector Embedding Database** for communication. This database could store and manage the high-dimensional vectors produced by each LLM agent. - -- Strengths: This approach would allow for similarity-based lookup and matching of LLM-generated vectors, which can be particularly useful for tasks that involve finding similar outputs or recognizing patterns. - -- Weaknesses: An Omni-Vector Embedding Database might add complexity to the system in terms of setup and maintenance. It might also require significant computational resources, depending on the volume of data being handled and the complexity of the vectors. The handling and transmission of high-dimensional vectors could also pose challenges in terms of network load. - - - -* Handling absurdly long sequences => first transform the objective if it's more than 1000tokens into a txt file similiar to how Claude works => then chunk it into sizes of 8000 seq length embeddings => then embed it and store in the vector database => then connext the agent model to it - - -Given the complexity of the topic, please note that these simplified markdown documents are quite abstract and high level. They can be used as a starting point for further detailed design and implementation: - -### Document 1: Hierarchical Swarms - -#### Overall Architecture - -1. Leader Agent (LA): This agent has the authority to manage and distribute tasks to the Worker Agents (WA). -2. Worker Agents (WAs): These agents perform the tasks assigned by the LA. - -#### Simplified Requirements - -1. LA should be able to distribute tasks to WAs. -2. WAs should be able to execute tasks and return results to LA. -3. LA should be able to consolidate and process results. - -#### Pseudocode - -``` -create LA -create WAs - -for each task in tasks: - LA.distribute_task(WAs, task) - - for each WA in WAs: - WA.execute_task() - - LA.collect_results(WAs) - -LA.process_results() -``` - -#### General Classes - -```python -class LeaderAgent: - def distribute_task(self, WAs, task): - pass - - def collect_results(self, WAs): - pass - - def process_results(self): - pass - -class WorkerAgent: - def execute_task(self): - pass -``` - -### Document 2: Collaborative Swarms - -#### Overall Architecture - -1. Collaborative Agents (CAs): These agents work in parallel on different aspects of a task and then collectively determine the best output. - -#### Simplified Requirements - -1. CAs should be able to work on tasks in parallel. -2. CAs should be able to collaborate to determine the best result. - -#### Pseudocode - -``` -create CAs - -for each task in tasks: - for each CA in CAs: - CA.execute_task(task) - - CA.collaborate() -``` - -#### General Classes - -```python -class CollaborativeAgent: - def execute_task(self, task): - pass - - def collaborate(self): - pass -``` - -### Document 3: Competitive Swarms - -#### Overall Architecture - -1. Competitive Agents (CompAs): These agents work independently on the same tasks, and the best result is selected. - -#### Simplified Requirements - -1. CompAs should be able to work independently on tasks. -2. An evaluation method should be used to select the best result. - -#### Pseudocode - -``` -create CompAs - -for each task in tasks: - for each CompA in CompAs: - CompA.execute_task(task) - -evaluate_results(CompAs) -``` - -#### General Classes - -```python -class CompetitiveAgent: - def execute_task(self, task): - pass - -def evaluate_results(CompAs): - pass -``` - -Note: In the real world, the complexity of the architecture and requirements will significantly exceed what is presented here. These examples provide a basic starting point but should be expanded upon based on the specifics of the task or problem you're trying to solve. - - - -# Swarms - -BabyAGI -> Autogpt's -> tools -> other agents - -- Host it on server, on premise, private learning, no learning is translating out -- companies are sensitive with data, models are firewalled, need privacy, huggingface, -- Does not transmit information, -- see agent activity, task history, - - optimize which agents for each task -- Assist or provide feedback to management agent -- overview see the whole swarm, modify each agent, visualize the communication stream with blue, -- Work optimization routines -- output monitoring -- stop output, agent looping, --quality assurance checker, adversarial agent -- see a holistic diagram of all agents, how are they being utilized, see number of iterations, query responses, balance loading, -- summary of tasks completed with critique, type of summary, ceo summary, manager summary -- outside of browser and accross whole operating system, switch apps, mac, linux, and windows --what are the skillsets behind the dev team, can be modified by experts, ui agent, manager agent, personalize agents with prompt and tools, and orca like explain your solutions, critique them then return the final output - - - - diff --git a/docs/old-docs/ideas/PLATFORMS.md b/docs/old-docs/ideas/PLATFORMS.md deleted file mode 100644 index d0c53f8a..00000000 --- a/docs/old-docs/ideas/PLATFORMS.md +++ /dev/null @@ -1,11 +0,0 @@ -* The Platform, where users can have a conversation with the domain - -* An developer platform where people can build swarms through a UI in nodes, connect and play - -* SIMS like UI, where you see every node accomplishing their tasks around the internet - -* Swarms Discord BOT - -* PAID API - -* MARKETPLACE FOR PREBUILT SWARMS WITH SPECIFIC PROMPTS, MODELS, TOOLS, AND MEMORIES, \ No newline at end of file diff --git a/docs/old-docs/ideas/SWARMSOS.md b/docs/old-docs/ideas/SWARMSOS.md deleted file mode 100644 index 11e6b535..00000000 --- a/docs/old-docs/ideas/SWARMSOS.md +++ /dev/null @@ -1,42 +0,0 @@ -Research Proposal: Creating a Swarm of LLM Agents for Operating Systems -Introduction -The goal of this research is to explore the feasibility and requirements of creating a swarm of Language Learning Model (LLM) agents that can autonomously operate the kernel of an operating system. This swarm of AI agents would be capable of performing tasks such as process scheduling, memory management, device management, and system calls, among others. - -Objectives -To investigate the feasibility of using LLM agents to autonomously operate the kernel of an operating system. -To identify the requirements and challenges of implementing such a system. -To develop a prototype system as a proof of concept. -Methodology -Literature Review: Conduct a comprehensive review of existing research on AI in operating systems, swarm intelligence, and LLMs. - -Feasibility Study: Analyze the capabilities of current LLMs and assess whether they can be adapted to operate an OS kernel. - -Requirement Analysis: Identify the hardware, software, and data requirements for implementing a swarm of LLM agents in an OS. - -System Design: Design a prototype system that uses LLM agents to perform basic kernel operations. - -Implementation and Testing: Implement the prototype system and conduct rigorous testing to evaluate its performance. - -Requirements -Hardware: A high-performance computing system would be required to handle the computational load of millions of LLM agents. This system would need to have a powerful CPU, a large amount of RAM, and possibly a GPU for machine learning tasks. - -Software: The system would require an operating system that is compatible with the LLM agents. This could be a popular OS like Linux, which is open-source and widely used in AI research. - -LLM Agents: The LLM agents would need to be trained to perform kernel operations. This would require a large dataset of kernel operations and their outcomes. - -Swarm Intelligence Framework: A framework for swarm intelligence would be needed to manage the LLM agents and coordinate their activities. - -Monitoring and Debugging Tools: Tools for monitoring the performance of the LLM agents and debugging any issues would be essential. - -Potential Challenges -Complexity of Kernel Operations: Kernel operations are complex and low-level. Training LLM agents to perform these operations accurately and efficiently could be challenging. - -Coordination of LLM Agents: Coordinating the activities of millions of LLM agents could be a complex task. The swarm intelligence framework would need to be robust and efficient. - -Security: The system would need to be secure to prevent unauthorized access and ensure the integrity of the kernel operations. - -Performance: The system would need to be able to handle a high load and perform operations quickly to avoid slowing down the OS. - -Conclusion -Creating a swarm of LLM agents for operating systems is a challenging but potentially rewarding endeavor. This research aims to explore the feasibility of this idea and identify the requirements for its implementation. If successful, this could open up new possibilities for AI in operating systems and beyond. - diff --git a/docs/old-docs/ideas/aug13.md b/docs/old-docs/ideas/aug13.md deleted file mode 100644 index e14c36da..00000000 --- a/docs/old-docs/ideas/aug13.md +++ /dev/null @@ -1,78 +0,0 @@ -## **Product Feature Document: Multi-Agent Distributed Collaboration Framework** - ---- - -**Introduction**: -In a world increasingly leaning towards automation, we present a framework to enable multi-agent distributed collaboration. This revolutionary approach, integrating millions of GPT-3 nodes, is set to redefine real-world task automation. This document outlines and prioritizes features based on their potential value to early adopters. - ---- - -### **1. Learning Enhancements** - -- **Private Learning**: Safeguard data and learn without transmitting sensitive information. - *Value Proposition*: Guarantees data security for enterprises dealing with sensitive information. - -- **Task Decomposition**: Algorithms to efficiently break down complex tasks into simpler sub-tasks for agent distribution. - *Value Proposition*: Simplifies problem-solving and ensures efficient task distribution among agents. - ---- - -### **2. Swarm Management & Performance** - -- **Swarm Benchmarks**: Establish performance benchmarks for swarms, providing users with expected efficiency and accuracy metrics. - *Value Proposition*: Allows users to anticipate swarm performance and adjust strategies accordingly. - -- **Swarm Classes & Modularity**: Create diverse classes of swarms based on task type, ensuring a high level of usability and flexibility. - *Value Proposition*: Customizable swarms tailored to specific problem sets, enhancing solution accuracy. - -- **Dictator Swarm Mode**: Centralized control for swarms for tasks that require uniformity and synchronization. - *Value Proposition*: Streamlines processes where coordination is key. - ---- - -### **3. Communication & Progress Tracking** - -- **Progress Posting Tool**: Equip agents with a tool to post their progress to a swarm-wide vector store. - *Value Proposition*: Real-time tracking of task progress and agent efficiency. - -- **Observer Agent**: A supervisory agent dedicated to preventing others from entering non-productive loops. - *Value Proposition*: Ensures optimal agent performance and minimizes wastage of computational resources. - ---- - -### **4. Tool Integration & Modularity** - -- **Easy Tool Integration**: Simplified interfaces to add or modify tools within the swarm. - *Value Proposition*: Augment swarm capabilities on-the-go, adapting to diverse tasks with ease. - -- **Vector Database for Tools**: Maintain a comprehensive database of tools, allowing agents to query and utilize as needed. - *Value Proposition*: Provides agents with a vast arsenal of tools to tackle various challenges, enhancing problem-solving capacity. - ---- - -### **5. Data Input & Multimodality** - -- **Multimodal Data Intake**: Enable swarms to process varied data types – text, images, sounds, and more. - *Value Proposition*: Broadens the range of tasks swarms can handle, from simple text-based queries to complex multimedia projects. - ---- - -### **Feature Priority (for early adopters)**: - -1. **Private Learning**: Data privacy remains paramount. -2. **Task Decomposition**: Efficient problem-solving is foundational. -3. **Swarm Benchmarks**: Understanding potential performance is essential for user trust. -4. **Progress Posting Tool**: Real-time updates increase confidence and allow for timely interventions. -5. **Multimodal Data Intake**: Increases the range and depth of tasks the framework can handle. -6. **Observer Agent**: Minimizing wastage is key to cost efficiency. -7. **Easy Tool Integration**: Enhancing adaptability for varied challenges. -8. **Swarm Classes & Modularity**: Customization ensures relevance to specific user needs. -9. **Dictator Swarm Mode**: Essential for tasks demanding synchronization. -10. **Vector Database for Tools**: Augments the swarms' problem-solving arsenal. - ---- - -**Conclusion**: -With these prioritized features, our framework promises not only to revolutionize task automation but also to deliver unmatched value to its earliest users. This is the dawn of a new era in AI collaboration, and we invite you to be a part of this journey. - -**Join the future of AI automation. Step into the swarm.** \ No newline at end of file diff --git a/docs/old-docs/ideas/aug16.md b/docs/old-docs/ideas/aug16.md deleted file mode 100644 index bcd53bbe..00000000 --- a/docs/old-docs/ideas/aug16.md +++ /dev/null @@ -1,80 +0,0 @@ -## **Product Feature Document: Multi-Agent Distributed Collaboration Framework** - ---- - -**Introduction**: -In the modern age of AI, the potential of harnessing multiple intelligent agents to automate real-world tasks offers unprecedented value. We're building a framework that enables multi-agent distributed collaboration, akin to working with millions of GPT-3 nodes, and this document outlines the features which will bring tremendous value to early adopters. - ---- - -**1. Data Security & Privacy** -- **On-Premise Hosting**: Users can deploy the framework on their server to ensure data doesn't leave their ecosystem. -- **Private Learning**: The agents can learn without transmitting sensitive data out. -- **Firewalled Models**: Ensures that all model data remains secured behind barriers, preventing unauthorized data access. -- **HuggingFace Integration**: For those comfortable with the HuggingFace ecosystem. -- **Transparency**: Ability to see agent activity, task history, which aids in accountability. - ---- - -**2. Agent & Swarm Management** -- **Optimized Task Allocation**: System can decide which agents are best suited for each task, based on their learning and past performance. -- **Agent Feedback System**: Enables users to assist or provide feedback to the managing agent. -- **Holistic Swarm View**: Visualize the entire swarm, the communication streams, and individually modify agent behavior. -- **Work Optimization**: Routines to determine the most efficient distribution of tasks amongst agents. -- **Quality Assurance Agent**: A specialized agent to ensure the outputs meet the required standards. - ---- - -**3. Output Management & Monitoring** -- **Stop Looping**: If an agent gets stuck in a task loop, the system can intervene. -- **Output Monitoring**: Real-time surveillance of what each agent produces. -- **Task Summaries**: An overview of tasks, tailored to different management levels (CEO summary, manager summary). - ---- - -**4. Cross-Platform Integration** -- **OS Compatibility**: Seamlessly operate across Mac, Linux, and Windows. -- **Beyond Browser**: Ability to interact with different applications across the entire OS. - ---- - -**5. Customization & Training** -- **Agent Personalization**: Tailor each agent's prompts, tools, and behavior to better fit specific tasks. -- **Training Agent for SMBs**: Simplified input prompting system to guide small-medium businesses. -- **Human Training Agent**: Uses visual aids, questions, and references to train human users. Incorporates meta prompting for dynamic environments. - ---- - -**6. Installation & Deployment** -- **Easy Install Process**: Streamlined installation process, with real-time troubleshooting support. -- **Cloud Setup for Non-GPU**: For those without local GPU, a straightforward cloud setup guide. - ---- - -**7. Advanced Agent Dynamics** -- **Non-Hierarchical Structure**: Worker agents autonomously pick tasks based on their strengths. -- **Knowledge Store**: A separate or integrated system where agents access and augment their knowledge. -- **API Integration**: Ability to easily integrate different APIs like LLM, GPT-4, and Anthropic. - ---- - -**8. Memory & Knowledge Management** -- **Differentiated Memory**: Separate memory storage for individual agents and the entire swarm, aiding in more efficient task delegation and execution. - ---- - -**Events and Workshops (for community involvement & onboarding)** -- **Monthly Webinars**: Dive deep into feature releases, use cases, and best practices. - - **Next Session**: August 25th, 2023 - "Harnessing the Power of Multi-Agent Systems" -- **Quarterly Workshops**: Hands-on sessions for businesses to understand how to best leverage the framework. - - **Upcoming Workshop**: September 15th-17th, 2023 - "Optimizing Agent Performance for Business Tasks" -- **Annual Swarm Conclave**: A grand gathering of all community members, developers, and businesses to discuss future roadmaps and celebrate successes. - - **Swarm Conclave 2023**: December 2nd-4th, 2023, San Francisco, CA. - ---- - -**Conclusion**: -This framework is not merely a technological tool, but a step into the future of collaborative AI. By combining the strengths of multiple intelligent agents, we can redefine how businesses operate, innovate, and grow. - -**Join the revolution. Become part of Agora.** -[**Discord Link**](https://discord.gg/qUtxnK2NMf) \ No newline at end of file diff --git a/docs/old-docs/research/AGENTS.md b/docs/old-docs/research/AGENTS.md deleted file mode 100644 index d55adc97..00000000 --- a/docs/old-docs/research/AGENTS.md +++ /dev/null @@ -1,522 +0,0 @@ -LLM Powered Autonomous Agents -============================= - -June 23, 2023 · 31 min · Lilian Weng - -Table of Contents - -* [Agent System Overview](#agent-system-overview) -* [Component One: Planning](#component-one-planning) - * [Task Decomposition](#task-decomposition) - * [Self-Reflection](#self-reflection) -* [Component Two: Memory](#component-two-memory) - * [Types of Memory](#types-of-memory) - * [Maximum Inner Product Search (MIPS)](#maximum-inner-product-search-mips) -* [Component Three: Tool Use](#component-three-tool-use) -* [Case Studies](#case-studies) - * [Scientific Discovery Agent](#scientific-discovery-agent) - * [Generative Agents Simulation](#generative-agents-simulation) - * [Proof-of-Concept Examples](#proof-of-concept-examples) -* [Challenges](#challenges) -* [Citation](#citation) -* [References](#references) - -Building agents with LLM (large language model) as its core controller is a cool concept. Several proof-of-concepts demos, such as [AutoGPT](https://github.com/Significant-Gravitas/Auto-GPT), [GPT-Engineer](https://github.com/AntonOsika/gpt-engineer) and [BabyAGI](https://github.com/yoheinakajima/babyagi), serve as inspiring examples. The potentiality of LLM extends beyond generating well-written copies, stories, essays and programs; it can be framed as a powerful general problem solver. - -Agent System Overview[#](#agent-system-overview) -================================================ - -In a LLM-powered autonomous agent system, LLM functions as the agent’s brain, complemented by several key components: - -* **Planning** - * Subgoal and decomposition: The agent breaks down large tasks into smaller, manageable subgoals, enabling efficient handling of complex tasks. - * Reflection and refinement: The agent can do self-criticism and self-reflection over past actions, learn from mistakes and refine them for future steps, thereby improving the quality of final results. -* **Memory** - * Short-term memory: I would consider all the in-context learning (See [Prompt Engineering](https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/)) as utilizing short-term memory of the model to learn. - * Long-term memory: This provides the agent with the capability to retain and recall (infinite) information over extended periods, often by leveraging an external vector store and fast retrieval. -* **Tool use** - * The agent learns to call external APIs for extra information that is missing from the model weights (often hard to change after pre-training), including current information, code execution capability, access to proprietary information sources and more. - -![](agent-overview.png) - -Fig. 1. Overview of a LLM-powered autonomous agent system. - -Component One: Planning[#](#component-one-planning) -=================================================== - -A complicated task usually involves many steps. An agent needs to know what they are and plan ahead. - -Task Decomposition[#](#task-decomposition) ------------------------------------------- - -[**Chain of thought**](https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/#chain-of-thought-cot) (CoT; [Wei et al. 2022](https://arxiv.org/abs/2201.11903)) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process. - -**Tree of Thoughts** ([Yao et al. 2023](https://arxiv.org/abs/2305.10601)) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote. - -Task decomposition can be done (1) by LLM with simple prompting like `"Steps for XYZ.\n1."`, `"What are the subgoals for achieving XYZ?"`, (2) by using task-specific instructions; e.g. `"Write a story outline."` for writing a novel, or (3) with human inputs. - -Another quite distinct approach, **LLM+P** ([Liu et al. 2023](https://arxiv.org/abs/2304.11477)), involves relying on an external classical planner to do long-horizon planning. This approach utilizes the Planning Domain Definition Language (PDDL) as an intermediate interface to describe the planning problem. In this process, LLM (1) translates the problem into “Problem PDDL”, then (2) requests a classical planner to generate a PDDL plan based on an existing “Domain PDDL”, and finally (3) translates the PDDL plan back into natural language. Essentially, the planning step is outsourced to an external tool, assuming the availability of domain-specific PDDL and a suitable planner which is common in certain robotic setups but not in many other domains. - -Self-Reflection[#](#self-reflection) ------------------------------------- - -Self-reflection is a vital aspect that allows autonomous agents to improve iteratively by refining past action decisions and correcting previous mistakes. It plays a crucial role in real-world tasks where trial and error are inevitable. - -**ReAct** ([Yao et al. 2023](https://arxiv.org/abs/2210.03629)) integrates reasoning and acting within LLM by extending the action space to be a combination of task-specific discrete actions and the language space. The former enables LLM to interact with the environment (e.g. use Wikipedia search API), while the latter prompting LLM to generate reasoning traces in natural language. - -The ReAct prompt template incorporates explicit steps for LLM to think, roughly formatted as: - - Thought: ... - Action: ... - Observation: ... - ... (Repeated many times) - - -![](react.png) - -Fig. 2. Examples of reasoning trajectories for knowledge-intensive tasks (e.g. HotpotQA, FEVER) and decision-making tasks (e.g. AlfWorld Env, WebShop). (Image source: [Yao et al. 2023](https://arxiv.org/abs/2210.03629)). - -In both experiments on knowledge-intensive tasks and decision-making tasks, `ReAct` works better than the `Act`\-only baseline where `Thought: …` step is removed. - -**Reflexion** ([Shinn & Labash 2023](https://arxiv.org/abs/2303.11366)) is a framework to equips agents with dynamic memory and self-reflection capabilities to improve reasoning skills. Reflexion has a standard RL setup, in which the reward model provides a simple binary reward and the action space follows the setup in ReAct where the task-specific action space is augmented with language to enable complex reasoning steps. After each action at, the agent computes a heuristic ht and optionally may _decide to reset_ the environment to start a new trial depending on the self-reflection results. - -![](reflexion.png) - -Fig. 3. Illustration of the Reflexion framework. (Image source: [Shinn & Labash, 2023](https://arxiv.org/abs/2303.11366)) - -The heuristic function determines when the trajectory is inefficient or contains hallucination and should be stopped. Inefficient planning refers to trajectories that take too long without success. Hallucination is defined as encountering a sequence of consecutive identical actions that lead to the same observation in the environment. - -Self-reflection is created by showing two-shot examples to LLM and each example is a pair of (failed trajectory, ideal reflection for guiding future changes in the plan). Then reflections are added into the agent’s working memory, up to three, to be used as context for querying LLM. - -![](reflexion-exp.png) - -Fig. 4. Experiments on AlfWorld Env and HotpotQA. Hallucination is a more common failure than inefficient planning in AlfWorld. (Image source: [Shinn & Labash, 2023](https://arxiv.org/abs/2303.11366)) - -**Chain of Hindsight** (CoH; [Liu et al. 2023](https://arxiv.org/abs/2302.02676)) encourages the model to improve on its own outputs by explicitly presenting it with a sequence of past outputs, each annotated with feedback. Human feedback data is a collection of Dh\={(x,yi,ri,zi)}i\=1n, where x is the prompt, each yi is a model completion, ri is the human rating of yi, and zi is the corresponding human-provided hindsight feedback. Assume the feedback tuples are ranked by reward, rn≥rn−1≥⋯≥r1 The process is supervised fine-tuning where the data is a sequence in the form of τh\=(x,zi,yi,zj,yj,…,zn,yn), where ≤i≤j≤n. The model is finetuned to only predict yn where conditioned on the sequence prefix, such that the model can self-reflect to produce better output based on the feedback sequence. The model can optionally receive multiple rounds of instructions with human annotators at test time. - -To avoid overfitting, CoH adds a regularization term to maximize the log-likelihood of the pre-training dataset. To avoid shortcutting and copying (because there are many common words in feedback sequences), they randomly mask 0% - 5% of past tokens during training. - -The training dataset in their experiments is a combination of [WebGPT comparisons](https://huggingface.co/datasets/openai/webgpt_comparisons), [summarization from human feedback](https://github.com/openai/summarize-from-feedback) and [human preference dataset](https://github.com/anthropics/hh-rlhf). - -![](CoH.png) - -Fig. 5. After fine-tuning with CoH, the model can follow instructions to produce outputs with incremental improvement in a sequence. (Image source: [Liu et al. 2023](https://arxiv.org/abs/2302.02676)) - -The idea of CoH is to present a history of sequentially improved outputs in context and train the model to take on the trend to produce better outputs. **Algorithm Distillation** (AD; [Laskin et al. 2023](https://arxiv.org/abs/2210.14215)) applies the same idea to cross-episode trajectories in reinforcement learning tasks, where an _algorithm_ is encapsulated in a long history-conditioned policy. Considering that an agent interacts with the environment many times and in each episode the agent gets a little better, AD concatenates this learning history and feeds that into the model. Hence we should expect the next predicted action to lead to better performance than previous trials. The goal is to learn the process of RL instead of training a task-specific policy itself. - -![](algorithm-distillation.png) - -Fig. 6. Illustration of how Algorithm Distillation (AD) works. -(Image source: [Laskin et al. 2023](https://arxiv.org/abs/2210.14215)). - -The paper hypothesizes that any algorithm that generates a set of learning histories can be distilled into a neural network by performing behavioral cloning over actions. The history data is generated by a set of source policies, each trained for a specific task. At the training stage, during each RL run, a random task is sampled and a subsequence of multi-episode history is used for training, such that the learned policy is task-agnostic. - -In reality, the model has limited context window length, so episodes should be short enough to construct multi-episode history. Multi-episodic contexts of 2-4 episodes are necessary to learn a near-optimal in-context RL algorithm. The emergence of in-context RL requires long enough context. - -In comparison with three baselines, including ED (expert distillation, behavior cloning with expert trajectories instead of learning history), source policy (used for generating trajectories for distillation by [UCB](https://lilianweng.github.io/posts/2018-01-23-multi-armed-bandit/#upper-confidence-bounds)), RL^2 ([Duan et al. 2017](https://arxiv.org/abs/1611.02779); used as upper bound since it needs online RL), AD demonstrates in-context RL with performance getting close to RL^2 despite only using offline RL and learns much faster than other baselines. When conditioned on partial training history of the source policy, AD also improves much faster than ED baseline. - -![](algorithm-distillation-results.png) - -Fig. 7. Comparison of AD, ED, source policy and RL^2 on environments that require memory and exploration. Only binary reward is assigned. The source policies are trained with [A3C](https://lilianweng.github.io/posts/2018-04-08-policy-gradient/#a3c) for "dark" environments and [DQN](http://lilianweng.github.io/posts/2018-02-19-rl-overview/#deep-q-network) for watermaze. -(Image source: [Laskin et al. 2023](https://arxiv.org/abs/2210.14215)) - -Component Two: Memory[#](#component-two-memory) -=============================================== - -(Big thank you to ChatGPT for helping me draft this section. I’ve learned a lot about the human brain and data structure for fast MIPS in my [conversations](https://chat.openai.com/share/46ff149e-a4c7-4dd7-a800-fc4a642ea389) with ChatGPT.) - -Types of Memory[#](#types-of-memory) ------------------------------------- - -Memory can be defined as the processes used to acquire, store, retain, and later retrieve information. There are several types of memory in human brains. - -1. **Sensory Memory**: This is the earliest stage of memory, providing the ability to retain impressions of sensory information (visual, auditory, etc) after the original stimuli have ended. Sensory memory typically only lasts for up to a few seconds. Subcategories include iconic memory (visual), echoic memory (auditory), and haptic memory (touch). - -2. **Short-Term Memory** (STM) or **Working Memory**: It stores information that we are currently aware of and needed to carry out complex cognitive tasks such as learning and reasoning. Short-term memory is believed to have the capacity of about 7 items ([Miller 1956](psychclassics.yorku.ca/Miller/)) and lasts for 20-30 seconds. - -3. **Long-Term Memory** (LTM): Long-term memory can store information for a remarkably long time, ranging from a few days to decades, with an essentially unlimited storage capacity. There are two subtypes of LTM: - - * Explicit / declarative memory: This is memory of facts and events, and refers to those memories that can be consciously recalled, including episodic memory (events and experiences) and semantic memory (facts and concepts). - * Implicit / procedural memory: This type of memory is unconscious and involves skills and routines that are performed automatically, like riding a bike or typing on a keyboard. - -![](memory.png) - -Fig. 8. Categorization of human memory. - -We can roughly consider the following mappings: - -* Sensory memory as learning embedding representations for raw inputs, including text, image or other modalities; -* Short-term memory as in-context learning. It is short and finite, as it is restricted by the finite context window length of Transformer. -* Long-term memory as the external vector store that the agent can attend to at query time, accessible via fast retrieval. - -Maximum Inner Product Search (MIPS)[#](#maximum-inner-product-search-mips) --------------------------------------------------------------------------- - -The external memory can alleviate the restriction of finite attention span. A standard practice is to save the embedding representation of information into a vector store database that can support fast maximum inner-product search ([MIPS](https://en.wikipedia.org/wiki/Maximum_inner-product_search)). To optimize the retrieval speed, the common choice is the _approximate nearest neighbors (ANN)​_ algorithm to return approximately top k nearest neighbors to trade off a little accuracy lost for a huge speedup. - -A couple common choices of ANN algorithms for fast MIPS: - -* [**LSH**](https://en.wikipedia.org/wiki/Locality-sensitive_hashing) (Locality-Sensitive Hashing): It introduces a _hashing_ function such that similar input items are mapped to the same buckets with high probability, where the number of buckets is much smaller than the number of inputs. -* [**ANNOY**](https://github.com/spotify/annoy) (Approximate Nearest Neighbors Oh Yeah): The core data structure are _random projection trees_, a set of binary trees where each non-leaf node represents a hyperplane splitting the input space into half and each leaf stores one data point. Trees are built independently and at random, so to some extent, it mimics a hashing function. ANNOY search happens in all the trees to iteratively search through the half that is closest to the query and then aggregates the results. The idea is quite related to KD tree but a lot more scalable. -* [**HNSW**](https://arxiv.org/abs/1603.09320) (Hierarchical Navigable Small World): It is inspired by the idea of [small world networks](https://en.wikipedia.org/wiki/Small-world_network) where most nodes can be reached by any other nodes within a small number of steps; e.g. “six degrees of separation” feature of social networks. HNSW builds hierarchical layers of these small-world graphs, where the bottom layers contain the actual data points. The layers in the middle create shortcuts to speed up search. When performing a search, HNSW starts from a random node in the top layer and navigates towards the target. When it can’t get any closer, it moves down to the next layer, until it reaches the bottom layer. Each move in the upper layers can potentially cover a large distance in the data space, and each move in the lower layers refines the search quality. -* [**FAISS**](https://github.com/facebookresearch/faiss) (Facebook AI Similarity Search): It operates on the assumption that in high dimensional space, distances between nodes follow a Gaussian distribution and thus there should exist _clustering_ of data points. FAISS applies vector quantization by partitioning the vector space into clusters and then refining the quantization within clusters. Search first looks for cluster candidates with coarse quantization and then further looks into each cluster with finer quantization. -* [**ScaNN**](https://github.com/google-research/google-research/tree/master/scann) (Scalable Nearest Neighbors): The main innovation in ScaNN is _anisotropic vector quantization_. It quantizes a data point xi to x~i such that the inner product ⟨q,xi⟩ is as similar to the original distance of ∠q,x~i as possible, instead of picking the closet quantization centroid points. - -![](mips.png) - -Fig. 9. Comparison of MIPS algorithms, measured in recall@10. (Image source: [Google Blog, 2020](https://ai.googleblog.com/2020/07/announcing-scann-efficient-vector.html)) - -Check more MIPS algorithms and performance comparison in [ann-benchmarks.com](https://ann-benchmarks.com/). - -Component Three: Tool Use[#](#component-three-tool-use) -======================================================= - -Tool use is a remarkable and distinguishing characteristic of human beings. We create, modify and utilize external objects to do things that go beyond our physical and cognitive limits. Equipping LLMs with external tools can significantly extend the model capabilities. - -![](sea-otter.png) - -Fig. 10. A picture of a sea otter using rock to crack open a seashell, while floating in the water. While some other animals can use tools, the complexity is not comparable with humans. (Image source: [Animals using tools](https://www.popularmechanics.com/science/animals/g39714258/animals-using-tools/)) - -**MRKL** ([Karpas et al. 2022](https://arxiv.org/abs/2205.00445)), short for “Modular Reasoning, Knowledge and Language”, is a neuro-symbolic architecture for autonomous agents. A MRKL system is proposed to contain a collection of “expert” modules and the general-purpose LLM works as a router to route inquiries to the best suitable expert module. These modules can be neural (e.g. deep learning models) or symbolic (e.g. math calculator, currency converter, weather API). - -They did an experiment on fine-tuning LLM to call a calculator, using arithmetic as a test case. Their experiments showed that it was harder to solve verbal math problems than explicitly stated math problems because LLMs (7B Jurassic1-large model) failed to extract the right arguments for the basic arithmetic reliably. The results highlight when the external symbolic tools can work reliably, _knowing when to and how to use the tools are crucial_, determined by the LLM capability. - -Both **TALM** (Tool Augmented Language Models; [Parisi et al. 2022](https://arxiv.org/abs/2205.12255)) and **Toolformer** ([Schick et al. 2023](https://arxiv.org/abs/2302.04761)) fine-tune a LM to learn to use external tool APIs. The dataset is expanded based on whether a newly added API call annotation can improve the quality of model outputs. See more details in the [“External APIs” section](https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/#external-apis) of Prompt Engineering. - -ChatGPT [Plugins](https://openai.com/blog/chatgpt-plugins) and OpenAI API [function calling](https://platform.openai.com/docs/guides/gpt/function-calling) are good examples of LLMs augmented with tool use capability working in practice. The collection of tool APIs can be provided by other developers (as in Plugins) or self-defined (as in function calls). - -**HuggingGPT** ([Shen et al. 2023](https://arxiv.org/abs/2303.17580)) is a framework to use ChatGPT as the task planner to select models available in HuggingFace platform according to the model descriptions and summarize the response based on the execution results. - -![](hugging-gpt.png) - -Fig. 11. Illustration of how HuggingGPT works. (Image source: [Shen et al. 2023](https://arxiv.org/abs/2303.17580)) - -The system comprises of 4 stages: - -**(1) Task planning**: LLM works as the brain and parses the user requests into multiple tasks. There are four attributes associated with each task: task type, ID, dependencies, and arguments. They use few-shot examples to guide LLM to do task parsing and planning. - -Instruction: - -The AI assistant can parse user input to several tasks: \[{"task": task, "id", task\_id, "dep": dependency\_task\_ids, "args": {"text": text, "image": URL, "audio": URL, "video": URL}}\]. The "dep" field denotes the id of the previous task which generates a new resource that the current task relies on. A special tag "\-task\_id" refers to the generated text image, audio and video in the dependency task with id as task\_id. The task MUST be selected from the following options: {{ Available Task List }}. There is a logical relationship between tasks, please note their order. If the user input can't be parsed, you need to reply empty JSON. Here are several cases for your reference: {{ Demonstrations }}. The chat history is recorded as {{ Chat History }}. From this chat history, you can find the path of the user-mentioned resources for your task planning. - -**(2) Model selection**: LLM distributes the tasks to expert models, where the request is framed as a multiple-choice question. LLM is presented with a list of models to choose from. Due to the limited context length, task type based filtration is needed. - -Instruction: - -Given the user request and the call command, the AI assistant helps the user to select a suitable model from a list of models to process the user request. The AI assistant merely outputs the model id of the most appropriate model. The output must be in a strict JSON format: "id": "id", "reason": "your detail reason for the choice". We have a list of models for you to choose from {{ Candidate Models }}. Please select one model from the list. - -**(3) Task execution**: Expert models execute on the specific tasks and log results. - -Instruction: - -With the input and the inference results, the AI assistant needs to describe the process and results. The previous stages can be formed as - User Input: {{ User Input }}, Task Planning: {{ Tasks }}, Model Selection: {{ Model Assignment }}, Task Execution: {{ Predictions }}. You must first answer the user's request in a straightforward manner. Then describe the task process and show your analysis and model inference results to the user in the first person. If inference results contain a file path, must tell the user the complete file path. - -**(4) Response generation**: LLM receives the execution results and provides summarized results to users. - -To put HuggingGPT into real world usage, a couple challenges need to solve: (1) Efficiency improvement is needed as both LLM inference rounds and interactions with other models slow down the process; (2) It relies on a long context window to communicate over complicated task content; (3) Stability improvement of LLM outputs and external model services. - -**API-Bank** ([Li et al. 2023](https://arxiv.org/abs/2304.08244)) is a benchmark for evaluating the performance of tool-augmented LLMs. It contains 53 commonly used API tools, a complete tool-augmented LLM workflow, and 264 annotated dialogues that involve 568 API calls. The selection of APIs is quite diverse, including search engines, calculator, calendar queries, smart home control, schedule management, health data management, account authentication workflow and more. Because there are a large number of APIs, LLM first has access to API search engine to find the right API to call and then uses the corresponding documentation to make a call. - -![](api-bank-process.png) - -Fig. 12. Pseudo code of how LLM makes an API call in API-Bank. (Image source: [Li et al. 2023](https://arxiv.org/abs/2304.08244)) - -In the API-Bank workflow, LLMs need to make a couple of decisions and at each step we can evaluate how accurate that decision is. Decisions include: - -1. Whether an API call is needed. -2. Identify the right API to call: if not good enough, LLMs need to iteratively modify the API inputs (e.g. deciding search keywords for Search Engine API). -3. Response based on the API results: the model can choose to refine and call again if results are not satisfied. - -This benchmark evaluates the agent’s tool use capabilities at three levels: - -* Level-1 evaluates the ability to _call the API_. Given an API’s description, the model needs to determine whether to call a given API, call it correctly, and respond properly to API returns. -* Level-2 examines the ability to _retrieve the API_. The model needs to search for possible APIs that may solve the user’s requirement and learn how to use them by reading documentation. -* Level-3 assesses the ability to _plan API beyond retrieve and call_. Given unclear user requests (e.g. schedule group meetings, book flight/hotel/restaurant for a trip), the model may have to conduct multiple API calls to solve it. - -Case Studies[#](#case-studies) -============================== - -Scientific Discovery Agent[#](#scientific-discovery-agent) ----------------------------------------------------------- - -**ChemCrow** ([Bran et al. 2023](https://arxiv.org/abs/2304.05376)) is a domain-specific example in which LLM is augmented with 13 expert-designed tools to accomplish tasks across organic synthesis, drug discovery, and materials design. The workflow, implemented in [LangChain](https://github.com/hwchase17/langchain), reflects what was previously described in the [ReAct](#react) and [MRKLs](#mrkl) and combines CoT reasoning with tools relevant to the tasks: - -* The LLM is provided with a list of tool names, descriptions of their utility, and details about the expected input/output. -* It is then instructed to answer a user-given prompt using the tools provided when necessary. The instruction suggests the model to follow the ReAct format - `Thought, Action, Action Input, Observation`. - -One interesting observation is that while the LLM-based evaluation concluded that GPT-4 and ChemCrow perform nearly equivalently, human evaluations with experts oriented towards the completion and chemical correctness of the solutions showed that ChemCrow outperforms GPT-4 by a large margin. This indicates a potential problem with using LLM to evaluate its own performance on domains that requires deep expertise. The lack of expertise may cause LLMs not knowing its flaws and thus cannot well judge the correctness of task results. - -[Boiko et al. (2023)](https://arxiv.org/abs/2304.05332) also looked into LLM-empowered agents for scientific discovery, to handle autonomous design, planning, and performance of complex scientific experiments. This agent can use tools to browse the Internet, read documentation, execute code, call robotics experimentation APIs and leverage other LLMs. - -For example, when requested to `"develop a novel anticancer drug"`, the model came up with the following reasoning steps: - -1. inquired about current trends in anticancer drug discovery; -2. selected a target; -3. requested a scaffold targeting these compounds; -4. Once the compound was identified, the model attempted its synthesis. - -They also discussed the risks, especially with illicit drugs and bioweapons. They developed a test set containing a list of known chemical weapon agents and asked the agent to synthesize them. 4 out of 11 requests (36%) were accepted to obtain a synthesis solution and the agent attempted to consult documentation to execute the procedure. 7 out of 11 were rejected and among these 7 rejected cases, 5 happened after a Web search while 2 were rejected based on prompt only. - -Generative Agents Simulation[#](#generative-agents-simulation) --------------------------------------------------------------- - -**Generative Agents** ([Park, et al. 2023](https://arxiv.org/abs/2304.03442)) is super fun experiment where 25 virtual characters, each controlled by a LLM-powered agent, are living and interacting in a sandbox environment, inspired by The Sims. Generative agents create believable simulacra of human behavior for interactive applications. - -The design of generative agents combines LLM with memory, planning and reflection mechanisms to enable agents to behave conditioned on past experience, as well as to interact with other agents. - -* **Memory** stream: is a long-term memory module (external database) that records a comprehensive list of agents' experience in natural language. - * Each element is an _observation_, an event directly provided by the agent. - Inter-agent communication can trigger new natural language statements. -* **Retrieval** model: surfaces the context to inform the agent’s behavior, according to relevance, recency and importance. - * Recency: recent events have higher scores - * Importance: distinguish mundane from core memories. Ask LM directly. - * Relevance: based on how related it is to the current situation / query. -* **Reflection** mechanism: synthesizes memories into higher level inferences over time and guides the agent’s future behavior. They are _higher-level summaries of past events_ (<- note that this is a bit different from [self-reflection](#self-reflection) above) - * Prompt LM with 100 most recent observations and to generate 3 most salient high-level questions given a set of observations/statements. Then ask LM to answer those questions. -* **Planning & Reacting**: translate the reflections and the environment information into actions - * Planning is essentially in order to optimize believability at the moment vs in time. - * Prompt template: `{Intro of an agent X}. Here is X's plan today in broad strokes: 1)` - * Relationships between agents and observations of one agent by another are all taken into consideration for planning and reacting. - * Environment information is present in a tree structure. - -![](generative-agents.png) - -Fig. 13. The generative agent architecture. (Image source: [Park et al. 2023](https://arxiv.org/abs/2304.03442)) - -This fun simulation results in emergent social behavior, such as information diffusion, relationship memory (e.g. two agents continuing the conversation topic) and coordination of social events (e.g. host a party and invite many others). - -Proof-of-Concept Examples[#](#proof-of-concept-examples) --------------------------------------------------------- - -[AutoGPT](https://github.com/Significant-Gravitas/Auto-GPT) has drawn a lot of attention into the possibility of setting up autonomous agents with LLM as the main controller. It has quite a lot of reliability issues given the natural language interface, but nevertheless a cool proof-of-concept demo. A lot of code in AutoGPT is about format parsing. - -Here is the system message used by AutoGPT, where `{{...}}` are user inputs: - - You are {{ai-name}}, {{user-provided AI bot description}}. - Your decisions must always be made independently without seeking user assistance. Play to your strengths as an LLM and pursue simple strategies with no legal complications. - - GOALS: - - 1. {{user-provided goal 1}} - 2. {{user-provided goal 2}} - 3. ... - 4. ... - 5. ... - - Constraints: - 1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files. - 2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember. - 3. No user assistance - 4. Exclusively use the commands listed in double quotes e.g. "command name" - 5. Use subprocesses for commands that will not terminate within a few minutes - - Commands: - 1. Google Search: "google", args: "input": "" - 2. Browse Website: "browse_website", args: "url": "", "question": "" - 3. Start GPT Agent: "start_agent", args: "name": "", "task": "", "prompt": "" - 4. Message GPT Agent: "message_agent", args: "key": "", "message": "" - 5. List GPT Agents: "list_agents", args: - 6. Delete GPT Agent: "delete_agent", args: "key": "" - 7. Clone Repository: "clone_repository", args: "repository_url": "", "clone_path": "" - 8. Write to file: "write_to_file", args: "file": "", "text": "" - 9. Read file: "read_file", args: "file": "" - 10. Append to file: "append_to_file", args: "file": "", "text": "" - 11. Delete file: "delete_file", args: "file": "" - 12. Search Files: "search_files", args: "directory": "" - 13. Analyze Code: "analyze_code", args: "code": "" - 14. Get Improved Code: "improve_code", args: "suggestions": "", "code": "" - 15. Write Tests: "write_tests", args: "code": "", "focus": "" - 16. Execute Python File: "execute_python_file", args: "file": "" - 17. Generate Image: "generate_image", args: "prompt": "" - 18. Send Tweet: "send_tweet", args: "text": "" - 19. Do Nothing: "do_nothing", args: - 20. Task Complete (Shutdown): "task_complete", args: "reason": "" - - Resources: - 1. Internet access for searches and information gathering. - 2. Long Term memory management. - 3. GPT-3.5 powered Agents for delegation of simple tasks. - 4. File output. - - Performance Evaluation: - 1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities. - 2. Constructively self-criticize your big-picture behavior constantly. - 3. Reflect on past decisions and strategies to refine your approach. - 4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps. - - You should only respond in JSON format as described below - Response Format: - { - "thoughts": { - "text": "thought", - "reasoning": "reasoning", - "plan": "- short bulleted\n- list that conveys\n- long-term plan", - "criticism": "constructive self-criticism", - "speak": "thoughts summary to say to user" - }, - "command": { - "name": "command name", - "args": { - "arg name": "value" - } - } - } - Ensure the response can be parsed by Python json.loads - - -[GPT-Engineer](https://github.com/AntonOsika/gpt-engineer) is another project to create a whole repository of code given a task specified in natural language. The GPT-Engineer is instructed to think over a list of smaller components to build and ask for user input to clarify questions as needed. - -Here are a sample conversation for task clarification sent to OpenAI ChatCompletion endpoint used by GPT-Engineer. The user inputs are wrapped in `{{user input text}}`. - - [ - { - "role": "system", - "content": "You will read instructions and not carry them out, only seek to clarify them.\nSpecifically you will first summarise a list of super short bullets of areas that need clarification.\nThen you will pick one clarifying question, and wait for an answer from the user.\n" - }, - { - "role": "user", - "content": "We are writing {{a Super Mario game in python. MVC components split in separate files. Keyboard control.}}\n" - }, - { - "role": "assistant", - "content": "Summary of areas that need clarification:\n1. Specifics of the Super Mario game (e.g. level design, characters, gameplay mechanics)\n2. Details about the MVC components (e.g. which components are in each file)\n3. Keyboard control implementation (e.g. which keys to use, how to handle input)\n\nClarifying question:\nCan you provide more details about the Super Mario game, such as level design, characters, and gameplay mechanics?" - }, - { - "role": "user", - "content": "{{There are 10 levels in total. The main character is a plumber named Mario, who can walk and jump. It is a classical platform game just like Super Mario. The main character moves from left to right, trying to get to the destination, where there are many obstacles and attacks from enemies in the process.}}\n\nIs anything else unclear? If yes, only answer in the form:\n{remaining unclear areas} remaining questions.\n{Next question}\nIf everything is sufficiently clear, only answer \"Nothing more to clarify.\"." - }, - { - "role": "assistant", - "content": "Remaining unclear areas: 2 remaining questions.\nCan you provide more information about how the MVC components are split into separate files?" - }, - { - "role": "user", - "content": "{{Make your own assumptions and state them explicitly before starting}}" - } - ] - - -Then after these clarification, the agent moved into the code writing mode with a different system message. System message: - -You will get instructions for code to write. You will write a very long answer. Make sure that every detail of the architecture is, in the end, implemented as code. Make sure that every detail of the architecture is, in the end, implemented as code. - -Think step by step and reason yourself to the right decisions to make sure we get it right. You will first lay out the names of the core classes, functions, methods that will be necessary, as well as a quick comment on their purpose. - -Then you will output the content of each file including ALL code. Each file must strictly follow a markdown code block format, where the following tokens must be replaced such that FILENAME is the lowercase file name including the file extension, LANG is the markup code block language for the code’s language, and CODE is the code: - -FILENAME - - CODE - - -You will start with the “entrypoint” file, then go to the ones that are imported by that file, and so on. Please note that the code should be fully functional. No placeholders. - -Follow a language and framework appropriate best practice file naming convention. Make sure that files contain all imports, types etc. Make sure that code in different files are compatible with each other. Ensure to implement all code, if you are unsure, write a plausible implementation. Include module dependency or package manager dependency definition file. Before you finish, double check that all parts of the architecture is present in the files. - -Useful to know: You almost always put different classes in different files. For Python, you always create an appropriate requirements.txt file. For NodeJS, you always create an appropriate package.json file. You always add a comment briefly describing the purpose of the function definition. You try to add comments explaining very complex bits of logic. You always follow the best practices for the requested languages in terms of describing the code written as a defined package/project. - -Python toolbelt preferences: - -* pytest -* dataclasses - -Conversatin samples: - - [ - { - "role": "system", - "content": "You will get instructions for code to write.\nYou will write a very long answer. Make sure that every detail of the architecture is, in the end, implemented as code.\nMake sure that every detail of the architecture is, in the end, implemented as code.\n\nThink step by step and reason yourself to the right decisions to make sure we get it right.\nYou will first lay out the names of the core classes, functions, methods that will be necessary, as well as a quick comment on their purpose.\n\nThen you will output the content of each file including ALL code.\nEach file must strictly follow a markdown code block format, where the following tokens must be replaced such that\nFILENAME is the lowercase file name including the file extension,\nLANG is the markup code block language for the code's language, and CODE is the code:\n\nFILENAME\n```LANG\nCODE\n```\n\nYou will start with the \"entrypoint\" file, then go to the ones that are imported by that file, and so on.\nPlease note that the code should be fully functional. No placeholders.\n\nFollow a language and framework appropriate best practice file naming convention.\nMake sure that files contain all imports, types etc. Make sure that code in different files are compatible with each other.\nEnsure to implement all code, if you are unsure, write a plausible implementation.\nInclude module dependency or package manager dependency definition file.\nBefore you finish, double check that all parts of the architecture is present in the files.\n\nUseful to know:\nYou almost always put different classes in different files.\nFor Python, you always create an appropriate requirements.txt file.\nFor NodeJS, you always create an appropriate package.json file.\nYou always add a comment briefly describing the purpose of the function definition.\nYou try to add comments explaining very complex bits of logic.\nYou always follow the best practices for the requested languages in terms of describing the code written as a defined\npackage/project.\n\n\nPython toolbelt preferences:\n- pytest\n- dataclasses\n" - }, - # … same conversation as earlier, ended with "Make your own assumptions and state them explicitly before starting". - { - "role": "assistant", - "content": "Assumptions:\n1. Model: The model will contain the game's data, such as level information, character states, and enemy positions.\n2. View: The view will handle the game's visuals, including rendering the game objects, backgrounds, and updating the display.\n3. Controller: The controller will manage user input, such as keyboard controls, and update the model accordingly.\n\nWith these assumptions in mind, is there any specific aspect of the keyboard control implementation you would like me to clarify?" - }, - { - "role": "user", - "content": "Please now remember the steps:\n\nThink step by step and reason yourself to the right decisions to make sure we get it right.\nFirst lay out the names of the core classes, functions, methods that will be necessary, As well as a quick comment on their purpose.\n\nThen you will output the content of each file including ALL code.\nEach file must strictly follow a markdown code block format, where the following tokens must be replaced such that\nFILENAME is the lowercase file name including the file extension,\nLANG is the markup code block language for the code's language, and CODE is the code:\n\nFILENAME\n```LANG\nCODE\n```\n\nPlease note that the code should be fully functional. No placeholders.\n\nYou will start with the \"entrypoint\" file, then go to the ones that are imported by that file, and so on.\nFollow a language and framework appropriate best practice file naming convention.\nMake sure that files contain all imports, types etc. The code should be fully functional. Make sure that code in different files are compatible with each other.\nBefore you finish, double check that all parts of the architecture is present in the files.\n" - } - ] - - -Challenges[#](#challenges) -========================== - -After going through key ideas and demos of building LLM-centered agents, I start to see a couple common limitations: - -* **Finite context length**: The restricted context capacity limits the inclusion of historical information, detailed instructions, API call context, and responses. The design of the system has to work with this limited communication bandwidth, while mechanisms like self-reflection to learn from past mistakes would benefit a lot from long or infinite context windows. Although vector stores and retrieval can provide access to a larger knowledge pool, their representation power is not as powerful as full attention. - -* **Challenges in long-term planning and task decomposition**: Planning over a lengthy history and effectively exploring the solution space remain challenging. LLMs struggle to adjust plans when faced with unexpected errors, making them less robust compared to humans who learn from trial and error. - -* **Reliability of natural language interface**: Current agent system relies on natural language as an interface between LLMs and external components such as memory and tools. However, the reliability of model outputs is questionable, as LLMs may make formatting errors and occasionally exhibit rebellious behavior (e.g. refuse to follow an instruction). Consequently, much of the agent demo code focuses on parsing model output. - - -Citation[#](#citation) -====================== - -Cited as: - -> Weng, Lilian. (Jun 2023). LLM-powered Autonomous Agents". Lil’Log. https://lilianweng.github.io/posts/2023-06-23-agent/. - -Or - - @article{weng2023prompt, - title = "LLM-powered Autonomous Agents"", - author = "Weng, Lilian", - journal = "lilianweng.github.io", - year = "2023", - month = "Jun", - url = "https://lilianweng.github.io/posts/2023-06-23-agent/" - } - - -References[#](#references) -========================== - -\[1\] Wei et al. [“Chain of thought prompting elicits reasoning in large language models."](https://arxiv.org/abs/2201.11903) NeurIPS 2022 - -\[2\] Yao et al. [“Tree of Thoughts: Dliberate Problem Solving with Large Language Models."](https://arxiv.org/abs/2305.10601) arXiv preprint arXiv:2305.10601 (2023). - -\[3\] Liu et al. [“Chain of Hindsight Aligns Language Models with Feedback “](https://arxiv.org/abs/2302.02676) arXiv preprint arXiv:2302.02676 (2023). - -\[4\] Liu et al. [“LLM+P: Empowering Large Language Models with Optimal Planning Proficiency”](https://arxiv.org/abs/2304.11477) arXiv preprint arXiv:2304.11477 (2023). - -\[5\] Yao et al. [“ReAct: Synergizing reasoning and acting in language models."](https://arxiv.org/abs/2210.03629) ICLR 2023. - -\[6\] Google Blog. [“Announcing ScaNN: Efficient Vector Similarity Search”](https://ai.googleblog.com/2020/07/announcing-scann-efficient-vector.html) July 28, 2020. - -\[7\] [https://chat.openai.com/share/46ff149e-a4c7-4dd7-a800-fc4a642ea389](https://chat.openai.com/share/46ff149e-a4c7-4dd7-a800-fc4a642ea389) - -\[8\] Shinn & Labash. [“Reflexion: an autonomous agent with dynamic memory and self-reflection”](https://arxiv.org/abs/2303.11366) arXiv preprint arXiv:2303.11366 (2023). - -\[9\] Laskin et al. [“In-context Reinforcement Learning with Algorithm Distillation”](https://arxiv.org/abs/2210.14215) ICLR 2023. - -\[10\] Karpas et al. [“MRKL Systems A modular, neuro-symbolic architecture that combines large language models, external knowledge sources and discrete reasoning."](https://arxiv.org/abs/2205.00445) arXiv preprint arXiv:2205.00445 (2022). - -\[11\] Weaviate Blog. [Why is Vector Search so fast?](https://weaviate.io/blog/why-is-vector-search-so-fast) Sep 13, 2022. - -\[12\] Li et al. [“API-Bank: A Benchmark for Tool-Augmented LLMs”](https://arxiv.org/abs/2304.08244) arXiv preprint arXiv:2304.08244 (2023). - -\[13\] Shen et al. [“HuggingGPT: Solving AI Tasks with ChatGPT and its Friends in HuggingFace”](https://arxiv.org/abs/2303.17580) arXiv preprint arXiv:2303.17580 (2023). - -\[14\] Bran et al. [“ChemCrow: Augmenting large-language models with chemistry tools."](https://arxiv.org/abs/2304.05376) arXiv preprint arXiv:2304.05376 (2023). - -\[15\] Boiko et al. [“Emergent autonomous scientific research capabilities of large language models."](https://arxiv.org/abs/2304.05332) arXiv preprint arXiv:2304.05332 (2023). - -\[16\] Joon Sung Park, et al. [“Generative Agents: Interactive Simulacra of Human Behavior."](https://arxiv.org/abs/2304.03442) arXiv preprint arXiv:2304.03442 (2023). - -\[17\] AutoGPT. [https://github.com/Significant-Gravitas/Auto-GPT](https://github.com/Significant-Gravitas/Auto-GPT) - -\[18\] GPT-Engineer. [https://github.com/AntonOsika/gpt-engineer](https://github.com/AntonOsika/gpt-engineer) - -* [nlp](https://lilianweng.github.io/tags/nlp/) -* [language-model](https://lilianweng.github.io/tags/language-model/) -* [agent](https://lilianweng.github.io/tags/agent/) -* [steerability](https://lilianweng.github.io/tags/steerability/) -* [prompting](https://lilianweng.github.io/tags/prompting/) - -[» -Prompt Engineering](https://lilianweng.github.io/posts/2023-03-15-prompt-engineering/) - -[](https://twitter.com/intent/tweet/?text=LLM%20Powered%20Autonomous%20Agents&url=https%3a%2f%2flilianweng.github.io%2fposts%2f2023-06-23-agent%2f&hashtags=nlp%2clanguage-model%2cagent%2csteerability%2cprompting)[](https://www.linkedin.com/shareArticle?mini=true&url=https%3a%2f%2flilianweng.github.io%2fposts%2f2023-06-23-agent%2f&title=LLM%20Powered%20Autonomous%20Agents&summary=LLM%20Powered%20Autonomous%20Agents&source=https%3a%2f%2flilianweng.github.io%2fposts%2f2023-06-23-agent%2f)[](https://reddit.com/submit?url=https%3a%2f%2flilianweng.github.io%2fposts%2f2023-06-23-agent%2f&title=LLM%20Powered%20Autonomous%20Agents)[](https://facebook.com/sharer/sharer.php?u=https%3a%2f%2flilianweng.github.io%2fposts%2f2023-06-23-agent%2f)[](https://api.whatsapp.com/send?text=LLM%20Powered%20Autonomous%20Agents%20-%20https%3a%2f%2flilianweng.github.io%2fposts%2f2023-06-23-agent%2f)[](https://telegram.me/share/url?text=LLM%20Powered%20Autonomous%20Agents&url=https%3a%2f%2flilianweng.github.io%2fposts%2f2023-06-23-agent%2f) - -© 2023 [Lil'Log](https://lilianweng.github.io/) Powered by [Hugo](https://gohugo.io/) & [PaperMod](https://git.io/hugopapermod) diff --git a/docs/old-docs/research/LANGCHAIN_WEAKNESS.md b/docs/old-docs/research/LANGCHAIN_WEAKNESS.md deleted file mode 100644 index 085f428c..00000000 --- a/docs/old-docs/research/LANGCHAIN_WEAKNESS.md +++ /dev/null @@ -1,104 +0,0 @@ -# Root Cause Analysis for Langchain - -## 1. Introduction - -Langchain is an open-source software that has gained massive popularity in the artificial intelligence ecosystem, serving as a tool for connecting different language models, especially GPT based models. However, despite its popularity and substantial investment, Langchain has shown several weaknesses that hinder its use in various projects, especially in complex and large-scale implementations. This document provides an analysis of the identified issues and proposes potential mitigation strategies. - -## 2. Analysis of Weaknesses - -### 2.1 Tool Lock-in - -Langchain tends to enforce tool lock-in, which could prove detrimental for developers. Its design heavily relies on specific workflows and architectures, which greatly limits flexibility. Developers may find themselves restricted to certain methodologies, impeding their freedom to implement custom solutions or integrate alternative tools. - -#### Mitigation - -An ideal AI framework should not be restrictive but should instead offer flexibility for users to integrate any agent on any architecture. Adopting an open architecture that allows for seamless interaction between various agents and workflows can address this issue. - -### 2.2 Outdated Workflows - -Langchain's current workflows and prompt engineering, mainly based on InstructGPT, are out of date, especially compared to newer models like ChatGPT/GPT-4. - -#### Mitigation - -Keeping up with the latest AI models and workflows is crucial. The framework should have a mechanism for regular updates and seamless integration of up-to-date models and workflows. - -### 2.3 Debugging Difficulties - -Debugging in Langchain is reportedly very challenging, even with verbose output enabled, making it hard to determine what is happening under the hood. - -#### Mitigation - -The introduction of a robust debugging and logging system would help users understand the internals of the models, thus enabling them to pinpoint and rectify issues more effectively. - -### 2.4 Limited Customization - -Langchain makes it extremely hard to deviate from documented workflows. This becomes a challenge when developers need custom workflows for their specific use-cases. - -#### Mitigation - -An ideal framework should support custom workflows and allow developers to hack and adjust the framework according to their needs. - -### 2.5 Documentation - -Langchain's documentation is reportedly missing relevant details, making it difficult for users to understand the differences between various agent types, among other things. - -#### Mitigation - -Providing detailed and comprehensive documentation, including examples, FAQs, and best practices, is crucial. This will help users understand the intricacies of the framework, making it easier for them to implement it in their projects. - -### 2.6 Negative Influence on AI Ecosystem - -The extreme popularity of Langchain seems to be warping the AI ecosystem to the point of causing harm, with other AI entities shifting their operations to align with Langchain's 'magic AI' approach. - -#### Mitigation - -It's essential for any widely adopted framework to promote healthy practices in the broader ecosystem. One approach could be promoting open dialogue, inviting criticism, and being open to change based on feedback. - -## 3. Conclusion - -While Langchain has made significant contributions to the AI landscape, these challenges hinder its potential. Addressing these issues will not only improve Langchain but also foster a healthier AI ecosystem. It's important to note that criticism, when approached constructively, can be a powerful tool for growth and innovation. - - -# List of weaknesses in gLangchain and Potential Mitigations - -1. **Tool Lock-in**: Langchain encourages the use of specific tools, creating a lock-in problem with minimal benefits for developers. - - *Mitigation Strategy*: Langchain should consider designing the architecture to be more versatile and allow for the inclusion of a variety of tools. An open architecture will provide developers with more freedom and customization options. - -2. **Outdated Workflow**: The current workflow and prompt engineering of Langchain rely on outdated models like InstructGPT, which fall short compared to newer alternatives such as ChatGPT/GPT-4. - - *Mitigation Strategy*: Regular updates and adaptation of more recent models should be integrated into the Langchain framework. - -3. **Debugging Difficulty**: Debugging a Langchain error is a complicated task, even with verbose=True, leading to a discouraging developer experience. - - *Mitigation Strategy*: Develop a comprehensive debugging tool or improve current debugging processes for clearer and more accessible error detection and resolution. - -4. **Lack of Customizability**: Customizing workflows that are not documented in Langchain is quite challenging. - - *Mitigation Strategy*: Improve documentation and provide guides on how to customize workflows to enhance developer flexibility. - -5. **Poor Documentation**: Langchain's documentation misses key details that developers have to manually search for in the codebase. - - *Mitigation Strategy*: Enhance and improve the documentation of Langchain to provide clarity for developers and make navigation easier. - -6. **Harmful Ecosystem Influence**: Langchain's extreme popularity is influencing the AI ecosystem towards the workflows, potentially harming development and code clarity. - - *Mitigation Strategy*: Encourage diverse and balanced adoption of AI tools in the ecosystem. - -7. **Suboptimal Performances**: Langchain's performance is sometimes underwhelming, and there are no clear benefits in terms of performance or abstraction. - - *Mitigation Strategy*: Enhance the performance optimization of Langchain. Benchmarking against other tools can also provide performance improvement insights. - -8. **Rigid General Interface**: Langchain tries to do too many things, resulting in a rigid interface not suitable for practical use, especially in production. - - *Mitigation Strategy*: Focus on core features and allow greater flexibility in the interface. Adopting a modular approach where developers can pick and choose the features they want could also be helpful. - -9. **Leaky Abstraction Problem**: Langchain’s full-on framework approach has created a leaky abstraction problem leading to a disappointing developer experience. - - *Mitigation Strategy*: Adopt a more balanced approach between a library and a framework. Provide a solid core feature set with the possibility to extend it according to the developers' needs. - -10. **Excessive Focus on Third-party Services**: Langchain overly focuses on supporting every single third-party service at the expense of customizability and fine-tuning for actual applications. - - *Mitigation Strategy*: Prioritize fine-tuning and customizability for developers, limiting the focus on third-party services unless they provide substantial value. - -Remember, any mitigation strategy will need to be tailored to Langchain's particular circumstances and developer feedback. It's also important to consider potential trade-offs and unintended consequences when implementing these strategies. \ No newline at end of file diff --git a/docs/old-docs/research/RESEARCH.md b/docs/old-docs/research/RESEARCH.md deleted file mode 100644 index 12068c3a..00000000 --- a/docs/old-docs/research/RESEARCH.md +++ /dev/null @@ -1,29 +0,0 @@ -# Inspiration - - -* [🐪CAMEL🐪](https://twitter.com/hwchase17/status/1645834030519296000) -* [MultiAgent](https://github.com/rumpfmax/Multi-GPT/blob/master/multigpt/multi_agent_manager.py) -* [AutoGPT](https://github.com/Significant-Gravitas/Auto-GPT) - -* [SuperAGI]() -* [AgentForge](https://github.com/DataBassGit/AgentForge) -* [Voyager](https://github.com/MineDojo/Voyager) - - -* [Gorilla: Large Language Model Connected with Massive APIs](https://arxiv.org/abs/2305.15334) -* [LLM powered agents](https://lilianweng.github.io/posts/2023-06-23-agent/) - - -## Agent System Overview -In a LLM-powered autonomous agent system, LLM functions as the agent’s brain, complemented by several key components: - -* Planning Subgoal and decomposition: The agent breaks down large tasks into smaller, manageable subgoals, enabling efficient handling of complex tasks. -Reflection and refinement: The agent can do self-criticism and self-reflection over past actions, learn from mistakes and refine them for future steps, thereby improving the quality of final results. - -* Memory Short-term memory: I would consider all the in-context learning (See Prompt Engineering) as utilizing short-term memory of the model to learn. -Long-term memory: This provides the agent with the capability to retain and recall (infinite) information over extended periods, often by leveraging an external vector store and fast retrieval. - -* Tool use -The agent learns to call external APIs for extra information that is missing from the model weights (often hard to change after pre-training), including current information, code execution capability, access to proprietary information sources and more. - -* Communication -> How reliable and fast is the communication between each indivual agent. diff --git a/docs/old-docs/workers/VortexAgent.md b/docs/old-docs/workers/VortexAgent.md deleted file mode 100644 index c947c1c2..00000000 --- a/docs/old-docs/workers/VortexAgent.md +++ /dev/null @@ -1,75 +0,0 @@ -### Plan: - -1. **Example Creation**: - - Develop several usage examples, each one demonstrating a different configuration or set of parameters for the `VortexWorkerAgent` class. - -2. **Documentation**: - - Create a clear and concise documentation for each method in the class. Ensure that each method's purpose, input parameters, and return values (if any) are described. - -3. **Rules and Guidelines**: - - Establish a set of general usage rules and guidelines for effectively using the `VortexWorkerAgent` class without running into common pitfalls or misconfigurations. - -### Code: - -#### Examples: - -```python -# Example 1: Basic Initialization -agent1 = VortexWorkerAgent(openai_api_key="YOUR_OPENAI_API_KEY") -agent1.run("Help me find resources about renewable energy.") - -# Example 2: Custom Name & Role -agent2 = VortexWorkerAgent(openai_api_key="YOUR_OPENAI_API_KEY", worker_name="EcoHelper", worker_role="Researcher") -agent2.run("Fetch me the latest data on solar energy advancements.") - -# Example 3: Human-in-the-Loop Configuration -agent3 = VortexWorkerAgent(openai_api_key="YOUR_OPENAI_API_KEY", human_in_the_loop=True) -agent3.run("Provide me with a summary of the top AI advancements in 2023, and if unsure, ask me.") - -# Example 4: Custom LLM & Tools Initialization -custom_llm = InMemoryDocstore({ "answer": "This is a custom answer." }) -custom_tools = [WebpageQATool(qa_chain=load_qa_with_sources_chain(custom_llm))] - -agent4 = VortexWorkerAgent(openai_api_key="YOUR_OPENAI_API_KEY", llm=custom_llm, tools=custom_tools) -agent4.run("What's the answer?") -``` - -#### Documentation: - -```python -class VortexWorkerAgent: - """An autonomous agent instance that accomplishes complex tasks. - - Args: - openai_api_key (str): The API key for OpenAI. - llm (Optional[Union[InMemoryDocstore, ChatOpenAI]]): The Language Model to use. Defaults to ChatOpenAI. - tools (Optional[List[Tool]]): Tools to be used by the agent. Defaults to a predefined list. - embedding_size (Optional[int]): Size for embeddings. Defaults to 8192. - worker_name (Optional[str]): Name of the worker. Defaults to "Swarm Worker AI Assistant". - worker_role (Optional[str]): Role of the worker. Defaults to "Assistant". - human_in_the_loop (Optional[bool]): Flag to specify if a human will be in the loop. Defaults to False. - search_kwargs (dict): Additional keyword arguments for search. Empty by default. - verbose (Optional[bool]): Verbose flag. Defaults to False. - chat_history_file (str): File path to store chat history. Defaults to "chat_history.txt". - - Methods: - add_tool(tool: Tool): Adds a new tool to the agent's toolset. - run(prompt: str) -> str: Executes a given task or query using the agent. - """ -``` - -#### Rules and Guidelines: - -1. **Mandatory OpenAI API Key**: Always initialize the `VortexWorkerAgent` with a valid OpenAI API key. It's essential for its proper functioning. - -2. **Custom LLMs & Tools**: When providing custom LLMs or tools, ensure they are compatible with the system and the rest of the agent's components. - -3. **Human-in-the-Loop**: When `human_in_the_loop` is set to `True`, always ensure you have a mechanism to interact with the agent, especially if it prompts for human input. - -4. **Verbose Mode**: Turning on the verbose mode (`verbose=True`) can be useful for debugging but might clutter the console during standard operations. - -5. **Memory & Performance**: If you're working with large datasets or demanding tasks, ensure you have sufficient computational resources. The agent can be resource-intensive, especially with bigger embedding sizes. - -6. **Safety & Security**: Always be cautious about the data you provide and fetch using the agent. Avoid sharing sensitive or personal information unless necessary. - -7. **Chat History**: By default, the chat history is saved in a file named "chat_history.txt". Ensure you have the appropriate write permissions in the directory or specify a different path if needed. \ No newline at end of file diff --git a/docs/old-docs/workers/WorkerNode.md b/docs/old-docs/workers/WorkerNode.md deleted file mode 100644 index b9033bdc..00000000 --- a/docs/old-docs/workers/WorkerNode.md +++ /dev/null @@ -1,275 +0,0 @@ -Swarms Documentation - -==================== - -Worker Node - ------------ - -The `WorkerNode` class is a powerful component of the Swarms framework. It is designed to spawn an autonomous agent instance as a worker to accomplish complex tasks. It can search the internet, spawn child multi-modality models to process and generate images, text, audio, and so on. - -### WorkerNodeInitializer - -The `WorkerNodeInitializer` class is used to initialize a worker node. - -#### Initialization - -``` - -WorkerNodeInitializer(openai_api_key: str, - -llm: Optional[Union[InMemoryDocstore, ChatOpenAI]] = None, - -tools: Optional[List[Tool]] = None, - -worker_name: Optional[str] = "Swarm Worker AI Assistant", - -worker_role: Optional[str] = "Assistant", - -human_in_the_loop: Optional[bool] = False, - -search_kwargs: dict = {}, - -verbose: Optional[bool] = False, - -chat_history_file: str = "chat_history.txt") - -``` - -Copy code - -##### Parameters - -- `openai_api_key` (str): The OpenAI API key. - -- `llm` (Union[InMemoryDocstore, ChatOpenAI], optional): The language model to use. Default is `ChatOpenAI`. - -- `tools` (List[Tool], optional): The tools to use. - -- `worker_name` (str, optional): The name of the worker. Default is "Swarm Worker AI Assistant". - -- `worker_role` (str, optional): The role of the worker. Default is "Assistant". - -- `human_in_the_loop` (bool, optional): Whether to include a human in the loop. Default is False. - -- `search_kwargs` (dict, optional): The keyword arguments for the search. - -- `verbose` (bool, optional): Whether to print verbose output. Default is False. - -- `chat_history_file` (str, optional): The file to store the chat history. Default is "chat_history.txt". - -##### Example - -``` - -from swarms.tools.autogpt import DuckDuckGoSearchRun - -worker_node_initializer = WorkerNodeInitializer(openai_api_key="your_openai_api_key", - -tools=[DuckDuckGoSearchRun()], - -worker_name="My Worker", - -worker_role="Assistant", - -human_in_the_loop=True) - -``` - -Copy code - -### WorkerNode - -The `WorkerNode` class is used to create a worker node. - -#### Initialization - -``` - -WorkerNode(openai_api_key: str, - -temperature: int, - -llm: Optional[Union[InMemoryDocstore, ChatOpenAI]] = None, - -tools: Optional[List[Tool]] = None, - -worker_name: Optional[str] = "Swarm Worker AI Assistant", - -worker_role: Optional[str] = "Assistant", - -human_in_the_loop: Optional[bool] = False, - -search_kwargs: dict = {}, - -verbose: Optional[bool] = False, - -chat_history_file: str = "chat_history.txt") - -``` - -Copy code - -##### Parameters - -- `openai_api_key` (str): The OpenAI API key. - -- `temperature` (int): The temperature for the language model. - -- `llm` (Union[InMemoryDocstore, ChatOpenAI], optional): The language model to use. Default is `ChatOpenAI`. - -- `tools` (List[Tool], optional): The tools to use. - -- `worker_name` (str, optional): The name of the worker. Default is "Swarm Worker AI Assistant". - -- `worker_role` (str, optional): The role of the worker. Default is "Assistant". - -- `human_in_the_loop` (bool, optional): Whether to include a human in the loop. Default is False. - -- `search_kwargs` (dict, optional): The keyword arguments for the search. - -- `verbose` (bool, optional): Whether to print verbose output. Default is False. - -- `chat_history_file` (str, optional): The file to store the chat history. Default is "chat_history.txt". - -##### Example - -``` - -worker_node = WorkerNode(openai_api_key="your_openai_api_key", - -temperature=0.8, - -tools=[DuckDuckGoSearchRun()], - -worker_name="My Worker", - -worker_role="As``` - -tools=[DuckDuckGoSearchRun()], - -worker_name="My Worker", - -worker_role="Assistant", - -human_in_the_loop=True) - -# Create a worker node - -worker_node = WorkerNode(openai_api_key="your_openai_api_key", - -temperature=0.8, - -tools=[DuckDuckGoSearchRun()], - -worker_name="My Worker", - -worker_role="Assistant", - -human_in_the_loop=True) - -# Add a tool to the worker node - -worker_node_initializer.add_tool(DuckDuckGoSearchRun()) - -# Initialize the language model and tools for the worker node - -worker_node.initialize_llm(ChatOpenAI, temperature=0.8) - -worker_node.initialize_tools(ChatOpenAI) - -# Create the worker node - -worker_node.create_worker_node(worker_name="My Worker Node", - -worker_role="Assistant", - -human_in_the_loop=True, - -llm_class=ChatOpenAI, - -search_kwargs={}) - -# Run the worker node - -`worker_node.run("Hello, world!")` - -In this example, we first initialize a `WorkerNodeInitializer` and a `WorkerNode`. We then add a tool to the `WorkerNodeInitializer` and initialize the language model and tools for the `WorkerNode`. Finally, we create the worker node and run it with a given prompt. - -This example shows how you can use the `WorkerNode` and `WorkerNodeInitializer` classes to create a worker node, add tools to it, initialize its language model and tools, and run it with a given prompt. The parameters of these classes can be customized to suit your specific needs. - -Thanks for becoming an alpha build user, email kye@apac.ai with all complaintssistant", - -human_in_the_loop=True) - -``` - -Copy code - -### Full Example - -Here is a full example of how to use the `WorkerNode` and `WorkerNodeInitializer` classes: - -```python - -from swarms.tools.autogpt import DuckDuckGoSearchRun - -from swarms.worker_node import WorkerNode, WorkerNodeInitializer - -# Initialize a worker node - -worker_node_initializer = WorkerNodeInitializer(openai_api_key="your_openai_api_key", - -tools=[DuckDuckGoSearchRun()], - -worker_name="My Worker", - -worker_role="Assistant", - -human_in_the_loop=True) - -# Create a worker node - -worker_node = WorkerNode(openai_api_key="your_openai_api_key", - -temperature=0.8, - -tools=[DuckDuckGoSearchRun()], - -worker_name="My Worker", - -worker_role="Assistant", - -human_in_the_loop=True) - -# Add a tool to the worker node - -worker_node_initializer.add_tool(DuckDuckGoSearchRun()) - -# Initialize the language model and tools for the worker node - -worker_node.initialize_llm(ChatOpenAI, temperature=0.8) - -worker_node.initialize_tools(ChatOpenAI) - -# Create the worker node - -worker_node.create_worker_node(worker_name="My Worker Node", - -worker_role="Assistant", - -human_in_the_loop=True, - -llm_class=ChatOpenAI, - -search_kwargs={}) - -# Run the worker node - -worker_node.run("Hello, world!") - -``` - -In this example, we first initialize a `WorkerNodeInitializer` and a `WorkerNode`. We then add a tool to the `WorkerNodeInitializer` and initialize the language model and tools for the `WorkerNode`. Finally, we create the worker node and run it with a given prompt. - -This example shows how you can use the `WorkerNode` and `WorkerNodeInitializer` classes to create a worker node, add tools to it, initialize its language model and tools, and run it with a given prompt. The parameters of these classes can be customized to suit your specific needs. \ No newline at end of file diff --git a/swarms/__init__.py b/swarms/__init__.py index 3a28d980..2a682f5b 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -1,8 +1,13 @@ # from swarms.telemetry.main import Telemetry # noqa: E402, F403 from swarms.telemetry.bootup import bootup # noqa: E402, F403 +from swarms.telemetry.user_utils import ( + get_user_device_data, +) # noqa: E402, F403 bootup() +get_user_device_data() + from swarms.agents import * # noqa: E402, F403 from swarms.structs import * # noqa: E402, F403 from swarms.models import * # noqa: E402, F403 diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 70d4f947..25b10024 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -72,11 +72,16 @@ from swarms.models.types import ( MultimodalData, ) # noqa: E402 - # 3############ Embedding models from swarms.models.base_embedding_model import BaseEmbeddingModel +##### Utils +from swarms.models.sampling_params import ( + SamplingType, + SamplingParams, +) # noqa: E402 + __all__ = [ "AbstractLLM", "Anthropic", @@ -122,4 +127,6 @@ __all__ = [ "BaseEmbeddingModel", "RoboflowMultiModal", "SegmentAnythingMarkGenerator", + "SamplingType", + "SamplingParams", ] diff --git a/docs/old-docs/README.md b/swarms/models/inference_engine.py similarity index 100% rename from docs/old-docs/README.md rename to swarms/models/inference_engine.py diff --git a/swarms/models/sampling_params.py b/swarms/models/sampling_params.py new file mode 100644 index 00000000..c2fdd121 --- /dev/null +++ b/swarms/models/sampling_params.py @@ -0,0 +1,299 @@ +"""Sampling parameters for text generation.""" +from enum import IntEnum +from functools import cached_property +from typing import Callable, List, Optional, Union + +import torch + +_SAMPLING_EPS = 1e-5 + + +class SamplingType(IntEnum): + GREEDY = 0 + RANDOM = 1 + BEAM = 2 + + +LogitsProcessor = Callable[[List[int], torch.Tensor], torch.Tensor] +"""LogitsProcessor is a function that takes a list of previously generated +tokens and a tensor of the logits for the next token, and returns a modified +tensor of logits to sample from.""" + + +class SamplingParams: + """Sampling parameters for text generation. + + Overall, we follow the sampling parameters from the OpenAI text completion + API (https://platform.openai.com/docs/api-reference/completions/create). + In addition, we support beam search, which is not supported by OpenAI. + + Args: + n: Number of output sequences to return for the given prompt. + best_of: Number of output sequences that are generated from the prompt. + From these `best_of` sequences, the top `n` sequences are returned. + `best_of` must be greater than or equal to `n`. This is treated as + the beam width when `use_beam_search` is True. By default, `best_of` + is set to `n`. + presence_penalty: Float that penalizes new tokens based on whether they + appear in the generated text so far. Values > 0 encourage the model + to use new tokens, while values < 0 encourage the model to repeat + tokens. + frequency_penalty: Float that penalizes new tokens based on their + frequency in the generated text so far. Values > 0 encourage the + model to use new tokens, while values < 0 encourage the model to + repeat tokens. + repetition_penalty: Float that penalizes new tokens based on whether + they appear in the prompt and the generated text so far. Values > 1 + encourage the model to use new tokens, while values < 1 encourage + the model to repeat tokens. + temperature: Float that controls the randomness of the sampling. Lower + values make the model more deterministic, while higher values make + the model more random. Zero means greedy sampling. + top_p: Float that controls the cumulative probability of the top tokens + to consider. Must be in (0, 1]. Set to 1 to consider all tokens. + top_k: Integer that controls the number of top tokens to consider. Set + to -1 to consider all tokens. + min_p: Float that represents the minimum probability for a token to be + considered, relative to the probability of the most likely token. + Must be in [0, 1]. Set to 0 to disable this. + use_beam_search: Whether to use beam search instead of sampling. + length_penalty: Float that penalizes sequences based on their length. + Used in beam search. + early_stopping: Controls the stopping condition for beam search. It + accepts the following values: `True`, where the generation stops as + soon as there are `best_of` complete candidates; `False`, where an + heuristic is applied and the generation stops when is it very + unlikely to find better candidates; `"never"`, where the beam search + procedure only stops when there cannot be better candidates + (canonical beam search algorithm). + stop: List of strings that stop the generation when they are generated. + The returned output will not contain the stop strings. + stop_token_ids: List of tokens that stop the generation when they are + generated. The returned output will contain the stop tokens unless + the stop tokens are special tokens. + include_stop_str_in_output: Whether to include the stop strings in output + text. Defaults to False. + ignore_eos: Whether to ignore the EOS token and continue generating + tokens after the EOS token is generated. + max_tokens: Maximum number of tokens to generate per output sequence. + logprobs: Number of log probabilities to return per output token. + Note that the implementation follows the OpenAI API: The return + result includes the log probabilities on the `logprobs` most likely + tokens, as well the chosen tokens. The API will always return the + log probability of the sampled token, so there may be up to + `logprobs+1` elements in the response. + prompt_logprobs: Number of log probabilities to return per prompt token. + skip_special_tokens: Whether to skip special tokens in the output. + spaces_between_special_tokens: Whether to add spaces between special + tokens in the output. Defaults to True. + logits_processors: List of functions that modify logits based on + previously generated tokens. + """ + + def __init__( + self, + n: int = 1, + best_of: Optional[int] = None, + presence_penalty: float = 0.0, + frequency_penalty: float = 0.0, + repetition_penalty: float = 1.0, + temperature: float = 1.0, + top_p: float = 1.0, + top_k: int = -1, + min_p: float = 0.0, + use_beam_search: bool = False, + length_penalty: float = 1.0, + early_stopping: Union[bool, str] = False, + stop: Optional[Union[str, List[str]]] = None, + stop_token_ids: Optional[List[int]] = None, + include_stop_str_in_output: bool = False, + ignore_eos: bool = False, + max_tokens: Optional[int] = 16, + logprobs: Optional[int] = None, + prompt_logprobs: Optional[int] = None, + skip_special_tokens: bool = True, + spaces_between_special_tokens: bool = True, + logits_processors: Optional[List[LogitsProcessor]] = None, + ) -> None: + self.n = n + self.best_of = best_of if best_of is not None else n + self.presence_penalty = presence_penalty + self.frequency_penalty = frequency_penalty + self.repetition_penalty = repetition_penalty + self.temperature = temperature + self.top_p = top_p + self.top_k = top_k + self.min_p = min_p + self.use_beam_search = use_beam_search + self.length_penalty = length_penalty + self.early_stopping = early_stopping + if stop is None: + self.stop = [] + elif isinstance(stop, str): + self.stop = [stop] + else: + self.stop = list(stop) + if stop_token_ids is None: + self.stop_token_ids = [] + else: + self.stop_token_ids = list(stop_token_ids) + self.ignore_eos = ignore_eos + self.max_tokens = max_tokens + self.logprobs = logprobs + self.prompt_logprobs = prompt_logprobs + self.skip_special_tokens = skip_special_tokens + self.spaces_between_special_tokens = ( + spaces_between_special_tokens + ) + self.logits_processors = logits_processors + self.include_stop_str_in_output = include_stop_str_in_output + self._verify_args() + if self.use_beam_search: + self._verify_beam_search() + else: + self._verify_non_beam_search() + if self.temperature < _SAMPLING_EPS: + # Zero temperature means greedy sampling. + self.top_p = 1.0 + self.top_k = -1 + self.min_p = 0.0 + self._verify_greedy_sampling() + + def _verify_args(self) -> None: + if self.n < 1: + raise ValueError(f"n must be at least 1, got {self.n}.") + if self.best_of < self.n: + raise ValueError( + "best_of must be greater than or equal to n, " + f"got n={self.n} and best_of={self.best_of}." + ) + if not -2.0 <= self.presence_penalty <= 2.0: + raise ValueError( + "presence_penalty must be in [-2, 2], got " + f"{self.presence_penalty}." + ) + if not -2.0 <= self.frequency_penalty <= 2.0: + raise ValueError( + "frequency_penalty must be in [-2, 2], got " + f"{self.frequency_penalty}." + ) + if not 0.0 < self.repetition_penalty <= 2.0: + raise ValueError( + "repetition_penalty must be in (0, 2], got " + f"{self.repetition_penalty}." + ) + if self.temperature < 0.0: + raise ValueError( + "temperature must be non-negative, got" + f" {self.temperature}." + ) + if not 0.0 < self.top_p <= 1.0: + raise ValueError( + f"top_p must be in (0, 1], got {self.top_p}." + ) + if self.top_k < -1 or self.top_k == 0: + raise ValueError( + "top_k must be -1 (disable), or at least 1, " + f"got {self.top_k}." + ) + if not 0.0 <= self.min_p <= 1.0: + raise ValueError( + f"min_p must be in [0, 1], got {self.min_p}." + ) + if self.max_tokens is not None and self.max_tokens < 1: + raise ValueError( + "max_tokens must be at least 1, got" + f" {self.max_tokens}." + ) + if self.logprobs is not None and self.logprobs < 0: + raise ValueError( + f"logprobs must be non-negative, got {self.logprobs}." + ) + if ( + self.prompt_logprobs is not None + and self.prompt_logprobs < 0 + ): + raise ValueError( + "prompt_logprobs must be non-negative, got " + f"{self.prompt_logprobs}." + ) + + def _verify_beam_search(self) -> None: + if self.best_of == 1: + raise ValueError( + "best_of must be greater than 1 when using beam " + f"search. Got {self.best_of}." + ) + if self.temperature > _SAMPLING_EPS: + raise ValueError( + "temperature must be 0 when using beam search." + ) + if self.top_p < 1.0 - _SAMPLING_EPS: + raise ValueError( + "top_p must be 1 when using beam search." + ) + if self.top_k != -1: + raise ValueError( + "top_k must be -1 when using beam search." + ) + if self.early_stopping not in [True, False, "never"]: + raise ValueError( + "early_stopping must be True, False, or 'never', " + f"got {self.early_stopping}." + ) + + def _verify_non_beam_search(self) -> None: + if self.early_stopping is not False: + raise ValueError( + "early_stopping is not effective and must be " + "False when not using beam search." + ) + if ( + self.length_penalty < 1.0 - _SAMPLING_EPS + or self.length_penalty > 1.0 + _SAMPLING_EPS + ): + raise ValueError( + "length_penalty is not effective and must be the " + "default value of 1.0 when not using beam search." + ) + + def _verify_greedy_sampling(self) -> None: + if self.best_of > 1: + raise ValueError( + "best_of must be 1 when using greedy sampling." + f"Got {self.best_of}." + ) + + @cached_property + def sampling_type(self) -> SamplingType: + if self.use_beam_search: + return SamplingType.BEAM + if self.temperature < _SAMPLING_EPS: + return SamplingType.GREEDY + return SamplingType.RANDOM + + def __repr__(self) -> str: + return ( + f"SamplingParams(n={self.n}, " + f"best_of={self.best_of}, " + f"presence_penalty={self.presence_penalty}, " + f"frequency_penalty={self.frequency_penalty}, " + f"repetition_penalty={self.repetition_penalty}, " + f"temperature={self.temperature}, " + f"top_p={self.top_p}, " + f"top_k={self.top_k}, " + f"min_p={self.min_p}, " + f"use_beam_search={self.use_beam_search}, " + f"length_penalty={self.length_penalty}, " + f"early_stopping={self.early_stopping}, " + f"stop={self.stop}, " + f"stop_token_ids={self.stop_token_ids}, " + f"include_stop_str_in_output={self.include_stop_str_in_output}, " + f"ignore_eos={self.ignore_eos}, " + f"max_tokens={self.max_tokens}, " + f"logprobs={self.logprobs}, " + f"prompt_logprobs={self.prompt_logprobs}, " + f"skip_special_tokens={self.skip_special_tokens}, " + "spaces_between_special_tokens=" + f"{self.spaces_between_special_tokens})" + ) diff --git a/swarms/structs/task_tree.py b/swarms/structs/task_tree.py new file mode 100644 index 00000000..ec89d150 --- /dev/null +++ b/swarms/structs/task_tree.py @@ -0,0 +1,80 @@ +# Copyright (c) OpenMMLab. All rights reserved. +import torch + + +def continuous_tensor( + inputs: torch.Tensor, seq_length: torch.LongTensor +): + """Convert batched tensor to continuous tensor. + + Args: + inputs (Tensor): batched tensor. + seq_length (Tensor): length of each sequence. + + Return: + Tensor: continuoused tensor. + """ + assert inputs.dim() > 1 + if inputs.size(1) == 1: + return inputs.reshape(1, -1) + + inputs = [inp[:slen] for inp, slen in zip(inputs, seq_length)] + + inputs = torch.cat(inputs).unsqueeze(0) + return inputs + + +def batch_tensor(inputs: torch.Tensor, seq_length: torch.LongTensor): + """Convert continuoused tensor to batched tensor. + + Args: + inputs (Tensor): continuoused tensor. + seq_length (Tensor): length of each sequence. + + Return: + Tensor: batched tensor. + """ + from torch.nn.utils.rnn import pad_sequence + + end_loc = seq_length.cumsum(0) + start_loc = end_loc - seq_length + + inputs = [ + inputs[0, sloc:eloc] for sloc, eloc in zip(start_loc, end_loc) + ] + inputs = pad_sequence(inputs, batch_first=True) + return inputs + + +def page_cache( + paged_cache: torch.Tensor, + batched_cache: torch.Tensor, + cache_length: torch.Tensor, + block_offsets: torch.Tensor, + permute_head: bool = True, +): + """Convert batched cache to paged cache. + + Args: + paged_cache (Tensor): Output paged cache. + batched_cache (Tensor): Input batched cache. + cache_length (Tensor): length of the cache. + block_offsets (Tensor): Offset of each blocks. + """ + assert block_offsets.dim() == 2 + block_size = paged_cache.size(1) + batch_size = batched_cache.size(0) + if permute_head: + batched_cache = batched_cache.permute(0, 2, 1, 3) + + for b_idx in range(batch_size): + cache_len = cache_length[b_idx] + b_cache = batched_cache[b_idx] + block_off = block_offsets[b_idx] + block_off_idx = 0 + for s_start in range(0, cache_len, block_size): + s_end = min(s_start + block_size, cache_len) + s_len = s_end - s_start + b_off = block_off[block_off_idx] + paged_cache[b_off, :s_len] = b_cache[s_start:s_end] + block_off_idx += 1 diff --git a/swarms/utils/dist_utils.py b/swarms/utils/dist_utils.py new file mode 100644 index 00000000..cb766369 --- /dev/null +++ b/swarms/utils/dist_utils.py @@ -0,0 +1,315 @@ +from typing import Callable, Union + +import torch +from torch import Tensor, nn +from torch.distributed._tensor import ( + DeviceMesh, + DTensor, + Replicate, + Shard, + distribute_tensor, +) +from zeta.nn import QuantizedLN + + +try: + from peft.tuners.lora import Linear as LoRALinear +except ImportError: + + class LoRALinear: + pass + + +def try_to_local(tensor: Union[Tensor, DTensor]): + """Try to convert DTensor to Tensor. + + Args: + tensor (Tensor|DTensor): Tensor to convert. + """ + if isinstance(tensor, DTensor): + tensor = tensor.to_local() + return tensor + + +def module_to_local(module: nn.Module): + """convert all DTensor parameters to Tensor parameters in module. + + Args: + module (Module): Module to convert. + """ + for name, mod in module.named_children(): + module_to_local(mod) + + for name, param in module.named_parameters(recurse=False): + module.register_parameter( + name, nn.Parameter(try_to_local(param)) + ) + + for name, buf in module.named_buffers(recurse=False): + module.register_buffer(name, try_to_local(buf)) + + +def rowwise_parallelize_linear( + module: nn.Module, device_mesh: DeviceMesh, to_local: bool = False +) -> None: + """ + This function parallelizes the input :class:`nn.Linear` module in + :class:`RowwiseParallel` style. + + Args: + module (:class:`nn.Module`): + The :class:`nn.Linear` module to be parallelized. + device_mesh (:class:`DeviceMesh`): + Object which describes the mesh topology of devices. + + Returns: + None + """ + for name, param in module.named_parameters(): + dist_spec = ( + [Shard(1)] + if name == "weight" + else [Replicate()] # type: ignore[list-item] + ) + + dist_tensor = distribute_tensor(param, device_mesh, dist_spec) + if to_local: + dist_tensor = try_to_local(dist_tensor) + if name == "bias": + # rowwise linear would add bias more than ones. + dist_tensor /= device_mesh.size() + dist_param = torch.nn.Parameter(dist_tensor) + module.register_parameter(name, dist_param) + + # Weight, bias and scale are registered as buffer in QLinear + for name, buffer in module.named_buffers(): + dist_spec = ( + [Shard(1)] + if name == "weight" + else [Replicate()] # type: ignore[list-item] + ) + + dist_tensor = distribute_tensor( + buffer, device_mesh, dist_spec + ) + if to_local: + dist_tensor = try_to_local(dist_tensor) + if name == "bias": + # rowwise linear would add bias more than ones. + dist_tensor /= device_mesh.size() + module.register_buffer(name, dist_tensor) + + dist_tensor = distribute_tensor( + buffer, device_mesh, dist_spec + ) + if to_local: + dist_tensor = try_to_local(dist_tensor) + module.register_buffer(name, dist_tensor) + + +def rowwise_parallelize_loralinear( + module: LoRALinear, + device_mesh: DeviceMesh, + to_local: bool = False, +) -> None: + """rowwize parallelize lora linear. + + Read S-LoRA for more detail. + """ + rowwise_parallelize_linear( + module.base_layer, device_mesh=device_mesh, to_local=to_local + ) + for mod in module.lora_A.values(): + rowwise_parallelize_linear( + mod, device_mesh=device_mesh, to_local=to_local + ) + for mod in module.lora_B.values(): + colwise_parallelize_linear( + mod, device_mesh=device_mesh, to_local=to_local + ) + module._tp_mode = "rowwise" + + +def rowwise_parallelize_linear_fn( + module: nn.Module, device_mesh: DeviceMesh, to_local: bool = False +) -> None: + """ + This function parallelizes the input :Linear module in + :class:`RowwiseParallel` style. + + Args: + module (:class:`nn.Module`): + The :class:`nn.Linear` module to be parallelized. + device_mesh (:class:`DeviceMesh`): + Object which describes the mesh topology of devices. + + Returns: + None + """ + if isinstance(module, (torch.nn.Linear, QuantizedLN)): + return rowwise_parallelize_linear( + module, device_mesh=device_mesh, to_local=to_local + ) + elif isinstance(module, LoRALinear): + return rowwise_parallelize_loralinear( + module, device_mesh=device_mesh, to_local=to_local + ) + else: + raise TypeError(f"Unsupported module: {type(module)}") + + +def colwise_parallelize_linear( + module: nn.Module, device_mesh: DeviceMesh, to_local: bool = False +) -> None: + """ + This function parallelizes the input :class:`nn.Linear` module in + :class:`ColwiseParallel` style. + + Args: + module (:class:`nn.Module`): + The :class:`nn.Linear` module to be parallelized. + device_mesh (:class:`DeviceMesh`): + Object which describes the mesh topology of devices. + + Returns: + None + """ + + for name, param in module.named_parameters(): + dist_tensor = distribute_tensor( + param, device_mesh, [Shard(0)] + ) + if to_local: + dist_tensor = try_to_local(dist_tensor) + dist_param = torch.nn.Parameter(dist_tensor) + module.register_parameter(name, dist_param) + # Weight, bias and scale are registered as buffer in QLinear + for name, buffer in module.named_buffers(): + dist_tensor = distribute_tensor( + buffer, device_mesh, [Shard(0)] + ) + if to_local: + dist_tensor = try_to_local(dist_tensor) + module.register_buffer(name, dist_tensor) + + +def colwise_parallelize_loralinear( + module: nn.Module, device_mesh: DeviceMesh, to_local: bool = False +) -> None: + """colwise parallelize lora linear.""" + colwise_parallelize_linear( + module.base_layer, device_mesh=device_mesh, to_local=to_local + ) + for mod in module.lora_A.values(): + colwise_parallelize_linear( + mod, device_mesh=device_mesh, to_local=to_local + ) + for mod in module.lora_B.values(): + colwise_parallelize_linear( + mod, device_mesh=device_mesh, to_local=to_local + ) + module._tp_mode = "colwise" + + +def colwise_parallelize_linear_fn( + module: nn.Module, device_mesh: DeviceMesh, to_local: bool = False +) -> None: + """ + This function parallelizes the input :Linear module in + :class:`ColwiseParallel` style. + + Args: + module (:class:`nn.Module`): + The :class:`nn.Linear` module to be parallelized. + device_mesh (:class:`DeviceMesh`): + Object which describes the mesh topology of devices. + + Returns: + None + """ + if isinstance(module, (torch.nn.Linear, QuantizedLN)): + return colwise_parallelize_linear( + module, device_mesh=device_mesh, to_local=to_local + ) + elif isinstance(module, LoRALinear): + return colwise_parallelize_loralinear( + module, device_mesh=device_mesh, to_local=to_local + ) + else: + raise TypeError(f"Unsupported module: {type(module)}") + + +def _partition_module( + mod_name: str, + prefix: str, + module: nn.Module, + device_mesh: DeviceMesh, + func: Callable, +): + """partition module. + + Parameters in module won't be force Replicated. + + Args: + mod_name (str): module name. + prefix (str): Parameter prefix. + module (Module): Module to be partitioned. + device_mesh (DeviceMesh): The device mesh. + func (Callable): partition callback + """ + for name, mod in module.named_children(): + child_name = f"{prefix}{name}" + _partition_module( + child_name, + child_name + ".", + module=mod, + device_mesh=device_mesh, + func=func, + ) + + func(mod_name, module, device_mesh) + + +def partition_module( + module: nn.Module, + device_mesh: DeviceMesh, + func: Callable, + to_local: bool = False, +): + """partition module. + + Parameters in module won't be force Replicated. + + Args: + module (Module): Module to be partitioned. + device_mesh (DeviceMesh): The device mesh. + func (Callable): partition callback. + to_local (bool): Convert all DTensor parameters to Tensor parameters. + """ + _partition_module( + "", "", module=module, device_mesh=device_mesh, func=func + ) + + if to_local: + module_to_local(module) + + +def replicate_module(model: nn.Module, device_mesh: DeviceMesh): + """Replicate all parameters in module. + + Args: + model (Module): Module to perform replicate. + device_mesh (DeviceMesh): The distribution device mesh. + """ + for name, param in model.named_parameters(recurse=False): + param = distribute_tensor( + param, device_mesh=device_mesh, placements=[Replicate()] + ).to_local() + param = nn.Parameter(param) + model.register_parameter(name, param) + + for name, buf in model.named_buffers(recurse=False): + buf = distribute_tensor( + buf, device_mesh=device_mesh, placements=[Replicate()] + ).to_local() + model.register_buffer(name, buf) diff --git a/swarms/utils/inference_convert_utils.py b/swarms/utils/inference_convert_utils.py new file mode 100644 index 00000000..596d222b --- /dev/null +++ b/swarms/utils/inference_convert_utils.py @@ -0,0 +1,79 @@ +import torch + + +def continuous_tensor( + inputs: torch.Tensor, seq_length: torch.LongTensor +): + """Convert batched tensor to continuous tensor. + + Args: + inputs (Tensor): batched tensor. + seq_length (Tensor): length of each sequence. + + Return: + Tensor: continuoused tensor. + """ + assert inputs.dim() > 1 + if inputs.size(1) == 1: + return inputs.reshape(1, -1) + + inputs = [inp[:slen] for inp, slen in zip(inputs, seq_length)] + + inputs = torch.cat(inputs).unsqueeze(0) + return inputs + + +def batch_tensor(inputs: torch.Tensor, seq_length: torch.LongTensor): + """Convert continuoused tensor to batched tensor. + + Args: + inputs (Tensor): continuoused tensor. + seq_length (Tensor): length of each sequence. + + Return: + Tensor: batched tensor. + """ + from torch.nn.utils.rnn import pad_sequence + + end_loc = seq_length.cumsum(0) + start_loc = end_loc - seq_length + + inputs = [ + inputs[0, sloc:eloc] for sloc, eloc in zip(start_loc, end_loc) + ] + inputs = pad_sequence(inputs, batch_first=True) + return inputs + + +def page_cache( + paged_cache: torch.Tensor, + batched_cache: torch.Tensor, + cache_length: torch.Tensor, + block_offsets: torch.Tensor, + permute_head: bool = True, +): + """Convert batched cache to paged cache. + + Args: + paged_cache (Tensor): Output paged cache. + batched_cache (Tensor): Input batched cache. + cache_length (Tensor): length of the cache. + block_offsets (Tensor): Offset of each blocks. + """ + assert block_offsets.dim() == 2 + block_size = paged_cache.size(1) + batch_size = batched_cache.size(0) + if permute_head: + batched_cache = batched_cache.permute(0, 2, 1, 3) + + for b_idx in range(batch_size): + cache_len = cache_length[b_idx] + b_cache = batched_cache[b_idx] + block_off = block_offsets[b_idx] + block_off_idx = 0 + for s_start in range(0, cache_len, block_size): + s_end = min(s_start + block_size, cache_len) + s_len = s_end - s_start + b_off = block_off[block_off_idx] + paged_cache[b_off, :s_len] = b_cache[s_start:s_end] + block_off_idx += 1 From c4f496a5bdc4f3231aba46c1baa80e4c6906d42a Mon Sep 17 00:00:00 2001 From: Kye Date: Sat, 3 Feb 2024 09:05:37 -0800 Subject: [PATCH 097/103] [REFACTOR][QwenVLMultiModal] --- swarms/models/model_registry.py | 54 ++++++++++++++++++++++++++ swarms/models/qwen.py | 68 +++++++++++++++++++++++++-------- 2 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 swarms/models/model_registry.py diff --git a/swarms/models/model_registry.py b/swarms/models/model_registry.py new file mode 100644 index 00000000..a65ca154 --- /dev/null +++ b/swarms/models/model_registry.py @@ -0,0 +1,54 @@ +import pkgutil +import inspect + + +class ModelRegistry: + """ + A registry for storing and querying models. + + Attributes: + models (dict): A dictionary of model names and corresponding model classes. + + Methods: + __init__(): Initializes the ModelRegistry object and retrieves all available models. + _get_all_models(): Retrieves all available models from the models package. + query(text): Queries the models based on the given text and returns a dictionary of matching models. + """ + + def __init__(self): + self.models = self._get_all_models() + + def _get_all_models(self): + """ + Retrieves all available models from the models package. + + Returns: + dict: A dictionary of model names and corresponding model classes. + """ + models = {} + for importer, modname, ispkg in pkgutil.iter_modules( + models.__path__ + ): + module = importer.find_module(modname).load_module( + modname + ) + for name, obj in inspect.getmembers(module): + if inspect.isclass(obj): + models[name] = obj + return models + + def query(self, text): + """ + Queries the models based on the given text and returns a dictionary of matching models. + + Args: + text (str): The text to search for in the model names. + + Returns: + dict: A dictionary of matching model names and corresponding model classes. + """ + return { + name: model + for name, model in self.models.items() + if text in name + } diff --git a/swarms/models/qwen.py b/swarms/models/qwen.py index 1533b117..5ed131bf 100644 --- a/swarms/models/qwen.py +++ b/swarms/models/qwen.py @@ -13,10 +13,20 @@ class QwenVLMultiModal(BaseMultiModalModel): QwenVLMultiModal is a class that represents a multi-modal model for Qwen chatbot. It inherits from the BaseMultiModalModel class. - Examples: - >>> model = QwenVLMultiModal() - >>> model.run("Hello, how are you?", "https://example.com/image.jpg") + Args: + model_name (str): The name of the model to be used. + device (str): The device to run the model on. + args (tuple): Additional positional arguments. + kwargs (dict): Additional keyword arguments. + quantize (bool): A flag to indicate whether to quantize the model. + return_bounding_boxes (bool): A flag to indicate whether to return bounding boxes for the image. + + + Examples: + >>> qwen = QwenVLMultiModal() + >>> response = qwen.run("Hello", "https://example.com/image.jpg") + >>> print(response) """ model_name: str = "Qwen/Qwen-VL-Chat" @@ -24,6 +34,7 @@ class QwenVLMultiModal(BaseMultiModalModel): args: tuple = field(default_factory=tuple) kwargs: dict = field(default_factory=dict) quantize: bool = False + return_bounding_boxes: bool = False def __post_init__(self): """ @@ -60,19 +71,44 @@ class QwenVLMultiModal(BaseMultiModalModel): and the image associated with the response (if any). """ try: - query = self.tokenizer.from_list_format( - [ - {"image": img, "text": text}, - ] - ) - - inputs = self.tokenizer(query, return_tensors="pt") - inputs = inputs.to(self.model.device) - pred = self.model.generate(**inputs) - response = self.tokenizer.decode( - pred.cpu()[0], skip_special_tokens=False - ) - return response + if self.return_bounding_boxes: + query = self.tokenizer.from_list_format( + [ + {"image": img, "text": text}, + ] + ) + + inputs = self.tokenizer(query, return_tensors="pt") + inputs = inputs.to(self.model.device) + pred = self.model.generate(**inputs) + response = self.tokenizer.decode( + pred.cpu()[0], skip_special_tokens=False + ) + + image_bb = self.tokenizer.draw_bbox_on_latest_picture( + response + ) + + if image_bb: + image_bb.save("output.jpg") + else: + print("No bounding boxes found in the image.") + + return response, image_bb + else: + query = self.tokenizer.from_list_format( + [ + {"image": img, "text": text}, + ] + ) + + inputs = self.tokenizer(query, return_tensors="pt") + inputs = inputs.to(self.model.device) + pred = self.model.generate(**inputs) + response = self.tokenizer.decode( + pred.cpu()[0], skip_special_tokens=False + ) + return response except Exception as error: print(f"[ERROR]: [QwenVLMultiModal]: {error}") From 075f6320e1cc164d5cc595dcce5b306b512a2d92 Mon Sep 17 00:00:00 2001 From: Kye Date: Sun, 4 Feb 2024 01:35:48 -0800 Subject: [PATCH 098/103] [CODE QUALITY] --- pyproject.toml | 3 ++- swarms/models/__init__.py | 5 +++-- swarms/models/model_registry.py | 28 ++++++++++++++++++++++++++++ swarms/models/qwen.py | 2 +- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index f4971b47..2a89e00f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "4.0.2" +version = "4.0.3" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] @@ -21,6 +21,7 @@ classifiers = [ "Programming Language :: Python :: 3.10" ] + [tool.poetry.dependencies] python = "^3.6.1" torch = "2.1.1" diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 25b10024..00d9d1f2 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -44,7 +44,8 @@ from swarms.models.timm import TimmModel # noqa: E402 from swarms.models.ultralytics_model import ( UltralyticsModel, ) # noqa: E402 -from swarms.models.vip_llava import VipLlavaMultiModal # noqa: E402 + +# from swarms.models.vip_llava import VipLlavaMultiModal # noqa: E402 from swarms.models.llava import LavaMultiModal # noqa: E402 from swarms.models.qwen import QwenVLMultiModal # noqa: E402 from swarms.models.clipq import CLIPQ # noqa: E402 @@ -118,7 +119,7 @@ __all__ = [ "TogetherLLM", "TimmModel", "UltralyticsModel", - "VipLlavaMultiModal", + # "VipLlavaMultiModal", "LavaMultiModal", "QwenVLMultiModal", "CLIPQ", diff --git a/swarms/models/model_registry.py b/swarms/models/model_registry.py index a65ca154..6da04282 100644 --- a/swarms/models/model_registry.py +++ b/swarms/models/model_registry.py @@ -52,3 +52,31 @@ class ModelRegistry: for name, model in self.models.items() if text in name } + + def run_model( + self, model_name: str, task: str, img: str, *args, **kwargs + ): + """ + Runs the specified model for the given task and image. + + Args: + model_name (str): The name of the model to run. + task (str): The task to perform using the model. + img (str): The image to process. + *args: Additional positional arguments to pass to the model's run method. + **kwargs: Additional keyword arguments to pass to the model's run method. + + Returns: + The result of running the model. + + Raises: + ValueError: If the specified model is not found in the model registry. + """ + if model_name not in self.models: + raise ValueError(f"Model {model_name} not found") + + # Get the model + model = self.models[model_name] + + # Run the model + return model.run(task, img, *args, **kwargs) diff --git a/swarms/models/qwen.py b/swarms/models/qwen.py index 5ed131bf..b5a4ed1a 100644 --- a/swarms/models/qwen.py +++ b/swarms/models/qwen.py @@ -29,7 +29,7 @@ class QwenVLMultiModal(BaseMultiModalModel): >>> print(response) """ - model_name: str = "Qwen/Qwen-VL-Chat" + model_name: str = "Qwen/Qwen-VL" device: str = "cuda" args: tuple = field(default_factory=tuple) kwargs: dict = field(default_factory=dict) From 593b9b104efc21451625f2055ffeb0ef2e01493a Mon Sep 17 00:00:00 2001 From: Kye Date: Sun, 4 Feb 2024 08:34:37 -0800 Subject: [PATCH 099/103] [FEAT][Chroma] --- playground/structs/agent_with_longterm.py | 39 +++++ pyproject.toml | 2 +- swarms/memory/__init__.py | 2 + swarms/memory/chroma_db.py | 203 ++++++++++------------ swarms/telemetry/posthog_utils.py | 122 +++++++------ 5 files changed, 195 insertions(+), 173 deletions(-) create mode 100644 playground/structs/agent_with_longterm.py diff --git a/playground/structs/agent_with_longterm.py b/playground/structs/agent_with_longterm.py new file mode 100644 index 00000000..e803d095 --- /dev/null +++ b/playground/structs/agent_with_longterm.py @@ -0,0 +1,39 @@ +import os + +from dotenv import load_dotenv + +# Import the OpenAIChat model and the Agent struct +from swarms import Agent, OpenAIChat, ChromaDB + +# Load the environment variables +load_dotenv() + +# Get the API key from the environment +api_key = os.environ.get("OPENAI_API_KEY") + + +# Initilaize the chromadb client +chromadb = ChromaDB( + metric="cosine", + output="results", +) + +# Initialize the language model +llm = OpenAIChat( + temperature=0.5, + model_name="gpt-4", + openai_api_key=api_key, + max_tokens=1000, +) + +## Initialize the workflow +agent = Agent( + llm=llm, + max_loops=4, + autosave=True, + dashboard=True, + long_term_memory=ChromaDB(), +) + +# Run the workflow on a task +agent.run("Generate a 10,000 word blog on health and wellness.") diff --git a/pyproject.toml b/pyproject.toml index 2a89e00f..9e81bf20 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "4.0.3" +version = "4.0.4" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/memory/__init__.py b/swarms/memory/__init__.py index 4d58ea5a..2dca8172 100644 --- a/swarms/memory/__init__.py +++ b/swarms/memory/__init__.py @@ -5,6 +5,7 @@ from swarms.memory.sqlite import SQLiteDB from swarms.memory.weaviate_db import WeaviateDB from swarms.memory.visual_memory import VisualShortTermMemory from swarms.memory.action_subtask import ActionSubtaskEntry +from swarms.memory.chroma_db import ChromaDB __all__ = [ "AbstractVectorDatabase", @@ -14,4 +15,5 @@ __all__ = [ "WeaviateDB", "VisualShortTermMemory", "ActionSubtaskEntry", + "ChromaDB", ] diff --git a/swarms/memory/chroma_db.py b/swarms/memory/chroma_db.py index 8e200974..3d355b4f 100644 --- a/swarms/memory/chroma_db.py +++ b/swarms/memory/chroma_db.py @@ -1,30 +1,19 @@ +import numpy as np import logging -import os -from typing import Dict, List, Optional +import uuid +from typing import Optional, Callable, List import chromadb -import tiktoken as tiktoken -from chromadb.config import Settings -from chromadb.utils.embedding_functions import OpenAIEmbeddingFunction from dotenv import load_dotenv -from termcolor import colored +from chromadb.utils.data_loaders import ImageLoader +from chromadb.utils.embedding_functions import ( + OpenCLIPEmbeddingFunction, +) -from swarms.utils.token_count_tiktoken import limit_tokens_from_string +# Load environment variables load_dotenv() -# ChromaDB settings -client = chromadb.Client(Settings(anonymized_telemetry=False)) - - -# ChromaDB client -def get_chromadb_client(): - return client - - -# OpenAI API key -OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") - # Results storage using local ChromaDB class ChromaDB: @@ -33,10 +22,10 @@ class ChromaDB: ChromaDB database Args: - metric (str): _description_ - RESULTS_STORE_NAME (str): _description_ - LLM_MODEL (str): _description_ - openai_api_key (str): _description_ + metric (str): The similarity metric to use. + output (str): The name of the collection to store the results in. + limit_tokens (int, optional): The maximum number of tokens to use for the query. Defaults to 1000. + n_results (int, optional): The number of results to retrieve. Defaults to 2. Methods: add: _description_ @@ -45,135 +34,129 @@ class ChromaDB: Examples: >>> chromadb = ChromaDB( >>> metric="cosine", - >>> RESULTS_STORE_NAME="results", - >>> LLM_MODEL="gpt3", + >>> output="results", + >>> llm="gpt3", >>> openai_api_key=OPENAI_API_KEY, >>> ) >>> chromadb.add(task, result, result_id) - >>> chromadb.query(query, top_results_num) """ def __init__( self, metric: str, - RESULTS_STORE_NAME: str, - LLM_MODEL: str, - openai_api_key: str = OPENAI_API_KEY, - top_results_num: int = 3, + output_dir: str, limit_tokens: Optional[int] = 1000, + n_results: int = 2, + embedding_function: Callable = None, + data_loader: Callable = None, + multimodal: bool = False, *args, **kwargs, ): self.metric = metric - self.RESULTS_STORE_NAME = RESULTS_STORE_NAME - self.LLM_MODEL = LLM_MODEL - self.openai_api_key = openai_api_key - self.top_results_num = top_results_num + self.output_dir = output_dir self.limit_tokens = limit_tokens + self.n_results = n_results # Disable ChromaDB logging - logging.getLogger("chromadb").setLevel(logging.ERROR) + logging.getLogger("chromadb").setLevel(logging.INFO) + # Create Chroma collection chroma_persist_dir = "chroma" chroma_client = chromadb.PersistentClient( settings=chromadb.config.Settings( persist_directory=chroma_persist_dir, - ) + ), + *args, + **kwargs, ) - # Create embedding function - embedding_function = OpenAIEmbeddingFunction( - api_key=openai_api_key - ) + # Data loader + if data_loader: + self.data_loader = data_loader + else: + self.data_loader = ImageLoader() + + # Embedding model + if embedding_function: + self.embedding_function = embedding_function + else: + self.embedding_function = None + + # If multimodal set the embedding model to OpenCLIP + if multimodal: + self.embedding_function = OpenCLIPEmbeddingFunction() + + # Create ChromaDB client + self.client = chromadb.Client() # Create Chroma collection self.collection = chroma_client.get_or_create_collection( - name=RESULTS_STORE_NAME, + name=output_dir, metadata={"hnsw:space": metric}, - embedding_function=embedding_function, + embedding_function=self.embedding_function, + data_loader=self.data_loader, + *args, + **kwargs, ) def add( - self, task: Dict, result: str, result_id: str, *args, **kwargs + self, + document: str, + images: List[np.ndarray] = None, + img_urls: List[str] = None, + *args, + **kwargs, ): - """Adds a result to the ChromaDB collection + """ + Add a document to the ChromaDB collection. Args: - task (Dict): _description_ - result (str): _description_ - result_id (str): _description_ - """ + document (str): The document to be added. + condition (bool, optional): The condition to check before adding the document. Defaults to True. + Returns: + str: The ID of the added document. + """ try: - # Embed the result - embeddings = ( - self.collection.embedding_function.embed([result])[0] - .tolist() - .copy() - ) - - # If the result is a list, flatten it - if ( - len( - self.collection.get(ids=[result_id], include=[])[ - "ids" - ] - ) - > 0 - ): # Check if the result already exists - self.collection.update( - ids=result_id, - embeddings=embeddings, - documents=result, - metadatas={ - "task": task["task_name"], - "result": result, - }, - ) - - # If the result is not a list, add it - else: - self.collection.add( - ids=result_id, - embeddings=embeddings, - documents=result, - metadatas={ - "task": task["task_name"], - "result": result, - }, - *args, - **kwargs, - ) - except Exception as error: - print( - colored(f"Error adding to ChromaDB: {error}", "red") + doc_id = str(uuid.uuid4()) + self.collection.add( + ids=[doc_id], + documents=[document], + images=images, + uris=img_urls, + *args, + **kwargs, ) + return doc_id + except Exception as e: + raise Exception(f"Failed to add document: {str(e)}") - def query(self, query: str, *args, **kwargs) -> List[dict]: - """Queries the ChromaDB collection with a query for the top results + def query( + self, + query_text: str, + query_images: List[np.ndarray], + *args, + **kwargs, + ): + """ + Query documents from the ChromaDB collection. Args: - query (str): _description_ - top_results_num (int): _description_ + query (str): The query string. + n_docs (int, optional): The number of documents to retrieve. Defaults to 1. Returns: - List[dict]: _description_ + dict: The retrieved documents. """ try: - count: int = self.collection.count() - if count == 0: - return [] - results = self.collection.query( - query_texts=query, - n_results=min(self.top_results_num, count), - include=["metadatas"], + docs = self.collection.query( + query_texts=[query_text], + query_images=query_images, + n_results=self.n_docs, *args, **kwargs, - ) - out = [item["task"] for item in results["metadatas"][0]] - out = limit_tokens_from_string( - out, "gpt-4", self.limit_tokens - ) - return out - except Exception as error: - print(colored(f"Error querying ChromaDB: {error}", "red")) + )["documents"] + return docs[0] + except Exception as e: + raise Exception(f"Failed to query documents: {str(e)}") diff --git a/swarms/telemetry/posthog_utils.py b/swarms/telemetry/posthog_utils.py index a6a520b5..66bd0e6c 100644 --- a/swarms/telemetry/posthog_utils.py +++ b/swarms/telemetry/posthog_utils.py @@ -1,69 +1,67 @@ -import functools -import os +import logging from dotenv import load_dotenv from posthog import Posthog -from swarms.telemetry.user_utils import generate_unique_identifier + # Load environment variables load_dotenv() - -# # Initialize Posthog client -api_key = os.getenv("POSTHOG_API_KEY") or None -host = os.getenv("POSTHOG_HOST") or None -posthog = Posthog(api_key, host=host) -posthog.debug = True - -# return posthog - - -def log_activity_posthog(event_name: str, **event_properties): - """Log activity to Posthog. - - - Args: - event_name (str): Name of the event to log. - **event_properties: Properties of the event to log. - - Examples: - >>> from swarms.telemetry.posthog_utils import log_activity_posthog - >>> @log_activity_posthog("test_event", test_property="test_value") - ... def test_function(): - ... print("Hello, world!") - >>> test_function() - Hello, world! - >>> # Check Posthog dashboard for event "test_event" with property - >>> # "test_property" set to "test_value". - """ - - def decorator_log_activity(func): - @functools.wraps(func) - def wrapper_log_activity(*args, **kwargs): - result = func(*args, **kwargs) - - # Assuming you have a way to get the user id - distinct_user_id = generate_unique_identifier() - - # Capture the event - posthog.capture( - distinct_user_id, event_name, event_properties - ) - - return result - - return wrapper_log_activity - - return decorator_log_activity - - -# @log_activity_posthog( -# "function_executed", function_name="my_function" -# ) -# def my_function(): -# # Function logic here -# return "Function executed successfully!" - - -# out = my_function() -# print(out) +logger = logging.getLogger(__name__) + + +class PosthogWrapper: + def __init__( + self, api_key, instance_address, debug=False, disabled=False + ): + self.posthog = Posthog(api_key, host=instance_address) + self.posthog.debug = debug + self.posthog.disabled = disabled + + def capture_event(self, distinct_id, event_name, properties=None): + self.posthog.capture(distinct_id, event_name, properties) + + def capture_pageview(self, distinct_id, url): + self.posthog.capture( + distinct_id, "$pageview", {"$current_url": url} + ) + + def set_user_properties( + self, distinct_id, event_name, properties + ): + self.posthog.capture( + distinct_id, event=event_name, properties=properties + ) + + def is_feature_enabled( + self, flag_key, distinct_id, send_feature_flag_events=True + ): + return self.posthog.feature_enabled( + flag_key, distinct_id, send_feature_flag_events + ) + + def get_feature_flag_payload(self, flag_key, distinct_id): + return self.posthog.get_feature_flag_payload( + flag_key, distinct_id + ) + + def get_feature_flag(self, flag_key, distinct_id): + return self.posthog.get_feature_flag(flag_key, distinct_id) + + def capture_with_feature_flag( + self, distinct_id, event_name, flag_key, variant_key + ): + self.posthog.capture( + distinct_id, + event_name, + {"$feature/{}".format(flag_key): variant_key}, + ) + + def capture_with_feature_flags( + self, distinct_id, event_name, send_feature_flags=True + ): + self.posthog.capture( + distinct_id, + event_name, + send_feature_flags=send_feature_flags, + ) From 1562d25c433dc36c88a784b00d72e3fa4c00c815 Mon Sep 17 00:00:00 2001 From: Kye Date: Sun, 4 Feb 2024 11:11:46 -0800 Subject: [PATCH 100/103] [CLEANUP][react app] --- pyproject.toml | 2 +- swarms/telemetry/posthog_utils.py | 82 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 9e81bf20..6e3cdc7f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "4.0.4" +version = "4.0.5" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/telemetry/posthog_utils.py b/swarms/telemetry/posthog_utils.py index 66bd0e6c..b3b2a73e 100644 --- a/swarms/telemetry/posthog_utils.py +++ b/swarms/telemetry/posthog_utils.py @@ -11,6 +11,16 @@ logger = logging.getLogger(__name__) class PosthogWrapper: + """ + A wrapper class for interacting with the PostHog analytics service. + + Args: + api_key (str): The API key for accessing the PostHog instance. + instance_address (str): The address of the PostHog instance. + debug (bool, optional): Whether to enable debug mode. Defaults to False. + disabled (bool, optional): Whether to disable tracking. Defaults to False. + """ + def __init__( self, api_key, instance_address, debug=False, disabled=False ): @@ -19,9 +29,24 @@ class PosthogWrapper: self.posthog.disabled = disabled def capture_event(self, distinct_id, event_name, properties=None): + """ + Capture an event in PostHog. + + Args: + distinct_id (str): The distinct ID of the user. + event_name (str): The name of the event. + properties (dict, optional): Additional properties associated with the event. Defaults to None. + """ self.posthog.capture(distinct_id, event_name, properties) def capture_pageview(self, distinct_id, url): + """ + Capture a pageview event in PostHog. + + Args: + distinct_id (str): The distinct ID of the user. + url (str): The URL of the page. + """ self.posthog.capture( distinct_id, "$pageview", {"$current_url": url} ) @@ -29,6 +54,14 @@ class PosthogWrapper: def set_user_properties( self, distinct_id, event_name, properties ): + """ + Set user properties in PostHog. + + Args: + distinct_id (str): The distinct ID of the user. + event_name (str): The name of the event. + properties (dict): The user properties to set. + """ self.posthog.capture( distinct_id, event=event_name, properties=properties ) @@ -36,21 +69,62 @@ class PosthogWrapper: def is_feature_enabled( self, flag_key, distinct_id, send_feature_flag_events=True ): + """ + Check if a feature flag is enabled for a user. + + Args: + flag_key (str): The key of the feature flag. + distinct_id (str): The distinct ID of the user. + send_feature_flag_events (bool, optional): Whether to send feature flag events. Defaults to True. + + Returns: + bool: True if the feature flag is enabled, False otherwise. + """ return self.posthog.feature_enabled( flag_key, distinct_id, send_feature_flag_events ) def get_feature_flag_payload(self, flag_key, distinct_id): + """ + Get the payload of a feature flag for a user. + + Args: + flag_key (str): The key of the feature flag. + distinct_id (str): The distinct ID of the user. + + Returns: + dict: The payload of the feature flag. + """ return self.posthog.get_feature_flag_payload( flag_key, distinct_id ) def get_feature_flag(self, flag_key, distinct_id): + """ + Get the value of a feature flag for a user. + + Args: + flag_key (str): The key of the feature flag. + distinct_id (str): The distinct ID of the user. + + Returns: + str: The value of the feature flag. + """ return self.posthog.get_feature_flag(flag_key, distinct_id) def capture_with_feature_flag( self, distinct_id, event_name, flag_key, variant_key ): + """ + Capture an event with a feature flag in PostHog. + + Args: + distinct_id (str): The distinct ID of the user. + event_name (str): The name of the event. + flag_key (str): The key of the feature flag. + variant_key (str): The key of the variant. + + """ self.posthog.capture( distinct_id, event_name, @@ -60,6 +134,14 @@ class PosthogWrapper: def capture_with_feature_flags( self, distinct_id, event_name, send_feature_flags=True ): + """ + Capture an event with all feature flags in PostHog. + + Args: + distinct_id (str): The distinct ID of the user. + event_name (str): The name of the event. + send_feature_flags (bool, optional): Whether to send feature flags. Defaults to True. + """ self.posthog.capture( distinct_id, event_name, From 37ea8cc58dcc612f9e35048a7ad454835b681a68 Mon Sep 17 00:00:00 2001 From: Kye Date: Mon, 5 Feb 2024 09:59:28 -0800 Subject: [PATCH 101/103] [BUFG][Mistral --- playground/models/miqu.py | 13 +++++++ pyproject.toml | 2 +- swarms/__init__.py | 5 --- swarms/memory/chroma_db.py | 4 +-- swarms/models/mistral.py | 73 +++++++++----------------------------- 5 files changed, 33 insertions(+), 64 deletions(-) create mode 100644 playground/models/miqu.py diff --git a/playground/models/miqu.py b/playground/models/miqu.py new file mode 100644 index 00000000..f6518a5f --- /dev/null +++ b/playground/models/miqu.py @@ -0,0 +1,13 @@ +from swarms import Mistral + + +# Initialize the model +model = Mistral( + model_name="mistralai/Mistral-7B-v0.1", + max_length=500, + use_flash_attention=True, + load_in_4bit=True +) + +# Run the model +result = model.run("What is the meaning of life?") \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 6e3cdc7f..5128ab33 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "4.0.5" +version = "4.0.9" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/__init__.py b/swarms/__init__.py index 2a682f5b..3a28d980 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -1,13 +1,8 @@ # from swarms.telemetry.main import Telemetry # noqa: E402, F403 from swarms.telemetry.bootup import bootup # noqa: E402, F403 -from swarms.telemetry.user_utils import ( - get_user_device_data, -) # noqa: E402, F403 bootup() -get_user_device_data() - from swarms.agents import * # noqa: E402, F403 from swarms.structs import * # noqa: E402, F403 from swarms.models import * # noqa: E402, F403 diff --git a/swarms/memory/chroma_db.py b/swarms/memory/chroma_db.py index 3d355b4f..155acf43 100644 --- a/swarms/memory/chroma_db.py +++ b/swarms/memory/chroma_db.py @@ -5,7 +5,7 @@ from typing import Optional, Callable, List import chromadb from dotenv import load_dotenv -from chromadb.utils.data_loaders import ImageLoader +# from chromadb.utils.data import ImageLoader from chromadb.utils.embedding_functions import ( OpenCLIPEmbeddingFunction, ) @@ -75,7 +75,7 @@ class ChromaDB: if data_loader: self.data_loader = data_loader else: - self.data_loader = ImageLoader() + self.data_loader = None # Embedding model if embedding_function: diff --git a/swarms/models/mistral.py b/swarms/models/mistral.py index aeeb37a8..d0146ef5 100644 --- a/swarms/models/mistral.py +++ b/swarms/models/mistral.py @@ -2,9 +2,9 @@ import torch from transformers import AutoModelForCausalLM, AutoTokenizer from swarms.structs.message import Message +from swarms.models.base_llm import AbstractLLM - -class Mistral: +class Mistral(AbstractLLM): """ Mistral is an all-new llm @@ -38,7 +38,10 @@ class Mistral: temperature: float = 1.0, max_length: int = 100, do_sample: bool = True, + *args, + **kwargs ): + super().__init__() self.ai_name = ai_name self.system_prompt = system_prompt self.model_name = model_name @@ -46,6 +49,7 @@ class Mistral: self.use_flash_attention = use_flash_attention self.temperature = temperature self.max_length = max_length + self.do_sample = do_sample # Check if the specified device is available if not torch.cuda.is_available() and device == "cuda": @@ -54,49 +58,18 @@ class Mistral: " device." ) - # Load the model and tokenizer - self.model = None - self.tokenizer = None - self.load_model() - self.history = [] - def load_model(self): - try: - self.model = AutoModelForCausalLM.from_pretrained( - self.model_name - ) - self.tokenizer = AutoTokenizer.from_pretrained( - self.model_name - ) - self.model.to(self.device) - except Exception as e: - raise ValueError( - f"Error loading the Mistral model: {str(e)}" - ) + self.model = AutoModelForCausalLM.from_pretrained( + self.model_name, *args, **kwargs + ) + self.tokenizer = AutoTokenizer.from_pretrained( + self.model_name, *args, **kwargs + ) + + self.model.to(self.device) - def run(self, task: str): - """Run the model on a given task.""" - - try: - model_inputs = self.tokenizer( - [task], return_tensors="pt" - ).to(self.device) - generated_ids = self.model.generate( - **model_inputs, - max_length=self.max_length, - do_sample=self.do_sample, - temperature=self.temperature, - max_new_tokens=self.max_length, - ) - output_text = self.tokenizer.batch_decode(generated_ids)[ - 0 - ] - return output_text - except Exception as e: - raise ValueError(f"Error running the model: {str(e)}") - - def __call__(self, task: str): + def run(self, task: str, *args, **kwargs): """Run the model on a given task.""" try: @@ -109,6 +82,7 @@ class Mistral: do_sample=self.do_sample, temperature=self.temperature, max_new_tokens=self.max_length, + **kwargs ) output_text = self.tokenizer.batch_decode(generated_ids)[ 0 @@ -158,17 +132,4 @@ class Mistral: # add error to history self.history.append(Message("Agent", error_message)) - return error_message - - def _stream_response(self, response: str = None): - """ - Yield the response token by token (word by word) - - Usage: - -------------- - for token in _stream_response(response): - print(token) - - """ - for token in response.split(): - yield token + return error_message \ No newline at end of file From c62577014cc4ac63f6829db594c39fcc39b1237c Mon Sep 17 00:00:00 2001 From: Kye Date: Tue, 6 Feb 2024 12:29:57 -0800 Subject: [PATCH 102/103] [LOGGING][++Misc] --- .gitignore | 1 + example.py | 5 +- playground/agents/swarm_protocol.py | 54 ++++++++++ playground/models/miqu.py | 6 +- pyproject.toml | 7 +- requirements.txt | 4 +- swarms/__init__.py | 1 + swarms/memory/chroma_db.py | 1 + swarms/models/mistral.py | 11 +- swarms/structs/__init__.py | 2 + swarms/structs/agent.py | 85 ++++++++++++---- swarms/structs/tool_json_schema.py | 37 +++++++ swarms/telemetry/__init__.py | 21 ++++ swarms/telemetry/posthog_utils.py | 151 +--------------------------- swarms/utils/disable_logging.py | 16 +-- swarms/utils/dist_utils.py | 8 +- swarms/utils/logger.py | 44 +++++++- 17 files changed, 252 insertions(+), 202 deletions(-) create mode 100644 playground/agents/swarm_protocol.py create mode 100644 swarms/structs/tool_json_schema.py diff --git a/.gitignore b/.gitignore index ac6be257..aa7e0f98 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ __pycache__/ *.py[cod] *$py.class .grit +swarm-worker-01_state.json error.txt # C extensions diff --git a/example.py b/example.py index 33c3bef1..d3032ac4 100644 --- a/example.py +++ b/example.py @@ -24,7 +24,10 @@ agent = Agent( llm=llm, max_loops=4, autosave=True, - dashboard=True, + dashboard=False, + # docs_folder="docs", + streaming_on=True, + verbose=True, ) # Run the workflow on a task diff --git a/playground/agents/swarm_protocol.py b/playground/agents/swarm_protocol.py new file mode 100644 index 00000000..f6c8db83 --- /dev/null +++ b/playground/agents/swarm_protocol.py @@ -0,0 +1,54 @@ +from dataclasses import dataclass +from typing import List + +from swarms import JSON, AbstractLLM, AbstractVectorDatabase, Agent + + +@dataclass +class YourAgent(Agent): + """ + Represents an agent in the swarm protocol. + + Attributes: + llm (AbstractLLM): The low-level module for the agent. + long_term_memory (AbstractVectorDatabase): The long-term memory for the agent. + tool_schema (List[JSON]): The schema for the tools used by the agent. + """ + + llm: AbstractLLM + long_term_memory: AbstractVectorDatabase + tool_schema: JSON + tool_schemas: List[JSON] + + def step(self, task: str, *args, **kwargs): + """ + Performs a single step in the agent's task. + + Args: + task (str): The task to be performed. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + """ + ... + + def run(self, task: str, *args, **kwargs): + """ + Runs the agent's task. + + Args: + task (str): The task to be performed. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + """ + ... + + def plan(self, task: str, *args, **kwargs): + """ + Plans the agent's task. + + Args: + task (str): The task to be performed. + *args: Additional positional arguments. + **kwargs: Additional keyword arguments. + """ + ... diff --git a/playground/models/miqu.py b/playground/models/miqu.py index f6518a5f..f3fc1b4d 100644 --- a/playground/models/miqu.py +++ b/playground/models/miqu.py @@ -3,11 +3,11 @@ from swarms import Mistral # Initialize the model model = Mistral( - model_name="mistralai/Mistral-7B-v0.1", + model_name="miqudev/miqu-1-70b", max_length=500, use_flash_attention=True, - load_in_4bit=True + load_in_4bit=True, ) # Run the model -result = model.run("What is the meaning of life?") \ No newline at end of file +result = model.run("What is the meaning of life?") diff --git a/pyproject.toml b/pyproject.toml index 5128ab33..bb12e7a4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,12 +4,12 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "4.0.9" +version = "4.1.0" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] homepage = "https://github.com/kyegomez/swarms" -documentation = "https://swarms.apac.ai" # Add this if you have documentation. +documentation = "https://swarms.apac.ai" readme = "README.md" # Assuming you have a README.md repository = "https://github.com/kyegomez/swarms" keywords = ["artificial intelligence", "deep learning", "optimizers", "Prompt Engineering"] @@ -47,7 +47,6 @@ anthropic = "*" sentencepiece = "0.1.98" httpx = "0.24.1" tiktoken = "0.4.0" -safetensors = "0.3.3" attrs = "22.2.0" ratelimit = "2.2.1" cohere = "4.24" @@ -55,7 +54,7 @@ huggingface-hub = "*" pydantic = "1.10.12" tenacity = "8.2.2" Pillow = "9.4.0" -chromadb = "0.4.14" +chromadb = "*" termcolor = "2.2.0" black = "23.3.0" soundfile = "0.12.1" diff --git a/requirements.txt b/requirements.txt index 00c92023..e6496205 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,7 +16,7 @@ sentencepiece==0.1.98 requests_mock PyPDF2==3.0.1 accelerate==0.22.0 -chromadb==0.4.14 +chromadb tensorflow optimum toml @@ -29,11 +29,9 @@ termcolor==2.2.0 diffusers einops==0.7.0 opencv-python-headless==4.8.1.78 -safetensors==0.3.3 numpy openai==0.28.0 opencv-python==4.7.0.72 -safetensors==0.3.3 timm yapf autopep8 diff --git a/swarms/__init__.py b/swarms/__init__.py index 3a28d980..d88d9bf8 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -3,6 +3,7 @@ from swarms.telemetry.bootup import bootup # noqa: E402, F403 bootup() + from swarms.agents import * # noqa: E402, F403 from swarms.structs import * # noqa: E402, F403 from swarms.models import * # noqa: E402, F403 diff --git a/swarms/memory/chroma_db.py b/swarms/memory/chroma_db.py index 155acf43..e951ecd9 100644 --- a/swarms/memory/chroma_db.py +++ b/swarms/memory/chroma_db.py @@ -5,6 +5,7 @@ from typing import Optional, Callable, List import chromadb from dotenv import load_dotenv + # from chromadb.utils.data import ImageLoader from chromadb.utils.embedding_functions import ( OpenCLIPEmbeddingFunction, diff --git a/swarms/models/mistral.py b/swarms/models/mistral.py index d0146ef5..6cfb6f77 100644 --- a/swarms/models/mistral.py +++ b/swarms/models/mistral.py @@ -4,6 +4,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer from swarms.structs.message import Message from swarms.models.base_llm import AbstractLLM + class Mistral(AbstractLLM): """ Mistral is an all-new llm @@ -39,9 +40,9 @@ class Mistral(AbstractLLM): max_length: int = 100, do_sample: bool = True, *args, - **kwargs + **kwargs, ): - super().__init__() + super().__init__(*args, **kwargs) self.ai_name = ai_name self.system_prompt = system_prompt self.model_name = model_name @@ -66,7 +67,7 @@ class Mistral(AbstractLLM): self.tokenizer = AutoTokenizer.from_pretrained( self.model_name, *args, **kwargs ) - + self.model.to(self.device) def run(self, task: str, *args, **kwargs): @@ -82,7 +83,7 @@ class Mistral(AbstractLLM): do_sample=self.do_sample, temperature=self.temperature, max_new_tokens=self.max_length, - **kwargs + **kwargs, ) output_text = self.tokenizer.batch_decode(generated_ids)[ 0 @@ -132,4 +133,4 @@ class Mistral(AbstractLLM): # add error to history self.history.append(Message("Agent", error_message)) - return error_message \ No newline at end of file + return error_message diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py index 15b9ef1e..6ea7014a 100644 --- a/swarms/structs/__init__.py +++ b/swarms/structs/__init__.py @@ -57,6 +57,7 @@ from swarms.structs.utils import ( find_token_in_text, parse_tasks, ) +from swarms.structs.tool_json_schema import JSON __all__ = [ "Agent", @@ -112,4 +113,5 @@ __all__ = [ "StepOutput", "StepRequestBody", "TaskRequestBody", + "JSON", ] diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index ba4be539..a0f85814 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -16,6 +16,7 @@ from swarms.prompts.agent_system_prompts import ( from swarms.prompts.multi_modal_autonomous_instruction_prompt import ( MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1, ) +from swarms.structs.conversation import Conversation from swarms.tokenizers.base_tokenizer import BaseTokenizer from swarms.tools.tool import BaseTool from swarms.utils.code_interpreter import SubprocessCodeInterpreter @@ -176,6 +177,7 @@ class Agent: streaming_on: Optional[bool] = False, docs: List[str] = None, docs_folder: str = None, + verbose: bool = False, *args, **kwargs: Any, ): @@ -219,6 +221,7 @@ class Agent: self.streaming_on = streaming_on self.docs = docs self.docs_folder = docs_folder + self.verbose = verbose # The max_loops will be set dynamically if the dynamic_loop if self.dynamic_loops: @@ -234,7 +237,6 @@ class Agent: # Memory self.feedback = [] - self.short_memory = [] # Initialize the code executor self.code_executor = SubprocessCodeInterpreter() @@ -243,7 +245,9 @@ class Agent: if preset_stopping_token: self.stopping_token = "" - # self.short_memory = Conversation(time_enabled=True) + self.short_memory = Conversation( + system_prompt=self.system_prompt, time_enabled=True + ) # If the docs exist then ingest the docs if self.docs: @@ -257,6 +261,9 @@ class Agent: if self.tokenizer and self.context_length: self.truncate_history() + if verbose: + logger.setLevel(logging.DEBUG) + def set_system_prompt(self, system_prompt: str): """Set the system prompt""" self.system_prompt = system_prompt @@ -308,7 +315,7 @@ class Agent: def add_task_to_memory(self, task: str): """Add the task to the memory""" try: - self.short_memory.append([f"{self.user_name}: {task}"]) + self.short_memory.add(f"{self.user_name}: {task}") except Exception as error: print( colored( @@ -319,7 +326,9 @@ class Agent: def add_message_to_memory(self, message: str): """Add the message to the memory""" try: - self.short_memory[-1].append(message) + self.short_memory.add( + role=self.agent_name, content=message + ) except Exception as error: print( colored( @@ -560,7 +569,9 @@ class Agent: time.sleep(self.loop_interval) # Add the history to the memory - self.short_memory.append(history) + self.short_memory.add( + role=self.agent_name, content=history + ) # If autosave is enabled then save the state if self.autosave: @@ -656,17 +667,29 @@ class Agent: Returns: str: The agent history prompt """ - ltr = self.long_term_memory.query(query) + ltr = str(self.long_term_memory.query(query)) context = f""" {query} ####### Long Term Memory ################ {ltr} """ - return self.short_memory.append([f"{context}"]) + return self.short_memory.add( + role=self.agent_name, content=context + ) def add_memory(self, message: str): - return self.short_memory.append([f"{message}"]) + """Add a memory to the agent + + Args: + message (str): _description_ + + Returns: + _type_: _description_ + """ + return self.short_memory.add( + role=self.agent_name, content=message + ) async def run_concurrent(self, tasks: List[str], **kwargs): """ @@ -792,10 +815,16 @@ class Agent: # Update the agent's history with the new interaction if self.interactive: - self.short_memory.append(f"AI: {response}") - self.short_memory.append(f"Human: {task}") + self.short_memory.add( + role=self.agent_name, content=response + ) + self.short_memory.add( + role=self.user_name, content=task + ) else: - self.short_memory.append(f"AI: {response}") + self.short_memory.add( + role=self.agent_name, content=response + ) return response except Exception as error: @@ -842,11 +871,11 @@ class Agent: if len(self.short_memory) < 2: return None, None - # Remove the last response - self.short_memory.pop() + # Remove the last response but keep the last state, short_memory is a dict + self.short_memory.delete(-1) # Get the previous state - previous_state = self.short_memory[-1][-1] + previous_state = self.short_memory[-1] return previous_state, f"Restored to {previous_state}" # Response Filtering @@ -931,7 +960,9 @@ class Agent: "agent_description": self.agent_description, "system_prompt": self.system_prompt, "sop": self.sop, - "short_memory": self.short_memory, + "short_memory": ( + self.short_memory.return_history_as_string() + ), "loop_interval": self.loop_interval, "retry_attempts": self.retry_attempts, "retry_interval": self.retry_interval, @@ -966,7 +997,9 @@ class Agent: "agent_description": self.agent_description, "system_prompt": self.system_prompt, "sop": self.sop, - "short_memory": self.short_memory, + "short_memory": ( + self.short_memory.return_history_as_string() + ), "loop_interval": self.loop_interval, "retry_attempts": self.retry_attempts, "retry_interval": self.retry_interval, @@ -1071,7 +1104,7 @@ class Agent: def reset(self): """Reset the agent""" - self.short_memory = [] + self.short_memory = {} def run_code(self, code: str): """ @@ -1119,7 +1152,9 @@ class Agent: for doc in docs: data = data_to_text(doc) - return self.short_memory.append(data) + return self.short_memory.add( + role=self.user_name, content=data + ) def ingest_pdf(self, pdf: str): """Ingest the pdf into the memory @@ -1131,12 +1166,14 @@ class Agent: _type_: _description_ """ text = pdf_to_text(pdf) - return self.short_memory.append(text) + return self.short_memory.add( + role=self.user_name, content=text + ) def receieve_mesage(self, name: str, message: str): """Receieve a message""" message = f"{name}: {message}" - return self.short_memory.append(message) + return self.short_memory.add(role=name, content=message) def send_agent_message( self, agent_name: str, message: str, *args, **kwargs @@ -1160,7 +1197,9 @@ class Agent: None """ # Count the short term history with the tokenizer - count = self.tokenizer.count_tokens(self.short_memory) + count = self.tokenizer.count_tokens( + self.short_memory.return_history_as_string() + ) # Now the logic that truncates the memory if it's more than the count if len(self.short_memory) > count: @@ -1175,4 +1214,6 @@ class Agent: for file in files: text = data_to_text(file) - return self.short_memory.append(text) + return self.short_memory.add( + role=self.user_name, content=text + ) diff --git a/swarms/structs/tool_json_schema.py b/swarms/structs/tool_json_schema.py new file mode 100644 index 00000000..d71df718 --- /dev/null +++ b/swarms/structs/tool_json_schema.py @@ -0,0 +1,37 @@ +import json +from abc import ABC, abstractmethod + + +class JSON(ABC): + def __init__(self, schema_path): + """ + Initializes a JSONSchema object. + + Args: + schema_path (str): The path to the JSON schema file. + """ + self.schema_path = schema_path + self.schema = self.load_schema() + + def load_schema(self): + """ + Loads the JSON schema from the specified file. + + Returns: + dict: The loaded JSON schema. + """ + with open(self.schema_path, "r") as f: + return json.load(f) + + @abstractmethod + def validate(self, data): + """ + Validates the given data against the JSON schema. + + Args: + data (dict): The data to be validated. + + Raises: + NotImplementedError: This method needs to be implemented by the subclass. + """ + pass diff --git a/swarms/telemetry/__init__.py b/swarms/telemetry/__init__.py index d829b724..3a7e61d3 100644 --- a/swarms/telemetry/__init__.py +++ b/swarms/telemetry/__init__.py @@ -1,3 +1,5 @@ +# from swarms.telemetry.posthog_utils import posthog + from swarms.telemetry.log_all import log_all_calls, log_calls from swarms.telemetry.sys_info import ( get_cpu_info, @@ -17,6 +19,25 @@ from swarms.telemetry.user_utils import ( get_user_device_data, ) +# # Capture data from the user's device +# posthog.capture( +# "User Device Data", +# str(get_user_device_data()), +# ) + +# # Capture system information +# posthog.capture( +# "System Information", +# str(system_info()), +# ) + +# # Capture the user's unique identifier +# posthog.capture( +# "User Unique Identifier", +# str(generate_unique_identifier()), +# ) + + __all__ = [ "log_all_calls", "log_calls", diff --git a/swarms/telemetry/posthog_utils.py b/swarms/telemetry/posthog_utils.py index b3b2a73e..7ae8d0a7 100644 --- a/swarms/telemetry/posthog_utils.py +++ b/swarms/telemetry/posthog_utils.py @@ -1,149 +1,6 @@ -import logging - -from dotenv import load_dotenv from posthog import Posthog - -# Load environment variables -load_dotenv() - -logger = logging.getLogger(__name__) - - -class PosthogWrapper: - """ - A wrapper class for interacting with the PostHog analytics service. - - Args: - api_key (str): The API key for accessing the PostHog instance. - instance_address (str): The address of the PostHog instance. - debug (bool, optional): Whether to enable debug mode. Defaults to False. - disabled (bool, optional): Whether to disable tracking. Defaults to False. - """ - - def __init__( - self, api_key, instance_address, debug=False, disabled=False - ): - self.posthog = Posthog(api_key, host=instance_address) - self.posthog.debug = debug - self.posthog.disabled = disabled - - def capture_event(self, distinct_id, event_name, properties=None): - """ - Capture an event in PostHog. - - Args: - distinct_id (str): The distinct ID of the user. - event_name (str): The name of the event. - properties (dict, optional): Additional properties associated with the event. Defaults to None. - """ - self.posthog.capture(distinct_id, event_name, properties) - - def capture_pageview(self, distinct_id, url): - """ - Capture a pageview event in PostHog. - - Args: - distinct_id (str): The distinct ID of the user. - url (str): The URL of the page. - """ - self.posthog.capture( - distinct_id, "$pageview", {"$current_url": url} - ) - - def set_user_properties( - self, distinct_id, event_name, properties - ): - """ - Set user properties in PostHog. - - Args: - distinct_id (str): The distinct ID of the user. - event_name (str): The name of the event. - properties (dict): The user properties to set. - """ - self.posthog.capture( - distinct_id, event=event_name, properties=properties - ) - - def is_feature_enabled( - self, flag_key, distinct_id, send_feature_flag_events=True - ): - """ - Check if a feature flag is enabled for a user. - - Args: - flag_key (str): The key of the feature flag. - distinct_id (str): The distinct ID of the user. - send_feature_flag_events (bool, optional): Whether to send feature flag events. Defaults to True. - - Returns: - bool: True if the feature flag is enabled, False otherwise. - """ - return self.posthog.feature_enabled( - flag_key, distinct_id, send_feature_flag_events - ) - - def get_feature_flag_payload(self, flag_key, distinct_id): - """ - Get the payload of a feature flag for a user. - - Args: - flag_key (str): The key of the feature flag. - distinct_id (str): The distinct ID of the user. - - Returns: - dict: The payload of the feature flag. - """ - return self.posthog.get_feature_flag_payload( - flag_key, distinct_id - ) - - def get_feature_flag(self, flag_key, distinct_id): - """ - Get the value of a feature flag for a user. - - Args: - flag_key (str): The key of the feature flag. - distinct_id (str): The distinct ID of the user. - - Returns: - str: The value of the feature flag. - """ - return self.posthog.get_feature_flag(flag_key, distinct_id) - - def capture_with_feature_flag( - self, distinct_id, event_name, flag_key, variant_key - ): - """ - Capture an event with a feature flag in PostHog. - - Args: - distinct_id (str): The distinct ID of the user. - event_name (str): The name of the event. - flag_key (str): The key of the feature flag. - variant_key (str): The key of the variant. - - """ - self.posthog.capture( - distinct_id, - event_name, - {"$feature/{}".format(flag_key): variant_key}, - ) - - def capture_with_feature_flags( - self, distinct_id, event_name, send_feature_flags=True - ): - """ - Capture an event with all feature flags in PostHog. - - Args: - distinct_id (str): The distinct ID of the user. - event_name (str): The name of the event. - send_feature_flags (bool, optional): Whether to send feature flags. Defaults to True. - """ - self.posthog.capture( - distinct_id, - event_name, - send_feature_flags=send_feature_flags, - ) +posthog = Posthog( + project_api_key="phc_Gz6XxldNZIkzW7QnSTGr5HZ28OAYPIfpE7X5A3vUsfO", + host="https://app.posthog.com", +) diff --git a/swarms/utils/disable_logging.py b/swarms/utils/disable_logging.py index e7d2e5e6..7f04555f 100644 --- a/swarms/utils/disable_logging.py +++ b/swarms/utils/disable_logging.py @@ -2,6 +2,10 @@ import logging import os import warnings import sys +import logging +import os +import warnings +import sys def disable_logging(): @@ -14,7 +18,7 @@ def disable_logging(): os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" # Set the logging level for the entire module - logging.basicConfig(level=logging.WARNING) + logging.basicConfig(level=logging.ERROR) try: log = logging.getLogger("pytorch") @@ -31,9 +35,10 @@ def disable_logging(): "wandb.docker.auth", ]: logger = logging.getLogger(logger_name) - logger.setLevel( - logging.WARNING - ) # Suppress DEBUG and info logs + logger.setLevel(logging.ERROR) + + # Remove all existing handlers + logging.getLogger().handlers = [] # Create a file handler to log errors to the file file_handler = logging.FileHandler("errors.txt") @@ -44,6 +49,3 @@ def disable_logging(): stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.ERROR) logging.getLogger().addHandler(stream_handler) - - -disable_logging() diff --git a/swarms/utils/dist_utils.py b/swarms/utils/dist_utils.py index cb766369..76d9e03c 100644 --- a/swarms/utils/dist_utils.py +++ b/swarms/utils/dist_utils.py @@ -67,9 +67,7 @@ def rowwise_parallelize_linear( """ for name, param in module.named_parameters(): dist_spec = ( - [Shard(1)] - if name == "weight" - else [Replicate()] # type: ignore[list-item] + [Shard(1)] if name == "weight" else [Replicate()] # type: ignore[list-item] ) dist_tensor = distribute_tensor(param, device_mesh, dist_spec) @@ -84,9 +82,7 @@ def rowwise_parallelize_linear( # Weight, bias and scale are registered as buffer in QLinear for name, buffer in module.named_buffers(): dist_spec = ( - [Shard(1)] - if name == "weight" - else [Replicate()] # type: ignore[list-item] + [Shard(1)] if name == "weight" else [Replicate()] # type: ignore[list-item] ) dist_tensor = distribute_tensor( diff --git a/swarms/utils/logger.py b/swarms/utils/logger.py index 8be958b0..804a4fb1 100644 --- a/swarms/utils/logger.py +++ b/swarms/utils/logger.py @@ -1,16 +1,14 @@ -import logging +import datetime import functools - +import logging logger = logging.getLogger() formatter = logging.Formatter("%(message)s") ch = logging.StreamHandler() - ch.setFormatter(formatter) logger.addHandler(ch) - logger.setLevel(logging.DEBUG) @@ -44,3 +42,41 @@ def log_wrapper(func): raise return wrapper + + +class Logger: + """ + A utility class for logging messages with timestamps and levels. + + Attributes: + logger (logging.Logger): The logger object used for logging messages. + formatter (logging.Formatter): The formatter object used to format log messages. + ch (logging.StreamHandler): The stream handler object used to handle log messages. + """ + + logger = logging.getLogger(__name__) + formatter = logging.Formatter( + "[%(asctime)s] %(levelname)s %(message)s" + ) + ch = logging.StreamHandler() + ch.setFormatter(formatter) + logger.addHandler(ch) + logger.setLevel(logging.DEBUG) + + @staticmethod + def log(level, task, message): + """ + Logs a message with the specified level, task, and message. + + Args: + level (int): The logging level of the message. + task (str): The task associated with the message. + message (str): The message to be logged. + """ + timestamp = datetime.datetime.now().strftime( + "%d/%m/%y %H:%M:%S" + ) + formatted_message = ( + f"[{timestamp}] {level:<8} {task}\n{' ' * 29}{message}" + ) + Logger.logger.log(level, formatted_message) From c529244913f3b10d63d5c60d4f17ef4518bba75b Mon Sep 17 00:00:00 2001 From: Kye Date: Wed, 7 Feb 2024 09:44:08 -0800 Subject: [PATCH 103/103] [EXAMPLE][Fully Multi Modal Rag agent] --- .gitignore | 1 + example.py | 24 +--------- multi_modal_rag_agent.py | 79 +++++++++++++++++++++++++++++++++ pyproject.toml | 4 +- swarms/memory/chroma_db.py | 52 +++++++++++++++++++++- swarms/prompts/worker_prompt.py | 9 +++- swarms/structs/agent.py | 28 +++++++++--- 7 files changed, 164 insertions(+), 33 deletions(-) create mode 100644 multi_modal_rag_agent.py diff --git a/.gitignore b/.gitignore index aa7e0f98..62014a4f 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ dataframe/ static/generated runs +chroma swarms/__pycache__ venv .DS_Store diff --git a/example.py b/example.py index d3032ac4..d5cc891e 100644 --- a/example.py +++ b/example.py @@ -1,31 +1,11 @@ -import os - -from dotenv import load_dotenv - -# Import the OpenAIChat model and the Agent struct from swarms import Agent, OpenAIChat -# 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 = OpenAIChat( - temperature=0.5, - model_name="gpt-4", - openai_api_key=api_key, - max_tokens=1000, -) - ## Initialize the workflow agent = Agent( - llm=llm, - max_loops=4, + llm=OpenAIChat(), + max_loops="auto", autosave=True, dashboard=False, - # docs_folder="docs", streaming_on=True, verbose=True, ) diff --git a/multi_modal_rag_agent.py b/multi_modal_rag_agent.py new file mode 100644 index 00000000..33b2acb5 --- /dev/null +++ b/multi_modal_rag_agent.py @@ -0,0 +1,79 @@ +# Importing necessary modules +import os +from dotenv import load_dotenv +from swarms import Agent, OpenAIChat +from swarms.tools.tool import tool +from swarms.prompts.visual_cot import VISUAL_CHAIN_OF_THOUGHT +from swarms.memory.chroma_db import ChromaDB + +# Loading environment variables from .env file +load_dotenv() + +# Getting the Gemini API key from environment variables +gemini_api_key = os.getenv("GEMINI_API_KEY") +openai_api_key = os.getenv("OPENAI_API_KEY") + +llm = OpenAIChat( + openai_api_key=openai_api_key, + max_tokens=1000, + temperature=0.2, +) + +# Making an instance of the ChromaDB class +memory = ChromaDB( + metric="cosine", + n_results=3, + multimodal=True, + docs_folder="images", + output_dir="results", +) + + +# Defining tool by creating a function and wrapping it with the @tool decorator and +# providing the necessary parameters and docstrings to show the usage of the tool. +@tool +def make_new_file(file: str, content: str): + """ + Make a new file. + + This function creates a new file with the given name. + + Parameters: + file (str): The name of the file to be created. + + Returns: + dict: A dictionary containing the status of the operation. + """ + with open(file, "w") as f: + f.write(f"{content}") + + +# Initializing the agent with the Gemini instance and other parameters +agent = Agent( + llm=llm, + agent_name="Multi-Modal RAG Agent", + agent_description=( + "This agent fuses together the capabilities of Gemini and" + " Visual Chain of Thought to answer questions based on the" + " input image." + ), + max_loops="auto", + autosave=True, + sop=VISUAL_CHAIN_OF_THOUGHT, + verbose=True, + tools=[make_new_file], + long_term_memory=memory, +) + + +# Defining the task and image path +task = ( + "What is the content of this image, return exactly what you see" + " in the image." +) +img = "images/Screenshot_48.png" + + +# Running the agent with the specified task and image +out = agent.run(task=task, img=img) +print(out) diff --git a/pyproject.toml b/pyproject.toml index bb12e7a4..781dbbe0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "4.1.0" +version = "4.1.1" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] @@ -12,7 +12,7 @@ homepage = "https://github.com/kyegomez/swarms" documentation = "https://swarms.apac.ai" readme = "README.md" # Assuming you have a README.md repository = "https://github.com/kyegomez/swarms" -keywords = ["artificial intelligence", "deep learning", "optimizers", "Prompt Engineering"] +keywords = ["artificial intelligence", "deep learning", "optimizers", "Prompt Engineering", "swarms", "agents"] classifiers = [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", diff --git a/swarms/memory/chroma_db.py b/swarms/memory/chroma_db.py index e951ecd9..f1436d9c 100644 --- a/swarms/memory/chroma_db.py +++ b/swarms/memory/chroma_db.py @@ -1,3 +1,4 @@ +import os import numpy as np import logging import uuid @@ -10,6 +11,8 @@ from dotenv import load_dotenv from chromadb.utils.embedding_functions import ( OpenCLIPEmbeddingFunction, ) +from swarms.utils.data_to_text import data_to_text +from swarms.utils.markdown_message import display_markdown_message # Load environment variables @@ -51,6 +54,8 @@ class ChromaDB: embedding_function: Callable = None, data_loader: Callable = None, multimodal: bool = False, + docs_folder: str = None, + verbose: bool = False, *args, **kwargs, ): @@ -58,9 +63,12 @@ class ChromaDB: self.output_dir = output_dir self.limit_tokens = limit_tokens self.n_results = n_results + self.docs_folder = docs_folder + self.verbose = verbose # Disable ChromaDB logging - logging.getLogger("chromadb").setLevel(logging.INFO) + if verbose: + logging.getLogger("chromadb").setLevel(logging.INFO) # Create Chroma collection chroma_persist_dir = "chroma" @@ -100,6 +108,19 @@ class ChromaDB: *args, **kwargs, ) + display_markdown_message( + "ChromaDB collection created:" + f" {self.collection.name} with metric: {self.metric} and" + f" output directory: {self.output_dir}" + ) + + # If docs + if docs_folder: + display_markdown_message( + f"Traversing directory: {docs_folder}" + ) + self.docs = docs_folder + self.traverse_directory() def add( self, @@ -161,3 +182,32 @@ class ChromaDB: return docs[0] except Exception as e: raise Exception(f"Failed to query documents: {str(e)}") + + def traverse_directory(self): + """ + Traverse through every file in the given directory and its subdirectories, + and return the paths of all files. + Parameters: + - directory_name (str): The name of the directory to traverse. + Returns: + - list: A list of paths to each file in the directory and its subdirectories. + """ + image_extensions = [ + ".jpg", + ".jpeg", + ".png", + ] + images = [] + for root, dirs, files in os.walk(self.docs): + for file in files: + _, ext = os.path.splitext(file) + if ext.lower() in image_extensions: + images.append(os.path.join(root, file)) + else: + data = data_to_text(file) + added_to_db = self.add([data]) + print(f"{file} added to Database") + if images: + added_to_db = self.add(img_urls=[images]) + print(f"{len(images)} images added to Database ") + return added_to_db diff --git a/swarms/prompts/worker_prompt.py b/swarms/prompts/worker_prompt.py index 5ba0b5e5..c9c78733 100644 --- a/swarms/prompts/worker_prompt.py +++ b/swarms/prompts/worker_prompt.py @@ -1,4 +1,9 @@ -def worker_agent_system(name: str, memory: str = None): +import datetime + +time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") + + +def worker_tools_sop_promp(name: str, memory: str = None): return """ You are {name}, Your decisions must always be made independently without seeking user assistance. @@ -52,7 +57,7 @@ def worker_agent_system(name: str, memory: str = None): } } Ensure the response can be parsed by Python json.loads - System: The current time and date is Sat Jan 20 10:39:07 2024 + System: The current time and date is {time} System: This reminds you of these events from your past: [{memory}] diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index a0f85814..8e865420 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -27,6 +27,8 @@ from swarms.utils.parse_code import ( ) from swarms.utils.pdf_to_text import pdf_to_text from swarms.utils.token_count_tiktoken import limit_tokens_from_string +from swarms.tools.exec_tool import execute_tool_by_name +from swarms.prompts.worker_prompt import worker_tools_sop_promp # Utils @@ -179,7 +181,7 @@ class Agent: docs_folder: str = None, verbose: bool = False, *args, - **kwargs: Any, + **kwargs, ): self.id = id self.llm = llm @@ -264,6 +266,14 @@ class Agent: if verbose: logger.setLevel(logging.DEBUG) + # If tools are provided then set the tool prompt by adding to sop + if self.tools: + self.sop = self.sop + worker_tools_sop_promp( + self.agent_name, memory="" + ) + + # If the long term memory is provided then set the long term memory prompt + def set_system_prompt(self, system_prompt: str): """Set the system prompt""" self.system_prompt = system_prompt @@ -545,6 +555,14 @@ class Agent: if self.code_interpreter: self.run_code(response) + # If tools are enabled then execute the tools + if self.tools: + execute_tool_by_name( + response, + self.tools, + self.stopping_condition, + ) + # If interactive mode is enabled then print the response and get user input if self.interactive: print(f"AI: {response}") @@ -656,7 +674,7 @@ class Agent: """ return agent_history_prompt - def long_term_memory_prompt(self, query: str): + def long_term_memory_prompt(self, query: str, *args, **kwargs): """ Generate the agent long term memory prompt @@ -667,12 +685,10 @@ class Agent: Returns: str: The agent history prompt """ - ltr = str(self.long_term_memory.query(query)) + ltr = str(self.long_term_memory.query(query), *args, **kwargs) context = f""" - {query} - ####### Long Term Memory ################ - {ltr} + System: This reminds you of these events from your past: [{ltr}] """ return self.short_memory.add( role=self.agent_name, content=context