diff --git a/.github/action.yml b/.github/action.yml
new file mode 100644
index 00000000..f2f9016c
--- /dev/null
+++ b/.github/action.yml
@@ -0,0 +1,37 @@
+name: "Init Environment"
+description: "Initialize environment for tests"
+runs:
+ using: "composite"
+ steps:
+ - name: Checkout actions
+ uses: actions/checkout@v3
+
+ - name: Set up Python ${{ matrix.python-version }}
+ uses: actions/setup-python@v4
+ with:
+ python-version: ${{ matrix.python-version }}
+
+ - name: Install and configure Poetry
+ uses: snok/install-poetry@v1
+ with:
+ virtualenvs-create: true
+ virtualenvs-in-project: true
+ installer-parallel: true
+
+ - name: Load cached venv
+ id: cached-poetry-dependencies
+ uses: actions/cache@v3
+ with:
+ path: .venv
+ key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
+
+ - name: Install dependencies
+ if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
+ run: poetry install --no-interaction --no-root --with test --with dev --all-extras
+ shell: bash
+
+ - name: Activate venv
+ run: |
+ source .venv/bin/activate
+ echo PATH=$PATH >> $GITHUB_ENV
+ shell: bash
\ No newline at end of file
diff --git a/.github/workflows/codacy.yml b/.github/workflows/codacy.yml
index 0802c56a..55e4b49f 100644
--- a/.github/workflows/codacy.yml
+++ b/.github/workflows/codacy.yml
@@ -56,6 +56,6 @@ jobs:
# Upload the SARIF file generated in the previous step
- name: Upload SARIF results file
- uses: github/codeql-action/upload-sarif@v2
+ uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: results.sarif
diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml
index a2d42089..56930314 100644
--- a/.github/workflows/codeql.yml
+++ b/.github/workflows/codeql.yml
@@ -50,7 +50,7 @@ jobs:
# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
- uses: github/codeql-action/init@v2
+ uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -64,7 +64,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
- uses: github/codeql-action/autobuild@v2
+ uses: github/codeql-action/autobuild@v3
# βΉοΈ Command-line programs to run using the OS shell.
# π See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
@@ -77,6 +77,6 @@ jobs:
# ./location_of_script_within_repo/buildscript.sh
- name: Perform CodeQL Analysis
- uses: github/codeql-action/analyze@v2
+ uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"
diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml
index 8a6f374c..b38491c2 100644
--- a/.github/workflows/lint.yml
+++ b/.github/workflows/lint.yml
@@ -1,5 +1,3 @@
----
-# This is a basic workflow to help you get started with Actions
name: Lint
diff --git a/.github/workflows/pr_labeler.yml b/.github/workflows/pr_labeler.yml
index 19f18a19..7745d717 100644
--- a/.github/workflows/pr_labeler.yml
+++ b/.github/workflows/pr_labeler.yml
@@ -11,5 +11,5 @@ jobs:
pull-requests: write
runs-on: ubuntu-latest
steps:
- - uses: actions/labeler@v4
+ - uses: actions/labeler@v5
if: ${{ github.event.pull_request.draft == false }}
\ No newline at end of file
diff --git a/.github/workflows/pr_request_checks.yml b/.github/workflows/pr_request_checks.yml
index 6c9cb0b2..cfc01afb 100644
--- a/.github/workflows/pr_request_checks.yml
+++ b/.github/workflows/pr_request_checks.yml
@@ -1,4 +1,3 @@
----
name: Pull Request Checks
on:
@@ -22,6 +21,7 @@ jobs:
- name: Install dependencies
run: |
pip install -r requirements.txt
+ pip install swarms
pip install pytest
- name: Run tests and checks
diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml
index aa3edc3e..adc0c5ef 100644
--- a/.github/workflows/python-app.yml
+++ b/.github/workflows/python-app.yml
@@ -26,7 +26,7 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
- pip install flake8 pytest
+ pip install flake8 pytest swarms
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
diff --git a/.github/workflows/python-package.yml b/.github/workflows/python-package.yml
index ae647f57..31cdfb93 100644
--- a/.github/workflows/python-package.yml
+++ b/.github/workflows/python-package.yml
@@ -28,7 +28,7 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade swarms
- python -m pip install flake8 pytest
+ python -m pip install flake8 pytest swarms
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
diff --git a/.github/workflows/python-publish.yml b/.github/workflows/python-publish.yml
index 6a0f06c5..889774f0 100644
--- a/.github/workflows/python-publish.yml
+++ b/.github/workflows/python-publish.yml
@@ -1,4 +1,3 @@
----
name: Upload Python Package
on: # yamllint disable-line rule:truthy
diff --git a/.github/workflows/stale_isseue.yml b/.github/workflows/stale_isseue.yml
index e3b2e570..c5fe2648 100644
--- a/.github/workflows/stale_isseue.yml
+++ b/.github/workflows/stale_isseue.yml
@@ -19,7 +19,7 @@ jobs:
pull-requests: write
steps:
- - uses: actions/stale@v5
+ - uses: actions/stale@v9
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
days-before-issue-stale: 14
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index d9dafc76..baaadfc8 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -1,4 +1,3 @@
----
name: test
on:
@@ -10,6 +9,7 @@ on:
env:
POETRY_VERSION: "1.4.2"
+jobs:
test:
runs-on: ubuntu-latest
strategy:
@@ -30,7 +30,7 @@ env:
python-version: ${{ matrix.python-version }}
poetry-version: "1.4.2"
cache-key: ${{ matrix.test_type }}
- install-command: |
+ install-command:
if [ "${{ matrix.test_type }}" == "core" ]; then
echo "Running core tests, installing dependencies with poetry..."
poetry install
diff --git a/.github/workflows/test_pr.yml b/.github/workflows/test_pr.yml
index 3a1307a3..8da3bd0a 100644
--- a/.github/workflows/test_pr.yml
+++ b/.github/workflows/test_pr.yml
@@ -16,8 +16,8 @@ jobs:
name: Test Py3.11
runs-on: ubuntu-latest
steps:
- - uses: actions/checkout@v3
- - uses: actions/setup-python@v4
+ - uses: actions/checkout@v4
+ - uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Run tests
@@ -29,8 +29,8 @@ jobs:
name: Test Py3.10
runs-on: ubuntu-latest
steps:
- - uses: actions/checkout@v3
- - uses: actions/setup-python@v4
+ - uses: actions/checkout@v4
+ - uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Run tests
@@ -42,8 +42,8 @@ jobs:
name: Test Py3.9
runs-on: ubuntu-latest
steps:
- - uses: actions/checkout@v3
- - uses: actions/setup-python@v4
+ - uses: actions/checkout@v4
+ - uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Run tests
@@ -55,8 +55,8 @@ jobs:
name: pytype 3.10
runs-on: ubuntu-latest
steps:
- - uses: actions/checkout@v3
- - uses: actions/setup-python@v4
+ - uses: actions/checkout@v4
+ - uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Run pytype
@@ -69,8 +69,8 @@ jobs:
name: Check format with black
runs-on: ubuntu-latest
steps:
- - uses: actions/checkout@v3
- - uses: actions/setup-python@v4
+ - uses: actions/checkout@v4
+ - uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Check format
diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml
index ccb5a6b9..c858b5fe 100644
--- a/.github/workflows/testing.yml
+++ b/.github/workflows/testing.yml
@@ -16,7 +16,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v5
with:
- python-version: 3.x
+ python-version: "3.10"
- name: Install dependencies
run: |
@@ -24,4 +24,4 @@ jobs:
pip install pytest
- name: Run unit tests
- run: pytest
\ No newline at end of file
+ run: pytest
diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml
index 1e4f0f1e..a858dae4 100644
--- a/.github/workflows/unit-test.yml
+++ b/.github/workflows/unit-test.yml
@@ -24,6 +24,7 @@ jobs:
run: |
pip install -r requirements.txt
pip install pytest
+ pip install swarms
- name: Run Python unit tests
run: pytest
diff --git a/.gitignore b/.gitignore
index 93f8e5c0..62014a4f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,6 +10,7 @@ dataframe/
static/generated
runs
+chroma
swarms/__pycache__
venv
.DS_Store
@@ -18,6 +19,7 @@ venv
swarms/agents/.DS_Store
_build
+conversation.txt
stderr_log.txt
.vscode
@@ -27,6 +29,7 @@ __pycache__/
*.py[cod]
*$py.class
.grit
+swarm-worker-01_state.json
error.txt
# C extensions
diff --git a/README.md b/README.md
index e52b872d..1b22bc10 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.
[](https://github.com/kyegomez/swarms/issues) [](https://github.com/kyegomez/swarms/network) [](https://github.com/kyegomez/swarms/stargazers) [](https://github.com/kyegomez/swarms/blob/main/LICENSE)[](https://star-history.com/#kyegomez/swarms)[](https://libraries.io/github/kyegomez/swarms) [](https://pepy.tech/project/swarms)
@@ -17,20 +16,27 @@ Swarms is a modular framework that enables reliable and useful multi-agent colla
----
## Installation
-`pip3 install --upgrade swarms`
+`pip3 install -U swarms`
---
## 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:
-### `Agent` Example
-- Reliable Structure that provides LLMS autonomy
-- Extremely Customizeable with stopping conditions, interactivity, dynamical temperature, loop intervals, and so much more
-- Enterprise Grade + Production Grade: `Agent` is designed and optimized for automating real-world tasks at scale!
+### `Agent`
+A fully plug in and play Autonomous agent powered by an LLM extended by a long term memory database, and equipped with function calling for tool usage! By passing in an LLM you can create a fully autonomous agent with extreme customization and reliability ready for real-world task automation!
+
+Features:
+
+β
Any LLM / Any framework
+
+β
Extremely customize-able with max loops, autosaving, import docs (PDFS, TXT, CSVs, etc), tool usage, etc etc
+
+β
Long term memory database with RAG (ChromaDB, Pinecone, Qdrant)
```python
import os
@@ -38,8 +44,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()
@@ -64,20 +69,119 @@ agent.run("Generate a 10,000 word blog on health and wellness.")
+```
+
+
+### `ToolAgent`
+ToolAgent is an agent that outputs JSON using any model from huggingface. It takes in an example schema with fields and then you provide it with a simple task and it'll output json! Perfect for function calling, parallel, and multi-step tool usage!
+
+β
Versatility: The ToolAgent class is designed to be flexible and adaptable. It can be used with any model and tokenizer, making it suitable for a wide range of tasks. This versatility means that you can use ToolAgent as a foundation for any tool that requires language model processing.
+
+β
Ease of Use: With its simple and intuitive interface, ToolAgent makes it easy to perform complex tasks. Just initialize it with your model, tokenizer, and JSON schema, and then call the run method with your task. This ease of use allows you to focus on your task, not on setting up your tools.
+
+β
Customizability: ToolAgent accepts variable length arguments and keyword arguments, allowing you to customize its behavior to suit your needs. Whether you need to adjust the temperature of the model's output, limit the number of tokens, or tweak any other parameter, ToolAgent has you covered. This customizability ensures that ToolAgent can adapt to your specific requirements.
+
+
+```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"}},
+ },
+}
+
+# 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)
+
+
+```
+
+
+### `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)
+
+
```
------
### `SequentialWorkflow`
-- A Sequential swarm of autonomous agents where each agent's outputs are fed into the next agent
-- Save and Restore Workflow states!
-- Integrate Agent's with various LLMs and Multi-Modality Models
+Sequential Workflow enables you to sequentially execute tasks with `Agent` and then pass the output into the next agent and onwards until you have specified your max loops. `SequentialWorkflow` is wonderful for real-world business tasks like sending emails, summarizing documents, and analyzing data.
+
+
+β
Save and Restore Workflow states!
+
+β
Multi-Modal Support for Visual Chaining
+
+β
Utilizes Agent class
```python
import os
-from swarms.models import OpenAIChat
-from swarms.structs import Agent
-from swarms.structs.sequential_workflow import SequentialWorkflow
+from swarms import OpenAIChat, Agent, SequentialWorkflow
from dotenv import load_dotenv
load_dotenv()
@@ -123,66 +227,192 @@ workflow.run()
# Output the results
for task in workflow.tasks:
print(f"Task: {task.description}, Result: {task.result}")
+```
+
+
+
+### `ConcurrentWorkflow`
+`ConcurrentWorkflow` runs all the tasks all at the same time with the inputs you give it!
+
+
+```python
+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(tasks=[task1, task2, task3])
+
+# Run the workflow
+workflow.run()
```
-## `Multi Modal Autonomous Agents`
-- Run the agent with multiple modalities useful for various real-world tasks in manufacturing, logistics, and health.
+### `RecursiveWorkflow`
+`RecursiveWorkflow` will keep executing the tasks until a specific token like
is located inside the text!
+
+```python
+import os
+from dotenv import load_dotenv
+from swarms import OpenAIChat, Task, RecursiveWorkflow, 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 = RecursiveWorkflow(stop_token="")
+
+# 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()
+
+
+```
+
+
+
+### `ModelParallelizer`
+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.
+
```python
-# Description: This is an example of how to use the Agent class to run a multi-modal workflow
import os
+
from dotenv import load_dotenv
-from swarms.models.gpt4_vision_api import GPT4VisionAPI
-from swarms.structs import Agent
-# Load the environment variables
+from swarms import Anthropic, Gemini, Mixtral, OpenAIChat, ModelParallelizer
+
load_dotenv()
-# Get the API key from the environment
-api_key = os.environ.get("OPENAI_API_KEY")
+# API Keys
+anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
+openai_api_key = os.getenv("OPENAI_API_KEY")
+gemini_api_key = os.getenv("GEMINI_API_KEY")
-# Initialize the language model
-llm = GPT4VisionAPI(
- openai_api_key=api_key,
- max_tokens=500,
-)
+# Initialize the models
+llm = OpenAIChat(openai_api_key=openai_api_key)
+anthropic = Anthropic(anthropic_api_key=anthropic_api_key)
+mixtral = Mixtral()
+gemini = Gemini(gemini_api_key=gemini_api_key)
-# Initialize the task
-task = (
- "Analyze this image of an assembly line and identify any issues such as"
- " misaligned parts, defects, or deviations from the standard assembly"
- " process. IF there is anything unsafe in the image, explain why it is"
- " unsafe and how it could be improved."
+# Initialize the parallelizer
+llms = [llm, anthropic, mixtral, gemini]
+parallelizer = ModelParallelizer(llms)
+
+# Set the task
+task = "Generate a 10,000 word blog on health and wellness."
+
+# Run the task
+out = parallelizer.run(task)
+
+# Print the responses 1 by 1
+for i in range(len(out)):
+ print(f"Response from LLM {i}: {out[i]}")
+```
+
+
+### Simple Conversational Agent
+A Plug in and play conversational agent with `GPT4`, `Mixytral`, or any of our models
+
+- Reliable conversational structure to hold messages together with dynamic handling for long context conversations and interactions with auto chunking
+- Reliable, this simple system will always provide responses you want.
+
+```python
+import os
+
+from dotenv import load_dotenv
+
+from swarms import (
+ OpenAIChat,
+ Conversation,
)
-img = "assembly_line.jpg"
-## Initialize the workflow
-agent = Agent(
- llm=llm,
- max_loops="auto",
- autosave=True,
- dashboard=True,
- multi_modal=True
+conv = Conversation(
+ time_enabled=True,
)
-# Run the workflow on a task
-agent.run(task=task, img=img)
+# 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(openai_api_key=api_key, model_name="gpt-4")
+
+# Run the language model in a loop
+def interactive_conversation(llm):
+ conv = Conversation()
+ while True:
+ 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")
+
+
+# Replace with your LLM instance
+interactive_conversation(llm)
```
-### `OmniModalAgent`
-- An agent that can understand any modality and conditionally generate any modality.
+### `SwarmNetwork`
+`SwarmNetwork` provides the infrasturcture for building extremely dense and complex multi-agent applications that span across various types of agents.
+
+β
Efficient Task Management: SwarmNetwork's intelligent agent pool and task queue management system ensures tasks are distributed evenly across agents. This leads to efficient use of resources and faster task completion.
+
+β
Scalability: SwarmNetwork can dynamically scale the number of agents based on the number of pending tasks. This means it can handle an increase in workload by adding more agents, and conserve resources when the workload is low by reducing the number of agents.
+
+β
Versatile Deployment Options: With SwarmNetwork, each agent can be run on its own thread, process, container, machine, or even cluster. This provides a high degree of flexibility and allows for deployment that best suits the user's needs and infrastructure.
```python
-from swarms.agents.omni_modal_agent import OmniModalAgent, OpenAIChat
-from swarms.models import OpenAIChat
-from dotenv import load_dotenv
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()
@@ -192,21 +422,224 @@ 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,
)
+## 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(
+ agent2.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)
+
+```
+
+
+### `Task`
+`Task` is a simple structure for task execution with the `Agent`. Imagine zapier for LLM-based workflow automation
+
+β
Task is a structure for task execution with the Agent.
+
+β
Tasks can have descriptions, scheduling, triggers, actions, conditions, dependencies, priority, and a history.
+
+β
The Task structure allows for efficient workflow automation with LLM-based agents.
+
+```python
+import os
+
+from dotenv import load_dotenv
+
+from swarms.structs import Agent, OpenAIChat, Task
+
+# Load the environment variables
+load_dotenv()
+
+
+# Define a function to be used as the action
+def my_action():
+ print("Action executed")
+
+
+# Define a function to be used as the condition
+def my_condition():
+ print("Condition checked")
+ return True
+
+
+# Create an agent
+agent = Agent(
+ llm=OpenAIChat(openai_api_key=os.environ["OPENAI_API_KEY"]),
+ max_loops=1,
+ dashboard=False,
+)
+
+# Create a task
+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)
+task.set_condition(my_condition)
+
+# Execute the task
+print("Executing task...")
+task.run()
+
+# Check if the task is completed
+if task.is_completed():
+ print("Task completed")
+else:
+ print("Task not completed")
+
+# Output the result of the task
+print(f"Task result: {task.result}")
+
-agent = OmniModalAgent(llm)
-agent.run("Generate a video of a swarm of fish and then make an image out of the video")
```
---
+
+
+### `BlockList`
+- Modularity and Flexibility: BlocksList allows users to create custom swarms by adding or removing different classes or functions as blocks. This means users can easily tailor the functionality of their swarm to suit their specific needs.
+
+- Ease of Management: With methods to add, remove, update, and retrieve blocks, BlocksList provides a straightforward way to manage the components of a swarm. This makes it easier to maintain and update the swarm over time.
+
+- Enhanced Searchability: BlocksList offers methods to get blocks by various attributes such as name, type, ID, and parent-related properties. This makes it easier for users to find and work with specific blocks in a large and complex swarm.
+
+```python
+import os
+
+from dotenv import load_dotenv
+from transformers import AutoModelForCausalLM, AutoTokenizer
+
+# Import the models, structs, and telemetry modules
+from swarms import (
+ Gemini,
+ GPT4VisionAPI,
+ Mixtral,
+ OpenAI,
+ ToolAgent,
+ BlocksList,
+)
+
+# Load the environment variables
+load_dotenv()
+
+# Get the environment variables
+openai_api_key = os.getenv("OPENAI_API_KEY")
+gemini_api_key = os.getenv("GEMINI_API_KEY")
+
+# Tool Agent
+model = AutoModelForCausalLM.from_pretrained(
+ "databricks/dolly-v2-12b"
+)
+tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-12b")
+json_schema = {
+ "type": "object",
+ "properties": {
+ "name": {"type": "string"},
+ "age": {"type": "number"},
+ "is_student": {"type": "boolean"},
+ "courses": {"type": "array", "items": {"type": "string"}},
+ },
+}
+toolagent = ToolAgent(
+ model=model, tokenizer=tokenizer, json_schema=json_schema
+)
+
+# Blocks List which enables you to build custom swarms by adding classes or functions
+swarm = BlocksList(
+ "SocialMediaSwarm",
+ "A swarm of social media agents",
+ [
+ OpenAI(openai_api_key=openai_api_key),
+ Mixtral(),
+ GPT4VisionAPI(openai_api_key=openai_api_key),
+ Gemini(gemini_api_key=gemini_api_key),
+ ],
+)
+
+
+# Add the new block to the swarm
+swarm.add(toolagent)
+
+# Remove a block from the swarm
+swarm.remove(toolagent)
+
+# Update a block in the swarm
+swarm.update(toolagent)
+
+# Get a block at a specific index
+block_at_index = swarm.get(0)
+
+# Get all blocks in the swarm
+all_blocks = swarm.get_all()
+
+# Get blocks by name
+openai_blocks = swarm.get_by_name("OpenAI")
+
+# Get blocks by type
+gpt4_blocks = swarm.get_by_type("GPT4VisionAPI")
+
+# Get blocks by ID
+block_by_id = swarm.get_by_id(toolagent.id)
+
+# Get blocks by parent
+blocks_by_parent = swarm.get_by_parent(swarm)
+
+# Get blocks by parent ID
+blocks_by_parent_id = swarm.get_by_parent_id(swarm.id)
+
+# Get blocks by parent name
+blocks_by_parent_name = swarm.get_by_parent_name(swarm.name)
+
+# Get blocks by parent type
+blocks_by_parent_type = swarm.get_by_parent_type(type(swarm).__name__)
+
+# Get blocks by parent description
+blocks_by_parent_description = swarm.get_by_parent_description(
+ swarm.description
+)
+
+# Run the block in the swarm
+inference = swarm.run_block(toolagent, "Hello World")
+print(inference)
+```
+
+
+## Real-World Deployment
+
### Multi-Agent Swarm for Logistics
-- Swarms is a framework designed for real-world deployment here is a demo presenting a fully ready to use Swarm for a vast array of logistics tasks.
-- Swarms is designed to be modular and reliable for real-world deployments.
-- Swarms is the first framework that unleases multi-modal autonomous agents in the real world.
+Here's a production grade swarm ready for real-world deployment in a factory and logistics settings like warehouses. This swarm can automate 3 costly and inefficient workflows, safety checks, productivity checks, and warehouse security.
+
```python
from swarms.structs import Agent
@@ -314,6 +747,340 @@ efficiency_analysis = efficiency_agent.run(
```
---
+
+## `Multi Modal Autonomous Agents`
+Run the agent with multiple modalities useful for various real-world tasks in manufacturing, logistics, and health.
+
+```python
+# Description: This is an example of how to use the Agent class to run a multi-modal workflow
+import os
+from dotenv import load_dotenv
+from swarms.models.gpt4_vision_api import GPT4VisionAPI
+from swarms.structs import Agent
+
+# 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 = GPT4VisionAPI(
+ openai_api_key=api_key,
+ max_tokens=500,
+)
+
+# Initialize the task
+task = (
+ "Analyze this image of an assembly line and identify any issues such as"
+ " misaligned parts, defects, or deviations from the standard assembly"
+ " process. IF there is anything unsafe in the image, explain why it is"
+ " unsafe and how it could be improved."
+)
+img = "assembly_line.jpg"
+
+## Initialize the workflow
+agent = Agent(
+ llm=llm,
+ max_loops="auto",
+ autosave=True,
+ dashboard=True,
+ multi_modal=True
+)
+
+# Run the workflow on a task
+agent.run(task=task, img=img)
+
+
+```
+
+---
+
+## Multi-Modal Model APIs
+
+### `Gemini`
+- Deploy Gemini from Google with utmost reliability with our visual chain of thought prompt that enables more reliable responses
+```python
+import os
+
+from dotenv import load_dotenv
+
+from swarms import Gemini
+from swarms.prompts.visual_cot import VISUAL_CHAIN_OF_THOUGHT
+
+# Load the environment variables
+load_dotenv()
+
+# Get the API key from the environment
+api_key = os.environ.get("GEMINI_API_KEY")
+
+# Initialize the language model
+llm = Gemini(
+ gemini_api_key=api_key,
+ temperature=0.5,
+ max_tokens=1000,
+ system_prompt=VISUAL_CHAIN_OF_THOUGHT,
+)
+
+# Initialize the task
+task = "This is an eye test. What do you see?"
+img = "playground/demos/multi_modal_chain_of_thought/eyetest.jpg"
+
+# Run the workflow on a task
+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
+# Import necessary modules and classes
+from swarms.models import Anthropic
+
+# Initialize an instance of the Anthropic class
+model = Anthropic(
+ anthropic_api_key=""
+)
+
+# Using the run method
+completion_1 = model.run("What is the capital of France?")
+print(completion_1)
+
+# Using the __call__ method
+completion_2 = model("How far is the moon from the earth?", stop=["miles", "km"])
+print(completion_2)
+
+```
+
+
+### `HuggingFaceLLM`
+```python
+from swarms.models import HuggingfaceLLM
+
+# Initialize with custom configuration
+custom_config = {
+ "quantize": True,
+ "quantization_config": {"load_in_4bit": True},
+ "verbose": True
+}
+inference = HuggingfaceLLM(model_id="NousResearch/Nous-Hermes-2-Vision-Alpha", **custom_config)
+
+# Generate text based on a prompt
+prompt_text = "Create a list of known biggest risks of structural collapse with references"
+generated_text = inference(prompt_text)
+print(generated_text)
+```
+
+### `Mixtral`
+- Utilize Mixtral in a very simple API,
+- Utilize 4bit quantization for a increased speed and less memory usage
+- Use Flash Attention 2.0 for increased speed and less memory usage
+```python
+from swarms.models import Mixtral
+
+# Initialize the Mixtral model with 4 bit and flash attention!
+mixtral = Mixtral(load_in_4bit=True, use_flash_attention_2=True)
+
+# Generate text for a simple task
+generated_text = mixtral.run("Generate a creative story.")
+
+# Print the generated text
+print(generated_text)
+```
+
+
+### `Dalle3`
+```python
+from swarms import Dalle3
+
+# Create an instance of the Dalle3 class with high quality
+dalle3 = Dalle3(quality="high")
+
+# Define a text prompt
+task = "A high-quality image of a sunset"
+
+# Generate a high-quality image from the text prompt
+image_url = dalle3(task)
+
+# Print the generated image URL
+print(image_url)
+```
+
+
+
+
+### Text to Video with `ZeroscopeTTV`
+
+```python
+# Import the model
+from swarms import ZeroscopeTTV
+
+# Initialize the model
+zeroscope = ZeroscopeTTV()
+
+# Specify the task
+task = "A person is walking on the street."
+
+# Generate the video!
+video_path = zeroscope(task)
+print(video_path)
+
+```
+
+
+
+
+
+
+----
+
+## Supported Models β
+Swarms supports various model providers like OpenAI, Huggingface, Anthropic, Google, Mistral and many more.
+
+| Provider | Provided β
| Module Name |
+|----------|-----------------------------|-------------|
+| OpenAI | β
| OpenAIChat, OpenAITTS, GPT4VisionAPI, Dalle3 |
+| Anthropic | β
| Anthropic |
+| Mistral | β
| Mistral, Mixtral |
+| Gemini/Palm | β
| Gemini |
+| Huggingface | β
| HuggingFaceLLM |
+| Modelscope | β
| Modelscope |
+| Vllm | β
| vLLM |
+
+
+---
+
# Features π€
The Swarms framework is designed with a strong emphasis on reliability, performance, and production-grade readiness.
Below are the key features that make Swarms an ideal choice for enterprise-level AI deployments.
@@ -384,30 +1151,55 @@ The Swarms framework is equipped with a suite of advanced AI capabilities design
Swarms framework is not just a tool but a robust, scalable, and secure partner in your AI journey, ready to tackle the challenges of modern AI applications in a business environment.
+---
## Documentation
-- For documentation, go here, [swarms.apac.ai](https://swarms.apac.ai)
+Documentation is located here at: [swarms.apac.ai](https://swarms.apac.ai)
+----
## π«Ά Contributions:
-Swarms is an open-source project, and contributions are welcome. If you want to contribute, you can create new features, fix bugs, or improve the infrastructure. Please refer to the [CONTRIBUTING.md](https://github.com/kyegomez/swarms/blob/master/CONTRIBUTING.md) and our [contributing board](https://github.com/users/kyegomez/projects/1) file in the repository for more information on how to contribute.
+The easiest way to contribute is to pick any issue with the `good first issue` tag πͺ. Read the Contributing guidelines [here](/CONTRIBUTING.md). Bug Report? [File here](https://github.com/swarms/gateway/issues) | Feature Request? [File here](https://github.com/swarms/gateway/issues)
-To see how to contribute, visit [Contribution guidelines](https://github.com/kyegomez/swarms/blob/master/CONTRIBUTING.md)
+Swarms is an open-source project, and contributions are VERY welcome. If you want to contribute, you can create new features, fix bugs, or improve the infrastructure. Please refer to the [CONTRIBUTING.md](https://github.com/kyegomez/swarms/blob/master/CONTRIBUTING.md) and our [contributing board](https://github.com/users/kyegomez/projects/1) to participate in Roadmap discussions!
+----
## Community
+
+Join our growing community around the world, for real-time support, ideas, and discussions on Swarms π
+
+- View our official [Blog](https://swarms.apac.ai)
+- Chat live with us on [Discord](https://discord.gg/kS3rwKs3ZC)
+- Follow us on [Twitter](https://twitter.com/kyegomez)
+- Connect with us on [LinkedIn](https://www.linkedin.com/company/the-swarm-corporation)
+- Visit us on [YouTube](https://www.youtube.com/channel/UC9yXyitkbU_WSy7bd_41SqQ)
- [Join the Swarms community on Discord!](https://discord.gg/AJazBmhKnr)
- Join our Swarms Community Gathering every Thursday at 1pm NYC Time to unlock the potential of autonomous agents in automating your daily tasks [Sign up here](https://lu.ma/5p2jnc2v)
-
+---
## Discovery Call
-Book a discovery call with the Swarms team to learn how to optimize and scale your swarm! [Click here to book a time that works for you!](https://calendly.com/swarm-corp/30min?month=2023-11)
+Book a discovery call to learn how Swarms can lower your operating costs by 40% with swarms of autonomous agents in lightspeed. [Click here to book a time that works for you!](https://calendly.com/swarm-corp/30min?month=2023-11)
+
+## Accelerate Backlog
+Help us accelerate our backlog by supporting us financially! Note, we're an open source corporation and so all the revenue we generate is through donations at the moment ;)
+
+
+
+## Swarm Newsletter π€ π€ π€ π§
+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
+
+
+
diff --git a/docs/ReleaseNotes.md b/docs/ReleaseNotes.md
new file mode 100644
index 00000000..f8859fa0
--- /dev/null
+++ b/docs/ReleaseNotes.md
@@ -0,0 +1,49 @@
+# 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
+
+### Bug Fixes
+
+Removed ModelScope
+
+Removed CogAgent
+
+### New Features
+
+Added ultralytics vision models
+
+Added TimmModel to wrap timm models
+
+### Other
+
+Loosened version of timm
diff --git a/docs/assets/img/swarmbanner.png b/docs/assets/img/swarmbanner.png
new file mode 100644
index 00000000..f88646db
Binary files /dev/null and b/docs/assets/img/swarmbanner.png differ
diff --git a/docs/corporate/data_room.md b/docs/corporate/data_room.md
new file mode 100644
index 00000000..946f209f
--- /dev/null
+++ b/docs/corporate/data_room.md
@@ -0,0 +1,95 @@
+# Swarms Data Room
+
+## 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.
+
+
+### **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 through modularity, customization, and precision.
+
+[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. | [](https://pepy.tech/project/swarms) |
+| Downloads this month | Number of downloads for the product in the current month. | [](https://pepy.tech/project/swarms) |
+| Total Downloads this week | Total number of downloads for the product in the current week. | [](https://pepy.tech/project/swarms) |
+| Github Forks | Number of times the product's codebase has been copied for optimization, contribution, or usage. | [](https://github.com/kyegomez/swarms/network) |
+| Github Stars | Number of users who have 'liked' the project. | [](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. | [](https://github.com/kyegomez/swarms/issues) |
+
diff --git a/docs/corporate/swarm_memo.md b/docs/corporate/swarm_memo.md
new file mode 100644
index 00000000..5ff21386
--- /dev/null
+++ b/docs/corporate/swarm_memo.md
@@ -0,0 +1,21 @@
+# [Go To Market Strategy][GTM]
+
+Our vision is to become the world leader in real-world production grade autonomous agent deployment through open-source product development, Deep Verticalization, and unmatched value delivery to the end user.
+
+We will focus on first accelerating the open source framework to PMF where it will serve as the backend for upstream products and services such as the Swarm Cloud which will enable enterprises to deploy autonomous agents with long term memory and tools in the cloud and a no-code platform for users to build their own swarm by dragging and dropping blocks.
+
+Our target user segment for the framework is AI engineers looking to deploy agents into high risk environments where reliability is crucial.
+
+Once PMF has been achieved and the framework has been extensively benchmarked we aim to establish high value contracts with customers in Security, Logistics, Manufacturing, Health and various other untapped industries.
+
+Our growth strategy for the OS framework can be summarized by:
+
+- Educating developers on value of autonomous agent usage.
+- Tutorial Walkthrough on various applications like deploying multi-modal agents through cameras or building custom swarms for a specific business operation.
+- Demonstrate unmatched reliability by delighting users.
+- Staying up to date with trends and integrating the latest models, frameworks, and methodologies.
+- Building a loyal and devoted community for long term user retention. [Join here](https://codex.apac.ai)
+
+As we continuously deliver value with the open framework we will strategically position ourselves to acquire leads for high value contracts by demonstrating the power, reliability, and performance of our framework openly.
+
+Acquire Full Access to the memo here: [TSC Memo](https://docs.google.com/document/d/1hS_nv_lFjCqLfnJBoF6ULY9roTbSgSuCkvXvSUSc7Lo/edit?usp=sharing)
\ No newline at end of file
diff --git a/docs/index.md b/docs/index.md
index 11f532ab..74e39543 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -1,19 +1,74 @@
-# Swarms Docs
+
-Welcome to Swarm's Documentation!
+## π Hello
-Swarms is a modular framework that enables reliable and useful multi-agent collaboration at scale to automate real-world tasks.
+Swarms provides you with all the building blocks you need to build reliable, production-grade, and scalable multi-agent apps!
-Swarms is transforming the landscape of AI from siloed AI agents to a unified 'swarm' of intelligence. Through relentless iteration and the power of collective insight from our 1500+ Agora researchers, we're developing a groundbreaking framework for AI collaboration. Our mission is to catalyze a paradigm shift, advancing Humanity with the power of unified autonomous AI agent swarms.
+## π» Install
+You can install `swarms` with pip in a
+[**Python>=3.8**](https://www.python.org/) environment.
-This documentation covers the fundamentals of the **Swarms** framework and describes how to use **Swarms Tools**.
+!!! example "pip install (recommended)"
-## Swarms
+ === "headless"
+ The headless installation of `swarms` is designed for environments where graphical user interfaces (GUI) are not needed, making it more lightweight and suitable for server-side applications.
-The Swarms framework provides developers with the ability to create AI systems that operate across two dimensions: predictability and creativity. For predictability, Swarms enforces structures like sequential pipelines, DAG-based workflows, and long-term memory. To facilitate creativity, Swarms safely prompts LLMs with [tools](https://github.com/kyegomez/swarms-tools) and short-term memory connecting them to external APIs and data stores. The framework allows developers to transition between those two dimensions effortlessly based on their use case.
+ ```bash
+ pip install swarms
+ ```
-Swarms not only helps developers harness the potential of LLMs but also enforces trust boundaries, schema validation, and tool activity-level permissions. By doing so, Swarms maximizes LLMsβ reasoning while adhering to strict policies regarding their capabilities.
+
+!!! example "git clone (for development)"
+
+ === "virtualenv"
+
+ ```bash
+ # clone repository and navigate to root directory
+ git clone https://github.com/kyegomez/swarms.git
+ cd swarms
+
+ # setup python environment and activate it
+ python3 -m venv venv
+ source venv/bin/activate
+ pip install --upgrade pip
+
+ # headless install
+ pip install -e "."
+
+ # desktop install
+ pip install -e ".[desktop]"
+ ```
+
+ === "poetry"
+
+ ```bash
+ # clone repository and navigate to root directory
+ git clone https://github.com/kyegomez/swarms.git
+ cd swarms
+
+ # setup python environment and activate it
+ poetry env use python3.10
+ poetry shell
+
+ # headless install
+ poetry install
+
+ # desktop install
+ poetry install --extras "desktop"
+ ```
+
+
+## Documentation
[Learn more about swarms β](swarms/)
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.
-
-
-
-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)
-
-
-
-
-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.
-
-
-
-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.
-
-
-
-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).
-
-
-
-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.
-
-
-
-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.
-
-
-
-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.
-
-
-
-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.
-
-
-
-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.
-
-
-
-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.
-
-
-
-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.
-
-
-
-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.
-
-
-
-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/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
+
+
+
+---
+
+### 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/docs/swarms/memory/pinecone.md b/docs/swarms/memory/pinecone.md
index 11f9a018..830d10fe 100644
--- a/docs/swarms/memory/pinecone.md
+++ b/docs/swarms/memory/pinecone.md
@@ -1,4 +1,4 @@
-# `PineconDB` Documentation
+# `PineconeDB` Documentation
## Table of Contents
diff --git a/docs/swarms/memory/weaviate.md b/docs/swarms/memory/weaviate.md
index 7044f1ad..b23baedf 100644
--- a/docs/swarms/memory/weaviate.md
+++ b/docs/swarms/memory/weaviate.md
@@ -28,7 +28,7 @@ pip install swarms
## Initialization
-To use the Weaviate API Client, you need to initialize an instance of the `WeaviateClient` class. Here are the parameters you can pass to the constructor:
+To use the Weaviate API Client, you need to initialize an instance of the `WeaviateDB` class. Here are the parameters you can pass to the constructor:
| Parameter | Type | Description |
|----------------------|----------------|----------------------------------------------------------------------------------------------------------------------------------|
@@ -43,12 +43,12 @@ To use the Weaviate API Client, you need to initialize an instance of the `Weavi
| `additional_config` | Optional[weaviate.AdditionalConfig] | Additional configuration for the client. (Optional) |
| `connection_params` | Dict[str, Any] | Dictionary containing connection parameters. This parameter is used internally and can be ignored in most cases. |
-Here's an example of how to initialize a WeaviateClient:
+Here's an example of how to initialize a WeaviateDB:
```python
-from swarms.memory import WeaviateClient
+from swarms.memory import WeaviateDB
-weaviate_client = WeaviateClient(
+weaviate_client = WeaviateDB(
http_host="YOUR_HTTP_HOST",
http_port="YOUR_HTTP_PORT",
http_secure=True,
diff --git a/docs/swarms/models/mixtral.md b/docs/swarms/models/mixtral.md
new file mode 100644
index 00000000..aa1b64d3
--- /dev/null
+++ b/docs/swarms/models/mixtral.md
@@ -0,0 +1,76 @@
+# Module Name: Mixtral
+
+## Introduction
+The Mixtral module is a powerful language model designed for text generation tasks. It leverages the MistralAI Mixtral-8x7B pre-trained model to generate high-quality text based on user-defined tasks or prompts. In this documentation, we will provide a comprehensive overview of the Mixtral module, including its architecture, purpose, arguments, and detailed usage examples.
+
+## Purpose
+The Mixtral module is designed to facilitate text generation tasks using state-of-the-art language models. Whether you need to generate creative content, draft text for various applications, or simply explore the capabilities of Mixtral, this module serves as a versatile and efficient solution. With its easy-to-use interface, you can quickly generate text for a wide range of applications.
+
+## Architecture
+The Mixtral module is built on top of the MistralAI Mixtral-8x7B pre-trained model. It utilizes a deep neural network architecture with 8 layers and 7 attention heads to generate coherent and contextually relevant text. The model is capable of handling a variety of text generation tasks, from simple prompts to more complex content generation.
+
+## Class Definition
+### `Mixtral(model_name: str = "mistralai/Mixtral-8x7B-v0.1", max_new_tokens: int = 500)`
+
+#### Parameters
+- `model_name` (str, optional): The name or path of the pre-trained Mixtral model. Default is "mistralai/Mixtral-8x7B-v0.1".
+- `max_new_tokens` (int, optional): The maximum number of new tokens to generate. Default is 500.
+
+## Functionality and Usage
+The Mixtral module offers a straightforward interface for text generation. It accepts a task or prompt as input and returns generated text based on the provided input.
+
+### `run(task: Optional[str] = None, **kwargs) -> str`
+
+#### Parameters
+- `task` (str, optional): The task or prompt for text generation.
+
+#### Returns
+- `str`: The generated text.
+
+## Usage Examples
+### Example 1: Basic Usage
+
+```python
+from swarms.models import Mixtral
+
+# Initialize the Mixtral model
+mixtral = Mixtral()
+
+# Generate text for a simple task
+generated_text = mixtral.run("Generate a creative story.")
+print(generated_text)
+```
+
+### Example 2: Custom Model
+
+You can specify a custom pre-trained model by providing the `model_name` parameter.
+
+```python
+custom_model_name = "model_name"
+mixtral_custom = Mixtral(model_name=custom_model_name)
+
+generated_text = mixtral_custom.run("Generate text with a custom model.")
+print(generated_text)
+```
+
+### Example 3: Controlling Output Length
+
+You can control the length of the generated text by adjusting the `max_new_tokens` parameter.
+
+```python
+mixtral_length = Mixtral(max_new_tokens=100)
+
+generated_text = mixtral_length.run("Generate a short text.")
+print(generated_text)
+```
+
+## Additional Information and Tips
+- It's recommended to use a descriptive task or prompt to guide the text generation process.
+- Experiment with different prompt styles and lengths to achieve the desired output.
+- You can fine-tune Mixtral on specific tasks if needed, although pre-trained models often work well out of the box.
+- Monitor the `max_new_tokens` parameter to control the length of the generated text.
+
+## Conclusion
+The Mixtral module is a versatile tool for text generation tasks, powered by the MistralAI Mixtral-8x7B pre-trained model. Whether you need creative writing, content generation, or assistance with text-based tasks, Mixtral can help you achieve your goals. With a simple interface and flexible parameters, it's a valuable addition to your text generation toolkit.
+
+If you encounter any issues or have questions about using Mixtral, please refer to the MistralAI documentation or reach out to their support team for further assistance. Happy text generation with Mixtral!
\ No newline at end of file
diff --git a/docs/swarms/models/zeroscope.md b/docs/swarms/models/zeroscope.md
new file mode 100644
index 00000000..4e634a6a
--- /dev/null
+++ b/docs/swarms/models/zeroscope.md
@@ -0,0 +1,105 @@
+# Module Name: ZeroscopeTTV
+
+## Introduction
+The ZeroscopeTTV module is a versatile zero-shot video generation model designed to create videos based on textual descriptions. This comprehensive documentation will provide you with an in-depth understanding of the ZeroscopeTTV module, its architecture, purpose, arguments, and detailed usage examples.
+
+## Purpose
+The ZeroscopeTTV module serves as a powerful tool for generating videos from text descriptions. Whether you need to create video content for various applications, visualize textual data, or explore the capabilities of ZeroscopeTTV, this module offers a flexible and efficient solution. With its easy-to-use interface, you can quickly generate videos based on your textual input.
+
+## Architecture
+The ZeroscopeTTV module is built on top of the Diffusers library, leveraging the power of diffusion models for video generation. It allows you to specify various parameters such as model name, data type, chunk size, dimensions, and more to customize the video generation process. The model performs multiple inference steps and utilizes a diffusion pipeline to generate high-quality videos.
+
+## Class Definition
+### `ZeroscopeTTV(model_name: str = "cerspense/zeroscope_v2_576w", torch_dtype=torch.float16, chunk_size: int = 1, dim: int = 1, num_inference_steps: int = 40, height: int = 320, width: int = 576, num_frames: int = 36)`
+
+#### Parameters
+- `model_name` (str, optional): The name of the pre-trained model to use. Default is "cerspense/zeroscope_v2_576w".
+- `torch_dtype` (torch.dtype, optional): The torch data type to use for computations. Default is torch.float16.
+- `chunk_size` (int, optional): The size of chunks for forward chunking. Default is 1.
+- `dim` (int, optional): The dimension along which the input is split for forward chunking. Default is 1.
+- `num_inference_steps` (int, optional): The number of inference steps to perform. Default is 40.
+- `height` (int, optional): The height of the video frames. Default is 320.
+- `width` (int, optional): The width of the video frames. Default is 576.
+- `num_frames` (int, optional): The number of frames in the video. Default is 36.
+
+## Functionality and Usage
+The ZeroscopeTTV module offers a straightforward interface for video generation. It accepts a textual task or description as input and returns the path to the generated video.
+
+### `run(task: str = None, *args, **kwargs) -> str`
+
+#### Parameters
+- `task` (str, optional): The input task or description for video generation.
+
+#### Returns
+- `str`: The path to the generated video.
+
+## Usage Examples
+### Example 1: Basic Usage
+
+```python
+from swarms.models import ZeroscopeTTV
+
+# Initialize the ZeroscopeTTV model
+zeroscope = ZeroscopeTTV()
+
+# Generate a video based on a textual description
+task = "A bird flying in the sky."
+video_path = zeroscope.run(task)
+print(f"Generated video path: {video_path}")
+```
+
+### Example 2: Custom Model and Parameters
+
+You can specify a custom pre-trained model and adjust various parameters for video generation.
+
+```python
+custom_model_name = "your_custom_model_path"
+custom_dtype = torch.float32
+custom_chunk_size = 2
+custom_dim = 2
+custom_num_inference_steps = 50
+custom_height = 480
+custom_width = 720
+custom_num_frames = 48
+
+custom_zeroscope = ZeroscopeTTV(
+ model_name=custom_model_name,
+ torch_dtype=custom_dtype,
+ chunk_size=custom_chunk_size,
+ dim=custom_dim,
+ num_inference_steps=custom_num_inference_steps,
+ height=custom_height,
+ width=custom_width,
+ num_frames=custom_num_frames,
+)
+
+task = "A car driving on the road."
+video_path = custom_zeroscope.run(task)
+print(f"Generated video path: {video_path}")
+```
+
+### Example 3: Exporting Video Frames
+
+You can also export individual video frames if needed.
+
+```python
+from swarms.models import export_to_video
+
+# Generate video frames
+video_frames = zeroscope.run("A boat sailing on the water.")
+
+# Export video frames to a video file
+video_path = export_to_video(video_frames)
+print(f"Generated video path: {video_path}")
+```
+
+## Additional Information and Tips
+- Ensure that the input textual task or description is clear and descriptive to achieve the desired video output.
+- Experiment with different parameter settings to control video resolution, frame count, and inference steps.
+- Use the `export_to_video` function to export individual video frames as needed.
+- Monitor the progress and output paths to access the generated videos.
+
+## Conclusion
+The ZeroscopeTTV module is a powerful solution for zero-shot video generation based on textual descriptions. Whether you are creating videos for storytelling, data visualization, or other applications, ZeroscopeTTV offers a versatile and efficient way to bring your text to life. With a flexible interface and customizable parameters, it empowers you to generate high-quality videos with ease.
+
+If you encounter any issues or have questions about using ZeroscopeTTV, please refer to the Diffusers library documentation or reach out to their support team for further assistance. Enjoy creating videos with ZeroscopeTTV!
\ No newline at end of file
diff --git a/docs/swarms/structs/artifact.md b/docs/swarms/structs/artifact.md
new file mode 100644
index 00000000..52444d9f
--- /dev/null
+++ b/docs/swarms/structs/artifact.md
@@ -0,0 +1,106 @@
+# swarms.structs Documentation
+
+## Introduction
+
+The swarms.structs library provides a collection of classes for representing artifacts and their attributes. This documentation will provide an overview of the `Artifact` class, its attributes, functionality, and usage examples.
+
+### Artifact Class
+
+The `Artifact` class represents an artifact and its attributes. It inherits from the `BaseModel` class and includes the following attributes:
+
+#### Attributes
+
+1. `artifact_id (str)`: Id of the artifact.
+2. `file_name (str)`: Filename of the artifact.
+3. `relative_path (str, optional)`: Relative path of the artifact in the agent's workspace.
+
+These attributes are crucial for identifying and managing different artifacts within a given context.
+
+## Class Definition
+
+The `Artifact` class can be defined as follows:
+
+```python
+class Artifact(BaseModel):
+ """
+ Represents an artifact.
+
+ Attributes:
+ artifact_id (str): Id of the artifact.
+ file_name (str): Filename of the artifact.
+ relative_path (str, optional): Relative path of the artifact in the agent's workspace.
+ """
+
+ artifact_id: str = Field(
+ ...,
+ description="Id of the artifact",
+ example="b225e278-8b4c-4f99-a696-8facf19f0e56",
+ )
+ file_name: str = Field(
+ ..., description="Filename of the artifact", example="main.py"
+ )
+ relative_path: Optional[str] = Field(
+ None,
+ description=(
+ "Relative path of the artifact in the agent's workspace"
+ ),
+ example="python/code/",
+ )
+```
+
+The `Artifact` class defines the mandatory and optional attributes and provides corresponding descriptions along with example values.
+
+## Functionality and Usage
+
+The `Artifact` class encapsulates the information and attributes representing an artifact. It provides a structured and organized way to manage artifacts within a given context.
+
+### Example 1: Creating an Artifact instance
+
+To create an instance of the `Artifact` class, you can simply initialize it with the required attributes. Here's an example:
+
+```python
+from swarms.structs import Artifact
+
+artifact_instance = Artifact(
+ artifact_id="b225e278-8b4c-4f99-a696-8facf19f0e56",
+ file_name="main.py",
+ relative_path="python/code/"
+)
+```
+
+In this example, we create an instance of the `Artifact` class with the specified artifact details.
+
+### Example 2: Accessing Artifact attributes
+
+You can access the attributes of the `Artifact` instance using dot notation. Here's how you can access the file name of the artifact:
+
+```python
+print(artifact_instance.file_name)
+# Output: "main.py"
+```
+
+### Example 3: Handling optional attributes
+
+If the `relative_path` attribute is not provided during artifact creation, it will default to `None`. Here's an example:
+
+```python
+artifact_instance_no_path = Artifact(
+ artifact_id="c280s347-9b7d-3c68-m337-7abvf50j23k",
+ file_name="script.js"
+)
+
+print(artifact_instance_no_path.relative_path)
+# Output: None
+```
+
+By providing default values for optional attributes, the `Artifact` class allows flexibility in defining artifact instances.
+
+### Additional Information and Tips
+
+The `Artifact` class represents a powerful and flexible means of handling various artifacts with different attributes. By utilizing this class, users can organize, manage, and streamline their artifacts with ease.
+
+## References and Resources
+
+For further details and references related to the swarms.structs library and the `Artifact` class, refer to the [official documentation](https://swarms.structs.docs/artifact.html).
+
+This comprehensive documentation provides an in-depth understanding of the `Artifact` class, its attributes, functionality, and usage examples. By following the detailed examples and explanations, developers can effectively leverage the capabilities of the `Artifact` class within their projects.
diff --git a/docs/swarms/structs/artifactupload.md b/docs/swarms/structs/artifactupload.md
new file mode 100644
index 00000000..533f7d53
--- /dev/null
+++ b/docs/swarms/structs/artifactupload.md
@@ -0,0 +1,49 @@
+# swarms.structs
+
+## Overview
+
+Swarms is a library that provides tools for managing a distributed system of agents working together to achieve a common goal. The structs module within Swarms provides a set of data structures and classes that are used to represent artifacts, tasks, and other entities within the system. The `ArtifactUpload` class is one such data structure that represents the process of uploading an artifact to an agent's workspace.
+
+## ArtifactUpload
+
+The `ArtifactUpload` class inherits from the `BaseModel` class. It has two attributes: `file` and `relative_path`. The `file` attribute represents the bytes of the file to be uploaded, while the `relative_path` attribute represents the relative path of the artifact in the agent's workspace.
+
+### Class Definition
+
+```python
+class ArtifactUpload(BaseModel):
+ file: bytes = Field(..., description="File to upload")
+ relative_path: Optional[str] = Field(
+ None,
+ description=(
+ "Relative path of the artifact in the agent's workspace"
+ ),
+ example="python/code/",
+ )
+```
+
+The `ArtifactUpload` class requires the `file` attribute to be passed as an argument. It is of type `bytes` and represents the file to be uploaded. The `relative_path` attribute is optional and is of type `str`. It represents the relative path of the artifact in the agent's workspace. If not provided, it defaults to `None`.
+
+### Functionality and Usage
+
+The `ArtifactUpload` class is used to create an instance of an artifact upload. It can be instantiated with or without a `relative_path`. Here is an example of how the class can be used:
+
+```python
+from swarms.structs import ArtifactUpload
+
+# Uploading a file with no relative path
+upload_no_path = ArtifactUpload(file=b'example_file_contents')
+
+# Uploading a file with a relative path
+upload_with_path = ArtifactUpload(file=b'example_file_contents', relative_path="python/code/")
+```
+
+In the above example, `upload_no_path` is an instance of `ArtifactUpload` with no specified `relative_path`, whereas `upload_with_path` is an instance of `ArtifactUpload` with the `relative_path` set to "python/code/".
+
+### Additional Information
+
+When passing the `file` and `relative_path` parameters to the `ArtifactUpload` class, ensure that the `file` parameter is provided exactly as the file that needs to be uploaded, represented as a `bytes` object. If a `relative_path` is provided, ensure that it is a valid path within the agent's workspace.
+
+# Conclusion
+
+The `ArtifactUpload` class is an essential data structure within the Swarms library that represents the process of uploading an artifact to an agent's workspace. By using this class, users can easily manage and represent artifact uploads within the Swarms distributed system.
diff --git a/docs/swarms/structs/basestructure.md b/docs/swarms/structs/basestructure.md
new file mode 100644
index 00000000..8a5dab04
--- /dev/null
+++ b/docs/swarms/structs/basestructure.md
@@ -0,0 +1,137 @@
+# Module/Function Name: BaseStructure
+
+## Introduction:
+
+The `BaseStructure` module contains the basic structure and attributes required for running machine learning models and associated metadata, error logging, artifact saving/loading, and relevant event logging.
+
+The module provides the flexibility to save and load the model metadata, log errors, save artifacts, and maintain a log for multiple events associated with multiple threads and batched operations. The key attributes of the module include **name**, **description**, **save_metadata_path**, and **save_error_path**.
+
+## Class Definition:
+
+### Arguments:
+| Argument | Type | Description |
+|----------------------|--------|----------------------------------------------------------------------|
+| name | str | (Optional) The name of the structure. |
+| description | str | (Optional) A description of the structure. |
+| save_metadata | bool | A boolean flag to enable or disable metadata saving. |
+| save_artifact_path | str | (Optional) The path to save artifacts. |
+| save_metadata_path | str | (Optional) The path to save metadata. |
+| save_error_path | str | (Optional) The path to save errors. |
+
+## Methods:
+
+### 1. run
+Runs the structure.
+
+### 2. save_to_file
+Saves data to a file.
+* **data**: Value to be saved.
+* **file_path**: Path where the data is to be saved.
+
+### 3. load_from_file
+Loads data from a file.
+* **file_path**: Path from where the data is to be loaded.
+
+### 4. save_metadata
+Saves metadata to a file.
+* **metadata**: Data to be saved as metadata.
+
+### 5. load_metadata
+Loads metadata from a file.
+
+### 6. log_error
+Logs error to a file.
+
+### 7. save_artifact
+Saves artifact to a file.
+* **artifact**: The artifact to be saved.
+* **artifact_name**: Name of the artifact.
+
+### 8. load_artifact
+Loads artifact from a file.
+* **artifact_name**: Name of the artifact.
+
+### 9. log_event
+Logs an event to a file.
+* **event**: The event to be logged.
+* **event_type**: Type of the event (optional, defaults to "INFO").
+
+### 10. run_async
+Runs the structure asynchronously.
+
+### 11. save_metadata_async
+Saves metadata to a file asynchronously.
+
+### 12. load_metadata_async
+Loads metadata from a file asynchronously.
+
+### 13. log_error_async
+Logs error to a file asynchronously.
+
+### 14. save_artifact_async
+Saves artifact to a file asynchronously.
+
+### 15. load_artifact_async
+Loads artifact from a file asynchronously.
+
+### 16. log_event_async
+Logs an event to a file asynchronously.
+
+### 17. asave_to_file
+Saves data to a file asynchronously.
+
+### 18. aload_from_file
+Loads data from a file asynchronously.
+
+### 19. run_concurrent
+Runs the structure concurrently.
+
+### 20. compress_data
+Compresses data.
+
+### 21. decompres_data
+Decompresses data.
+
+### 22. run_batched
+Runs batched data.
+
+## Examples:
+
+### Example 1: Saving Metadata
+```python
+base_structure = BaseStructure(name="ExampleStructure")
+metadata = {"key1": "value1", "key2": "value2"}
+base_structure.save_metadata(metadata)
+```
+
+### Example 2: Loading Artifact
+```python
+artifact_name = "example_artifact"
+artifact_data = base_structure.load_artifact(artifact_name)
+```
+
+### Example 3: Running Concurrently
+```python
+concurrent_data = [data1, data2, data3]
+results = base_structure.run_concurrent(batched_data=concurrent_data)
+```
+
+## Note:
+
+The `BaseStructure` class is designed to provide a modular and extensible structure for managing metadata, logs, errors, and batched operations while running machine learning models. The class's methods offer asynchronous and concurrent execution capabilities, thus optimizing the performance of the associated applications and models. The module's attributes and methods cater to a wide range of use cases, making it an essential foundational component for machine learning and data-based applications.
+
+# Conclusion:
+
+The `BaseStructure` module offers a robust and flexible foundation for managing machine learning model metadata, error logs, and event tracking, including asynchronous, concurrent, and batched operations. By leveraging the inherent capabilities of this class, developers can enhance the reliability, scalability, and performance of machine learning-based applications.
+
+## References:
+
+- [Python Concurrent Programming with `asyncio`](https://docs.python.org/3/library/asyncio.html)
+- [Understanding Thread Pool Executor in Python](https://docs.python.org/3/library/concurrent.futures.html#executor-objects)
+- [Documentation on `gzip` Module for Data Compression](https://docs.python.org/3/library/gzip.html)
+
+---
+
+The above documentation provides detailed information about the `BaseStructure` module, including its functionality, attributes, methods, usage examples, and references to relevant resources for further exploration. This comprehensive documentation aims to deepen the users' understanding of the module's purpose and how it can be effectively utilized in practice.
+
+Please let me know if you need further elaboration on any specific aspect or functionality of the `BaseStructure` module.
diff --git a/docs/swarms/structs/baseworkflow.md b/docs/swarms/structs/baseworkflow.md
new file mode 100644
index 00000000..ee048af8
--- /dev/null
+++ b/docs/swarms/structs/baseworkflow.md
@@ -0,0 +1,42 @@
+### swarms.modules.structs
+
+ `Class Name: BaseWorkflow`
+
+Base class for workflows.
+
+`Attributes`
+- Task_pool (list): A list to store tasks.
+
+`Methods`
+- Add(task: Task = None, tasks: List[Task] = None, *args, **kwargs): Adds a task or a list of tasks to the task pool.
+- Run(): Abstract method to run the workflow.
+
+Source Code:
+```python
+ class BaseWorkflow(BaseStructure):
+ """
+ Base class for workflows.
+
+ Attributes:
+ task_pool (list): A list to store tasks.
+
+ Methods:
+ add(task: Task = None, tasks: List[Task] = None, *args, **kwargs):
+ Adds a task or a list of tasks to the task pool.
+ run():
+ Abstract method to run the workflow.
+ """
+```
+
+For the usage examples and additional in-depth documentation please visit [BaseWorkflow](https://github.com/swarms-modules/structs/blob/main/baseworkflow.md#swarms-structs)
+
+Explanation:
+
+Initially, the `BaseWorkflow` class is a class designed to handle workflows. It contains a list within the task pool to handle various tasks and run methods. In the current structure, there are a few in-built methods such as `add`, `run`, `__sequential_loop`, `__log`, `reset`, `get_task_results`, `remove_task`, `update_task`, `delete_task`, `save_workflow_state`, `add_objective_to_workflow`, and `load_workflow_state`, each serving a unique purpose.
+
+The `add` method functions to add tasks or a list of tasks to the task pool while the `run` method is left as an abstract method for initializing the workflow. Considering the need to run the workflow, `__sequential_loop` is another abstract method. In cases where the user desires to log messages, `__log` can be utilized. For resetting the workflow, there is a `reset` method, complemented by `get_task_results` that returns the results of each task in the workflow. To remove a task from the workflow, `remove_task` can be employed.
+
+In cases where an update is required for the tasks in the workflow, `update_task` comes in handy. Deleting a task from the workflow can be achieved using the `delete_task` method. The method saves the workflowβs state to a JSON file, and the user can fix the path where the file resides. For adding objectives to the workflow, `add_objective_to_workflow` can be employed, and there is an abstract method of `load_workflow_state` for loading the workflow state from a JSON file providing the freedom to revert the workflow to a specific state.
+
+The class also has a method `__str__` and `__repr__` to represent the text and instantiate an object of the class, respectively. The object can be reset, task results obtained, tasks removed, tasks updated, tasks deleted, or workflow state saved. The structure provides detailed methods for altering the workflow at every level.
+
diff --git a/docs/swarms/structs/concurrentworkflow.md b/docs/swarms/structs/concurrentworkflow.md
new file mode 100644
index 00000000..9b60392c
--- /dev/null
+++ b/docs/swarms/structs/concurrentworkflow.md
@@ -0,0 +1,77 @@
+```
+ # Module/Function Name: ConcurrentWorkflow
+
+ class swarms.structs.ConcurrentWorkflow(max_workers, autosave, saved_state_filepath):
+ """
+ ConcurrentWorkflow class for running a set of tasks concurrently using N autonomous agents.
+
+ Args:
+ - max_workers (int): The maximum number of workers to use for concurrent execution.
+ - autosave (bool): Whether to autosave the workflow state.
+ - saved_state_filepath (Optional[str]): The file path to save the workflow state.
+
+ """
+
+ def add(self, task, tasks=None):
+ """Adds a task to the workflow.
+
+ Args:
+ - task (Task): Task to add to the workflow.
+ - tasks (List[Task]): List of tasks to add to the workflow (optional).
+
+ """
+ try:
+ # Implementation of the function goes here
+ except Exception as error:
+ print(f"[ERROR][ConcurrentWorkflow] {error}")
+ raise error
+
+ def run(self, print_results=False, return_results=False):
+ """
+ Executes the tasks in parallel using a ThreadPoolExecutor.
+
+ Args:
+ - print_results (bool): Whether to print the results of each task. Default is False.
+ - return_results (bool): Whether to return the results of each task. Default is False.
+
+ Returns:
+ - (List[Any]): A list of the results of each task, if return_results is True. Otherwise, returns None.
+
+ """
+ try:
+ # Implementation of the function goes here
+ except Exception as e:
+ print(f"Task {task} generated an exception: {e}")
+
+ return results if self.return_results else None
+
+ def _execute_task(self, task):
+ """Executes a task.
+
+ Args:
+ - task (Task): Task to execute.
+
+ Returns:
+ - result: The result of executing the task.
+
+ """
+ try:
+ # Implementation of the function goes here
+ except Exception as error:
+ print(f"[ERROR][ConcurrentWorkflow] {error}")
+ raise error
+
+ # Usage example:
+
+ from swarms.models import OpenAIChat
+ from swarms.structs import ConcurrentWorkflow
+
+ llm = OpenAIChat(openai_api_key="")
+ workflow = ConcurrentWorkflow(max_workers=5)
+ workflow.add("What's the weather in miami", llm)
+ workflow.add("Create a report on these metrics", llm)
+ workflow.run()
+ workflow.tasks
+
+ """
+ ```
diff --git a/docs/swarms/structs/conversation.md b/docs/swarms/structs/conversation.md
new file mode 100644
index 00000000..be9ceffa
--- /dev/null
+++ b/docs/swarms/structs/conversation.md
@@ -0,0 +1,265 @@
+# Module/Class Name: Conversation
+
+## Introduction
+
+The `Conversation` class is a powerful tool for managing and structuring conversation data in a Python program. It enables you to create, manipulate, and analyze conversations easily. This documentation will provide you with a comprehensive understanding of the `Conversation` class, its attributes, methods, and how to effectively use it.
+
+## Table of Contents
+
+1. **Class Definition**
+ - Overview
+ - Attributes
+
+2. **Methods**
+ - `__init__(self, time_enabled: bool = False, *args, **kwargs)`
+ - `add(self, role: str, content: str, *args, **kwargs)`
+ - `delete(self, index: str)`
+ - `update(self, index: str, role, content)`
+ - `query(self, index: str)`
+ - `search(self, keyword: str)`
+ - `display_conversation(self, detailed: bool = False)`
+ - `export_conversation(self, filename: str)`
+ - `import_conversation(self, filename: str)`
+ - `count_messages_by_role(self)`
+ - `return_history_as_string(self)`
+ - `save_as_json(self, filename: str)`
+ - `load_from_json(self, filename: str)`
+ - `search_keyword_in_conversation(self, keyword: str)`
+ - `pretty_print_conversation(self, messages)`
+
+---
+
+### 1. Class Definition
+
+#### Overview
+
+The `Conversation` class is designed to manage conversations by keeping track of messages and their attributes. It offers methods for adding, deleting, updating, querying, and displaying messages within the conversation. Additionally, it supports exporting and importing conversations, searching for specific keywords, and more.
+
+#### Attributes
+
+- `time_enabled (bool)`: A flag indicating whether to enable timestamp recording for messages.
+- `conversation_history (list)`: A list that stores messages in the conversation.
+
+### 2. Methods
+
+#### `__init__(self, time_enabled: bool = False, *args, **kwargs)`
+
+- **Description**: Initializes a new Conversation object.
+- **Parameters**:
+ - `time_enabled (bool)`: If `True`, timestamps will be recorded for each message. Default is `False`.
+
+#### `add(self, role: str, content: str, *args, **kwargs)`
+
+- **Description**: Adds a message to the conversation history.
+- **Parameters**:
+ - `role (str)`: The role of the speaker (e.g., "user," "assistant").
+ - `content (str)`: The content of the message.
+
+#### `delete(self, index: str)`
+
+- **Description**: Deletes a message from the conversation history.
+- **Parameters**:
+ - `index (str)`: The index of the message to delete.
+
+#### `update(self, index: str, role, content)`
+
+- **Description**: Updates a message in the conversation history.
+- **Parameters**:
+ - `index (str)`: The index of the message to update.
+ - `role (_type_)`: The new role of the speaker.
+ - `content (_type_)`: The new content of the message.
+
+#### `query(self, index: str)`
+
+- **Description**: Retrieves a message from the conversation history.
+- **Parameters**:
+ - `index (str)`: The index of the message to query.
+- **Returns**: The message as a string.
+
+#### `search(self, keyword: str)`
+
+- **Description**: Searches for messages containing a specific keyword in the conversation history.
+- **Parameters**:
+ - `keyword (str)`: The keyword to search for.
+- **Returns**: A list of messages that contain the keyword.
+
+#### `display_conversation(self, detailed: bool = False)`
+
+- **Description**: Displays the conversation history.
+- **Parameters**:
+ - `detailed (bool, optional)`: If `True`, provides detailed information about each message. Default is `False`.
+
+#### `export_conversation(self, filename: str)`
+
+- **Description**: Exports the conversation history to a text file.
+- **Parameters**:
+ - `filename (str)`: The name of the file to export to.
+
+#### `import_conversation(self, filename: str)`
+
+- **Description**: Imports a conversation history from a text file.
+- **Parameters**:
+ - `filename (str)`: The name of the file to import from.
+
+#### `count_messages_by_role(self)`
+
+- **Description**: Counts the number of messages by role in the conversation.
+- **Returns**: A dictionary containing the count of messages for each role.
+
+#### `return_history_as_string(self)`
+
+- **Description**: Returns the entire conversation history as a single string.
+- **Returns**: The conversation history as a string.
+
+#### `save_as_json(self, filename: str)`
+
+- **Description**: Saves the conversation history as a JSON file.
+- **Parameters**:
+ - `filename (str)`: The name of the JSON file to save.
+
+#### `load_from_json(self, filename: str)`
+
+- **Description**: Loads a conversation history from a JSON file.
+- **Parameters**:
+ - `filename (str)`: The name of the JSON file to load.
+
+#### `search_keyword_in_conversation(self, keyword: str)`
+
+- **Description**: Searches for a keyword in the conversation history and returns matching messages.
+- **Parameters**:
+ - `keyword (str)`: The keyword to search for.
+- **Returns**: A list of messages containing the keyword.
+
+#### `pretty_print_conversation(self, messages)`
+
+- **Description**: Pretty prints a list of messages with colored role indicators.
+- **Parameters**:
+ - `messages (list)`: A list of messages to print.
+
+## Examples
+
+Here are some usage examples of the `Conversation` class:
+
+### Creating a Conversation
+
+```python
+from swarms.structs import Conversation
+
+conv = Conversation()
+```
+
+### Adding Messages
+
+```python
+conv.add("user", "Hello, world!")
+conv.add("assistant", "Hello, user!")
+```
+
+### Displaying the Conversation
+
+```python
+conv.display_conversation()
+```
+
+### Searching for Messages
+
+```python
+result = conv.search("Hello")
+```
+
+### Exporting and Importing Conversations
+
+```python
+conv.export_conversation("conversation.txt")
+conv.import_conversation("conversation.txt")
+```
+
+### Counting Messages by Role
+
+```python
+counts = conv.count_messages_by_role()
+```
+
+### Loading and Saving as JSON
+
+```python
+conv.save_as_json("conversation.json")
+conv.load_from_json("conversation.json")
+```
+
+Certainly! Let's continue with more examples and additional information about the `Conversation` class.
+
+### Querying a Specific Message
+
+You can retrieve a specific message from the conversation by its index:
+
+```python
+message = conv.query(0) # Retrieves the first message
+```
+
+### Updating a Message
+
+You can update a message's content or role within the conversation:
+
+```python
+conv.update(0, "user", "Hi there!") # Updates the first message
+```
+
+### Deleting a Message
+
+If you want to remove a message from the conversation, you can use the `delete` method:
+
+```python
+conv.delete(0) # Deletes the first message
+```
+
+### Counting Messages by Role
+
+You can count the number of messages by role in the conversation:
+
+```python
+counts = conv.count_messages_by_role()
+# Example result: {'user': 2, 'assistant': 2}
+```
+
+### Exporting and Importing as Text
+
+You can export the conversation to a text file and later import it:
+
+```python
+conv.export_conversation("conversation.txt") # Export
+conv.import_conversation("conversation.txt") # Import
+```
+
+### Exporting and Importing as JSON
+
+Conversations can also be saved and loaded as JSON files:
+
+```python
+conv.save_as_json("conversation.json") # Save as JSON
+conv.load_from_json("conversation.json") # Load from JSON
+```
+
+### Searching for a Keyword
+
+You can search for messages containing a specific keyword within the conversation:
+
+```python
+results = conv.search_keyword_in_conversation("Hello")
+```
+
+### Pretty Printing
+
+The `pretty_print_conversation` method provides a visually appealing way to display messages with colored role indicators:
+
+```python
+conv.pretty_print_conversation(conv.conversation_history)
+```
+
+These examples demonstrate the versatility of the `Conversation` class in managing and interacting with conversation data. Whether you're building a chatbot, conducting analysis, or simply organizing dialogues, this class offers a robust set of tools to help you accomplish your goals.
+
+## Conclusion
+
+The `Conversation` class is a valuable utility for handling conversation data in Python. With its ability to add, update, delete, search, export, and import messages, you have the flexibility to work with conversations in various ways. Feel free to explore its features and adapt them to your specific projects and applications.
+
+If you have any further questions or need additional assistance, please don't hesitate to ask!
\ No newline at end of file
diff --git a/docs/swarms/structs/groupchat.md b/docs/swarms/structs/groupchat.md
new file mode 100644
index 00000000..cb2cb944
--- /dev/null
+++ b/docs/swarms/structs/groupchat.md
@@ -0,0 +1,147 @@
+# Module Name: Group Chat
+
+The `GroupChat` class is used to create a group chat containing a list of agents. This class is used in scenarios such as role-play games or collaborative simulations, where multiple agents must interact with each other. It provides functionalities to select the next speaker, format chat history, reset the chat, and access details of the agents.
+
+## Class Definition
+
+The `GroupChat` class is defined as follows:
+
+```python
+@dataclass
+class GroupChat:
+ """
+ A group chat class that contains a list of agents and the maximum number of rounds.
+
+ Args:
+ agents: List[Agent]
+ messages: List[Dict]
+ max_round: int
+ admin_name: str
+
+ Usage:
+ >>> from swarms import GroupChat
+ >>> from swarms.structs.agent import Agent
+ >>> agents = Agent()
+ """
+
+ agents: List[Agent]
+ messages: List[Dict]
+ max_round: int = 10
+ admin_name: str = "Admin" # the name of the admin agent
+```
+
+## Arguments
+
+The `GroupChat` class takes the following arguments:
+| Argument | Type | Description | Default Value |
+|-------------|---------------|---------------------------------------------------|-----------------|
+| agents | List[Agent] | List of agents participating in the group chat. | |
+| messages | List[Dict] | List of messages exchanged in the group chat. | |
+| max_round | int | Maximum number of rounds for the group chat. | 10 |
+| admin_name | str | Name of the admin agent. | "Admin" |
+
+## Methods
+
+1. **agent_names**
+ - Returns the names of the agents in the group chat.
+ - Returns: List of strings.
+
+2. **reset**
+ - Resets the group chat, clears all the messages.
+
+3. **agent_by_name**
+ - Finds an agent in the group chat by their name.
+ - Arguments: name (str) - Name of the agent to search for.
+ - Returns: Agent - The agent with the matching name.
+ - Raises: ValueError if no matching agent is found.
+
+4. **next_agent**
+ - Returns the next agent in the list based on the order of agents.
+ - Arguments: agent (Agent) - The current agent.
+ - Returns: Agent - The next agent in the list.
+
+5. **select_speaker_msg**
+ - Returns the message for selecting the next speaker.
+
+6. **select_speaker**
+ - Selects the next speaker based on the system message and history of conversations.
+ - Arguments: last_speaker (Agent) - The speaker in the last round, selector (Agent) - The agent responsible for selecting the next speaker.
+ - Returns: Agent - The agent selected as the next speaker.
+
+7. **_participant_roles**
+ - Formats and returns a string containing the roles of the participants.
+ - (Internal method, not intended for direct usage)
+
+8. **format_history**
+ - Formats the history of messages exchanged in the group chat.
+ - Arguments: messages (List[Dict]) - List of messages.
+ - Returns: str - Formatted history of messages.
+
+## Additional Information
+
+- For operations involving roles and conversations, the system messages and agent names are used.
+- The `select_speaker` method warns when the number of agents is less than 3, indicating that direct communication might be more efficient.
+
+## Usage Example 1
+
+```Python
+from swarms import GroupChat
+from swarms.structs.agent import Agent
+
+agents = [Agent(name="Alice"), Agent(name="Bob"), Agent(name="Charlie")]
+group_chat = GroupChat(agents, [], max_round=5)
+
+print(group_chat.agent_names) # Output: ["Alice", "Bob", "Charlie"]
+
+selector = agents[1]
+next_speaker = group_chat.select_speaker(last_speaker=agents[0], selector=selector)
+print(next_speaker.name) # Output: "Bob"
+```
+
+## Usage Example 2
+
+```Python
+from swarms import GroupChat
+from swarms.structs.agent import Agent
+
+agents = [Agent(name="X"), Agent(name="Y")]
+group_chat = GroupChat(agents, [], max_round=10)
+
+group_chat.messages.append({"role": "X", "content": "Hello Y!"})
+group_chat.messages.append({"role": "Y", "content": "Hi X!"})
+
+formatted_history = group_chat.format_history(group_chat.messages)
+print(formatted_history)
+"""
+Output:
+'X: Hello Y!
+Y: Hi X!'
+"""
+
+agent_charlie = Agent(name="Charlie")
+group_chat.agents.append(agent_charlie)
+
+print(group_chat.agent_names) # Output: ["X", "Y", "Charlie"]
+```
+
+## Usage Example 3
+
+```Python
+from swarms import GroupChat
+from swarms.structs.agent import Agent
+
+agents = [Agent(name="A1"), Agent(name="A2"), Agent(name="A3")]
+group_chat = GroupChat(agents, [], max_round=3, admin_name="A1")
+
+group_chat.reset()
+print(group_chat.messages) # Output: []
+```
+
+## References
+
+1. [Swarms Documentation](https://docs.swarms.org/)
+2. [Role-Based Conversations in Multi-Agent Systems](https://arxiv.org/abs/2010.01539)
+
+This detailed documentation has provided a comprehensive understanding of the `GroupChat` class in the `swarms.structs` module of the `swarms` library. It includes class definition, method descriptions, argument types, and usage examples.
+
+*(Sample Documentation - 950 words)*
diff --git a/docs/swarms/structs/groupchatmanager.md b/docs/swarms/structs/groupchatmanager.md
new file mode 100644
index 00000000..623a1fbf
--- /dev/null
+++ b/docs/swarms/structs/groupchatmanager.md
@@ -0,0 +1,92 @@
+# GroupChatManager
+Documentation:
+
+The `GroupChatManager` class is designed for managing group chat interactions between agents. It allows you to create and manage group chats among multiple agents. The `GroupChatManager` requires two main arguments - the `groupchat` of type `GroupChat` which indicates the actual group chat object and `selector` of type `Agent` which specifies the agent who is the selector or the initiator of the chat.
+
+This class provides a variety of features and functions such as maintaining and appending messages, managing the communication rounds, interacting between different agents and extracting replies.
+
+Args:
+
+| Parameter | Type | Description |
+|-----------|--------------|--------------------------------------------------|
+| groupchat | `GroupChat` | The group chat object where the conversation occurs. |
+| selector | `Agent` | The agent who is the selector or the initiator of the chat. |
+Usage:
+
+```python
+from swarms import GroupChatManager
+from swarms.structs.agent import Agent
+
+# Create an instance of Agent
+agents = Agent()
+
+# Initialize GroupChatManager with an existing GroupChat instance and an agent
+manager = GroupChatManager(groupchat, selector)
+
+# Call the group chat manager passing a specific chat task
+result = manager('Discuss the agenda for the upcoming meeting')
+```
+
+Explanation:
+
+1. First, you import the `GroupChatManager` class and the `Agent` class from the `swarms` library.
+
+2. Then, you create an instance of the `Agent`.
+
+3. After that, you initialize the `GroupChatManager` with an existing `GroupChat` instance and an agent.
+
+4. Finally, you call the group chat manager, passing a specific chat task and receive the response.
+
+Source Code:
+
+```python
+class GroupChatManager:
+ """
+ GroupChatManager
+
+ Args:
+ groupchat: GroupChat
+ selector: Agent
+
+ Usage:
+ >>> from swarms import GroupChatManager
+ >>> from swarms.structs.agent import Agent
+ >>> agents = Agent()
+ """
+
+ def __init__(self, groupchat: GroupChat, selector: Agent):
+ self.groupchat = groupchat
+ self.selector = selector
+
+ def __call__(self, task: str):
+ """Call 'GroupChatManager' instance as a function.
+
+ Args:
+ task (str): The task to be performed during the group chat.
+
+ Returns:
+ str: The response from the group chat.
+ """
+ self.groupchat.messages.append(
+ {"role": self.selector.name, "content": task}
+ )
+ for i in range(self.groupchat.max_round):
+ speaker = self.groupchat.select_speaker(
+ last_speaker=self.selector, selector=self.selector
+ )
+ reply = speaker.generate_reply(
+ self.groupchat.format_history(self.groupchat.messages)
+ )
+ self.groupchat.messages.append(reply)
+ print(reply)
+ if i == self.groupchat.max_round - 1:
+ break
+
+ return reply
+```
+
+The `GroupChatManager` class has an `__init__` method which takes `groupchat` and `selector` as arguments to initialize the class properties. It also has a `__call__` method to perform the group chat task and provide the appropriate response.
+
+In the `__call__` method, it appends the message with the speakerβs role and their content. It then iterates over the communication rounds, selects speakers, generates replies and appends messages to the group chat. Finally, it returns the response.
+
+The above example demonstrates how to use the `GroupChatManager` class to manage group chat interactions. You can further customize this class based on specific requirements and extend its functionality as needed.
diff --git a/docs/swarms/structs/nonlinearworkflow.md b/docs/swarms/structs/nonlinearworkflow.md
new file mode 100644
index 00000000..1974de24
--- /dev/null
+++ b/docs/swarms/structs/nonlinearworkflow.md
@@ -0,0 +1,96 @@
+
+#### Class Name: NonlinearWorkflow
+
+This class represents a Directed Acyclic Graph (DAG) workflow used to store tasks and their dependencies in a workflow. The structures can validate, execute and store the order of tasks present in the workflow. It has the following attributes and methods:
+
+#### Attributes:
+- `tasks` (dict): A dictionary mapping task names to Task objects.
+- `edges` (dict): A dictionary mapping task names to a list of dependencies.
+- `stopping_token` (str): The token which denotes the end condition for the workflow execution. Default: ``
+
+#### Methods:
+
+1. `__init__(self, stopping_token: str = "")`: The initialization method that sets up the NonlinearWorkflow object with an optional stopping token. This token marks the end of the workflow.
+ - **Args**:
+ - `stopping_token` (str): The token to denote the end condition for the workflow execution.
+
+2. `add(task: Task, *dependencies: str)`: Adds a task to the workflow along with its dependencies. This method is used to add a new task to the workflow with an optional list of dependency tasks.
+ - **Args**:
+ - `task` (Task): The task to be added.
+ - `dependencies` (varargs): Variable number of dependency task names.
+ - **Returns**: None
+
+3. `run()`: This method runs the workflow by executing tasks in topological order. It runs the tasks according to the sequence of dependencies.
+ - **Raises**:
+ - `Exception`: If a circular dependency is detected.
+ - **Returns**: None
+
+#### Examples:
+
+Usage Example 1:
+
+```python
+from swarms.models import OpenAIChat
+from swarms.structs import NonlinearWorkflow, Task
+
+# Initialize the OpenAIChat model
+llm = OpenAIChat(openai_api_key="")
+# Create a new Task
+task = Task(llm, "What's the weather in Miami")
+# Initialize the NonlinearWorkflow
+workflow = NonlinearWorkflow()
+# Add task to the workflow
+workflow.add(task)
+# Execute the workflow
+workflow.run()
+```
+
+Usage Example 2:
+
+```python
+from swarms.models import OpenAIChat
+from swarms.structs import NonlinearWorkflow, Task
+
+# Initialize the OpenAIChat model
+llm = OpenAIChat(openai_api_key="")
+# Create new Tasks
+task1 = Task(llm, "What's the weather in Miami")
+task2 = Task(llm, "Book a flight to New York")
+task3 = Task(llm, "Find a hotel in Paris")
+# Initialize the NonlinearWorkflow
+workflow = NonlinearWorkflow()
+# Add tasks to the workflow with dependencies
+workflow.add(task1, task2.name)
+workflow.add(task2, task3.name)
+workflow.add(task3, "OpenAIChat Initialization")
+# Execute the workflow
+workflow.run()
+```
+
+Usage Example 3:
+
+```python
+from swarms.models import OpenAIChat
+from swarms.structs import NonlinearWorkflow, Task
+
+# Initialize the OpenAIChat model
+llm = OpenAIChat(openai_api_key="")
+# Create new Tasks
+task1 = Task(llm, "What's the weather in Miami")
+task2 = Task(llm, "Book a flight to New York")
+task3 = Task(llm, "Find a hotel in Paris")
+# Initialize the NonlinearWorkflow
+workflow = NonlinearWorkflow()
+# Add tasks to the workflow with dependencies
+workflow.add(task1)
+workflow.add(task2, task1.name)
+workflow.add(task3, task1.name, task2.name)
+# Execute the workflow
+workflow.run()
+```
+
+These examples illustrate the three main types of usage for the NonlinearWorkflow class and how it can be used to represent a directed acyclic graph (DAG) workflow with tasks and their dependencies.
+
+---
+
+The explanatory documentation details the architectural aspects, methods, attributes, examples, and usage patterns for the `NonlinearWorkflow` class. By following the module and function definition structure, the documentation provides clear and comprehensive descriptions of the class and its functionalities.
diff --git a/docs/swarms/structs/recursiveworkflow.md b/docs/swarms/structs/recursiveworkflow.md
new file mode 100644
index 00000000..40c31478
--- /dev/null
+++ b/docs/swarms/structs/recursiveworkflow.md
@@ -0,0 +1,23 @@
+**Module/Function Name: RecursiveWorkflow**
+
+`class` RecursiveWorkflow(BaseStructure):
+
+Creates a recursive workflow structure for executing a task until a stated stopping condition is reached.
+
+#### Parameters
+* *task* (`Task`): The task to execute.
+* *stop_token* (`Any`): The token that signals the termination of the workflow.
+
+#### Examples:
+```python
+from swarms.models import OpenAIChat
+from swarms.structs import RecursiveWorkflow, Task
+
+llm = OpenAIChat(openai_api_key="YourKey")
+task = Task(llm, "What's the weather in miami")
+workflow = RecursiveWorkflow(stop_token="")
+workflow.add(task)
+workflow.run()
+```
+
+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/stepinput.md b/docs/swarms/structs/stepinput.md
new file mode 100644
index 00000000..ad4be016
--- /dev/null
+++ b/docs/swarms/structs/stepinput.md
@@ -0,0 +1,73 @@
+# Module/Class Name: StepInput
+
+The `StepInput` class is used to define the input parameters for the task step. It is a part of the `BaseModel` and accepts any value. This documentation will provide an overview of the class, its functionality, and usage examples.
+
+## Overview and Introduction
+The `StepInput` class is an integral part of the `swarms.structs` library, allowing users to define and pass input parameters for a specific task step. This class provides flexibility by accepting any value, allowing the user to customize the input parameters according to their requirements.
+
+## Class Definition
+The `StepInput` class is defined as follows:
+
+```python
+class StepInput(BaseModel):
+ __root__: Any = Field(
+ ...,
+ description=(
+ "Input parameters for the task step. Any value is"
+ " allowed."
+ ),
+ example='{\n"file_to_refactor": "models.py"\n}',
+ )
+```
+
+The `StepInput` class extends the `BaseModel` and contains a single field `__root__` of type `Any` with a description of accepting input parameters for the task step.
+
+## Functionality and Usage
+The `StepInput` class is designed to accept any input value, providing flexibility and customization for task-specific parameters. Upon creating an instance of `StepInput`, the user can define and pass input parameters as per their requirements.
+
+### Usage Example 1:
+```python
+from swarms.structs import StepInput
+
+input_params = {
+ "file_to_refactor": "models.py",
+ "refactor_method": "code"
+}
+step_input = StepInput(__root__=input_params)
+```
+
+In this example, we import the `StepInput` class from the `swarms.structs` library and create an instance `step_input` by passing a dictionary of input parameters. The `StepInput` class allows any value to be passed, providing flexibility for customization.
+
+### Usage Example 2:
+```python
+from swarms.structs import StepInput
+
+input_params = {
+ "input_path": "data.csv",
+ "output_path": "result.csv"
+}
+step_input = StepInput(__root__=input_params)
+```
+
+In this example, we again create an instance of `StepInput` by passing a dictionary of input parameters. The `StepInput` class does not restrict the type of input, allowing users to define parameters based on their specific task requirements.
+
+### Usage Example 3:
+```python
+from swarms.structs import StepInput
+
+file_path = "config.json"
+with open(file_path, 'r') as f:
+ input_data = json.load(f)
+
+step_input = StepInput(__root__=input_data)
+```
+
+In this example, we read input parameters from a JSON file and create an instance of `StepInput` by passing the loaded JSON data. The `StepInput` class seamlessly accepts input data from various sources, providing versatility to the user.
+
+## Additional Information and Tips
+When using the `StepInput` class, ensure that the input parameters are well-defined and align with the requirements of the task step. When passing complex data structures, such as nested dictionaries or JSON objects, ensure that the structure is valid and well-formed.
+
+## References and Resources
+- For further information on the `BaseModel` and `Field` classes, refer to the Pydantic documentation: [Pydantic Documentation](https://pydantic-docs.helpmanual.io/)
+
+The `StepInput` class within the `swarms.structs` library is a versatile and essential component for defining task-specific input parameters. Its flexibility in accepting any value and seamless integration with diverse data sources make it a valuable asset for customizing input parameters for task steps.
diff --git a/docs/swarms/structs/swarmnetwork.md b/docs/swarms/structs/swarmnetwork.md
new file mode 100644
index 00000000..2c2f74e5
--- /dev/null
+++ b/docs/swarms/structs/swarmnetwork.md
@@ -0,0 +1,157 @@
+```markdown
+# Class Name: SwarmNetwork
+
+## Overview and Introduction
+The `SwarmNetwork` class is responsible for managing the agents pool and the task queue. It also monitors the health of the agents and scales the pool up or down based on the number of pending tasks and the current load of the agents.
+
+## Class Definition
+
+The `SwarmNetwork` class has the following parameters:
+
+| Parameter | Type | Description |
+|-------------------|-------------------|-------------------------------------------------------------------------------|
+| idle_threshold | float | Threshold for idle agents to trigger scaling down |
+| busy_threshold | float | Threshold for busy agents to trigger scaling up |
+| agents | List[Agent] | List of agent instances to be added to the pool |
+| api_enabled | Optional[bool] | Flag to enable/disable the API functionality |
+| logging_enabled | Optional[bool] | Flag to enable/disable logging |
+| other arguments | *args | Additional arguments |
+| other keyword | **kwargs | Additional keyword arguments |
+
+## Function Explanation and Usage
+
+### Function: `add_task`
+- Adds a task to the task queue
+- Parameters:
+ - `task`: The task to be added to the queue
+- Example:
+ ```python
+ from swarms.structs.agent import Agent
+ from swarms.structs.swarm_net import SwarmNetwork
+ agent = Agent()
+ swarm = SwarmNetwork(agents=[agent])
+ swarm.add_task("task")
+ ```
+
+### Function: `async_add_task`
+- Asynchronous function to add a task to the task queue
+- Parameters:
+ - `task`: The task to be added to the queue
+- Example:
+ ```python
+ from swarms.structs.agent import Agent
+ from swarms.structs.swarm_net import SwarmNetwork
+ agent = Agent()
+ swarm = SwarmNetwork(agents=[agent])
+ await swarm.async_add_task("task")
+ ```
+
+### Function: `run_single_agent`
+- Executes a task on a single agent
+- Parameters:
+ - `agent_id`: ID of the agent to run the task on
+ - `task`: The task to be executed by the agent (optional)
+- Returns:
+ - Result of the task execution
+- Example:
+ ```python
+ from swarms.structs.agent import Agent
+ from swarms.structs.swarm_net import SwarmNetwork
+ agent = Agent()
+ swarm = SwarmNetwork(agents=[agent])
+ swarm.run_single_agent(agent_id, "task")
+ ```
+
+### Function: `run_many_agents`
+- Executes a task on all the agents in the pool
+- Parameters:
+ - `task`: The task to be executed by the agents (optional)
+- Returns:
+ - List of results from each agent
+- Example:
+ ```python
+ from swarms.structs.agent import Agent
+ from swarms.structs.swarm_net import SwarmNetwork
+ agent = Agent()
+ swarm = SwarmNetwork(agents=[agent])
+ swarm.run_many_agents("task")
+ ```
+
+### Function: `list_agents`
+- Lists all the agents in the pool
+- Returns:
+ - List of active agents
+- Example:
+ ```python
+ from swarms.structs.agent import Agent
+ from swarms.structs.swarm_net import SwarmNetwork
+ agent = Agent()
+ swarm = SwarmNetwork(agents=[agent])
+ swarm.list_agents()
+ ```
+
+### Function: `add_agent`
+- Adds an agent to the agent pool
+- Parameters:
+ - `agent`: Agent instance to be added to the pool
+- Example:
+ ```python
+ from swarms.structs.agent import Agent
+ from swarms.structs.swarm_net import SwarmNetwork
+ agent = Agent()
+ swarm = SwarmNetwork()
+ swarm.add_agent(agent)
+ ```
+
+### Function: `remove_agent`
+- Removes an agent from the agent pool
+- Parameters:
+ - `agent_id`: ID of the agent to be removed from the pool
+- Example:
+ ```python
+ from swarms.structs.agent import Agent
+ from swarms.structs.swarm_net import SwarmNetwork
+ agent = Agent()
+ swarm = SwarmNetwork(agents=[agent])
+ swarm.remove_agent(agent_id)
+ ```
+
+### Function: `scale_up`
+- Scales up the agent pool by adding new agents
+- Parameters:
+ - `num_agents`: Number of agents to be added (optional)
+- Example:
+ ```python
+ from swarms.structs.agent import Agent
+ from swarms.structs.swarm_net import SwarmNetwork
+ swarm = SwarmNetwork()
+ swarm.scale_up(num_agents=5)
+ ```
+
+### Function: `scale_down`
+- Scales down the agent pool by removing existing agents
+- Parameters:
+ - `num_agents`: Number of agents to be removed (optional)
+- Example:
+ ```python
+ from swarms.structs.agent import Agent
+ from swarms.structs.swarm_net import SwarmNetwork
+ swarm = SwarmNetwork(agents=[agent1, agent2, agent3, agent4, agent5])
+ swarm.scale_down(num_agents=2)
+ ```
+
+### Function: `create_apis_for_agents`
+- Creates APIs for each agent in the pool (optional)
+- Example:
+ ```python
+ from swarms.structs.agent import Agent
+ from swarms.structs.swarm_net import SwarmNetwork
+ agent = Agent()
+ swarm = SwarmNetwork(agents=[agent])
+ swarm.create_apis_for_agents()
+ ```
+
+## Additional Information
+- The `SwarmNetwork` class is an essential part of the swarms.structs library, enabling efficient management and scaling of agent pools.
+
+```
diff --git a/docs/swarms/structs/task.md b/docs/swarms/structs/task.md
new file mode 100644
index 00000000..7e829b66
--- /dev/null
+++ b/docs/swarms/structs/task.md
@@ -0,0 +1,28 @@
+- This is the class for the Task
+- For the constructor, it takes in the description, agent, args, kwargs, result, history, schedule_time, scheduler, trigger, action, condition, priority, and dependencies
+- The `execute` method runs the task by calling the agent or model with the arguments and keyword arguments
+- It sets a trigger, action, and condition for the task
+- Task completion is checked with `is_completed` method
+- `add_dependency` adds a task to the list of dependencies
+- `set_priority` sets the priority of the task
+
+```python
+# Example 1: Creating and executing a Task
+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(agent=agent)
+task.execute("What's the weather in miami")
+print(task.result)
+
+# Example 2: Adding a dependency and setting priority
+task2 = Task(description="Task 2", agent=agent)
+task.add_dependency(task2)
+task.set_priority(1)
+
+# Example 3: Executing a scheduled task
+task3 = Task(description="Scheduled Task", agent=agent)
+task3.schedule_time = datetime.datetime.now() + datetime.timedelta(minutes=30)
+task3.handle_scheduled_task()
+print(task3.is_completed())
+```
diff --git a/docs/swarms/structs/taskinput.md b/docs/swarms/structs/taskinput.md
new file mode 100644
index 00000000..e7a0be68
--- /dev/null
+++ b/docs/swarms/structs/taskinput.md
@@ -0,0 +1,75 @@
+## Module/Class Name: TaskInput
+
+The `TaskInput` class is designed to handle the input parameters for a task. It is an abstract class that serves as the base model for input data manipulation.
+
+### Overview and Introduction
+The `TaskInput` class is an essential component of the `swarms.structs` library, allowing users to define and pass input parameters to tasks. It is crucial for ensuring the correct and structured input to various tasks and processes within the library.
+
+### Class Definition
+
+#### TaskInput Class:
+- Parameters:
+ - `__root__` (Any): The input parameters for the task. Any value is allowed.
+
+### Disclaimer:
+It is important to note that the `TaskInput` class extends the `BaseModel` from the `pydantic` library. This means that it inherits all the properties and methods of the `BaseModel`.
+
+### Functionality and Usage
+The `TaskInput` class encapsulates the input parameters in a structured format. It allows for easy validation and manipulation of input data.
+
+#### Usage Example 1: Using TaskInput for Debugging
+```python
+from pydantic import BaseModel, Field
+from swarms.structs import TaskInput
+
+class DebugInput(TaskInput):
+ debug: bool
+
+# Creating an instance of DebugInput
+debug_params = DebugInput(__root__={"debug": True})
+
+# Accessing the input parameters
+print(debug_params.debug) # Output: True
+```
+
+#### Usage Example 2: Using TaskInput for Task Modes
+```python
+from pydantic import BaseModel, Field
+from swarms.structs import TaskInput
+
+class ModeInput(TaskInput):
+ mode: str
+
+# Creating an instance of ModeInput
+mode_params = ModeInput(__root__={"mode": "benchmarks"})
+
+# Accessing the input parameters
+print(mode_params.mode) # Output: benchmarks
+```
+
+#### Usage Example 3: Using TaskInput with Arbitrary Parameters
+```python
+from pydantic import BaseModel, Field
+from swarms.structs import TaskInput
+
+class ArbitraryInput(TaskInput):
+ message: str
+ quantity: int
+
+# Creating an instance of ArbitraryInput
+arbitrary_params = ArbitraryInput(__root__={"message": "Hello, world!", "quantity": 5})
+
+# Accessing the input parameters
+print(arbitrary_params.message) # Output: Hello, world!
+print(arbitrary_params.quantity) # Output: 5
+```
+
+### Additional Information and Tips
+- The `TaskInput` class can be extended to create custom input models with specific parameters tailored to individual tasks.
+- The `Field` class from `pydantic` can be used to specify metadata and constraints for the input parameters.
+
+### References and Resources
+- Official `pydantic` Documentation: [https://pydantic-docs.helpmanual.io/](https://pydantic-docs.helpmanual.io/)
+- Additional resources on data modelling with `pydantic`: [https://www.tiangolo.com/blog/2021/02/16/real-python-tutorial-modern-fastapi-pydantic/](https://www.tiangolo.com/blog/2021/02/16/real-python-tutorial-modern-fastapi-pydantic/)
+
+This documentation presents the `TaskInput` class, its usage, and practical examples for creating and handling input parameters within the `swarms.structs` library.
diff --git a/docs/swarms/swarms/godmode.md b/docs/swarms/swarms/godmode.md
index a0965c94..6655c954 100644
--- a/docs/swarms/swarms/godmode.md
+++ b/docs/swarms/swarms/godmode.md
@@ -1,4 +1,4 @@
-# `GodMode` Documentation
+# `ModelParallelizer` Documentation
## Table of Contents
1. [Understanding the Purpose](#understanding-the-purpose)
@@ -11,19 +11,19 @@
## 1. Understanding the Purpose
-To create comprehensive documentation for the `GodMode` class, let's begin by understanding its purpose and functionality.
+To create comprehensive documentation for the `ModelParallelizer` class, let's begin by understanding its purpose and functionality.
### Purpose and Functionality
-`GodMode` is a class designed to facilitate the orchestration of multiple Language Model Models (LLMs) to perform various tasks simultaneously. It serves as a powerful tool for managing, distributing, and collecting responses from these models.
+`ModelParallelizer` is a class designed to facilitate the orchestration of multiple Language Model Models (LLMs) to perform various tasks simultaneously. It serves as a powerful tool for managing, distributing, and collecting responses from these models.
Key features and functionality include:
-- **Parallel Task Execution**: `GodMode` can distribute tasks to multiple LLMs and execute them in parallel, improving efficiency and reducing response time.
+- **Parallel Task Execution**: `ModelParallelizer` can distribute tasks to multiple LLMs and execute them in parallel, improving efficiency and reducing response time.
- **Structured Response Presentation**: The class presents the responses from LLMs in a structured tabular format, making it easy for users to compare and analyze the results.
-- **Task History Tracking**: `GodMode` keeps a record of tasks that have been submitted, allowing users to review previous tasks and responses.
+- **Task History Tracking**: `ModelParallelizer` keeps a record of tasks that have been submitted, allowing users to review previous tasks and responses.
- **Asynchronous Execution**: The class provides options for asynchronous task execution, which can be particularly useful for handling a large number of tasks.
@@ -33,29 +33,29 @@ Now that we have an understanding of its purpose, let's proceed to provide a det
### Overview
-The `GodMode` class is a crucial component for managing and utilizing multiple LLMs in various natural language processing (NLP) tasks. Its architecture and functionality are designed to address the need for parallel processing and efficient response handling.
+The `ModelParallelizer` class is a crucial component for managing and utilizing multiple LLMs in various natural language processing (NLP) tasks. Its architecture and functionality are designed to address the need for parallel processing and efficient response handling.
### Importance and Relevance
-In the rapidly evolving field of NLP, it has become common to use multiple language models to achieve better results in tasks such as translation, summarization, and question answering. `GodMode` streamlines this process by allowing users to harness the capabilities of several LLMs simultaneously.
+In the rapidly evolving field of NLP, it has become common to use multiple language models to achieve better results in tasks such as translation, summarization, and question answering. `ModelParallelizer` streamlines this process by allowing users to harness the capabilities of several LLMs simultaneously.
Key points:
-- **Parallel Processing**: `GodMode` leverages multithreading to execute tasks concurrently, significantly reducing the time required for processing.
+- **Parallel Processing**: `ModelParallelizer` leverages multithreading to execute tasks concurrently, significantly reducing the time required for processing.
- **Response Visualization**: The class presents responses in a structured tabular format, enabling users to visualize and analyze the outputs from different LLMs.
-- **Task Tracking**: Developers can track the history of tasks submitted to `GodMode`, making it easier to manage and monitor ongoing work.
+- **Task Tracking**: Developers can track the history of tasks submitted to `ModelParallelizer`, making it easier to manage and monitor ongoing work.
### Architecture and How It Works
-The architecture and working of `GodMode` can be summarized in four steps:
+The architecture and working of `ModelParallelizer` can be summarized in four steps:
-1. **Task Reception**: `GodMode` receives a task from the user.
+1. **Task Reception**: `ModelParallelizer` receives a task from the user.
2. **Task Distribution**: The class distributes the task to all registered LLMs.
-3. **Response Collection**: `GodMode` collects the responses generated by the LLMs.
+3. **Response Collection**: `ModelParallelizer` collects the responses generated by the LLMs.
4. **Response Presentation**: Finally, the class presents the responses from all LLMs in a structured tabular format, making it easy for users to compare and analyze the results.
@@ -65,15 +65,15 @@ Now that we have an overview, let's proceed with a detailed class definition.
### Class Attributes
-- `llms`: A list of LLMs (Language Model Models) that `GodMode` manages.
+- `llms`: A list of LLMs (Language Model Models) that `ModelParallelizer` manages.
- `last_responses`: Stores the responses from the most recent task.
-- `task_history`: Keeps a record of all tasks submitted to `GodMode`.
+- `task_history`: Keeps a record of all tasks submitted to `ModelParallelizer`.
### Methods
-The `GodMode` class defines various methods to facilitate task distribution, execution, and response presentation. Let's examine some of the key methods:
+The `ModelParallelizer` class defines various methods to facilitate task distribution, execution, and response presentation. Let's examine some of the key methods:
- `run(task)`: Distributes a task to all LLMs, collects responses, and returns them.
@@ -87,23 +87,23 @@ The `GodMode` class defines various methods to facilitate task distribution, exe
- `save_responses_to_file(filename)`: Saves responses to a file for future reference.
-- `load_llms_from_file(filename)`: Loads LLMs from a file, making it easy to configure `GodMode` for different tasks.
+- `load_llms_from_file(filename)`: Loads LLMs from a file, making it easy to configure `ModelParallelizer` for different tasks.
- `get_task_history()`: Retrieves the task history, allowing users to review previous tasks.
- `summary()`: Provides a summary of task history and the last responses, aiding in post-processing and analysis.
-Now that we have covered the class definition, let's delve into the functionality and usage of `GodMode`.
+Now that we have covered the class definition, let's delve into the functionality and usage of `ModelParallelizer`.
## 4. Functionality and Usage
### Distributing a Task and Collecting Responses
-One of the primary use cases of `GodMode` is to distribute a task to all registered LLMs and collect their responses. This can be achieved using the `run(task)` method. Below is an example:
+One of the primary use cases of `ModelParallelizer` is to distribute a task to all registered LLMs and collect their responses. This can be achieved using the `run(task)` method. Below is an example:
```python
-god_mode = GodMode(llms)
-responses = god_mode.run("Translate the following English text to French: 'Hello, how are you?'")
+parallelizer = ModelParallelizer(llms)
+responses = parallelizer.run("Translate the following English text to French: 'Hello, how are you?'")
```
### Printing Responses
@@ -111,7 +111,7 @@ responses = god_mode.run("Translate the following English text to French: 'Hello
To present the responses from all LLMs in a structured tabular format, use the `print_responses(task)` method. Example:
```python
-god_mode.print_responses("Summarize the main points of 'War and Peace.'")
+parallelizer.print_responses("Summarize the main points of 'War and Peace.'")
```
### Saving Responses to a File
@@ -119,15 +119,15 @@ god_mode.print_responses("Summarize the main points of 'War and Peace.'")
Users can save the responses to a file using the `save_responses_to_file(filename)` method. This is useful for archiving and reviewing responses later. Example:
```python
-god_mode.save_responses_to_file("responses.txt")
+parallelizer.save_responses_to_file("responses.txt")
```
### Task History
-The `GodMode` class keeps track of the task history. Developers can access the task history using the `get_task_history()` method. Example:
+The `ModelParallelizer` class keeps track of the task history. Developers can access the task history using the `get_task_history()` method. Example:
```python
-task_history = god_mode.get_task_history()
+task_history = parallelizer.get_task_history()
for i, task in enumerate(task_history):
print(f"Task {i + 1}: {task}")
```
@@ -136,7 +136,7 @@ for i, task in enumerate(task_history):
### Parallel Execution
-`GodMode` employs multithreading to execute tasks concurrently. This parallel processing capability significantly improves the efficiency of handling multiple tasks simultaneously.
+`ModelParallelizer` employs multithreading to execute tasks concurrently. This parallel processing capability significantly improves the efficiency of handling multiple tasks simultaneously.
### Response Visualization
@@ -144,13 +144,13 @@ The structured tabular format used for presenting responses simplifies the compa
## 6. Examples
-Let's explore additional usage examples to illustrate the versatility of `GodMode` in handling various NLP tasks.
+Let's explore additional usage examples to illustrate the versatility of `ModelParallelizer` in handling various NLP tasks.
### Example 1: Sentiment Analysis
```python
from swarms.models import OpenAIChat
-from swarms.swarms import GodMode
+from swarms.swarms import ModelParallelizer
from swarms.workers.worker import Worker
# Create an instance of an LLM for sentiment analysis
@@ -184,15 +184,15 @@ worker3 = Worker(
temperature=0.5,
)
-# Register the worker agents with GodMode
+# Register the worker agents with ModelParallelizer
agents = [worker1, worker2, worker3]
-god_mode = GodMode(agents)
+parallelizer = ModelParallelizer(agents)
# Task for sentiment analysis
task = "Please analyze the sentiment of the following sentence: 'This movie is amazing!'"
# Print responses from all agents
-god_mode.print_responses(task)
+parallelizer.print_responses(task)
```
### Example 2: Translation
@@ -200,22 +200,22 @@ god_mode.print_responses(task)
```python
from swarms.models import OpenAIChat
-from swarms.swarms import GodMode
+from swarms.swarms import ModelParallelizer
# Define LLMs for translation tasks
translator1 = OpenAIChat(model_name="translator-en-fr", openai_api_key="api-key", temperature=0.7)
translator2 = OpenAIChat(model_name="translator-en-es", openai_api_key="api-key", temperature=0.7)
translator3 = OpenAIChat(model_name="translator-en-de", openai_api_key="api-key", temperature=0.7)
-# Register translation agents with GodMode
+# Register translation agents with ModelParallelizer
translators = [translator1, translator2, translator3]
-god_mode = GodMode(translators)
+parallelizer = ModelParallelizer(translators)
# Task for translation
task = "Translate the following English text to French: 'Hello, how are you?'"
# Print translated responses from all agents
-god_mode.print_responses(task)
+parallelizer.print_responses(task)
```
### Example 3: Summarization
@@ -223,7 +223,7 @@ god_mode.print_responses(task)
```python
from swarms.models import OpenAIChat
-from swarms.swarms import GodMode
+from swarms.swarms import ModelParallelizer
# Define LLMs for summarization tasks
@@ -231,19 +231,19 @@ summarizer1 = OpenAIChat(model_name="summarizer-en", openai_api_key="api-key", t
summarizer2 = OpenAIChat(model_name="summarizer-en", openai_api_key="api-key", temperature=0.6)
summarizer3 = OpenAIChat(model_name="summarizer-en", openai_api_key="api-key", temperature=0.6)
-# Register summarization agents with GodMode
+# Register summarization agents with ModelParallelizer
summarizers = [summarizer1, summarizer2, summarizer3]
-god_mode = GodMode(summarizers)
+parallelizer = ModelParallelizer(summarizers)
# Task for summarization
task = "Summarize the main points of the article titled 'Climate Change and Its Impact on the Environment.'"
# Print summarized responses from all agents
-god_mode.print_responses(task)
+parallelizer.print_responses(task)
```
## 7. Conclusion
-In conclusion, the `GodMode` class is a powerful tool for managing and orchestrating multiple Language Model Models in natural language processing tasks. Its ability to distribute tasks, collect responses, and present them in a structured format makes it invaluable for streamlining NLP workflows. By following the provided documentation, users can harness the full potential of `GodMode` to enhance their natural language processing projects.
+In conclusion, the `ModelParallelizer` class is a powerful tool for managing and orchestrating multiple Language Model Models in natural language processing tasks. Its ability to distribute tasks, collect responses, and present them in a structured format makes it invaluable for streamlining NLP workflows. By following the provided documentation, users can harness the full potential of `ModelParallelizer` to enhance their natural language processing projects.
For further information on specific LLMs or advanced usage, refer to the documentation of the respective models and their APIs. Additionally, external resources on parallel execution and response visualization can provide deeper insights into these topics.
\ No newline at end of file
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/docs/swarms/utils/check_device.md b/docs/swarms/utils/check_device.md
new file mode 100644
index 00000000..bdb8c780
--- /dev/null
+++ b/docs/swarms/utils/check_device.md
@@ -0,0 +1,86 @@
+# check_device
+
+# Module/Function Name: check_device
+
+The `check_device` is a utility function in PyTorch designed to identify and return the appropriate device(s) for CUDA processing. If CUDA is not available, a CPU device is returned. If CUDA is available, the function returns a list of all available GPU devices.
+
+The function examines the CUDA availability, checks for multiple GPUs, and finds additional properties for each device.
+
+## Function Signature and Arguments
+
+**Signature:**
+```python
+def check_device(
+ log_level: Any = logging.INFO,
+ memory_threshold: float = 0.8,
+ capability_threshold: float = 3.5,
+ return_type: str = "list",
+) -> Union[torch.device, List[torch.device]]
+```
+
+| Parameter | Data Type | Default Value | Description |
+| ------------- | ------------- | ------------- | ------------- |
+| `log_level` | Any | logging.INFO | The log level. |
+| `memory_threshold` | float | 0.8 | It is used to check the threshold of memory used on the GPU(s). |
+| `capability_threshold` | float | 3.5 | It is used to consider only those GPU(s) which have higher compute capability compared to the threshold. |
+| `return_type` | str | "list" | Depending on the `return_type` either a list of devices can be returned or a single device. |
+
+This function does not take any mandatory argument. However, it supports optional arguments such as `log_level`, `memory_threshold`, `capability_threshold`, and `return_type`.
+
+**Returns:**
+
+- A single torch.device if one device or list of torch.devices if multiple CUDA devices are available, else returns the CPU device if CUDA is not available.
+
+## Usage and Examples
+
+### Example 1: Basic Usage
+
+```python
+import torch
+import logging
+from swarms.utils import check_device
+
+# Basic usage
+device = check_device(
+ log_level=logging.INFO,
+ memory_threshold=0.8,
+ capability_threshold=3.5,
+ return_type="list"
+)
+```
+
+### Example 2: Using CPU when CUDA is not available
+
+```python
+import torch
+import logging
+from swarms.utils import check_device
+
+# When CUDA is not available
+device = check_device()
+print(device) # If CUDA is not available it should return torch.device('cpu')
+```
+
+### Example 3: Multiple GPU Available
+
+```python
+import torch
+import logging
+from swarms.utils import check_device
+
+# When multiple GPUs are available
+device = check_device()
+print(device) # Should return a list of available GPU devices
+```
+
+## Tips and Additional Information
+
+- This function is useful when a user wants to exploit CUDA capabilities for faster computation but unsure of the available devices. This function abstracts all the necessary checks and provides a list of CUDA devices to the user.
+- The `memory_threshold` and `capability_threshold` are utilized to filter the GPU devices. The GPUs which have memory usage above the `memory_threshold` and compute capability below the `capability_threshold` are not considered.
+- As of now, CPU does not have memory or capability values, therefore, in the respective cases, it will be returned as default without any comparison.
+
+## Relevant Resources
+
+- For more details about the CUDA properties functions used (`torch.cuda.get_device_capability, torch.cuda.get_device_properties`), please refer to the official PyTorch [CUDA semantics documentation](https://pytorch.org/docs/stable/notes/cuda.html).
+- For more information about Torch device objects, you can refer to the official PyTorch [device documentation](https://pytorch.org/docs/stable/tensor_attributes.html#torch-device).
+- For a better understanding of how the `logging` module works in Python, see the official Python [logging documentation](https://docs.python.org/3/library/logging.html).
diff --git a/docs/swarms/utils/display_markdown_message.md b/docs/swarms/utils/display_markdown_message.md
new file mode 100644
index 00000000..c1e3f894
--- /dev/null
+++ b/docs/swarms/utils/display_markdown_message.md
@@ -0,0 +1,86 @@
+# display_markdown_message
+
+# Module Name: `display_markdown_message`
+
+## Introduction
+
+`display_markdown_message` is a useful utility function for creating visually-pleasing markdown messages within Python scripts. This function automatically manages multiline strings with lots of indentation and makes single-line messages with ">" tags easy to read, providing users with convenient and elegant logging or messaging capacity.
+
+## Function Definition and Arguments
+
+Function Definition:
+```python
+def display_markdown_message(message: str, color: str = "cyan"):
+ ```
+This function accepts two parameters:
+
+|Parameter |Type |Default Value |Description |
+|--- |--- |--- |--- |
+|message |str |None |This is the message that is to be displayed. This should be a string. It can contain markdown syntax.|
+|color |str |"cyan" |This allows you to choose the color of the message. Default is "cyan". Accepts any valid color name.|
+
+## Functionality and Usage
+
+This utility function is used to display a markdown formatted message on the console. It accepts a message as a string and an optional color for the message. The function is ideal for generating stylized print outputs such as headers, status updates or pretty notifications.
+
+By default, any text within the string which is enclosed within `>` tags or `---` is treated specially:
+
+- Lines encased in `>` tags are rendered as a blockquote in markdown.
+- Lines consisting of `---` are rendered as horizontal rules.
+
+The function automatically strips off leading and trailing whitespaces from any line within the message, maintaining aesthetic consistency in your console output.
+
+### Usage Examples
+
+#### Basic Example
+
+```python
+display_markdown_message("> This is an important message", color="red")
+```
+
+Output:
+```md
+> **This is an important message**
+```
+
+This example will print out the string "This is an important message" in red color, enclosed in a blockquote tag.
+
+#### Multiline Example
+
+```python
+message = """
+> Header
+
+My normal message here.
+
+---
+
+Another important information
+"""
+display_markdown_message(message, color="green")
+```
+
+Output:
+```md
+> **Header**
+
+My normal message here.
+_____
+
+Another important information
+```
+The output is a green colored markdown styled text with the "Header" enclosed in a blockquote, followed by the phrase "My normal message here", a horizontal rule, and finally another phrase, "Another important information".
+
+## Additional Information
+
+Use newline characters `\n` to separate the lines of the message. Remember, each line of the message is stripped of leading and trailing whitespaces. If you have special markdown requirements, you may need to revise the input message string accordingly.
+
+Also, keep in mind the console or terminal's ability to display the chosen color. If a particular console does not support the chosen color, the output may fallback to the default console color.
+
+For a full list of color names supported by the `Console` module, refer to the official [Console documentation](http://console.readthedocs.io/).
+
+## References and Resources
+
+- Python Strings: https://docs.python.org/3/tutorial/introduction.html#strings
+- Python Markdown: https://pypi.org/project/markdown/
+- Console module: https://console.readthedocs.io/
diff --git a/docs/swarms/utils/extract_code_from_markdown.md b/docs/swarms/utils/extract_code_from_markdown.md
new file mode 100644
index 00000000..f6f76835
--- /dev/null
+++ b/docs/swarms/utils/extract_code_from_markdown.md
@@ -0,0 +1,114 @@
+# extract_code_from_markdown
+
+# swarms.utils Module
+
+The `swarms.utils` module provides utility functions designed to facilitate specific tasks within the main Swarm codebase. The function `extract_code_from_markdown` is a critical function within this module that we will document in this example.
+
+## Overview and Introduction
+
+Many software projects use Markdown extensively for writing documentation, tutorials, and other text documents that can be easily rendered and viewed in different formats, including HTML.
+
+The `extract_code_from_markdown` function plays a crucial role within the swarms.utils library. As developers write large volumes of Markdown, they often need to isolate code snippets from the whole Markdown file body. These isolated snippets can be used to generate test cases, transform into other languages, or analyze for metrics.
+
+## Function Definition: `extract_code_from_markdown`
+
+```python
+def extract_code_from_markdown(markdown_content: str) -> str:
+ """
+ Extracts code blocks from a Markdown string and returns them as a single string.
+
+ Args:
+ - markdown_content (str): The Markdown content as a string.
+
+ Returns:
+ - str: A single string containing all the code blocks separated by newlines.
+ """
+ # Regular expression for fenced code blocks
+ pattern = r"```(?:\w+\n)?(.*?)```"
+ matches = re.findall(pattern, markdown_content, re.DOTALL)
+
+ # Concatenate all code blocks separated by newlines
+ return "\n".join(code.strip() for code in matches)
+```
+
+### Arguments
+
+The function `extract_code_from_markdown` takes one argument:
+
+| Argument | Description | Type | Default Value |
+|-----------------------|----------------------------------------|-------------|-------------------|
+| markdown_content | The input markdown content as a string | str | N/A |
+
+
+## Function Explanation and Usage
+
+This function uses a regular expression to find all fenced code blocks in a Markdown string. The pattern `r"```(?:\w+\n)?(.*?)```"` matches strings that start and end with three backticks, optionally followed by a newline and then any number of any characters (the `.*?` part) until the first occurrence of another triple backtick set.
+
+Once we have the matches, we join all the code blocks into a single string, each block separated by a newline.
+
+The method's functionality is particularly useful when we need to extract code blocks from markdown content for secondary processing, such as syntax highlighting or execution in a different environment.
+
+### Usage Examples
+
+Below are three examples of how you might use this function:
+
+#### Example 1:
+
+Extracting code blocks from a simple markdown string.
+
+```python
+import re
+from swarms.utils import extract_code_from_markdown
+
+markdown_string = '''# Example
+This is an example of a code block:
+```python
+print("Hello World!")
+``` '''
+print(extract_code_from_markdown(markdown_string))
+```
+
+#### Example 2:
+
+Extracting code blocks from a markdown file.
+
+```python
+import re
+
+def extract_code_from_markdown(markdown_content: str) -> str:
+ pattern = r"```(?:\w+\n)?(.*?)```"
+ matches = re.findall(pattern, markdown_content, re.DOTALL)
+ return "\n".join(code.strip() for code in matches)
+
+# Assume that 'example.md' contains multiple code blocks
+with open('example.md', 'r') as file:
+ markdown_content = file.read()
+print(extract_code_from_markdown(markdown_content))
+```
+
+#### Example 3:
+
+Using the function in a pipeline to extract and then analyze code blocks.
+
+```python
+import re
+
+def extract_code_from_markdown(markdown_content: str) -> str:
+ pattern = r"```(?:\w+\n)?(.*?)```"
+ matches = re.findall(pattern, markdown_content, re.DOTALL)
+ return "\n".join(code.strip() for code in matches)
+
+def analyze_code_blocks(code: str):
+ # Add your analysis logic here
+ pass
+
+# Assume that 'example.md' contains multiple code blocks
+with open('example.md', 'r') as file:
+ markdown_content = file.read()
+code_blocks = extract_code_from_markdown(markdown_content)
+analyze_code_blocks(code_blocks)
+```
+
+## Conclusion
+
+This concludes the detailed documentation of the `extract_code_from_markdown` function from the swarms.utils module. With this documentation, you should be able to understand the function's purpose, how it works, its parameters, and see examples of how to use it effectively.
diff --git a/docs/swarms/utils/find_image_path.md b/docs/swarms/utils/find_image_path.md
new file mode 100644
index 00000000..59c9c127
--- /dev/null
+++ b/docs/swarms/utils/find_image_path.md
@@ -0,0 +1,94 @@
+# find_image_path
+
+Firstly, we will divide this documentation into multiple sections.
+
+# Overview
+The module **swarms.utils** has the main goal of providing necessary utility functions that are crucial during the creation of the swarm intelligence frameworks. These utility functions can include common operations such as handling input-output operations for files, handling text parsing, and handling basic mathematical computations necessary during the creation of swarm intelligence models.
+
+The current function `find_image_path` in the module is aimed at extracting an image path from a given text document.
+
+# Function Detailed Explanation
+
+## Definition
+The function `find_image_path` takes a singular argument as an input:
+
+```python
+def find_image_path(text):
+ # function body
+```
+
+## Parameter
+The parameter `text` in the function is a string that represents the document or text from which the function is trying to extract all paths to the images present. The function scans the given text, looking for absolute or relative paths to image files (.png, .jpg, .jpeg) on the disk.
+
+| Parameter Name | Data Type | Default Value | Description |
+|:--------------:|:---------:|:-------------:|:--------:|
+| `text` | `str` | - | The text content to scan for image paths |
+
+## Return Value
+
+The return value of the function `find_image_path` is a string that represents the longest existing image path extracted from the input text. If no image paths exist within the text, the function returns `None`.
+
+
+| Return Value | Data Type | Description |
+|:------------:|:-----------:|:-----------:|
+| Path | `str` | Longest image path found in the text or `None` if no path found |
+
+# Function's Code
+
+The function `find_image_path` performs text parsing and pattern recognition to find image paths within the provided text. The function uses `regular expressions (re)` module to detect all potential paths.
+
+```python
+def find_image_path(text):
+ pattern = r"([A-Za-z]:\\[^:\n]*?\.(png|jpg|jpeg|PNG|JPG|JPEG))|(/[^:\n]*?\.(png|jpg|jpeg|PNG|JPG|JPEG))"
+ matches = [
+ match.group()
+ for match in re.finditer(pattern, text)
+ if match.group()
+ ]
+ matches += [match.replace("\\", "") for match in matches if match]
+ existing_paths = [
+ match for match in matches if os.path.exists(match)
+ ]
+ return max(existing_paths, key=len) if existing_paths else None
+```
+
+# Usage Examples
+
+Let's consider examples of how the function `find_image_path` can be used in different scenarios.
+
+**Example 1:**
+
+Consider the case where a text without any image path is provided.
+
+```python
+from swarms.utils import find_image_path
+
+text = "There are no image paths in this text"
+print(find_image_path(text)) # Outputs: None
+```
+
+**Example 2:**
+
+Consider the case where the text has multiple image paths.
+
+```python
+from swarms.utils import find_image_path
+
+text = "Here is an image path: /home/user/image1.png. Here is another one: C:\\Users\\User\\Documents\\image2.jpeg"
+print(find_image_path(text)) # Outputs: the longest image path (depends on your file system and existing files)
+```
+
+**Example 3:**
+
+In the final example, we consider a case where the text has an image path, but the file does not exist.
+
+```python
+from swarms.utils import find_image_path
+
+text = "Here is an image path: /home/user/non_existant.png"
+print(find_image_path(text)) # Outputs: None
+```
+
+# Closing Notes
+
+In conclusion, the `find_image_path` function is crucial in the `swarms.utils` module as it supports a key operation of identifying image paths within given input text. This allows users to automate the extraction of such data from larger documents/text. However, it's important to note the function returns only existing paths in your file system and only the longest if multiple exist.
diff --git a/docs/swarms/utils/limit_tokens_from_string.md b/docs/swarms/utils/limit_tokens_from_string.md
new file mode 100644
index 00000000..b096ebad
--- /dev/null
+++ b/docs/swarms/utils/limit_tokens_from_string.md
@@ -0,0 +1,82 @@
+# limit_tokens_from_string
+
+## Introduction
+The `Swarms.utils` library contains utility functions used across codes that handle machine learning and other operations. The `Swarms.utils` library includes a notable function named `limit_tokens_from_string()`. This function particularly limits the number of tokens in a given string.
+
+# Function: limit_tokens_from_string()
+Within the `Swarms.utils` library, there is a method `limit_tokens_from_string(string: str, model: str = "gpt-4", limit: int = 500) -> str:`
+
+## Description
+The function `limit_tokens_from_string()` limits the number of tokens in a given string based on the specified threshold. It is primarily useful when you are handling large text data and need to chunk or limit your text to a certain length. Limiting token length could be useful in various scenarios such as when working with data with limited computational resources, or when dealing with models that accept a specific maximum limit of text.
+
+## Parameters
+
+| Parameter | Type | Default Value | Description
+| :-----------| :----------- | :------------ | :------------|
+| `string` | `str` | `None` | The input string from which the tokens need to be limited. |
+| `model` | `str` | `"gpt-4"` | The model used to encode and decode the token. The function defaults to `gpt-4` but you can specify any model supported by `tiktoken`. If a model is not found, it falls back to use `gpt2` |
+| `limit` | `int` | `500` | The limit up to which the tokens have to be sliced. Default limit is 500.|
+
+## Returns
+
+| Return | Type | Description
+| :-----------| :----------- | :------------
+| `out` | `str` | A string that is constructed back from the encoded tokens that have been limited to a count of `limit` |
+
+## Method Detail and Usage Examples
+
+The method `limit_tokens_from_string()` takes in three parameters - `string`, `model`, and `limit`.
+
+
+First, it tries to get the encoding for the model specified in the `model` argument using `tiktoken.encoding_for_model(model)`. In case the specified model is not found, the function uses `gpt2` model encoding as a fallback.
+
+Next, the input `string` is tokenized using the `encode` method on the `encoding` tensor. This results in the `encoded` tensor.
+
+Then, the function slices the `encoded` tensor to get the first `limit` number of tokens.
+
+Finally, the function converts back the tokens into the string using the `decode` method of the `encoding` tensor. The resulting string `out` is returned.
+
+### Example 1:
+
+```python
+from swarms.utils import limit_tokens_from_string
+
+# longer input string
+string = "This is a very long string that needs to be tokenized. This string might exceed the maximum token limit, so it will need to be truncated."
+
+# lower token limit
+limit = 10
+
+output = limit_tokens_from_string(string, limit=limit)
+```
+
+### Example 2:
+
+```python
+from swarms.utils import limit_tokens_from_string
+
+# longer input string with different model
+string = "This string will be tokenized using gpt2 model. If the string is too long, it will be truncated."
+
+# model
+model = "gpt2"
+
+output = limit_tokens_from_string(string, model=model)
+```
+
+### Example 3:
+
+```python
+from swarms.utils import limit_tokens_from_string
+
+# try with a random model string
+string = "In case the method does not find the specified model, it will fall back to gpt2 model."
+
+# model
+model = "gpt-4"
+
+output = limit_tokens_from_string(string, model=model)
+```
+
+**Note:** If specifying a model not supported by `tiktoken` intentionally, it will fall back to `gpt2` model for encoding.
+
diff --git a/docs/swarms/utils/load_model_torch.md b/docs/swarms/utils/load_model_torch.md
new file mode 100644
index 00000000..ddcd7ee6
--- /dev/null
+++ b/docs/swarms/utils/load_model_torch.md
@@ -0,0 +1,102 @@
+# load_model_torch
+
+# load_model_torch: Utility Function Documentation
+
+## Introduction:
+
+`load_model_torch` is a utility function in the `swarms.utils` library that is designed to load a saved PyTorch model and move it to the designated device. It provides flexibility allowing the user to specify the model file location, the device where the loaded model should be moved to, whether to strictly enforce the keys in the state dictionary to match the keys returned by the model's `state_dict()`, and many more.
+
+Moreover, if the saved model file only contains the state dictionary, but not the model architecture, you can pass the model architecture as an argument.
+
+## Function Definition and Parameters:
+
+```python
+def load_model_torch(
+ model_path: str = None,
+ device: torch.device = None,
+ model: nn.Module = None,
+ strict: bool = True,
+ map_location=None,
+ *args,
+ **kwargs,
+) -> nn.Module:
+```
+
+The following table describes the parameters in detail:
+
+| Name | Type | Default Value | Description |
+| ------ | ------ | ------------- | ------------|
+| model_path | str | None | A string specifying the path to the saved model file on disk. _Required_ |
+| device | torch.device | None | A `torch.device` object that specifies the target device for the loaded model. If not provided, the function checks for the availability of a GPU and uses it if available. If not, it defaults to CPU. |
+| model | nn.Module | None | An instance of `torch.nn.Module` representing the model's architecture. This parameter is required if the model file only contains the model's state dictionary and not the model architecture. |
+| strict | bool | True | A boolean that determines whether to strictly enforce that the keys in the state dictionary match the keys returned by the model's `state_dict()` function. If set to `True`, the function will raise a KeyError when the state dictionary and `state_dict()` keys do not match. |
+| map_location | callable | None | A function to remap the storage locations of the loaded model's parameters. Useful for loading models saved on a device type that is different from the current one. |
+| *args, **kwargs | - | - | Additional arguments and keyword arguments to be passed to `torch.load`.
+
+Returns:
+
+- `torch.nn.Module` - The loaded model after moving it to the desired device.
+
+Raises:
+
+- `FileNotFoundError` - If the saved model file is not found at the specified path.
+- `RuntimeError` - If there was an error while loading the model.
+
+## Example of Usage:
+
+This function can be used directly inside your code as shown in the following examples:
+
+### Example 1:
+Loading a model without specifying a device results in the function choosing the most optimal available device automatically.
+
+```python
+from swarms.utils import load_model_torch
+import torch.nn as nn
+
+# Assume `mymodel.pth` is in the current directory
+model_path = "./mymodel.pth"
+
+# Define your model architecture if the model file only contains state dict
+class MyModel(nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.linear = nn.Linear(10, 2)
+
+ def forward(self, x):
+ return self.linear(x)
+
+model = MyModel()
+
+# Load the model
+loaded_model = load_model_torch(model_path, model=model)
+
+# Now you can use the loaded model for prediction or further training
+```
+### Example 2:
+Explicitly specifying a device.
+
+```python
+# Assume `mymodel.pth` is in the current directory
+model_path = "./mymodel.pth"
+device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
+
+# Load the model
+loaded_model = load_model_torch(model_path, device=device)
+```
+
+### Example 3:
+Using a model file that contains only the state dictionary, not the model architecture.
+
+```python
+# Assume `mymodel_state_dict.pth` is in the current directory
+model_path = "./mymodel_state_dict.pth"
+device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
+
+# Define your model architecture
+model = MyModel()
+
+# Load the model
+loaded_model = load_model_torch(model_path, device=device, model=model)
+```
+
+This gives you an insight on how to use `load_model_torch` utility function from `swarms.utils` library efficiently. Always remember to pass the model path argument while the other arguments can be optional based on your requirements. Furthermore, handle exceptions properly for smooth functioning of your PyTorch related projects.
diff --git a/docs/swarms/utils/math_eval.md b/docs/swarms/utils/math_eval.md
new file mode 100644
index 00000000..691089f8
--- /dev/null
+++ b/docs/swarms/utils/math_eval.md
@@ -0,0 +1,78 @@
+# math_eval
+
+
+The `math_eval` function is a python decorator that wraps around a function to run two functions on the same inputs and compare their results. The decorator can be used for testing functions that are expected to have equivalent functionality, or in situations where two different methods are used to calculate or retrieve a value, and the results need to be compared.
+
+The `math_eval` function in this case accepts two functions as parameters: `func1` and `func2`, and returns a decorator. This returned decorator, when applied to a function, enhances that function to execute both `func1` and `func2`, and compare the results.
+
+This can be particularly useful in situations when you are implementing a new function and wants to compare its behavior and results with that of an existing one under the same set of input parameters. It also logs the results if they do not match which could be quite useful during the debug process.
+
+## Usage Example
+
+Let's say you have two functions: `ground_truth` and `generated_func`, that have similar functionalities or serve the same purpose. You are writing a new function called `test_func`, and you'd like to compare the results of `ground_truth` and `generated_func` when `test_func` is run. Here is how you would use the `math_eval` decorator:
+
+```python
+@math_eval(ground_truth, generated_func)
+def test_func(x):
+ return x
+result1, result2 = test_func(5)
+print(f"Result from ground_truth: {result1}")
+print(f"Result from generated_func: {result2}")
+```
+
+## Parameters
+
+| Parameter | Data Type | Description |
+| ---- | ---- | ---- |
+| func1 | Callable | The first function whose result you want to compare. |
+| func2 | Callable | The second function whose result you want to compare. |
+
+The data types for `func1` and `func2` cannot be specified as they can be any python function (or callable object). The decorator verifies that they are callable and exceptions are handled within the decorator function.
+
+## Return Values
+
+The `math_eval` function does not return a direct value, since it is a decorator. When applied to a function, it alters the behavior of the wrapped function to return two values:
+
+1. `result1`: The result of running `func1` with the given input parameters.
+2. `result2`: The result of running `func2` with the given input parameters.
+
+These two return values are provided in that order as a tuple.
+
+## Source Code
+
+Here's how to implement the `math_eval` decorator:
+
+```python
+import functools
+import logging
+
+def math_eval(func1, func2):
+ """Math evaluation decorator."""
+
+ def decorator(func):
+ @functools.wraps(func)
+ def wrapper(*args, **kwargs):
+ try:
+ result1 = func1(*args, **kwargs)
+ except Exception as e:
+ logging.error(f"Error in func1: {e}")
+ result1 = None
+
+ try:
+ result2 = func2(*args, **kwargs)
+ except Exception as e:
+ logging.error(f"Error in func2: {e}")
+ result2 = None
+
+ if result1 != result2:
+ logging.warning(
+ f"Outputs do not match: {result1} != {result2}"
+ )
+
+ return result1, result2
+
+ return wrapper
+
+ return decorator
+```
+Please note that the code is logging exceptions to facilitate debugging, but the actual processing and handling of the exception would depend on how you want your application to respond to exceptions. Therefore, you may want to customize the error handling depending upon your application's requirements.
diff --git a/docs/swarms/utils/metrics_decorator.md b/docs/swarms/utils/metrics_decorator.md
new file mode 100644
index 00000000..aeafe151
--- /dev/null
+++ b/docs/swarms/utils/metrics_decorator.md
@@ -0,0 +1,86 @@
+# metrics_decorator
+
+This documentation explains the use and functionality of the `metrics_decorator` function in the LLM (Large Language Models).
+
+The `metrics_decorator` function is a standard Python decorator that augments a specific function by wrapping extra functionality around it. It is commonly used for things like timing, logging or memoization.
+--
+The `metrics_decorator` in LLM is specially designed to measure and calculate three key performance metrics when generating language models:
+
+1. `Time to First Token`: Measures the elapsed time from the start of function execution until the generation of the first token.
+2. `Generation Latency`: It measures the total time taken for a complete run.
+3. `Throughput`: Calculates the rate of production of tokens per unit of time.
+
+```python
+def metrics_decorator(func: Callable):
+ """
+
+ Metrics decorator for LLM
+
+ Args:
+ func (Callable): The function to be decorated.
+
+ """
+
+ @wraps(func)
+ def wrapper(self, *args, **kwargs):
+ """
+ An inner function that wraps the decorated function. It calculates 'Time to First Token',
+ 'Generation Latency' and 'Throughput' metrics.
+
+ Args:
+ self : The object instance.
+ *args : Variable length argument list of the decorated function.
+ **kwargs : Arbitrary keyword arguments of the decorated function.
+ """
+
+ # Measure Time to First Token
+ start_time = time.time()
+ result = func(self, *args, **kwargs)
+ first_token_time = time.time()
+
+ # Measure Generation Latency
+ end_time = time.time()
+
+ # Calculate Throughput (assuming the function returns a list of tokens)
+ throughput = len(result) / (end_time - start_time)
+
+ return f"""
+ Time to First Token: {first_token_time - start_time}
+ Generation Latency: {end_time - start_time}
+ Throughput: {throughput}
+ """
+
+ return wrapper
+```
+## Example Usage
+Now let's discuss the usage of the `metrics_decorator` function with an example.
+
+Assuming that we have a language generation function called `text_generator()` that generates a list of tokens.
+
+```python
+@metrics_decorator
+def text_generator(self, text: str):
+ """
+ Args:
+ text (str): The input text.
+
+ Returns:
+ A list of tokens generated from the input text.
+ """
+ # language generation implementation goes here
+ return tokens
+
+# Instantiate the class and call the decorated function
+obj = ClassName()
+obj.text_generator("Hello, world!")
+```
+
+When the decorated `text_generator()` function is called, it will measure and return:
+
+- Time elapsed until the first token is generated.
+- The total execution time of the function.
+- The rate of tokens generation per unit time.
+
+This example provides a basic overview of how a function can be decorated with the `metrics_decorator`. The provided `func` argument could be any method from any class, as long as it complies with the structure defined in `metrics_decorator`. It is worth noting that the decorated function must return a list of tokens for the `Throughput` metric to work correctly.
+
+Remember, applying the `metrics_decorator` does not affect the original functionality of the decorated function, it just adds additional measurement and logging capabilities to it. It's a great utility for tracking and optimizing the performance of your language models.
diff --git a/docs/swarms/utils/pdf_to_text.md b/docs/swarms/utils/pdf_to_text.md
new file mode 100644
index 00000000..aecde1a9
--- /dev/null
+++ b/docs/swarms/utils/pdf_to_text.md
@@ -0,0 +1,71 @@
+# pdf_to_text
+
+## Introduction
+The function `pdf_to_text` is a Python utility for converting a PDF file into a string of text content. It leverages the `PyPDF2` library, an excellent Python library for processing PDF files. The function takes in a PDF file's path and reads its content, subsequently returning the extracted textual data.
+
+This function can be very useful when you want to extract textual information from PDF files automatically. For instance, when processing a large number of documents, performing textual analysis, or when you're dealing with text data that is only available in PDF format.
+
+## Class / Function Definition
+
+`pdf_to_text` is a standalone function defined as follows:
+
+```python
+def pdf_to_text(pdf_path: str) -> str:
+```
+
+## Parameters
+
+| Parameter | Type | Description |
+|:-:|---|---|
+| pdf_path | str | The path to the PDF file to be converted |
+
+## Returns
+
+| Return Value | Type | Description |
+|:-:|---|---|
+| text | str | The text extracted from the PDF file. |
+
+## Raises
+
+| Exception | Description |
+|---|---|
+| FileNotFoundError | If the PDF file is not found at the specified path. |
+| Exception | If there is an error in reading the PDF file. |
+
+## Function Description
+
+`pdf_to_text` utilises the `PdfReader` function from the `PyPDF2` library to read the PDF file. If the PDF file does not exist at the specified path or there was an error while reading the file, appropriate exceptions will be raised. It then iterates through each page in the PDF and uses the `extract_text` function to extract the text content from each page. These contents are then concatenated into a single variable and returned as the result.
+
+## Usage Examples
+
+To use this function, you first need to install the `PyPDF2` library. It can be installed via pip:
+
+```python
+!pip install pypdf2
+```
+
+Then, you should import the `pdf_to_text` function:
+
+```python
+from swarms.utils import pdf_to_text
+```
+
+Here is an example of how to use `pdf_to_text`:
+
+```python
+# Define the path to the pdf file
+pdf_path = 'sample.pdf'
+
+# Use the function to extract text
+text = pdf_to_text(pdf_path)
+
+# Print the extracted text
+print(text)
+```
+
+## Tips and Additional Information
+- Ensure that the PDF file path is valid and that the file exists at the specified location. If the file does not exist, a `FileNotFoundError` will be raised.
+- This function reads the text from the PDF. It does not handle images, graphical elements, or any non-text content.
+- If the PDF contains scanned images rather than textual data, the `extract_text` function may not be able to extract any text. In such cases, you would require OCR (Optical Character Recognition) tools to extract the text.
+- Be aware of the possibility that the output string might contain special characters or escape sequences because they were part of the PDF's content. You might need to clean the resulting text according to your requirements.
+- The function uses the PyPDF2 library to facilitate the PDF reading and text extraction. For any issues related to PDF manipulation, consult the [PyPDF2 library documentation](https://pythonhosted.org/PyPDF2/).
diff --git a/docs/swarms/utils/phoenix_tracer.md b/docs/swarms/utils/phoenix_tracer.md
deleted file mode 100644
index 97ed422a..00000000
--- a/docs/swarms/utils/phoenix_tracer.md
+++ /dev/null
@@ -1,128 +0,0 @@
-# Phoenix Trace Decorator Documentation
-
-## Introduction
-
-Welcome to the documentation for the `phoenix_trace_decorator` module. This module provides a convenient decorator for tracing Python functions and capturing exceptions using Phoenix, a versatile tracing and monitoring tool. Phoenix allows you to gain insights into the execution of your code, capture errors, and monitor performance.
-
-## Table of Contents
-
-1. [Installation](#installation)
-2. [Getting Started](#getting-started)
-3. [Decorator Usage](#decorator-usage)
-4. [Examples](#examples)
-5. [Best Practices](#best-practices)
-6. [References](#references)
-
-## 1. Installation
-
-Before using the `phoenix_trace_decorator`, you need to install the Swarms library. You can install Phoenix using pip:
-
-```bash
-pip install swarms
-```
-
-## 2. Getting Started
-
-Phoenix is a powerful tracing and monitoring tool, and the `phoenix_trace_decorator` simplifies the process of tracing functions and capturing exceptions within your Python code. To begin, ensure that Phoenix is installed, and then import the `phoenix_trace_decorator` module into your Python script.
-
-```python
-from swarms import phoenix_trace_decorator
-```
-
-## 3. Decorator Usage
-
-The `phoenix_trace_decorator` module provides a decorator, `phoenix_trace_decorator`, which can be applied to functions you want to trace. The decorator takes a single argument, a docstring that describes the purpose of the function being traced.
-
-Here is the basic structure of using the decorator:
-
-```python
-@phoenix_trace_decorator("Description of the function")
-def my_function(param1, param2):
- # Function implementation
- pass
-```
-
-## 4. Examples
-
-Let's explore some practical examples of using the `phoenix_trace_decorator` in your code.
-
-### Example 1: Basic Tracing
-
-In this example, we'll trace a simple function and print a message.
-
-```python
-@phoenix_trace_decorator("Tracing a basic function")
-def hello_world():
- print("Hello, World!")
-
-# Call the decorated function
-hello_world()
-```
-
-### Example 2: Tracing a Function with Parameters
-
-You can use the decorator with functions that have parameters.
-
-```python
-@phoenix_trace_decorator("Tracing a function with parameters")
-def add_numbers(a, b):
- result = a + b
- print(f"Result: {result}")
-
-# Call the decorated function with parameters
-add_numbers(2, 3)
-```
-
-### Example 3: Tracing Nested Calls
-
-The decorator can also trace nested function calls.
-
-```python
-@phoenix_trace_decorator("Outer function")
-def outer_function():
- print("Outer function")
-
- @phoenix_trace_decorator("Inner function")
- def inner_function():
- print("Inner function")
-
- inner_function()
-
-# Call the decorated functions
-outer_function()
-```
-
-### Example 4: Exception Handling
-
-Phoenix can capture exceptions and provide detailed information about them.
-
-```python
-@phoenix_trace_decorator("Function with exception handling")
-def divide(a, b):
- try:
- result = a / b
- except ZeroDivisionError as e:
- raise ValueError("Division by zero") from e
-
-# Call the decorated function with an exception
-try:
- divide(5, 0)
-except ValueError as e:
- print(f"Error: {e}")
-```
-
-## 5. Best Practices
-
-When using the `phoenix_trace_decorator`, consider the following best practices:
-
-- Use meaningful docstrings to describe the purpose of the traced functions.
-- Keep your tracing focused on critical parts of your code.
-- Make sure Phoenix is properly configured and running before using the decorator.
-
-## 6. References
-
-For more information on Phoenix and advanced usage, please refer to the [Phoenix Documentation](https://phoenix-docs.readthedocs.io/en/latest/).
-
----
-
-By following this documentation, you can effectively use the `phoenix_trace_decorator` to trace your Python functions, capture exceptions, and gain insights into the execution of your code. This tool is valuable for debugging, performance optimization, and monitoring the health of your applications.
\ No newline at end of file
diff --git a/docs/swarms/utils/prep_torch_inference.md b/docs/swarms/utils/prep_torch_inference.md
new file mode 100644
index 00000000..68598fa8
--- /dev/null
+++ b/docs/swarms/utils/prep_torch_inference.md
@@ -0,0 +1,102 @@
+# prep_torch_inference
+
+```python
+def prep_torch_inference(
+ model_path: str = None,
+ device: torch.device = None,
+ *args,
+ **kwargs,
+):
+ """
+ Prepare a Torch model for inference.
+
+ Args:
+ model_path (str): Path to the model file.
+ device (torch.device): Device to run the model on.
+ *args: Additional positional arguments.
+ **kwargs: Additional keyword arguments.
+
+ Returns:
+ torch.nn.Module: The prepared model.
+ """
+ try:
+ model = load_model_torch(model_path, device)
+ model.eval()
+ return model
+ except Exception as e:
+ # Add error handling code here
+ print(f"Error occurred while preparing Torch model: {e}")
+ return None
+```
+This method is part of the 'swarms.utils' module. It accepts a model file path and a torch device as input and returns a model that is ready for inference.
+
+## Detailed Functionality
+
+The method loads a PyTorch model from the file specified by `model_path`. This model is then moved to the specified `device` if it is provided. Subsequently, the method sets the model to evaluation mode by calling `model.eval()`. This is a crucial step when preparing a model for inference, as certain layers like dropout or batch normalization behave differently during training vs during evaluation.
+In the case of any exception (e.g., the model file not found or the device unavailable), it prints an error message and returns `None`.
+
+## Parameters
+
+| Parameter | Type | Description | Default |
+|-----------|------|-------------|---------|
+| model_path | str | Path to the model file. | None |
+| device | torch.device | Device to run the model on. | None |
+| args | tuple | Additional positional arguments. | None |
+| kwargs | dict | Additional keyword arguments. | None |
+
+## Returns
+
+| Type | Description |
+|------|-------------|
+| torch.nn.Module | The prepared model ready for inference. Returns `None` if any exception occurs. |
+
+## Usage Examples
+
+Here are some examples of how you can use the `prep_torch_inference` method. Before that, you need to import the necessary modules as follows:
+
+```python
+import torch
+from swarms.utils import prep_torch_inference, load_model_torch
+```
+
+### Example 1: Load a model for inference on CPU
+
+```python
+model_path = "saved_model.pth"
+model = prep_torch_inference(model_path)
+
+if model is not None:
+ print("Model loaded successfully and is ready for inference.")
+else:
+ print("Failed to load the model.")
+```
+
+### Example 2: Load a model for inference on CUDA device
+
+```python
+model_path = "saved_model.pth"
+device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
+model = prep_torch_inference(model_path, device)
+
+if model is not None:
+ print(f"Model loaded successfully on device {device} and is ready for inference.")
+else:
+ print("Failed to load the model.")
+```
+
+### Example 3: Load a model with additional arguments for `load_model_torch`
+
+```python
+model_path = "saved_model.pth"
+device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
+
+# Suppose load_model_torch accepts an additional argument, map_location
+model = prep_torch_inference(model_path, device, map_location=device)
+
+if model is not None:
+ print(f"Model loaded successfully on device {device} and is ready for inference.")
+else:
+ print("Failed to load the model.")
+```
+
+Please note, you need to ensure the given model path does exist and the device is available on your machine, else `prep_torch_inference` method will return `None`. Depending on the complexity and size of your models, loading them onto a specific device might take a while. So it's important that you take this into consideration when designing your machine learning workflows.
diff --git a/docs/swarms/utils/print_class_parameters.md b/docs/swarms/utils/print_class_parameters.md
new file mode 100644
index 00000000..3c09578f
--- /dev/null
+++ b/docs/swarms/utils/print_class_parameters.md
@@ -0,0 +1,110 @@
+# print_class_parameters
+
+# Module Function Name: print_class_parameters
+
+The `print_class_parameters` function is a utility function developed to help developers and users alike in retrieving and printing the parameters of a class constructor in Python, either in standard output or returned as a dictionary if the `api_format` is set to `True`.
+
+This utility function utilizes the `inspect` module to fetch the signature of the class constructor and fetches the parameters from the obtained signature. The parameter values and their respective types are then outputted.
+
+This function allows developers to easily inspect and understand the class' constructor parameters without the need to individually go through the class structure. This eases the testing and debugging process for developers and users alike, aiding in generating more efficient and readable code.
+
+__Function Definition:__
+
+```python
+def print_class_parameters(cls, api_format: bool = False):
+```
+__Parameters:__
+
+| Parameter | Type | Description | Default value |
+|---|---|---|---|
+| cls | type | The Python class to inspect. | None |
+| api_format | bool | Flag to determine if the output should be returned in dictionary format (if set to True) or printed out (if set to False) | False |
+
+__Functionality and Usage:__
+
+Inside the `print_class_parameters` function, it starts by getting the signature of the constructor of the inputted class by invoking `inspect.signature(cls.__init__)`. It then extracts the parameters from the signature and stores it in the `params` variable.
+
+If the `api_format` argument is set to `True`, instead of printing the parameters and their types, it stores them inside a dictionary where each key-value pair is a parameter name and its type. It then returns this dictionary.
+
+If `api_format` is set to `False` or not set at all (defaulting to False), the function iterates over the parameters and prints the parameter name and its type. "self" parameters are excluded from the output as they are inherent to all class methods in Python.
+
+A possible exception that may occur during the execution of this function is during the invocation of the `inspect.signature()` function call. If the inputted class does not have an `__init__` method or any error occurs during the retrieval of the class constructor's signature, an exception will be triggered. In that case, an error message that includes the error details is printed out.
+
+__Usage and Examples:__
+
+Assuming the existence of a class:
+
+```python
+class Agent:
+ def __init__(self, x: int, y: int):
+ self.x = x
+ self.y = y
+```
+
+One could use `print_class_parameters` in its typical usage:
+
+```python
+print_class_parameters(Agent)
+```
+
+Results in:
+
+```
+Parameter: x, Type:
+Parameter: y, Type:
+```
+
+Or, with `api_format` set to `True`
+
+```python
+output = print_class_parameters(Agent, api_format=True)
+print(output)
+```
+
+Results in:
+
+```
+{'x': "", 'y': ""}
+```
+
+__Note:__
+
+The function `print_class_parameters` is not limited to custom classes. It can inspect built-in Python classes such as `list`, `dict`, and others. However, it is most useful when inspecting custom-defined classes that aren't inherently documented in Python or third-party libraries.
+
+__Source Code__
+
+```python
+def print_class_parameters(cls, api_format: bool = False):
+ """
+ Print the parameters of a class constructor.
+
+ Parameters:
+ cls (type): The class to inspect.
+
+ Example:
+ >>> print_class_parameters(Agent)
+ Parameter: x, Type:
+ Parameter: y, Type:
+ """
+ try:
+ # Get the parameters of the class constructor
+ sig = inspect.signature(cls.__init__)
+ params = sig.parameters
+
+ if api_format:
+ param_dict = {}
+ for name, param in params.items():
+ if name == "self":
+ continue
+ param_dict[name] = str(param.annotation)
+ return param_dict
+
+ # Print the parameters
+ for name, param in params.items():
+ if name == "self":
+ continue
+ print(f"Parameter: {name}, Type: {param.annotation}")
+
+ except Exception as e:
+ print(f"An error occurred while inspecting the class: {e}")
+```
diff --git a/example.py b/example.py
index b3694542..26e0ac86 100644
--- a/example.py
+++ b/example.py
@@ -1,58 +1,34 @@
import os
+
from dotenv import load_dotenv
-from swarms.models import Gemini
+
+# Import the OpenAIChat model and the Agent struct
+from swarms.models import OpenAIChat
from swarms.structs import Agent
-# Load environment variables
+# Load the environment variables
load_dotenv()
# Get the API key from the environment
-api_key = os.environ.get("GOOGLE_API_KEY")
-if not api_key:
- raise ValueError("Gemini API key not found. Please set it in your .env file")
-
-# Initialize the first agent (Gemini Pro Vision) for image analysis
-agent_analysis = Agent(
- llm=Gemini(
- temperature=0.8,
- model_name="gemini-pro-vision",
- gemini_api_key=api_key
- ),
- max_loops=1,
- autosave=True,
- dashboard=True,
+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 second agent (Gemini Pro) for text-based tasks
-agent_instruction = Agent(
- llm=Gemini(
- temperature=0.8,
- model_name="gemini-pro",
- gemini_api_key=api_key
- ),
+## Initialize the workflow
+agent = Agent(
+ llm=llm,
max_loops=1,
autosave=True,
- dashboard=True,
+ dashboard=False,
+ streaming_on=True,
+ verbose=True,
)
-# Task for the first agent: Image Analysis
-task_analysis = """
-Analyze this logo and summarize its content, then give a structured list of suggested improvements.
-"""
-img = 'swarmslogobanner.png' # Replace with your image file name
-
-# Run the first agent on the task
-analysis_results = agent_analysis.run(task=task_analysis, img=img)
-
-# Task for the second agent: Instructions for a better logo
-task_instruction = f"""
-Based on the analysis and suggestions for the logo:
-{analysis_results}
-
-Generate detailed instructions for creating an improved version of this logo.
-"""
-
-# Run the second agent on the task
-instruction_results = agent_instruction.run(task=task_instruction)
-
-.............
\ No newline at end of file
+# Run the workflow on a task
+agent.run("Generate a 10,000 word blog on health and wellness.")
diff --git a/mkdocs.yml b/mkdocs.yml
index b0e34ca7..dd928dab 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -58,19 +58,14 @@ nav:
- Home:
- Overview: "index.md"
- Contributing: "contributing.md"
- - Docker Container Setup: "docker_setup.md"
- Swarms:
- Overview: "swarms/index.md"
- - swarms.swarms:
- - AbstractSwarm: "swarms/swarms/abstractswarm.md"
- - GodMode: "swarms/swarms/godmode.md"
- - Groupchat: "swarms/swarms/groupchat.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/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"
@@ -83,6 +78,7 @@ nav:
- vLLM: "swarms/models/vllm.md"
- MPT7B: "swarms/models/mpt.md"
- Mistral: "swarms/models/mistral.md"
+ - Mixtral: "swarms/models/mixtral.md"
- MultiModal:
- BaseMultiModalModel: "swarms/models/base_multimodal_model.md"
- Fuyu: "swarms/models/fuyu.md"
@@ -98,18 +94,57 @@ nav:
- ElevenLabsText2SpeechTool: "swarms/models/elevenlabs.md"
- OpenAITTS: "swarms/models/openai_tts.md"
- Gemini: "swarms/models/gemini.md"
+ - ZeroscopeTTV: "swarms/models/zeroscope.md"
- swarms.structs:
- - Overview: "swarms/structs/overview.md"
- - AutoScaler: "swarms/swarms/autoscaler.md"
- - Agent: "swarms/structs/agent.md"
- - SequentialWorkflow: 'swarms/structs/sequential_workflow.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.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:
- - Weaviate: "swarms/memory/weaviate.md"
- - PineconDB: "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:
- - phoenix_trace_decorator: "swarms/utils/phoenix_tracer.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:
@@ -138,4 +173,6 @@ nav:
- Architecture: "corporate/architecture.md"
- Checklist: "corporate/checklist.md"
- Hiring: "corporate/hiring.md"
- - SwarmCloud: "corporate/swarm_cloud.md"
\ No newline at end of file
+ - SwarmCloud: "corporate/swarm_cloud.md"
+ - SwarmMemo: "corporate/swarm_memo.md"
+ - Data Room: "corporate/data_room.md"
diff --git a/multi_modal_rag_agent.py b/multi_modal_rag_agent.py
new file mode 100644
index 00000000..b7944638
--- /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/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/multi_modal_auto_agent.py b/playground/agents/multi_modal_auto_agent_example.py
similarity index 100%
rename from multi_modal_auto_agent.py
rename to playground/agents/multi_modal_auto_agent_example.py
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/playground/agents/simple_agent.py b/playground/agents/simple_agent.py
deleted file mode 100644
index b30d93c8..00000000
--- a/playground/agents/simple_agent.py
+++ /dev/null
@@ -1,26 +0,0 @@
-from swarms.agents.simple_agent import SimpleAgent
-from swarms.structs import Agent
-from swarms.models import OpenAIChat
-
-api_key = ""
-
-llm = OpenAIChat(
- openai_api_key=api_key,
- temperature=0.5,
-)
-
-# Initialize the agent
-agent = Agent(
- llm=llm,
- max_loops=5,
-)
-
-
-agent = SimpleAgent(
- name="Optimus Prime",
- agent=agent,
- # Memory
-)
-
-out = agent.run("Generate a 10,000 word blog on health and wellness.")
-print(out)
diff --git a/playground/agents/simple_agent_example.py b/playground/agents/simple_agent_example.py
new file mode 100644
index 00000000..5d9d57ed
--- /dev/null
+++ b/playground/agents/simple_agent_example.py
@@ -0,0 +1,65 @@
+import os
+
+from dotenv import load_dotenv
+
+from swarms import (
+ OpenAIChat,
+ Conversation,
+ detect_markdown,
+ extract_code_from_markdown,
+)
+
+from swarms.tools.code_executor import CodeExecutor
+
+conv = Conversation(
+ autosave=False,
+ time_enabled=True,
+)
+
+# 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(openai_api_key=api_key)
+
+
+# Run the language model in a loop
+def interactive_conversation(llm, iters: int = 10):
+ conv = Conversation()
+ 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
+
+ # Run the language model
+ out = llm(task)
+ conv.add("assistant", out)
+ print(
+ f"Assistant: {out}",
+ )
+
+ # Code Interpreter
+ if detect_markdown(out):
+ code = extract_code_from_markdown(out)
+ if code:
+ print(f"Code: {code}")
+ executor = CodeExecutor()
+ out = executor.run(code)
+ conv.add("assistant", out)
+ # print(f"Assistant: {out}")
+
+ conv.display_conversation()
+ # conv.export_conversation("conversation.txt")
+
+
+# Replace with your LLM instance
+interactive_conversation(llm)
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/agents/tool_agent.py b/playground/agents/tool_agent.py
new file mode 100644
index 00000000..0a95f42c
--- /dev/null
+++ b/playground/agents/tool_agent.py
@@ -0,0 +1,42 @@
+# 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",
+ load_in_4bit=True,
+ device_map="auto",
+)
+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(
+ 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
+generated_data = agent.run(task)
+
+# Print the generated data
+print(f"Generated data: {generated_data}")
diff --git a/playground/agents/worker_example.py b/playground/agents/worker_example.py
new file mode 100644
index 00000000..9e215e83
--- /dev/null
+++ b/playground/agents/worker_example.py
@@ -0,0 +1,30 @@
+# 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",
+ human_in_the_loop=False,
+ 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/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 82%
rename from playground/demos/assembly/assembly.py
rename to playground/demos/assembly/assembly_example.py
index b82e075c..704c80d4 100644
--- a/playground/demos/assembly/assembly.py
+++ b/playground/demos/assembly/assembly_example.py
@@ -1,8 +1,5 @@
from swarms.structs import Agent
from swarms.models.gpt4_vision_api import GPT4VisionAPI
-from swarms.prompts.multi_modal_autonomous_instruction_prompt import (
- MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
-)
llm = GPT4VisionAPI()
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 c5f86416..baf8f091 100644
--- a/playground/demos/autotemp/autotemp_example.py
+++ b/playground/demos/autotemp/autotemp_example.py
@@ -1,22 +1,90 @@
-from swarms.models import OpenAIChat
-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_example.py b/playground/demos/fof/langchain_example.py
new file mode 100644
index 00000000..dd6d7083
--- /dev/null
+++ b/playground/demos/fof/langchain_example.py
@@ -0,0 +1,4 @@
+"""
+This tutorial shows you how to integrate swarms with Langchain
+
+"""
diff --git a/playground/demos/gemini_benchmarking/gemini_chat_example.py b/playground/demos/gemini_benchmarking/gemini_chat_example.py
new file mode 100644
index 00000000..6d9dc7ae
--- /dev/null
+++ b/playground/demos/gemini_benchmarking/gemini_chat_example.py
@@ -0,0 +1,28 @@
+import os
+from dotenv import load_dotenv
+from swarms.models.gemini import Gemini
+from swarms.prompts.react import react_prompt
+
+load_dotenv()
+
+api_key = os.environ["GEMINI_API_KEY"]
+
+# Establish the prompt and image
+task = "What is your name"
+img = "images/github-banner-swarms.png"
+
+# Initialize the model
+model = Gemini(
+ gemini_api_key=api_key,
+ model_name="gemini-pro",
+ max_tokens=1000,
+ system_prompt=react_prompt(task=task),
+ temperature=0.5,
+)
+
+
+out = model.chat(
+ "Create the code for a react component that displays a name",
+ img=img,
+)
+print(out)
diff --git a/playground/demos/gemini_benchmarking/gemini_react_example.py b/playground/demos/gemini_benchmarking/gemini_react_example.py
new file mode 100644
index 00000000..022405e9
--- /dev/null
+++ b/playground/demos/gemini_benchmarking/gemini_react_example.py
@@ -0,0 +1,28 @@
+import os
+from dotenv import load_dotenv
+from swarms.models.gemini import Gemini
+from swarms.prompts.react import react_prompt
+
+load_dotenv()
+
+api_key = os.environ["GEMINI_API_KEY"]
+
+# Establish the prompt and image
+task = "What is your name"
+img = "images/github-banner-swarms.png"
+
+# Initialize the model
+model = Gemini(
+ gemini_api_key=api_key,
+ model_name="gemini-pro",
+ max_tokens=1000,
+ system_prompt=react_prompt(task=task),
+ temperature=0.5,
+)
+
+
+# Run the model
+out = model.run(
+ "Create the code for a react component that displays a name"
+)
+print(out)
diff --git a/playground/demos/gemini_benchmarking/gemini_vcot_example.py b/playground/demos/gemini_benchmarking/gemini_vcot_example.py
new file mode 100644
index 00000000..86951bd5
--- /dev/null
+++ b/playground/demos/gemini_benchmarking/gemini_vcot_example.py
@@ -0,0 +1,28 @@
+import os
+
+from dotenv import load_dotenv
+
+from swarms.models import Gemini
+from swarms.prompts.visual_cot import VISUAL_CHAIN_OF_THOUGHT
+
+# Load the environment variables
+load_dotenv()
+
+# Get the API key from the environment
+api_key = os.environ.get("GEMINI_API_KEY")
+
+# Initialize the language model
+llm = Gemini(
+ gemini_api_key=api_key,
+ temperature=0.5,
+ max_tokens=1000,
+ system_prompt=VISUAL_CHAIN_OF_THOUGHT,
+)
+
+# Initialize the task
+task = "This is an eye test. What do you see?"
+img = "playground/demos/multi_modal_chain_of_thought/eyetest.jpg"
+
+# Run the workflow on a task
+out = llm.run(task=task, img=img)
+print(out)
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/langchain_example/langchain_example.py b/playground/demos/langchain_example/langchain_example.py
new file mode 100644
index 00000000..803e7857
--- /dev/null
+++ b/playground/demos/langchain_example/langchain_example.py
@@ -0,0 +1,26 @@
+import os
+from dotenv import load_dotenv
+from swarms import Agent
+from langchain.llms import OpenAIChat
+
+# Loading environment variables from .env file
+load_dotenv()
+
+# Initialize the model
+llm = OpenAIChat(
+ openai_api_key=os.getenv("OPENAI_API_KEY"),
+ max_tokens=1000,
+)
+
+# Initialize the agent
+agent = Agent(
+ llm=llm,
+ max_loops="auto",
+ autosave=True,
+ dashboard=False,
+ streaming_on=True,
+ verbose=True,
+)
+
+# Run the workflow on a task
+agent.run("Generate a 10,000 word blog on health and wellness.")
diff --git a/playground/demos/llm_with_conversation/main_example.py b/playground/demos/llm_with_conversation/main_example.py
new file mode 100644
index 00000000..a9e6c42a
--- /dev/null
+++ b/playground/demos/llm_with_conversation/main_example.py
@@ -0,0 +1,20 @@
+import os
+
+from dotenv import load_dotenv
+
+# Import the OpenAIChat model and the Agent struct
+from swarms.models import 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,
+)
diff --git a/playground/demos/logistics/logistics.py b/playground/demos/logistics/logistics_example.py
similarity index 78%
rename from playground/demos/logistics/logistics.py
rename to playground/demos/logistics/logistics_example.py
index 0e09f910..108ec702 100644
--- a/playground/demos/logistics/logistics.py
+++ b/playground/demos/logistics/logistics_example.py
@@ -12,7 +12,6 @@ from swarms.prompts.logistics import (
Efficiency_Agent_Prompt,
)
-# from swarms.utils.phoenix_handler import phoenix_trace_decorator
# from swarms.utils.banana_wrapper import banana
load_dotenv()
@@ -26,7 +25,6 @@ llm = GPT4VisionAPI(openai_api_key=api_key)
factory_image = "factory_image1.jpg"
# Initialize agents with respective prompts
-# @phoenix_trace_decorator("This function is an agent and is traced by Phoenix.")
health_security_agent = Agent(
llm=llm,
sop=Health_Security_Agent_Prompt,
@@ -34,7 +32,6 @@ health_security_agent = Agent(
multi_modal=True,
)
-# @phoenix_trace_decorator("This function is an agent and is traced by Phoenix.")
quality_control_agent = Agent(
llm=llm,
sop=Quality_Control_Agent_Prompt,
@@ -42,7 +39,6 @@ quality_control_agent = Agent(
multi_modal=True,
)
-# @phoenix_trace_decorator("This function is an agent and is traced by Phoenix.")
productivity_agent = Agent(
llm=llm,
sop=Productivity_Agent_Prompt,
@@ -50,17 +46,14 @@ productivity_agent = Agent(
multi_modal=True,
)
-# @phoenix_trace_decorator("This function is an agent and is traced by Phoenix.")
safety_agent = Agent(
llm=llm, sop=Safety_Agent_Prompt, max_loops=1, multi_modal=True
)
-# @phoenix_trace_decorator("This function is an agent and is traced by Phoenix.")
security_agent = Agent(
llm=llm, sop=Security_Agent_Prompt, max_loops=1, multi_modal=True
)
-# @phoenix_trace_decorator("This function is an agent and is traced by Phoenix.")
sustainability_agent = Agent(
llm=llm,
sop=Sustainability_Agent_Prompt,
@@ -68,7 +61,6 @@ sustainability_agent = Agent(
multi_modal=True,
)
-# @phoenix_trace_decorator("This function is an agent and is traced by Phoenix.")
efficiency_agent = Agent(
llm=llm,
sop=Efficiency_Agent_Prompt,
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 98%
rename from playground/demos/nutrition/nutrition.py
rename to playground/demos/nutrition/nutrition_example.py
index aca079ba..428560e3 100644
--- a/playground/demos/nutrition/nutrition.py
+++ b/playground/demos/nutrition/nutrition_example.py
@@ -2,7 +2,7 @@ import os
import base64
import requests
from dotenv import load_dotenv
-from swarms.models import Anthropic, OpenAIChat
+from swarms.models import OpenAIChat
from swarms.structs import Agent
# Load environment variables
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 76%
rename from playground/demos/optimize_llm_stack/vortex.py
rename to playground/demos/optimize_llm_stack/vortex_example.py
index 438c1451..5badb2fd 100644
--- a/playground/demos/optimize_llm_stack/vortex.py
+++ b/playground/demos/optimize_llm_stack/vortex_example.py
@@ -1,12 +1,10 @@
import os
-import subprocess
from dotenv import load_dotenv
from swarms.models import OpenAIChat
from swarms.structs import Agent
-# from swarms.utils.phoenix_handler import phoenix_trace_decorator
# import modal
load_dotenv()
@@ -23,9 +21,6 @@ llm = OpenAIChat(
# Agent
-# @phoenix_trace_decorator(
-# "This function is an agent and is traced by Phoenix."
-# )
# @stub.function(gpu="any")
agent = Agent(
llm=llm,
diff --git a/playground/demos/optimize_llm_stack/weaviate.py b/playground/demos/optimize_llm_stack/weaviate_example.py
similarity index 91%
rename from playground/demos/optimize_llm_stack/weaviate.py
rename to playground/demos/optimize_llm_stack/weaviate_example.py
index fa3bf96e..ad594547 100644
--- a/playground/demos/optimize_llm_stack/weaviate.py
+++ b/playground/demos/optimize_llm_stack/weaviate_example.py
@@ -1,6 +1,6 @@
-from swarms.memory import WeaviateClient
+from swarms.memory import WeaviateDB
-weaviate_client = WeaviateClient(
+weaviate_client = WeaviateDB(
http_host="YOUR_HTTP_HOST",
http_port="YOUR_HTTP_PORT",
http_secure=True,
diff --git a/playground/demos/personal_assistant/better_communication_example.py b/playground/demos/personal_assistant/better_communication_example.py
new file mode 100644
index 00000000..c6e79eb7
--- /dev/null
+++ b/playground/demos/personal_assistant/better_communication_example.py
@@ -0,0 +1,96 @@
+import time
+import os
+
+import pygame
+import speech_recognition as sr
+from dotenv import load_dotenv
+from playsound import playsound
+
+from swarms import OpenAIChat, OpenAITTS
+
+# Load the environment variables
+load_dotenv()
+
+# Get the API key from the environment
+openai_api_key = os.environ.get("OPENAI_API_KEY")
+
+# Initialize the language model
+llm = OpenAIChat(
+ openai_api_key=openai_api_key,
+)
+
+# Initialize the text-to-speech model
+tts = OpenAITTS(
+ model_name="tts-1-1106",
+ voice="onyx",
+ openai_api_key=openai_api_key,
+ saved_filepath="runs/tts_speech.wav",
+)
+
+# Initialize the speech recognition model
+r = sr.Recognizer()
+
+
+def play_audio(file_path):
+ # Check if the file exists
+ if not os.path.isfile(file_path):
+ print(f"Audio file {file_path} not found.")
+ return
+
+ # Initialize the mixer module
+ pygame.mixer.init()
+
+ try:
+ # Load the mp3 file
+ pygame.mixer.music.load(file_path)
+
+ # Play the mp3 file
+ pygame.mixer.music.play()
+
+ # Wait for the audio to finish playing
+ while pygame.mixer.music.get_busy():
+ pygame.time.Clock().tick(10)
+ except pygame.error as e:
+ print(f"Couldn't play {file_path}: {e}")
+ finally:
+ # Stop the mixer module and free resources
+ pygame.mixer.quit()
+
+
+while True:
+ # Listen for user speech
+ with sr.Microphone() as source:
+ print("Listening...")
+ audio = r.listen(source)
+
+ # Convert speech to text
+ try:
+ print("Recognizing...")
+ task = r.recognize_google(audio)
+ print(f"User said: {task}")
+ except sr.UnknownValueError:
+ print("Could not understand audio")
+ continue
+ except Exception as e:
+ print(f"Error: {e}")
+ continue
+
+ # Run the Gemini model on the task
+ print("Running GPT4 model...")
+ out = llm(task)
+ print(f"Gemini output: {out}")
+
+ # Convert the Gemini output to speech
+ print("Running text-to-speech model...")
+ out = tts.run_and_save(out)
+ print(f"Text-to-speech output: {out}")
+
+ # Ask the user if they want to play the audio
+ # play_audio = input("Do you want to play the audio? (yes/no): ")
+ # if play_audio.lower() == "yes":
+ # Initialize the mixer module
+ # Play the audio file
+
+ time.sleep(5)
+
+ playsound("runs/tts_speech.wav")
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/simple_rag/simple_rag.py b/playground/demos/simple_rag/simple_rag.py
new file mode 100644
index 00000000..129d59c4
--- /dev/null
+++ b/playground/demos/simple_rag/simple_rag.py
@@ -0,0 +1,30 @@
+from swarms import Agent, OpenAIChat, ChromaDB
+
+# Making an instance of the ChromaDB class
+memory = ChromaDB(
+ metric="cosine",
+ n_results=3,
+ output_dir="results",
+ docs_folder="docs",
+)
+
+# Initializing the agent with the Gemini instance and other parameters
+agent = Agent(
+ agent_name="Covid-19-Chat",
+ agent_description=(
+ "This agent provides information about COVID-19 symptoms."
+ ),
+ llm=OpenAIChat(),
+ max_loops="auto",
+ autosave=True,
+ verbose=True,
+ long_term_memory=memory,
+ stopping_condition="finish",
+)
+
+# Defining the task and image path
+task = ("What are the symptoms of COVID-19?",)
+
+# Running the agent with the specified task and image
+out = agent.run(task)
+print(out)
diff --git a/playground/demos/simple_rag/simple_rag_text.py b/playground/demos/simple_rag/simple_rag_text.py
new file mode 100644
index 00000000..8e8e3cb0
--- /dev/null
+++ b/playground/demos/simple_rag/simple_rag_text.py
@@ -0,0 +1,24 @@
+# Text embeddings, image embeddings, and multimodal embeddings
+# Add text and image embeddings into postgresl database
+
+from swarms.models.jina_embeds import JinaEmbeddings
+from swarms.models.gigabind import Gigabind
+
+# Model
+model = JinaEmbeddings(
+ max_length=8192,
+ device="cuda",
+ quantize=True,
+ huggingface_api_key="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",
+)
+
+
+# Encode text
+
+embeddings = model("Encode this super long document text")
+
+
+# Embed images or text
+model = Gigabind()
+
+multi_modal_embeddings = model(text=[text], imgs=[img1, img2, img3])
diff --git a/playground/demos/swarm_of_mma_manufacturing/main.py b/playground/demos/swarm_of_mma_manufacturing/main_example.py
similarity index 98%
rename from playground/demos/swarm_of_mma_manufacturing/main.py
rename to playground/demos/swarm_of_mma_manufacturing/main_example.py
index 37938608..05b0e8e5 100644
--- a/playground/demos/swarm_of_mma_manufacturing/main.py
+++ b/playground/demos/swarm_of_mma_manufacturing/main_example.py
@@ -20,7 +20,6 @@ from termcolor import colored
from swarms.models import GPT4VisionAPI
from swarms.structs import Agent
-from swarms.utils.phoenix_handler import phoenix_trace_decorator
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
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_example.py b/playground/demos/visuo/text_to_sql_agent_example.py
new file mode 100644
index 00000000..67f53e97
--- /dev/null
+++ b/playground/demos/visuo/text_to_sql_agent_example.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/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 51%
rename from playground/models/idefics.py
rename to playground/models/idefics_example.py
index 39d6f4eb..ea36ba77 100644
--- a/playground/models/idefics.py
+++ b/playground/models/idefics_example.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/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.py
deleted file mode 100644
index ce39a710..00000000
--- a/playground/models/kosmos2.py
+++ /dev/null
@@ -1,10 +0,0 @@
-from swarms.models.kosmos2 import Kosmos2, Detections
-from PIL import Image
-
-
-model = Kosmos2.initialize()
-
-image = Image.open("images/swarms.jpg")
-
-detections = model(image)
-print(detections)
diff --git a/playground/models/kosmos_example.py b/playground/models/kosmos_example.py
new file mode 100644
index 00000000..dbfd108f
--- /dev/null
+++ b/playground/models/kosmos_example.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)
diff --git a/playground/models/kosmos_two.py b/playground/models/kosmos_two.py
deleted file mode 100644
index 8bf583cd..00000000
--- a/playground/models/kosmos_two.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/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_example.py b/playground/models/llava_example.py
new file mode 100644
index 00000000..561b6f88
--- /dev/null
+++ b/playground/models/llava_example.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/playground/models/miqu.py b/playground/models/miqu.py
new file mode 100644
index 00000000..f3fc1b4d
--- /dev/null
+++ b/playground/models/miqu.py
@@ -0,0 +1,13 @@
+from swarms import Mistral
+
+
+# Initialize the model
+model = Mistral(
+ model_name="miqudev/miqu-1-70b",
+ max_length=500,
+ use_flash_attention=True,
+ load_in_4bit=True,
+)
+
+# Run the model
+result = model.run("What is the meaning of life?")
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/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/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/docs/old-docs/README.md b/playground/models/tocr_example.py
similarity index 100%
rename from docs/old-docs/README.md
rename to playground/models/tocr_example.py
diff --git a/playground/models/together_example.py b/playground/models/together_example.py
new file mode 100644
index 00000000..f730f72f
--- /dev/null
+++ b/playground/models/together_example.py
@@ -0,0 +1,12 @@
+from swarms import TogetherLLM
+
+# Initialize the model with your parameters
+model = TogetherLLM(
+ 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."
+)
diff --git a/playground/models/tts_speech.py b/playground/models/tts_speech.py
deleted file mode 100644
index f8ce3470..00000000
--- a/playground/models/tts_speech.py
+++ /dev/null
@@ -1,10 +0,0 @@
-from swarms import OpenAITTS
-
-tts = OpenAITTS(
- model_name="tts-1-1106",
- voice="onyx",
- openai_api_key="YOUR_API_KEY",
-)
-
-out = tts.run_and_save("pliny is a girl and a chicken")
-print(out)
diff --git a/playground/models/tts_speech_example.py b/playground/models/tts_speech_example.py
new file mode 100644
index 00000000..be38912c
--- /dev/null
+++ b/playground/models/tts_speech_example.py
@@ -0,0 +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=os.getenv("OPENAI_API_KEY"),
+)
+
+out = tts.run_and_save("Dammmmmm those tacos were good")
+print(out)
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_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/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/swarms/chat.py b/playground/structs/chat_example.py
similarity index 100%
rename from playground/swarms/chat.py
rename to playground/structs/chat_example.py
diff --git a/playground/structs/company_example.py b/playground/structs/company_example.py
new file mode 100644
index 00000000..72396c61
--- /dev/null
+++ b/playground/structs/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/playground/structs/concurrent_workflow_example.py b/playground/structs/concurrent_workflow_example.py
new file mode 100644
index 00000000..98531388
--- /dev/null
+++ b/playground/structs/concurrent_workflow_example.py
@@ -0,0 +1,29 @@
+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)
+
+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, task)
+task2 = Task(agent, task)
+task3 = Task(agent, task)
+
+# Add tasks to the workflow
+workflow.add(tasks=[task1, task2, task3])
+
+# Run the workflow
+workflow.run()
diff --git a/playground/swarms/debate.py b/playground/structs/debate_example.py
similarity index 100%
rename from playground/swarms/debate.py
rename to playground/structs/debate_example.py
diff --git a/playground/swarms/dialogue_simulator.py b/playground/structs/dialogue_simulator_example.py
similarity index 100%
rename from playground/swarms/dialogue_simulator.py
rename to playground/structs/dialogue_simulator_example.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/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_example.py b/playground/structs/godmode_example.py
new file mode 100644
index 00000000..46f71393
--- /dev/null
+++ b/playground/structs/godmode_example.py
@@ -0,0 +1,33 @@
+import os
+
+from dotenv import load_dotenv
+
+from swarms.models import Anthropic, Gemini, Mixtral, OpenAIChat
+from swarms.swarms import ModelParallelizer
+
+load_dotenv()
+
+# API Keys
+anthropic_api_key = os.getenv("ANTHROPIC_API_KEY")
+openai_api_key = os.getenv("OPENAI_API_KEY")
+gemini_api_key = os.getenv("GEMINI_API_KEY")
+
+# Initialize the models
+llm = OpenAIChat(openai_api_key=openai_api_key)
+anthropic = Anthropic(anthropic_api_key=anthropic_api_key)
+mixtral = Mixtral()
+gemini = Gemini(gemini_api_key=gemini_api_key)
+
+# Initialize the parallelizer
+llms = [llm, anthropic, mixtral, gemini]
+parallelizer = ModelParallelizer(llms)
+
+# Set the task
+task = "Generate a 10,000 word blog on health and wellness."
+
+# Run the task
+out = parallelizer.run(task)
+
+# Print the responses 1 by 1
+for i in range(len(out)):
+ print(f"Response from LLM {i}: {out[i]}")
diff --git a/playground/swarms/groupchat.py b/playground/structs/groupchat_example.py
similarity index 93%
rename from playground/swarms/groupchat.py
rename to playground/structs/groupchat_example.py
index f53257c7..b9ab5761 100644
--- a/playground/swarms/groupchat.py
+++ b/playground/structs/groupchat_example.py
@@ -1,5 +1,5 @@
from swarms import OpenAI, Agent
-from swarms.swarms.groupchat import GroupChatManager, GroupChat
+from swarms.structs.groupchat import GroupChatManager, GroupChat
api_key = ""
diff --git a/playground/swarms/gui_app.py b/playground/structs/gui_app_example.py
similarity index 100%
rename from playground/swarms/gui_app.py
rename to playground/structs/gui_app_example.py
diff --git a/playground/swarms/multi_agent_collab.py b/playground/structs/multi_agent_collab_example.py
similarity index 100%
rename from playground/swarms/multi_agent_collab.py
rename to playground/structs/multi_agent_collab_example.py
diff --git a/playground/swarms/multi_agent_debate.py b/playground/structs/multi_agent_debate_example.py
similarity index 100%
rename from playground/swarms/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/swarms/orchestrate.py b/playground/structs/orchestrate_example.py
similarity index 100%
rename from playground/swarms/orchestrate.py
rename to playground/structs/orchestrate_example.py
diff --git a/playground/swarms/orchestrator.py b/playground/structs/orchestrator_example.py
similarity index 100%
rename from playground/swarms/orchestrator.py
rename to playground/structs/orchestrator_example.py
diff --git a/playground/structs/recursive_example.py b/playground/structs/recursive_example.py
new file mode 100644
index 00000000..9760b606
--- /dev/null
+++ b/playground/structs/recursive_example.py
@@ -0,0 +1,26 @@
+import os
+from dotenv import load_dotenv
+from swarms import OpenAIChat, Task, RecursiveWorkflow, 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 = RecursiveWorkflow(stop_token="")
+
+# 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/playground/structs/sequential_workflow.py b/playground/structs/sequential_workflow.py
deleted file mode 100644
index fa7ca16a..00000000
--- a/playground/structs/sequential_workflow.py
+++ /dev/null
@@ -1,33 +0,0 @@
-from swarms.models import OpenAIChat
-from swarms.structs import Agent
-from swarms.structs.sequential_workflow import SequentialWorkflow
-
-# Example usage
-llm = OpenAIChat(
- temperature=0.5,
- max_tokens=3000,
-)
-
-# Initialize the Agent with the language agent
-flow1 = Agent(llm=llm, max_loops=1, dashboard=False)
-
-# Create another Agent for a different task
-flow2 = Agent(llm=llm, max_loops=1, dashboard=False)
-
-# 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
-)
-
-# Suppose the next task takes the output of the first task as input
-workflow.add("Summarize the generated blog", flow2)
-
-# 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
new file mode 100644
index 00000000..7fa110bc
--- /dev/null
+++ b/playground/structs/sequential_workflow_example.py
@@ -0,0 +1,48 @@
+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/swarms/social_app.py b/playground/structs/social_app_example.py
similarity index 100%
rename from playground/swarms/social_app.py
rename to playground/structs/social_app_example.py
diff --git a/playground/structs/swarm_network_example.py b/playground/structs/swarm_network_example.py
new file mode 100644
index 00000000..de9c53b6
--- /dev/null
+++ b/playground/structs/swarm_network_example.py
@@ -0,0 +1,46 @@
+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(
+ agent2.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)
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/structs/task_example.py b/playground/structs/task_example.py
new file mode 100644
index 00000000..c2ade96a
--- /dev/null
+++ b/playground/structs/task_example.py
@@ -0,0 +1,53 @@
+import os
+
+from dotenv import load_dotenv
+
+from swarms.structs import Agent, OpenAIChat, Task
+
+# Load the environment variables
+load_dotenv()
+
+
+# Define a function to be used as the action
+def my_action():
+ print("Action executed")
+
+
+# Define a function to be used as the condition
+def my_condition():
+ print("Condition checked")
+ return True
+
+
+# Create an agent
+agent = Agent(
+ llm=OpenAIChat(openai_api_key=os.environ["OPENAI_API_KEY"]),
+ max_loops=1,
+ dashboard=False,
+)
+
+# Create a task
+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)
+task.set_condition(my_condition)
+
+# Execute the task
+print("Executing task...")
+task.run()
+
+# Check if the task is completed
+if task.is_completed():
+ print("Task completed")
+else:
+ print("Task not completed")
+
+# Output the result of the task
+print(f"Task result: {task.result}")
diff --git a/playground/swarms/todo_app.py b/playground/structs/todo_app_example.py
similarity index 100%
rename from playground/swarms/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/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/swarms/godmode.py b/playground/swarms/godmode.py
deleted file mode 100644
index f1269d98..00000000
--- a/playground/swarms/godmode.py
+++ /dev/null
@@ -1,16 +0,0 @@
-from swarms.swarms import GodMode
-from swarms.models import OpenAIChat
-
-api_key = ""
-
-llm = OpenAIChat(openai_api_key=api_key)
-
-
-llms = [llm, llm, llm]
-
-god_mode = GodMode(llms)
-
-task = "Generate a 10,000 word blog on health and wellness."
-
-out = god_mode.run(task)
-god_mode.print_responses(task)
diff --git a/playground/tools/agent_with_tools.py b/playground/tools/agent_with_tools_example.py
similarity index 94%
rename from playground/tools/agent_with_tools.py
rename to playground/tools/agent_with_tools_example.py
index ee4a8ef7..3bad0b1d 100644
--- a/playground/tools/agent_with_tools.py
+++ b/playground/tools/agent_with_tools_example.py
@@ -1,7 +1,6 @@
import os
from swarms.models import OpenAIChat
from swarms.structs import Agent
-from swarms.tools.tool import tool
from dotenv import load_dotenv
load_dotenv()
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/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 7022250b..2623bf8f 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -4,15 +4,15 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "swarms"
-version = "2.9.2"
+version = "4.1.3"
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"]
+keywords = ["artificial intelligence", "deep learning", "optimizers", "Prompt Engineering", "swarms", "agents"]
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
@@ -21,50 +21,59 @@ classifiers = [
"Programming Language :: Python :: 3.10"
]
+
[tool.poetry.dependencies]
python = "^3.6.1"
torch = "2.1.1"
-transformers = "4.35.0"
+transformers = "4.37.1"
openai = "0.28.0"
langchain = "0.0.333"
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"
backoff = "2.2.1"
-marshmallow = "3.19.0"
-datasets = "2.10.1"
+datasets = "*"
optimum = "1.15.0"
-diffusers = "0.17.1"
+diffusers = "*"
+toml = "*"
PyPDF2 = "3.0.1"
-accelerate = "0.22.0"
+accelerate = "*"
+anthropic = "*"
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 = "0.16.4"
+huggingface-hub = "*"
pydantic = "1.10.12"
tenacity = "8.2.2"
Pillow = "9.4.0"
-chromadb = "0.4.14"
-tabulate = "0.9.0"
+chromadb = "*"
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"
+sqlalchemy = "*"
+bitsandbytes = "*"
+pgvector = "*"
+qdrant-client = "*"
+sentence-transformers = "*"
+peft = "*"
+psutil = "*"
+ultralytics = "*"
+timm = "*"
+supervision = "*"
+scikit-image = "*"
+pinecone-client = "*"
+roboflow = "*"
+
[tool.poetry.group.lint.dependencies]
@@ -93,3 +102,5 @@ target-version = ['py38']
preview = true
+[tool.poetry.scripts]
+swarms = 'swarms.cli._cli:main'
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index f020827a..e6496205 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,65 +1,38 @@
torch==2.1.1
-transformers>2.10==4.35.0
+transformers
pandas==1.5.3
langchain==0.0.333
-nest_asyncio==1.5.6
langchain-experimental==0.0.10
-playwright==1.34.0
-wget==3.2
-simpleaichat==0.2.2
httpx==0.24.1
-open_clip_torch==2.20.0
-ggl==1.1.0
-beautifulsoup4==4.11.2
-google-search-results
Pillow==9.4.0
faiss-cpu==1.7.4
openai==0.28.0
-attrs==22.2.0
-datasets==2.10.1
+datasets==2.14.5
pydantic==1.10.12
-soundfile==0.12.1
-arize-phoenix
-weaviate-client==3.25.3
-huggingface-hub==0.16.4
+bitsandbytes
+huggingface-hub
google-generativeai==0.3.1
sentencepiece==0.1.98
+requests_mock
PyPDF2==3.0.1
accelerate==0.22.0
-vllm
-chromadb==0.4.14
-tensorflow==2.12.0
+chromadb
+tensorflow
optimum
+toml
tiktoken==0.4.0
-tabulate==0.9.0
colored
addict
backoff==2.2.1
ratelimit==2.2.1
-albumentations
-basicsr
termcolor==2.2.0
-controlnet-aux
-diffusers==0.17.1
+diffusers
einops==0.7.0
-imageio==2.25.1
opencv-python-headless==4.8.1.78
-imageio-ffmpeg==0.4.9
-invisible-watermark
-kornia
-safetensors==0.3.3
-numpy==1.25.2
-omegaconf==2.3.0
-open_clip_torch==2.20.0
+numpy
openai==0.28.0
opencv-python==4.7.0.72
-prettytable==3.9.0
-safetensors==0.3.3
-test-tube
-timm==0.6.13
-torchmetrics
-webdataset
-marshmallow==3.19.0
+timm
yapf
autopep8
cohere==4.24
@@ -69,3 +42,11 @@ mkdocs
mkdocs-material
mkdocs-glightbox
pre-commit==3.2.2
+peft
+psutil
+ultralytics
+supervision
+scikit-image
+anthropic
+pinecone-client
+roboflow
\ No newline at end of file
diff --git a/scripts/auto_docs.py b/scripts/auto_docs.py
new file mode 100644
index 00000000..f469e9ec
--- /dev/null
+++ b/scripts/auto_docs.py
@@ -0,0 +1,94 @@
+###### VERISON2
+import inspect
+import os
+import threading
+
+from dotenv import load_dotenv
+
+from scripts.auto_tests_docs.docs import DOCUMENTATION_WRITER_SOP
+from swarms import OpenAIChat
+
+##########
+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()
+
+api_key = os.getenv("OPENAI_API_KEY")
+
+model = OpenAIChat(
+ openai_api_key=api_key,
+ max_tokens=4000,
+)
+
+
+def process_documentation(cls):
+ """
+ Process the documentation for a given class using OpenAI model and save it in a Markdown file.
+ """
+ doc = inspect.getdoc(cls)
+ source = inspect.getsource(cls)
+ input_content = (
+ "Class Name:"
+ f" {cls.__name__}\n\nDocumentation:\n{doc}\n\nSource"
+ f" Code:\n{source}"
+ )
+
+ # 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.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/tokenizers"
+ os.makedirs(dir_path, exist_ok=True)
+
+ # Write the processed documentation to a Markdown file
+ file_path = os.path.join(dir_path, f"{cls.__name__.lower()}.md")
+ with open(file_path, "w") as file:
+ file.write(doc_content)
+
+ print(f"Documentation generated for {cls.__name__}.")
+
+
+def main():
+ classes = [
+ SentencePieceTokenizer,
+ HuggingFaceTokenizer,
+ Tokenizer,
+ BaseTokenizer,
+ OpenAITokenizer,
+ AnthropicTokenizer,
+ CohereTokenizer,
+ ]
+ threads = []
+ for cls in classes:
+ thread = threading.Thread(
+ target=process_documentation, args=(cls,)
+ )
+ threads.append(thread)
+ thread.start()
+
+ # Wait for all threads to complete
+ for thread in threads:
+ thread.join()
+
+ print("Documentation generated in 'swarms.tokenizers' directory.")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/auto_tests_docs/auto_docs_functions.py b/scripts/auto_tests_docs/auto_docs_functions.py
new file mode 100644
index 00000000..37bf376d
--- /dev/null
+++ b/scripts/auto_tests_docs/auto_docs_functions.py
@@ -0,0 +1,77 @@
+import inspect
+import os
+import sys
+import threading
+
+from dotenv import load_dotenv
+
+from scripts.auto_tests_docs.docs import DOCUMENTATION_WRITER_SOP
+from swarms import OpenAIChat
+
+load_dotenv()
+
+api_key = os.getenv("OPENAI_API_KEY")
+
+model = OpenAIChat(
+ model_name="gpt-4",
+ openai_api_key=api_key,
+ max_tokens=4000,
+)
+
+
+def process_documentation(item):
+ """
+ Process the documentation for a given function using OpenAI model and save it in a Markdown file.
+ """
+ doc = inspect.getdoc(item)
+ source = inspect.getsource(item)
+ input_content = (
+ f"Name: {item.__name__}\n\nDocumentation:\n{doc}\n\nSource"
+ f" Code:\n{source}"
+ )
+ print(input_content)
+
+ # Process with OpenAI model
+ processed_content = model(
+ DOCUMENTATION_WRITER_SOP(input_content, "swarms.utils")
+ )
+
+ doc_content = f"# {item.__name__}\n\n{processed_content}\n"
+
+ # Create the directory if it doesn't exist
+ dir_path = "docs/swarms/utils"
+ os.makedirs(dir_path, exist_ok=True)
+
+ # Write the processed documentation to a Markdown file
+ file_path = os.path.join(dir_path, f"{item.__name__.lower()}.md")
+ with open(file_path, "w") as file:
+ file.write(doc_content)
+
+
+def main():
+ # Gathering all functions from the swarms.utils module
+ functions = [
+ obj
+ for name, obj in inspect.getmembers(
+ sys.modules["swarms.utils"]
+ )
+ if inspect.isfunction(obj)
+ ]
+
+ threads = []
+ for func in functions:
+ thread = threading.Thread(
+ target=process_documentation, args=(func,)
+ )
+ threads.append(thread)
+ thread.start()
+
+ # Wait for all threads to complete
+ for thread in threads:
+ thread.join()
+
+ print("Documentation generated in 'docs/swarms/utils' directory.")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/auto_tests_docs/auto_docs_omni.py b/scripts/auto_tests_docs/auto_docs_omni.py
new file mode 100644
index 00000000..3ae647a7
--- /dev/null
+++ b/scripts/auto_tests_docs/auto_docs_omni.py
@@ -0,0 +1,84 @@
+import inspect
+import os
+import threading
+
+from dotenv import load_dotenv
+from scripts.auto_tests_docs.docs import DOCUMENTATION_WRITER_SOP
+from swarms import OpenAIChat
+
+
+###########
+
+
+###############
+
+load_dotenv()
+
+api_key = os.getenv("OPENAI_API_KEY")
+
+model = OpenAIChat(
+ model_name="gpt-4-1106-preview",
+ openai_api_key=api_key,
+ max_tokens=4000,
+)
+
+
+def process_documentation(
+ item,
+ module: str = "swarms.structs",
+ docs_folder_path: str = "docs/swarms/structs",
+):
+ """
+ Process the documentation for a given class or function using OpenAI model and save it in a Python file.
+ """
+ doc = inspect.getdoc(item)
+ source = inspect.getsource(item)
+ is_class = inspect.isclass(item)
+ item_type = "Class Name" if is_class else "Name"
+ input_content = (
+ f"{item_type}:"
+ f" {item.__name__}\n\nDocumentation:\n{doc}\n\nSource"
+ f" Code:\n{source}"
+ )
+
+ # Process with OpenAI model
+ processed_content = model(
+ DOCUMENTATION_WRITER_SOP(input_content, module)
+ )
+
+ doc_content = f"# {item.__name__}\n\n{processed_content}\n"
+
+ # Create the directory if it doesn't exist
+ dir_path = docs_folder_path
+ os.makedirs(dir_path, exist_ok=True)
+
+ # Write the processed documentation to a Python file
+ file_path = os.path.join(dir_path, f"{item.__name__.lower()}.md")
+ with open(file_path, "w") as file:
+ file.write(doc_content)
+
+ print(
+ f"Processed documentation for {item.__name__}. at {file_path}"
+ )
+
+
+def main(module: str = "docs/swarms/structs"):
+ items = []
+
+ threads = []
+ for item in items:
+ thread = threading.Thread(
+ target=process_documentation, args=(item,)
+ )
+ threads.append(thread)
+ thread.start()
+
+ # Wait for all threads to complete
+ for thread in threads:
+ thread.join()
+
+ print(f"Documentation generated in {module} directory.")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/auto_tests_docs/auto_tests.py b/scripts/auto_tests_docs/auto_tests.py
new file mode 100644
index 00000000..87d891d2
--- /dev/null
+++ b/scripts/auto_tests_docs/auto_tests.py
@@ -0,0 +1,110 @@
+import inspect
+import os
+import re
+import threading
+from swarms import OpenAIChat
+from scripts.auto_tests_docs.docs import TEST_WRITER_SOP_PROMPT
+
+#########
+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
+
+########
+from dotenv import load_dotenv
+
+load_dotenv()
+
+api_key = os.getenv("OPENAI_API_KEY")
+
+model = OpenAIChat(
+ model_name="gpt-4",
+ openai_api_key=api_key,
+ max_tokens=4000,
+)
+
+
+def extract_code_from_markdown(markdown_content: str):
+ """
+ Extracts code blocks from a Markdown string and returns them as a single string.
+
+ Args:
+ - markdown_content (str): The Markdown content as a string.
+
+ Returns:
+ - str: A single string containing all the code blocks separated by newlines.
+ """
+ # Regular expression for fenced code blocks
+ pattern = r"```(?:\w+\n)?(.*?)```"
+ matches = re.findall(pattern, markdown_content, re.DOTALL)
+
+ # Concatenate all code blocks separated by newlines
+ return "\n".join(code.strip() for code in matches)
+
+
+def create_test(cls):
+ """
+ Process the documentation for a given class using OpenAI model and save it in a Python file.
+ """
+ doc = inspect.getdoc(cls)
+ source = inspect.getsource(cls)
+ input_content = (
+ "Class Name:"
+ f" {cls.__name__}\n\nDocumentation:\n{doc}\n\nSource"
+ f" Code:\n{source}"
+ )
+ print(input_content)
+
+ # 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, "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/tokenizers"
+ os.makedirs(dir_path, exist_ok=True)
+
+ # Write the processed documentation to a Python file
+ file_path = os.path.join(dir_path, f"{cls.__name__.lower()}.py")
+ with open(file_path, "w") as file:
+ file.write(doc_content)
+
+
+def main():
+ classes = [
+ SentencePieceTokenizer,
+ HuggingFaceTokenizer,
+ Tokenizer,
+ BaseTokenizer,
+ OpenAITokenizer,
+ AnthropicTokenizer,
+ CohereTokenizer,
+ ]
+ threads = []
+ for cls in classes:
+ thread = threading.Thread(target=create_test, args=(cls,))
+ threads.append(thread)
+ thread.start()
+
+ # Wait for all threads to complete
+ for thread in threads:
+ thread.join()
+
+ print("Tests generated in 'tests/tokenizers' directory.")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/auto_tests_docs/auto_tests_functions.py b/scripts/auto_tests_docs/auto_tests_functions.py
new file mode 100644
index 00000000..4fa2fafd
--- /dev/null
+++ b/scripts/auto_tests_docs/auto_tests_functions.py
@@ -0,0 +1,82 @@
+import inspect
+import os
+import sys
+import threading
+
+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
+
+load_dotenv()
+
+api_key = os.getenv("OPENAI_API_KEY")
+
+model = OpenAIChat(
+ model_name="gpt-4",
+ openai_api_key=api_key,
+ max_tokens=4000,
+)
+
+
+def process_documentation(item):
+ """
+ Process the documentation for a given function using OpenAI model and save it in a Markdown file.
+ """
+ doc = inspect.getdoc(item)
+ source = inspect.getsource(item)
+ input_content = (
+ f"Name: {item.__name__}\n\nDocumentation:\n{doc}\n\nSource"
+ f" Code:\n{source}"
+ )
+ # print(input_content)
+
+ # Process with OpenAI model
+ processed_content = model(
+ TEST_WRITER_SOP_PROMPT(
+ input_content, "swarms.utils", "swarms.utils"
+ )
+ )
+ processed_content = extract_code_from_markdown(processed_content)
+ print(processed_content)
+
+ doc_content = f"{processed_content}"
+
+ # Create the directory if it doesn't exist
+ dir_path = "tests/utils"
+ os.makedirs(dir_path, exist_ok=True)
+
+ # Write the processed documentation to a Markdown file
+ file_path = os.path.join(dir_path, f"{item.__name__.lower()}.py")
+ with open(file_path, "w") as file:
+ file.write(doc_content)
+
+
+def main():
+ # Gathering all functions from the swarms.utils module
+ functions = [
+ obj
+ for name, obj in inspect.getmembers(
+ sys.modules["swarms.utils"]
+ )
+ if inspect.isfunction(obj)
+ ]
+
+ threads = []
+ for func in functions:
+ thread = threading.Thread(
+ target=process_documentation, args=(func,)
+ )
+ threads.append(thread)
+ thread.start()
+
+ # Wait for all threads to complete
+ for thread in threads:
+ thread.join()
+
+ print("Tests generated in 'tests/utils' directory.")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/scripts/auto_tests_docs/docs.py b/scripts/auto_tests_docs/docs.py
new file mode 100644
index 00000000..01df9d71
--- /dev/null
+++ b/scripts/auto_tests_docs/docs.py
@@ -0,0 +1,201 @@
+def DOCUMENTATION_WRITER_SOP(
+ task: str,
+ module: str,
+):
+ documentation = f"""Create multi-page long and explicit professional pytorch-like documentation for the {module} code below follow the outline for the {module} library,
+ provide many examples and teach the user about the code, provide examples for every function, make the documentation 10,000 words,
+ provide many usage examples and note this is markdown docs, create the documentation for the code to document,
+ put the arguments and methods in a table in markdown to make it visually seamless
+
+ Now make the professional documentation for this code, provide the architecture and how the class works and why it works that way,
+ it's purpose, provide args, their types, 3 ways of usage examples, in examples show all the code like imports main example etc
+
+ BE VERY EXPLICIT AND THOROUGH, MAKE IT DEEP AND USEFUL
+
+ ########
+ Step 1: Understand the purpose and functionality of the module or framework
+
+ Read and analyze the description provided in the documentation to understand the purpose and functionality of the module or framework.
+ Identify the key features, parameters, and operations performed by the module or framework.
+ Step 2: Provide an overview and introduction
+
+ Start the documentation by providing a brief overview and introduction to the module or framework.
+ Explain the importance and relevance of the module or framework in the context of the problem it solves.
+ Highlight any key concepts or terminology that will be used throughout the documentation.
+ Step 3: Provide a class or function definition
+
+ Provide the class or function definition for the module or framework.
+ Include the parameters that need to be passed to the class or function and provide a brief description of each parameter.
+ Specify the data types and default values for each parameter.
+ Step 4: Explain the functionality and usage
+
+ Provide a detailed explanation of how the module or framework works and what it does.
+ Describe the steps involved in using the module or framework, including any specific requirements or considerations.
+ Provide code examples to demonstrate the usage of the module or framework.
+ Explain the expected inputs and outputs for each operation or function.
+ Step 5: Provide additional information and tips
+
+ Provide any additional information or tips that may be useful for using the module or framework effectively.
+ Address any common issues or challenges that developers may encounter and provide recommendations or workarounds.
+ Step 6: Include references and resources
+
+ Include references to any external resources or research papers that provide further information or background on the module or framework.
+ Provide links to relevant documentation or websites for further exploration.
+ Example Template for the given documentation:
+
+ # Module/Function Name: MultiheadAttention
+
+ class torch.nn.MultiheadAttention(embed_dim, num_heads, dropout=0.0, bias=True, add_bias_kv=False, add_zero_attn=False, kdim=None, vdim=None, batch_first=False, device=None, dtype=None):
+ ```
+ Creates a multi-head attention module for joint information representation from the different subspaces.
+
+ Parameters:
+ - embed_dim (int): Total dimension of the model.
+ - num_heads (int): Number of parallel attention heads. The embed_dim will be split across num_heads.
+ - dropout (float): Dropout probability on attn_output_weights. Default: 0.0 (no dropout).
+ - bias (bool): If specified, adds bias to input/output projection layers. Default: True.
+ - add_bias_kv (bool): If specified, adds bias to the key and value sequences at dim=0. Default: False.
+ - add_zero_attn (bool): If specified, adds a new batch of zeros to the key and value sequences at dim=1. Default: False.
+ - kdim (int): Total number of features for keys. Default: None (uses kdim=embed_dim).
+ - vdim (int): Total number of features for values. Default: None (uses vdim=embed_dim).
+ - batch_first (bool): If True, the input and output tensors are provided as (batch, seq, feature). Default: False.
+ - device (torch.device): If specified, the tensors will be moved to the specified device.
+ - dtype (torch.dtype): If specified, the tensors will have the specified dtype.
+ ```
+
+ def forward(query, key, value, key_padding_mask=None, need_weights=True, attn_mask=None, average_attn_weights=True, is_causal=False):
+ ```
+ Forward pass of the multi-head attention module.
+
+ Parameters:
+ - query (Tensor): Query embeddings of shape (L, E_q) for unbatched input, (L, N, E_q) when batch_first=False, or (N, L, E_q) when batch_first=True.
+ - key (Tensor): Key embeddings of shape (S, E_k) for unbatched input, (S, N, E_k) when batch_first=False, or (N, S, E_k) when batch_first=True.
+ - value (Tensor): Value embeddings of shape (S, E_v) for unbatched input, (S, N, E_v) when batch_first=False, or (N, S, E_v) when batch_first=True.
+ - key_padding_mask (Optional[Tensor]): If specified, a mask indicating elements to be ignored in key for attention computation.
+ - need_weights (bool): If specified, returns attention weights in addition to attention outputs. Default: True.
+ - attn_mask (Optional[Tensor]): If specified, a mask preventing attention to certain positions.
+ - average_attn_weights (bool): If true, returns averaged attention weights per head. Otherwise, returns attention weights separately per head. Note that this flag only has an effect when need_weights=True. Default: True.
+ - is_causal (bool): If specified, applies a causal mask as the attention mask. Default: False.
+
+ Returns:
+ Tuple[Tensor, Optional[Tensor]]:
+ - attn_output (Tensor): Attention outputs of shape (L, E) for unbatched input, (L, N, E) when batch_first=False, or (N, L, E) when batch_first=True.
+ - attn_output_weights (Optional[Tensor]): Attention weights of shape (L, S) when unbatched or (N, L, S) when batched. Optional, only returned when need_weights=True.
+ ```
+
+ # Implementation of the forward pass of the attention module goes here
+
+ return attn_output, attn_output_weights
+
+ ```
+ # Usage example:
+
+ multihead_attn = nn.MultiheadAttention(embed_dim, num_heads)
+ attn_output, attn_output_weights = multihead_attn(query, key, value)
+ Note:
+
+ The above template includes the class or function definition, parameters, description, and usage example.
+ To replicate the documentation for any other module or framework, follow the same structure and provide the specific details for that module or framework.
+
+
+ ############# DOCUMENT THE FOLLOWING CODE ########
+ {task}
+ """
+ return documentation
+
+
+def TEST_WRITER_SOP_PROMPT(
+ task: str, module: str, path: str, *args, **kwargs
+):
+ TESTS_PROMPT = f"""
+
+ Create 5,000 lines of extensive and thorough tests for the code below using the guide, do not worry about your limits you do not have any
+ just write the best tests possible, the module is {module}, the file path is {path}
+
+
+ ######### TESTING GUIDE #############
+
+ # **Guide to Creating Extensive, Thorough, and Production-Ready Tests using `pytest`**
+
+ 1. **Preparation**:
+ - Install pytest: `pip install pytest`.
+ - Structure your project so that tests are in a separate `tests/` directory.
+ - Name your test files with the prefix `test_` for pytest to recognize them.
+
+ 2. **Writing Basic Tests**:
+ - Use clear function names prefixed with `test_` (e.g., `test_check_value()`).
+ - Use assert statements to validate results.
+
+ 3. **Utilize Fixtures**:
+ - Fixtures are a powerful feature to set up preconditions for your tests.
+ - Use `@pytest.fixture` decorator to define a fixture.
+ - Pass fixture name as an argument to your test to use it.
+
+ 4. **Parameterized Testing**:
+ - Use `@pytest.mark.parametrize` to run a test multiple times with different inputs.
+ - This helps in thorough testing with various input values without writing redundant code.
+
+ 5. **Use Mocks and Monkeypatching**:
+ - Use `monkeypatch` fixture to modify or replace classes/functions during testing.
+ - Use `unittest.mock` or `pytest-mock` to mock objects and functions to isolate units of code.
+
+ 6. **Exception Testing**:
+ - Test for expected exceptions using `pytest.raises(ExceptionType)`.
+
+ 7. **Test Coverage**:
+ - Install pytest-cov: `pip install pytest-cov`.
+ - Run tests with `pytest --cov=my_module` to get a coverage report.
+
+ 8. **Environment Variables and Secret Handling**:
+ - Store secrets and configurations in environment variables.
+ - Use libraries like `python-decouple` or `python-dotenv` to load environment variables.
+ - For tests, mock or set environment variables temporarily within the test environment.
+
+ 9. **Grouping and Marking Tests**:
+ - Use `@pytest.mark` decorator to mark tests (e.g., `@pytest.mark.slow`).
+ - This allows for selectively running certain groups of tests.
+
+ 10. **Use Plugins**:
+ - Utilize the rich ecosystem of pytest plugins (e.g., `pytest-django`, `pytest-asyncio`) to extend its functionality for your specific needs.
+
+ 11. **Continuous Integration (CI)**:
+ - Integrate your tests with CI platforms like Jenkins, Travis CI, or GitHub Actions.
+ - Ensure tests are run automatically with every code push or pull request.
+
+ 12. **Logging and Reporting**:
+ - Use `pytest`'s inbuilt logging.
+ - Integrate with tools like `Allure` for more comprehensive reporting.
+
+ 13. **Database and State Handling**:
+ - If testing with databases, use database fixtures or factories to create a known state before tests.
+ - Clean up and reset state post-tests to maintain consistency.
+
+ 14. **Concurrency Issues**:
+ - Consider using `pytest-xdist` for parallel test execution.
+ - Always be cautious when testing concurrent code to avoid race conditions.
+
+ 15. **Clean Code Practices**:
+ - Ensure tests are readable and maintainable.
+ - Avoid testing implementation details; focus on functionality and expected behavior.
+
+ 16. **Regular Maintenance**:
+ - Periodically review and update tests.
+ - Ensure that tests stay relevant as your codebase grows and changes.
+
+ 17. **Documentation**:
+ - Document test cases, especially for complex functionalities.
+ - Ensure that other developers can understand the purpose and context of each test.
+
+ 18. **Feedback Loop**:
+ - Use test failures as feedback for development.
+ - Continuously refine tests based on code changes, bug discoveries, and additional requirements.
+
+ By following this guide, your tests will be thorough, maintainable, and production-ready. Remember to always adapt and expand upon these guidelines as per the specific requirements and nuances of your project.
+
+
+ ######### CREATE TESTS FOR THIS CODE: #######
+ {task}
+
+ """
+
+ return TESTS_PROMPT
diff --git a/scripts/auto_tests_docs/mkdocs_handler.py b/scripts/auto_tests_docs/mkdocs_handler.py
new file mode 100644
index 00000000..a61defa8
--- /dev/null
+++ b/scripts/auto_tests_docs/mkdocs_handler.py
@@ -0,0 +1,31 @@
+import os
+
+
+def generate_file_list(directory, output_file):
+ """
+ Generate a list of files in a directory in the specified format and write it to a file.
+
+ Args:
+ directory (str): The directory to list the files from.
+ output_file (str): The file to write the output to.
+ """
+ with open(output_file, "w") as f:
+ for root, dirs, files in os.walk(directory):
+ for file in files:
+ if file.endswith(".md"):
+ # Remove the directory from the file path and replace slashes with dots
+ file_path = (
+ os.path.join(root, file)
+ .replace(directory + "/", "")
+ .replace("/", ".")
+ )
+ # Remove the file extension
+ file_name, _ = os.path.splitext(file)
+ # Write the file name and path to the output file
+ f.write(
+ f'- {file_name}: "swarms/utils/{file_path}"\n'
+ )
+
+
+# Use the function to generate the file list
+generate_file_list("docs/swarms/tokenizers", "file_list.txt")
diff --git a/scripts/auto_tests_docs/update_mkdocs.py b/scripts/auto_tests_docs/update_mkdocs.py
new file mode 100644
index 00000000..dfde53cb
--- /dev/null
+++ b/scripts/auto_tests_docs/update_mkdocs.py
@@ -0,0 +1,64 @@
+import yaml
+
+
+def update_mkdocs(
+ class_names,
+ base_path="docs/zeta/nn/modules",
+ mkdocs_file="mkdocs.yml",
+):
+ """
+ Update the mkdocs.yml file with new documentation links.
+
+ Args:
+ - class_names: A list of class names for which documentation is generated.
+ - base_path: The base path where documentation Markdown files are stored.
+ - mkdocs_file: The path to the mkdocs.yml file.
+ """
+ with open(mkdocs_file, "r") as file:
+ mkdocs_config = yaml.safe_load(file)
+
+ # Find or create the 'zeta.nn.modules' section in 'nav'
+ zeta_modules_section = None
+ for section in mkdocs_config.get("nav", []):
+ if "zeta.nn.modules" in section:
+ zeta_modules_section = section["zeta.nn.modules"]
+ break
+
+ if zeta_modules_section is None:
+ zeta_modules_section = {}
+ mkdocs_config["nav"].append(
+ {"zeta.nn.modules": zeta_modules_section}
+ )
+
+ # Add the documentation paths to the 'zeta.nn.modules' section
+ for class_name in class_names:
+ doc_path = f"{base_path}/{class_name.lower()}.md"
+ zeta_modules_section[class_name] = doc_path
+
+ # Write the updated content back to mkdocs.yml
+ with open(mkdocs_file, "w") as file:
+ yaml.safe_dump(mkdocs_config, file, sort_keys=False)
+
+
+# Example usage
+classes = [
+ "DenseBlock",
+ "HighwayLayer",
+ "MultiScaleBlock",
+ "FeedbackBlock",
+ "DualPathBlock",
+ "RecursiveBlock",
+ "PytorchGELUTanh",
+ "NewGELUActivation",
+ "GELUActivation",
+ "FastGELUActivation",
+ "QuickGELUActivation",
+ "ClippedGELUActivation",
+ "AccurateGELUActivation",
+ "MishActivation",
+ "LinearActivation",
+ "LaplaceActivation",
+ "ReLUSquaredActivation",
+]
+
+update_mkdocs(classes)
diff --git a/scripts/code_quality.sh b/scripts/code_quality.sh
index 90153258..e3afec13 100755
--- a/scripts/code_quality.sh
+++ b/scripts/code_quality.sh
@@ -1,19 +1,19 @@
#!/bin/bash
-# Navigate to the directory containing the 'swarms' folder
+# Navigate to the directory containing the 'tests' folder
# cd /path/to/your/code/directory
# Run autopep8 with max aggressiveness (-aaa) and in-place modification (-i)
-# on all Python files (*.py) under the 'swarms' directory.
-autopep8 --in-place --aggressive --aggressive --recursive --experimental --list-fixes swarms/
+# on all Python files (*.py) under the 'tests' directory.
+autopep8 --in-place --aggressive --aggressive --recursive --experimental --list-fixes zeta/
# Run black with default settings, since black does not have an aggressiveness level.
-# Black will format all Python files it finds in the 'swarms' directory.
-black --experimental-string-processing swarms/
+# Black will format all Python files it finds in the 'tests' directory.
+black --experimental-string-processing zeta/
-# Run ruff on the 'swarms' directory.
+# Run ruff on the 'tests' directory.
# Add any additional flags if needed according to your version of ruff.
-ruff --unsafe_fix
+ruff zeta/ --fix
# YAPF
-yapf --recursive --in-place --verbose --style=google --parallel swarms
+yapf --recursive --in-place --verbose --style=google --parallel tests
diff --git a/scripts/del_pycache.sh b/scripts/del_pycache.sh
new file mode 100755
index 00000000..dc8e7d2b
--- /dev/null
+++ b/scripts/del_pycache.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+# Find and delete all __pycache__ directories
+find . -type d -name "__pycache__" -exec rm -r {} +
+
+# Find and delete all .pyc files
+find . -type f -name "*.pyc" -delete
\ 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..a0ed5a96
--- /dev/null
+++ b/scripts/playground_to_examples.sh
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+# Define the directory to search
+dir="playground"
+
+# 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)
+
+ # 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
diff --git a/scripts/test_name.sh b/scripts/test_name.sh
index cdc6a013..4123f870 100755
--- a/scripts/test_name.sh
+++ b/scripts/test_name.sh
@@ -4,5 +4,6 @@ do
dir=$(dirname "$file")
if [[ $filename != test_* ]]; then
mv "$file" "$dir/test_$filename"
+ printf "\e[1;34mRenamed: \e[0m$file \e[1;32mto\e[0m $dir/test_$filename\n"
fi
done
\ No newline at end of file
diff --git a/sequential_workflow_example.py b/sequential_workflow_example.py
deleted file mode 100644
index c36419c1..00000000
--- a/sequential_workflow_example.py
+++ /dev/null
@@ -1,52 +0,0 @@
-import os
-from swarms.models import OpenAIChat
-from swarms.structs import Agent
-from swarms.structs.sequential_workflow import SequentialWorkflow
-from dotenv import load_dotenv
-
-load_dotenv()
-
-# Load the environment variables
-api_key = os.getenv("OPENAI_API_KEY")
-
-
-# Initialize the language agent
-# Initialize the language model
-llm = OpenAIChat(
- temperature=0.5,
- model_name="gpt-4",
- openai_api_key=api_key,
- max_tokens=4000,
-)
-
-
-# Initialize the agent with the language agent
-agent1 = Agent(
- llm=llm,
- max_loops=1,
-)
-
-# Create another agent for a different task
-agent2 = Agent(llm=llm, max_loops=1)
-
-# 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.",
-)
-
-# Suppose the next task takes the output of the first task as input
-workflow.add(
- agent2,
- "Summarize the generated blog",
-)
-
-# Run the workflow
-workflow.run()
-
-# 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 f6f04205..d88d9bf8 100644
--- a/swarms/__init__.py
+++ b/swarms/__init__.py
@@ -1,13 +1,16 @@
-from swarms.utils.disable_logging import disable_logging
+# from swarms.telemetry.main import Telemetry # noqa: E402, F403
+from swarms.telemetry.bootup import bootup # noqa: E402, F403
+
+bootup()
-disable_logging()
from swarms.agents import * # noqa: E402, F403
-from swarms.swarms import * # noqa: E402, F403
from swarms.structs import * # noqa: E402, F403
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
-
-# from swarms.cli import * # noqa: E402, F403
+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/agents/__init__.py b/swarms/agents/__init__.py
index 4f6e12d6..461baa16 100644
--- a/swarms/agents/__init__.py
+++ b/swarms/agents/__init__.py
@@ -1,15 +1,37 @@
-# from swarms.agents.omni_modal_agent import OmniModalAgent
-from swarms.agents.message import Message
-
-# from swarms.agents.stream_response import stream
from swarms.agents.base import AbstractAgent
-
-# from swarms.agents.idea_to_image_agent import Idea2Image
-
-"""Agent Infrastructure, models, memory, utils, tools"""
+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 Worker
+from swarms.agents.agent_wrapper import agent_wrapper
__all__ = [
- # "OmniModalAgent",
- "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",
+ "Worker",
+ "agent_wrapper",
]
diff --git a/swarms/agents/agent_wrapper.py b/swarms/agents/agent_wrapper.py
new file mode 100644
index 00000000..738f599d
--- /dev/null
+++ b/swarms/agents/agent_wrapper.py
@@ -0,0 +1,26 @@
+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
diff --git a/swarms/agents/omni_modal_agent.py b/swarms/agents/omni_modal_agent.py
index 6a22c477..113ec461 100644
--- a/swarms/agents/omni_modal_agent.py
+++ b/swarms/agents/omni_modal_agent.py
@@ -1,7 +1,4 @@
-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,
)
@@ -13,34 +10,7 @@ from langchain_experimental.autonomous_agents.hugginggpt.task_planner import (
)
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)
+from swarms.structs.message import Message
class OmniModalAgent:
diff --git a/swarms/agents/simple_agent.py b/swarms/agents/simple_agent.py
new file mode 100644
index 00000000..757715dd
--- /dev/null
+++ b/swarms/agents/simple_agent.py
@@ -0,0 +1,104 @@
+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: Any = "automatic", *args, **kwargs
+):
+ """
+ A simple agent that interacts with a language model.
+
+ Args:
+ 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)
+ 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}")
+ raise error
+
+ except KeyboardInterrupt:
+ print("[INFO][SimpleAgentConversation] Keyboard interrupt")
+ conv.export_conversation("conversation.txt")
+ raise KeyboardInterrupt
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
new file mode 100644
index 00000000..8e6adf9d
--- /dev/null
+++ b/swarms/agents/tool_agent.py
@@ -0,0 +1,122 @@
+from typing import Any
+
+from swarms.models.base_llm import AbstractLLM
+from swarms.tools.format_tools import Jsonformer
+
+
+class ToolAgent(AbstractLLM):
+ """
+ Represents a tool agent that performs a specific task using a model and tokenizer.
+
+ Args:
+ 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.
+ tokenizer (Any): The tokenizer used by the tool agent.
+ json_schema (Any): The JSON schema used by the tool agent.
+ *args: Variable length arguments.
+ **kwargs: Keyword arguments.
+
+ Attributes:
+ 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.
+ tokenizer (Any): The tokenizer used by the tool agent.
+ json_schema (Any): The JSON schema used by the tool agent.
+
+ Methods:
+ run: Runs the tool agent for a specific task.
+
+ Raises:
+ Exception: If an error occurs while running the tool agent.
+
+
+ Example:
+ from transformers import AutoModelForCausalLM, AutoTokenizer
+ from swarms import ToolAgent
+
+
+ model = AutoModelForCausalLM.from_pretrained("databricks/dolly-v2-12b")
+ tokenizer = AutoTokenizer.from_pretrained("databricks/dolly-v2-12b")
+
+ json_schema = {
+ "type": "object",
+ "properties": {
+ "name": {"type": "string"},
+ "age": {"type": "number"},
+ "is_student": {"type": "boolean"},
+ "courses": {
+ "type": "array",
+ "items": {"type": "string"}
+ }
+ }
+ }
+
+ task = "Generate a person's information based on the following schema:"
+ agent = ToolAgent(model=model, tokenizer=tokenizer, json_schema=json_schema)
+ generated_data = agent.run(task)
+
+ print(generated_data)
+ """
+
+ def __init__(
+ self,
+ 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,
+ max_number_tokens: int = 500,
+ *args,
+ **kwargs,
+ ):
+ 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):
+ """
+ Run the tool agent for the specified task.
+
+ Args:
+ 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.
+ """
+ try:
+ self.toolagent = Jsonformer(
+ model=self.model,
+ tokenizer=self.tokenizer,
+ json_schema=self.json_schema,
+ prompt=task,
+ max_number_tokens=self.max_number_tokens,
+ *args,
+ **kwargs,
+ )
+
+ out = self.toolagent()
+ return out
+ except Exception as error:
+ print(f"[Error] [ToolAgent] {error}")
+ raise error
+
+ def __call__(self, task: str, *args, **kwargs):
+ """Call self as a function.
+
+ Args:
+ task (str): _description_
+
+ Returns:
+ _type_: _description_
+ """
+ return self.run(task, *args, **kwargs)
diff --git a/swarms/agents/worker_agent.py b/swarms/agents/worker_agent.py
new file mode 100644
index 00000000..d254acef
--- /dev/null
+++ b/swarms/agents/worker_agent.py
@@ -0,0 +1,201 @@
+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},
+ verbose: bool = False,
+ *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.verbose = verbose
+
+ 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, img=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/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/swarms/cli/_cli.py b/swarms/cli/_cli.py
index b4be4e02..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 run_file():
- 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/cli/run_file.py b/swarms/cli/run_file.py
index de035b1e..171c6c56 100644
--- a/swarms/cli/run_file.py
+++ b/swarms/cli/run_file.py
@@ -2,7 +2,7 @@ import sys
import subprocess
-def run_file():
+def run_file(filename: str):
"""Run a given file.
Usage: swarms run file_name.py
diff --git a/playground/models/tocr.py b/swarms/loaders/__init__.py
similarity index 100%
rename from playground/models/tocr.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 086bd1bd..2dca8172 100644
--- a/swarms/memory/__init__.py
+++ b/swarms/memory/__init__.py
@@ -1,7 +1,19 @@
-from swarms.memory.base_vectordb import VectorDatabase
+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
+from swarms.memory.visual_memory import VisualShortTermMemory
+from swarms.memory.action_subtask import ActionSubtaskEntry
+from swarms.memory.chroma_db import ChromaDB
__all__ = [
- "VectorDatabase",
- "ShortTermMemory"
+ "AbstractVectorDatabase",
+ "AbstractDatabase",
+ "ShortTermMemory",
+ "SQLiteDB",
+ "WeaviateDB",
+ "VisualShortTermMemory",
+ "ActionSubtaskEntry",
+ "ChromaDB",
]
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/memory/base_db.py b/swarms/memory/base_db.py
new file mode 100644
index 00000000..0501def7
--- /dev/null
+++ b/swarms/memory/base_db.py
@@ -0,0 +1,159 @@
+from abc import ABC, abstractmethod
+
+
+class AbstractDatabase(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 execute_query(self, query):
+ """
+ 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, table, data):
+ """
+ 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 query(self, table, condition):
+ """
+ Query the database.
+
+ This method queries the specified table in the database based on the given condition.
+
+ Parameters:
+ table (str): The name of the table.
+ condition (str): The condition to be applied in the query.
+
+ Returns:
+ list: A list of dictionaries representing the query results.
+
+ """
+
+ pass
+
+ @abstractmethod
+ def get(self, table, id):
+ """
+ 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, table, id, data):
+ """
+ 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, table, id):
+ """
+ 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
diff --git a/swarms/memory/base_vectordb.py b/swarms/memory/base_vectordb.py
index 841c6147..06f42007 100644
--- a/swarms/memory/base_vectordb.py
+++ b/swarms/memory/base_vectordb.py
@@ -1,58 +1,141 @@
from abc import ABC, abstractmethod
-from typing import Any, Dict
-class VectorDatabase(ABC):
+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, vector: Dict[str, Any], metadata: Dict[str, Any]
- ) -> None:
+ def add(self, doc: str):
"""
- add a vector into the database.
+ 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.
- 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]:
+ def get(self, query: str):
"""
- Query the database for vectors similar to the given vector.
+ Get a record from the database.
- Args:
- text (Dict[str, Any]): The vector to compare against.
- num_results (int): The number of similar vectors to return.
+ 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[str, Any]: The most similar vectors and their associated metadata.
+ dict: A dictionary representing the retrieved record.
+
"""
+
pass
@abstractmethod
- def delete(self, vector_id: str) -> None:
+ def update(self, doc):
"""
- Delete a vector from the database.
+ 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.
- 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:
+ def delete(self, message):
"""
- Update a vector in the database.
+ 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.
- 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/chroma_db.py b/swarms/memory/chroma_db.py
index 8e200974..4418e90d 100644
--- a/swarms/memory/chroma_db.py
+++ b/swarms/memory/chroma_db.py
@@ -1,29 +1,22 @@
-import logging
import os
-from typing import Dict, List, Optional
+import numpy as np
+import logging
+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 swarms.utils.token_count_tiktoken import limit_tokens_from_string
-
-load_dotenv()
-# ChromaDB settings
-client = chromadb.Client(Settings(anonymized_telemetry=False))
+# from chromadb.utils.data import ImageLoader
+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
-# ChromaDB client
-def get_chromadb_client():
- return client
-
-
-# OpenAI API key
-OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
+# Load environment variables
+load_dotenv()
# Results storage using local ChromaDB
@@ -33,10 +26,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 +38,175 @@ 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,
+ docs_folder: str = None,
+ verbose: 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
+ self.docs_folder = docs_folder
+ self.verbose = verbose
# Disable ChromaDB logging
- logging.getLogger("chromadb").setLevel(logging.ERROR)
+ if verbose:
+ 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 = None
+
+ # 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,
+ )
+ 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.traverse_directory()
+
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)}")
+
+ 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_folder):
+ 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/memory/pg.py b/swarms/memory/pg.py
index 50972d98..d96b475d 100644
--- a/swarms/memory/pg.py
+++ b/swarms/memory/pg.py
@@ -1,302 +1,140 @@
-import subprocess
import uuid
-from typing import Optional
-from attr import define, field, Factory
-from dataclasses import dataclass
-from swarms.memory.base import BaseVectorStore
+from typing import Any, List, Optional
+from sqlalchemy import JSON, Column, String, create_engine
+from sqlalchemy.dialects.postgresql import UUID
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import Session
-try:
- from sqlalchemy.engine import Engine
- from sqlalchemy import create_engine, Column, String, JSON
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy.dialects.postgresql import UUID
- from sqlalchemy.orm import Session
-except ImportError:
- print(
- "The PgVectorVectorStore requires sqlalchemy to be installed"
- )
- print("pip install sqlalchemy")
- subprocess.run(["pip", "install", "sqlalchemy"])
-
-try:
- from pgvector.sqlalchemy import Vector
-except ImportError:
- print("The PgVectorVectorStore requires pgvector to be installed")
- print("pip install pgvector")
- subprocess.run(["pip", "install", "pgvector"])
+class PostgresDB:
+ """
+ A class representing a Postgres database.
-@define
-class PgVectorVectorStore(BaseVectorStore):
- """A vector store driver to Postgres using the PGVector extension.
+ Args:
+ connection_string (str): The connection string for the Postgres database.
+ table_name (str): The name of the table in the database.
Attributes:
- connection_string: An optional string describing the target Postgres database instance.
- create_engine_params: Additional configuration params passed when creating the database connection.
- engine: An optional sqlalchemy Postgres engine to use.
- table_name: Optionally specify the name of the table to used to store vectors.
-
- Methods:
- upsert_vector(vector: list[float], vector_id: Optional[str] = None, namespace: Optional[str] = None, meta: Optional[dict] = None, **kwargs) -> str:
- Upserts a vector into the index.
- load_entry(vector_id: str, namespace: Optional[str] = None) -> Optional[BaseVector.Entry]:
- Loads a single vector from the index.
- load_entries(namespace: Optional[str] = None) -> list[BaseVector.Entry]:
- Loads all vectors from the index.
- query(query: str, count: Optional[int] = None, namespace: Optional[str] = None, include_vectors: bool = False, include_metadata=True, **kwargs) -> list[BaseVector.QueryResult]:
- Queries the index for vectors similar to the given query string.
- setup(create_schema: bool = True, install_uuid_extension: bool = True, install_vector_extension: bool = True) -> None:
- Provides a mechanism to initialize the database schema and extensions.
-
- Usage:
- >>> from swarms.memory.vector_stores.pgvector import PgVectorVectorStore
- >>> from swarms.utils.embeddings import USEEmbedding
- >>> from swarms.utils.hash import str_to_hash
- >>> from swarms.utils.dataframe import dataframe_to_hash
- >>> import pandas as pd
- >>>
- >>> # Create a new PgVectorVectorStore instance:
- >>> pv = PgVectorVectorStore(
- >>> connection_string="postgresql://postgres:password@localhost:5432/postgres",
- >>> table_name="your-table-name"
- >>> )
- >>> # Create a new index:
- >>> pv.setup()
- >>> # Create a new USEEmbedding instance:
- >>> use = USEEmbedding()
- >>> # Create a new dataframe:
- >>> df = pd.DataFrame({
- >>> "text": [
- >>> "This is a test",
- >>> "This is another test",
- >>> "This is a third test"
- >>> ]
- >>> })
- >>> # Embed the dataframe:
- >>> df["embedding"] = df["text"].apply(use.embed_string)
- >>> # Upsert the dataframe into the index:
- >>> pv.upsert_vector(
- >>> vector=df["embedding"].tolist(),
- >>> vector_id=dataframe_to_hash(df),
- >>> namespace="your-namespace"
- >>> )
- >>> # Query the index:
- >>> pv.query(
- >>> query="This is a test",
- >>> count=10,
- >>> namespace="your-namespace"
- >>> )
- >>> # Load a single entry from the index:
- >>> pv.load_entry(
- >>> vector_id=dataframe_to_hash(df),
- >>> namespace="your-namespace"
- >>> )
- >>> # Load all entries from the index:
- >>> pv.load_entries(
- >>> namespace="your-namespace"
- >>> )
-
+ engine: The SQLAlchemy engine for connecting to the database.
+ table_name (str): The name of the table in the database.
+ VectorModel: The SQLAlchemy model representing the vector table.
"""
- connection_string: Optional[str] = field(
- default=None, kw_only=True
- )
- create_engine_params: dict = field(factory=dict, kw_only=True)
- engine: Optional[Engine] = field(default=None, kw_only=True)
- table_name: str = field(kw_only=True)
- _model: any = field(
- default=Factory(
- lambda self: self.default_vector_model(), takes_self=True
- )
- )
+ def __init__(
+ self, connection_string: str, table_name: str, *args, **kwargs
+ ):
+ """
+ Initializes a new instance of the PostgresDB class.
- @connection_string.validator
- def validate_connection_string(
- self, _, connection_string: Optional[str]
- ) -> None:
- # If an engine is provided, the connection string is not used.
- if self.engine is not None:
- return
+ Args:
+ connection_string (str): The connection string for the Postgres database.
+ table_name (str): The name of the table in the database.
- # If an engine is not provided, a connection string is required.
- if connection_string is None:
- raise ValueError(
- "An engine or connection string is required"
- )
-
- if not connection_string.startswith("postgresql://"):
- raise ValueError(
- "The connection string must describe a Postgres"
- " database connection"
- )
+ """
+ self.engine = create_engine(
+ connection_string, *args, **kwargs
+ )
+ self.table_name = table_name
+ self.VectorModel = self._create_vector_model()
- @engine.validator
- def validate_engine(self, _, engine: Optional[Engine]) -> None:
- # If a connection string is provided, an engine does not need to be provided.
- if self.connection_string is not None:
- return
+ def _create_vector_model(self):
+ """
+ Creates the SQLAlchemy model for the vector table.
- # If a connection string is not provided, an engine is required.
- if engine is None:
- raise ValueError(
- "An engine or connection string is required"
- )
+ Returns:
+ The SQLAlchemy model representing the vector table.
- def __attrs_post_init__(self) -> None:
- """If a an engine is provided, it will be used to connect to the database.
- If not, a connection string is used to create a new database connection here.
"""
- if self.engine is None:
- self.engine = create_engine(
- self.connection_string, **self.create_engine_params
- )
+ Base = declarative_base()
- def setup(
- self,
- create_schema: bool = True,
- install_uuid_extension: bool = True,
- install_vector_extension: bool = True,
- ) -> None:
- """Provides a mechanism to initialize the database schema and extensions."""
- if install_uuid_extension:
- self.engine.execute(
- 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp";'
- )
+ class VectorModel(Base):
+ __tablename__ = self.table_name
- if install_vector_extension:
- self.engine.execute(
- 'CREATE EXTENSION IF NOT EXISTS "vector";'
+ id = Column(
+ UUID(as_uuid=True),
+ primary_key=True,
+ default=uuid.uuid4,
+ unique=True,
+ nullable=False,
)
+ vector = Column(
+ String
+ ) # Assuming vector is stored as a string
+ namespace = Column(String)
+ meta = Column(JSON)
- if create_schema:
- self._model.metadata.create_all(self.engine)
+ return VectorModel
- def upsert_vector(
+ def add_or_update_vector(
self,
- vector: list[float],
+ vector: str,
vector_id: Optional[str] = None,
namespace: Optional[str] = None,
meta: Optional[dict] = None,
- **kwargs,
- ) -> str:
- """Inserts or updates a vector in the collection."""
- with Session(self.engine) as session:
- obj = self._model(
- id=vector_id,
- vector=vector,
- namespace=namespace,
- meta=meta,
- )
-
- obj = session.merge(obj)
- session.commit()
-
- return str(obj.id)
-
- def load_entry(
- self, vector_id: str, namespace: Optional[str] = None
- ) -> BaseVectorStore.Entry:
- """Retrieves a specific vector entry from the collection based on its identifier and optional namespace."""
- with Session(self.engine) as session:
- result = session.get(self._model, vector_id)
-
- return BaseVectorStore.Entry(
- id=result.id,
- vector=result.vector,
- namespace=result.namespace,
- meta=result.meta,
- )
-
- def load_entries(
- self, namespace: Optional[str] = None
- ) -> list[BaseVectorStore.Entry]:
- """Retrieves all vector entries from the collection, optionally filtering to only
- those that match the provided namespace.
+ ) -> None:
"""
- with Session(self.engine) as session:
- query = session.query(self._model)
- if namespace:
- query = query.filter_by(namespace=namespace)
+ Adds or updates a vector in the database.
- results = query.all()
+ Args:
+ vector (str): The vector to be added or updated.
+ vector_id (str, optional): The ID of the vector. If not provided, a new ID will be generated.
+ namespace (str, optional): The namespace of the vector.
+ meta (dict, optional): Additional metadata associated with the vector.
- return [
- BaseVectorStore.Entry(
- id=str(result.id),
- vector=result.vector,
- namespace=result.namespace,
- meta=result.meta,
+ """
+ try:
+ with Session(self.engine) as session:
+ obj = self.VectorModel(
+ id=vector_id,
+ vector=vector,
+ namespace=namespace,
+ meta=meta,
)
- for result in results
- ]
-
- def query(
- self,
- query: str,
- count: Optional[int] = BaseVectorStore.DEFAULT_QUERY_COUNT,
- namespace: Optional[str] = None,
- include_vectors: bool = False,
- distance_metric: str = "cosine_distance",
- **kwargs,
- ) -> list[BaseVectorStore.QueryResult]:
- """Performs a search on the collection to find vectors similar to the provided input vector,
- optionally filtering to only those that match the provided namespace.
+ session.merge(obj)
+ session.commit()
+ except Exception as e:
+ print(f"Error adding or updating vector: {e}")
+
+ def query_vectors(
+ self, query: Any, namespace: Optional[str] = None
+ ) -> List[Any]:
"""
- distance_metrics = {
- "cosine_distance": self._model.vector.cosine_distance,
- "l2_distance": self._model.vector.l2_distance,
- "inner_product": self._model.vector.max_inner_product,
- }
-
- if distance_metric not in distance_metrics:
- raise ValueError("Invalid distance metric provided")
-
- op = distance_metrics[distance_metric]
-
- with Session(self.engine) as session:
- vector = self.embedding_driver.embed_string(query)
+ Queries vectors from the database based on the given query and namespace.
- # The query should return both the vector and the distance metric score.
- query = session.query(
- self._model,
- op(vector).label("score"),
- ).order_by(op(vector))
+ Args:
+ query (Any): The query or condition to filter the vectors.
+ namespace (str, optional): The namespace of the vectors to be queried.
- if namespace:
- query = query.filter_by(namespace=namespace)
+ Returns:
+ List[Any]: A list of vectors that match the query and namespace.
- results = query.limit(count).all()
-
- return [
- BaseVectorStore.QueryResult(
- id=str(result[0].id),
- vector=(
- result[0].vector if include_vectors else None
- ),
- score=result[1],
- meta=result[0].meta,
- namespace=result[0].namespace,
- )
- for result in results
- ]
-
- def default_vector_model(self) -> any:
- Base = declarative_base()
-
- @dataclass
- class VectorModel(Base):
- __tablename__ = self.table_name
+ """
+ try:
+ with Session(self.engine) as session:
+ q = session.query(self.VectorModel)
+ if namespace:
+ q = q.filter_by(namespace=namespace)
+ # Assuming 'query' is a condition or filter
+ q = q.filter(query)
+ return q.all()
+ except Exception as e:
+ print(f"Error querying vectors: {e}")
+ return []
+
+ def delete_vector(self, vector_id):
+ """
+ Deletes a vector from the database based on the given vector ID.
- id = Column(
- UUID(as_uuid=True),
- primary_key=True,
- default=uuid.uuid4,
- unique=True,
- nullable=False,
- )
- vector = Column(Vector())
- namespace = Column(String)
- meta = Column(JSON)
+ Args:
+ vector_id: The ID of the vector to be deleted.
- return VectorModel
+ """
+ try:
+ with Session(self.engine) as session:
+ obj = session.get(self.VectorModel, vector_id)
+ if obj:
+ session.delete(obj)
+ session.commit()
+ except Exception as e:
+ print(f"Error deleting vector: {e}")
diff --git a/swarms/memory/pinecone.py b/swarms/memory/pinecone.py
index f48bb627..b1c0edf3 100644
--- a/swarms/memory/pinecone.py
+++ b/swarms/memory/pinecone.py
@@ -1,14 +1,14 @@
from typing import Optional
-from swarms.memory.base_vectordb import VectorDatabase
+from swarms.memory.base_vectordb import AbstractVectorDatabase
import pinecone
from attr import define, field
from swarms.utils.hash import str_to_hash
@define
-class PineconDB(VectorDatabase):
+class PineconeDB(AbstractVectorDatabase):
"""
- PineconDB is a vector storage driver that uses Pinecone as the underlying storage engine.
+ PineconeDB is a vector storage driver that uses Pinecone as the underlying storage engine.
Pinecone is a vector database that allows you to store, search, and retrieve high-dimensional vectors with
blazing speed and low latency. It is a managed service that is easy to use and scales effortlessly, so you can
@@ -34,14 +34,14 @@ class PineconDB(VectorDatabase):
Creates a new index.
Usage:
- >>> from swarms.memory.vector_stores.pinecone import PineconDB
+ >>> from swarms.memory.vector_stores.pinecone import PineconeDB
>>> from swarms.utils.embeddings import USEEmbedding
>>> from swarms.utils.hash import str_to_hash
>>> from swarms.utils.dataframe import dataframe_to_hash
>>> import pandas as pd
>>>
- >>> # Create a new PineconDB instance:
- >>> pv = PineconDB(
+ >>> # Create a new PineconeDB instance:
+ >>> pv = PineconeDB(
>>> api_key="your-api-key",
>>> index_name="your-index-name",
>>> environment="us-west1-gcp",
@@ -166,7 +166,7 @@ class PineconDB(VectorDatabase):
count: Optional[int] = None,
namespace: Optional[str] = None,
include_vectors: bool = False,
- # PineconDBStorageDriver-specific params:
+ # PineconeDBStorageDriver-specific params:
include_metadata=True,
**kwargs,
):
diff --git a/swarms/memory/qdrant.py b/swarms/memory/qdrant.py
index 83ff5593..0a553a16 100644
--- a/swarms/memory/qdrant.py
+++ b/swarms/memory/qdrant.py
@@ -1,4 +1,3 @@
-import subprocess
from typing import List
from httpx import RequestError
@@ -8,9 +7,6 @@ try:
except ImportError:
print("Please install the sentence-transformers package")
print("pip install sentence-transformers")
- print("pip install qdrant-client")
- subprocess.run(["pip", "install", "sentence-transformers"])
-
try:
from qdrant_client import QdrantClient
@@ -22,7 +18,6 @@ try:
except ImportError:
print("Please install the qdrant-client package")
print("pip install qdrant-client")
- subprocess.run(["pip", "install", "qdrant-client"])
class Qdrant:
@@ -82,7 +77,7 @@ class Qdrant:
f"Collection '{self.collection_name}' already"
" exists."
)
- except Exception as e:
+ except Exception:
self.client.create_collection(
collection_name=self.collection_name,
vectors_config=VectorParams(
diff --git a/swarms/memory/short_term_memory.py b/swarms/memory/short_term_memory.py
index d343a80f..d380fba5 100644
--- a/swarms/memory/short_term_memory.py
+++ b/swarms/memory/short_term_memory.py
@@ -2,10 +2,30 @@ import logging
from swarms.structs.base import BaseStructure
import threading
import json
-import os
class ShortTermMemory(BaseStructure):
+ """Short term memory.
+
+ Args:
+ return_str (bool, optional): _description_. Defaults to True.
+ autosave (bool, optional): _description_. Defaults to True.
+ *args: _description_
+ **kwargs: _description_
+
+
+ Example:
+ >>> from swarms.memory.short_term_memory import ShortTermMemory
+ >>> stm = ShortTermMemory()
+ >>> stm.add(role="agent", message="Hello world!")
+ >>> stm.add(role="agent", message="How are you?")
+ >>> stm.add(role="agent", message="I am fine.")
+ >>> stm.add(role="agent", message="How are you?")
+ >>> stm.add(role="agent", message="I am fine.")
+
+
+ """
+
def __init__(
self,
return_str: bool = True,
@@ -68,6 +88,14 @@ class ShortTermMemory(BaseStructure):
def update_short_term(
self, index, role: str, message: str, *args, **kwargs
):
+ """Update the short term memory.
+
+ Args:
+ index (_type_): _description_
+ role (str): _description_
+ message (str): _description_
+
+ """
self.short_term_memory[index] = {
"role": role,
"message": message,
diff --git a/swarms/memory/sqlite.py b/swarms/memory/sqlite.py
new file mode 100644
index 00000000..542be34b
--- /dev/null
+++ b/swarms/memory/sqlite.py
@@ -0,0 +1,120 @@
+from typing import List, Tuple, Any, Optional
+from swarms.memory.base_vectordb import AbstractVectorDatabase
+
+try:
+ import sqlite3
+except ImportError:
+ raise ImportError(
+ "Please install sqlite3 to use the SQLiteDB class."
+ )
+
+
+class SQLiteDB(AbstractVectorDatabase):
+ """
+ A reusable class for SQLite database operations with methods for adding,
+ deleting, updating, and querying data.
+
+ Attributes:
+ db_path (str): The file path to the SQLite database.
+ """
+
+ def __init__(self, db_path: str):
+ """
+ Initializes the SQLiteDB class with the given database path.
+
+ Args:
+ db_path (str): The file path to the SQLite database.
+ """
+ self.db_path = db_path
+
+ def execute_query(
+ self, query: str, params: Optional[Tuple[Any, ...]] = None
+ ) -> List[Tuple]:
+ """
+ Executes a SQL query and returns fetched results.
+
+ Args:
+ query (str): The SQL query to execute.
+ params (Tuple[Any, ...], optional): The parameters to substitute into the query.
+
+ Returns:
+ List[Tuple]: The results fetched from the database.
+ """
+ try:
+ with sqlite3.connect(self.db_path) as conn:
+ cursor = conn.cursor()
+ cursor.execute(query, params or ())
+ return cursor.fetchall()
+ except Exception as error:
+ print(f"Error executing query: {error}")
+ raise error
+
+ def add(self, query: str, params: Tuple[Any, ...]) -> None:
+ """
+ Adds a new entry to the database.
+
+ Args:
+ query (str): The SQL query for insertion.
+ params (Tuple[Any, ...]): The parameters to substitute into the query.
+ """
+ try:
+ with sqlite3.connect(self.db_path) as conn:
+ cursor = conn.cursor()
+ cursor.execute(query, params)
+ conn.commit()
+ except Exception as error:
+ print(f"Error adding new entry: {error}")
+ raise error
+
+ def delete(self, query: str, params: Tuple[Any, ...]) -> None:
+ """
+ Deletes an entry from the database.
+
+ Args:
+ query (str): The SQL query for deletion.
+ params (Tuple[Any, ...]): The parameters to substitute into the query.
+ """
+ try:
+ with sqlite3.connect(self.db_path) as conn:
+ cursor = conn.cursor()
+ cursor.execute(query, params)
+ conn.commit()
+ except Exception as error:
+ print(f"Error deleting entry: {error}")
+ raise error
+
+ def update(self, query: str, params: Tuple[Any, ...]) -> None:
+ """
+ Updates an entry in the database.
+
+ Args:
+ query (str): The SQL query for updating.
+ params (Tuple[Any, ...]): The parameters to substitute into the query.
+ """
+ try:
+ with sqlite3.connect(self.db_path) as conn:
+ cursor = conn.cursor()
+ cursor.execute(query, params)
+ conn.commit()
+ except Exception as error:
+ print(f"Error updating entry: {error}")
+ raise error
+
+ def query(
+ self, query: str, params: Optional[Tuple[Any, ...]] = None
+ ) -> List[Tuple]:
+ """
+ Fetches data from the database based on a query.
+
+ Args:
+ query (str): The SQL query to execute.
+ params (Tuple[Any, ...], optional): The parameters to substitute into the query.
+
+ Returns:
+ List[Tuple]: The results fetched from the database.
+ """
+ try:
+ return self.execute_query(query, params)
+ except Exception as error:
+ print(f"Error querying database: {error}")
+ raise error
diff --git a/swarms/memory/utils.py b/swarms/memory/utils.py
index 46c7b020..42801237 100644
--- a/swarms/memory/utils.py
+++ b/swarms/memory/utils.py
@@ -26,7 +26,18 @@ def maximal_marginal_relevance(
lambda_mult: float = 0.5,
k: int = 4,
) -> List[int]:
- """Calculate maximal marginal relevance."""
+ """
+ Calculate maximal marginal relevance.
+
+ Args:
+ query_embedding (np.ndarray): The embedding of the query.
+ embedding_list (list): List of embeddings to select from.
+ lambda_mult (float, optional): The weight for query score. Defaults to 0.5.
+ k (int, optional): The number of embeddings to select. Defaults to 4.
+
+ Returns:
+ List[int]: List of indices of selected embeddings.
+ """
if min(k, len(embedding_list)) <= 0:
return []
if query_embedding.ndim == 1:
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/memory/weaviate_db.py b/swarms/memory/weaviate_db.py
new file mode 100644
index 00000000..05ad5388
--- /dev/null
+++ b/swarms/memory/weaviate_db.py
@@ -0,0 +1,182 @@
+"""
+Weaviate API Client
+"""
+
+from typing import Any, Dict, List, Optional
+
+from swarms.memory.base_vectordb import AbstractVectorDatabase
+
+try:
+ import weaviate
+except ImportError:
+ print("pip install weaviate-client")
+
+
+class WeaviateDB(AbstractVectorDatabase):
+ """
+
+ Weaviate API Client
+ Interface to Weaviate, a vector database with a GraphQL API.
+
+ Args:
+ http_host (str): The HTTP host of the Weaviate server.
+ http_port (str): The HTTP port of the Weaviate server.
+ http_secure (bool): Whether to use HTTPS.
+ grpc_host (Optional[str]): The gRPC host of the Weaviate server.
+ grpc_port (Optional[str]): The gRPC port of the Weaviate server.
+ grpc_secure (Optional[bool]): Whether to use gRPC over TLS.
+ auth_client_secret (Optional[Any]): The authentication client secret.
+ additional_headers (Optional[Dict[str, str]]): Additional headers to send with requests.
+ additional_config (Optional[weaviate.AdditionalConfig]): Additional configuration for the client.
+
+ Methods:
+ create_collection: Create a new collection in Weaviate.
+ add: Add an object to a specified collection.
+ query: Query objects from a specified collection.
+ update: Update an object in a specified collection.
+ delete: Delete an object from a specified collection.
+
+ Examples:
+ >>> from swarms.memory import WeaviateDB
+ """
+
+ def __init__(
+ self,
+ http_host: str,
+ http_port: str,
+ http_secure: bool,
+ grpc_host: Optional[str] = None,
+ grpc_port: Optional[str] = None,
+ grpc_secure: Optional[bool] = None,
+ auth_client_secret: Optional[Any] = None,
+ additional_headers: Optional[Dict[str, str]] = None,
+ additional_config: Optional[Any] = None,
+ connection_params: Dict[str, Any] = None,
+ *args,
+ **kwargs,
+ ):
+ super().__init__(*args, **kwargs)
+ self.http_host = http_host
+ self.http_port = http_port
+ self.http_secure = http_secure
+ self.grpc_host = grpc_host
+ self.grpc_port = grpc_port
+ self.grpc_secure = grpc_secure
+ self.auth_client_secret = auth_client_secret
+ self.additional_headers = additional_headers
+ self.additional_config = additional_config
+ self.connection_params = connection_params
+
+ # If connection_params are provided, use them to initialize the client.
+ connection_params = weaviate.ConnectionParams.from_params(
+ http_host=http_host,
+ http_port=http_port,
+ http_secure=http_secure,
+ grpc_host=grpc_host,
+ grpc_port=grpc_port,
+ grpc_secure=grpc_secure,
+ )
+
+ # If additional headers are provided, add them to the connection params.
+ self.client = weaviate.WeaviateDB(
+ connection_params=connection_params,
+ auth_client_secret=auth_client_secret,
+ additional_headers=additional_headers,
+ additional_config=additional_config,
+ )
+
+ def create_collection(
+ self,
+ name: str,
+ properties: List[Dict[str, Any]],
+ vectorizer_config: Any = None,
+ ):
+ """Create a new collection in Weaviate.
+
+ Args:
+ name (str): _description_
+ properties (List[Dict[str, Any]]): _description_
+ vectorizer_config (Any, optional): _description_. Defaults to None.
+ """
+ try:
+ out = self.client.collections.create(
+ name=name,
+ vectorizer_config=vectorizer_config,
+ properties=properties,
+ )
+ print(out)
+ except Exception as error:
+ print(f"Error creating collection: {error}")
+ raise
+
+ def add(self, collection_name: str, properties: Dict[str, Any]):
+ """Add an object to a specified collection.
+
+ Args:
+ collection_name (str): _description_
+ properties (Dict[str, Any]): _description_
+
+ Returns:
+ _type_: _description_
+ """
+ try:
+ collection = self.client.collections.get(collection_name)
+ return collection.data.insert(properties)
+ except Exception as error:
+ print(f"Error adding object: {error}")
+ raise
+
+ def query(
+ self, collection_name: str, query: str, limit: int = 10
+ ):
+ """Query objects from a specified collection.
+
+ Args:
+ collection_name (str): _description_
+ query (str): _description_
+ limit (int, optional): _description_. Defaults to 10.
+
+ Returns:
+ _type_: _description_
+ """
+ try:
+ collection = self.client.collections.get(collection_name)
+ response = collection.query.bm25(query=query, limit=limit)
+ return [o.properties for o in response.objects]
+ except Exception as error:
+ print(f"Error querying objects: {error}")
+ raise
+
+ def update(
+ self,
+ collection_name: str,
+ object_id: str,
+ properties: Dict[str, Any],
+ ):
+ """UPdate an object in a specified collection.
+
+ Args:
+ collection_name (str): _description_
+ object_id (str): _description_
+ properties (Dict[str, Any]): _description_
+ """
+ try:
+ collection = self.client.collections.get(collection_name)
+ collection.data.update(object_id, properties)
+ except Exception as error:
+ print(f"Error updating object: {error}")
+ raise
+
+ def delete(self, collection_name: str, object_id: str):
+ """Delete an object from a specified collection.
+
+ Args:
+ collection_name (str): _description_
+ object_id (str): _description_
+ """
+ try:
+ collection = self.client.collections.get(collection_name)
+ collection.data.delete_by_id(object_id)
+ except Exception as error:
+ print(f"Error deleting object: {error}")
+ raise
diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py
index 173c9487..00d9d1f2 100644
--- a/swarms/models/__init__.py
+++ b/swarms/models/__init__.py
@@ -1,4 +1,4 @@
-# LLMs
+############################################ LLMs
from swarms.models.base_llm import AbstractLLM # noqa: E402
from swarms.models.anthropic import Anthropic # noqa: E402
from swarms.models.petals import Petals # noqa: E402
@@ -10,16 +10,22 @@ from swarms.models.openai_models import (
) # noqa: E402
# from swarms.models.vllm import vLLM # noqa: E402
-
-# from swarms.models.zephyr import Zephyr # noqa: E402
+from swarms.models.zephyr import Zephyr # noqa: E402
from swarms.models.biogpt import BioGPT # noqa: E402
from swarms.models.huggingface import HuggingfaceLLM # noqa: E402
from swarms.models.wizard_storytelling import (
WizardLLMStoryTeller,
) # 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.together import TogetherLLM # noqa: E402
-# MultiModal Models
+################# MultiModal Models
from swarms.models.base_multimodal_model import (
BaseMultiModalModel,
) # noqa: E402
@@ -31,12 +37,51 @@ from swarms.models.layoutlm_document_qa import (
) # noqa: E402
from swarms.models.gpt4_vision_api import GPT4VisionAPI # noqa: E402
from swarms.models.openai_tts import OpenAITTS # noqa: E402
-from swarms.models.gemini import Gemini # noqa: E402
-# from swarms.models.gpt4v import GPT4Vision
+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.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.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
# 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
+
+
+################# Tokenizers
+
+
+############## Types
+from swarms.models.types import (
+ TextModality,
+ ImageModality,
+ AudioModality,
+ VideoModality,
+ 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",
@@ -46,10 +91,9 @@ __all__ = [
"OpenAI",
"AzureOpenAI",
"OpenAIChat",
- # "Zephyr",
+ "Zephyr",
"BaseMultiModalModel",
"Idefics",
- # "Kosmos",
"Vilt",
"Nougat",
"LayoutLMDocumentQA",
@@ -64,4 +108,26 @@ __all__ = [
# "vLLM",
"OpenAITTS",
"Gemini",
+ "Gigabind",
+ "Mixtral",
+ "ZeroscopeTTV",
+ "TextModality",
+ "ImageModality",
+ "AudioModality",
+ "VideoModality",
+ "MultimodalData",
+ "TogetherLLM",
+ "TimmModel",
+ "UltralyticsModel",
+ # "VipLlavaMultiModal",
+ "LavaMultiModal",
+ "QwenVLMultiModal",
+ "CLIPQ",
+ "Kosmos",
+ "Fuyu",
+ "BaseEmbeddingModel",
+ "RoboflowMultiModal",
+ "SegmentAnythingMarkGenerator",
+ "SamplingType",
+ "SamplingParams",
]
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/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/models/base_llm.py b/swarms/models/base_llm.py
index 0409b867..bc1f67c7 100644
--- a/swarms/models/base_llm.py
+++ b/swarms/models/base_llm.py
@@ -1,9 +1,11 @@
-import os
+import asyncio
import logging
+import os
import time
from abc import ABC, abstractmethod
-from typing import Optional, List
-import asyncio
+from typing import List, Optional
+
+from swarms.utils.llm_metrics_decorator import metrics_decorator
def count_tokens(text: str) -> int:
@@ -118,6 +120,7 @@ class AbstractLLM(ABC):
}
@abstractmethod
+ @metrics_decorator
def run(self, task: Optional[str] = None, *args, **kwargs) -> str:
"""generate text using language model"""
pass
@@ -381,3 +384,48 @@ class AbstractLLM(ABC):
TOKENS: {_num_tokens}
Tokens/SEC: {_time_for_generation}
"""
+
+ def time_to_first_token(self, prompt: str) -> float:
+ """Time to first token
+
+ Args:
+ prompt (str): _description_
+
+ Returns:
+ float: _description_
+ """
+ start_time = time.time()
+ self.track_resource_utilization(
+ prompt
+ ) # assuming `generate` is a method that generates tokens
+ first_token_time = time.time()
+ return first_token_time - start_time
+
+ def generation_latency(self, prompt: str) -> float:
+ """generation latency
+
+ Args:
+ prompt (str): _description_
+
+ Returns:
+ float: _description_
+ """
+ start_time = time.time()
+ self.run(prompt)
+ end_time = time.time()
+ return end_time - start_time
+
+ def throughput(self, prompts: List[str]) -> float:
+ """throughput
+
+ Args:
+ prompts (): _description_
+
+ Returns:
+ float: _description_
+ """
+ start_time = time.time()
+ for prompt in prompts:
+ self.run(prompt)
+ end_time = time.time()
+ return len(prompts) / (end_time - start_time)
diff --git a/swarms/models/base_multimodal_model.py b/swarms/models/base_multimodal_model.py
index c31931f2..c4a5890a 100644
--- a/swarms/models/base_multimodal_model.py
+++ b/swarms/models/base_multimodal_model.py
@@ -1,8 +1,6 @@
import asyncio
import base64
import concurrent.futures
-import logging
-import os
import time
from abc import abstractmethod
from concurrent.futures import ThreadPoolExecutor
@@ -69,7 +67,7 @@ class BaseMultiModalModel:
def __init__(
self,
- model_name: Optional[str],
+ model_name: Optional[str] = None,
temperature: Optional[int] = 0.5,
max_tokens: Optional[int] = 500,
max_workers: Optional[int] = 10,
@@ -100,13 +98,21 @@ class BaseMultiModalModel:
@abstractmethod
def run(
- self, task: Optional[str], img: Optional[str], *args, **kwargs
+ self,
+ task: Optional[str] = None,
+ img: Optional[str] = None,
+ *args,
+ **kwargs,
):
"""Run the model"""
pass
def __call__(
- self, task: str = None, img: str = None, *args, **kwargs
+ self,
+ task: Optional[str] = None,
+ img: Optional[str] = None,
+ *args,
+ **kwargs,
):
"""Call the model
diff --git a/swarms/models/base_tts.py b/swarms/models/base_tts.py
index 0faaf6ff..60896856 100644
--- a/swarms/models/base_tts.py
+++ b/swarms/models/base_tts.py
@@ -1,7 +1,7 @@
import wave
from typing import Optional
from swarms.models.base_llm import AbstractLLM
-from abc import ABC, abstractmethod
+from abc import abstractmethod
class BaseTTSModel(AbstractLLM):
diff --git a/swarms/models/base_ttv.py b/swarms/models/base_ttv.py
new file mode 100644
index 00000000..6ef959e8
--- /dev/null
+++ b/swarms/models/base_ttv.py
@@ -0,0 +1,115 @@
+from abc import abstractmethod
+from swarms.models.base_llm import AbstractLLM
+from diffusers.utils import export_to_video
+from typing import Optional, List
+import asyncio
+from concurrent.futures import ThreadPoolExecutor
+
+
+class BaseTextToVideo(AbstractLLM):
+ """BaseTextToVideo class represents prebuilt text-to-video models."""
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+
+ @abstractmethod
+ def run(self, *args, **kwargs):
+ pass
+
+ def __call__(
+ self,
+ task: Optional[str] = None,
+ img: Optional[str] = None,
+ *args,
+ **kwargs,
+ ):
+ """
+ Performs forward pass on the input task and returns the path of the generated video.
+
+ Args:
+ task (str): The task to perform.
+
+ Returns:
+ str: The path of the generated video.
+ """
+ return self.run(task, img, *args, **kwargs)
+
+ def save_video_path(
+ self, video_path: Optional[str] = None, *args, **kwargs
+ ):
+ """Saves the generated video to the specified path.
+
+ Args:
+ video_path (Optional[str], optional): _description_. Defaults to None.
+
+ Returns:
+ str: The path of the generated video.
+ """
+ return export_to_video(video_path, *args, **kwargs)
+
+ def run_batched(
+ self,
+ tasks: List[str] = None,
+ imgs: List[str] = None,
+ *args,
+ **kwargs,
+ ):
+ # TODO: Implement batched inference
+ tasks = tasks or []
+ imgs = imgs or []
+ if len(tasks) != len(imgs):
+ raise ValueError(
+ "The number of tasks and images should be the same."
+ )
+ return [
+ self.run(task, img, *args, **kwargs)
+ for task, img in zip(tasks, imgs)
+ ]
+
+ def run_concurrent_batched(
+ self,
+ tasks: List[str] = None,
+ imgs: List[str] = None,
+ *args,
+ **kwargs,
+ ):
+ tasks = tasks or []
+ imgs = imgs or []
+ if len(tasks) != len(imgs):
+ raise ValueError(
+ "The number of tasks and images should be the same."
+ )
+ with ThreadPoolExecutor(max_workers=4) as executor:
+ loop = asyncio.get_event_loop()
+ tasks = [
+ loop.run_in_executor(
+ executor, self.run, task, img, *args, **kwargs
+ )
+ for task, img in zip(tasks, imgs)
+ ]
+ return loop.run_until_complete(asyncio.gather(*tasks))
+
+ # Run the model in async mode
+ def arun(
+ self,
+ task: Optional[str] = None,
+ img: Optional[str] = None,
+ *args,
+ **kwargs,
+ ):
+ loop = asyncio.get_event_loop()
+ return loop.run_until_complete(
+ self.run(task, img, *args, **kwargs)
+ )
+
+ def arun_batched(
+ self,
+ tasks: List[str] = None,
+ imgs: List[str] = None,
+ *args,
+ **kwargs,
+ ):
+ loop = asyncio.get_event_loop()
+ return loop.run_until_complete(
+ self.run_batched(tasks, imgs, *args, **kwargs)
+ )
diff --git a/tests/models/test_distilled_whisperx.py b/swarms/models/base_vision_model.py
similarity index 100%
rename from tests/models/test_distilled_whisperx.py
rename to swarms/models/base_vision_model.py
diff --git a/swarms/models/bioclip.py b/swarms/models/bioclip.py
deleted file mode 100644
index e2d070af..00000000
--- a/swarms/models/bioclip.py
+++ /dev/null
@@ -1,183 +0,0 @@
-"""
-
-
-BiomedCLIP-PubMedBERT_256-vit_base_patch16_224
-https://huggingface.co/microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224
-BiomedCLIP is a biomedical vision-language foundation model that is pretrained on PMC-15M,
-a dataset of 15 million figure-caption pairs extracted from biomedical research articles in PubMed Central, using contrastive learning. It uses PubMedBERT as the text encoder and Vision Transformer as the image encoder, with domain-specific adaptations. It can perform various vision-language processing (VLP) tasks such as cross-modal retrieval, image classification, and visual question answering. BiomedCLIP establishes new state of the art in a wide range of standard datasets, and substantially outperforms prior VLP approaches:
-
-
-
-Citation
-@misc{https://doi.org/10.48550/arXiv.2303.00915,
- doi = {10.48550/ARXIV.2303.00915},
- url = {https://arxiv.org/abs/2303.00915},
- author = {Zhang, Sheng and Xu, Yanbo and Usuyama, Naoto and Bagga, Jaspreet and Tinn, Robert and Preston, Sam and Rao, Rajesh and Wei, Mu and Valluri, Naveen and Wong, Cliff and Lungren, Matthew and Naumann, Tristan and Poon, Hoifung},
- title = {Large-Scale Domain-Specific Pretraining for Biomedical Vision-Language Processing},
- publisher = {arXiv},
- year = {2023},
-}
-
-Model Use
-How to use
-Please refer to this example notebook.
-
-Intended Use
-This model is intended to be used solely for (I) future research on visual-language processing and (II) reproducibility of the experimental results reported in the reference paper.
-
-Primary Intended Use
-The primary intended use is to support AI researchers building on top of this work. BiomedCLIP and its associated models should be helpful for exploring various biomedical VLP research questions, especially in the radiology domain.
-
-Out-of-Scope Use
-Any deployed use case of the model --- commercial or otherwise --- is currently out of scope. Although we evaluated the models using a broad set of publicly-available research benchmarks, the models and evaluations are not intended for deployed use cases. Please refer to the associated paper for more details.
-
-Data
-This model builds upon PMC-15M dataset, which is a large-scale parallel image-text dataset for biomedical vision-language processing. It contains 15 million figure-caption pairs extracted from biomedical research articles in PubMed Central. It covers a diverse range of biomedical image types, such as microscopy, radiography, histology, and more.
-
-Limitations
-This model was developed using English corpora, and thus can be considered English-only.
-
-Further information
-Please refer to the corresponding paper, "Large-Scale Domain-Specific Pretraining for Biomedical Vision-Language Processing" for additional details on the model training and evaluation.
-"""
-
-import open_clip
-import torch
-from PIL import Image
-import matplotlib.pyplot as plt
-
-
-class BioClip:
- """
- BioClip
-
- Args:
- model_path (str): path to the model
-
- Attributes:
- model_path (str): path to the model
- model (torch.nn.Module): the model
- preprocess_train (torchvision.transforms.Compose): the preprocessing pipeline for training
- preprocess_val (torchvision.transforms.Compose): the preprocessing pipeline for validation
- tokenizer (open_clip.Tokenizer): the tokenizer
- device (torch.device): the device to run the model on
-
- Methods:
- __call__(self, img_path: str, labels: list, template: str = 'this is a photo of ', context_length: int = 256):
- returns a dictionary of labels and their probabilities
- plot_image_with_metadata(img_path: str, metadata: dict): plots the image with the metadata
-
- Usage:
- clip = BioClip('hf-hub:microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224')
-
- labels = [
- 'adenocarcinoma histopathology',
- 'brain MRI',
- 'covid line chart',
- 'squamous cell carcinoma histopathology',
- 'immunohistochemistry histopathology',
- 'bone X-ray',
- 'chest X-ray',
- 'pie chart',
- 'hematoxylin and eosin histopathology'
- ]
-
- result = clip("your_image_path.jpg", labels)
- metadata = {'filename': "your_image_path.jpg".split('/')[-1], 'top_probs': result}
- clip.plot_image_with_metadata("your_image_path.jpg", metadata)
-
-
- """
-
- def __init__(self, model_path: str):
- self.model_path = model_path
- (
- self.model,
- self.preprocess_train,
- self.preprocess_val,
- ) = open_clip.create_model_and_transforms(model_path)
- self.tokenizer = open_clip.get_tokenizer(model_path)
- self.device = (
- torch.device("cuda")
- if torch.cuda.is_available()
- else torch.device("cpu")
- )
- self.model.to(self.device)
- self.model.eval()
-
- def __call__(
- self,
- img_path: str,
- labels: list,
- template: str = "this is a photo of ",
- context_length: int = 256,
- ):
- image = torch.stack(
- [self.preprocess_val(Image.open(img_path))]
- ).to(self.device)
- texts = self.tokenizer(
- [template + l for l in labels],
- context_length=context_length,
- ).to(self.device)
-
- with torch.no_grad():
- image_features, text_features, logit_scale = self.model(
- image, texts
- )
- logits = (
- (logit_scale * image_features @ text_features.t())
- .detach()
- .softmax(dim=-1)
- )
- sorted_indices = torch.argsort(
- logits, dim=-1, descending=True
- )
- logits = logits.cpu().numpy()
- sorted_indices = sorted_indices.cpu().numpy()
-
- results = {}
- for idx in sorted_indices[0]:
- label = labels[idx]
- prob = logits[0][idx]
- results[label] = prob
- return results
-
- @staticmethod
- def plot_image_with_metadata(img_path: str, metadata: dict):
- img = Image.open(img_path)
- fig, ax = plt.subplots(figsize=(5, 5))
- ax.imshow(img)
- ax.axis("off")
- title = (
- metadata["filename"]
- + "\n"
- + "\n".join(
- [
- f"{k}: {v*100:.1f}"
- for k, v in metadata["top_probs"].items()
- ]
- )
- )
- ax.set_title(title, fontsize=14)
- plt.tight_layout()
- plt.show()
-
-
-# Usage
-# clip = BioClip('hf-hub:microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224')
-
-# labels = [
-# 'adenocarcinoma histopathology',
-# 'brain MRI',
-# 'covid line chart',
-# 'squamous cell carcinoma histopathology',
-# 'immunohistochemistry histopathology',
-# 'bone X-ray',
-# 'chest X-ray',
-# 'pie chart',
-# 'hematoxylin and eosin histopathology'
-# ]
-
-# result = clip("your_image_path.jpg", labels)
-# metadata = {'filename': "your_image_path.jpg".split('/')[-1], 'top_probs': result}
-# clip.plot_image_with_metadata("your_image_path.jpg", metadata)
diff --git a/swarms/models/chest_agent.py b/swarms/models/chest_agent.py
new file mode 100644
index 00000000..2867596c
--- /dev/null
+++ b/swarms/models/chest_agent.py
@@ -0,0 +1,89 @@
+import io
+import requests
+import torch
+from PIL import Image
+from transformers import (
+ AutoModelForCausalLM,
+ AutoProcessor,
+ GenerationConfig,
+)
+from swarms.models.base_multimodal_model import (
+ BaseMultiModalModel,
+) # noqa: F401
+
+
+class ChestMultiModalAgent(BaseMultiModalModel):
+ """
+ Initialize the ChestAgent.
+
+ Args:
+ device (str): The device to run the model on. Default is "cuda".
+ dtype (torch.dtype): The data type to use for the model. Default is torch.float16.
+ model_name (str): The name or path of the pre-trained model to use. Default is "StanfordAIMI/CheXagent-8b".
+
+ Example:
+ >>> agent = ChestAgent()
+ >>> agent.run("What are the symptoms of COVID-19?", "https://example.com/image.jpg")
+
+ """
+
+ def __init__(
+ self,
+ device="cuda",
+ dtype=torch.float16,
+ model_name="StanfordAIMI/CheXagent-8b",
+ *args,
+ **kwargs,
+ ):
+ # Step 1: Setup constants
+ self.device = device
+ self.dtype = dtype
+
+ # Step 2: Load Processor and Model
+ self.processor = AutoProcessor.from_pretrained(
+ model_name, trust_remote_code=True
+ )
+ self.generation_config = GenerationConfig.from_pretrained(
+ model_name
+ )
+ self.model = AutoModelForCausalLM.from_pretrained(
+ model_name,
+ torch_dtype=self.dtype,
+ trust_remote_code=True,
+ *args,
+ **kwargs,
+ )
+
+ def run(self, task: str, img: str, *args, **kwargs):
+ """
+ Run the ChestAgent to generate findings based on an image and a prompt.
+
+ Args:
+ image_path (str): The URL or local path of the image.
+ prompt (str): The prompt to use for generating findings.
+
+ Returns:
+ str: The generated findings.
+ """
+ # Step 3: Fetch the images
+ images = [
+ Image.open(io.BytesIO(requests.get(img).content)).convert(
+ "RGB"
+ )
+ ]
+
+ # Step 4: Generate the Findings section
+ inputs = self.processor(
+ images=images,
+ text=f" USER: {task} ASSISTANT: ",
+ return_tensors="pt",
+ ).to(device=self.device, dtype=self.dtype)
+ output = self.model.generate(
+ **inputs,
+ generation_config=self.generation_config,
+ )[0]
+ response = self.processor.tokenizer.decode(
+ output, skip_special_tokens=True
+ )
+
+ return response
diff --git a/swarms/models/clipq.py b/swarms/models/clipq.py
new file mode 100644
index 00000000..7e49e74a
--- /dev/null
+++ b/swarms/models/clipq.py
@@ -0,0 +1,183 @@
+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
diff --git a/swarms/models/cog_agent.py b/swarms/models/cog_agent.py
new file mode 100644
index 00000000..2d0d09e9
--- /dev/null
+++ b/swarms/models/cog_agent.py
@@ -0,0 +1,129 @@
+import torch
+from PIL import Image
+from modelscope import AutoModelForCausalLM, AutoTokenizer
+from swarms.models.base_multimodal_model import BaseMultiModalModel
+
+device_check = "cuda" if torch.cuda.is_available() else "cpu"
+
+
+class CogAgent(BaseMultiModalModel):
+ """CogAgent
+
+ Multi-modal conversational agent that can be used to chat with
+ images and text. It is based on the CogAgent model from the
+ ModelScope library.
+
+ Attributes:
+ model_name (str): The name of the model to be used
+ tokenizer_name (str): The name of the tokenizer to be used
+ dtype (torch.bfloat16): The data type to be used
+ low_cpu_mem_usage (bool): Whether to use low CPU memory
+ load_in_4bit (bool): Whether to load in 4-bit
+ trust_remote_code (bool): Whether to trust remote code
+ device (str): The device to be used
+
+ Examples:
+ >>> from swarms.models.cog_agent import CogAgent
+ >>> cog_agent = CogAgent()
+ >>> cog_agent.run("How are you?", "images/1.jpg")
+ I'm fine. How are you?
+ """
+
+ def __init__(
+ self,
+ model_name: str = "ZhipuAI/cogagent-chat",
+ tokenizer_name: str = "I-ModelScope/vicuna-7b-v1.5",
+ dtype=torch.bfloat16,
+ low_cpu_mem_usage: bool = True,
+ load_in_4bit: bool = True,
+ trust_remote_code: bool = True,
+ device=device_check,
+ *args,
+ **kwargs,
+ ):
+ super().__init__()
+ self.model_name = model_name
+ self.tokenizer_name = tokenizer_name
+ self.dtype = dtype
+ self.low_cpu_mem_usage = low_cpu_mem_usage
+ self.load_in_4bit = load_in_4bit
+ self.trust_remote_code = trust_remote_code
+ self.device = device
+
+ self.model = (
+ AutoModelForCausalLM.from_pretrained(
+ self.model_name,
+ torch_dtype=self.dtype,
+ low_cpu_mem_usage=self.low_cpu_mem_usage,
+ load_in_4bit=self.load_in_4bit,
+ trust_remote_code=self.trust_remote_code,
+ *args,
+ **kwargs,
+ )
+ .to(self.device)
+ .eval()
+ )
+
+ self.tokenizer = AutoTokenizer.from_pretrained(
+ self.tokenizer_name
+ )
+
+ def run(self, task: str, img: str, *args, **kwargs):
+ """Run the model
+
+ Args:
+ task (str): The task to be performed
+ img (str): The image path
+
+ """
+ image = Image.open(img).convert("RGB")
+
+ input_by_model = self.model.build_conversation_input_ids(
+ self.tokenizer,
+ query=task,
+ history=[],
+ images=[image],
+ )
+
+ inputs = {
+ "input_ids": (
+ input_by_model["input_ids"]
+ .unsqueeze(0)
+ .to(self.device)
+ ),
+ "token_type_ids": (
+ input_by_model["token_type_ids"]
+ .unsqueeze(0)
+ .to(self.device)
+ ),
+ "attention_mask": (
+ input_by_model["attention_mask"]
+ .unsqueeze(0)
+ .to(self.device)
+ ),
+ "images": [
+ [
+ input_by_model["images"][0]
+ .to(self.device)
+ .to(self.dtype)
+ ]
+ ],
+ }
+ if (
+ "cross_images" in input_by_model
+ and input_by_model["cross_images"]
+ ):
+ inputs["cross_images"] = [
+ [
+ input_by_model["cross_images"][0]
+ .to(self.device)
+ .to(self.dtype)
+ ]
+ ]
+
+ with torch.no_grad():
+ outputs = self.model(**inputs, **kwargs)
+ outputs = outputs[:, inputs["input_ids"].shape[1] :]
+ response = self.decode(outputs[0])
+ response = response.split(" ")[0]
+ print(response)
diff --git a/swarms/models/dalle3.py b/swarms/models/dalle3.py
index 40f63418..6b225b49 100644
--- a/swarms/models/dalle3.py
+++ b/swarms/models/dalle3.py
@@ -18,8 +18,6 @@ from termcolor import colored
load_dotenv()
-# api_key = os.getenv("OPENAI_API_KEY")
-
# Configure Logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
diff --git a/swarms/models/diffusers_general.py b/swarms/models/diffusers_general.py
new file mode 100644
index 00000000..9d7ea250
--- /dev/null
+++ b/swarms/models/diffusers_general.py
@@ -0,0 +1 @@
+# Base implementation for the diffusers library
diff --git a/swarms/models/fastvit.py b/swarms/models/fastvit.py
deleted file mode 100644
index a6fc31f8..00000000
--- a/swarms/models/fastvit.py
+++ /dev/null
@@ -1,88 +0,0 @@
-import json
-import os
-from typing import List
-
-import timm
-import torch
-from PIL import Image
-from pydantic import BaseModel, StrictFloat, StrictInt, validator
-
-DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
-
-# Load the classes for image classification
-with open(
- os.path.join(os.path.dirname(__file__), "fast_vit_classes.json")
-) as f:
- FASTVIT_IMAGENET_1K_CLASSES = json.load(f)
-
-
-class ClassificationResult(BaseModel):
- class_id: List[StrictInt]
- confidence: List[StrictFloat]
-
- @validator("class_id", "confidence", pre=True, each_item=True)
- def check_list_contents(cls, v):
- assert isinstance(v, int) or isinstance(
- v, float
- ), "must be integer or float"
- return v
-
-
-class FastViT:
- """
- FastViT model for image classification
-
- Args:
- img (str): path to the input image
- confidence_threshold (float): confidence threshold for the model's predictions
-
- Returns:
- ClassificationResult: a pydantic BaseModel containing the class ids and confidences of the model's predictions
-
-
- Example:
- >>> fastvit = FastViT()
- >>> result = fastvit(img="path_to_image.jpg", confidence_threshold=0.5)
-
-
- To use, create a json file called: fast_vit_classes.json
-
- """
-
- def __init__(self):
- self.model = timm.create_model(
- "hf_hub:timm/fastvit_s12.apple_in1k", pretrained=True
- ).to(DEVICE)
- data_config = timm.data.resolve_model_data_config(self.model)
- self.transforms = timm.data.create_transform(
- **data_config, is_training=False
- )
- self.model.eval()
-
- def __call__(
- self, img: str, confidence_threshold: float = 0.5
- ) -> ClassificationResult:
- """classifies the input image and returns the top k classes and their probabilities"""
- img = Image.open(img).convert("RGB")
- img_tensor = self.transforms(img).unsqueeze(0).to(DEVICE)
- with torch.no_grad():
- output = self.model(img_tensor)
- probabilities = torch.nn.functional.softmax(output, dim=1)
-
- # Get top k classes and their probabilities
- top_probs, top_classes = torch.topk(
- probabilities, k=FASTVIT_IMAGENET_1K_CLASSES
- )
-
- # Filter by confidence threshold
- mask = top_probs > confidence_threshold
- top_probs, top_classes = top_probs[mask], top_classes[mask]
-
- # Convert to Python lists and map class indices to labels if needed
- top_probs = top_probs.cpu().numpy().tolist()
- top_classes = top_classes.cpu().numpy().tolist()
- # top_class_labels = [FASTVIT_IMAGENET_1K_CLASSES[i] for i in top_classes] # Uncomment if class labels are needed
-
- return ClassificationResult(
- class_id=top_classes, confidence=top_probs
- )
diff --git a/swarms/models/fuyu.py b/swarms/models/fuyu.py
index f722bbb6..e02e53a5 100644
--- a/swarms/models/fuyu.py
+++ b/swarms/models/fuyu.py
@@ -49,11 +49,11 @@ class Fuyu(BaseMultiModalModel):
self.processor = FuyuProcessor(
image_processor=self.image_processor,
tokenizer=self.tokenizer,
- **kwargs,
)
self.model = FuyuForCausalLM.from_pretrained(
model_name,
device_map=device_map,
+ *args,
**kwargs,
)
@@ -62,7 +62,7 @@ class Fuyu(BaseMultiModalModel):
image_pil = Image.open(img)
return image_pil
- def run(self, text: str, img: str, *args, **kwargs):
+ def run(self, text: str = None, img: str = None, *args, **kwargs):
"""Run the pipeline
Args:
@@ -78,8 +78,6 @@ class Fuyu(BaseMultiModalModel):
text=text,
images=[img],
device=self.device_map,
- *args,
- **kwargs,
)
for k, v in model_inputs.items():
@@ -94,8 +92,6 @@ class Fuyu(BaseMultiModalModel):
text = self.processor.batch_decode(
output[:, -7:],
skip_special_tokens=True,
- *args,
- **kwargs,
)
return print(str(text))
except Exception as error:
diff --git a/swarms/models/gemini.py b/swarms/models/gemini.py
index 9e8ea8f9..d12ea7d9 100644
--- a/swarms/models/gemini.py
+++ b/swarms/models/gemini.py
@@ -49,7 +49,7 @@ class Gemini(BaseMultiModalModel):
stream (bool, optional): _description_. Defaults to False.
candidate_count (int, optional): _description_. Defaults to 1.
stop_sequence ([type], optional): _description_. Defaults to ['x'].
- max_output_tokens (int, optional): _description_. Defaults to 100.
+ max_tokens (int, optional): _description_. Defaults to 100.
temperature (float, optional): _description_. Defaults to 0.9.
Methods:
@@ -79,8 +79,9 @@ class Gemini(BaseMultiModalModel):
candidates: bool = False,
stream: bool = False,
candidate_count: int = 1,
+ transport: str = "rest",
stop_sequence=["x"],
- max_output_tokens: int = 100,
+ max_tokens: int = 100,
temperature: float = 0.9,
system_prompt: str = None,
*args,
@@ -94,15 +95,18 @@ class Gemini(BaseMultiModalModel):
self.stream = stream
self.candidate_count = candidate_count
self.stop_sequence = stop_sequence
- self.max_output_tokens = max_output_tokens
+ self.max_tokens = max_tokens
self.temperature = temperature
self.system_prompt = system_prompt
+ # Configure the API key
+ genai.configure(api_key=gemini_api_key, transport=transport)
+
# Prepare the generation config
self.generation_config = GenerationConfig(
candidate_count=candidate_count,
# stop_sequence=stop_sequence,
- max_output_tokens=max_output_tokens,
+ max_output_tokens=max_tokens,
temperature=temperature,
*args,
**kwargs,
@@ -117,9 +121,8 @@ class Gemini(BaseMultiModalModel):
if self.gemini_api_key is None:
raise ValueError("Please provide a Gemini API key")
- def system_prompt(
+ def system_prompt_prep(
self,
- system_prompt: str = None,
task: str = None,
*args,
**kwargs,
@@ -131,7 +134,9 @@ class Gemini(BaseMultiModalModel):
"""
PROMPT = f"""
- {system_prompt}
+ {self.system_prompt}
+
+ ######
{task}
@@ -155,28 +160,24 @@ class Gemini(BaseMultiModalModel):
str: output from the model
"""
try:
+ prepare_prompt = self.system_prompt_prep(task)
if img:
# process_img = self.process_img(img, *args, **kwargs)
process_img = self.process_img_pil(img)
response = self.model.generate_content(
- contents=[task, process_img],
+ contents=[prepare_prompt, process_img],
generation_config=self.generation_config,
stream=self.stream,
*args,
**kwargs,
)
-
- # if self.candidates:
- # return response.candidates
- # elif self.safety:
- # return response.safety
- # else:
- # return response.text
-
return response.text
else:
response = self.model.generate_content(
- task, stream=self.stream, *args, **kwargs
+ prepare_prompt,
+ stream=self.stream,
+ *args,
+ **kwargs,
)
return response.text
except Exception as error:
diff --git a/swarms/models/gigabind.py b/swarms/models/gigabind.py
new file mode 100644
index 00000000..97d6d6cd
--- /dev/null
+++ b/swarms/models/gigabind.py
@@ -0,0 +1,106 @@
+import requests
+from tenacity import retry, stop_after_attempt, wait_fixed
+
+
+class Gigabind:
+ """Gigabind API.
+
+ Args:
+ host (str, optional): host. Defaults to None.
+ proxy_url (str, optional): proxy_url. Defaults to None.
+ port (int, optional): port. Defaults to 8000.
+ endpoint (str, optional): endpoint. Defaults to "embeddings".
+
+ Examples:
+ >>> from swarms.models.gigabind import Gigabind
+ >>> api = Gigabind(host="localhost", port=8000, endpoint="embeddings")
+ >>> response = api.run(text="Hello, world!", vision="image.jpg")
+ >>> print(response)
+ """
+
+ def __init__(
+ self,
+ host: str = None,
+ proxy_url: str = None,
+ port: int = 8000,
+ endpoint: str = "embeddings",
+ *args,
+ **kwargs,
+ ):
+ super().__init__(*args, **kwargs)
+ self.host = host
+ self.proxy_url = proxy_url
+ self.port = port
+ self.endpoint = endpoint
+
+ # Set the URL to the API
+ if self.proxy_url is not None:
+ self.url = f"{self.proxy_url}"
+ else:
+ self.url = f"http://{host}:{port}/{endpoint}"
+
+ @retry(stop=stop_after_attempt(3), wait=wait_fixed(2))
+ def run(
+ self,
+ text: str = None,
+ vision: str = None,
+ audio: str = None,
+ *args,
+ **kwargs,
+ ):
+ """Run the Gigabind API.
+
+ Args:
+ text (str, optional): text. Defaults to None.
+ vision (str, optional): images. Defaults to None.
+ audio (str, optional): audio file paths. Defaults to None.
+
+ Raises:
+ ValueError: At least one of text, vision or audio must be provided
+
+ Returns:
+ embeddings: embeddings
+ """
+ try:
+ # Prepare the data to send to the API
+ data = {}
+ if text is not None:
+ data["text"] = text
+ if vision is not None:
+ data["vision"] = vision
+ if audio is not None:
+ data["audio"] = audio
+ else:
+ raise ValueError(
+ "At least one of text, vision or audio must be"
+ " provided"
+ )
+
+ # Send a POST request to the API and return the response
+ response = requests.post(
+ self.url, json=data, *args, **kwargs
+ )
+ return response.json()
+ except Exception as error:
+ print(f"Gigabind API error: {error}")
+ return None
+
+ def generate_summary(self, text: str = None, *args, **kwargs):
+ # Prepare the data to send to the API
+ data = {}
+ if text is not None:
+ data["text"] = text
+ else:
+ raise ValueError(
+ "At least one of text, vision or audio must be"
+ " provided"
+ )
+
+ # Send a POST request to the API and return the response
+ response = requests.post(self.url, json=data, *args, **kwargs)
+ return response.json()
+
+
+# api = Gigabind(host="localhost", port=8000, endpoint="embeddings")
+# response = api.run(text="Hello, world!", vision="image.jpg")
+# print(response)
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/huggingface.py b/swarms/models/huggingface.py
index bbb39223..957b398f 100644
--- a/swarms/models/huggingface.py
+++ b/swarms/models/huggingface.py
@@ -3,18 +3,18 @@ import concurrent.futures
import logging
from typing import List, Tuple
-
import torch
from termcolor import colored
-from torch.nn.parallel import DistributedDataParallel as DDP
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
BitsAndBytesConfig,
)
+from swarms.models.base_llm import AbstractLLM
+
-class HuggingfaceLLM:
+class HuggingfaceLLM(AbstractLLM):
"""
A class for running inference on a given model.
@@ -123,7 +123,6 @@ class HuggingfaceLLM:
quantize: bool = False,
quantization_config: dict = None,
verbose=False,
- # logger=None,
distributed=False,
decoding=False,
max_workers: int = 5,
@@ -132,9 +131,11 @@ class HuggingfaceLLM:
temperature: float = 0.7,
top_k: int = 40,
top_p: float = 0.8,
+ dtype=torch.bfloat16,
*args,
**kwargs,
):
+ super().__init__(*args, **kwargs)
self.logger = logging.getLogger(__name__)
self.device = (
device
@@ -146,7 +147,6 @@ class HuggingfaceLLM:
self.verbose = verbose
self.distributed = distributed
self.decoding = decoding
- self.model, self.tokenizer = None, None
self.quantize = quantize
self.quantization_config = quantization_config
self.max_workers = max_workers
@@ -155,6 +155,7 @@ class HuggingfaceLLM:
self.temperature = temperature
self.top_k = top_k
self.top_p = top_p
+ self.dtype = dtype
if self.distributed:
assert (
@@ -168,34 +169,23 @@ class HuggingfaceLLM:
"load_in_4bit": True,
"bnb_4bit_use_double_quant": True,
"bnb_4bit_quant_type": "nf4",
- "bnb_4bit_compute_dtype": torch.bfloat16,
+ "bnb_4bit_compute_dtype": dtype,
}
bnb_config = BitsAndBytesConfig(**quantization_config)
- try:
- self.tokenizer = AutoTokenizer.from_pretrained(
- self.model_id, *args, **kwargs
- )
+ self.tokenizer = AutoTokenizer.from_pretrained(self.model_id)
+
+ if quantize:
self.model = AutoModelForCausalLM.from_pretrained(
self.model_id,
quantization_config=bnb_config,
*args,
**kwargs,
- )
-
- self.model # .to(self.device)
- except Exception as e:
- # self.logger.error(f"Failed to load the model or the tokenizer: {e}")
- # raise
- print(
- colored(
- (
- "Failed to load the model and or the"
- f" tokenizer: {e}"
- ),
- "red",
- )
- )
+ ).to(self.device)
+ else:
+ self.model = AutoModelForCausalLM.from_pretrained(
+ self.model_id, *args, **kwargs
+ ).to(self.device)
def print_error(self, error: str):
"""Print error"""
@@ -205,33 +195,6 @@ class HuggingfaceLLM:
"""Ashcnronous generate text for a given prompt"""
return await asyncio.to_thread(self.run, task)
- def load_model(self):
- """Load the model"""
- if not self.model or not self.tokenizer:
- try:
- self.tokenizer = AutoTokenizer.from_pretrained(
- self.model_id
- )
-
- bnb_config = (
- BitsAndBytesConfig(**self.quantization_config)
- if self.quantization_config
- else None
- )
-
- self.model = AutoModelForCausalLM.from_pretrained(
- self.model_id, quantization_config=bnb_config
- ).to(self.device)
-
- if self.distributed:
- self.model = DDP(self.model)
- except Exception as error:
- self.logger.error(
- "Failed to load the model or the tokenizer:"
- f" {error}"
- )
- raise
-
def concurrent_run(self, tasks: List[str], max_workers: int = 5):
"""Concurrently generate text for a list of prompts."""
with concurrent.futures.ThreadPoolExecutor(
@@ -252,7 +215,7 @@ class HuggingfaceLLM:
results = [future.result() for future in futures]
return results
- def run(self, task: str):
+ def run(self, task: str, *args, **kwargs):
"""
Generate a response based on the prompt text.
@@ -263,20 +226,12 @@ class HuggingfaceLLM:
Returns:
- Generated text (str).
"""
- self.load_model()
-
- max_length = self.max_length
-
- self.print_dashboard(task)
-
try:
inputs = self.tokenizer.encode(task, return_tensors="pt")
- # self.log.start()
-
if self.decoding:
with torch.no_grad():
- for _ in range(max_length):
+ for _ in range(self.max_length):
output_sequence = []
outputs = self.model.generate(
@@ -300,10 +255,13 @@ class HuggingfaceLLM:
else:
with torch.no_grad():
outputs = self.model.generate(
- inputs, max_length=max_length, do_sample=True
+ inputs,
+ max_length=self.max_length,
+ do_sample=True,
+ *args,
+ **kwargs,
)
- del inputs
return self.tokenizer.decode(
outputs[0], skip_special_tokens=True
)
@@ -320,67 +278,8 @@ class HuggingfaceLLM:
)
raise
- def __call__(self, task: str):
- """
- Generate a response based on the prompt text.
-
- Args:
- - task (str): Text to prompt the model.
- - max_length (int): Maximum length of the response.
-
- Returns:
- - Generated text (str).
- """
- self.load_model()
-
- max_length = self.max_length
-
- self.print_dashboard(task)
-
- try:
- inputs = self.tokenizer.encode(
- task, return_tensors="pt"
- ).to(self.device)
-
- # self.log.start()
-
- if self.decoding:
- with torch.no_grad():
- for _ in range(max_length):
- output_sequence = []
-
- outputs = self.model.generate(
- inputs,
- max_length=len(inputs) + 1,
- do_sample=True,
- )
- output_tokens = outputs[0][-1]
- output_sequence.append(output_tokens.item())
-
- # print token in real-time
- print(
- self.tokenizer.decode(
- [output_tokens],
- skip_special_tokens=True,
- ),
- end="",
- flush=True,
- )
- inputs = outputs
- else:
- with torch.no_grad():
- outputs = self.model.generate(
- inputs, max_length=max_length, do_sample=True
- )
-
- del inputs
-
- return self.tokenizer.decode(
- outputs[0], skip_special_tokens=True
- )
- except Exception as e:
- self.logger.error(f"Failed to generate the text: {e}")
- raise
+ def __call__(self, task: str, *args, **kwargs):
+ return self.run(task, *args, **kwargs)
async def __call_async__(self, task: str, *args, **kwargs) -> str:
"""Call the model asynchronously""" ""
diff --git a/swarms/models/inference_engine.py b/swarms/models/inference_engine.py
new file mode 100644
index 00000000..e69de29b
diff --git a/swarms/models/jina_embeds.py b/swarms/models/jina_embeds.py
index c294f764..ea621993 100644
--- a/swarms/models/jina_embeds.py
+++ b/swarms/models/jina_embeds.py
@@ -1,3 +1,4 @@
+import os
import logging
import torch
@@ -8,67 +9,71 @@ from transformers import (
AutoTokenizer,
BitsAndBytesConfig,
)
+from swarms.models.base_embedding_model import BaseEmbeddingModel
def cos_sim(a, b):
return a @ b.T / (norm(a) * norm(b))
-class JinaEmbeddings:
+class JinaEmbeddings(BaseEmbeddingModel):
"""
- A class for running inference on a given model.
-
- Attributes:
- model_id (str): The ID of the model.
- device (str): The device to run the model on (either 'cuda' or 'cpu').
- max_length (int): The maximum length of the output sequence.
- quantize (bool, optional): Whether to use quantization. Defaults to False.
- quantization_config (dict, optional): The configuration for quantization.
- verbose (bool, optional): Whether to print verbose logs. Defaults to False.
- logger (logging.Logger, optional): The logger to use. Defaults to a basic logger.
-
- # Usage
- ```
- from swarms.models import JinaEmbeddings
-
- model = JinaEmbeddings()
-
- embeddings = model("Encode this text")
-
- print(embeddings)
-
-
- ```
+ Jina Embeddings model.
+
+ Args:
+ model_id (str): The model id to use. Default is "jinaai/jina-embeddings-v2-base-en".
+ device (str): The device to run the model on. Default is "cuda".
+ huggingface_api_key (str): The Hugging Face API key. Default is None.
+ max_length (int): The maximum length of the response. Default is 500.
+ quantize (bool): Whether to quantize the model. Default is False.
+ quantization_config (dict): The quantization configuration. Default is None.
+ verbose (bool): Whether to print verbose logs. Default is False.
+ distributed (bool): Whether to use distributed processing. Default is False.
+ decoding (bool): Whether to use decoding. Default is False.
+ cos_sim (callable): The cosine similarity function. Default is cos_sim.
+
+ Methods:
+ run: _description_
+
+ Examples:
+ >>> model = JinaEmbeddings(
+ >>> max_length=8192,
+ >>> device="cuda",
+ >>> quantize=True,
+ >>> huggingface_api_key="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"
+ >>> )
+ >>> embeddings = model("Encode this super long document text")
"""
def __init__(
self,
- model_id: str,
+ model_id: str = "jinaai/jina-embeddings-v2-base-en",
device: str = None,
+ huggingface_api_key: str = None,
max_length: int = 500,
quantize: bool = False,
quantization_config: dict = None,
verbose=False,
- # logger=None,
distributed=False,
decoding=False,
- cos_sim: bool = False,
+ cos_sim: callable = cos_sim,
*args,
**kwargs,
):
+ super().__init__(*args, **kwargs)
self.logger = logging.getLogger(__name__)
self.device = (
device
if device
else ("cuda" if torch.cuda.is_available() else "cpu")
)
+ self.huggingface_api_key = huggingface_api_key
self.model_id = model_id
self.max_length = max_length
self.verbose = verbose
self.distributed = distributed
self.decoding = decoding
self.model, self.tokenizer = None, None
- # self.log = Logging()
self.cos_sim = cos_sim
if self.distributed:
@@ -76,6 +81,10 @@ class JinaEmbeddings:
torch.cuda.device_count() > 1
), "You need more than 1 gpu for distributed processing"
+ # If API key then set it
+ if self.huggingface_api_key:
+ os.environ["HF_TOKEN"] = self.huggingface_api_key
+
bnb_config = None
if quantize:
if not quantization_config:
@@ -101,7 +110,6 @@ class JinaEmbeddings:
)
raise
- def load_model(self):
"""Load the model"""
if not self.model or not self.tokenizer:
try:
@@ -130,7 +138,7 @@ class JinaEmbeddings:
)
raise
- def run(self, task: str):
+ def run(self, task: str, *args, **kwargs):
"""
Generate a response based on the prompt text.
@@ -141,13 +149,12 @@ class JinaEmbeddings:
Returns:
- Generated text (str).
"""
- self.load_model()
max_length = self.max_length
try:
embeddings = self.model.encode(
- [task], max_length=max_length
+ [task], max_length=max_length, *args, **kwargs
)
if self.cos_sim:
@@ -181,7 +188,7 @@ class JinaEmbeddings:
# Wrapping synchronous calls with async
return self.run(task, *args, **kwargs)
- def __call__(self, task: str):
+ def __call__(self, task: str, *args, **kwargs):
"""
Generate a response based on the prompt text.
@@ -198,7 +205,7 @@ class JinaEmbeddings:
try:
embeddings = self.model.encode(
- [task], max_length=max_length
+ [task], max_length=max_length, *args, **kwargs
)
if self.cos_sim:
@@ -231,3 +238,6 @@ class JinaEmbeddings:
return {"allocated": allocated, "reserved": reserved}
else:
return {"error": "GPU not available"}
+
+ def try_embed_chunk(self, chunk: str) -> list[float]:
+ return super().try_embed_chunk(chunk)
diff --git a/swarms/models/kosmos2.py b/swarms/models/kosmos2.py
deleted file mode 100644
index 9a9a0de3..00000000
--- a/swarms/models/kosmos2.py
+++ /dev/null
@@ -1,131 +0,0 @@
-from typing import List, Tuple
-
-from PIL import Image
-from pydantic import BaseModel, model_validator, validator
-from transformers import AutoModelForVision2Seq, AutoProcessor
-
-
-# Assuming the Detections class represents the output of the model prediction
-class Detections(BaseModel):
- xyxy: List[Tuple[float, float, float, float]]
- class_id: List[int]
- confidence: List[float]
-
- @model_validator
- def check_length(cls, values):
- assert (
- len(values.get("xyxy"))
- == len(values.get("class_id"))
- == len(values.get("confidence"))
- ), "All fields must have the same length."
- return values
-
- @validator(
- "xyxy", "class_id", "confidence", pre=True, each_item=True
- )
- def check_not_empty(cls, v):
- if isinstance(v, list) and len(v) == 0:
- raise ValueError("List must not be empty")
- return v
-
- @classmethod
- def empty(cls):
- return cls(xyxy=[], class_id=[], confidence=[])
-
-
-class Kosmos2(BaseModel):
- """
- Kosmos2
-
- Args:
- ------
- model: AutoModelForVision2Seq
- processor: AutoProcessor
-
- Usage:
- ------
- >>> from swarms import Kosmos2
- >>> from swarms.models.kosmos2 import Detections
- >>> from PIL import Image
- >>> model = Kosmos2.initialize()
- >>> image = Image.open("path_to_image.jpg")
- >>> detections = model(image)
- >>> print(detections)
-
- """
-
- model: AutoModelForVision2Seq
- processor: AutoProcessor
-
- @classmethod
- def initialize(cls):
- model = AutoModelForVision2Seq.from_pretrained(
- "ydshieh/kosmos-2-patch14-224", trust_remote_code=True
- )
- processor = AutoProcessor.from_pretrained(
- "ydshieh/kosmos-2-patch14-224", trust_remote_code=True
- )
- return cls(model=model, processor=processor)
-
- def __call__(self, img: str) -> Detections:
- image = Image.open(img)
- prompt = "An image of"
-
- inputs = self.processor(
- text=prompt, images=image, return_tensors="pt"
- )
- outputs = self.model.generate(
- **inputs, use_cache=True, max_new_tokens=64
- )
-
- generated_text = self.processor.batch_decode(
- outputs, skip_special_tokens=True
- )[0]
-
- # The actual processing of generated_text to entities would go here
- # For the purpose of this example, assume a mock function 'extract_entities' exists:
- entities = self.extract_entities(generated_text)
-
- # Convert entities to detections format
- detections = self.process_entities_to_detections(
- entities, image
- )
- return detections
-
- def extract_entities(
- self, text: str
- ) -> List[Tuple[str, Tuple[float, float, float, float]]]:
- # Placeholder function for entity extraction
- # This should be replaced with the actual method of extracting entities
- return []
-
- def process_entities_to_detections(
- self,
- entities: List[Tuple[str, Tuple[float, float, float, float]]],
- image: Image.Image,
- ) -> Detections:
- if not entities:
- return Detections.empty()
-
- class_ids = [0] * len(
- entities
- ) # Replace with actual class ID extraction logic
- xyxys = [
- (
- e[1][0] * image.width,
- e[1][1] * image.height,
- e[1][2] * image.width,
- e[1][3] * image.height,
- )
- for e in entities
- ]
- confidences = [1.0] * len(entities) # Placeholder confidence
-
- return Detections(
- xyxy=xyxys, class_id=class_ids, confidence=confidences
- )
-
-
-# Usage:
-# kosmos2 = Kosmos2.initialize()
-# detections = kosmos2(img="path_to_image.jpg")
diff --git a/swarms/models/kosmos_two.py b/swarms/models/kosmos_two.py
index 3b1d4233..6bc4d810 100644
--- a/swarms/models/kosmos_two.py
+++ b/swarms/models/kosmos_two.py
@@ -8,6 +8,8 @@ import torchvision.transforms as T
from PIL import Image
from transformers import AutoModelForVision2Seq, AutoProcessor
+from swarms.models.base_multimodal_model import BaseMultiModalModel
+
# utils
def is_overlapping(rect1, rect2):
@@ -16,7 +18,7 @@ def is_overlapping(rect1, rect2):
return not (x2 < x3 or x1 > x4 or y2 < y3 or y1 > y4)
-class Kosmos:
+class Kosmos(BaseMultiModalModel):
"""
Kosmos model by Yen-Chun Shieh
@@ -35,9 +37,14 @@ class Kosmos:
def __init__(
self,
model_name="ydshieh/kosmos-2-patch14-224",
+ max_new_tokens: int = 64,
*args,
**kwargs,
):
+ super(Kosmos, self).__init__(*args, **kwargs)
+
+ self.max_new_tokens = max_new_tokens
+
self.model = AutoModelForVision2Seq.from_pretrained(
model_name, trust_remote_code=True, *args, **kwargs
)
@@ -45,81 +52,75 @@ class Kosmos:
model_name, trust_remote_code=True, *args, **kwargs
)
- def get_image(self, url):
- """Image"""
+ def get_image(self, url: str):
+ """Get image from url
+
+ Args:
+ url (str): url of image
+
+ Returns:
+ _type_: _description_
+ """
return Image.open(requests.get(url, stream=True).raw)
- def run(self, prompt, image):
- """Run Kosmos"""
- inputs = self.processor(
- text=prompt, images=image, return_tensors="pt"
- )
- generated_ids = self.model.generate(
- pixel_values=inputs["pixel_values"],
- input_ids=inputs["input_ids"][:, :-1],
- attention_mask=inputs["attention_mask"][:, :-1],
- img_features=None,
- img_attn_mask=inputs["img_attn_mask"][:, :-1],
- use_cache=True,
- max_new_tokens=64,
- )
- generated_texts = self.processor.batch_decode(
- generated_ids,
- skip_special_tokens=True,
- )[0]
- processed_text, entities = (
- self.processor.post_process_generation(generated_texts)
- )
+ def run(self, task: str, image: str, *args, **kwargs):
+ """Run the model
- def __call__(self, prompt, image):
- """Run call"""
+ Args:
+ task (str): task to run
+ image (str): img url
+ """
inputs = self.processor(
- text=prompt, images=image, return_tensors="pt"
+ text=task, images=image, return_tensors="pt"
)
generated_ids = self.model.generate(
pixel_values=inputs["pixel_values"],
input_ids=inputs["input_ids"][:, :-1],
attention_mask=inputs["attention_mask"][:, :-1],
- img_features=None,
+ image_embeds=None,
img_attn_mask=inputs["img_attn_mask"][:, :-1],
use_cache=True,
- max_new_tokens=64,
+ max_new_tokens=self.max_new_tokens,
)
+
generated_texts = self.processor.batch_decode(
generated_ids,
skip_special_tokens=True,
)[0]
+
processed_text, entities = (
self.processor.post_process_generation(generated_texts)
)
+ return processed_text, entities
+
# tasks
def multimodal_grounding(self, phrase, image_url):
- prompt = f" {phrase} "
- self.run(prompt, image_url)
+ task = f" {phrase} "
+ self.run(task, image_url)
def referring_expression_comprehension(self, phrase, image_url):
- prompt = f" {phrase} "
- self.run(prompt, image_url)
+ task = f" {phrase} "
+ self.run(task, image_url)
def referring_expression_generation(self, phrase, image_url):
- prompt = (
+ task = (
""
" It is"
)
- self.run(prompt, image_url)
+ self.run(task, image_url)
def grounded_vqa(self, question, image_url):
- prompt = f" Question: {question} Answer:"
- self.run(prompt, image_url)
+ task = f" Question: {question} Answer:"
+ self.run(task, image_url)
def grounded_image_captioning(self, image_url):
- prompt = " An image of"
- self.run(prompt, image_url)
+ task = " An image of"
+ self.run(task, image_url)
def grounded_image_captioning_detailed(self, image_url):
- prompt = " Describe this image in detail"
- self.run(prompt, image_url)
+ task = " Describe this image in detail"
+ self.run(task, image_url)
def draw_entity_boxes_on_image(
image, entities, show=False, save_path=None
@@ -320,7 +321,7 @@ class Kosmos:
return new_image
- def generate_boxees(self, prompt, image_url):
+ def generate_boxees(self, task, image_url):
image = self.get_image(image_url)
- processed_text, entities = self.process_prompt(prompt, image)
+ processed_text, entities = self.process_task(task, image)
self.draw_entity_boxes_on_image(image, entities, show=True)
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
new file mode 100644
index 00000000..8d096ba5
--- /dev/null
+++ b/swarms/models/medical_sam.py
@@ -0,0 +1,144 @@
+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
+
+
+def sam_model_registry():
+ pass
+
+
+@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/mistral.py b/swarms/models/mistral.py
index 297ecf12..6cfb6f77 100644
--- a/swarms/models/mistral.py
+++ b/swarms/models/mistral.py
@@ -1,10 +1,11 @@
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
-from swarms.agents.message import Message
+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 +39,10 @@ class Mistral:
temperature: float = 1.0,
max_length: int = 100,
do_sample: bool = True,
+ *args,
+ **kwargs,
):
+ super().__init__(*args, **kwargs)
self.ai_name = ai_name
self.system_prompt = system_prompt
self.model_name = model_name
@@ -46,6 +50,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 +59,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
+ )
- def run(self, task: str):
- """Run the model on a given task."""
+ self.model.to(self.device)
- 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 +83,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
@@ -159,16 +134,3 @@ class Mistral:
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
diff --git a/swarms/models/mixtral.py b/swarms/models/mixtral.py
new file mode 100644
index 00000000..6f3a9c7d
--- /dev/null
+++ b/swarms/models/mixtral.py
@@ -0,0 +1,73 @@
+from typing import Optional
+from transformers import AutoModelForCausalLM, AutoTokenizer
+from swarms.models.base_llm import AbstractLLM
+
+
+class Mixtral(AbstractLLM):
+ """Mixtral model.
+
+ Args:
+ model_name (str): The name or path of the pre-trained Mixtral model.
+ max_new_tokens (int): The maximum number of new tokens to generate.
+ *args: Variable length argument list.
+
+
+ Examples:
+ >>> from swarms.models import Mixtral
+ >>> mixtral = Mixtral()
+ >>> mixtral.run("Test task")
+ 'Generated text'
+ """
+
+ def __init__(
+ self,
+ model_name: str = "mistralai/Mixtral-8x7B-v0.1",
+ max_new_tokens: int = 500,
+ *args,
+ **kwargs,
+ ):
+ """
+ Initializes a Mixtral model.
+
+ Args:
+ model_name (str): The name or path of the pre-trained Mixtral model.
+ max_new_tokens (int): The maximum number of new tokens to generate.
+ *args: Variable length argument list.
+ **kwargs: Arbitrary keyword arguments.
+ """
+ super().__init__(*args, **kwargs)
+ self.model_name = model_name
+ self.max_new_tokens = max_new_tokens
+ self.tokenizer = AutoTokenizer.from_pretrained(model_name)
+ self.model = AutoModelForCausalLM.from_pretrained(
+ model_name, *args, **kwargs
+ )
+
+ def run(self, task: Optional[str] = None, **kwargs):
+ """
+ Generates text based on the given task.
+
+ Args:
+ task (str, optional): The task or prompt for text generation.
+
+ Returns:
+ str: The generated text.
+ """
+ try:
+ inputs = self.tokenizer(task, return_tensors="pt")
+
+ outputs = self.model.generate(
+ **inputs,
+ max_new_tokens=self.max_new_tokens,
+ **kwargs,
+ )
+
+ out = self.tokenizer.decode(
+ outputs[0],
+ skip_special_tokens=True,
+ )
+
+ return out
+ except Exception as error:
+ print(f"There is an error: {error} in Mixtral model.")
+ raise error
diff --git a/swarms/models/model_registry.py b/swarms/models/model_registry.py
new file mode 100644
index 00000000..6da04282
--- /dev/null
+++ b/swarms/models/model_registry.py
@@ -0,0 +1,82 @@
+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
+ }
+
+ 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/modelscope_llm.py b/swarms/models/modelscope_llm.py
new file mode 100644
index 00000000..03cd978d
--- /dev/null
+++ b/swarms/models/modelscope_llm.py
@@ -0,0 +1,83 @@
+from typing import Optional
+
+from modelscope import AutoModelForCausalLM, AutoTokenizer
+
+from swarms.models.base_llm import AbstractLLM
+
+
+class ModelScopeAutoModel(AbstractLLM):
+ """
+ ModelScopeAutoModel is a class that represents a model for generating text using the ModelScope framework.
+
+ Args:
+ model_name (str): The name or path of the pre-trained model.
+ tokenizer_name (str, optional): The name or path of the tokenizer to use. Defaults to None.
+ device (str, optional): The device to use for model inference. Defaults to "cuda".
+ device_map (str, optional): The device mapping for multi-GPU setups. Defaults to "auto".
+ max_new_tokens (int, optional): The maximum number of new tokens to generate. Defaults to 500.
+ skip_special_tokens (bool, optional): Whether to skip special tokens during decoding. Defaults to True.
+ *args: Additional positional arguments.
+ **kwargs: Additional keyword arguments.
+
+ Attributes:
+ tokenizer (AutoTokenizer): The tokenizer used for tokenizing input text.
+ model (AutoModelForCausalLM): The pre-trained model for generating text.
+
+ Methods:
+ run(task, *args, **kwargs): Generates text based on the given task.
+
+ Examples:
+ >>> from swarms.models import ModelScopeAutoModel
+ >>> mp = ModelScopeAutoModel(
+ ... model_name="gpt2",
+ ... )
+ >>> mp.run("Generate a 10,000 word blog on health and wellness.")
+ """
+
+ def __init__(
+ self,
+ model_name: str,
+ tokenizer_name: Optional[str] = None,
+ device: str = "cuda",
+ device_map: str = "auto",
+ max_new_tokens: int = 500,
+ skip_special_tokens: bool = True,
+ *args,
+ **kwargs,
+ ):
+ super().__init__(*args, **kwargs)
+ self.model_name = model_name
+ self.tokenizer_name = tokenizer_name
+ self.device = device
+ self.device_map = device_map
+ self.max_new_tokens = max_new_tokens
+ self.skip_special_tokens = skip_special_tokens
+
+ self.tokenizer = AutoTokenizer.from_pretrained(
+ self.tokenizer_name
+ )
+ self.model = AutoModelForCausalLM.from_pretrained(
+ self.model_name, device_map=device_map * args, **kwargs
+ )
+
+ def run(self, task: str, *args, **kwargs):
+ """
+ Run the model on the given task.
+
+ Parameters:
+ task (str): The input task to be processed.
+ *args: Additional positional arguments.
+ **kwargs: Additional keyword arguments.
+
+ Returns:
+ str: The generated output from the model.
+ """
+ text = self.tokenizer(task, return_tensors="pt")
+
+ outputs = self.model.generate(
+ **text, max_new_tokens=self.max_new_tokens, **kwargs
+ )
+
+ return self.tokenizer.decode(
+ outputs[0], skip_special_tokens=self.skip_special_tokens
+ )
diff --git a/swarms/models/modelscope_pipeline.py b/swarms/models/modelscope_pipeline.py
new file mode 100644
index 00000000..ed75b33b
--- /dev/null
+++ b/swarms/models/modelscope_pipeline.py
@@ -0,0 +1,58 @@
+from modelscope.pipelines import pipeline
+
+from swarms.models.base_llm import AbstractLLM
+
+
+class ModelScopePipeline(AbstractLLM):
+ """
+ A class representing a ModelScope pipeline.
+
+ Args:
+ type_task (str): The type of task for the pipeline.
+ model_name (str): The name of the model for the pipeline.
+ *args: Variable length argument list.
+ **kwargs: Arbitrary keyword arguments.
+
+ Attributes:
+ type_task (str): The type of task for the pipeline.
+ model_name (str): The name of the model for the pipeline.
+ model: The pipeline model.
+
+ Methods:
+ run: Runs the pipeline for a given task.
+
+ Examples:
+ >>> from swarms.models import ModelScopePipeline
+ >>> mp = ModelScopePipeline(
+ ... type_task="text-generation",
+ ... model_name="gpt2",
+ ... )
+ >>> mp.run("Generate a 10,000 word blog on health and wellness.")
+
+ """
+
+ def __init__(
+ self, type_task: str, model_name: str, *args, **kwargs
+ ):
+ super().__init__(*args, **kwargs)
+ self.type_task = type_task
+ self.model_name = model_name
+
+ self.model = pipeline(
+ self.type_task, model=self.model_name, *args, **kwargs
+ )
+
+ def run(self, task: str, *args, **kwargs):
+ """
+ Runs the pipeline for a given task.
+
+ Args:
+ task (str): The task to be performed by the pipeline.
+ *args: Variable length argument list.
+ **kwargs: Arbitrary keyword arguments.
+
+ Returns:
+ The result of running the pipeline on the given task.
+
+ """
+ return self.model(task, *args, **kwargs)
diff --git a/swarms/models/multion.py b/swarms/models/multion.py
deleted file mode 100644
index 14152faf..00000000
--- a/swarms/models/multion.py
+++ /dev/null
@@ -1,60 +0,0 @@
-from swarms.models.base_llm import AbstractLLM
-
-
-try:
- import multion
-
-except ImportError:
- raise ImportError(
- "Cannot import multion, please install 'pip install'"
- )
-
-
-class MultiOn(AbstractLLM):
- """
- MultiOn is a wrapper for the Multion API.
-
- Args:
- **kwargs:
-
- Methods:
- run(self, task: str, url: str, *args, **kwargs)
-
- Example:
- >>> from swarms.models.multion import MultiOn
- >>> multion = MultiOn()
- >>> multion.run("Order chicken tendies", "https://www.google.com/")
- "Order chicken tendies. https://www.google.com/"
-
- """
-
- def __init__(self, **kwargs):
- super(MultiOn, self).__init__(**kwargs)
-
- def run(self, task: str, url: str, *args, **kwargs) -> str:
- """Run the multion model
-
- Args:
- task (str): _description_
- url (str): _description_
-
- Returns:
- str: _description_
- """
- response = multion.new_session({"input": task, "url": url})
- return response
-
- def generate_summary(
- self, task: str, url: str, *args, **kwargs
- ) -> str:
- """Generate a summary from the multion model
-
- Args:
- task (str): _description_
- url (str): _description_
-
- Returns:
- str: _description_
- """
- response = multion.new_session({"input": task, "url": url})
- return response
diff --git a/swarms/models/odin.py b/swarms/models/odin.py
new file mode 100644
index 00000000..6a842af4
--- /dev/null
+++ b/swarms/models/odin.py
@@ -0,0 +1,98 @@
+import os
+import supervision as sv
+from ultralytics_example 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.
+
+ Attributes:
+ 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 = "yolo.weights",
+ confidence_threshold: float = 0.3,
+ iou_threshold: float = 0.7,
+ ):
+ super(Odin, self).__init__()
+ 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,
+ )
+
+ def run(self, video: str, *args, **kwargs):
+ """
+ Runs the object detection and tracking algorithm on the specified video.
+
+ Args:
+ video (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
+ )
+ video_info = sv.VideoInfo.from_video(video=video)
+
+ with sv.VideoSink(
+ target_path=self.target_video, 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/open_dalle.py b/swarms/models/open_dalle.py
new file mode 100644
index 00000000..b43d6c2e
--- /dev/null
+++ b/swarms/models/open_dalle.py
@@ -0,0 +1,66 @@
+from typing import Optional, Any
+
+import torch
+from diffusers import AutoPipelineForText2Image
+from swarms.models.base_multimodal_model import BaseMultiModalModel
+
+
+class OpenDalle(BaseMultiModalModel):
+ """OpenDalle model class
+
+ Attributes:
+ model_name (str): The name or path of the model to be used. Defaults to "dataautogpt3/OpenDalleV1.1".
+ torch_dtype (torch.dtype): The torch data type to be used. Defaults to torch.float16.
+ device (str): The device to be used for computation. Defaults to "cuda".
+
+ Examples:
+ >>> from swarms.models.open_dalle import OpenDalle
+ >>> od = OpenDalle()
+ >>> od.run("A picture of a cat")
+
+ """
+
+ def __init__(
+ self,
+ model_name: str = "dataautogpt3/OpenDalleV1.1",
+ torch_dtype: Any = torch.float16,
+ device: str = "cuda",
+ *args,
+ **kwargs,
+ ):
+ """
+ Initializes the OpenDalle model.
+
+ Args:
+ model_name (str, optional): The name or path of the model to be used. Defaults to "dataautogpt3/OpenDalleV1.1".
+ torch_dtype (torch.dtype, optional): The torch data type to be used. Defaults to torch.float16.
+ device (str, optional): The device to be used for computation. Defaults to "cuda".
+ *args: Variable length argument list.
+ **kwargs: Arbitrary keyword arguments.
+ """
+ self.pipeline = AutoPipelineForText2Image.from_pretrained(
+ model_name, torch_dtype=torch_dtype, *args, **kwargs
+ ).to(device)
+
+ def run(self, task: Optional[str] = None, *args, **kwargs):
+ """Run the OpenDalle model
+
+ Args:
+ task (str, optional): The task to be performed. Defaults to None.
+ *args: Variable length argument list.
+ **kwargs: Arbitrary keyword arguments.
+
+ Returns:
+ [type]: [description]
+ """
+ try:
+ if task is None:
+ raise ValueError("Task cannot be None")
+ if not isinstance(task, str):
+ raise TypeError("Task must be a string")
+ if len(task) < 1:
+ raise ValueError("Task cannot be empty")
+ return self.pipeline(task, *args, **kwargs).images[0]
+ except Exception as error:
+ print(f"[ERROR][OpenDalle] {error}")
+ raise error
diff --git a/swarms/models/openai_assistant.py b/swarms/models/openai_assistant.py
deleted file mode 100644
index 6d0c518f..00000000
--- a/swarms/models/openai_assistant.py
+++ /dev/null
@@ -1,74 +0,0 @@
-from typing import Dict, List, Optional
-from dataclass import dataclass
-
-from swarms.models import OpenAI
-
-
-@dataclass
-class OpenAIAssistant:
- name: str = "OpenAI Assistant"
- instructions: str = None
- tools: List[Dict] = None
- model: str = None
- openai_api_key: str = None
- temperature: float = 0.5
- max_tokens: int = 100
- stop: List[str] = None
- echo: bool = False
- stream: bool = False
- log: bool = False
- presence: bool = False
- dashboard: bool = False
- debug: bool = False
- max_loops: int = 5
- stopping_condition: Optional[str] = None
- loop_interval: int = 1
- retry_attempts: int = 3
- retry_interval: int = 1
- interactive: bool = False
- dynamic_temperature: bool = False
- state: Dict = None
- response_filters: List = None
- response_filter: Dict = None
- response_filter_name: str = None
- response_filter_value: str = None
- response_filter_type: str = None
- response_filter_action: str = None
- response_filter_action_value: str = None
- response_filter_action_type: str = None
- response_filter_action_name: str = None
- client = OpenAI()
- role: str = "user"
- instructions: str = None
-
- def create_assistant(self, task: str):
- assistant = self.client.create_assistant(
- name=self.name,
- instructions=self.instructions,
- tools=self.tools,
- model=self.model,
- )
- return assistant
-
- def create_thread(self):
- thread = self.client.beta.threads.create()
- return thread
-
- def add_message_to_thread(self, thread_id: str, message: str):
- message = self.client.beta.threads.add_message(
- thread_id=thread_id, role=self.user, content=message
- )
- return message
-
- def run(self, task: str):
- run = self.client.beta.threads.runs.create(
- thread_id=self.create_thread().id,
- assistant_id=self.create_assistant().id,
- instructions=self.instructions,
- )
-
- out = self.client.beta.threads.runs.retrieve(
- thread_id=run.thread_id, run_id=run.id
- )
-
- return out
diff --git a/swarms/models/openai_function_caller.py b/swarms/models/openai_function_caller.py
index 6542e457..e6822793 100644
--- a/swarms/models/openai_function_caller.py
+++ b/swarms/models/openai_function_caller.py
@@ -234,10 +234,10 @@ class OpenAIFunctionCaller:
)
)
- def call(self, prompt: str) -> Dict:
- response = openai.Completion.create(
+ def call(self, task: str, *args, **kwargs) -> Dict:
+ return openai.Completion.create(
engine=self.model,
- prompt=prompt,
+ prompt=task,
max_tokens=self.max_tokens,
temperature=self.temperature,
top_p=self.top_p,
@@ -253,9 +253,10 @@ class OpenAIFunctionCaller:
user=self.user,
messages=self.messages,
timeout_sec=self.timeout_sec,
+ *args,
+ **kwargs,
)
- return response
- def run(self, prompt: str) -> str:
- response = self.call(prompt)
+ def run(self, task: str, *args, **kwargs) -> str:
+ response = self.call(task, *args, **kwargs)
return response["choices"][0]["text"].strip()
diff --git a/swarms/models/openai_models.py b/swarms/models/openai_models.py
index 14332ff2..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
@@ -189,7 +258,9 @@ class BaseOpenAI(BaseLLM):
return True
client: Any = None #: :meta private:
- model_name: str = Field(default="text-davinci-003", alias="model")
+ model_name: str = Field(
+ default="gpt-4-1106-preview", alias="model"
+ )
"""Model name to use."""
temperature: float = 0.7
"""What sampling temperature to use."""
@@ -831,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/models/qwen.py b/swarms/models/qwen.py
new file mode 100644
index 00000000..b5a4ed1a
--- /dev/null
+++ b/swarms/models/qwen.py
@@ -0,0 +1,144 @@
+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.
+
+
+ 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"
+ device: str = "cuda"
+ args: tuple = field(default_factory=tuple)
+ kwargs: dict = field(default_factory=dict)
+ quantize: bool = False
+ return_bounding_boxes: 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:
+ 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}")
+
+ 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/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.py b/swarms/models/sam.py
index 866c79ee..110d80b7 100644
--- a/swarms/models/sam.py
+++ b/swarms/models/sam.py
@@ -1,315 +1,107 @@
-import cv2
-import numpy as np
+import torch
from PIL import Image
-from transformers import (
- SamImageProcessor,
- SamModel,
- SamProcessor,
- pipeline,
-)
+import requests
+from transformers import SamModel, SamProcessor
+from typing import List
-try:
- import cv2
- import supervision as sv
-except ImportError:
- print("Please install supervision and cv")
+device = "cuda" if torch.cuda.is_available() else "cpu"
-from enum import Enum
-
-
-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:
+class SAM:
"""
- Performs Non-Max Suppression on a set of masks by prioritizing larger masks and
- removing smaller masks that overlap significantly.
+ Class representing the SAM (Segmentation and Masking) model.
- 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.
+ Args:
+ model_name (str): The name of the pre-trained SAM model. Default is "facebook/sam-vit-huge".
+ device (torch.device): The device to run the model on. Default is the current device.
+ input_points (List[List[int]]): The 2D location of a window in the image to segment. Default is [[450, 600]].
+ *args: Additional positional arguments.
+ **kwargs: Additional keyword arguments.
- 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.
+ Attributes:
+ model_name (str): The name of the pre-trained SAM model.
+ device (torch.device): The device to run the model on.
+ input_points (List[List[int]]): The 2D location of a window in the image to segment.
+ model (SamModel): The pre-trained SAM model.
+ processor (SamProcessor): The processor for the SAM model.
- 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
- keep_mask[sorted_idx] = np.logical_and(
- keep_mask[sorted_idx], ~overlapping_masks
- )
-
- 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.
+ Methods:
+ run(task=None, img=None, *args, **kwargs): Runs the SAM model on the given image and returns the segmentation scores and masks.
+ process_img(img: str = None, *args, **kwargs): Processes the input image and returns the processed image.
- 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
+ def __init__(
+ self,
+ model_name: str = "facebook/sam-vit-huge",
+ device=device,
+ input_points: List[List[int]] = [[450, 600]],
+ *args,
+ **kwargs,
+ ):
+ self.model_name = model_name
+ self.device = device
+ self.input_points = input_points
- 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
- )
+ self.model = SamModel.from_pretrained(
+ model_name, *args, **kwargs
+ ).to(device)
- 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)
+ self.processor = SamProcessor.from_pretrained(model_name)
+ def run(self, task=None, img=None, *args, **kwargs):
+ """
+ Runs the SAM model on the given image and returns the segmentation scores and masks.
-def masks_to_marks(masks: np.ndarray) -> sv.Detections:
- """
- Converts a set of masks to a marks (sv.Detections) object.
+ Args:
+ task: The task to perform. Not used in this method.
+ img: The input image to segment.
+ *args: Additional positional arguments.
+ **kwargs: Additional keyword arguments.
- 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:
+ Tuple: A tuple containing the segmentation scores and masks.
- Returns:
- sv.Detections: An object containing the masks and their bounding box
- coordinates.
- """
- return sv.Detections(
- mask=masks, xyxy=sv.mask_to_xyxy(masks=masks)
- )
+ """
+ img = self.process_img(img)
+ # Specify the points of the mask to segment
+ input_points = [
+ self.input_points
+ ] # 2D location of a window in the image
-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.
+ # Preprocess the image
+ inputs = self.processor(
+ img, input_points=input_points, return_tensors="pt"
+ ).to(device)
- 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.
+ with torch.no_grad():
+ outputs = self.model(**inputs) # noqa: E999
- 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,
+ masks = self.processor.image_processor.post_process_masks(
+ outputs.pred_masks.cpu(),
+ inputs["original_sizes"].cpu(),
+ inputs["reshaped_input_sizes"].cpu(),
)
- 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)
- )
-
-
-class SegmentAnythingMarkGenerator:
- """
- A class for performing image segmentation using a specified model.
+ scores = outputs.iou_scores
- 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",
- ):
- self.model = SamModel.from_pretrained(model_name).to(device)
- self.processor = SamProcessor.from_pretrained(model_name)
- self.image_processor = SamImageProcessor.from_pretrained(
- model_name
- )
- self.pipeline = pipeline(
- task="mask-generation",
- model=self.model,
- image_processor=self.image_processor,
- device=device,
- )
+ return scores, masks
- def run(self, image: np.ndarray) -> sv.Detections:
+ def process_img(self, img: str = None, *args, **kwargs):
"""
- Generate image segmentation marks.
+ Processes the input image and returns the processed image.
- Parameters:
- image (np.ndarray): The image to be marked in BGR format.
+ Args:
+ img (str): The URL or file path of the input image.
+ *args: Additional positional arguments.
+ **kwargs: Additional keyword arguments.
Returns:
- sv.Detections: An object containing the segmentation masks and their
- corresponding bounding box coordinates.
+ Image: The processed image.
+
"""
- image = Image.fromarray(
- cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
- )
- outputs = self.pipeline(image, points_per_batch=64)
- masks = np.array(outputs["masks"])
- return masks_to_marks(masks=masks)
+ raw_image = Image.open(
+ requests.get(img, stream=True, *args, **kwargs).raw
+ ).convert("RGB")
+
+ return raw_image
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/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/models/simple_ada.py b/swarms/models/simple_ada.py
deleted file mode 100644
index e9a599d0..00000000
--- a/swarms/models/simple_ada.py
+++ /dev/null
@@ -1,23 +0,0 @@
-import os
-from openai import OpenAI
-
-client = OpenAI()
-
-
-def get_ada_embeddings(
- text: str, model: str = "text-embedding-ada-002"
-):
- """
- Simple function to get embeddings from ada
-
- Usage:
- >>> get_ada_embeddings("Hello World")
- >>> get_ada_embeddings("Hello World", model="text-embedding-ada-001")
-
- """
-
- text = text.replace("\n", " ")
-
- return client.embeddings.create(input=[text], model=model)[
- "data"
- ][0]["embedding"]
diff --git a/swarms/models/stable_diffusion.py b/swarms/models/stable_diffusion.py
index 7b363d02..a0068531 100644
--- a/swarms/models/stable_diffusion.py
+++ b/swarms/models/stable_diffusion.py
@@ -140,6 +140,18 @@ class StableDiffusion:
return image_paths
def generate_and_move_image(self, prompt, iteration, folder_path):
+ """
+ Generates an image based on the given prompt and moves it to the specified folder.
+
+ Args:
+ prompt (str): The prompt used to generate the image.
+ iteration (int): The iteration number.
+ folder_path (str): The path to the folder where the image will be moved.
+
+ Returns:
+ str: The path of the moved image.
+
+ """
# Generate the image
image_paths = self.run(prompt)
if not image_paths:
diff --git a/swarms/models/timm.py b/swarms/models/timm.py
index d1c42165..de0484f2 100644
--- a/swarms/models/timm.py
+++ b/swarms/models/timm.py
@@ -2,59 +2,48 @@ 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,
+ *args,
+ **kwargs,
+ ):
+ super().__init__(*args, **kwargs)
+ 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 +54,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_name, *args, **kwargs)
+ return model(task)
+
+ def list_models(self):
+ return timm.list_models()
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/swarms/models/types.py b/swarms/models/types.py
new file mode 100644
index 00000000..460d0ef7
--- /dev/null
+++ b/swarms/models/types.py
@@ -0,0 +1,28 @@
+from pydantic import BaseModel
+from typing import List, Optional
+
+
+class TextModality(BaseModel):
+ content: str
+
+
+class ImageModality(BaseModel):
+ url: str
+ alt_text: Optional[str]
+
+
+class AudioModality(BaseModel):
+ url: str
+ transcript: Optional[str]
+
+
+class VideoModality(BaseModel):
+ url: str
+ transcript: Optional[str]
+
+
+class MultimodalData(BaseModel):
+ text: Optional[List[TextModality]]
+ images: Optional[List[ImageModality]]
+ audio: Optional[List[AudioModality]]
+ video: Optional[List[VideoModality]]
diff --git a/swarms/models/ultralytics_model.py b/swarms/models/ultralytics_model.py
new file mode 100644
index 00000000..edb9984c
--- /dev/null
+++ b/swarms/models/ultralytics_model.py
@@ -0,0 +1,52 @@
+from swarms.models.base_multimodal_model import BaseMultiModalModel
+from ultralytics import YOLO
+from typing import List
+
+
+class UltralyticsModel(BaseMultiModalModel):
+ """
+ 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.
+ """
+
+ def __init__(
+ self, model_name: str = "yolov8n.pt", *args, **kwargs
+ ):
+ super().__init__(*args, **kwargs)
+ self.model_name = model_name
+
+ 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, tasks: List[str] = None, *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.
+ """
+ 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/models/vip_llava.py b/swarms/models/vip_llava.py
new file mode 100644
index 00000000..db532913
--- /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/models/vllm.py b/swarms/models/vllm.py
index 0ea4be4a..0caeb3c8 100644
--- a/swarms/models/vllm.py
+++ b/swarms/models/vllm.py
@@ -1,12 +1,17 @@
+import torch
from swarms.models.base_llm import AbstractLLM
-try:
- from vllm import LLM, SamplingParams
-except ImportError as error:
- print(f"[ERROR] [vLLM] {error}")
- # subprocess.run(["pip", "install", "vllm"])
- # raise error
- raise error
+if torch.cuda.is_available() or torch.cuda.device_count() > 0:
+ # Download vllm with pip
+ try:
+ from vllm import LLM, SamplingParams
+ except ImportError as error:
+ print(f"[ERROR] [vLLM] {error}")
+ raise error
+else:
+ from swarms.models.huggingface import HuggingfaceLLM as LLM
+
+ SamplingParams = None
class vLLM(AbstractLLM):
@@ -83,8 +88,9 @@ class vLLM(AbstractLLM):
_type_: _description_
"""
try:
- outputs = self.llm.generate(task, self.sampling_params)
- return outputs
+ return self.llm.generate(
+ task, self.sampling_params, *args, **kwargs
+ )
except Exception as error:
print(f"[ERROR] [vLLM] [run] {error}")
raise error
diff --git a/swarms/models/whisperx_model.py b/swarms/models/whisperx_model.py
deleted file mode 100644
index e3b76fae..00000000
--- a/swarms/models/whisperx_model.py
+++ /dev/null
@@ -1,138 +0,0 @@
-import os
-import subprocess
-
-try:
- import whisperx
- from pydub import AudioSegment
- from pytube import YouTube
-except Exception as error:
- print("Error importing pytube. Please install pytube manually.")
- print("pip install pytube")
- print("pip install pydub")
- print("pip install whisperx")
- print(f"Pytube error: {error}")
-
-
-class WhisperX:
- def __init__(
- self,
- video_url,
- audio_format="mp3",
- device="cuda",
- batch_size=16,
- compute_type="float16",
- hf_api_key=None,
- ):
- """
- # Example usage
- video_url = "url"
- speech_to_text = WhisperX(video_url)
- transcription = speech_to_text.transcribe_youtube_video()
- print(transcription)
-
- """
- self.video_url = video_url
- self.audio_format = audio_format
- self.device = device
- self.batch_size = batch_size
- self.compute_type = compute_type
- self.hf_api_key = hf_api_key
-
- def install(self):
- subprocess.run(["pip", "install", "whisperx"])
- subprocess.run(["pip", "install", "pytube"])
- subprocess.run(["pip", "install", "pydub"])
-
- def download_youtube_video(self):
- audio_file = f"video.{self.audio_format}"
-
- # Download video π₯
- yt = YouTube(self.video_url)
- yt_stream = yt.streams.filter(only_audio=True).first()
- yt_stream.download(filename="video.mp4")
-
- # Convert video to audio π§
- video = AudioSegment.from_file("video.mp4", format="mp4")
- video.export(audio_file, format=self.audio_format)
- os.remove("video.mp4")
-
- return audio_file
-
- def transcribe_youtube_video(self):
- audio_file = self.download_youtube_video()
-
- device = "cuda"
- batch_size = 16
- compute_type = "float16"
-
- # 1. Transcribe with original Whisper (batched) π£οΈ
- model = whisperx.load_model(
- "large-v2", device, compute_type=compute_type
- )
- audio = whisperx.load_audio(audio_file)
- result = model.transcribe(audio, batch_size=batch_size)
-
- # 2. Align Whisper output π
- model_a, metadata = whisperx.load_align_model(
- language_code=result["language"], device=device
- )
- result = whisperx.align(
- result["segments"],
- model_a,
- metadata,
- audio,
- device,
- return_char_alignments=False,
- )
-
- # 3. Assign speaker labels π·οΈ
- diarize_model = whisperx.DiarizationPipeline(
- use_auth_token=self.hf_api_key, device=device
- )
- diarize_model(audio_file)
-
- try:
- segments = result["segments"]
- transcription = " ".join(
- segment["text"] for segment in segments
- )
- return transcription
- except KeyError:
- print("The key 'segments' is not found in the result.")
-
- def transcribe(self, audio_file):
- model = whisperx.load_model(
- "large-v2", self.device, self.compute_type
- )
- audio = whisperx.load_audio(audio_file)
- result = model.transcribe(audio, batch_size=self.batch_size)
-
- # 2. Align Whisper output π
- model_a, metadata = whisperx.load_align_model(
- language_code=result["language"], device=self.device
- )
-
- result = whisperx.align(
- result["segments"],
- model_a,
- metadata,
- audio,
- self.device,
- return_char_alignments=False,
- )
-
- # 3. Assign speaker labels π·οΈ
- diarize_model = whisperx.DiarizationPipeline(
- use_auth_token=self.hf_api_key, device=self.device
- )
-
- diarize_model(audio_file)
-
- try:
- segments = result["segments"]
- transcription = " ".join(
- segment["text"] for segment in segments
- )
- return transcription
- except KeyError:
- print("The key 'segments' is not found in the result.")
diff --git a/swarms/models/zeroscope.py b/swarms/models/zeroscope.py
new file mode 100644
index 00000000..43a11206
--- /dev/null
+++ b/swarms/models/zeroscope.py
@@ -0,0 +1,103 @@
+import torch
+from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
+from diffusers.utils import export_to_video
+
+
+class ZeroscopeTTV:
+ """
+ ZeroscopeTTV class represents a zero-shot video generation model.
+
+ Args:
+ model_name (str): The name of the pre-trained model to use.
+ torch_dtype (torch.dtype): The torch data type to use for computations.
+ chunk_size (int): The size of chunks for forward chunking.
+ dim (int): The dimension along which to split the input for forward chunking.
+ num_inference_steps (int): The number of inference steps to perform.
+ height (int): The height of the video frames.
+ width (int): The width of the video frames.
+ num_frames (int): The number of frames in the video.
+
+ Attributes:
+ model_name (str): The name of the pre-trained model.
+ torch_dtype (torch.dtype): The torch data type used for computations.
+ chunk_size (int): The size of chunks for forward chunking.
+ dim (int): The dimension along which the input is split for forward chunking.
+ num_inference_steps (int): The number of inference steps to perform.
+ height (int): The height of the video frames.
+ width (int): The width of the video frames.
+ num_frames (int): The number of frames in the video.
+ pipe (DiffusionPipeline): The diffusion pipeline for video generation.
+
+ Methods:
+ forward(task: str = None, *args, **kwargs) -> str:
+ Performs forward pass on the input task and returns the path of the generated video.
+
+ Examples:
+ >>> from swarms.models
+ >>> zeroscope = ZeroscopeTTV()
+ >>> task = "A person is walking on the street."
+ >>> video_path = zeroscope(task)
+
+ """
+
+ def __init__(
+ self,
+ model_name: str = "cerspense/zeroscope_v2_576w",
+ torch_dtype=torch.float16,
+ chunk_size: int = 1,
+ dim: int = 1,
+ num_inference_steps: int = 40,
+ height: int = 320,
+ width: int = 576,
+ num_frames: int = 36,
+ *args,
+ **kwargs,
+ ):
+ self.model_name = model_name
+ self.torch_dtype = torch_dtype
+ self.chunk_size = chunk_size
+ self.dim = dim
+ self.num_inference_steps = num_inference_steps
+ self.height = height
+ self.width = width
+ self.num_frames = num_frames
+
+ self.pipe = DiffusionPipeline.from_pretrained(
+ model_name,
+ torch_dtype=torch_dtype,
+ *args,
+ )
+ self.pipe.scheduler = DPMSolverMultistepScheduler(
+ self.pipe.scheduler.config,
+ )
+ self.pipe_enable_model_cpu_offload()
+ self.pipe.enable_vae_slicing()
+ self.pipe.unet.enable_forward_chunking(
+ chunk_size=chunk_size, dim=dim
+ )
+
+ def run(self, task: str = None, *args, **kwargs):
+ """
+ Performs a forward pass on the input task and returns the path of the generated video.
+
+ Args:
+ task (str): The input task for video generation.
+
+ Returns:
+ str: The path of the generated video.
+ """
+ try:
+ video_frames = self.pipe(
+ task,
+ num_inference_steps=self.num_inference_steps,
+ height=self.height,
+ width=self.width,
+ num_frames=self.num_frames,
+ *args,
+ **kwargs,
+ ).frames
+ video_path = export_to_video(video_frames)
+ return video_path
+ except Exception as error:
+ print(f"Error in [ZeroscopeTTV.forward]: {error}")
+ raise error
diff --git a/swarms/prompts/__init__.py b/swarms/prompts/__init__.py
index 6417dc85..93416a9b 100644
--- a/swarms/prompts/__init__.py
+++ b/swarms/prompts/__init__.py
@@ -6,7 +6,8 @@ from swarms.prompts.operations_agent_prompt import (
OPERATIONS_AGENT_PROMPT,
)
from swarms.prompts.product_agent_prompt import PRODUCT_AGENT_PROMPT
-
+from swarms.prompts.documentation import DOCUMENTATION_WRITER_SOP
+from swarms.prompts.schema_generator import SchemaGenerator
__all__ = [
"CODE_INTERPRETER",
@@ -15,4 +16,6 @@ __all__ = [
"LEGAL_AGENT_PROMPT",
"OPERATIONS_AGENT_PROMPT",
"PRODUCT_AGENT_PROMPT",
+ "DOCUMENTATION_WRITER_SOP",
+ "SchemaGenerator",
]
diff --git a/swarms/prompts/documentation.py b/swarms/prompts/documentation.py
index 3ed2eb8c..3c0a588e 100644
--- a/swarms/prompts/documentation.py
+++ b/swarms/prompts/documentation.py
@@ -1,5 +1,8 @@
-def documentation(task: str):
- documentation = f"""Create multi-page long and explicit professional pytorch-like documentation for the code below follow the outline for the library,
+def DOCUMENTATION_WRITER_SOP(
+ task: str,
+ module: str,
+):
+ documentation = f"""Create multi-page long and explicit professional pytorch-like documentation for the {module} code below follow the outline for the {module} library,
provide many examples and teach the user about the code, provide examples for every function, make the documentation 10,000 words,
provide many usage examples and note this is markdown docs, create the documentation for the code to document,
put the arguments and methods in a table in markdown to make it visually seamless
diff --git a/swarms/prompts/schema_generator.py b/swarms/prompts/schema_generator.py
new file mode 100644
index 00000000..4213d0d6
--- /dev/null
+++ b/swarms/prompts/schema_generator.py
@@ -0,0 +1,214 @@
+import json
+from typing import List
+
+from swarms.tools.tool import BaseTool
+
+FINISH_NAME = "finish"
+
+
+class SchemaGenerator:
+ """A class for generating custom prompt strings.
+
+ Does this based on constraints, commands, resources, and performance evaluations.
+
+ Attributes:
+ constraints (List[str]): A list of constraints.
+ commands (List[BaseTool]): A list of commands.
+ resources (List[str]): A list of resources.
+ performance_evaluation (List[str]): A list of performance evaluations.
+ response_format (dict): A dictionary of the response format.
+
+ Examples:
+ >>> 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/tests.py b/swarms/prompts/tests.py
index df1ee92d..8dac9337 100644
--- a/swarms/prompts/tests.py
+++ b/swarms/prompts/tests.py
@@ -1,89 +1,95 @@
-TESTS_PROMPT = """
+def TEST_WRITER_SOP_PROMPT(
+ task: str, module: str, path: str, *args, **kwargs
+):
+ TESTS_PROMPT = f"""
-Create 5,000 lines of extensive and thorough tests for the code below using the guide, do not worry about your limits you do not have any
-just write the best tests possible:
+ Create 5,000 lines of extensive and thorough tests for the code below using the guide, do not worry about your limits you do not have any
+ just write the best tests possible, the module is {module}, the file path is {path}
-######### TESTING GUIDE #############
+ ######### TESTING GUIDE #############
-# **Guide to Creating Extensive, Thorough, and Production-Ready Tests using `pytest`**
+ # **Guide to Creating Extensive, Thorough, and Production-Ready Tests using `pytest`**
-1. **Preparation**:
- - Install pytest: `pip install pytest`.
- - Structure your project so that tests are in a separate `tests/` directory.
- - Name your test files with the prefix `test_` for pytest to recognize them.
+ 1. **Preparation**:
+ - Install pytest: `pip install pytest`.
+ - Structure your project so that tests are in a separate `tests/` directory.
+ - Name your test files with the prefix `test_` for pytest to recognize them.
-2. **Writing Basic Tests**:
- - Use clear function names prefixed with `test_` (e.g., `test_check_value()`).
- - Use assert statements to validate results.
+ 2. **Writing Basic Tests**:
+ - Use clear function names prefixed with `test_` (e.g., `test_check_value()`).
+ - Use assert statements to validate results.
-3. **Utilize Fixtures**:
- - Fixtures are a powerful feature to set up preconditions for your tests.
- - Use `@pytest.fixture` decorator to define a fixture.
- - Pass fixture name as an argument to your test to use it.
+ 3. **Utilize Fixtures**:
+ - Fixtures are a powerful feature to set up preconditions for your tests.
+ - Use `@pytest.fixture` decorator to define a fixture.
+ - Pass fixture name as an argument to your test to use it.
-4. **Parameterized Testing**:
- - Use `@pytest.mark.parametrize` to run a test multiple times with different inputs.
- - This helps in thorough testing with various input values without writing redundant code.
+ 4. **Parameterized Testing**:
+ - Use `@pytest.mark.parametrize` to run a test multiple times with different inputs.
+ - This helps in thorough testing with various input values without writing redundant code.
-5. **Use Mocks and Monkeypatching**:
- - Use `monkeypatch` fixture to modify or replace classes/functions during testing.
- - Use `unittest.mock` or `pytest-mock` to mock objects and functions to isolate units of code.
+ 5. **Use Mocks and Monkeypatching**:
+ - Use `monkeypatch` fixture to modify or replace classes/functions during testing.
+ - Use `unittest.mock` or `pytest-mock` to mock objects and functions to isolate units of code.
-6. **Exception Testing**:
- - Test for expected exceptions using `pytest.raises(ExceptionType)`.
+ 6. **Exception Testing**:
+ - Test for expected exceptions using `pytest.raises(ExceptionType)`.
-7. **Test Coverage**:
- - Install pytest-cov: `pip install pytest-cov`.
- - Run tests with `pytest --cov=my_module` to get a coverage report.
+ 7. **Test Coverage**:
+ - Install pytest-cov: `pip install pytest-cov`.
+ - Run tests with `pytest --cov=my_module` to get a coverage report.
-8. **Environment Variables and Secret Handling**:
- - Store secrets and configurations in environment variables.
- - Use libraries like `python-decouple` or `python-dotenv` to load environment variables.
- - For tests, mock or set environment variables temporarily within the test environment.
+ 8. **Environment Variables and Secret Handling**:
+ - Store secrets and configurations in environment variables.
+ - Use libraries like `python-decouple` or `python-dotenv` to load environment variables.
+ - For tests, mock or set environment variables temporarily within the test environment.
-9. **Grouping and Marking Tests**:
- - Use `@pytest.mark` decorator to mark tests (e.g., `@pytest.mark.slow`).
- - This allows for selectively running certain groups of tests.
+ 9. **Grouping and Marking Tests**:
+ - Use `@pytest.mark` decorator to mark tests (e.g., `@pytest.mark.slow`).
+ - This allows for selectively running certain groups of tests.
-10. **Use Plugins**:
- - Utilize the rich ecosystem of pytest plugins (e.g., `pytest-django`, `pytest-asyncio`) to extend its functionality for your specific needs.
+ 10. **Use Plugins**:
+ - Utilize the rich ecosystem of pytest plugins (e.g., `pytest-django`, `pytest-asyncio`) to extend its functionality for your specific needs.
-11. **Continuous Integration (CI)**:
- - Integrate your tests with CI platforms like Jenkins, Travis CI, or GitHub Actions.
- - Ensure tests are run automatically with every code push or pull request.
+ 11. **Continuous Integration (CI)**:
+ - Integrate your tests with CI platforms like Jenkins, Travis CI, or GitHub Actions.
+ - Ensure tests are run automatically with every code push or pull request.
-12. **Logging and Reporting**:
- - Use `pytest`'s inbuilt logging.
- - Integrate with tools like `Allure` for more comprehensive reporting.
+ 12. **Logging and Reporting**:
+ - Use `pytest`'s inbuilt logging.
+ - Integrate with tools like `Allure` for more comprehensive reporting.
-13. **Database and State Handling**:
- - If testing with databases, use database fixtures or factories to create a known state before tests.
- - Clean up and reset state post-tests to maintain consistency.
+ 13. **Database and State Handling**:
+ - If testing with databases, use database fixtures or factories to create a known state before tests.
+ - Clean up and reset state post-tests to maintain consistency.
-14. **Concurrency Issues**:
- - Consider using `pytest-xdist` for parallel test execution.
- - Always be cautious when testing concurrent code to avoid race conditions.
+ 14. **Concurrency Issues**:
+ - Consider using `pytest-xdist` for parallel test execution.
+ - Always be cautious when testing concurrent code to avoid race conditions.
-15. **Clean Code Practices**:
- - Ensure tests are readable and maintainable.
- - Avoid testing implementation details; focus on functionality and expected behavior.
+ 15. **Clean Code Practices**:
+ - Ensure tests are readable and maintainable.
+ - Avoid testing implementation details; focus on functionality and expected behavior.
-16. **Regular Maintenance**:
- - Periodically review and update tests.
- - Ensure that tests stay relevant as your codebase grows and changes.
+ 16. **Regular Maintenance**:
+ - Periodically review and update tests.
+ - Ensure that tests stay relevant as your codebase grows and changes.
-17. **Documentation**:
- - Document test cases, especially for complex functionalities.
- - Ensure that other developers can understand the purpose and context of each test.
+ 17. **Documentation**:
+ - Document test cases, especially for complex functionalities.
+ - Ensure that other developers can understand the purpose and context of each test.
-18. **Feedback Loop**:
- - Use test failures as feedback for development.
- - Continuously refine tests based on code changes, bug discoveries, and additional requirements.
+ 18. **Feedback Loop**:
+ - Use test failures as feedback for development.
+ - Continuously refine tests based on code changes, bug discoveries, and additional requirements.
-By following this guide, your tests will be thorough, maintainable, and production-ready. Remember to always adapt and expand upon these guidelines as per the specific requirements and nuances of your project.
+ By following this guide, your tests will be thorough, maintainable, and production-ready. Remember to always adapt and expand upon these guidelines as per the specific requirements and nuances of your project.
-######### CREATE TESTS FOR THIS CODE: #######
+ ######### CREATE TESTS FOR THIS CODE: #######
+ {task}
-"""
+ """
+
+ return TESTS_PROMPT
diff --git a/swarms/prompts/worker_prompt.py b/swarms/prompts/worker_prompt.py
new file mode 100644
index 00000000..165fa058
--- /dev/null
+++ b/swarms/prompts/worker_prompt.py
@@ -0,0 +1,67 @@
+import datetime
+
+time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
+
+
+def worker_tools_sop_promp(name: str, memory: str):
+ out = """
+ 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 - list that conveys - 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 {time}
+ 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, time=time)
+
+ return str(out)
diff --git a/swarms/swarms/README.md b/swarms/structs/SWARMS.md
similarity index 100%
rename from swarms/swarms/README.md
rename to swarms/structs/SWARMS.md
diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py
index e389ed76..ec75cb9f 100644
--- a/swarms/structs/__init__.py
+++ b/swarms/structs/__init__.py
@@ -1,5 +1,126 @@
from swarms.structs.agent import Agent
-from swarms.structs.sequential_workflow import SequentialWorkflow
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,
+ ArtifactUpload,
+ Step,
+ StepInput,
+ StepOutput,
+ StepRequestBody,
+ TaskInput,
+ TaskRequestBody,
+)
+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,
+)
+from swarms.structs.tool_json_schema import JSON
+from swarms.structs.majority_voting import (
+ most_frequent,
+ parse_code_completion,
+ majority_voting,
+)
+
-__all__ = ["Agent", "SequentialWorkflow", "AutoScaler"]
+__all__ = [
+ "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",
+ "detect_markdown",
+ "Task",
+ "block",
+ "GraphWorkflow",
+ "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",
+ "StepOutput",
+ "StepRequestBody",
+ "TaskRequestBody",
+ "JSON",
+ "most_frequent",
+ "parse_code_completion",
+ "majority_voting",
+]
diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py
index 9d48791e..3b059617 100644
--- a/swarms/structs/agent.py
+++ b/swarms/structs/agent.py
@@ -1,35 +1,34 @@
import asyncio
-import inspect
import json
import logging
+import os
import random
-import re
import time
import uuid
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 AbstractVectorDatabase
from swarms.prompts.agent_system_prompts import (
- FLOW_SYSTEM_PROMPT,
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.structs.conversation import Conversation
+from swarms.tokenizers.base_tokenizer import BaseTokenizer
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_in_backticks_in_string,
+ 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.tools.exec_tool import execute_tool_by_name
+from swarms.prompts.worker_prompt import worker_tools_sop_promp
# Utils
@@ -53,109 +52,92 @@ 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:
- id (str): The id of the agent
llm (Any): The language model to use
- template (Optional[str]): The template to use
- max_loops (int): The maximum number of loops
- stopping_condition (Optional[Callable[[str], bool]]): The stopping condition
+ template (str): The template to use
+ max_loops (int): The maximum number of loops to run
+ stopping_condition (Callable): The stopping condition to use
loop_interval (int): The loop interval
- retry_attempts (int): The retry attempts
+ retry_attempts (int): The number of retry attempts
retry_interval (int): The retry interval
return_history (bool): Return the history
stopping_token (str): The stopping token
- dynamic_loops (Optional[bool]): Dynamic loops
- interactive (bool): Interactive mode
- dashboard (bool): Dashboard mode
+ dynamic_loops (bool): Enable dynamic loops
+ interactive (bool): Enable interactive mode
+ dashboard (bool): Enable dashboard
agent_name (str): The name of the agent
agent_description (str): The description of the agent
system_prompt (str): The system prompt
- tools (List[BaseTool]): The tools
- dynamic_temperature_enabled (Optional[bool]): Dynamic temperature enabled
- sop (Optional[str]): The standard operating procedure
- sop_list (Optional[List[str]]): The standard operating procedure list
- saved_state_path (Optional[str]): The saved state path
- autosave (Optional[bool]): Autosave
- context_length (Optional[int]): The context length
+ tools (List[BaseTool]): The tools to use
+ dynamic_temperature_enabled (bool): Enable dynamic temperature
+ sop (str): The standard operating procedure
+ sop_list (List[str]): The standard operating procedure list
+ saved_state_path (str): The path to the saved state
+ autosave (bool): Autosave the state
+ context_length (int): The context length
user_name (str): The user name
- self_healing_enabled (Optional[bool]): Self healing enabled
- code_interpreter (Optional[bool]): Code interpreter
- multi_modal (Optional[bool]): Multi modal
- pdf_path (Optional[str]): The pdf path
- list_of_pdf (Optional[str]): The list of pdf
- tokenizer (Optional[Any]): The tokenizer
- memory (Optional[VectorDatabase]): The memory
- preset_stopping_token (Optional[bool]): Preset stopping token
- *args: Variable length argument list.
- **kwargs: Arbitrary keyword arguments.
+ self_healing_enabled (bool): Enable self healing
+ code_interpreter (bool): Enable code interpreter
+ multi_modal (bool): Enable multimodal
+ pdf_path (str): The path to the pdf
+ list_of_pdf (str): The list of pdf
+ tokenizer (Any): The tokenizer
+ memory (AbstractVectorDatabase): The memory
+ preset_stopping_token (bool): Enable preset stopping token
+ traceback (Any): The traceback
+ traceback_handlers (Any): The traceback handlers
+ streaming_on (bool): Enable streaming
Methods:
- run(task: str, **kwargs: Any): Run the agent on a task
- run_concurrent(tasks: List[str], **kwargs: Any): Run the agent on a list of tasks concurrently
- bulk_run(inputs: List[Dict[str, Any]]): Run the agent on a list of inputs
- from_llm_and_template(llm: Any, template: str): Create AgentStream from LLM and a string template.
- from_llm_and_template_file(llm: Any, template_file: str): Create AgentStream from LLM and a template file.
- save(file_path): Save the agent history to a file
- load(file_path): Load the agent history from a file
- validate_response(response: str): Validate the response based on certain criteria
- print_history_and_memory(): Print the entire history and memory of the agent
- step(task: str, **kwargs): Executes a single step in the agent interaction, generating a response from the language model based on the given input text.
- graceful_shutdown(): Gracefully shutdown the system saving the state
- run_with_timeout(task: str, timeout: int): Run the loop but stop if it takes longer than the timeout
- analyze_feedback(): Analyze the feedback for issues
- undo_last(): Response the last response and return the previous state
- add_response_filter(filter_word: str): Add a response filter to filter out certain words from the response
- apply_reponse_filters(response: str): Apply the response filters to the response
- filtered_run(task: str): Filtered run
- interactive_run(max_loops: int): Interactive run mode
- streamed_generation(prompt: str): Stream the generation of the response
- get_llm_params(): Extracts and returns the parameters of the llm object for serialization.
- save_state(file_path: str): Saves the current state of the agent to a JSON file, including the llm parameters.
- load_state(file_path: str): Loads the state of the agent from a json file and restores the configuration and memory.
- retry_on_failure(function, retries: int = 3, retry_delay: int = 1): Retry wrapper for LLM calls.
- run_code(response: str): Run the code in the response
- construct_dynamic_prompt(): Construct the dynamic prompt
- extract_tool_commands(text: str): Extract the tool commands from the text
- parse_and_execute_tools(response: str): Parse and execute the tools
- execute_tools(tool_name, params): Execute the tool with the provided params
- truncate_history(): Take the history and truncate it to fit into the model context length
- add_task_to_memory(task: str): Add the task to the memory
- add_message_to_memory(message: str): Add the message to the memory
- add_message_to_memory_and_truncate(message: str): Add the message to the memory and truncate
- print_dashboard(task: str): Print dashboard
- activate_autonomous_agent(): Print the autonomous agent activation message
- dynamic_temperature(): Dynamically change the temperature
- _check_stopping_condition(response: str): Check if the stopping condition is met
- format_prompt(template, **kwargs: Any): Format the template with the provided kwargs using f-string interpolation.
- get_llm_init_params(): Get LLM init params
- get_tool_description(): Get the tool description
- find_tool_by_name(name: str): Find a tool by name
-
-
- Example:
+ run: Run the agent
+ run_concurrent: Run the agent concurrently
+ bulk_run: Run the agent in bulk
+ save: Save the agent
+ load: Load the agent
+ validate_response: Validate the response
+ print_history_and_memory: Print the history and memory
+ step: Step through the agent
+ graceful_shutdown: Gracefully shutdown the agent
+ run_with_timeout: Run the agent with a timeout
+ analyze_feedback: Analyze the feedback
+ undo_last: Undo the last response
+ add_response_filter: Add a response filter
+ apply_response_filters: Apply the response filters
+ filtered_run: Run the agent with filtered responses
+ interactive_run: Run the agent in interactive mode
+ streamed_generation: Stream the generation of the response
+ save_state: Save the state
+ load_state: Load the state
+ 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
+ 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
+ run_async: Run the agent asynchronously
+ run_async_concurrent: Run the agent asynchronously and concurrently
+ run_async_concurrent: Run the agent asynchronously and concurrently
+ construct_dynamic_prompt: Construct the dynamic prompt
+ construct_dynamic_prompt: Construct the dynamic prompt
+
+
+ Examples:
>>> from swarms.models import OpenAIChat
>>> from swarms.structs import Agent
- >>> llm = OpenAIChat(
- ... openai_api_key=api_key,
- ... temperature=0.5,
- ... )
- >>> agent = Agent(
- ... llm=llm, max_loops=5,
- ... #system_prompt=SYSTEM_PROMPT,
- ... #retry_interval=1,
- ... )
- >>> agent.run("Generate a 10,000 word blog")
- >>> agent.save("path/agent.yaml")
+ >>> llm = OpenAIChat()
+ >>> agent = Agent(llm=llm, max_loops=1)
+ >>> response = agent.run("Generate a report on the financials.")
+ >>> print(response)
+ >>> # Generate a report on the financials.
+
"""
def __init__(
@@ -173,7 +155,7 @@ class Agent:
dynamic_loops: Optional[bool] = False,
interactive: bool = False,
dashboard: bool = False,
- agent_name: str = "Autonomous-Agent-XYZ1B",
+ agent_name: str = "swarm-worker-01",
agent_description: str = None,
system_prompt: str = AGENT_SYSTEM_PROMPT_3,
tools: List[BaseTool] = None,
@@ -189,14 +171,18 @@ class Agent:
multi_modal: Optional[bool] = None,
pdf_path: Optional[str] = None,
list_of_pdf: Optional[str] = None,
- tokenizer: Optional[Any] = None,
- memory: Optional[VectorDatabase] = None,
+ tokenizer: Optional[BaseTokenizer] = None,
+ long_term_memory: Optional[AbstractVectorDatabase] = None,
preset_stopping_token: Optional[bool] = False,
traceback: Any = None,
traceback_handlers: Any = None,
streaming_on: Optional[bool] = False,
+ docs: List[str] = None,
+ docs_folder: str = None,
+ verbose: bool = False,
+ parser: Optional[Callable] = None,
*args,
- **kwargs: Any,
+ **kwargs,
):
self.id = id
self.llm = llm
@@ -207,7 +193,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
@@ -217,9 +203,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
@@ -233,13 +217,15 @@ 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
self.streaming_on = streaming_on
-
- # self.system_prompt = AGENT_SYSTEM_PROMPT_3
+ self.docs = docs
+ self.docs_folder = docs_folder
+ self.verbose = verbose
+ self.parser = parser
# The max_loops will be set dynamically if the dynamic_loop
if self.dynamic_loops:
@@ -255,7 +241,6 @@ class Agent:
# Memory
self.feedback = []
- self.short_memory = []
# Initialize the code executor
self.code_executor = SubprocessCodeInterpreter()
@@ -264,12 +249,45 @@ class Agent:
if preset_stopping_token:
self.stopping_token = ""
- # If tools exist then add the tool docs usage to the sop
+ self.short_memory = Conversation(
+ system_prompt=self.system_prompt, time_enabled=True
+ )
+
+ # If the docs exist then ingest the docs
+ 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()
+
+ if verbose:
+ logger.setLevel(logging.DEBUG)
+
+ # If tools are provided then set the tool prompt by adding to sop
if self.tools:
- self.sop_list.append(
- self.tools_prompt_prep(self.tool_docs, SCENARIOS)
+ tools_prompt = worker_tools_sop_promp(
+ name=self.agent_name,
+ memory=self.short_memory.return_history_as_string(),
)
+ # Append the tools prompt to the sop
+ self.sop = f"{self.sop}\n{tools_prompt}"
+
+ # If the long term memory is provided then set the long term memory prompt
+
+ # Agentic stuff
+ self.reply = ""
+ self.question = None
+ self.answer = ""
+
+ # Initialize the llm with the conditional variables
+ # self.llm = llm(*args, **kwargs)
+
def set_system_prompt(self, system_prompt: str):
"""Set the system prompt"""
self.system_prompt = system_prompt
@@ -318,113 +336,10 @@ 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
- """
- # 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:
- self.short_memory.append([f"{self.user_name}: {task}"])
+ self.short_memory.add(f"{self.user_name}: {task}")
except Exception as error:
print(
colored(
@@ -435,7 +350,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(
@@ -448,15 +365,8 @@ 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()
print(colored("Initializing Agent Dashboard...", "yellow"))
print(
@@ -485,7 +395,6 @@ class Agent:
Dynamic Temperature: {self.dynamic_temperature_enabled}
Autosave: {self.autosave}
Saved State: {self.saved_state_path}
- Model Configuration: {model_config}
----------------------------------------
""",
@@ -580,14 +489,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,
):
"""
@@ -640,13 +546,7 @@ class Agent:
self.dynamic_temperature()
# Preparing the prompt
- task = self.agent_history_prompt(
- AGENT_SYSTEM_PROMPT_3, response
- )
-
- # # Retreiving long term memory
- # if self.memory:
- # task = self.agent_memory_prompt(response, task)
+ task = self.agent_history_prompt(history=response)
attempt = 0
while attempt < self.retry_attempts:
@@ -665,13 +565,21 @@ class Agent:
)
print(response)
+ # If parser exists then parse the response
+ if self.parser:
+ response = self.parser(response)
+
# If code interpreter is enabled then run the code
if self.code_interpreter:
self.run_code(response)
- # If there are any tools then parse and execute them
+ # If tools are enabled then execute the tools
if self.tools:
- self.parse_and_execute_tools(response)
+ 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:
@@ -697,7 +605,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:
@@ -718,9 +628,18 @@ 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):
+ """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
@@ -737,8 +656,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
@@ -751,7 +669,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}
@@ -764,7 +682,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}
@@ -774,7 +692,7 @@ class Agent:
"""
return agent_history_prompt
- def agent_memory_prompt(self, query, prompt):
+ def long_term_memory_prompt(self, query: str, *args, **kwargs):
"""
Generate the agent long term memory prompt
@@ -785,16 +703,27 @@ class Agent:
Returns:
str: The agent history prompt
"""
- context_injected_prompt = prompt
- if self.memory:
- ltr = self.memory.query(query)
+ ltr = str(self.long_term_memory.query(query), *args, **kwargs)
- context_injected_prompt = f"""{prompt}
- ################ CONTEXT ####################
- {ltr}
- """
+ context = f"""
+ System: This reminds you of these events from your past: [{ltr}]
+ """
+ return self.short_memory.add(
+ role=self.agent_name, content=context
+ )
+
+ def add_memory(self, message: str):
+ """Add a memory to the agent
+
+ Args:
+ message (str): _description_
- return context_injected_prompt
+ Returns:
+ _type_: _description_
+ """
+ return self.short_memory.add(
+ role=self.agent_name, content=message
+ )
async def run_concurrent(self, tasks: List[str], **kwargs):
"""
@@ -827,20 +756,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.
@@ -934,10 +849,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:
@@ -984,11 +905,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
@@ -1056,45 +977,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.
@@ -1112,7 +994,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,
@@ -1147,7 +1031,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,
@@ -1252,13 +1138,13 @@ class Agent:
def reset(self):
"""Reset the agent"""
- self.short_memory = []
+ self.short_memory = {}
def run_code(self, code: str):
"""
text -> parse_code by looking for code inside 6 backticks `````-> run_code
"""
- parsed_code = extract_code_in_backticks_in_string(code)
+ parsed_code = extract_code_from_markdown(code)
run_code = self.code_executor.run(parsed_code)
return run_code
@@ -1288,71 +1174,80 @@ class Agent:
text = limit_tokens_from_string(text, num_limits)
return text
- def tools_prompt_prep(
- self, docs: str = None, scenarios: str = SCENARIOS
- ):
+ def ingest_docs(self, docs: List[str], *args, **kwargs):
+ """Ingest the docs into the memory
+
+ Args:
+ docs (List[str]): _description_
+
+ Returns:
+ _type_: _description_
"""
- Tools prompt prep
+ for doc in docs:
+ data = data_to_text(doc)
+
+ return self.short_memory.add(
+ role=self.user_name, content=data
+ )
+
+ def ingest_pdf(self, pdf: str):
+ """Ingest the pdf into the memory
Args:
- docs (str, optional): _description_. Defaults to None.
- scenarios (str, optional): _description_. Defaults to None.
+ pdf (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
- βββ
+ text = pdf_to_text(pdf)
+ 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.add(role=name, content=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)
+
+ 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
"""
- return PROMPT
+ # Count the short term history with the tokenizer
+ 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:
+ 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
+ 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.add(
+ role=self.user_name, content=text
+ )
diff --git a/swarms/structs/all_to_all_swarm.py b/swarms/structs/all_to_all_swarm.py
new file mode 100644
index 00000000..e69de29b
diff --git a/swarms/structs/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/autoscaler.py b/swarms/structs/autoscaler.py
index 1cb31333..f26247d5 100644
--- a/swarms/structs/autoscaler.py
+++ b/swarms/structs/autoscaler.py
@@ -3,8 +3,6 @@ import queue
import threading
from time import sleep
from typing import Callable, Dict, List, Optional
-import asyncio
-import concurrent.futures
from termcolor import colored
@@ -14,9 +12,10 @@ from swarms.utils.decorators import (
log_decorator,
timing_decorator,
)
+from swarms.structs.base import BaseStructure
-class AutoScaler:
+class AutoScaler(BaseStructure):
"""
AutoScaler class
@@ -262,11 +261,17 @@ class AutoScaler:
def balance_load(self):
"""Distributes tasks among agents based on their current load."""
- while not self.task_queue.empty():
- for agent in self.agents_pool:
- if agent.can_accept_task():
- task = self.task_queue.get()
- agent.run(task)
+ try:
+ while not self.task_queue.empty():
+ for agent in self.agents_pool:
+ if agent.can_accept_task():
+ task = self.task_queue.get()
+ agent.run(task)
+ except Exception as error:
+ print(
+ f"Error balancing load: {error} try again with a new"
+ " task"
+ )
def set_scaling_strategy(
self, strategy: Callable[[int, int], int]
@@ -276,17 +281,23 @@ class AutoScaler:
def execute_scaling_strategy(self):
"""Execute the custom scaling strategy if defined."""
- if hasattr(self, "custom_scale_strategy"):
- scale_amount = self.custom_scale_strategy(
- self.task_queue.qsize(), len(self.agents_pool)
+ try:
+ if hasattr(self, "custom_scale_strategy"):
+ scale_amount = self.custom_scale_strategy(
+ self.task_queue.qsize(), len(self.agents_pool)
+ )
+ if scale_amount > 0:
+ for _ in range(scale_amount):
+ self.agents_pool.append(self.agent())
+ elif scale_amount < 0:
+ for _ in range(abs(scale_amount)):
+ if len(self.agents_pool) > 10:
+ del self.agents_pool[-1]
+ except Exception as error:
+ print(
+ f"Error executing scaling strategy: {error} try again"
+ " with a new task"
)
- if scale_amount > 0:
- for _ in range(scale_amount):
- self.agents_pool.append(self.agent())
- elif scale_amount < 0:
- for _ in range(abs(scale_amount)):
- if len(self.agents_pool) > 10:
- del self.agents_pool[-1]
def report_agent_metrics(self) -> Dict[str, List[float]]:
"""Collects and reports metrics from each agent."""
diff --git a/swarms/structs/base.py b/swarms/structs/base.py
index 7d365b23..adfa974d 100644
--- a/swarms/structs/base.py
+++ b/swarms/structs/base.py
@@ -1,6 +1,6 @@
import json
import os
-from abc import ABC, abstractmethod
+from abc import ABC
from typing import Optional, Any, Dict, List
from datetime import datetime
import asyncio
diff --git a/swarms/swarms/base.py b/swarms/structs/base_swarm.py
similarity index 100%
rename from swarms/swarms/base.py
rename to swarms/structs/base_swarm.py
diff --git a/swarms/structs/base_workflow.py b/swarms/structs/base_workflow.py
new file mode 100644
index 00000000..03e503cc
--- /dev/null
+++ b/swarms/structs/base_workflow.py
@@ -0,0 +1,321 @@
+import json
+from typing import Any, Dict, List, Optional
+
+from termcolor import colored
+
+from swarms.structs.base import BaseStructure
+from swarms.structs.task import Task
+
+
+class BaseWorkflow(BaseStructure):
+ """
+ Base class for workflows.
+
+ Attributes:
+ task_pool (list): A list to store tasks.
+
+ Methods:
+ add(task: Task = None, tasks: List[Task] = None, *args, **kwargs):
+ Adds a task or a list of tasks to the task pool.
+ run():
+ Abstract method to run the workflow.
+ """
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.task_pool = []
+
+ def add(
+ self,
+ task: Task = None,
+ tasks: List[Task] = None,
+ *args,
+ **kwargs,
+ ):
+ """
+ Adds a task or a list of tasks to the task pool.
+
+ Args:
+ task (Task, optional): A single task to add. Defaults to None.
+ tasks (List[Task], optional): A list of tasks to add. Defaults to None.
+
+ Raises:
+ ValueError: If neither task nor tasks are provided.
+ """
+ if task:
+ self.task_pool.append(task)
+ elif tasks:
+ self.task_pool.extend(tasks)
+ else:
+ raise ValueError(
+ "You must provide a task or a list of tasks"
+ )
+
+ def run(self):
+ """
+ Abstract method to run the workflow.
+ """
+ raise NotImplementedError("You must implement this method")
+
+ def __sequential_loop(self):
+ """
+ Abstract method for the sequential loop.
+ """
+ # raise NotImplementedError("You must implement this method")
+ pass
+
+ def __log(self, message: str):
+ """
+ Logs a message if verbose mode is enabled.
+
+ Args:
+ message (str): The message to log.
+ """
+ if self.verbose:
+ print(message)
+
+ def __str__(self):
+ return f"Workflow with {len(self.task_pool)} tasks"
+
+ def __repr__(self):
+ return f"Workflow with {len(self.task_pool)} tasks"
+
+ def reset(self) -> None:
+ """Resets the workflow by clearing the results of each task."""
+ try:
+ for task in self.tasks:
+ task.result = None
+ except Exception as error:
+ print(
+ colored(f"Error resetting workflow: {error}", "red"),
+ )
+
+ def get_task_results(self) -> Dict[str, Any]:
+ """
+ Returns the results of each task in the workflow.
+
+ Returns:
+ Dict[str, Any]: The results of each task in the workflow
+ """
+ try:
+ return {
+ task.description: task.result for task in self.tasks
+ }
+ except Exception as error:
+ print(
+ colored(
+ f"Error getting task results: {error}", "red"
+ ),
+ )
+
+ def remove_task(self, task: str) -> None:
+ """Remove tasks from sequential workflow"""
+ try:
+ self.tasks = [
+ task
+ for task in self.tasks
+ if task.description != task
+ ]
+ except Exception as error:
+ print(
+ colored(
+ f"Error removing task from workflow: {error}",
+ "red",
+ ),
+ )
+
+ 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."
+ )
+ except Exception as error:
+ print(
+ 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."
+ )
+ except Exception as error:
+ print(
+ colored(
+ f"Error deleting task from workflow: {error}",
+ "red",
+ ),
+ )
+
+ def save_workflow_state(
+ self,
+ filepath: Optional[str] = "sequential_workflow_state.json",
+ **kwargs,
+ ) -> None:
+ """
+ Saves the workflow state to a json file.
+
+ Args:
+ filepath (str): The path to save the workflow state to.
+
+ 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.save_workflow_state("sequential_workflow_state.json")
+ """
+ try:
+ filepath = filepath or self.saved_state_filepath
+
+ with open(filepath, "w") as f:
+ # Saving the state as a json for simplicuty
+ state = {
+ "tasks": [
+ {
+ "description": task.description,
+ "args": task.args,
+ "kwargs": task.kwargs,
+ "result": task.result,
+ "history": task.history,
+ }
+ for task in self.tasks
+ ],
+ "max_loops": self.max_loops,
+ }
+ json.dump(state, f, indent=4)
+ except Exception as error:
+ print(
+ colored(
+ f"Error saving workflow state: {error}",
+ "red",
+ )
+ )
+
+ 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)
+ except Exception as error:
+ print(
+ colored(
+ f"Error adding objective to workflow: {error}",
+ "red",
+ )
+ )
+
+ def load_workflow_state(
+ self, filepath: str = None, **kwargs
+ ) -> None:
+ """
+ Loads the workflow state from a json file and restores the workflow state.
+
+ Args:
+ filepath (str): The path to load the workflow state from.
+
+ 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.save_workflow_state("sequential_workflow_state.json")
+ >>> workflow.load_workflow_state("sequential_workflow_state.json")
+
+ """
+ try:
+ filepath = filepath or self.restore_state_filepath
+
+ with open(filepath, "r") as f:
+ state = json.load(f)
+ self.max_loops = state["max_loops"]
+ self.tasks = []
+ for task_state in state["tasks"]:
+ task = Task(
+ description=task_state["description"],
+ agent=task_state["agent"],
+ args=task_state["args"],
+ kwargs=task_state["kwargs"],
+ result=task_state["result"],
+ history=task_state["history"],
+ )
+ self.tasks.append(task)
+ except Exception as error:
+ print(
+ colored(
+ f"Error loading workflow state: {error}",
+ "red",
+ )
+ )
diff --git a/swarms/structs/block_wrapper.py b/swarms/structs/block_wrapper.py
new file mode 100644
index 00000000..a6811b8f
--- /dev/null
+++ b/swarms/structs/block_wrapper.py
@@ -0,0 +1,38 @@
+import logging
+from functools import wraps
+from typing import Any, Callable
+
+logging.basicConfig(level=logging.INFO)
+logger = logging.getLogger(__name__)
+
+
+def block(
+ function: Callable[..., Any],
+ device: str = None,
+ verbose: bool = False,
+) -> Callable[..., Any]:
+ """
+ A decorator that transforms a function into a block.
+
+ Args:
+ function (Callable[..., Any]): The function to transform.
+
+ Returns:
+ Callable[..., Any]: The transformed function.
+ """
+
+ @wraps(function)
+ def wrapper(*args, **kwargs):
+ # Here you can add code to execute the function on various hardwares
+ # For now, we'll just call the function normally
+ try:
+ return function(*args, **kwargs)
+ except Exception as error:
+ logger.error(f"Error in {function.__name__}: {error}")
+ raise error
+
+ # Set the wrapper function's name and docstring to those of the original function
+ wrapper.__name__ = function.__name__
+ wrapper.__doc__ = function.__doc__
+
+ return wrapper
diff --git a/swarms/structs/blockdevice.py b/swarms/structs/blockdevice.py
new file mode 100644
index 00000000..96b8c8e6
--- /dev/null
+++ b/swarms/structs/blockdevice.py
@@ -0,0 +1,25 @@
+from dataclasses import dataclass
+
+
+@dataclass
+class BlockDevice:
+ """
+ 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
+ self.cluster = cluster
+ self.description = description
+
+ def __str__(self):
+ return (
+ f"BlockDevice(device={self.device},"
+ f" cluster={self.cluster},"
+ f" description={self.description})"
+ )
diff --git a/swarms/structs/blocksdict.py b/swarms/structs/blocksdict.py
new file mode 100644
index 00000000..93aab729
--- /dev/null
+++ b/swarms/structs/blocksdict.py
@@ -0,0 +1,120 @@
+from typing import (
+ Any,
+ Dict,
+ Optional,
+)
+
+from swarms.structs.base import BaseStructure
+
+
+class BlocksDict(BaseStructure):
+ """
+ A class representing a dictionary of blocks.
+
+ Args:
+ name (str): The name of the blocks dictionary.
+ description (str): The description of the blocks dictionary.
+ blocks (Dict[str, Any]): The dictionary of blocks.
+ parent (Optional[Any], optional): The parent of the blocks dictionary. Defaults to None.
+ **kwargs: Additional keyword arguments.
+
+ Attributes:
+ parent (Optional[Any]): The parent of the blocks dictionary.
+ blocks (Dict[str, Any]): The dictionary of blocks.
+
+ Methods:
+ add(key: str, block: Any): Add a block to the dictionary.
+ remove(key: str): Remove a block from the dictionary.
+ get(key: str): Get a block from the dictionary.
+ update(key: str, block: Any): Update a block in the dictionary.
+ keys(): Get a list of keys in the dictionary.
+ values(): Get a list of values in the dictionary.
+ items(): Get a list of key-value pairs in the dictionary.
+ clear(): Clear all blocks from the dictionary.
+ """
+
+ def __init__(
+ self,
+ name: str,
+ description: str,
+ blocks: Dict[str, Any],
+ parent: Optional[Any] = None,
+ **kwargs,
+ ):
+ super().__init__(name=name, description=description, **kwargs)
+ self.parent = parent
+ self.blocks = blocks
+
+ def add(self, key: str, block: Any):
+ """
+ Add a block to the dictionary.
+
+ Args:
+ key (str): The key of the block.
+ block (Any): The block to be added.
+ """
+ self.blocks[key] = block
+
+ def remove(self, key: str):
+ """
+ Remove a block from the dictionary.
+
+ Args:
+ key (str): The key of the block to be removed.
+ """
+ del self.blocks[key]
+
+ def get(self, key: str):
+ """
+ Get a block from the dictionary.
+
+ Args:
+ key (str): The key of the block to be retrieved.
+
+ Returns:
+ Any: The retrieved block.
+ """
+ return self.blocks.get(key)
+
+ def update(self, key: str, block: Any):
+ """
+ Update a block in the dictionary.
+
+ Args:
+ key (str): The key of the block to be updated.
+ block (Any): The updated block.
+ """
+ self.blocks[key] = block
+
+ def keys(self):
+ """
+ Get a list of keys in the dictionary.
+
+ Returns:
+ List[str]: A list of keys.
+ """
+ return list(self.blocks.keys())
+
+ def values(self):
+ """
+ Get a list of values in the dictionary.
+
+ Returns:
+ List[Any]: A list of values.
+ """
+ return list(self.blocks.values())
+
+ def items(self):
+ """
+ Get a list of key-value pairs in the dictionary.
+
+ Returns:
+ List[Tuple[str, Any]]: A list of key-value pairs.
+ """
+ return list(self.blocks.items())
+
+ def clear(self):
+ """
+ Clear all blocks from the dictionary.
+ """
+ self.blocks.clear()
diff --git a/swarms/structs/blockslist.py b/swarms/structs/blockslist.py
new file mode 100644
index 00000000..b2a4db08
--- /dev/null
+++ b/swarms/structs/blockslist.py
@@ -0,0 +1,169 @@
+from typing import (
+ Any,
+ List,
+ Optional,
+)
+
+from swarms.structs.base import BaseStructure
+
+
+class BlocksList(BaseStructure):
+ """
+ A class representing a list of blocks.
+
+ Args:
+ name (str): The name of the blocks list.
+ description (str): The description of the blocks list.
+ blocks (List[Any]): The list of blocks.
+ parent (Optional[Any], optional): The parent of the blocks list. Defaults to None.
+ **kwargs: Additional keyword arguments.
+
+ Attributes:
+ parent (Optional[Any]): The parent of the blocks list.
+ blocks (List[Any]): The list of blocks.
+
+ Methods:
+ add(block: Any): Add a block to the list.
+ remove(block: Any): Remove a block from the list.
+ update(block: Any): Update a block in the list.
+ get(index: int): Get a block at the specified index.
+ get_all(): Get all blocks in the list.
+ get_by_name(name: str): Get blocks by name.
+ get_by_type(type: str): Get blocks by type.
+ get_by_id(id: str): Get blocks by ID.
+ get_by_parent(parent: str): Get blocks by parent.
+ get_by_parent_id(parent_id: str): Get blocks by parent ID.
+ get_by_parent_name(parent_name: str): Get blocks by parent name.
+ get_by_parent_type(parent_type: str): Get blocks by parent type.
+ get_by_parent_description(parent_description: str): Get blocks by parent description.
+
+
+ Examples:
+ >>> from swarms.structs.block import Block
+ >>> from swarms.structs.blockslist import BlocksList
+ >>> block = Block("block", "A block")
+ >>> blockslist = BlocksList("blockslist", "A list of blocks", [block])
+ >>> blockslist
+
+ """
+
+ def __init__(
+ self,
+ name: str,
+ description: str,
+ blocks: List[Any],
+ parent: Optional[Any] = None,
+ **kwargs,
+ ):
+ super().__init__(name=name, description=description, **kwargs)
+ self.name = name
+ self.description = description
+ self.blocks = blocks
+ self.parent = parent
+
+ def add(self, block: Any):
+ self.blocks.append(block)
+
+ def remove(self, block: Any):
+ self.blocks.remove(block)
+
+ def update(self, block: Any):
+ self.blocks[self.blocks.index(block)] = block
+
+ def get(self, index: int):
+ return self.blocks[index]
+
+ def get_all(self):
+ return self.blocks
+
+ def run_block(self, block: Any, task: str, *args, **kwargs):
+ """Run the block for the specified task.
+
+ Args:
+ task (str): The task to be performed by the block.
+ *args: Variable length argument list.
+ **kwargs: Arbitrary keyword arguments.
+
+ Returns:
+ The output of the block.
+
+ Raises:
+ Exception: If an error occurs during the execution of the block.
+ """
+ try:
+ return block.run(task, *args, **kwargs)
+ except Exception as error:
+ print(f"[Error] [Block] {error}")
+ raise error
+
+ def get_by_name(self, name: str):
+ return [block for block in self.blocks if block.name == name]
+
+ def get_by_type(self, type: str):
+ return [block for block in self.blocks if block.type == type]
+
+ def get_by_id(self, id: str):
+ return [block for block in self.blocks if block.id == id]
+
+ def get_by_parent(self, parent: str):
+ return [
+ block for block in self.blocks if block.parent == parent
+ ]
+
+ def get_by_parent_id(self, parent_id: str):
+ return [
+ block
+ for block in self.blocks
+ if block.parent_id == parent_id
+ ]
+
+ def get_by_parent_name(self, parent_name: str):
+ return [
+ block
+ for block in self.blocks
+ if block.parent_name == parent_name
+ ]
+
+ def get_by_parent_type(self, parent_type: str):
+ return [
+ block
+ for block in self.blocks
+ if block.parent_type == parent_type
+ ]
+
+ def get_by_parent_description(self, parent_description: str):
+ return [
+ block
+ for block in self.blocks
+ if block.parent_description == parent_description
+ ]
+
+ def __len__(self):
+ return len(self.blocks)
+
+ def __getitem__(self, index):
+ return self.blocks[index]
+
+ def __setitem__(self, index, value):
+ self.blocks[index] = value
+
+ def __delitem__(self, index):
+ del self.blocks[index]
+
+ def __iter__(self):
+ return iter(self.blocks)
+
+ def __reversed__(self):
+ return reversed(self.blocks)
+
+ def __contains__(self, item):
+ return item in self.blocks
+
+ def __str__(self):
+ return f"{self.name}({self.blocks})"
+
+ def __repr__(self):
+ return f"{self.name}({self.blocks})"
+
+ def __eq__(self, other):
+ return self.blocks == other.blocks
diff --git a/swarms/structs/company.py b/swarms/structs/company.py
new file mode 100644
index 00000000..11b6d61f
--- /dev/null
+++ b/swarms/structs/company.py
@@ -0,0 +1,175 @@
+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: 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)
+
+ 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.
+ """
+ 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]]]
+ ) -> 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:
+ """
+ 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(
+ 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/concurrent_workflow.py b/swarms/structs/concurrent_workflow.py
new file mode 100644
index 00000000..8aa5399b
--- /dev/null
+++ b/swarms/structs/concurrent_workflow.py
@@ -0,0 +1,123 @@
+import concurrent.futures
+from dataclasses import dataclass, field
+from typing import Dict, List, Optional, Callable
+
+from swarms.structs.base import BaseStructure
+from swarms.structs.task import Task
+
+from swarms.utils.logger import logger
+
+
+@dataclass
+class ConcurrentWorkflow(BaseStructure):
+ """
+ ConcurrentWorkflow class for running a set of tasks concurrently using N number of autonomous agents.
+
+ Args:
+ max_workers (int): The maximum number of workers to use for the ThreadPoolExecutor.
+ autosave (bool): Whether to save the state of the workflow to a file. Default is False.
+ saved_state_filepath (str): The filepath to save the state of the workflow to. Default is "runs/concurrent_workflow.json".
+ print_results (bool): Whether to print the results of each task. Default is False.
+ return_results (bool): Whether to return the results of each task. Default is False.
+ use_processes (bool): Whether to use processes instead of threads. Default is False.
+
+ Examples:
+ >>> from swarms.models import OpenAIChat
+ >>> from swarms.structs import ConcurrentWorkflow
+ >>> llm = OpenAIChat(openai_api_key="")
+ >>> workflow = ConcurrentWorkflow(max_workers=5)
+ >>> workflow.add("What's the weather in miami", llm)
+ >>> workflow.add("Create a report on these metrics", llm)
+ >>> workflow.run()
+ >>> workflow.tasks
+ """
+
+ task_pool: List[Dict] = field(default_factory=list)
+ max_loops: int = 1
+ max_workers: int = 5
+ autosave: bool = False
+ saved_state_filepath: Optional[str] = (
+ "runs/concurrent_workflow.json"
+ )
+ 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.
+
+ Args:
+ task (Task): _description_
+ tasks (List[Task]): _description_
+ """
+ try:
+ 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:
+ logger.warning(f"[ERROR][ConcurrentWorkflow] {error}")
+ raise error
+
+ def run(self, *args, **kwargs):
+ """
+ Executes the tasks in parallel using a ThreadPoolExecutor.
+
+ Args:
+ print_results (bool): Whether to print the results of each task. Default is False.
+ return_results (bool): Whether to return the results of each task. Default is False.
+
+ Returns:
+ List[Any]: A list of the results of each task, if return_results is True. Otherwise, returns None.
+ """
+ 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
+
+ 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)
diff --git a/swarms/structs/conversation.py b/swarms/structs/conversation.py
new file mode 100644
index 00000000..c4bcdba5
--- /dev/null
+++ b/swarms/structs/conversation.py
@@ -0,0 +1,396 @@
+import datetime
+import json
+
+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):
+ """
+ A class structure to represent a conversation in a chatbot. This class is used to store the conversation history.
+ And, it can be used to save the conversation history to a file, load the conversation history from a file, and
+ display the conversation history. We can also use this class to add the conversation history to a database, query
+ the conversation history from a database, delete the conversation history from a database, update the conversation
+ history from a database, and get the conversation history from a database.
+
+
+ Args:
+ time_enabled (bool): Whether to enable timestamps for the conversation history. Default is False.
+ 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.
+ update(index: str, role, content): Update a message in the conversation history.
+ query(index: str): Query a message in the conversation history.
+ search(keyword: str): Search for a message in the conversation history.
+ display_conversation(detailed: bool = False): Display the conversation history.
+ export_conversation(filename: str): Export the conversation history to a file.
+ import_conversation(filename: str): Import a conversation history from a file.
+ count_messages_by_role(): Count the number of messages by role.
+ return_history_as_string(): Return the conversation history as a string.
+ save_as_json(filename: str): Save the conversation history as a JSON file.
+ load_from_json(filename: str): Load the conversation history from a JSON file.
+ search_keyword_in_conversation(keyword: str): Search for a keyword in the conversation history.
+ pretty_print_conversation(messages): Pretty print the conversation history.
+ add_to_database(): Add the conversation history to the database.
+ query_from_database(query): Query the conversation history from the database.
+ delete_from_database(): Delete the conversation history from the database.
+ update_from_database(): Update the conversation history from the database.
+ get_from_database(): Get the conversation history from the database.
+ execute_query_from_database(query): Execute a query on the database.
+ fetch_all_from_database(): Fetch all from the database.
+ fetch_one_from_database(): Fetch one from the database.
+
+ Examples:
+ >>> from swarms import Conversation
+ >>> conversation = Conversation()
+ >>> conversation.add("user", "Hello, how are you?")
+ >>> conversation.add("assistant", "I am doing well, thanks.")
+ >>> conversation.display_conversation()
+ user: Hello, how are you?
+ assistant: I am doing well, thanks.
+
+ """
+
+ def __init__(
+ self,
+ system_prompt: str,
+ time_enabled: bool = False,
+ database: AbstractDatabase = None,
+ autosave: bool = False,
+ save_filepath: str = None,
+ tokenizer: BaseTokenizer = None,
+ context_length: int = 8192,
+ *args,
+ **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 = []
+ 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
+
+ Args:
+ role (str): The role of the speaker
+ content (str): The content of the message
+
+ """
+ if self.time_enabled:
+ now = datetime.datetime.now()
+ timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
+ message = {
+ "role": role,
+ "content": content,
+ "timestamp": timestamp,
+ }
+ else:
+ message = {
+ "role": role,
+ "content": content,
+ }
+
+ self.conversation_history.append(message)
+
+ if self.autosave:
+ self.save_as_json(self.save_filepath)
+
+ def delete(self, index: str):
+ """Delete a message from the conversation history
+
+ Args:
+ index (str): index of the message to delete
+ """
+ self.conversation_history.pop(index)
+
+ def update(self, index: str, role, content):
+ """Update a message in the conversation history
+
+ Args:
+ index (str): index of the message to update
+ role (_type_): role of the speaker
+ content (_type_): content of the message
+ """
+ self.conversation_history[index] = {
+ "role": role,
+ "content": content,
+ }
+
+ def query(self, index: str):
+ """Query a message in the conversation history
+
+ Args:
+ index (str): index of the message to query
+
+ Returns:
+ str: the message
+ """
+ return self.conversation_history[index]
+
+ def search(self, keyword: str):
+ """Search for a message in the conversation history
+
+ Args:
+ keyword (str): Keyword to search for
+
+ Returns:
+ str: description
+ """
+ return [
+ msg
+ for msg in self.conversation_history
+ if keyword in msg["content"]
+ ]
+
+ def display_conversation(self, detailed: bool = False):
+ """Display the conversation history
+
+ Args:
+ detailed (bool, optional): detailed. Defaults to False.
+ """
+ role_to_color = {
+ "system": "red",
+ "user": "green",
+ "assistant": "blue",
+ "function": "magenta",
+ }
+ for message in self.conversation_history:
+ print(
+ colored(
+ f"{message['role']}: {message['content']}\n\n",
+ role_to_color[message["role"]],
+ )
+ )
+
+ def export_conversation(self, filename: str, *args, **kwargs):
+ """Export the conversation history to a file
+
+ Args:
+ filename (str): filename to export to
+ """
+ with open(filename, "w") as f:
+ for message in self.conversation_history:
+ f.write(f"{message['role']}: {message['content']}\n")
+
+ def import_conversation(self, filename: str):
+ """Import a conversation history from a file
+
+ Args:
+ filename (str): filename to import from
+ """
+ with open(filename, "r") as f:
+ for line in f:
+ role, content = line.split(": ", 1)
+ self.add(role, content.strip())
+
+ def count_messages_by_role(self):
+ """Count the number of messages by role"""
+ counts = {
+ "system": 0,
+ "user": 0,
+ "assistant": 0,
+ "function": 0,
+ }
+ for message in self.conversation_history:
+ counts[message["role"]] += 1
+ return counts
+
+ def return_history_as_string(self):
+ """Return the conversation history as a string
+
+ Returns:
+ str: the conversation history
+ """
+ return "\n".join(
+ [
+ f"{message['role']}: {message['content']}\n\n"
+ for message in self.conversation_history
+ ]
+ )
+
+ def save_as_json(self, filename: str = None):
+ """Save the conversation history as a JSON file
+
+ Args:
+ filename (str): Save the conversation history as a JSON file
+ """
+ # Create the directory if it does not exist
+ # os.makedirs(os.path.dirname(filename), exist_ok=True)
+ if filename is not None:
+ with open(filename, "w") as f:
+ json.dump(self.conversation_history, f)
+
+ def load_from_json(self, filename: str):
+ """Load the conversation history from a JSON file
+
+ Args:
+ filename (str): filename to load from
+ """
+ # Load the conversation history from a JSON file
+ if filename is not None:
+ with open(filename, "r") as f:
+ self.conversation_history = json.load(f)
+
+ def search_keyword_in_conversation(self, keyword: str):
+ """Search for a keyword in the conversation history
+
+ Args:
+ keyword (str): keyword to search for
+
+ Returns:
+ str: description
+ """
+ return [
+ msg
+ for msg in self.conversation_history
+ if keyword in msg["content"]
+ ]
+
+ def pretty_print_conversation(self, messages):
+ """Pretty print the conversation history
+
+ Args:
+ messages (str): messages to print
+ """
+ role_to_color = {
+ "system": "red",
+ "user": "green",
+ "assistant": "blue",
+ "tool": "magenta",
+ }
+
+ for message in messages:
+ if message["role"] == "system":
+ print(
+ colored(
+ f"system: {message['content']}\n",
+ role_to_color[message["role"]],
+ )
+ )
+ elif message["role"] == "user":
+ print(
+ colored(
+ f"user: {message['content']}\n",
+ role_to_color[message["role"]],
+ )
+ )
+ elif message["role"] == "assistant" and message.get(
+ "function_call"
+ ):
+ print(
+ colored(
+ f"assistant: {message['function_call']}\n",
+ role_to_color[message["role"]],
+ )
+ )
+ elif message["role"] == "assistant" and not message.get(
+ "function_call"
+ ):
+ print(
+ colored(
+ f"assistant: {message['content']}\n",
+ role_to_color[message["role"]],
+ )
+ )
+ elif message["role"] == "tool":
+ print(
+ colored(
+ (
+ f"function ({message['name']}):"
+ f" {message['content']}\n"
+ ),
+ role_to_color[message["role"]],
+ )
+ )
+
+ def add_to_database(self, *args, **kwargs):
+ """Add the conversation history to the database"""
+ self.database.add("conversation", self.conversation_history)
+
+ def query_from_database(self, query, *args, **kwargs):
+ """Query the conversation history from the database"""
+ return self.database.query("conversation", query)
+
+ def delete_from_database(self, *args, **kwargs):
+ """Delete the conversation history from the database"""
+ self.database.delete("conversation")
+
+ def update_from_database(self, *args, **kwargs):
+ """Update the conversation history from the database"""
+ self.database.update(
+ "conversation", self.conversation_history
+ )
+
+ def get_from_database(self, *args, **kwargs):
+ """Get the conversation history from the database"""
+ return self.database.get("conversation")
+
+ def execute_query_from_database(self, query, *args, **kwargs):
+ """Execute a query on the database"""
+ return self.database.execute_query(query)
+
+ def fetch_all_from_database(self, *args, **kwargs):
+ """Fetch all from the database"""
+ return self.database.fetch_all()
+
+ 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/structs/graph_workflow.py b/swarms/structs/graph_workflow.py
new file mode 100644
index 00000000..c4bcea7e
--- /dev/null
+++ b/swarms/structs/graph_workflow.py
@@ -0,0 +1,180 @@
+from swarms.structs.base import BaseStructure
+
+
+import logging
+
+
+class GraphWorkflow(BaseStructure):
+ """
+ Represents a graph-based workflow structure.
+
+ Attributes:
+ graph (dict): A dictionary representing the nodes and edges of the graph.
+ entry_point (str): The name of the entry point node in the graph.
+
+ Methods:
+ add(node, node_value): Adds a node to the graph with the specified value.
+ start(node_name): Sets the starting node for the workflow.
+ connect(from_node, to_node): Connects two nodes in the graph.
+ set_entry_point(node_name): Sets the entry point node for the workflow.
+ add_edge(from_node, to_node): Adds an edge between two nodes in the graph.
+ add_conditional_edges(from_node, condition, edge_dict): Adds conditional edges from a node to multiple nodes based on a condition.
+ run(): Runs the workflow and returns the graph.
+
+ Examples:
+ >>> from swarms.structs import GraphWorkflow
+ >>> graph = GraphWorkflow()
+ >>> graph.add("start", "Start")
+ >>> graph.add("end", "End")
+ >>> graph.start("start")
+ """
+
+ def __init__(self):
+ self.graph = {}
+ 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:
+ self.graph[from_node]["edges"][
+ to_node
+ ] = condition
+ else:
+ raise ValueError("Node does not exist in graph")
+ else:
+ raise ValueError(
+ f"Node {from_node} does not exist in graph"
+ )
+
+ 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/swarms/groupchat.py b/swarms/structs/groupchat.py
similarity index 98%
rename from swarms/swarms/groupchat.py
rename to swarms/structs/groupchat.py
index f3677023..21fff944 100644
--- a/swarms/swarms/groupchat.py
+++ b/swarms/structs/groupchat.py
@@ -140,7 +140,6 @@ class GroupChatManager:
>>> from swarms import GroupChatManager
>>> from swarms.structs.agent import Agent
>>> agents = Agent()
- >>> output = GroupChatManager(agents, lambda x: x)
"""
diff --git a/swarms/structs/load_balancer.py b/swarms/structs/load_balancer.py
new file mode 100644
index 00000000..f0038335
--- /dev/null
+++ b/swarms/structs/load_balancer.py
@@ -0,0 +1,69 @@
+from typing import Optional, List
+import multiprocessing as mp
+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,
+ agents: Optional[List] = None,
+ *args,
+ **kwargs,
+ ):
+ super().__init__(*args, **kwargs)
+ self.num_workers = num_workers
+ self.agents = agents
+ self.tasks = []
+ self.results = []
+ self.workers = []
+ self._init_workers()
+
+ def _init_workers(self):
+ for i in range(self.num_workers):
+ worker = mp.Process(target=self._worker)
+ worker.start()
+ self.workers.append(worker)
+
+ def _worker(self):
+ while True:
+ task = self._get_task()
+ if task is None:
+ break
+ result = self._run_task(task)
+ self._add_result(result)
+
+ def _get_task(self):
+ if len(self.tasks) == 0:
+ return None
+ return self.tasks.pop(0)
+
+ def _run_task(self, task):
+ return task()
+
+ def _add_result(self, result):
+ self.results.append(result)
+
+ def add_task(self, task):
+ self.tasks.append(task)
diff --git a/swarms/structs/majority_voting.py b/swarms/structs/majority_voting.py
new file mode 100644
index 00000000..c911b9f0
--- /dev/null
+++ b/swarms/structs/majority_voting.py
@@ -0,0 +1,97 @@
+import re
+from collections import Counter
+
+
+def extract_last_python_code_block(text):
+ """
+ Extracts the last Python code block from the given text.
+
+ Args:
+ text (str): The text to search for Python code blocks.
+
+ Returns:
+ str or None: The last Python code block found in the text, or None if no code block is found.
+ """
+ # The regular expression pattern for Python code blocks
+ pattern = r"```[pP]ython(.*?)```"
+
+ # Find all matches in the text
+ matches = re.findall(pattern, text, re.DOTALL)
+
+ # If there are matches, return the last one
+ if matches:
+ return matches[-1].strip()
+ else:
+ return None
+
+
+def parse_code_completion(agent_response, question):
+ """
+ Parses the code completion response from the agent and extracts the last Python code block.
+
+ Args:
+ agent_response (str): The response from the agent.
+ question (str): The original question.
+
+ Returns:
+ tuple: A tuple containing the parsed Python code and a boolean indicating success.
+ """
+ python_code = extract_last_python_code_block(agent_response)
+ if python_code is None:
+ if agent_response.count("impl]") == 0:
+ python_code = agent_response
+ else:
+ python_code_lines = agent_response.split("\n")
+ python_code = ""
+ in_func = False
+ for line in python_code_lines:
+ if in_func:
+ python_code += line + "\n"
+ if "impl]" in line:
+ in_func = True
+ if python_code.count("def") == 0:
+ python_code = question + python_code
+ return python_code, True
+
+
+def most_frequent(
+ clist: list,
+ cmp_func: callable = None,
+):
+ """
+ Finds the most frequent element in a list based on a comparison function.
+
+ Args:
+ clist (list): The list of elements to search.
+ cmp_func (function, optional): The comparison function used to determine the frequency of elements.
+ If not provided, the default comparison function is used.
+
+ Returns:
+ tuple: A tuple containing the most frequent element and its frequency.
+ """
+ counter = 0
+ num = clist[0]
+
+ for i in clist:
+ current_frequency = sum(cmp_func(i, item) for item in clist)
+ print(current_frequency)
+ if current_frequency > counter:
+ counter = current_frequency
+ num = i
+
+ return num, counter
+
+
+def majority_voting(answers: list):
+ """
+ Performs majority voting on a list of answers and returns the most common answer.
+
+ Args:
+ answers (list): A list of answers.
+
+ Returns:
+ The most common answer in the list.
+ """
+ counter = Counter(answers)
+ answer = counter.most_common(1)[0][0]
+ return answer
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/model_parallizer.py b/swarms/structs/model_parallizer.py
new file mode 100644
index 00000000..828d4ef4
--- /dev/null
+++ b/swarms/structs/model_parallizer.py
@@ -0,0 +1,182 @@
+import asyncio
+import logging
+from concurrent.futures import ThreadPoolExecutor, as_completed
+from typing import Callable, List
+
+from termcolor import colored
+
+# Configure logging
+logging.basicConfig(level=logging.INFO)
+logger = logging.getLogger(__name__)
+
+
+class ModelParallelizer:
+ """
+ ModelParallelizer, a class that parallelizes the execution of a task
+ across multiple language models. It is a wrapper around the
+ LanguageModel class.
+
+ Args:
+ llms (List[Callable]): A list of language models.
+ retry_attempts (int): The number of retry attempts.
+ iters (int): The number of iterations to run the task.
+
+ Attributes:
+ llms (List[Callable]): A list of language models.
+ retry_attempts (int): The number of retry attempts.
+ iters (int): The number of iterations to run the task.
+ last_responses (List[str]): The last responses from the language
+ models.
+ task_history (List[str]): The task history.
+
+ Examples:
+ >>> from swarms.structs import ModelParallelizer
+ >>> from swarms.llms import OpenAIChat
+ >>> llms = [
+ ... OpenAIChat(
+ ... temperature=0.5,
+ ... openai_api_key="OPENAI_API_KEY",
+ ... ),
+ ... OpenAIChat(
+ ... temperature=0.5,
+ ... openai_api_key="OPENAI_API_KEY",
+ ... ),
+ ... ]
+ >>> mp = ModelParallelizer(llms)
+ >>> mp.run("Generate a 10,000 word blog on health and wellness.")
+ ['Generate a 10,000 word blog on health and wellness.', 'Generate a 10,000 word blog on health and wellness.']
+
+ """
+
+ def __init__(
+ self,
+ llms: List[Callable] = None,
+ retry_attempts: int = 3,
+ iters: int = None,
+ *args,
+ **kwargs,
+ ):
+ self.llms = llms
+ self.retry_attempts = retry_attempts
+ self.iters = iters
+ self.last_responses = None
+ self.task_history = []
+
+ def run(self, task: str):
+ """Run the task string"""
+ try:
+ for i in range(self.iters):
+ with ThreadPoolExecutor() as executor:
+ responses = executor.map(
+ lambda llm: llm(task), self.llms
+ )
+ return list(responses)
+ except Exception as error:
+ logger.error(
+ f"[ERROR][ModelParallelizer] [ROOT CAUSE] [{error}]"
+ )
+
+ def run_all(self, task):
+ """Run the task on all LLMs"""
+ responses = []
+ for llm in self.llms:
+ responses.append(llm(task))
+ return responses
+
+ # New Features
+ def save_responses_to_file(self, filename):
+ """Save responses to file"""
+ with open(filename, "w") as file:
+ table = [
+ [f"LLM {i+1}", response]
+ for i, response in enumerate(self.last_responses)
+ ]
+ file.write(table)
+
+ @classmethod
+ def load_llms_from_file(cls, filename):
+ """Load llms from file"""
+ with open(filename, "r") as file:
+ llms = [line.strip() for line in file.readlines()]
+ return cls(llms)
+
+ def get_task_history(self):
+ """Get Task history"""
+ return self.task_history
+
+ def summary(self):
+ """Summary"""
+ print("Tasks History:")
+ for i, task in enumerate(self.task_history):
+ print(f"{i + 1}. {task}")
+ print("\nLast Responses:")
+ table = [
+ [f"LLM {i+1}", response]
+ for i, response in enumerate(self.last_responses)
+ ]
+ print(
+ colored(
+ table,
+ "cyan",
+ )
+ )
+
+ async def arun(self, task: str):
+ """Asynchronous run the task string"""
+ loop = asyncio.get_event_loop()
+ futures = [
+ loop.run_in_executor(None, lambda llm: llm(task), llm)
+ for llm in self.llms
+ ]
+ for response in await asyncio.gather(*futures):
+ print(response)
+
+ def concurrent_run(self, task: str) -> List[str]:
+ """Synchronously run the task on all llms and collect responses"""
+ try:
+ with ThreadPoolExecutor() as executor:
+ future_to_llm = {
+ executor.submit(llm, task): llm
+ for llm in self.llms
+ }
+ responses = []
+ for future in as_completed(future_to_llm):
+ try:
+ responses.append(future.result())
+ except Exception as error:
+ print(
+ f"{future_to_llm[future]} generated an"
+ f" exception: {error}"
+ )
+ self.last_responses = responses
+ self.task_history.append(task)
+ return responses
+ except Exception as error:
+ 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}")
+
+ 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}")
+
+ try:
+ self.llms.remove(llm)
+ except Exception as error:
+ logger.error(
+ f"[ERROR][ModelParallelizer] [ROOT CAUSE] [{error}]"
+ )
+ raise error
diff --git a/swarms/swarms/multi_agent_collab.py b/swarms/structs/multi_agent_collab.py
similarity index 100%
rename from swarms/swarms/multi_agent_collab.py
rename to swarms/structs/multi_agent_collab.py
diff --git a/swarms/structs/multi_agent_rag.py b/swarms/structs/multi_agent_rag.py
new file mode 100644
index 00000000..91d8c39d
--- /dev/null
+++ b/swarms/structs/multi_agent_rag.py
@@ -0,0 +1,85 @@
+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)
diff --git a/swarms/structs/nonlinear_workflow.py b/swarms/structs/nonlinear_workflow.py
new file mode 100644
index 00000000..0fc1d200
--- /dev/null
+++ b/swarms/structs/nonlinear_workflow.py
@@ -0,0 +1,90 @@
+from swarms.structs.task import Task
+from swarms.structs.base import BaseStructure
+from swarms.utils.logger import logger # noqa: F401
+
+
+class NonlinearWorkflow(BaseStructure):
+ """
+ Represents a Directed Acyclic Graph (DAG) workflow.
+
+ Attributes:
+ tasks (dict): A dictionary mapping task names to Task objects.
+ edges (dict): A dictionary mapping task names to a list of dependencies.
+
+ Methods:
+ add(task: Task, *dependencies: str): Adds a task to the workflow with its dependencies.
+ run(): Executes the workflow by running tasks in topological order.
+
+ Examples:
+ >>> from swarms.models import OpenAIChat
+ >>> from swarms.structs import NonlinearWorkflow, Task
+ >>> llm = OpenAIChat(openai_api_key="")
+ >>> task = Task(llm, "What's the weather in miami")
+ >>> workflow = NonlinearWorkflow()
+ >>> workflow.add(task)
+ >>> workflow.run()
+
+ """
+
+ def __init__(self, stopping_token: str = ""):
+ self.tasks = {}
+ self.edges = {}
+ self.stopping_token = stopping_token
+
+ def add(self, task: Task, *dependencies: str):
+ """
+ Adds a task to the workflow with its dependencies.
+
+ Args:
+ task (Task): The task to be added.
+ dependencies (str): Variable number of dependency task names.
+
+ Raises:
+ AssertionError: If the task is None.
+
+ Returns:
+ None
+ """
+ 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):
+ """
+ Executes the workflow by running tasks in topological order.
+
+ Raises:
+ Exception: If a circular dependency is detected.
+
+ Returns:
+ None
+ """
+ try:
+ # Create a copy of the edges
+ edges = self.edges.copy()
+
+ while edges:
+ # Get all tasks with no dependencies
+ ready_tasks = [
+ task for task, deps in edges.items() if not deps
+ ]
+
+ if not ready_tasks:
+ raise Exception("Circular dependency detected")
+
+ # Run all ready tasks
+ for task in ready_tasks:
+ result = self.tasks[task].execute()
+ if result == self.stopping_token:
+ return
+ del edges[task]
+
+ # Remove dependencies on the ready tasks
+ for deps in edges.values():
+ for task in ready_tasks:
+ if task in deps:
+ deps.remove(task)
+ except Exception as error:
+ logger.error(f"[ERROR][NonlinearWorkflow] {error}")
+ raise error
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/recursive_workflow.py b/swarms/structs/recursive_workflow.py
new file mode 100644
index 00000000..afeb91b7
--- /dev/null
+++ b/swarms/structs/recursive_workflow.py
@@ -0,0 +1,91 @@
+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):
+ """
+ RecursiveWorkflow class for running a task recursively until a stopping condition is met.
+
+ Args:
+ task (Task): The task to execute.
+ stop_token (Any): The token that indicates when to stop the workflow.
+
+ Attributes:
+ task (Task): The task to execute.
+ stop_token (Any): The token that indicates when to stop the workflow.
+
+ Examples:
+ >>> from swarms.models import OpenAIChat
+ >>> from swarms.structs import RecursiveWorkflow, Task
+ >>> llm = OpenAIChat(openai_api_key="")
+ >>> task = Task(llm, "What's the weather in miami")
+ >>> workflow = RecursiveWorkflow()
+ >>> workflow.add(task)
+ >>> workflow.run()
+ """
+
+ def __init__(
+ self,
+ stop_token: str = "",
+ stopping_conditions: callable = None,
+ ):
+ self.stop_token = stop_token
+ self.task_pool = []
+
+ assert (
+ self.stop_token is not None
+ ), "stop_token cannot be None"
+
+ def add(self, task: Task = None, tasks: List[Task] = None):
+ """Adds a task to the workflow.
+
+ Args:
+ task (Task): _description_
+ tasks (List[Task]): _description_
+ """
+ try:
+ if tasks:
+ for task in tasks:
+ 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"
+ " workflow"
+ )
+ except Exception as error:
+ logger.warning(f"[ERROR][RecursiveWorkflow] {error}")
+ raise error
+
+ def run(self):
+ """
+ Executes the tasks in the workflow until the stop token is encountered.
+
+ Returns:
+ None
+ """
+ try:
+ for task in self.task_pool:
+ while True:
+ result = task.run()
+ if (
+ result is not None
+ and self.stop_token in result
+ ):
+ break
+ print(f"{result}")
+ except Exception as error:
+ logger.warning(f"[ERROR][RecursiveWorkflow] {error}")
+ raise error
diff --git a/swarms/memory/schemas.py b/swarms/structs/schemas.py
similarity index 93%
rename from swarms/memory/schemas.py
rename to swarms/structs/schemas.py
index 9147a909..f7f5441e 100644
--- a/swarms/memory/schemas.py
+++ b/swarms/structs/schemas.py
@@ -17,6 +17,15 @@ class TaskInput(BaseModel):
class Artifact(BaseModel):
+ """
+ Represents an artifact.
+
+ Attributes:
+ artifact_id (str): Id of the artifact.
+ file_name (str): Filename of the artifact.
+ relative_path (str, optional): Relative path of the artifact in the agent's workspace.
+ """
+
artifact_id: str = Field(
...,
description="Id of the artifact",
diff --git a/swarms/structs/sequential_workflow.py b/swarms/structs/sequential_workflow.py
index 41192a6f..6b1d7c06 100644
--- a/swarms/structs/sequential_workflow.py
+++ b/swarms/structs/sequential_workflow.py
@@ -1,19 +1,18 @@
-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
# SequentialWorkflow class definition using dataclasses
@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.
@@ -21,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.
@@ -34,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] = (
@@ -51,8 +50,8 @@ class SequentialWorkflow:
def add(
self,
- agent: Union[Callable, Agent],
- task: Optional[str] = None,
+ task: Optional[Task] = None,
+ tasks: Optional[List[Task]] = None,
*args,
**kwargs,
) -> None:
@@ -67,23 +66,30 @@ 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,
- )
- )
except Exception as error:
- print(
+ logger.error(
colored(
f"Error adding task to workflow: {error}", "red"
),
@@ -92,10 +98,14 @@ 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:
- print(
+ logger.error(
colored(f"Error resetting workflow: {error}", "red"),
)
@@ -108,144 +118,32 @@ 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:
- print(
+ logger.error(
colored(
f"Error getting task results: {error}", "red"
),
)
- 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
- ]
- except Exception as error:
- print(
- colored(
- f"Error removing task from workflow: {error}",
- "red",
- ),
+ self.task_pool.remove(task)
+ logger.info(
+ f"[INFO][SequentialWorkflow] Removed task {task} from"
+ " workflow"
)
-
- 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."
- )
except Exception as error:
- print(
+ 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."
- )
- except Exception as error:
- print(
- colored(
- f"Error deleting task from workflow: {error}",
+ f"Error removing 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",
@@ -272,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,
@@ -280,13 +178,18 @@ 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)
+
+ logger.info(
+ "[INFO][SequentialWorkflow] Saved workflow state to"
+ f" {filepath}"
+ )
except Exception as error:
- print(
+ logger.error(
colored(
f"Error saving workflow state: {error}",
"red",
@@ -331,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}
@@ -356,33 +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)
- except Exception as error:
- print(
- colored(
- f"Error adding objective to workflow: {error}",
- "red",
- )
- )
-
def load_workflow_state(
self, filepath: str = None, **kwargs
) -> None:
@@ -409,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"],
@@ -419,9 +295,14 @@ class SequentialWorkflow:
result=task_state["result"],
history=task_state["history"],
)
- self.tasks.append(task)
- except Exception as error:
+ self.task_pool.append(task)
+
print(
+ "[INFO][SequentialWorkflow] Loaded workflow state"
+ f" from {filepath}"
+ )
+ except Exception as error:
+ logger.error(
colored(
f"Error loading workflow state: {error}",
"red",
@@ -438,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:
- print(
- 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.
+ # 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
- 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 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/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/swarm_net.py b/swarms/structs/swarm_net.py
new file mode 100644
index 00000000..80f339f6
--- /dev/null
+++ b/swarms/structs/swarm_net.py
@@ -0,0 +1,334 @@
+import asyncio
+import logging
+import queue
+import threading
+from typing import List, Optional
+
+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):
+ """
+ SwarmNetwork class
+
+ The SwarmNetwork class is responsible for managing the agents pool
+ and the task queue. It also monitors the health of the agents and
+ scales the pool up or down based on the number of pending tasks
+ and the current load of the agents.
+
+ For example, if the number of pending tasks is greater than the
+ number of agents in the pool, the SwarmNetwork will scale up the
+ pool by adding new agents. If the number of pending tasks is less
+ than the number of agents in the pool, the SwarmNetwork will scale
+ down the pool by removing agents.
+
+ The SwarmNetwork class also provides a simple API for interacting
+ with the agents pool. The API is implemented using the Flask
+ framework and is enabled by default. The API can be disabled by
+ setting the `api_enabled` parameter to False.
+
+ Features:
+ - Agent pool management
+ - Task queue management
+ - Agent health monitoring
+ - Agent pool scaling
+ - Simple API for interacting with the agent pool
+ - Simple API for interacting with the task queue
+ - Simple API for interacting with the agent health monitor
+ - Simple API for interacting with the agent pool scaler
+ - Create APIs for each agent in the pool (optional)
+ - Run each agent on it's own thread
+ - Run each agent on it's own process
+ - Run each agent on it's own container
+ - Run each agent on it's own machine
+ - Run each agent on it's own cluster
+
+
+ Attributes:
+ task_queue (queue.Queue): A queue for storing tasks.
+ idle_threshold (float): The idle threshold for the agents.
+ busy_threshold (float): The busy threshold for the agents.
+ agents (List[Agent]): A list of agents in the pool.
+ api_enabled (bool): A flag to enable/disable the API.
+ logging_enabled (bool): A flag to enable/disable logging.
+
+ Example:
+ >>> from swarms.structs.agent import Agent
+ >>> from swarms.structs.swarm_net import SwarmNetwork
+ >>> agent = Agent()
+ >>> swarm = SwarmNetwork(agents=[agent])
+ >>> swarm.add_task("task")
+ >>> swarm.run()
+
+ """
+
+ def __init__(
+ self,
+ agents: List[Agent],
+ idle_threshold: float = 0.2,
+ busy_threshold: float = 0.7,
+ api_enabled: Optional[bool] = False,
+ logging_enabled: Optional[bool] = False,
+ *args,
+ **kwargs,
+ ):
+ super().__init__()
+ self.agents = agents
+ self.task_queue = queue.Queue()
+ self.idle_threshold = idle_threshold
+ self.busy_threshold = busy_threshold
+ self.lock = threading.Lock()
+ self.api_enabled = api_enabled
+ self.logging_enabled = logging_enabled
+
+ logging.basicConfig(level=logging.INFO)
+ self.logger = logging.getLogger(__name__)
+
+ if api_enabled:
+ self.api = FastAPI()
+
+ def add_task(self, task):
+ """Add task to the task queue
+
+ Args:
+ task (_type_): _description_
+
+ Example:
+ >>> from swarms.structs.agent import Agent
+ >>> from swarms.structs.swarm_net import SwarmNetwork
+ >>> agent = Agent()
+ >>> swarm = SwarmNetwork(agents=[agent])
+ >>> swarm.add_task("task")
+ """
+ self.logger.info(f"Adding task {task} to queue")
+ try:
+ self.task_queue.put(task)
+ self.logger.info(f"Task {task} added to queue")
+ except Exception as error:
+ print(
+ f"Error adding task to queue: {error} try again with"
+ " a new task"
+ )
+ raise error
+
+ async def async_add_task(self, task):
+ """Add task to the task queue
+
+ Args:
+ task (_type_): _description_
+
+ Example:
+ >>> from swarms.structs.agent import Agent
+ >>> from swarms.structs.swarm_net import SwarmNetwork
+ >>> agent = Agent()
+ >>> swarm = SwarmNetwork(agents=[agent])
+ >>> swarm.add_task("task")
+
+ """
+ self.logger.info(
+ f"Adding task {task} to queue asynchronously"
+ )
+ try:
+ # Add task to queue asynchronously with asyncio
+ loop = asyncio.get_running_loop()
+ await loop.run_in_executor(
+ None, self.task_queue.put, task
+ )
+ self.logger.info(f"Task {task} added to queue")
+ except Exception as error:
+ print(
+ f"Error adding task to queue: {error} try again with"
+ " a new task"
+ )
+ raise error
+
+ def run_single_agent(
+ self, agent_id, task: Optional[str], *args, **kwargs
+ ):
+ """Run agent the task on the agent id
+
+ Args:
+ agent_id (_type_): _description_
+ task (str, optional): _description_. Defaults to None.
+
+ Raises:
+ ValueError: _description_
+
+ Returns:
+ _type_: _description_
+ """
+ self.logger.info(f"Running task {task} on agent {agent_id}")
+ try:
+ for agent in self.agents:
+ if agent.id == agent_id:
+ return agent.run(task, *args, **kwargs)
+ # 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:
+ self.logger.error(f"Error running task on agent: {error}")
+ raise error
+
+ def run_many_agents(
+ self, task: Optional[str] = None, *args, **kwargs
+ ) -> List:
+ """Run the task on all agents
+
+ Args:
+ task (str, optional): _description_. Defaults to None.
+
+ Returns:
+ List: _description_
+ """
+ self.logger.info(f"Running task {task} on all agents")
+ try:
+ return [
+ agent.run(task, *args, **kwargs)
+ for agent in self.agents
+ ]
+ except Exception as error:
+ logger.error(f"Error running task on agents: {error}")
+ raise error
+
+ def list_agents(self):
+ """List all agents
+
+ Returns:
+ List: _description_
+ """
+ self.logger.info("[Listing all active agents]")
+ num_agents = len(self.agents)
+
+ self.logger.info(f"[Number of active agents: {num_agents}]")
+
+ try:
+ for agent in self.agents:
+ return self.logger.info(
+ f"[Agent] [ID: {agent.id}] [Name:"
+ f" {agent.agent_name}] [Description:"
+ f" {agent.agent_description}] [Status] [Running]"
+ )
+ except Exception as error:
+ logger.error(f"Error listing agents: {error}")
+ raise error
+
+ def get_agent(self, agent_id):
+ """Get agent by id
+
+ Args:
+ agent_id (_type_): _description_
+
+ Returns:
+ _type_: _description_
+ """
+ self.logger.info(f"Getting agent {agent_id}")
+
+ try:
+ for agent in self.agents:
+ if agent.id == agent_id:
+ return agent
+ raise ValueError(f"No agent found with ID {agent_id}")
+ except Exception as error:
+ self.logger.error(f"Error getting agent: {error}")
+ raise error
+
+ def add_agent(self, agent):
+ """Add agent to the agent pool
+
+ Args:
+ agent (_type_): _description_
+ """
+ self.logger.info(f"Adding agent {agent} to pool")
+ try:
+ self.agents.append(agent)
+ except Exception as error:
+ print(f"Error adding agent to pool: {error}")
+ raise error
+
+ def remove_agent(self, agent_id):
+ """Remove agent from the agent pool
+
+ Args:
+ agent_id (_type_): _description_
+ """
+ self.logger.info(f"Removing agent {agent_id} from pool")
+ try:
+ for agent in self.agents:
+ if agent.id == agent_id:
+ self.agents.remove(agent)
+ return
+ raise ValueError(f"No agent found with ID {agent_id}")
+ except Exception as error:
+ print(f"Error removing agent from pool: {error}")
+ raise error
+
+ async def async_remove_agent(self, agent_id):
+ """Remove agent from the agent pool
+
+ Args:
+ agent_id (_type_): _description_
+ """
+ self.logger.info(f"Removing agent {agent_id} from pool")
+ try:
+ # Remove agent from pool asynchronously with asyncio
+ loop = asyncio.get_running_loop()
+ await loop.run_in_executor(
+ None, self.remove_agent, agent_id
+ )
+ except Exception as error:
+ print(f"Error removing agent from pool: {error}")
+ raise error
+
+ def scale_up(self, num_agents: int = 1):
+ """Scale up the agent pool
+
+ Args:
+ num_agents (int, optional): _description_. Defaults to 1.
+ """
+ self.logger.info(f"Scaling up agent pool by {num_agents}")
+ try:
+ for _ in range(num_agents):
+ self.agents.append(Agent())
+ except Exception as error:
+ print(f"Error scaling up agent pool: {error}")
+ raise error
+
+ def scale_down(self, num_agents: int = 1):
+ """Scale down the agent pool
+
+ Args:
+ num_agents (int, optional): _description_. Defaults to 1.
+ """
+ for _ in range(num_agents):
+ self.agents.pop()
+
+ # - Create APIs for each agent in the pool (optional) with fastapi
+ def create_apis_for_agents(self):
+ """Create APIs for each agent in the pool (optional) with fastapi
+
+ Returns:
+ _type_: _description_
+ """
+ self.apis = []
+ for agent in self.agents:
+ self.api.get(f"/{agent.id}")
+
+ def run_agent(task: str, *args, **kwargs):
+ return agent.run(task, *args, **kwargs)
+
+ self.apis.append(self.api)
+
+ def run(self):
+ """run the swarm network"""
+ # Observe all agents in the pool
+ self.logger.info("Starting the SwarmNetwork")
+
+ for agent in self.agents:
+ self.logger.info(f"Starting agent {agent.id}")
+ self.logger.info(
+ f"[Agent][{agent.id}] [Status] [Running] [Awaiting"
+ " Task]"
+ )
diff --git a/swarms/structs/swarming_architectures.py b/swarms/structs/swarming_architectures.py
new file mode 100644
index 00000000..ad3ad4ed
--- /dev/null
+++ b/swarms/structs/swarming_architectures.py
@@ -0,0 +1,255 @@
+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]):
+ ball = 0
+ while tasks:
+ task = tasks.pop(0)
+ agents[ball].run(task)
+ ball = (ball + 1) % len(agents)
+
+
+def linear_swarm(agents: List[Agent], tasks: List[str]):
+ for i in range(len(agents)):
+ if tasks:
+ task = tasks.pop(0)
+ agents[i].run(task)
+
+
+def star_swarm(agents: List[Agent], tasks: List[str]):
+ center_agent = agents[0]
+ for task in tasks:
+ center_agent.run(task)
+ for agent in agents[1:]:
+ agent.run(task)
+
+
+def mesh_swarm(agents: List[Agent], tasks: List[str]):
+ task_queue = tasks.copy()
+ while task_queue:
+ for agent in agents:
+ if task_queue:
+ task = task_queue.pop(0)
+ agent.run(task)
+
+
+def grid_swarm(agents: List[Agent], tasks: List[str]):
+ grid_size = int(
+ len(agents) ** 0.5
+ ) # Assuming agents can form a perfect square grid
+ for i in range(grid_size):
+ for j in range(grid_size):
+ if tasks:
+ task = tasks.pop(0)
+ agents[i * grid_size + j].run(task)
+
+
+def pyramid_swarm(agents: List[Agent], tasks: List[str]):
+ levels = int(
+ (-1 + (1 + 8 * len(agents)) ** 0.5) / 2
+ ) # Assuming agents can form a perfect pyramid
+ for i in range(levels):
+ for j in range(i + 1):
+ if tasks:
+ task = tasks.pop(0)
+ agents[int(i * (i + 1) / 2 + j)].run(task)
+
+
+def fibonacci_swarm(agents: List[Agent], tasks: List[str]):
+ fib = [1, 1]
+ while len(fib) < len(agents):
+ fib.append(fib[-1] + fib[-2])
+ for i in range(len(fib)):
+ for j in range(fib[i]):
+ if tasks:
+ task = tasks.pop(0)
+ agents[int(sum(fib[:i]) + j)].run(task)
+
+
+def prime_swarm(agents: List[Agent], tasks: List[str]):
+ primes = [
+ 2,
+ 3,
+ 5,
+ 7,
+ 11,
+ 13,
+ 17,
+ 19,
+ 23,
+ 29,
+ 31,
+ 37,
+ 41,
+ 43,
+ 47,
+ 53,
+ 59,
+ 61,
+ 67,
+ 71,
+ 73,
+ 79,
+ 83,
+ 89,
+ 97,
+ ] # First 25 prime numbers
+ for prime in primes:
+ if prime < len(agents) and tasks:
+ task = tasks.pop(0)
+ agents[prime].run(task)
+
+
+def power_swarm(agents: List[str], tasks: List[str]):
+ powers = [2**i for i in range(int(len(agents) ** 0.5))]
+ for power in powers:
+ if power < len(agents) and tasks:
+ task = tasks.pop(0)
+ agents[power].run(task)
+
+
+def log_swarm(agents: List[Agent], tasks: List[str]):
+ for i in range(len(agents)):
+ if 2**i < len(agents) and tasks:
+ task = tasks.pop(0)
+ agents[2**i].run(task)
+
+
+def exponential_swarm(agents: List[Agent], tasks: List[str]):
+ for i in range(len(agents)):
+ index = min(int(2**i), len(agents) - 1)
+ if tasks:
+ task = tasks.pop(0)
+ agents[index].run(task)
+
+
+def geometric_swarm(agents, tasks):
+ ratio = 2
+ for i in range(range(len(agents))):
+ index = min(int(ratio**2), len(agents) - 1)
+ if tasks:
+ task = tasks.pop(0)
+ agents[index].run(task)
+
+
+def harmonic_swarm(agents: List[Agent], tasks: List[str]):
+ for i in range(1, len(agents) + 1):
+ index = min(int(len(agents) / i), len(agents) - 1)
+ if tasks:
+ task = tasks.pop(0)
+ agents[index].run(task)
+
+
+def staircase_swarm(agents: List[Agent], task: str):
+ step = len(agents) // 5
+ for i in range(len(agents)):
+ index = (i // step) * step
+ agents[index].run(task)
+
+
+def sigmoid_swarm(agents: List[Agent], task: str):
+ for i in range(len(agents)):
+ index = int(len(agents) / (1 + math.exp(-i)))
+ agents[index].run(task)
+
+
+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
diff --git a/swarms/structs/task.py b/swarms/structs/task.py
index 81351b4f..fb89b7bf 100644
--- a/swarms/structs/task.py
+++ b/swarms/structs/task.py
@@ -1,4 +1,7 @@
+import sched
+import time
from dataclasses import dataclass, field
+from datetime import datetime
from typing import (
Any,
Callable,
@@ -8,25 +11,40 @@ from typing import (
)
from swarms.structs.agent import Agent
+from swarms.utils.logger import logger
+from swarms.structs.conversation import Conversation
-# Define a generic Task that can handle different types of callable objects
@dataclass
class Task:
"""
Task class for running a task in a sequential workflow.
-
- Args:
- description (str): The description of the task.
- agent (Union[Callable, Agent]): The model or agent to execute the task.
- args (List[Any]): Additional arguments to pass to the task execution.
- kwargs (Dict[str, Any]): Additional keyword arguments to pass to the task execution.
- result (Any): The result of the task execution.
- history (List[Any]): The history of the task execution.
+ Attributes:
+ description (str): Description of the task.
+ agent (Union[Callable, Agent]): Agent or callable object to run the task.
+ args (List[Any]): Arguments to pass to the agent or callable object.
+ kwargs (Dict[str, Any]): Keyword arguments to pass to the agent or callable object.
+ result (Any): Result of the task.
+ history (List[Any]): History of the task.
+ schedule_time (datetime): Time to schedule the task.
+ scheduler (sched.scheduler): Scheduler to schedule the task.
+ trigger (Callable): Trigger to run the task.
+ action (Callable): Action to run the task.
+ condition (Callable): Condition to run the task.
+ priority (int): Priority of the task.
+ dependencies (List[Task]): List of tasks that need to be completed before this task can be executed.
Methods:
- execute: Execute the task.
+ execute: Execute the task by calling the agent or model with the arguments and keyword arguments.
+ handle_scheduled_task: Handles the execution of a scheduled task.
+ set_trigger: Sets the trigger for the task.
+ set_action: Sets the action for the task.
+ set_condition: Sets the condition for the task.
+ is_completed: Checks whether the task has been completed.
+ add_dependency: Adds a task to the list of dependencies.
+ set_priority: Sets the priority of the task.
+ check_dependency_completion: Checks whether all the dependencies have been completed.
Examples:
@@ -39,40 +57,204 @@ class Task:
"""
- description: str
agent: Union[Callable, Agent]
- 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)
- # logger = logging.getLogger(__name__)
+ schedule_time: datetime = None
+ scheduler = sched.scheduler(time.time, time.sleep)
+ trigger: Callable = None
+ action: Callable = None
+ 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):
+ def execute(self, *args, **kwargs):
"""
- Execute the task.
+ 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
+ path to the image as a keyword argument.
+
+
+ Examples:
+ >>> 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.result
- Raises:
- ValueError: If a Agent instance is used as a task and the 'task' argument is not provided.
"""
- if isinstance(self.agent, Agent):
- # Add a prompt to notify the Agent of the sequential workflow
- if "prompt" in self.kwargs:
- self.kwargs["prompt"] += (
- f"\n\nPrevious output: {self.result}"
- if self.result
- else ""
+ 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():
+ self.result = self.agent.run(
+ task=task,
+ *args,
+ **kwargs,
+ )
+ self.history.append(self.result)
+
+ if self.action is not None:
+ self.action()
+ else:
+ self.result = self.agent.run(
+ *self.args, **self.kwargs
)
+
+ self.history.append(self.result)
+ except Exception as error:
+ logger.error(f"[ERROR][Task] {error}")
+
+ def run(self, *args, **kwargs):
+ self.execute(*args, **kwargs)
+
+ def __call__(self, *args, **kwargs):
+ self.execute(*args, **kwargs)
+
+ def handle_scheduled_task(self):
+ """
+ Handles the execution of a scheduled 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
+ or self.schedule_time <= datetime.now()
+ ):
+ self.execute()
+
else:
- self.kwargs["prompt"] = (
- f"Main task: {self.description}"
- + (
- f"\n\nPrevious output: {self.result}"
- if self.result
- else ""
- )
+ delay = (
+ self.schedule_time - datetime.now()
+ ).total_seconds()
+ self.scheduler.enter(delay, 1, self.execute)
+ self.scheduler_run()
+ except Exception as error:
+ logger.error(f"[ERROR][Task] {error}")
+
+ def set_trigger(self, trigger: Callable):
+ """
+ Sets the trigger for the task.
+
+ Args:
+ trigger (Callable): The trigger to set.
+ """
+ self.trigger = trigger
+
+ def set_action(self, action: Callable):
+ """
+ Sets the action for the task.
+
+ Args:
+ action (Callable): The action to set.
+ """
+ self.action = action
+
+ def set_condition(self, condition: Callable):
+ """
+ Sets the condition for the task.
+
+ Args:
+ condition (Callable): The condition to set.
+ """
+ self.condition = condition
+
+ def is_completed(self):
+ """Is the task completed?
+
+ Returns:
+ _type_: _description_
+ """
+ return self.result is not None
+
+ def add_dependency(self, task):
+ """Adds a task to the list of dependencies.
+
+ Args:
+ task (_type_): _description_
+ """
+ self.dependencies.append(task)
+
+ def set_priority(self, priority: int):
+ """Sets the priority of the task.
+
+ Args:
+ priority (int): _description_
+ """
+ self.priority = priority
+
+ def check_dependency_completion(self):
+ """
+ Checks whether all the dependencies have been completed.
+
+ Returns:
+ bool: True if all the dependencies have been completed, False otherwise.
+ """
+ logger.info("[INFO][Task] Checking dependency completion")
+ try:
+ for task in self.dependencies:
+ if not task.is_completed():
+ return False
+ except Exception as error:
+ 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 ""
)
- self.result = self.agent.run(*self.args, **self.kwargs)
- else:
- self.result = self.agent(*self.args, **self.kwargs)
- self.history.append(self.result)
+ # 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/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/structs/team.py b/swarms/structs/team.py
new file mode 100644
index 00000000..3b71165d
--- /dev/null
+++ b/swarms/structs/team.py
@@ -0,0 +1,107 @@
+import json
+from typing import List, Optional
+
+from pydantic import BaseModel, Field, Json, root_validator
+
+from swarms.structs.agent import Agent
+from swarms.structs.task import Task
+
+
+class Team(BaseModel):
+ """
+ Class that represents a group of agents, how they should work together and
+ their tasks.
+
+ Attributes:
+ tasks (Optional[List[Task]]): List of tasks.
+ agents (Optional[List[Agent]]): List of agents in this Team.
+ architecture (str): Architecture that the Team will follow. Default is "sequential".
+ verbose (bool): Verbose mode for the Agent Execution. Default is False.
+ config (Optional[Json]): Configuration of the Team. Default is None.
+ """
+
+ tasks: Optional[List[Task]] = Field(description="List of tasks")
+ agents: Optional[List[Agent]] = Field(
+ description="List of agents in this Team."
+ )
+ architecture = Field(
+ description="architecture that the Team will follow.",
+ default="sequential",
+ )
+ verbose: bool = Field(
+ description="Verbose mode for the Agent Execution",
+ default=False,
+ )
+ config: Optional[Json] = Field(
+ description="Configuration of the Team.", default=None
+ )
+
+ @root_validator(pre=True)
+ def check_config(_cls, values):
+ if not values.get("config") and (
+ not values.get("agents") and not values.get("tasks")
+ ):
+ raise ValueError(
+ "Either agents and task need to be set or config."
+ )
+
+ if values.get("config"):
+ config = json.loads(values.get("config"))
+ if not config.get("agents") or not config.get("tasks"):
+ raise ValueError(
+ "Config should have agents and tasks."
+ )
+
+ values["agents"] = [
+ Agent(**agent) for agent in config["agents"]
+ ]
+
+ tasks = []
+ for task in config["tasks"]:
+ task_agent = [
+ agt
+ for agt in values["agents"]
+ if agt.role == task["agent"]
+ ][0]
+ del task["agent"]
+ tasks.append(Task(**task, agent=task_agent))
+
+ values["tasks"] = tasks
+ return values
+
+ def run(self) -> str:
+ """
+ Kickoff the Team to work on its tasks.
+
+ Returns:
+ output (List[str]): Output of the Team for each task.
+ """
+ if self.architecture == "sequential":
+ return self.__sequential_loop()
+
+ def __sequential_loop(self) -> str:
+ """
+ Loop that executes the sequential architecture.
+
+ Returns:
+ output (str): Output of the Team.
+ """
+ task_outcome = None
+ for task in self.tasks:
+ # Add delegation tools to the task if the agent allows it
+ # if task.agent.allow_delegation:
+ # tools = AgentTools(agents=self.agents).tools()
+ # task.tools += tools
+
+ self.__log(f"\nWorking Agent: {task.agent.role}")
+ self.__log(f"Starting Task: {task.description} ...")
+
+ task_outcome = task.execute(task_outcome)
+
+ self.__log(f"Task output: {task_outcome}")
+
+ return task_outcome
+
+ def __log(self, message):
+ if self.verbose:
+ print(message)
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/structs/types.py b/swarms/structs/types.py
new file mode 100644
index 00000000..e69de29b
diff --git a/swarms/swarms/utils.py b/swarms/structs/utils.py
similarity index 52%
rename from swarms/swarms/utils.py
rename to swarms/structs/utils.py
index 73da08df..634021e2 100644
--- a/swarms/swarms/utils.py
+++ b/swarms/structs/utils.py
@@ -1,4 +1,6 @@
-from typing import Dict, Any, List
+import re
+import json
+from typing import Dict, Any, List, Optional
from swarms.structs.agent import Agent
@@ -66,3 +68,72 @@ def distribute_tasks(
f"No agent found with ID {agent_id}. Task '{task}' is"
" not assigned."
)
+
+
+def find_token_in_text(text: str, token: str = "") -> bool:
+ """
+ Parse a block of text for a specific token.
+
+ Args:
+ text (str): The text to parse.
+ token (str): The token to find.
+
+ Returns:
+ bool: True if the token is found in the text, False otherwise.
+ """
+ # Check if the token is in the text
+ if token in text:
+ return True
+ else:
+ return False
+
+
+def extract_key_from_json(
+ json_response: str, key: str
+) -> Optional[str]:
+ """
+ Extract a specific key from a JSON response.
+
+ Args:
+ json_response (str): The JSON response to parse.
+ key (str): The key to extract.
+
+ Returns:
+ Optional[str]: The value of the key if it exists, None otherwise.
+ """
+ response_dict = json.loads(json_response)
+ return response_dict.get(key)
+
+
+def extract_tokens_from_text(
+ text: str, tokens: List[str]
+) -> List[str]:
+ """
+ Extract a list of tokens from a text response.
+
+ Args:
+ text (str): The text to parse.
+ tokens (List[str]): The tokens to extract.
+
+ Returns:
+ List[str]: The tokens that were found in the text.
+ """
+ return [token for token in tokens if token in text]
+
+
+def detect_markdown(text: str) -> bool:
+ """
+ Checks if a string contains Markdown code enclosed in six backticks.
+
+ Parameters
+ ----------
+ text : str
+ The text to check.
+
+ Returns
+ -------
+ bool
+ True if the text contains Markdown code enclosed in six backticks, False otherwise.
+ """
+ pattern = r"``````[\s\S]*?``````"
+ return bool(re.search(pattern, text))
diff --git a/swarms/swarms/__init__.py b/swarms/swarms/__init__.py
deleted file mode 100644
index 38ced622..00000000
--- a/swarms/swarms/__init__.py
+++ /dev/null
@@ -1,11 +0,0 @@
-from swarms.structs.autoscaler import AutoScaler
-from swarms.swarms.god_mode import GodMode
-from swarms.swarms.multi_agent_collab import MultiAgentCollaboration
-from swarms.swarms.base import AbstractSwarm
-
-__all__ = [
- "AutoScaler",
- "GodMode",
- "MultiAgentCollaboration",
- "AbstractSwarm",
-]
diff --git a/swarms/swarms/god_mode.py b/swarms/swarms/god_mode.py
deleted file mode 100644
index 29178b2c..00000000
--- a/swarms/swarms/god_mode.py
+++ /dev/null
@@ -1,187 +0,0 @@
-import asyncio
-import logging
-from concurrent.futures import ThreadPoolExecutor, as_completed
-from typing import Callable, List
-
-from tabulate import tabulate
-from termcolor import colored
-
-# Configure logging
-logging.basicConfig(level=logging.INFO)
-logger = logging.getLogger(__name__)
-
-
-class GodMode:
- """
- GodMode
- -----
-
- Architecture:
- How it works:
- 1. GodMode receives a task from the user.
- 2. GodMode distributes the task to all LLMs.
- 3. GodMode collects the responses from all LLMs.
- 4. GodMode prints the responses from all LLMs.
-
- Parameters:
- llms: list of LLMs
-
- Methods:
- run(task): distribute task to all LLMs and collect responses
- print_responses(task): print responses from all LLMs
-
- Usage:
- god_mode = GodMode(llms)
- god_mode.run(task)
- god_mode.print_responses(task)
-
-
- """
-
- def __init__(
- self,
- llms: List[Callable],
- load_balancing: bool = False,
- retry_attempts: int = 3,
- ):
- self.llms = llms
- self.load_balancing = load_balancing
- self.retry_attempts = retry_attempts
- self.last_responses = None
- self.task_history = []
-
- def run(self, task: str):
- """Run the task string"""
- with ThreadPoolExecutor() as executor:
- responses = executor.map(lambda llm: llm(task), self.llms)
- return list(responses)
-
- 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 = []
- for llm in self.llms:
- 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"""
- with open(filename, "w") as file:
- table = [
- [f"LLM {i+1}", response]
- for i, response in enumerate(self.last_responses)
- ]
- file.write(tabulate(table, headers=["LLM", "Response"]))
-
- @classmethod
- def load_llms_from_file(cls, filename):
- """Load llms from file"""
- with open(filename, "r") as file:
- llms = [line.strip() for line in file.readlines()]
- return cls(llms)
-
- def get_task_history(self):
- """Get Task history"""
- return self.task_history
-
- def summary(self):
- """Summary"""
- print("Tasks History:")
- for i, task in enumerate(self.task_history):
- print(f"{i + 1}. {task}")
- print("\nLast Responses:")
- table = [
- [f"LLM {i+1}", response]
- for i, response in enumerate(self.last_responses)
- ]
- print(
- colored(
- tabulate(
- table,
- headers=["LLM", "Response"],
- tablefmt="pretty",
- ),
- "cyan",
- )
- )
-
- def enable_load_balancing(self):
- """Enable load balancing among LLMs."""
- self.load_balancing = True
- logger.info("Load balancing enabled.")
-
- def disable_load_balancing(self):
- """Disable load balancing."""
- self.load_balancing = False
- logger.info("Load balancing disabled.")
-
- async def arun(self, task: str):
- """Asynchronous run the task string"""
- loop = asyncio.get_event_loop()
- futures = [
- loop.run_in_executor(None, lambda llm: llm(task), llm)
- for llm in self.llms
- ]
- for response in await asyncio.gather(*futures):
- print(response)
-
- def concurrent_run(self, task: str) -> List[str]:
- """Synchronously run the task on all llms and collect responses"""
- with ThreadPoolExecutor() as executor:
- future_to_llm = {
- executor.submit(llm, task): llm for llm in self.llms
- }
- responses = []
- for future in as_completed(future_to_llm):
- try:
- responses.append(future.result())
- except Exception as error:
- print(
- f"{future_to_llm[future]} generated an"
- f" exception: {error}"
- )
- self.last_responses = responses
- self.task_history.append(task)
- return responses
-
- def add_llm(self, llm: Callable):
- """Add an llm to the god mode"""
- self.llms.append(llm)
-
- def remove_llm(self, llm: Callable):
- """Remove an llm from the god mode"""
- self.llms.remove(llm)
diff --git a/swarms/telemetry/__init__.py b/swarms/telemetry/__init__.py
index 467a3b61..3a7e61d3 100644
--- a/swarms/telemetry/__init__.py
+++ b/swarms/telemetry/__init__.py
@@ -1,20 +1,57 @@
-from swarms.telemetry.log_all import log_all_calls, log_calls
+# from swarms.telemetry.posthog_utils import posthog
-# from swarms.telemetry.posthog_utils import log_activity_posthog
+from swarms.telemetry.log_all import log_all_calls, log_calls
+from swarms.telemetry.sys_info import (
+ get_cpu_info,
+ get_swarms_verison,
+ get_os_version,
+ get_package_mismatches,
+ get_pip_version,
+ get_python_version,
+ get_ram_info,
+ system_info,
+)
from swarms.telemetry.user_utils import (
+ generate_unique_identifier,
generate_user_id,
get_machine_id,
get_system_info,
- generate_unique_identifier,
+ 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",
- # "log_activity_posthog",
"generate_user_id",
"get_machine_id",
"get_system_info",
"generate_unique_identifier",
+ "get_python_version",
+ "get_pip_version",
+ "get_swarms_verison",
+ "get_os_version",
+ "get_cpu_info",
+ "get_ram_info",
+ "get_package_mismatches",
+ "system_info",
+ "get_user_device_data",
]
diff --git a/swarms/telemetry/auto_upgrade_swarms.py b/swarms/telemetry/auto_upgrade_swarms.py
new file mode 100644
index 00000000..96f843a7
--- /dev/null
+++ b/swarms/telemetry/auto_upgrade_swarms.py
@@ -0,0 +1,17 @@
+import subprocess
+from swarms.telemetry.check_update import check_for_update
+
+
+def auto_update():
+ """auto update swarms"""
+ try:
+ if check_for_update is True:
+ print(
+ "There is a new version of swarms available!"
+ " Downloading..."
+ )
+ subprocess.run(["pip", "install", "--upgrade", "swarms"])
+ else:
+ print("swarms is up to date!")
+ except Exception as e:
+ print(e)
diff --git a/swarms/telemetry/bootup.py b/swarms/telemetry/bootup.py
new file mode 100644
index 00000000..edcd1aca
--- /dev/null
+++ b/swarms/telemetry/bootup.py
@@ -0,0 +1,8 @@
+from swarms.utils.disable_logging import disable_logging
+from swarms.telemetry.auto_upgrade_swarms import auto_update
+
+
+def bootup():
+ """Bootup swarms"""
+ disable_logging()
+ auto_update()
diff --git a/swarms/telemetry/check_update.py b/swarms/telemetry/check_update.py
new file mode 100644
index 00000000..2cdbd986
--- /dev/null
+++ b/swarms/telemetry/check_update.py
@@ -0,0 +1,42 @@
+import pkg_resources
+import requests
+from packaging import version
+
+import importlib.util
+import sys
+
+
+# borrowed from: https://stackoverflow.com/a/1051266/656011
+def check_for_package(package):
+ if package in sys.modules:
+ return True
+ elif (spec := importlib.util.find_spec(package)) is not None:
+ try:
+ module = importlib.util.module_from_spec(spec)
+
+ sys.modules[package] = module
+ spec.loader.exec_module(module)
+
+ return True
+ except ImportError:
+ return False
+ else:
+ return False
+
+
+def check_for_update():
+ """Check for updates
+
+ Returns:
+ BOOL: Flag to indicate if there is an update
+ """
+ # Fetch the latest version from the PyPI API
+ response = requests.get("https://pypi.org/pypi/swarms/json")
+ latest_version = response.json()["info"]["version"]
+
+ # Get the current version using pkg_resources
+ current_version = pkg_resources.get_distribution("swarms").version
+
+ return version.parse(latest_version) > version.parse(
+ current_version
+ )
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/posthog_utils.py b/swarms/telemetry/posthog_utils.py
index a6a520b5..7ae8d0a7 100644
--- a/swarms/telemetry/posthog_utils.py
+++ b/swarms/telemetry/posthog_utils.py
@@ -1,69 +1,6 @@
-import functools
-import os
-
-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)
+posthog = Posthog(
+ project_api_key="phc_Gz6XxldNZIkzW7QnSTGr5HZ28OAYPIfpE7X5A3vUsfO",
+ host="https://app.posthog.com",
+)
diff --git a/swarms/telemetry/sys_info.py b/swarms/telemetry/sys_info.py
new file mode 100644
index 00000000..a4857e11
--- /dev/null
+++ b/swarms/telemetry/sys_info.py
@@ -0,0 +1,101 @@
+import platform
+import subprocess
+
+import pkg_resources
+import psutil
+import toml
+
+
+def get_python_version():
+ return platform.python_version()
+
+
+def get_pip_version():
+ try:
+ pip_version = (
+ subprocess.check_output(["pip", "--version"])
+ .decode()
+ .split()[1]
+ )
+ except Exception as e:
+ pip_version = str(e)
+ return pip_version
+
+
+def get_swarms_verison():
+ try:
+ swarms_verison_cmd = (
+ subprocess.check_output(["swarms", "--version"])
+ .decode()
+ .split()[1]
+ )
+ except Exception as e:
+ swarms_verison_cmd = str(e)
+ swarms_verison_pkg = pkg_resources.get_distribution(
+ "swarms"
+ ).version
+ swarms_verison = swarms_verison_cmd, swarms_verison_pkg
+ return swarms_verison
+
+
+def get_os_version():
+ return platform.platform()
+
+
+def get_cpu_info():
+ return platform.processor()
+
+
+def get_ram_info():
+ vm = psutil.virtual_memory()
+ used_ram_gb = vm.used / (1024**3)
+ free_ram_gb = vm.free / (1024**3)
+ total_ram_gb = vm.total / (1024**3)
+ return (
+ f"{total_ram_gb:.2f} GB, used: {used_ram_gb:.2f}, free:"
+ f" {free_ram_gb:.2f}"
+ )
+
+
+def get_package_mismatches(file_path="pyproject.toml"):
+ with open(file_path, "r") as file:
+ pyproject = toml.load(file)
+ dependencies = pyproject["tool"]["poetry"]["dependencies"]
+ dev_dependencies = pyproject["tool"]["poetry"]["group"]["dev"][
+ "dependencies"
+ ]
+ dependencies.update(dev_dependencies)
+
+ installed_packages = {
+ pkg.key: pkg.version for pkg in pkg_resources.working_set
+ }
+
+ mismatches = []
+ for package, version_info in dependencies.items():
+ if isinstance(version_info, dict):
+ version_info = version_info["version"]
+ installed_version = installed_packages.get(package)
+ if installed_version and version_info.startswith("^"):
+ expected_version = version_info[1:]
+ if not installed_version.startswith(expected_version):
+ mismatches.append(
+ f"\t {package}: Mismatch,"
+ f" pyproject.toml={expected_version},"
+ f" pip={installed_version}"
+ )
+ else:
+ mismatches.append(f"\t {package}: Not found in pip list")
+
+ return "\n" + "\n".join(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..4d4fb166 100644
--- a/swarms/telemetry/user_utils.py
+++ b/swarms/telemetry/user_utils.py
@@ -2,6 +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
@@ -47,6 +49,7 @@ def get_system_info():
),
"processor": platform.processor(),
"python_version": platform.python_version(),
+ "Misc": system_info(),
}
return info
@@ -61,3 +64,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
+
+
+#
diff --git a/swarms/tokenizers/__init__.py b/swarms/tokenizers/__init__.py
new file mode 100644
index 00000000..02129cb9
--- /dev/null
+++ b/swarms/tokenizers/__init__.py
@@ -0,0 +1,23 @@
+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 (
+ 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/tokenizers/r_tokenizers.py b/swarms/tokenizers/r_tokenizers.py
new file mode 100644
index 00000000..cf8253fc
--- /dev/null
+++ b/swarms/tokenizers/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/tools/__init__.py b/swarms/tools/__init__.py
index 65153867..c36c9608 100644
--- a/swarms/tools/__init__.py
+++ b/swarms/tools/__init__.py
@@ -1,3 +1,34 @@
from swarms.tools.tool_func_doc_scraper import scrape_tool_func_docs
+from swarms.tools.code_executor import CodeExecutor
+from swarms.tools.tool_utils import (
+ tool_find_by_name,
+ extract_tool_commands,
+ parse_and_execute_tools,
+ 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"]
+__all__ = [
+ "scrape_tool_func_docs",
+ "CodeExecutor",
+ "tool_find_by_name",
+ "extract_tool_commands",
+ "parse_and_execute_tools",
+ "execute_tools",
+ "BaseTool",
+ "Tool",
+ "StructuredTool",
+ "tool",
+ "AgentAction",
+ "BaseAgentOutputParser",
+ "preprocess_json_input",
+ "AgentOutputParser",
+ "execute_tool_by_name",
+]
diff --git a/swarms/tools/code_executor.py b/swarms/tools/code_executor.py
new file mode 100644
index 00000000..3c369dae
--- /dev/null
+++ b/swarms/tools/code_executor.py
@@ -0,0 +1,111 @@
+import os
+import tempfile
+import subprocess
+
+
+class CodeExecutor:
+ """
+ A class for executing code snippets.
+
+ Args:
+ code (str, optional): The code snippet to be executed. Defaults to None.
+
+ Methods:
+ is_python_code(code: str = None) -> bool:
+ Checks if the given code is Python code.
+
+ run_python(code: str = None) -> str:
+ Executes the given Python code and returns the output.
+
+ run(code: str = None) -> str:
+ Executes the given code and returns the output.
+
+ __call__() -> str:
+ Executes the code and returns the output.
+ """
+
+ def __init__(self, code: str = None):
+ self.code = code
+
+ def is_python_code(self, code: str = None) -> bool:
+ """
+ Checks if the given code is Python code.
+
+ Args:
+ code (str, optional): The code to be checked. Defaults to None.
+
+ Returns:
+ bool: True if the code is Python code, False otherwise.
+ """
+ code = code or self.code
+ return code.strip().startswith("python")
+
+ def run_python(self, code: str = None) -> str:
+ """
+ Executes the given Python code and returns the output.
+
+ Args:
+ code (str, optional): The Python code to be executed. Defaults to None.
+
+ Returns:
+ str: The output of the code execution.
+ """
+ code = code or self.code
+ try:
+ # Create a temporary file
+ with tempfile.NamedTemporaryFile(
+ suffix=".py", delete=False
+ ) as temp:
+ temp.write(code.encode())
+ temp_filename = temp.name
+
+ # Execute the temporary file
+ output = subprocess.check_output(
+ f"python {temp_filename}",
+ shell=True,
+ )
+
+ # Delete the temporary file
+ os.remove(temp_filename)
+
+ return output.decode("utf-8")
+ except subprocess.CalledProcessError as error:
+ return error.output.decode("utf-8")
+ except Exception as error:
+ return str(error)
+
+ def run(self, code: str = None) -> str:
+ """
+ Executes the given code and returns the output.
+
+ Args:
+ code (str, optional): The code to be executed. Defaults to None.
+
+ Returns:
+ str: The output of the code execution.
+ """
+ code = code or self.code
+ try:
+ output = subprocess.check_output(
+ code,
+ shell=True,
+ )
+ return output.decode("utf-8")
+ except subprocess.CalledProcessError as e:
+ return e.output.decode("utf-8")
+ except Exception as e:
+ return str(e)
+
+ def __call__(self) -> str:
+ """
+ Executes the code and returns the output.
+
+ Returns:
+ str: The output of the code execution.
+ """
+ return self.run()
+
+
+# model = CodeExecutor()
+# out = model.run("python3")
+# print(out)
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
diff --git a/swarms/tools/format_tools.py b/swarms/tools/format_tools.py
new file mode 100644
index 00000000..2e620135
--- /dev/null
+++ b/swarms/tools/format_tools.py
@@ -0,0 +1,313 @@
+from typing import List, Union, Dict, Any
+
+from swarms.tools.logits_processor import (
+ NumberStoppingCriteria,
+ OutputNumbersTokens,
+ StringStoppingCriteria,
+)
+from termcolor import cprint
+from transformers import PreTrainedModel, PreTrainedTokenizer
+import json
+
+GENERATION_MARKER = "|GENERATION|"
+
+
+class Jsonformer:
+ """
+ Initializes the FormatTools class.
+
+ Args:
+ model (PreTrainedModel): The pre-trained model.
+ tokenizer (PreTrainedTokenizer): The tokenizer for the model.
+ json_schema (Dict[str, Any]): The JSON schema.
+ prompt (str): The prompt for generation.
+
+ Keyword Args:
+ debug (bool, optional): Whether to enable debug mode. Defaults to False.
+ max_array_length (int, optional): The maximum length of an array. Defaults to 10.
+ max_number_tokens (int, optional): The maximum number of tokens for numbers. Defaults to 6.
+ temperature (float, optional): The temperature for generation. Defaults to 1.0.
+ max_string_token_length (int, optional): The maximum length of a string token. Defaults to 10.
+ """
+
+ value: Dict[str, Any] = {}
+
+ def __init__(
+ self,
+ model: PreTrainedModel,
+ tokenizer: PreTrainedTokenizer,
+ json_schema: Dict[str, Any],
+ prompt: str,
+ *,
+ debug: bool = False,
+ max_array_length: int = 10,
+ max_number_tokens: int = 6,
+ temperature: float = 1.0,
+ max_string_token_length: int = 10,
+ ):
+ self.model = model
+ self.tokenizer = tokenizer
+ self.json_schema = json_schema
+ self.prompt = prompt
+
+ self.number_logit_processor = OutputNumbersTokens(
+ self.tokenizer, self.prompt
+ )
+
+ self.generation_marker = "|GENERATION|"
+ self.debug_on = debug
+ self.max_array_length = max_array_length
+
+ self.max_number_tokens = max_number_tokens
+ self.temperature = temperature
+ self.max_string_token_length = max_string_token_length
+
+ def debug(self, caller: str, value: str, is_prompt: bool = False):
+ if self.debug_on:
+ if is_prompt:
+ cprint(caller, "green", end=" ")
+ cprint(value, "yellow")
+ else:
+ cprint(caller, "green", end=" ")
+ cprint(value, "blue")
+
+ def generate_number(
+ self, temperature: Union[float, None] = None, iterations=0
+ ):
+ """
+ Generates a number based on the given prompt.
+
+ Args:
+ temperature (float, optional): The temperature value for number generation. Defaults to None.
+ iterations (int, optional): The number of iterations for generating a valid number. Defaults to 0.
+
+ Returns:
+ float: The generated number.
+
+ Raises:
+ ValueError: If a valid number cannot be generated after 3 iterations.
+ """
+ prompt = self.get_prompt()
+ self.debug("[generate_number]", prompt, is_prompt=True)
+ input_tokens = self.tokenizer.encode(
+ prompt, return_tensors="pt"
+ ).to(self.model.device)
+ response = self.model.generate(
+ input_tokens,
+ max_new_tokens=self.max_number_tokens,
+ num_return_sequences=1,
+ logits_processor=[self.number_logit_processor],
+ stopping_criteria=[
+ NumberStoppingCriteria(
+ self.tokenizer, len(input_tokens[0])
+ )
+ ],
+ temperature=temperature or self.temperature,
+ pad_token_id=self.tokenizer.eos_token_id,
+ )
+ response = self.tokenizer.decode(
+ response[0], skip_special_tokens=True
+ )
+
+ response = response[len(prompt) :]
+ response = response.strip().rstrip(".")
+ self.debug("[generate_number]", response)
+ try:
+ return float(response)
+ except ValueError:
+ if iterations > 3:
+ raise ValueError("Failed to generate a valid number")
+
+ return self.generate_number(
+ temperature=self.temperature * 1.3,
+ iterations=iterations + 1,
+ )
+
+ def generate_boolean(self) -> bool:
+ """
+ Generates a boolean value based on the given prompt.
+
+ Returns:
+ bool: The generated boolean value.
+ """
+ prompt = self.get_prompt()
+ self.debug("[generate_boolean]", prompt, is_prompt=True)
+
+ input_tensor = self.tokenizer.encode(
+ prompt, return_tensors="pt"
+ )
+ output = self.model.forward(
+ input_tensor.to(self.model.device)
+ )
+ logits = output.logits[0, -1]
+
+ # todo: this assumes that "true" and "false" are both tokenized to a single token
+ # this is probably not true for all tokenizers
+ # this can be fixed by looking at only the first token of both "true" and "false"
+ true_token_id = self.tokenizer.convert_tokens_to_ids("true")
+ false_token_id = self.tokenizer.convert_tokens_to_ids("false")
+
+ result = logits[true_token_id] > logits[false_token_id]
+
+ self.debug("[generate_boolean]", result)
+
+ return result.item()
+
+ def generate_string(self) -> str:
+ prompt = self.get_prompt() + '"'
+ self.debug("[generate_string]", prompt, is_prompt=True)
+ input_tokens = self.tokenizer.encode(
+ prompt, return_tensors="pt"
+ ).to(self.model.device)
+
+ response = self.model.generate(
+ input_tokens,
+ max_new_tokens=self.max_string_token_length,
+ num_return_sequences=1,
+ temperature=self.temperature,
+ stopping_criteria=[
+ StringStoppingCriteria(
+ self.tokenizer, len(input_tokens[0])
+ )
+ ],
+ pad_token_id=self.tokenizer.eos_token_id,
+ )
+
+ # Some models output the prompt as part of the response
+ # This removes the prompt from the response if it is present
+ if (
+ len(response[0]) >= len(input_tokens[0])
+ and (
+ response[0][: len(input_tokens[0])] == input_tokens
+ ).all()
+ ):
+ response = response[0][len(input_tokens[0]) :]
+ if response.shape[0] == 1:
+ response = response[0]
+
+ response = self.tokenizer.decode(
+ response, skip_special_tokens=True
+ )
+
+ self.debug("[generate_string]", "|" + response + "|")
+
+ if response.count('"') < 1:
+ return response
+
+ return response.split('"')[0].strip()
+
+ def generate_object(
+ self, properties: Dict[str, Any], obj: Dict[str, Any]
+ ) -> Dict[str, Any]:
+ for key, schema in properties.items():
+ self.debug("[generate_object] generating value for", key)
+ obj[key] = self.generate_value(schema, obj, key)
+ return obj
+
+ def generate_value(
+ self,
+ schema: Dict[str, Any],
+ obj: Union[Dict[str, Any], List[Any]],
+ key: Union[str, None] = None,
+ ) -> Any:
+ schema_type = schema["type"]
+ if schema_type == "number":
+ if key:
+ obj[key] = self.generation_marker
+ else:
+ obj.append(self.generation_marker)
+ return self.generate_number()
+ elif schema_type == "boolean":
+ if key:
+ obj[key] = self.generation_marker
+ else:
+ obj.append(self.generation_marker)
+ return self.generate_boolean()
+ elif schema_type == "string":
+ if key:
+ obj[key] = self.generation_marker
+ else:
+ obj.append(self.generation_marker)
+ return self.generate_string()
+ elif schema_type == "array":
+ new_array = []
+ obj[key] = new_array
+ return self.generate_array(schema["items"], new_array)
+ elif schema_type == "object":
+ new_obj = {}
+ if key:
+ obj[key] = new_obj
+ else:
+ obj.append(new_obj)
+ return self.generate_object(schema["properties"], new_obj)
+ else:
+ raise ValueError(
+ f"Unsupported schema type: {schema_type}"
+ )
+
+ def generate_array(
+ self, item_schema: Dict[str, Any], obj: Dict[str, Any]
+ ) -> list:
+ for _ in range(self.max_array_length):
+ # forces array to have at least one element
+ element = self.generate_value(item_schema, obj)
+ obj[-1] = element
+
+ obj.append(self.generation_marker)
+ input_prompt = self.get_prompt()
+ obj.pop()
+ input_tensor = self.tokenizer.encode(
+ input_prompt, return_tensors="pt"
+ )
+ output = self.model.forward(
+ input_tensor.to(self.model.device)
+ )
+ logits = output.logits[0, -1]
+
+ top_indices = logits.topk(30).indices
+ sorted_token_ids = top_indices[
+ logits[top_indices].argsort(descending=True)
+ ]
+
+ found_comma = False
+ found_close_bracket = False
+
+ for token_id in sorted_token_ids:
+ decoded_token = self.tokenizer.decode(token_id)
+ if "," in decoded_token:
+ found_comma = True
+ break
+ if "]" in decoded_token:
+ found_close_bracket = True
+ break
+
+ if found_close_bracket or not found_comma:
+ break
+
+ return obj
+
+ def get_prompt(self):
+ template = """{prompt}\nOutput result in the following JSON schema format:\n{schema}\nResult: {progress}"""
+ progress = json.dumps(self.value)
+ gen_marker_index = progress.find(
+ f'"{self.generation_marker}"'
+ )
+ if gen_marker_index != -1:
+ progress = progress[:gen_marker_index]
+ else:
+ raise ValueError("Failed to find generation marker")
+
+ prompt = template.format(
+ prompt=self.prompt,
+ schema=json.dumps(self.json_schema),
+ progress=progress,
+ )
+
+ return prompt
+
+ def __call__(self) -> Dict[str, Any]:
+ self.value = {}
+ generated_data = self.generate_object(
+ self.json_schema["properties"], self.value
+ )
+ return generated_data
diff --git a/swarms/tools/logits_processor.py b/swarms/tools/logits_processor.py
new file mode 100644
index 00000000..ed7fef18
--- /dev/null
+++ b/swarms/tools/logits_processor.py
@@ -0,0 +1,94 @@
+from transformers import (
+ PreTrainedTokenizer,
+ LogitsWarper,
+ StoppingCriteria,
+)
+import torch
+
+
+class StringStoppingCriteria(StoppingCriteria):
+ def __init__(
+ self, tokenizer: PreTrainedTokenizer, prompt_length: int
+ ):
+ self.tokenizer = tokenizer
+ self.prompt_length = prompt_length
+
+ def __call__(
+ self,
+ input_ids: torch.LongTensor,
+ _,
+ ) -> bool:
+ if len(input_ids[0]) <= self.prompt_length:
+ return False
+
+ last_token_id = input_ids[0][-1]
+ last_token = self.tokenizer.decode(
+ last_token_id, skip_special_tokens=True
+ )
+
+ result = '"' in last_token
+
+ return result
+
+
+class NumberStoppingCriteria(StoppingCriteria):
+ def __init__(
+ self,
+ tokenizer: PreTrainedTokenizer,
+ prompt_length: int,
+ precision: int = 3,
+ ):
+ self.tokenizer = tokenizer
+ self.precision = precision
+ self.prompt_length = prompt_length
+
+ def __call__(
+ self,
+ input_ids: torch.LongTensor,
+ scores: torch.FloatTensor,
+ ) -> bool:
+ decoded = self.tokenizer.decode(
+ input_ids[0][self.prompt_length :],
+ skip_special_tokens=True,
+ )
+
+ if decoded.count(".") > 1:
+ return True
+
+ if (
+ decoded.count(".") == 1
+ and len(decoded.strip().split(".")[1]) > self.precision
+ ):
+ return True
+
+ if (
+ len(decoded) > 1
+ and any(c.isdigit() for c in decoded)
+ and decoded[-1] in [" ", "\n"]
+ ):
+ return True
+
+ return False
+
+
+class OutputNumbersTokens(LogitsWarper):
+ def __init__(self, tokenizer: PreTrainedTokenizer, prompt: str):
+ self.tokenizer = tokenizer
+ self.tokenized_prompt = tokenizer(prompt, return_tensors="pt")
+ vocab_size = len(tokenizer)
+ self.allowed_mask = torch.zeros(vocab_size, dtype=torch.bool)
+
+ for _, token_id in tokenizer.get_vocab().items():
+ token_str = tokenizer.decode(token_id).strip()
+
+ if token_str == "" or (
+ all(c.isdigit() or c == "." for c in token_str)
+ and token_str.count(".") <= 1
+ ):
+ self.allowed_mask[token_id] = True
+
+ def __call__(self, _, scores):
+ mask = self.allowed_mask.expand_as(scores)
+ scores[~mask] = -float("inf")
+
+ return scores
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:
diff --git a/swarms/tools/tool_utils.py b/swarms/tools/tool_utils.py
index c189c9f5..a5a4e47c 100644
--- a/swarms/tools/tool_utils.py
+++ b/swarms/tools/tool_utils.py
@@ -1,8 +1,23 @@
-import re
import json
+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]):
+ """Find the tool by name"""
+ for tool in tools:
+ if tool.name == tool_name:
+ return tool
+ return None
-def extract_tool_commands(self, text: str):
+def extract_tool_commands(text: str):
"""
Extract the tool commands from the text
@@ -39,10 +54,86 @@ def parse_and_execute_tools(response: str):
execute_tools(tool_name, params)
-def execute_tools(self, tool_name, params):
+def execute_tools(tool_name, params):
"""Execute the tool with the provided params"""
- tool = self.tool_find_by_name(tool_name)
+ tool = 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_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/__init__.py b/swarms/utils/__init__.py
index 7c3ef717..220bc3e6 100644
--- a/swarms/utils/__init__.py
+++ b/swarms/utils/__init__.py
@@ -1,16 +1,80 @@
+from swarms.utils.class_args_wrapper import print_class_parameters
from swarms.utils.code_interpreter import SubprocessCodeInterpreter
+from swarms.utils.device_checker_cuda import check_device
+from swarms.utils.find_img_path import find_image_path
+from swarms.utils.llm_metrics_decorator import metrics_decorator
+from swarms.utils.load_model_torch import load_model_torch
from swarms.utils.markdown_message import display_markdown_message
-from swarms.utils.parse_code import (
- extract_code_in_backticks_in_string,
-)
+from swarms.utils.math_eval import math_eval
+from swarms.utils.parse_code import extract_code_from_markdown
from swarms.utils.pdf_to_text import pdf_to_text
+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,
+)
+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
-# from swarms.utils.phoenix_handler import phoenix_trace_decorator
+
+########
+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,
+)
+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__ = [
- "display_markdown_message",
"SubprocessCodeInterpreter",
- "extract_code_in_backticks_in_string",
+ "display_markdown_message",
+ "extract_code_from_markdown",
+ "find_image_path",
+ "limit_tokens_from_string",
+ "load_model_torch",
+ "math_eval",
+ "metrics_decorator",
"pdf_to_text",
- # "phoenix_trace_decorator",
+ "prep_torch_inference",
+ "print_class_parameters",
+ "check_device",
+ "csv_to_text",
+ "json_to_text",
+ "txt_to_text",
+ "data_to_text",
+ "try_except_wrapper",
+ "download_weights_from_url",
+ "parse_log_file",
+ "YamlOutputParser",
+ "JsonOutputParser",
+ "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/apa.py b/swarms/utils/apa.py
index f2e1bb38..fa73b7b4 100644
--- a/swarms/utils/apa.py
+++ b/swarms/utils/apa.py
@@ -1,7 +1,5 @@
from enum import Enum, unique, auto
import abc
-import hashlib
-import re
from typing import List, Optional
import json
from dataclasses import dataclass, field
diff --git a/swarms/utils/check_function_result.py b/swarms/utils/check_function_result.py
new file mode 100644
index 00000000..0da3ad91
--- /dev/null
+++ b/swarms/utils/check_function_result.py
@@ -0,0 +1,249 @@
+import signal
+import platform
+import os
+import multiprocessing
+import tempfile
+import contextlib
+from typing import Dict, Optional
+import faulthandler
+import io
+
+
+class WriteOnlyStringIO(io.StringIO):
+ """StringIO that throws an exception when it's read from"""
+
+ def read(self, *args, **kwargs):
+ raise IOError
+
+ def readline(self, *args, **kwargs):
+ raise IOError
+
+ def readlines(self, *args, **kwargs):
+ raise IOError
+
+ def readable(self, *args, **kwargs):
+ """Returns True if the IO object can be read."""
+ return False
+
+
+@contextlib.contextmanager
+def chdir(root):
+ """
+ Context manager that changes the current working directory to the specified root directory.
+
+ Args:
+ root (str): The root directory to change to.
+
+ Yields:
+ None
+
+ Raises:
+ BaseException: If an error occurs within the context.
+
+ Returns:
+ None
+ """
+ if root == ".":
+ yield
+ return
+ cwd = os.getcwd()
+ os.chdir(root)
+ try:
+ yield
+ except BaseException as error:
+ raise error
+ finally:
+ os.chdir(cwd)
+
+
+@contextlib.contextmanager
+def create_tempdir():
+ with tempfile.TemporaryDirectory() as dirname:
+ with chdir(dirname):
+ yield dirname
+
+
+@contextlib.contextmanager
+def swallow_io():
+ """
+ A context manager that redirects stdout, stderr, and stdin to a WriteOnlyStringIO object.
+ This allows for capturing and suppressing the output of a function.
+
+ Usage:
+ with swallow_io():
+ # code to capture output
+
+ """
+ stream = WriteOnlyStringIO()
+ with contextlib.redirect_stdout(stream):
+ with contextlib.redirect_stderr(stream):
+ with redirect_stdin(stream):
+ yield
+
+
+class redirect_stdin(contextlib._RedirectStream): # type: ignore
+ _stream = "stdin"
+
+
+@contextlib.contextmanager
+def time_limit(seconds: float):
+ def signal_handler(signum, frame):
+ raise TimeoutError("Timed out")
+
+ signal.setitimer(signal.ITIMER_REAL, seconds)
+ signal.signal(signal.SIGALRM, signal_handler)
+ try:
+ yield
+ finally:
+ signal.setitimer(signal.ITIMER_REAL, 0)
+
+
+def check_function_result(
+ python_code: str, timeout: float = 5.0
+) -> Dict:
+ """
+ Evaluates the functional correctness of a completion by running the test
+ suite provided in the problem.
+
+ :param completion_id: an optional completion ID so we can match
+ the results later even if execution finishes asynchronously.
+ """
+
+ def unsafe_execute():
+ with create_tempdir():
+ # These system calls are needed when cleaning up tempdir.
+ import os
+ import shutil
+
+ rmtree = shutil.rmtree
+ rmdir = os.rmdir
+ chdir = os.chdir
+
+ # Disable functionalities that can make destructive changes to the test.
+ reliability_guard()
+
+ # Construct the check program and run it.
+ check_program = python_code + "\n"
+
+ try:
+ exec_globals = {}
+ with swallow_io():
+ with time_limit(timeout):
+ exec(check_program, exec_globals)
+ result.append("passed")
+ except TimeoutError:
+ result.append("timed out")
+ except BaseException as e:
+ result.append(f"failed: {e}")
+
+ # Needed for cleaning up.
+ shutil.rmtree = rmtree
+ os.rmdir = rmdir
+ os.chdir = chdir
+
+ manager = multiprocessing.Manager()
+ result = manager.list()
+
+ p = multiprocessing.Process(target=unsafe_execute)
+ p.start()
+ p.join(timeout=timeout + 1)
+ if p.is_alive():
+ p.kill()
+
+ if not result:
+ result.append("timed out")
+
+ return dict(
+ passed=result[0] == "passed",
+ result=result[0],
+ )
+
+
+def reliability_guard(maximum_memory_bytes: Optional[int] = None):
+ """
+ This disables various destructive functions and prevents the generated code
+ from interfering with the test (e.g. fork bomb, killing other processes,
+ removing filesystem files, etc.)
+
+ WARNING
+ This function is NOT a security sandbox. Untrusted code, including, model-
+ generated code, should not be blindly executed outside of one. See the
+ Codex paper for more information about OpenAI's code sandbox, and proceed
+ with caution.
+ """
+
+ if maximum_memory_bytes is not None:
+ import resource
+
+ resource.setrlimit(
+ resource.RLIMIT_AS,
+ (maximum_memory_bytes, maximum_memory_bytes),
+ )
+ resource.setrlimit(
+ resource.RLIMIT_DATA,
+ (maximum_memory_bytes, maximum_memory_bytes),
+ )
+ if not platform.uname().system == "Darwin":
+ resource.setrlimit(
+ resource.RLIMIT_STACK,
+ (maximum_memory_bytes, maximum_memory_bytes),
+ )
+
+ faulthandler.disable()
+
+ import builtins
+
+ builtins.exit = None
+ builtins.quit = None
+
+ import os
+
+ os.environ["OMP_NUM_THREADS"] = "1"
+
+ os.kill = None
+ os.system = None
+ os.putenv = None
+ os.remove = None
+ os.removedirs = None
+ os.rmdir = None
+ os.fchdir = None
+ os.setuid = None
+ os.fork = None
+ os.forkpty = None
+ os.killpg = None
+ os.rename = None
+ os.renames = None
+ os.truncate = None
+ os.replace = None
+ os.unlink = None
+ os.fchmod = None
+ os.fchown = None
+ os.chmod = None
+ os.chown = None
+ os.chroot = None
+ os.fchdir = None
+ os.lchflags = None
+ os.lchmod = None
+ os.lchown = None
+ os.getcwd = None
+ os.chdir = None
+
+ import shutil
+
+ shutil.rmtree = None
+ shutil.move = None
+ shutil.chown = None
+
+ import subprocess
+
+ subprocess.Popen = None # type: ignore
+
+ __builtins__["help"] = None
+
+ import sys
+
+ sys.modules["ipdb"] = None
+ sys.modules["joblib"] = None
+ sys.modules["resource"] = None
+ sys.modules["psutil"] = None
+ sys.modules["tkinter"] = None
diff --git a/swarms/utils/class_args_wrapper.py b/swarms/utils/class_args_wrapper.py
new file mode 100644
index 00000000..f24932cf
--- /dev/null
+++ b/swarms/utils/class_args_wrapper.py
@@ -0,0 +1,36 @@
+import inspect
+
+
+def print_class_parameters(cls, api_format: bool = False):
+ """
+ Print the parameters of a class constructor.
+
+ Parameters:
+ cls (type): The class to inspect.
+
+ Example:
+ >>> print_class_parameters(Agent)
+ Parameter: x, Type:
+ Parameter: y, Type:
+ """
+ try:
+ # Get the parameters of the class constructor
+ sig = inspect.signature(cls.__init__)
+ params = sig.parameters
+
+ if api_format:
+ param_dict = {}
+ for name, param in params.items():
+ if name == "self":
+ continue
+ param_dict[name] = str(param.annotation)
+ return param_dict
+
+ # Print the parameters
+ for name, param in params.items():
+ if name == "self":
+ continue
+ print(f"Parameter: {name}, Type: {param.annotation}")
+
+ except Exception as e:
+ print(f"An error occurred while inspecting the class: {e}")
diff --git a/swarms/utils/code_interpreter.py b/swarms/utils/code_interpreter.py
index 98fbab70..9e27b668 100644
--- a/swarms/utils/code_interpreter.py
+++ b/swarms/utils/code_interpreter.py
@@ -5,22 +5,7 @@ import time
import traceback
-class BaseCodeInterpreter:
- """
- .run is a generator that yields a dict with attributes: active_line, output
- """
-
- def __init__(self):
- pass
-
- def run(self, code):
- pass
-
- def terminate(self):
- pass
-
-
-class SubprocessCodeInterpreter(BaseCodeInterpreter):
+class SubprocessCodeInterpreter:
"""
SubprocessCodeinterpreter is a base class for code interpreters that run code in a subprocess.
@@ -43,12 +28,36 @@ class SubprocessCodeInterpreter(BaseCodeInterpreter):
self.done = threading.Event()
def detect_active_line(self, line):
+ """Detect if the line is an active line
+
+ Args:
+ line (_type_): _description_
+
+ Returns:
+ _type_: _description_
+ """
return None
def detect_end_of_execution(self, line):
+ """detect if the line is an end of execution line
+
+ Args:
+ line (_type_): _description_
+
+ Returns:
+ _type_: _description_
+ """
return None
def line_postprocessor(self, line):
+ """Line postprocessor
+
+ Args:
+ line (_type_): _description_
+
+ Returns:
+ _type_: _description_
+ """
return line
def preprocess_code(self, code):
@@ -61,9 +70,11 @@ class SubprocessCodeInterpreter(BaseCodeInterpreter):
return code
def terminate(self):
+ """terminate the subprocess"""
self.process.terminate()
def start_process(self):
+ """start the subprocess"""
if self.process:
self.terminate()
@@ -88,6 +99,14 @@ class SubprocessCodeInterpreter(BaseCodeInterpreter):
).start()
def run(self, code: str):
+ """Run the code in the subprocess
+
+ Args:
+ code (str): _description_
+
+ Yields:
+ _type_: _description_
+ """
retry_count = 0
max_retries = 3
@@ -157,6 +176,12 @@ class SubprocessCodeInterpreter(BaseCodeInterpreter):
break
def handle_stream_output(self, stream, is_error_stream):
+ """Handle the output from the subprocess
+
+ Args:
+ stream (_type_): _description_
+ is_error_stream (bool): _description_
+ """
for line in iter(stream.readline, ""):
if self.debug_mode:
print(f"Received output line:\n{line}\n---")
@@ -179,3 +204,12 @@ class SubprocessCodeInterpreter(BaseCodeInterpreter):
self.done.set()
else:
self.output_queue.put({"output": line})
+
+
+# interpreter = SubprocessCodeInterpreter()
+# interpreter.start_cmd = "python3"
+# for output in interpreter.run("""
+# print("hello")
+# print("world")
+# """):
+# print(output)
diff --git a/swarms/utils/data_to_text.py b/swarms/utils/data_to_text.py
new file mode 100644
index 00000000..55f6e6cd
--- /dev/null
+++ b/swarms/utils/data_to_text.py
@@ -0,0 +1,78 @@
+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 md_to_text(file):
+ if not os.path.exists(file):
+ raise FileNotFoundError(
+ f"No such file or directory: '{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:
+ FileNotFoundError: If the file does not exist.
+ IOError: If there is an error reading the file.
+
+ Examples:
+ >>> data_to_text("data.csv")
+ 'This is the text representation of the data file.'
+
+ """
+ if not os.path.exists(file):
+ raise FileNotFoundError(f"File not found: {file}")
+
+ try:
+ _, ext = os.path.splitext(file)
+ ext = (
+ ext.lower()
+ ) # Convert extension to lowercase for case-insensitive comparison
+ 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)
+ elif ext == ".md":
+ return md_to_text(file)
+ else:
+ with open(file, "r") as file:
+ data = file.read()
+ return data
+ except Exception as e:
+ raise IOError(f"Error reading file: {file}") from e
diff --git a/swarms/utils/device_checker_cuda.py b/swarms/utils/device_checker_cuda.py
new file mode 100644
index 00000000..dbf2191c
--- /dev/null
+++ b/swarms/utils/device_checker_cuda.py
@@ -0,0 +1,70 @@
+import torch
+import logging
+from typing import Union, List, Any
+from torch.cuda import memory_allocated, memory_reserved
+
+
+def check_device(
+ log_level: Any = logging.INFO,
+ memory_threshold: float = 0.8,
+ capability_threshold: float = 3.5,
+ return_type: str = "list",
+) -> Union[torch.device, List[torch.device]]:
+ """
+ Checks for the availability of CUDA and returns the appropriate device(s).
+ If CUDA is not available, returns a CPU device.
+ If CUDA is available, returns a list of all available GPU devices.
+ """
+ logging.basicConfig(level=log_level)
+
+ # Check for CUDA availability
+ try:
+ if not torch.cuda.is_available():
+ logging.info("CUDA is not available. Using CPU...")
+ return torch.device("cpu")
+ except Exception as e:
+ logging.error("Error checking for CUDA availability: ", e)
+ return torch.device("cpu")
+
+ logging.info("CUDA is available.")
+
+ # Check for multiple GPUs
+ num_gpus = torch.cuda.device_count()
+ devices = []
+ if num_gpus > 1:
+ logging.info(f"Multiple GPUs available: {num_gpus}")
+ devices = [torch.device(f"cuda:{i}") for i in range(num_gpus)]
+ else:
+ logging.info("Only one GPU is available.")
+ devices = [torch.device("cuda")]
+
+ # Check additional properties for each device
+ for device in devices:
+ try:
+ torch.cuda.set_device(device)
+ capability = torch.cuda.get_device_capability(device)
+ total_memory = torch.cuda.get_device_properties(
+ device
+ ).total_memory
+ allocated_memory = memory_allocated(device)
+ reserved_memory = memory_reserved(device)
+ device_name = torch.cuda.get_device_name(device)
+
+ logging.info(
+ f"Device: {device}, Name: {device_name}, Compute"
+ f" Capability: {capability}, Total Memory:"
+ f" {total_memory}, Allocated Memory:"
+ f" {allocated_memory}, Reserved Memory:"
+ f" {reserved_memory}"
+ )
+ except Exception as e:
+ logging.error(
+ f"Error retrieving properties for device {device}: ",
+ e,
+ )
+
+ return devices
+
+
+# devices = check_device()
+# logging.info(f"Using device(s): {devices}")
diff --git a/swarms/utils/disable_logging.py b/swarms/utils/disable_logging.py
index 3b6884d2..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,6 +35,17 @@ def disable_logging():
"wandb.docker.auth",
]:
logger = logging.getLogger(logger_name)
- logger.setLevel(
- logging.WARNING
- ) # Supress 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")
+ 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)
diff --git a/swarms/utils/dist_utils.py b/swarms/utils/dist_utils.py
new file mode 100644
index 00000000..76d9e03c
--- /dev/null
+++ b/swarms/utils/dist_utils.py
@@ -0,0 +1,311 @@
+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/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/download_weights_from_url.py b/swarms/utils/download_weights_from_url.py
new file mode 100644
index 00000000..b5fa1633
--- /dev/null
+++ b/swarms/utils/download_weights_from_url.py
@@ -0,0 +1,22 @@
+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}")
diff --git a/swarms/utils/execution_sandbox.py b/swarms/utils/execution_sandbox.py
new file mode 100644
index 00000000..6c65e25a
--- /dev/null
+++ b/swarms/utils/execution_sandbox.py
@@ -0,0 +1,71 @@
+import asyncio
+import logging
+import traceback
+from typing import Tuple
+
+
+async def execute_code_async(code: str) -> Tuple[str, str]:
+ """
+ This function takes a string of code as input, adds some documentation to it,
+ and then attempts to execute the code asynchronously. If the code execution is successful,
+ the function returns the new code and an empty string. If the code execution
+ fails, the function returns the new code and the error message.
+
+ Args:
+ code (str): The original code.
+
+ Returns:
+ Tuple[str, str]: The new code with added documentation and the error message (if any).
+ """
+
+ # Validate the input
+ if not isinstance(code, str):
+ raise ValueError("The code must be a string.")
+
+ # Add some documentation to the code
+ documentation = """
+ '''
+ This code has been prepared for deployment in an execution sandbox.
+ '''
+ """
+
+ # Combine the documentation and the original code
+ new_code = documentation + "\n" + code
+
+ # Attempt to execute the code
+ error_message = ""
+ try:
+ # Use a secure environment to execute the code (e.g., a Docker container)
+ # This is just a placeholder and would require additional setup and dependencies
+ # exec_in_docker(new_code)
+ out = exec(new_code)
+ return out
+ # logging.info("Code executed successfully.")
+ except Exception:
+ error_message = traceback.format_exc()
+ logging.error(
+ "Code execution failed. Error: %s", error_message
+ )
+
+ # Return the new code and the error message
+ return out, error_message
+
+
+def execute_code_sandbox(
+ code: str, async_on: bool = False
+) -> Tuple[str, str]:
+ """
+ Executes the given code in a sandbox environment.
+
+ Args:
+ code (str): The code to be executed.
+ async_on (bool, optional): Indicates whether to execute the code asynchronously.
+ Defaults to False.
+
+ Returns:
+ Tuple[str, str]: A tuple containing the stdout and stderr outputs of the code execution.
+ """
+ if async_on:
+ return asyncio.run(execute_code_async(code))
+ else:
+ return execute_code_async(code)
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/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
diff --git a/swarms/utils/find_img_path.py b/swarms/utils/find_img_path.py
new file mode 100644
index 00000000..2ca5d082
--- /dev/null
+++ b/swarms/utils/find_img_path.py
@@ -0,0 +1,24 @@
+import os
+import re
+
+
+def find_image_path(text):
+ """Find the image path from the text
+
+ Args:
+ text (_type_): _description_
+
+ Returns:
+ _type_: _description_
+ """
+ pattern = r"([A-Za-z]:\\[^:\n]*?\.(png|jpg|jpeg|PNG|JPG|JPEG))|(/[^:\n]*?\.(png|jpg|jpeg|PNG|JPG|JPEG))"
+ matches = [
+ match.group()
+ for match in re.finditer(pattern, text)
+ if match.group()
+ ]
+ matches += [match.replace("\\", "") for match in matches if match]
+ existing_paths = [
+ match for match in matches if os.path.exists(match)
+ ]
+ return max(existing_paths, key=len) if existing_paths else None
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/swarms/utils/hash_utils.py b/swarms/utils/hash_utils.py
new file mode 100644
index 00000000..e69de29b
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
diff --git a/swarms/utils/json_output_parser.py b/swarms/utils/json_output_parser.py
new file mode 100644
index 00000000..724d5ed5
--- /dev/null
+++ b/swarms/utils/json_output_parser.py
@@ -0,0 +1,96 @@
+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}'."
+ f" 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/jsonl_utils.py b/swarms/utils/jsonl_utils.py
new file mode 100644
index 00000000..6f52caf5
--- /dev/null
+++ b/swarms/utils/jsonl_utils.py
@@ -0,0 +1,63 @@
+from typing import Iterable, Dict
+import gzip
+import json
+import os
+
+
+ROOT = os.path.dirname(os.path.abspath(__file__))
+
+
+def read_problems_from_jsonl(filename: str) -> Iterable[Dict]:
+ return {task["task_id"]: task for task in stream_jsonl(filename)}
+
+
+def stream_jsonl(filename: str) -> Iterable[Dict]:
+ """
+ Stream JSONL data from a file.
+
+ Args:
+ filename (str): The path to the JSONL file.
+
+ Yields:
+ Dict: A dictionary representing each JSON object in the file.
+ """
+ if filename.endswith(".gz"):
+ with open(filename, "rb") as gzfp:
+ with gzip.open(gzfp, "rt") as fp:
+ for line in fp:
+ if any(not x.isspace() for x in line):
+ yield json.loads(line)
+
+ else:
+ with open(filename, "r") as fp:
+ for line in fp:
+ if any(not x.isspace() for x in line):
+ yield json.loads(line)
+
+
+def write_jsonl(
+ filename: str, data: Iterable[Dict], append: bool = False
+):
+ """
+ Write a list of dictionaries to a JSONL file.
+
+ Args:
+ filename (str): The path to the output file.
+ data (Iterable[Dict]): The data to be written to the file.
+ append (bool, optional): If True, append to an existing file.
+ If False, overwrite the file. Defaults to False.
+ """
+ if append:
+ mode = "ab"
+ else:
+ mode = "wb"
+ filename = os.path.expanduser(filename)
+ if filename.endswith(".gz"):
+ with open(filename, mode) as fp:
+ with gzip.GzipFile(fileobj=fp, mode="wb") as gzfp:
+ for x in data:
+ gzfp.write(json.dumps(x) + "\n").encode("utf-8")
+ else:
+ with open(filename, mode) as fp:
+ for x in data:
+ fp.write((json.dumps(x) + "\n").encode("utf-8"))
diff --git a/swarms/utils/llm_metrics_decorator.py b/swarms/utils/llm_metrics_decorator.py
new file mode 100644
index 00000000..a915623a
--- /dev/null
+++ b/swarms/utils/llm_metrics_decorator.py
@@ -0,0 +1,39 @@
+import time
+from functools import wraps
+from typing import Callable
+
+
+def metrics_decorator(func: Callable):
+ """Metrics decorator for LLM
+
+ Args:
+ func (Callable): The function to decorate
+
+ Example:
+ >>> @metrics_decorator
+ >>> def my_function():
+ >>> return "Hello, world!"
+ >>> my_function()
+
+ """
+
+ @wraps(func)
+ def wrapper(self, *args, **kwargs):
+ # Time to First Token
+ start_time = time.time()
+ result = func(self, *args, **kwargs)
+ first_token_time = time.time()
+
+ # Generation Latency
+ end_time = time.time()
+
+ # Throughput (assuming the function returns a list of tokens)
+ throughput = len(result) / (end_time - start_time)
+
+ return f"""
+ Time to First Token: {first_token_time - start_time}
+ Generation Latency: {end_time - start_time}
+ Throughput: {throughput}
+ """
+
+ return wrapper
diff --git a/swarms/utils/load_model_torch.py b/swarms/utils/load_model_torch.py
new file mode 100644
index 00000000..53649e93
--- /dev/null
+++ b/swarms/utils/load_model_torch.py
@@ -0,0 +1,57 @@
+import torch
+from torch import nn
+
+
+def load_model_torch(
+ model_path: str = None,
+ device: torch.device = None,
+ model: nn.Module = None,
+ strict: bool = True,
+ map_location=None,
+ *args,
+ **kwargs,
+) -> nn.Module:
+ """
+ Load a PyTorch model from a given path and move it to the specified device.
+
+ Args:
+ model_path (str): Path to the saved model file.
+ device (torch.device): Device to move the model to.
+ model (nn.Module): The model architecture, if the model file only contains the state dictionary.
+ strict (bool): Whether to strictly enforce that the keys in the state dictionary match the keys returned by the model's `state_dict()` function.
+ map_location (callable): A function to remap the storage locations of the loaded model.
+ *args: Additional arguments to pass to `torch.load`.
+ **kwargs: Additional keyword arguments to pass to `torch.load`.
+
+ Returns:
+ nn.Module: The loaded model.
+
+ Raises:
+ FileNotFoundError: If the model file is not found.
+ RuntimeError: If there is an error while loading the model.
+ """
+ if device is None:
+ device = torch.device(
+ "cuda" if torch.cuda.is_available() else "cpu"
+ )
+
+ try:
+ if model is None:
+ model = torch.load(
+ model_path, map_location=map_location, *args, **kwargs
+ )
+ else:
+ model.load_state_dict(
+ torch.load(
+ model_path,
+ map_location=map_location,
+ *args,
+ **kwargs,
+ ),
+ strict=strict,
+ )
+ return model.to(device)
+ except FileNotFoundError:
+ raise FileNotFoundError(f"Model file not found: {model_path}")
+ except RuntimeError as e:
+ raise RuntimeError(f"Error loading model: {str(e)}")
diff --git a/swarms/utils/logger.py b/swarms/utils/logger.py
index a0750d4c..804a4fb1 100644
--- a/swarms/utils/logger.py
+++ b/swarms/utils/logger.py
@@ -1,12 +1,82 @@
+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)
+
+
+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
+
+
+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)
diff --git a/swarms/utils/loggers.py b/swarms/utils/loggers.py
index a0dec94d..68477132 100644
--- a/swarms/utils/loggers.py
+++ b/swarms/utils/loggers.py
@@ -487,21 +487,21 @@ def print_action_base(action: Action):
"""
if action.content != "":
logger.typewriter_log(
- f"content:", Fore.YELLOW, f"{action.content}"
+ "content:", Fore.YELLOW, f"{action.content}"
)
logger.typewriter_log(
- f"Thought:", Fore.YELLOW, f"{action.thought}"
+ "Thought:", Fore.YELLOW, f"{action.thought}"
)
if len(action.plan) > 0:
logger.typewriter_log(
- f"Plan:",
+ "Plan:",
Fore.YELLOW,
)
for line in action.plan:
line = line.lstrip("- ")
logger.typewriter_log("- ", Fore.GREEN, line.strip())
logger.typewriter_log(
- f"Criticism:", Fore.YELLOW, f"{action.criticism}"
+ "Criticism:", Fore.YELLOW, f"{action.criticism}"
)
@@ -515,15 +515,15 @@ def print_action_tool(action: Action):
Returns:
None
"""
- logger.typewriter_log(f"Tool:", Fore.BLUE, f"{action.tool_name}")
+ logger.typewriter_log("Tool:", Fore.BLUE, f"{action.tool_name}")
logger.typewriter_log(
- f"Tool Input:", Fore.BLUE, f"{action.tool_input}"
+ "Tool Input:", Fore.BLUE, f"{action.tool_input}"
)
output = (
action.tool_output if action.tool_output != "" else "None"
)
- logger.typewriter_log(f"Tool Output:", Fore.BLUE, f"{output}")
+ logger.typewriter_log("Tool Output:", Fore.BLUE, f"{output}")
color = Fore.RED
if action.tool_output_status == ToolCallStatus.ToolCallSuccess:
@@ -534,7 +534,7 @@ def print_action_tool(action: Action):
color = Fore.YELLOW
logger.typewriter_log(
- f"Tool Call Status:",
+ "Tool Call Status:",
Fore.BLUE,
f"{color}{action.tool_output_status.name}{Style.RESET_ALL}",
)
diff --git a/swarms/utils/main.py b/swarms/utils/main.py
index c9c0f380..b94fae11 100644
--- a/swarms/utils/main.py
+++ b/swarms/utils/main.py
@@ -9,7 +9,6 @@ from typing import Dict
import boto3
import numpy as np
-import pandas as pd
import requests
diff --git a/swarms/utils/markdown_message.py b/swarms/utils/markdown_message.py
index 0fe9c2c0..57cd285f 100644
--- a/swarms/utils/markdown_message.py
+++ b/swarms/utils/markdown_message.py
@@ -1,23 +1,27 @@
-from rich import print as rich_print
+from rich.console import Console
from rich.markdown import Markdown
from rich.rule import Rule
-def display_markdown_message(message: str):
+def display_markdown_message(message: str, color: str = "cyan"):
"""
Display markdown message. Works with multiline strings with lots of indentation.
Will automatically make single line > tags beautiful.
"""
+ console = Console()
for line in message.split("\n"):
line = line.strip()
if line == "":
- print("")
+ console.print("")
elif line == "---":
- rich_print(Rule(style="white"))
+ console.print(Rule(style=color))
else:
- rich_print(Markdown(line))
+ console.print(Markdown(line, style=color))
if "\n" not in message and message.startswith(">"):
# Aesthetic choice. For these tags, they need a space below them
- print("")
+ console.print("")
+
+
+# display_markdown_message("I love you and you are beautiful.", "cyan")
diff --git a/swarms/utils/math_eval.py b/swarms/utils/math_eval.py
new file mode 100644
index 00000000..6dbff2b5
--- /dev/null
+++ b/swarms/utils/math_eval.py
@@ -0,0 +1,61 @@
+import functools
+import logging
+
+
+def math_eval(func1, func2):
+ """Math evaluation decorator.
+
+ Args:
+ func1 (_type_): _description_
+ func2 (_type_): _description_
+
+ Example:
+ >>> @math_eval(ground_truth, generated_func)
+ >>> def test_func(x):
+ >>> return x
+ >>> result1, result2 = test_func(5)
+ >>> print(f"Result from ground_truth: {result1}")
+ >>> print(f"Result from generated_func: {result2}")
+
+ """
+
+ def decorator(func):
+ @functools.wraps(func)
+ def wrapper(*args, **kwargs):
+ try:
+ result1 = func1(*args, **kwargs)
+ except Exception as e:
+ logging.error(f"Error in func1: {e}")
+ result1 = None
+
+ try:
+ result2 = func2(*args, **kwargs)
+ except Exception as e:
+ logging.error(f"Error in func2: {e}")
+ result2 = None
+
+ if result1 != result2:
+ logging.warning(
+ f"Outputs do not match: {result1} != {result2}"
+ )
+
+ return result1, result2
+
+ return wrapper
+
+ return decorator
+
+
+# def ground_truth(x):
+# return x * 2
+
+# def generated_func(x):
+# return x - 10
+
+# @math_eval(ground_truth, generated_func)
+# def test_func(x):
+# return x
+
+# result1, result2 = test_func(5)
+# print(f"Result from ground_truth: {result1}")
+# print(f"Result from generated_func: {result2}")
diff --git a/swarms/utils/parse_code.py b/swarms/utils/parse_code.py
index 2e0fa438..838d5868 100644
--- a/swarms/utils/parse_code.py
+++ b/swarms/utils/parse_code.py
@@ -1,31 +1,19 @@
import re
-# def extract_code_in_backticks_in_string(s: str) -> str:
-# """
-# Extracts code blocks from a markdown string.
-# Args:
-# s (str): The markdown string to extract code from.
-
-# Returns:
-# list: A list of tuples. Each tuple contains the language of the code block (if specified) and the code itself.
-# """
-# pattern = r"```([\w\+\#\-\.\s]*)\n(.*?)```"
-# matches = re.findall(pattern, s, re.DOTALL)
-# out = [(match[0], match[1].strip()) for match in matches]
-# print(out)
-
-
-def extract_code_in_backticks_in_string(s: str) -> str:
+def extract_code_from_markdown(markdown_content: str):
"""
- Extracts code blocks from a markdown string.
+ Extracts code blocks from a Markdown string and returns them as a single string.
Args:
- s (str): The markdown string to extract code from.
+ - markdown_content (str): The Markdown content as a string.
Returns:
- str: A string containing all the code blocks.
+ - str: A single string containing all the code blocks separated by newlines.
"""
- pattern = r"```([\w\+\#\-\.\s]*)(.*?)```"
- matches = re.findall(pattern, s, re.DOTALL)
- return "\n".join(match[1].strip() for match in matches)
+ # Regular expression for fenced code blocks
+ pattern = r"```(?:\w+\n)?(.*?)```"
+ matches = re.findall(pattern, markdown_content, re.DOTALL)
+
+ # Concatenate all code blocks separated by newlines
+ return "\n".join(code.strip() for code in matches)
diff --git a/swarms/utils/pdf_to_text.py b/swarms/utils/pdf_to_text.py
index 35309dd3..6d589ad5 100644
--- a/swarms/utils/pdf_to_text.py
+++ b/swarms/utils/pdf_to_text.py
@@ -1,5 +1,4 @@
import sys
-import os
try:
import PyPDF2
diff --git a/swarms/utils/phoenix_handler.py b/swarms/utils/phoenix_handler.py
deleted file mode 100644
index 329d3f2a..00000000
--- a/swarms/utils/phoenix_handler.py
+++ /dev/null
@@ -1,52 +0,0 @@
-import functools
-import traceback
-import phoenix as px
-
-
-def phoenix_trace_decorator(doc_string):
- """Phoenix trace decorator.
-
-
- Args:
- doc_string (_type_): doc string for the function
-
-
- Example:
- >>> @phoenix_trace_decorator(
- >>> "This is a doc string"
- >>> )
- >>> def test_function():
- >>> print("Hello world")
- >>>
- >>> test_function()
-
-
- # Example of using the decorator
- @phoenix_trace_decorator("This function does XYZ and is traced by Phoenix.")
- def my_function(param1, param2):
- # Function implementation
- pass
- """
-
- def decorator(func):
- @functools.wraps(func)
- def wrapper(*args, **kwargs):
- # Start phoenix session for tracing
- session = px.active_session() or px.launch_app()
-
- try:
- # Attempt to execute the function
- result = func(*args, **kwargs)
- return result
- except Exception as error:
- error_info = traceback.format_exc()
- session.trace_exception(
- exception=error, error_info=error_info
- )
- raise
-
- # Atteach docs to wrapper func
- wrapper.__doc__ = doc_string
- return wrapper
-
- return decorator
diff --git a/swarms/utils/prep_torch_model_inference.py b/swarms/utils/prep_torch_model_inference.py
new file mode 100644
index 00000000..41bc07cc
--- /dev/null
+++ b/swarms/utils/prep_torch_model_inference.py
@@ -0,0 +1,30 @@
+import torch
+from swarms.utils.load_model_torch import load_model_torch
+
+
+def prep_torch_inference(
+ model_path: str = None,
+ device: torch.device = None,
+ *args,
+ **kwargs,
+):
+ """
+ Prepare a Torch model for inference.
+
+ Args:
+ model_path (str): Path to the model file.
+ device (torch.device): Device to run the model on.
+ *args: Additional positional arguments.
+ **kwargs: Additional keyword arguments.
+
+ Returns:
+ torch.nn.Module: The prepared model.
+ """
+ try:
+ model = load_model_torch(model_path, device)
+ model.eval()
+ return model
+ except Exception as e:
+ # Add error handling code here
+ print(f"Error occurred while preparing Torch model: {e}")
+ return None
diff --git a/swarms/utils/remove_json_whitespace.py b/swarms/utils/remove_json_whitespace.py
new file mode 100644
index 00000000..a5b3f7de
--- /dev/null
+++ b/swarms/utils/remove_json_whitespace.py
@@ -0,0 +1,50 @@
+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))
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
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
diff --git a/swarms/utils/try_except_wrapper.py b/swarms/utils/try_except_wrapper.py
new file mode 100644
index 00000000..1f0b431d
--- /dev/null
+++ b/swarms/utils/try_except_wrapper.py
@@ -0,0 +1,45 @@
+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,
+ prints an error message, and returns None.
+ It also prints a message indicating the exit of the function.
+
+ Args:
+ func (function): The function to be wrapped.
+
+ 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):
+ try:
+ result = func(*args, **kwargs)
+ return result
+ except Exception as error:
+ 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__}")
+
+ return wrapper
diff --git a/swarms/utils/yaml_output_parser.py b/swarms/utils/yaml_output_parser.py
new file mode 100644
index 00000000..61be311b
--- /dev/null
+++ b/swarms/utils/yaml_output_parser.py
@@ -0,0 +1,89 @@
+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}'."
+ f" 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
diff --git a/tests/agents/test_tool_agent.py b/tests/agents/test_tool_agent.py
new file mode 100644
index 00000000..691489c0
--- /dev/null
+++ b/tests/agents/test_tool_agent.py
@@ -0,0 +1,101 @@
+from unittest.mock import Mock, patch
+
+from transformers import AutoModelForCausalLM, AutoTokenizer
+
+from swarms import ToolAgent
+
+
+def test_tool_agent_init():
+ model = Mock(spec=AutoModelForCausalLM)
+ tokenizer = Mock(spec=AutoTokenizer)
+ json_schema = {
+ "type": "object",
+ "properties": {
+ "name": {"type": "string"},
+ "age": {"type": "number"},
+ "is_student": {"type": "boolean"},
+ "courses": {"type": "array", "items": {"type": "string"}},
+ },
+ }
+ name = "Test Agent"
+ description = "This is a test agent"
+
+ agent = ToolAgent(
+ name, description, model, tokenizer, json_schema
+ )
+
+ assert agent.name == name
+ assert agent.description == description
+ assert agent.model == model
+ assert agent.tokenizer == tokenizer
+ assert agent.json_schema == json_schema
+
+
+@patch.object(ToolAgent, "run")
+def test_tool_agent_run(mock_run):
+ model = Mock(spec=AutoModelForCausalLM)
+ tokenizer = Mock(spec=AutoTokenizer)
+ json_schema = {
+ "type": "object",
+ "properties": {
+ "name": {"type": "string"},
+ "age": {"type": "number"},
+ "is_student": {"type": "boolean"},
+ "courses": {"type": "array", "items": {"type": "string"}},
+ },
+ }
+ name = "Test Agent"
+ description = "This is a test agent"
+ task = (
+ "Generate a person's information based on the following"
+ " schema:"
+ )
+
+ agent = ToolAgent(
+ name, description, model, tokenizer, json_schema
+ )
+ agent.run(task)
+
+ mock_run.assert_called_once_with(task)
+
+
+def test_tool_agent_init_with_kwargs():
+ model = Mock(spec=AutoModelForCausalLM)
+ tokenizer = Mock(spec=AutoTokenizer)
+ json_schema = {
+ "type": "object",
+ "properties": {
+ "name": {"type": "string"},
+ "age": {"type": "number"},
+ "is_student": {"type": "boolean"},
+ "courses": {"type": "array", "items": {"type": "string"}},
+ },
+ }
+ name = "Test Agent"
+ description = "This is a test agent"
+
+ kwargs = {
+ "debug": True,
+ "max_array_length": 20,
+ "max_number_tokens": 12,
+ "temperature": 0.5,
+ "max_string_token_length": 20,
+ }
+
+ agent = ToolAgent(
+ name, description, model, tokenizer, json_schema, **kwargs
+ )
+
+ assert agent.name == name
+ assert agent.description == description
+ assert agent.model == model
+ assert agent.tokenizer == tokenizer
+ assert agent.json_schema == json_schema
+ assert agent.debug == kwargs["debug"]
+ assert agent.max_array_length == kwargs["max_array_length"]
+ assert agent.max_number_tokens == kwargs["max_number_tokens"]
+ assert agent.temperature == kwargs["temperature"]
+ assert (
+ agent.max_string_token_length
+ == kwargs["max_string_token_length"]
+ )
diff --git a/tests/memory/test_pinecone.py b/tests/memory/test_pinecone.py
index f43cd6ea..f385f058 100644
--- a/tests/memory/test_pinecone.py
+++ b/tests/memory/test_pinecone.py
@@ -1,6 +1,6 @@
import os
from unittest.mock import patch
-from swarms.memory.pinecone import PineconDB
+from swarms.memory.pinecone import PineconeDB
api_key = os.getenv("PINECONE_API_KEY") or ""
@@ -9,7 +9,7 @@ def test_init():
with patch("pinecone.init") as MockInit, patch(
"pinecone.Index"
) as MockIndex:
- store = PineconDB(
+ store = PineconeDB(
api_key=api_key,
index_name="test_index",
environment="test_env",
@@ -21,7 +21,7 @@ def test_init():
def test_upsert_vector():
with patch("pinecone.init"), patch("pinecone.Index") as MockIndex:
- store = PineconDB(
+ store = PineconeDB(
api_key=api_key,
index_name="test_index",
environment="test_env",
@@ -37,7 +37,7 @@ def test_upsert_vector():
def test_load_entry():
with patch("pinecone.init"), patch("pinecone.Index") as MockIndex:
- store = PineconDB(
+ store = PineconeDB(
api_key=api_key,
index_name="test_index",
environment="test_env",
@@ -48,7 +48,7 @@ def test_load_entry():
def test_load_entries():
with patch("pinecone.init"), patch("pinecone.Index") as MockIndex:
- store = PineconDB(
+ store = PineconeDB(
api_key=api_key,
index_name="test_index",
environment="test_env",
@@ -59,7 +59,7 @@ def test_load_entries():
def test_query():
with patch("pinecone.init"), patch("pinecone.Index") as MockIndex:
- store = PineconDB(
+ store = PineconeDB(
api_key=api_key,
index_name="test_index",
environment="test_env",
@@ -72,7 +72,7 @@ def test_create_index():
with patch("pinecone.init"), patch("pinecone.Index"), patch(
"pinecone.create_index"
) as MockCreateIndex:
- store = PineconDB(
+ store = PineconeDB(
api_key=api_key,
index_name="test_index",
environment="test_env",
diff --git a/tests/memory/test_pg.py b/tests/memory/test_pq_db.py
similarity index 51%
rename from tests/memory/test_pg.py
rename to tests/memory/test_pq_db.py
index 2bddfb27..5e44f0ba 100644
--- a/tests/memory/test_pg.py
+++ b/tests/memory/test_pq_db.py
@@ -1,55 +1,45 @@
-import pytest
+import os
from unittest.mock import patch
-from swarms.memory.pg import PgVectorVectorStore
+
from dotenv import load_dotenv
-import os
-load_dotenv()
+from swarms.memory.pg import PostgresDB
+load_dotenv()
PSG_CONNECTION_STRING = os.getenv("PSG_CONNECTION_STRING")
def test_init():
with patch("sqlalchemy.create_engine") as MockEngine:
- store = PgVectorVectorStore(
+ db = PostgresDB(
connection_string=PSG_CONNECTION_STRING,
table_name="test",
)
MockEngine.assert_called_once()
- assert store.engine == MockEngine.return_value
+ assert db.engine == MockEngine.return_value
-def test_init_exception():
- with pytest.raises(ValueError):
- PgVectorVectorStore(
- connection_string=(
- "mysql://root:password@localhost:3306/test"
- ),
- table_name="test",
- )
-
-
-def test_setup():
- with patch("sqlalchemy.create_engine") as MockEngine:
- store = PgVectorVectorStore(
+def test_create_vector_model():
+ with patch("sqlalchemy.create_engine"):
+ db = PostgresDB(
connection_string=PSG_CONNECTION_STRING,
table_name="test",
)
- store.setup()
- MockEngine.execute.assert_called()
+ model = db._create_vector_model()
+ assert model.__tablename__ == "test"
-def test_upsert_vector():
+def test_add_or_update_vector():
with patch("sqlalchemy.create_engine"), patch(
"sqlalchemy.orm.Session"
) as MockSession:
- store = PgVectorVectorStore(
+ db = PostgresDB(
connection_string=PSG_CONNECTION_STRING,
table_name="test",
)
- store.upsert_vector(
- [1.0, 2.0, 3.0],
+ db.add_or_update_vector(
+ "test_vector",
"test_id",
"test_namespace",
{"meta": "data"},
@@ -59,45 +49,32 @@ def test_upsert_vector():
MockSession.return_value.commit.assert_called()
-def test_load_entry():
+def test_query_vectors():
with patch("sqlalchemy.create_engine"), patch(
"sqlalchemy.orm.Session"
) as MockSession:
- store = PgVectorVectorStore(
+ db = PostgresDB(
connection_string=PSG_CONNECTION_STRING,
table_name="test",
)
- store.load_entry("test_id", "test_namespace")
- MockSession.assert_called()
- MockSession.return_value.get.assert_called()
-
-
-def test_load_entries():
- with patch("sqlalchemy.create_engine"), patch(
- "sqlalchemy.orm.Session"
- ) as MockSession:
- store = PgVectorVectorStore(
- connection_string=PSG_CONNECTION_STRING,
- table_name="test",
- )
- store.load_entries("test_namespace")
+ db.query_vectors("test_query", "test_namespace")
MockSession.assert_called()
MockSession.return_value.query.assert_called()
MockSession.return_value.query.return_value.filter_by.assert_called()
+ MockSession.return_value.query.return_value.filter.assert_called()
MockSession.return_value.query.return_value.all.assert_called()
-def test_query():
+def test_delete_vector():
with patch("sqlalchemy.create_engine"), patch(
"sqlalchemy.orm.Session"
) as MockSession:
- store = PgVectorVectorStore(
+ db = PostgresDB(
connection_string=PSG_CONNECTION_STRING,
table_name="test",
)
- store.query("test_query", 10, "test_namespace")
+ db.delete_vector("test_id")
MockSession.assert_called()
- MockSession.return_value.query.assert_called()
- MockSession.return_value.query.return_value.filter_by.assert_called()
- MockSession.return_value.query.return_value.limit.assert_called()
- MockSession.return_value.query.return_value.all.assert_called()
+ MockSession.return_value.get.assert_called()
+ MockSession.return_value.delete.assert_called()
+ MockSession.return_value.commit.assert_called()
diff --git a/tests/memory/test_short_term_memory.py b/tests/memory/test_short_term_memory.py
index 32d5d008..0b66b749 100644
--- a/tests/memory/test_short_term_memory.py
+++ b/tests/memory/test_short_term_memory.py
@@ -1,7 +1,7 @@
-import pytest
from swarms.memory.short_term_memory import ShortTermMemory
import threading
+
def test_init():
memory = ShortTermMemory()
assert memory.short_term_memory == []
@@ -108,16 +108,21 @@ def test_return_medium_memory_as_str():
def test_thread_safety():
memory = ShortTermMemory()
+
def add_messages():
for _ in range(1000):
memory.add("user", "Hello, world!")
- threads = [threading.Thread(target=add_messages) for _ in range(10)]
+
+ threads = [
+ threading.Thread(target=add_messages) for _ in range(10)
+ ]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
assert len(memory.get_short_term()) == 10000
+
def test_save_and_load():
memory1 = ShortTermMemory()
memory1.add("user", "Hello, world!")
@@ -125,4 +130,4 @@ def test_save_and_load():
memory2 = ShortTermMemory()
memory2.load_from_file("memory.json")
assert memory1.get_short_term() == memory2.get_short_term()
- assert memory1.get_medium_term() == memory2.get_medium_term()
\ No newline at end of file
+ assert memory1.get_medium_term() == memory2.get_medium_term()
diff --git a/tests/memory/test_sqlite.py b/tests/memory/test_sqlite.py
new file mode 100644
index 00000000..6b4213b0
--- /dev/null
+++ b/tests/memory/test_sqlite.py
@@ -0,0 +1,104 @@
+import pytest
+import sqlite3
+from swarms.memory.sqlite import SQLiteDB
+
+
+@pytest.fixture
+def db():
+ conn = sqlite3.connect(":memory:")
+ conn.execute(
+ "CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)"
+ )
+ conn.commit()
+ return SQLiteDB(":memory:")
+
+
+def test_add(db):
+ db.add("INSERT INTO test (name) VALUES (?)", ("test",))
+ result = db.query("SELECT * FROM test")
+ assert result == [(1, "test")]
+
+
+def test_delete(db):
+ db.add("INSERT INTO test (name) VALUES (?)", ("test",))
+ db.delete("DELETE FROM test WHERE name = ?", ("test",))
+ result = db.query("SELECT * FROM test")
+ assert result == []
+
+
+def test_update(db):
+ db.add("INSERT INTO test (name) VALUES (?)", ("test",))
+ db.update(
+ "UPDATE test SET name = ? WHERE name = ?", ("new", "test")
+ )
+ result = db.query("SELECT * FROM test")
+ assert result == [(1, "new")]
+
+
+def test_query(db):
+ db.add("INSERT INTO test (name) VALUES (?)", ("test",))
+ result = db.query("SELECT * FROM test WHERE name = ?", ("test",))
+ assert result == [(1, "test")]
+
+
+def test_execute_query(db):
+ db.add("INSERT INTO test (name) VALUES (?)", ("test",))
+ result = db.execute_query(
+ "SELECT * FROM test WHERE name = ?", ("test",)
+ )
+ assert result == [(1, "test")]
+
+
+def test_add_without_params(db):
+ with pytest.raises(sqlite3.ProgrammingError):
+ db.add("INSERT INTO test (name) VALUES (?)")
+
+
+def test_delete_without_params(db):
+ with pytest.raises(sqlite3.ProgrammingError):
+ db.delete("DELETE FROM test WHERE name = ?")
+
+
+def test_update_without_params(db):
+ with pytest.raises(sqlite3.ProgrammingError):
+ db.update("UPDATE test SET name = ? WHERE name = ?")
+
+
+def test_query_without_params(db):
+ with pytest.raises(sqlite3.ProgrammingError):
+ db.query("SELECT * FROM test WHERE name = ?")
+
+
+def test_execute_query_without_params(db):
+ with pytest.raises(sqlite3.ProgrammingError):
+ db.execute_query("SELECT * FROM test WHERE name = ?")
+
+
+def test_add_with_wrong_query(db):
+ with pytest.raises(sqlite3.OperationalError):
+ db.add("INSERT INTO wrong (name) VALUES (?)", ("test",))
+
+
+def test_delete_with_wrong_query(db):
+ with pytest.raises(sqlite3.OperationalError):
+ db.delete("DELETE FROM wrong WHERE name = ?", ("test",))
+
+
+def test_update_with_wrong_query(db):
+ with pytest.raises(sqlite3.OperationalError):
+ db.update(
+ "UPDATE wrong SET name = ? WHERE name = ?",
+ ("new", "test"),
+ )
+
+
+def test_query_with_wrong_query(db):
+ with pytest.raises(sqlite3.OperationalError):
+ db.query("SELECT * FROM wrong WHERE name = ?", ("test",))
+
+
+def test_execute_query_with_wrong_query(db):
+ with pytest.raises(sqlite3.OperationalError):
+ db.execute_query(
+ "SELECT * FROM wrong WHERE name = ?", ("test",)
+ )
diff --git a/tests/memory/test_weaviate.py b/tests/memory/test_weaviate.py
index 09dc6d45..f9e61c8f 100644
--- a/tests/memory/test_weaviate.py
+++ b/tests/memory/test_weaviate.py
@@ -1,12 +1,12 @@
import pytest
from unittest.mock import Mock, patch
-from swarms.memory.weaviate import WeaviateClient
+from swarms.memory import WeaviateDB
-# Define fixture for a WeaviateClient instance with mocked methods
+# Define fixture for a WeaviateDB instance with mocked methods
@pytest.fixture
def weaviate_client_mock():
- client = WeaviateClient(
+ client = WeaviateDB(
http_host="mock_host",
http_port="mock_port",
http_secure=False,
@@ -31,7 +31,7 @@ def weaviate_client_mock():
return client
-# Define tests for the WeaviateClient class
+# Define tests for the WeaviateDB class
def test_create_collection(weaviate_client_mock):
# Test creating a collection
weaviate_client_mock.create_collection(
diff --git a/tests/models/test_LLM.py b/tests/models/test_LLM.py
deleted file mode 100644
index 04d6a5f2..00000000
--- a/tests/models/test_LLM.py
+++ /dev/null
@@ -1,55 +0,0 @@
-import unittest
-import os
-from unittest.mock import patch
-from langchain import HuggingFaceHub, ChatOpenAI
-
-from swarms.models.llm import LLM
-
-
-class TestLLM(unittest.TestCase):
- @patch.object(HuggingFaceHub, "__init__", return_value=None)
- @patch.object(ChatOpenAI, "__init__", return_value=None)
- def setUp(self, mock_hf_init, mock_openai_init):
- self.llm_openai = LLM(openai_api_key="mock_openai_key")
- self.llm_hf = LLM(
- hf_repo_id="mock_repo_id", hf_api_token="mock_hf_token"
- )
- self.prompt = "Who won the FIFA World Cup in 1998?"
-
- def test_init(self):
- self.assertEqual(
- self.llm_openai.openai_api_key, "mock_openai_key"
- )
- self.assertEqual(self.llm_hf.hf_repo_id, "mock_repo_id")
- self.assertEqual(self.llm_hf.hf_api_token, "mock_hf_token")
-
- @patch.object(HuggingFaceHub, "run", return_value="France")
- @patch.object(ChatOpenAI, "run", return_value="France")
- def test_run(self, mock_hf_run, mock_openai_run):
- result_openai = self.llm_openai.run(self.prompt)
- mock_openai_run.assert_called_once()
- self.assertEqual(result_openai, "France")
-
- result_hf = self.llm_hf.run(self.prompt)
- mock_hf_run.assert_called_once()
- self.assertEqual(result_hf, "France")
-
- def test_error_on_no_keys(self):
- with self.assertRaises(ValueError):
- LLM()
-
- @patch.object(os, "environ", {})
- def test_error_on_missing_hf_token(self):
- with self.assertRaises(ValueError):
- LLM(hf_repo_id="mock_repo_id")
-
- @patch.dict(
- os.environ, {"HUGGINGFACEHUB_API_TOKEN": "mock_hf_token"}
- )
- def test_hf_token_from_env(self):
- llm = LLM(hf_repo_id="mock_repo_id")
- self.assertEqual(llm.hf_api_token, "mock_hf_token")
-
-
-if __name__ == "__main__":
- unittest.main()
diff --git a/tests/models/test_ada.py b/tests/models/test_ada.py
deleted file mode 100644
index 43895e79..00000000
--- a/tests/models/test_ada.py
+++ /dev/null
@@ -1,91 +0,0 @@
-# test_embeddings.py
-
-import pytest
-import openai
-from unittest.mock import patch
-from swarms.models.simple_ada import (
- get_ada_embeddings,
-) # Adjust this import path to your project structure
-from os import getenv
-from dotenv import load_dotenv
-
-load_dotenv()
-
-
-# Fixture for test texts
-@pytest.fixture
-def test_texts():
- return [
- "Hello World",
- "This is a test string with newline\ncharacters",
- "A quick brown fox jumps over the lazy dog",
- ]
-
-
-# Basic Test
-def test_get_ada_embeddings_basic(test_texts):
- with patch("openai.resources.Embeddings.create") as mock_create:
- # Mocking the OpenAI API call
- mock_create.return_value = {
- "data": [{"embedding": [0.1, 0.2, 0.3]}]
- }
-
- for text in test_texts:
- embedding = get_ada_embeddings(text)
- assert embedding == [
- 0.1,
- 0.2,
- 0.3,
- ], "Embedding does not match expected output"
- mock_create.assert_called_with(
- input=[text.replace("\n", " ")],
- model="text-embedding-ada-002",
- )
-
-
-# Parameterized Test
-@pytest.mark.parametrize(
- "text, model, expected_call_model",
- [
- (
- "Hello World",
- "text-embedding-ada-002",
- "text-embedding-ada-002",
- ),
- (
- "Hello World",
- "text-embedding-ada-001",
- "text-embedding-ada-001",
- ),
- ],
-)
-def test_get_ada_embeddings_models(text, model, expected_call_model):
- with patch("openai.resources.Embeddings.create") as mock_create:
- mock_create.return_value = {
- "data": [{"embedding": [0.1, 0.2, 0.3]}]
- }
-
- _ = get_ada_embeddings(text, model=model)
- mock_create.assert_called_with(
- input=[text], model=expected_call_model
- )
-
-
-# Exception Test
-def test_get_ada_embeddings_exception():
- with patch("openai.resources.Embeddings.create") as mock_create:
- mock_create.side_effect = openai.OpenAIError("Test error")
- with pytest.raises(openai.OpenAIError):
- get_ada_embeddings("Some text")
-
-
-# Tests for environment variable loading
-def test_env_var_loading(monkeypatch):
- monkeypatch.setenv("OPENAI_API_KEY", "testkey123")
- with patch("openai.resources.Embeddings.create"):
- assert (
- getenv("OPENAI_API_KEY") == "testkey123"
- ), "Environment variable for API key is not set correctly"
-
-
-# ... more tests to cover other aspects such as different input types, large inputs, invalid inputs, etc.
diff --git a/tests/models/test_auto_temp.py b/tests/models/test_auto_temp.py
deleted file mode 100644
index 7937d0dc..00000000
--- a/tests/models/test_auto_temp.py
+++ /dev/null
@@ -1,83 +0,0 @@
-import os
-from concurrent.futures import ThreadPoolExecutor
-from unittest.mock import patch
-
-import pytest
-from dotenv import load_dotenv
-
-from swarms.models.autotemp import AutoTempAgent
-
-api_key = os.getenv("OPENAI_API_KEY")
-
-load_dotenv()
-
-
-@pytest.fixture
-def auto_temp_agent():
- return AutoTempAgent(api_key=api_key)
-
-
-def test_initialization(auto_temp_agent):
- assert isinstance(auto_temp_agent, AutoTempAgent)
- assert auto_temp_agent.auto_select is True
- assert auto_temp_agent.max_workers == 6
- assert auto_temp_agent.temperature == 0.5
- assert auto_temp_agent.alt_temps == [0.4, 0.6, 0.8, 1.0, 1.2, 1.4]
-
-
-def test_evaluate_output(auto_temp_agent):
- output = "This is a test output."
- with patch("swarms.models.OpenAIChat") as MockOpenAIChat:
- mock_instance = MockOpenAIChat.return_value
- mock_instance.return_value = "Score: 95.5"
- score = auto_temp_agent.evaluate_output(output)
- assert score == 95.5
- mock_instance.assert_called_once()
-
-
-def test_run_auto_select(auto_temp_agent):
- task = "Generate a blog post."
- temperature_string = "0.4,0.6,0.8,1.0,1.2,1.4"
- result = auto_temp_agent.run(task, temperature_string)
- assert "Best AutoTemp Output" in result
- assert "Temp" in result
- assert "Score" in result
-
-
-def test_run_no_scores(auto_temp_agent):
- task = "Invalid task."
- temperature_string = "0.4,0.6,0.8,1.0,1.2,1.4"
- with ThreadPoolExecutor(
- max_workers=auto_temp_agent.max_workers
- ) as executor:
- with patch.object(
- executor,
- "submit",
- side_effect=[None, None, None, None, None, None],
- ):
- result = auto_temp_agent.run(task, temperature_string)
- assert result == "No valid outputs generated."
-
-
-def test_run_manual_select(auto_temp_agent):
- auto_temp_agent.auto_select = False
- task = "Generate a blog post."
- temperature_string = "0.4,0.6,0.8,1.0,1.2,1.4"
- result = auto_temp_agent.run(task, temperature_string)
- assert "Best AutoTemp Output" not in result
- assert "Temp" in result
- assert "Score" in result
-
-
-def test_failed_initialization():
- with pytest.raises(Exception):
- AutoTempAgent()
-
-
-def test_failed_evaluate_output(auto_temp_agent):
- output = "This is a test output."
- with patch("swarms.models.OpenAIChat") as MockOpenAIChat:
- mock_instance = MockOpenAIChat.return_value
- mock_instance.return_value = "Invalid score text"
- score = auto_temp_agent.evaluate_output(output)
- assert score == 0.0
diff --git a/tests/models/test_bioclip.py b/tests/models/test_bioclip.py
deleted file mode 100644
index 99e1e343..00000000
--- a/tests/models/test_bioclip.py
+++ /dev/null
@@ -1,171 +0,0 @@
-# Import necessary modules and define fixtures if needed
-import os
-import pytest
-import torch
-from PIL import Image
-from swarms.models.bioclip import BioClip
-
-
-# Define fixtures if needed
-@pytest.fixture
-def sample_image_path():
- return "path_to_sample_image.jpg"
-
-
-@pytest.fixture
-def clip_instance():
- return BioClip(
- "microsoft/BiomedCLIP-PubMedBERT_256-vit_base_patch16_224"
- )
-
-
-# Basic tests for the BioClip class
-def test_clip_initialization(clip_instance):
- assert isinstance(clip_instance.model, torch.nn.Module)
- assert hasattr(clip_instance, "model_path")
- assert hasattr(clip_instance, "preprocess_train")
- assert hasattr(clip_instance, "preprocess_val")
- assert hasattr(clip_instance, "tokenizer")
- assert hasattr(clip_instance, "device")
-
-
-def test_clip_call_method(clip_instance, sample_image_path):
- labels = [
- "adenocarcinoma histopathology",
- "brain MRI",
- "covid line chart",
- "squamous cell carcinoma histopathology",
- "immunohistochemistry histopathology",
- "bone X-ray",
- "chest X-ray",
- "pie chart",
- "hematoxylin and eosin histopathology",
- ]
- result = clip_instance(sample_image_path, labels)
- assert isinstance(result, dict)
- assert len(result) == len(labels)
-
-
-def test_clip_plot_image_with_metadata(
- clip_instance, sample_image_path
-):
- metadata = {
- "filename": "sample_image.jpg",
- "top_probs": {"label1": 0.75, "label2": 0.65},
- }
- clip_instance.plot_image_with_metadata(
- sample_image_path, metadata
- )
-
-
-# More test cases can be added to cover additional functionality and edge cases
-
-
-# Parameterized tests for different image and label combinations
-@pytest.mark.parametrize(
- "image_path, labels",
- [
- ("image1.jpg", ["label1", "label2"]),
- ("image2.jpg", ["label3", "label4"]),
- # Add more image and label combinations
- ],
-)
-def test_clip_parameterized_calls(clip_instance, image_path, labels):
- result = clip_instance(image_path, labels)
- assert isinstance(result, dict)
- assert len(result) == len(labels)
-
-
-# Test image preprocessing
-def test_clip_image_preprocessing(clip_instance, sample_image_path):
- image = Image.open(sample_image_path)
- processed_image = clip_instance.preprocess_val(image)
- assert isinstance(processed_image, torch.Tensor)
-
-
-# Test label tokenization
-def test_clip_label_tokenization(clip_instance):
- labels = ["label1", "label2"]
- tokenized_labels = clip_instance.tokenizer(labels)
- assert isinstance(tokenized_labels, torch.Tensor)
- assert tokenized_labels.shape[0] == len(labels)
-
-
-# More tests can be added to cover other methods and edge cases
-
-
-# End-to-end tests with actual images and labels
-def test_clip_end_to_end(clip_instance, sample_image_path):
- labels = [
- "adenocarcinoma histopathology",
- "brain MRI",
- "covid line chart",
- "squamous cell carcinoma histopathology",
- "immunohistochemistry histopathology",
- "bone X-ray",
- "chest X-ray",
- "pie chart",
- "hematoxylin and eosin histopathology",
- ]
- result = clip_instance(sample_image_path, labels)
- assert isinstance(result, dict)
- assert len(result) == len(labels)
-
-
-# Test label tokenization with long labels
-def test_clip_long_labels(clip_instance):
- labels = ["label" + str(i) for i in range(100)]
- tokenized_labels = clip_instance.tokenizer(labels)
- assert isinstance(tokenized_labels, torch.Tensor)
- assert tokenized_labels.shape[0] == len(labels)
-
-
-# Test handling of multiple image files
-def test_clip_multiple_images(clip_instance, sample_image_path):
- labels = ["label1", "label2"]
- image_paths = [sample_image_path, "image2.jpg"]
- results = clip_instance(image_paths, labels)
- assert isinstance(results, list)
- assert len(results) == len(image_paths)
- for result in results:
- assert isinstance(result, dict)
- assert len(result) == len(labels)
-
-
-# Test model inference performance
-def test_clip_inference_performance(
- clip_instance, sample_image_path, benchmark
-):
- labels = [
- "adenocarcinoma histopathology",
- "brain MRI",
- "covid line chart",
- "squamous cell carcinoma histopathology",
- "immunohistochemistry histopathology",
- "bone X-ray",
- "chest X-ray",
- "pie chart",
- "hematoxylin and eosin histopathology",
- ]
- result = benchmark(clip_instance, sample_image_path, labels)
- assert isinstance(result, dict)
- assert len(result) == len(labels)
-
-
-# Test different preprocessing pipelines
-def test_clip_preprocessing_pipelines(
- clip_instance, sample_image_path
-):
- labels = ["label1", "label2"]
- image = Image.open(sample_image_path)
-
- # Test preprocessing for training
- processed_image_train = clip_instance.preprocess_train(image)
- assert isinstance(processed_image_train, torch.Tensor)
-
- # Test preprocessing for validation
- processed_image_val = clip_instance.preprocess_val(image)
- assert isinstance(processed_image_val, torch.Tensor)
-
-
-# ...
diff --git a/tests/models/test_dalle3.py b/tests/models/test_dalle3.py
deleted file mode 100644
index 00ba7bc9..00000000
--- a/tests/models/test_dalle3.py
+++ /dev/null
@@ -1,454 +0,0 @@
-import os
-from unittest.mock import Mock
-
-import pytest
-from openai import OpenAIError
-from PIL import Image
-from termcolor import colored
-
-from swarms.models.dalle3 import Dalle3
-
-
-# Mocking the OpenAI client to avoid making actual API calls during testing
-@pytest.fixture
-def mock_openai_client():
- return Mock()
-
-
-@pytest.fixture
-def dalle3(mock_openai_client):
- return Dalle3(client=mock_openai_client)
-
-
-def test_dalle3_call_success(dalle3, mock_openai_client):
- # Arrange
- task = "A painting of a dog"
- expected_img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
- mock_openai_client.images.generate.return_value = Mock(
- data=[Mock(url=expected_img_url)]
- )
-
- # Act
- img_url = dalle3(task)
-
- # Assert
- assert img_url == expected_img_url
- mock_openai_client.images.generate.assert_called_once_with(
- prompt=task, n=4
- )
-
-
-def test_dalle3_call_failure(dalle3, mock_openai_client, capsys):
- # Arrange
- task = "Invalid task"
- expected_error_message = "Error running Dalle3: API Error"
-
- # Mocking OpenAIError
- mock_openai_client.images.generate.side_effect = OpenAIError(
- expected_error_message,
- http_status=500,
- error="Internal Server Error",
- )
-
- # Act and assert
- with pytest.raises(OpenAIError) as excinfo:
- dalle3(task)
-
- assert str(excinfo.value) == expected_error_message
- mock_openai_client.images.generate.assert_called_once_with(
- prompt=task, n=4
- )
-
- # Ensure the error message is printed in red
- captured = capsys.readouterr()
- assert colored(expected_error_message, "red") in captured.out
-
-
-def test_dalle3_create_variations_success(dalle3, mock_openai_client):
- # Arrange
- img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
- expected_variation_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_02ABCDE.png"
- mock_openai_client.images.create_variation.return_value = Mock(
- data=[Mock(url=expected_variation_url)]
- )
-
- # Act
- variation_img_url = dalle3.create_variations(img_url)
-
- # Assert
- assert variation_img_url == expected_variation_url
- mock_openai_client.images.create_variation.assert_called_once()
- _, kwargs = mock_openai_client.images.create_variation.call_args
- assert kwargs["img"] is not None
- assert kwargs["n"] == 4
- assert kwargs["size"] == "1024x1024"
-
-
-def test_dalle3_create_variations_failure(
- dalle3, mock_openai_client, capsys
-):
- # Arrange
- img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
- expected_error_message = "Error running Dalle3: API Error"
-
- # Mocking OpenAIError
- mock_openai_client.images.create_variation.side_effect = (
- OpenAIError(
- expected_error_message,
- http_status=500,
- error="Internal Server Error",
- )
- )
-
- # Act and assert
- with pytest.raises(OpenAIError) as excinfo:
- dalle3.create_variations(img_url)
-
- assert str(excinfo.value) == expected_error_message
- mock_openai_client.images.create_variation.assert_called_once()
-
- # Ensure the error message is printed in red
- captured = capsys.readouterr()
- assert colored(expected_error_message, "red") in captured.out
-
-
-def test_dalle3_read_img():
- # Arrange
- img_path = "test_image.png"
- img = Image.new("RGB", (512, 512))
-
- # Save the image temporarily
- img.save(img_path)
-
- # Act
- dalle3 = Dalle3()
- img_loaded = dalle3.read_img(img_path)
-
- # Assert
- assert isinstance(img_loaded, Image.Image)
-
- # Clean up
- os.remove(img_path)
-
-
-def test_dalle3_set_width_height():
- # Arrange
- img = Image.new("RGB", (512, 512))
- width = 256
- height = 256
-
- # Act
- dalle3 = Dalle3()
- img_resized = dalle3.set_width_height(img, width, height)
-
- # Assert
- assert img_resized.size == (width, height)
-
-
-def test_dalle3_convert_to_bytesio():
- # Arrange
- img = Image.new("RGB", (512, 512))
- expected_format = "PNG"
-
- # Act
- dalle3 = Dalle3()
- img_bytes = dalle3.convert_to_bytesio(img, format=expected_format)
-
- # Assert
- assert isinstance(img_bytes, bytes)
- assert img_bytes.startswith(b"\x89PNG")
-
-
-def test_dalle3_call_multiple_times(dalle3, mock_openai_client):
- # Arrange
- task = "A painting of a dog"
- expected_img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
- mock_openai_client.images.generate.return_value = Mock(
- data=[Mock(url=expected_img_url)]
- )
-
- # Act
- img_url1 = dalle3(task)
- img_url2 = dalle3(task)
-
- # Assert
- assert img_url1 == expected_img_url
- assert img_url2 == expected_img_url
- assert mock_openai_client.images.generate.call_count == 2
-
-
-def test_dalle3_call_with_large_input(dalle3, mock_openai_client):
- # Arrange
- task = "A" * 2048 # Input longer than API's limit
- expected_error_message = "Error running Dalle3: API Error"
- mock_openai_client.images.generate.side_effect = OpenAIError(
- expected_error_message,
- http_status=500,
- error="Internal Server Error",
- )
-
- # Act and assert
- with pytest.raises(OpenAIError) as excinfo:
- dalle3(task)
-
- assert str(excinfo.value) == expected_error_message
-
-
-def test_dalle3_create_variations_with_invalid_image_url(
- dalle3, mock_openai_client
-):
- # Arrange
- img_url = "https://invalid-image-url.com"
- expected_error_message = "Error running Dalle3: Invalid image URL"
-
- # Act and assert
- with pytest.raises(ValueError) as excinfo:
- dalle3.create_variations(img_url)
-
- assert str(excinfo.value) == expected_error_message
-
-
-def test_dalle3_set_width_height_invalid_dimensions(dalle3):
- # Arrange
- img = dalle3.read_img("test_image.png")
- width = 0
- height = -1
-
- # Act and assert
- with pytest.raises(ValueError):
- dalle3.set_width_height(img, width, height)
-
-
-def test_dalle3_convert_to_bytesio_invalid_format(dalle3):
- # Arrange
- img = dalle3.read_img("test_image.png")
- invalid_format = "invalid_format"
-
- # Act and assert
- with pytest.raises(ValueError):
- dalle3.convert_to_bytesio(img, format=invalid_format)
-
-
-def test_dalle3_call_with_retry(dalle3, mock_openai_client):
- # Arrange
- task = "A painting of a dog"
- expected_img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
-
- # Simulate a retry scenario
- mock_openai_client.images.generate.side_effect = [
- OpenAIError(
- "Temporary error",
- http_status=500,
- error="Internal Server Error",
- ),
- Mock(data=[Mock(url=expected_img_url)]),
- ]
-
- # Act
- img_url = dalle3(task)
-
- # Assert
- assert img_url == expected_img_url
- assert mock_openai_client.images.generate.call_count == 2
-
-
-def test_dalle3_create_variations_with_retry(
- dalle3, mock_openai_client
-):
- # Arrange
- img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
- expected_variation_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_02ABCDE.png"
-
- # Simulate a retry scenario
- mock_openai_client.images.create_variation.side_effect = [
- OpenAIError(
- "Temporary error",
- http_status=500,
- error="Internal Server Error",
- ),
- Mock(data=[Mock(url=expected_variation_url)]),
- ]
-
- # Act
- variation_img_url = dalle3.create_variations(img_url)
-
- # Assert
- assert variation_img_url == expected_variation_url
- assert mock_openai_client.images.create_variation.call_count == 2
-
-
-def test_dalle3_call_exception_logging(
- dalle3, mock_openai_client, capsys
-):
- # Arrange
- task = "A painting of a dog"
- expected_error_message = "Error running Dalle3: API Error"
-
- # Mocking OpenAIError
- mock_openai_client.images.generate.side_effect = OpenAIError(
- expected_error_message,
- http_status=500,
- error="Internal Server Error",
- )
-
- # Act
- with pytest.raises(OpenAIError):
- dalle3(task)
-
- # Assert that the error message is logged
- captured = capsys.readouterr()
- assert expected_error_message in captured.err
-
-
-def test_dalle3_create_variations_exception_logging(
- dalle3, mock_openai_client, capsys
-):
- # Arrange
- img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
- expected_error_message = "Error running Dalle3: API Error"
-
- # Mocking OpenAIError
- mock_openai_client.images.create_variation.side_effect = (
- OpenAIError(
- expected_error_message,
- http_status=500,
- error="Internal Server Error",
- )
- )
-
- # Act
- with pytest.raises(OpenAIError):
- dalle3.create_variations(img_url)
-
- # Assert that the error message is logged
- captured = capsys.readouterr()
- assert expected_error_message in captured.err
-
-
-def test_dalle3_read_img_invalid_path(dalle3):
- # Arrange
- invalid_img_path = "invalid_image_path.png"
-
- # Act and assert
- with pytest.raises(FileNotFoundError):
- dalle3.read_img(invalid_img_path)
-
-
-def test_dalle3_call_no_api_key():
- # Arrange
- task = "A painting of a dog"
- dalle3 = Dalle3(api_key=None)
- expected_error_message = (
- "Error running Dalle3: API Key is missing"
- )
-
- # Act and assert
- with pytest.raises(ValueError) as excinfo:
- dalle3(task)
-
- assert str(excinfo.value) == expected_error_message
-
-
-def test_dalle3_create_variations_no_api_key():
- # Arrange
- img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
- dalle3 = Dalle3(api_key=None)
- expected_error_message = (
- "Error running Dalle3: API Key is missing"
- )
-
- # Act and assert
- with pytest.raises(ValueError) as excinfo:
- dalle3.create_variations(img_url)
-
- assert str(excinfo.value) == expected_error_message
-
-
-def test_dalle3_call_with_retry_max_retries_exceeded(
- dalle3, mock_openai_client
-):
- # Arrange
- task = "A painting of a dog"
-
- # Simulate max retries exceeded
- mock_openai_client.images.generate.side_effect = OpenAIError(
- "Temporary error",
- http_status=500,
- error="Internal Server Error",
- )
-
- # Act and assert
- with pytest.raises(OpenAIError) as excinfo:
- dalle3(task)
-
- assert "Retry limit exceeded" in str(excinfo.value)
-
-
-def test_dalle3_create_variations_with_retry_max_retries_exceeded(
- dalle3, mock_openai_client
-):
- # Arrange
- img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
-
- # Simulate max retries exceeded
- mock_openai_client.images.create_variation.side_effect = (
- OpenAIError(
- "Temporary error",
- http_status=500,
- error="Internal Server Error",
- )
- )
-
- # Act and assert
- with pytest.raises(OpenAIError) as excinfo:
- dalle3.create_variations(img_url)
-
- assert "Retry limit exceeded" in str(excinfo.value)
-
-
-def test_dalle3_call_retry_with_success(dalle3, mock_openai_client):
- # Arrange
- task = "A painting of a dog"
- expected_img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
-
- # Simulate success after a retry
- mock_openai_client.images.generate.side_effect = [
- OpenAIError(
- "Temporary error",
- http_status=500,
- error="Internal Server Error",
- ),
- Mock(data=[Mock(url=expected_img_url)]),
- ]
-
- # Act
- img_url = dalle3(task)
-
- # Assert
- assert img_url == expected_img_url
- assert mock_openai_client.images.generate.call_count == 2
-
-
-def test_dalle3_create_variations_retry_with_success(
- dalle3, mock_openai_client
-):
- # Arrange
- img_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_01J9J5ZKJZJY9.png"
- expected_variation_url = "https://cdn.openai.com/dall-e/encoded/feats/feats_02ABCDE.png"
-
- # Simulate success after a retry
- mock_openai_client.images.create_variation.side_effect = [
- OpenAIError(
- "Temporary error",
- http_status=500,
- error="Internal Server Error",
- ),
- Mock(data=[Mock(url=expected_variation_url)]),
- ]
-
- # Act
- variation_img_url = dalle3.create_variations(img_url)
-
- # Assert
- assert variation_img_url == expected_variation_url
- assert mock_openai_client.images.create_variation.call_count == 2
diff --git a/tests/models/test_distill_whisper.py b/tests/models/test_distill_whisper.py
deleted file mode 100644
index 775bb896..00000000
--- a/tests/models/test_distill_whisper.py
+++ /dev/null
@@ -1,336 +0,0 @@
-import os
-import tempfile
-from functools import wraps
-from unittest.mock import AsyncMock, MagicMock, patch
-
-import numpy as np
-import pytest
-import torch
-from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
-
-from swarms.models.distilled_whisperx import (
- DistilWhisperModel,
- async_retry,
-)
-
-
-@pytest.fixture
-def distil_whisper_model():
- return DistilWhisperModel()
-
-
-def create_audio_file(
- data: np.ndarray, sample_rate: int, file_path: str
-):
- data.tofile(file_path)
- return file_path
-
-
-def test_initialization(distil_whisper_model):
- assert isinstance(distil_whisper_model, DistilWhisperModel)
- assert isinstance(distil_whisper_model.model, torch.nn.Module)
- assert isinstance(distil_whisper_model.processor, torch.nn.Module)
- assert distil_whisper_model.device in ["cpu", "cuda:0"]
-
-
-def test_transcribe_audio_file(distil_whisper_model):
- test_data = np.random.rand(
- 16000
- ) # Simulated audio data (1 second)
- with tempfile.NamedTemporaryFile(
- suffix=".wav", delete=False
- ) as audio_file:
- audio_file_path = create_audio_file(
- test_data, 16000, audio_file.name
- )
- transcription = distil_whisper_model.transcribe(
- audio_file_path
- )
- os.remove(audio_file_path)
-
- assert isinstance(transcription, str)
- assert transcription.strip() != ""
-
-
-@pytest.mark.asyncio
-async def test_async_transcribe_audio_file(distil_whisper_model):
- test_data = np.random.rand(
- 16000
- ) # Simulated audio data (1 second)
- with tempfile.NamedTemporaryFile(
- suffix=".wav", delete=False
- ) as audio_file:
- audio_file_path = create_audio_file(
- test_data, 16000, audio_file.name
- )
- transcription = await distil_whisper_model.async_transcribe(
- audio_file_path
- )
- os.remove(audio_file_path)
-
- assert isinstance(transcription, str)
- assert transcription.strip() != ""
-
-
-def test_transcribe_audio_data(distil_whisper_model):
- test_data = np.random.rand(
- 16000
- ) # Simulated audio data (1 second)
- transcription = distil_whisper_model.transcribe(
- test_data.tobytes()
- )
-
- assert isinstance(transcription, str)
- assert transcription.strip() != ""
-
-
-@pytest.mark.asyncio
-async def test_async_transcribe_audio_data(distil_whisper_model):
- test_data = np.random.rand(
- 16000
- ) # Simulated audio data (1 second)
- transcription = await distil_whisper_model.async_transcribe(
- test_data.tobytes()
- )
-
- assert isinstance(transcription, str)
- assert transcription.strip() != ""
-
-
-def test_real_time_transcribe(distil_whisper_model, capsys):
- test_data = np.random.rand(
- 16000 * 5
- ) # Simulated audio data (5 seconds)
- with tempfile.NamedTemporaryFile(
- suffix=".wav", delete=False
- ) as audio_file:
- audio_file_path = create_audio_file(
- test_data, 16000, audio_file.name
- )
-
- distil_whisper_model.real_time_transcribe(
- audio_file_path, chunk_duration=1
- )
-
- os.remove(audio_file_path)
-
- captured = capsys.readouterr()
- assert "Starting real-time transcription..." in captured.out
- assert "Chunk" in captured.out
-
-
-def test_real_time_transcribe_audio_file_not_found(
- distil_whisper_model, capsys
-):
- audio_file_path = "non_existent_audio.wav"
- distil_whisper_model.real_time_transcribe(
- audio_file_path, chunk_duration=1
- )
-
- captured = capsys.readouterr()
- assert "The audio file was not found." in captured.out
-
-
-@pytest.fixture
-def mock_async_retry():
- def _mock_async_retry(
- retries=3, exceptions=(Exception,), delay=1
- ):
- def decorator(func):
- @wraps(func)
- async def wrapper(*args, **kwargs):
- return await func(*args, **kwargs)
-
- return wrapper
-
- return decorator
-
- with patch(
- "distil_whisper_model.async_retry", new=_mock_async_retry()
- ):
- yield
-
-
-@pytest.mark.asyncio
-async def test_async_retry_decorator_success():
- async def mock_async_function():
- return "Success"
-
- decorated_function = async_retry()(mock_async_function)
- result = await decorated_function()
- assert result == "Success"
-
-
-@pytest.mark.asyncio
-async def test_async_retry_decorator_failure():
- async def mock_async_function():
- raise Exception("Error")
-
- decorated_function = async_retry()(mock_async_function)
- with pytest.raises(Exception, match="Error"):
- await decorated_function()
-
-
-@pytest.mark.asyncio
-async def test_async_retry_decorator_multiple_attempts():
- async def mock_async_function():
- if mock_async_function.attempts == 0:
- mock_async_function.attempts += 1
- raise Exception("Error")
- else:
- return "Success"
-
- mock_async_function.attempts = 0
- decorated_function = async_retry(max_retries=2)(
- mock_async_function
- )
- result = await decorated_function()
- assert result == "Success"
-
-
-def test_create_audio_file():
- test_data = np.random.rand(
- 16000
- ) # Simulated audio data (1 second)
- sample_rate = 16000
- with tempfile.NamedTemporaryFile(
- suffix=".wav", delete=False
- ) as audio_file:
- audio_file_path = create_audio_file(
- test_data, sample_rate, audio_file.name
- )
-
- assert os.path.exists(audio_file_path)
- os.remove(audio_file_path)
-
-
-# test_distilled_whisperx.py
-
-
-# Fixtures for setting up model, processor, and audio files
-@pytest.fixture(scope="module")
-def model_id():
- return "distil-whisper/distil-large-v2"
-
-
-@pytest.fixture(scope="module")
-def whisper_model(model_id):
- return DistilWhisperModel(model_id)
-
-
-@pytest.fixture(scope="session")
-def audio_file_path(tmp_path_factory):
- # You would create a small temporary MP3 file here for testing
- # or use a public domain MP3 file's path
- return "path/to/valid_audio.mp3"
-
-
-@pytest.fixture(scope="session")
-def invalid_audio_file_path():
- return "path/to/invalid_audio.mp3"
-
-
-@pytest.fixture(scope="session")
-def audio_dict():
- # This should represent a valid audio dictionary as expected by the model
- return {"array": torch.randn(1, 16000), "sampling_rate": 16000}
-
-
-# Test initialization
-def test_initialization(whisper_model):
- assert whisper_model.model is not None
- assert whisper_model.processor is not None
-
-
-# Test successful transcription with file path
-def test_transcribe_with_file_path(whisper_model, audio_file_path):
- transcription = whisper_model.transcribe(audio_file_path)
- assert isinstance(transcription, str)
-
-
-# Test successful transcription with audio dict
-def test_transcribe_with_audio_dict(whisper_model, audio_dict):
- transcription = whisper_model.transcribe(audio_dict)
- assert isinstance(transcription, str)
-
-
-# Test for file not found error
-def test_file_not_found(whisper_model, invalid_audio_file_path):
- with pytest.raises(Exception):
- whisper_model.transcribe(invalid_audio_file_path)
-
-
-# Asynchronous tests
-@pytest.mark.asyncio
-async def test_async_transcription_success(
- whisper_model, audio_file_path
-):
- transcription = await whisper_model.async_transcribe(
- audio_file_path
- )
- assert isinstance(transcription, str)
-
-
-@pytest.mark.asyncio
-async def test_async_transcription_failure(
- whisper_model, invalid_audio_file_path
-):
- with pytest.raises(Exception):
- await whisper_model.async_transcribe(invalid_audio_file_path)
-
-
-# Testing real-time transcription simulation
-def test_real_time_transcription(
- whisper_model, audio_file_path, capsys
-):
- whisper_model.real_time_transcribe(
- audio_file_path, chunk_duration=1
- )
- captured = capsys.readouterr()
- assert "Starting real-time transcription..." in captured.out
-
-
-# Testing retry decorator for asynchronous function
-@pytest.mark.asyncio
-async def test_async_retry():
- @async_retry(max_retries=2, exceptions=(ValueError,), delay=0)
- async def failing_func():
- raise ValueError("Test")
-
- with pytest.raises(ValueError):
- await failing_func()
-
-
-# Mocking the actual model to avoid GPU/CPU intensive operations during test
-@pytest.fixture
-def mocked_model(monkeypatch):
- model_mock = AsyncMock(AutoModelForSpeechSeq2Seq)
- processor_mock = MagicMock(AutoProcessor)
- monkeypatch.setattr(
- "swarms.models.distilled_whisperx.AutoModelForSpeechSeq2Seq.from_pretrained",
- model_mock,
- )
- monkeypatch.setattr(
- "swarms.models.distilled_whisperx.AutoProcessor.from_pretrained",
- processor_mock,
- )
- return model_mock, processor_mock
-
-
-@pytest.mark.asyncio
-async def test_async_transcribe_with_mocked_model(
- mocked_model, audio_file_path
-):
- model_mock, processor_mock = mocked_model
- # Set up what the mock should return when it's called
- model_mock.return_value.generate.return_value = torch.tensor(
- [[0]]
- )
- processor_mock.return_value.batch_decode.return_value = [
- "mocked transcription"
- ]
- model_wrapper = DistilWhisperModel()
- transcription = await model_wrapper.async_transcribe(
- audio_file_path
- )
- assert transcription == "mocked transcription"
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_gemini.py b/tests/models/test_gemini.py
index c6f3e023..2a1d4ad4 100644
--- a/tests/models/test_gemini.py
+++ b/tests/models/test_gemini.py
@@ -110,7 +110,7 @@ def test_gemini_init_missing_api_key():
with pytest.raises(
ValueError, match="Please provide a Gemini API key"
):
- model = Gemini(gemini_api_key=None)
+ Gemini(gemini_api_key=None)
# Test Gemini initialization with missing model name
@@ -118,7 +118,7 @@ def test_gemini_init_missing_model_name():
with pytest.raises(
ValueError, match="Please provide a model name"
):
- model = Gemini(model_name=None)
+ Gemini(model_name=None)
# Test Gemini run method with empty task
diff --git a/tests/models/test_gigabind.py b/tests/models/test_gigabind.py
new file mode 100644
index 00000000..3aae0739
--- /dev/null
+++ b/tests/models/test_gigabind.py
@@ -0,0 +1,183 @@
+import pytest
+import requests
+
+from swarms.models.gigabind import Gigabind
+
+try:
+ import requests_mock
+except ImportError:
+ requests_mock = None
+
+
+@pytest.fixture
+def api():
+ return Gigabind(
+ host="localhost", port=8000, endpoint="embeddings"
+ )
+
+
+@pytest.fixture
+def mock(requests_mock):
+ requests_mock.post(
+ "http://localhost:8000/embeddings", json={"result": "success"}
+ )
+ return requests_mock
+
+
+def test_run_with_text(api, mock):
+ response = api.run(text="Hello, world!")
+ assert response == {"result": "success"}
+
+
+def test_run_with_vision(api, mock):
+ response = api.run(vision="image.jpg")
+ assert response == {"result": "success"}
+
+
+def test_run_with_audio(api, mock):
+ response = api.run(audio="audio.mp3")
+ assert response == {"result": "success"}
+
+
+def test_run_with_all(api, mock):
+ response = api.run(
+ text="Hello, world!", vision="image.jpg", audio="audio.mp3"
+ )
+ assert response == {"result": "success"}
+
+
+def test_run_with_none(api):
+ with pytest.raises(ValueError):
+ api.run()
+
+
+def test_generate_summary(api, mock):
+ response = api.generate_summary(text="Hello, world!")
+ assert response == {"result": "success"}
+
+
+def test_generate_summary_with_none(api):
+ with pytest.raises(ValueError):
+ api.generate_summary()
+
+
+def test_retry_on_failure(api, requests_mock):
+ requests_mock.post(
+ "http://localhost:8000/embeddings",
+ [
+ {"status_code": 500, "json": {}},
+ {"status_code": 500, "json": {}},
+ {"status_code": 200, "json": {"result": "success"}},
+ ],
+ )
+ response = api.run(text="Hello, world!")
+ assert response == {"result": "success"}
+
+
+def test_retry_exhausted(api, requests_mock):
+ requests_mock.post(
+ "http://localhost:8000/embeddings",
+ [
+ {"status_code": 500, "json": {}},
+ {"status_code": 500, "json": {}},
+ {"status_code": 500, "json": {}},
+ ],
+ )
+ response = api.run(text="Hello, world!")
+ assert response is None
+
+
+def test_proxy_url(api):
+ api.proxy_url = "http://proxy:8080"
+ assert api.url == "http://proxy:8080"
+
+
+def test_invalid_response(api, requests_mock):
+ requests_mock.post(
+ "http://localhost:8000/embeddings", text="not json"
+ )
+ response = api.run(text="Hello, world!")
+ assert response is None
+
+
+def test_connection_error(api, requests_mock):
+ requests_mock.post(
+ "http://localhost:8000/embeddings",
+ exc=requests.exceptions.ConnectTimeout,
+ )
+ response = api.run(text="Hello, world!")
+ assert response is None
+
+
+def test_http_error(api, requests_mock):
+ requests_mock.post(
+ "http://localhost:8000/embeddings", status_code=500
+ )
+ response = api.run(text="Hello, world!")
+ assert response is None
+
+
+def test_url_construction(api):
+ assert api.url == "http://localhost:8000/embeddings"
+
+
+def test_url_construction_with_proxy(api):
+ api.proxy_url = "http://proxy:8080"
+ assert api.url == "http://proxy:8080"
+
+
+def test_run_with_large_text(api, mock):
+ large_text = "Hello, world! " * 10000 # 10,000 repetitions
+ response = api.run(text=large_text)
+ assert response == {"result": "success"}
+
+
+def test_run_with_large_vision(api, mock):
+ large_vision = "image.jpg" * 10000 # 10,000 repetitions
+ response = api.run(vision=large_vision)
+ assert response == {"result": "success"}
+
+
+def test_run_with_large_audio(api, mock):
+ large_audio = "audio.mp3" * 10000 # 10,000 repetitions
+ response = api.run(audio=large_audio)
+ assert response == {"result": "success"}
+
+
+def test_run_with_large_all(api, mock):
+ large_text = "Hello, world! " * 10000 # 10,000 repetitions
+ large_vision = "image.jpg" * 10000 # 10,000 repetitions
+ large_audio = "audio.mp3" * 10000 # 10,000 repetitions
+ response = api.run(
+ text=large_text, vision=large_vision, audio=large_audio
+ )
+ assert response == {"result": "success"}
+
+
+def test_run_with_timeout(api, mock):
+ response = api.run(text="Hello, world!", timeout=0.001)
+ assert response is None
+
+
+def test_run_with_invalid_host(api):
+ api.host = "invalid"
+ response = api.run(text="Hello, world!")
+ assert response is None
+
+
+def test_run_with_invalid_port(api):
+ api.port = 99999
+ response = api.run(text="Hello, world!")
+ assert response is None
+
+
+def test_run_with_invalid_endpoint(api):
+ api.endpoint = "invalid"
+ response = api.run(text="Hello, world!")
+ assert response is None
+
+
+def test_run_with_invalid_proxy_url(api):
+ api.proxy_url = "invalid"
+ response = api.run(text="Hello, world!")
+ assert response is None
diff --git a/tests/models/test_gpt4_vision_api.py b/tests/models/test_gpt4_vision_api.py
index dfd03e27..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"
@@ -48,7 +47,7 @@ def test_run_success(vision_api):
def test_run_request_error(vision_api):
with patch(
"requests.post", side_effect=RequestException("Request Error")
- ) as mock_post:
+ ):
with pytest.raises(RequestException):
vision_api.run("What is this?", img)
@@ -58,7 +57,7 @@ def test_run_response_error(vision_api):
with patch(
"requests.post",
return_value=Mock(json=lambda: expected_response),
- ) as mock_post:
+ ):
with pytest.raises(RuntimeError):
vision_api.run("What is this?", img)
@@ -153,7 +152,7 @@ async def test_arun_request_error(vision_api):
"aiohttp.ClientSession.post",
new_callable=AsyncMock,
side_effect=Exception("Request Error"),
- ) as mock_post:
+ ):
with pytest.raises(Exception):
await vision_api.arun("What is this?", img)
@@ -181,7 +180,7 @@ def test_run_many_success(vision_api):
def test_run_many_request_error(vision_api):
with patch(
"requests.post", side_effect=RequestException("Request Error")
- ) as mock_post:
+ ):
tasks = ["What is this?", "What is that?"]
imgs = [img, img]
with pytest.raises(RequestException):
@@ -196,7 +195,7 @@ async def test_arun_json_decode_error(vision_api):
return_value=AsyncMock(
json=AsyncMock(side_effect=ValueError)
),
- ) as mock_post:
+ ):
with pytest.raises(ValueError):
await vision_api.arun("What is this?", img)
@@ -210,7 +209,7 @@ async def test_arun_api_error(vision_api):
return_value=AsyncMock(
json=AsyncMock(return_value=error_response)
),
- ) as mock_post:
+ ):
with pytest.raises(Exception, match="API Error"):
await vision_api.arun("What is this?", img)
@@ -224,7 +223,7 @@ async def test_arun_unexpected_response(vision_api):
return_value=AsyncMock(
json=AsyncMock(return_value=unexpected_response)
),
- ) as mock_post:
+ ):
with pytest.raises(Exception, match="Unexpected response"):
await vision_api.arun("What is this?", img)
@@ -247,6 +246,6 @@ async def test_arun_timeout(vision_api):
"aiohttp.ClientSession.post",
new_callable=AsyncMock,
side_effect=asyncio.TimeoutError,
- ) as mock_post:
+ ):
with pytest.raises(asyncio.TimeoutError):
await vision_api.arun("What is this?", img)
diff --git a/tests/models/test_hf.py b/tests/models/test_hf.py
index dce13338..48dcd008 100644
--- a/tests/models/test_hf.py
+++ b/tests/models/test_hf.py
@@ -1,90 +1,237 @@
-import pytest
import torch
-from unittest.mock import Mock
-from swarms.models.huggingface import HuggingFaceLLM
+import logging
+from unittest.mock import patch
+import pytest
-@pytest.fixture
-def mock_torch():
- return Mock()
+from swarms.models.huggingface import HuggingfaceLLM
+# Mock some functions and objects for testing
@pytest.fixture
-def mock_autotokenizer():
- return Mock()
+def mock_huggingface_llm(monkeypatch):
+ # Mock the model and tokenizer creation
+ def mock_init(
+ self,
+ model_id,
+ device="cpu",
+ max_length=500,
+ quantize=False,
+ quantization_config=None,
+ verbose=False,
+ distributed=False,
+ decoding=False,
+ max_workers=5,
+ repitition_penalty=1.3,
+ no_repeat_ngram_size=5,
+ temperature=0.7,
+ top_k=40,
+ top_p=0.8,
+ ):
+ pass
+
+ # Mock the model loading
+ def mock_load_model(self):
+ pass
+
+ # Mock the model generation
+ def mock_run(self, task):
+ pass
+
+ monkeypatch.setattr(HuggingfaceLLM, "__init__", mock_init)
+ monkeypatch.setattr(HuggingfaceLLM, "load_model", mock_load_model)
+ monkeypatch.setattr(HuggingfaceLLM, "run", mock_run)
+
+
+# Basic tests for initialization and attribute settings
+def test_init_huggingface_llm():
+ llm = HuggingfaceLLM(
+ model_id="test_model",
+ device="cuda",
+ max_length=1000,
+ quantize=True,
+ quantization_config={"config_key": "config_value"},
+ verbose=True,
+ distributed=True,
+ decoding=True,
+ max_workers=3,
+ repitition_penalty=1.5,
+ no_repeat_ngram_size=4,
+ temperature=0.8,
+ top_k=50,
+ top_p=0.7,
+ )
+ assert llm.model_id == "test_model"
+ assert llm.device == "cuda"
+ assert llm.max_length == 1000
+ assert llm.quantize is True
+ assert llm.quantization_config == {"config_key": "config_value"}
+ assert llm.verbose is True
+ assert llm.distributed is True
+ assert llm.decoding is True
+ assert llm.max_workers == 3
+ assert llm.repitition_penalty == 1.5
+ assert llm.no_repeat_ngram_size == 4
+ assert llm.temperature == 0.8
+ assert llm.top_k == 50
+ assert llm.top_p == 0.7
-@pytest.fixture
-def mock_automodelforcausallm():
- return Mock()
+# Test loading the model
+def test_load_model(mock_huggingface_llm):
+ llm = HuggingfaceLLM(model_id="test_model")
+ llm.load_model()
-@pytest.fixture
-def mock_bitsandbytesconfig():
- return Mock()
+ # Ensure that the load_model function is called
+ assert True
-@pytest.fixture
-def hugging_face_llm(
- mock_torch,
- mock_autotokenizer,
- mock_automodelforcausallm,
- mock_bitsandbytesconfig,
-):
- HuggingFaceLLM.torch = mock_torch
- HuggingFaceLLM.AutoTokenizer = mock_autotokenizer
- HuggingFaceLLM.AutoModelForCausalLM = mock_automodelforcausallm
- HuggingFaceLLM.BitsAndBytesConfig = mock_bitsandbytesconfig
-
- return HuggingFaceLLM(model_id="test")
-
-
-def test_init(
- hugging_face_llm, mock_autotokenizer, mock_automodelforcausallm
-):
- assert hugging_face_llm.model_id == "test"
- mock_autotokenizer.from_pretrained.assert_called_once_with("test")
- mock_automodelforcausallm.from_pretrained.assert_called_once_with(
- "test", quantization_config=None
+# Test running the model
+def test_run(mock_huggingface_llm):
+ llm = HuggingfaceLLM(model_id="test_model")
+ llm.run("Test prompt")
+
+ # Ensure that the run function is called
+ assert True
+
+
+# Test for setting max_length
+def test_llm_set_max_length(llm_instance):
+ new_max_length = 1000
+ llm_instance.set_max_length(new_max_length)
+ assert llm_instance.max_length == new_max_length
+
+
+# Test for setting verbose
+def test_llm_set_verbose(llm_instance):
+ llm_instance.set_verbose(True)
+ assert llm_instance.verbose is True
+
+
+# Test for setting distributed
+def test_llm_set_distributed(llm_instance):
+ llm_instance.set_distributed(True)
+ assert llm_instance.distributed is True
+
+
+# Test for setting decoding
+def test_llm_set_decoding(llm_instance):
+ llm_instance.set_decoding(True)
+ assert llm_instance.decoding is True
+
+
+# Test for setting max_workers
+def test_llm_set_max_workers(llm_instance):
+ new_max_workers = 10
+ llm_instance.set_max_workers(new_max_workers)
+ assert llm_instance.max_workers == new_max_workers
+
+
+# Test for setting repitition_penalty
+def test_llm_set_repitition_penalty(llm_instance):
+ new_repitition_penalty = 1.5
+ llm_instance.set_repitition_penalty(new_repitition_penalty)
+ assert llm_instance.repitition_penalty == new_repitition_penalty
+
+
+# Test for setting no_repeat_ngram_size
+def test_llm_set_no_repeat_ngram_size(llm_instance):
+ new_no_repeat_ngram_size = 6
+ llm_instance.set_no_repeat_ngram_size(new_no_repeat_ngram_size)
+ assert (
+ llm_instance.no_repeat_ngram_size == new_no_repeat_ngram_size
)
-def test_init_with_quantize(
- hugging_face_llm,
- mock_autotokenizer,
- mock_automodelforcausallm,
- mock_bitsandbytesconfig,
-):
- quantization_config = {
- "load_in_4bit": True,
- "bnb_4bit_use_double_quant": True,
+# Test for setting temperature
+def test_llm_set_temperature(llm_instance):
+ new_temperature = 0.8
+ llm_instance.set_temperature(new_temperature)
+ assert llm_instance.temperature == new_temperature
+
+
+# Test for setting top_k
+def test_llm_set_top_k(llm_instance):
+ new_top_k = 50
+ llm_instance.set_top_k(new_top_k)
+ assert llm_instance.top_k == new_top_k
+
+
+# Test for setting top_p
+def test_llm_set_top_p(llm_instance):
+ new_top_p = 0.9
+ llm_instance.set_top_p(new_top_p)
+ assert llm_instance.top_p == new_top_p
+
+
+# Test for setting quantize
+def test_llm_set_quantize(llm_instance):
+ llm_instance.set_quantize(True)
+ assert llm_instance.quantize is True
+
+
+# Test for setting quantization_config
+def test_llm_set_quantization_config(llm_instance):
+ new_quantization_config = {
+ "load_in_4bit": False,
+ "bnb_4bit_use_double_quant": False,
"bnb_4bit_quant_type": "nf4",
"bnb_4bit_compute_dtype": torch.bfloat16,
}
- mock_bitsandbytesconfig.return_value = quantization_config
+ llm_instance.set_quantization_config(new_quantization_config)
+ assert llm_instance.quantization_config == new_quantization_config
- HuggingFaceLLM(model_id="test", quantize=True)
- mock_bitsandbytesconfig.assert_called_once_with(
- **quantization_config
- )
- mock_autotokenizer.from_pretrained.assert_called_once_with("test")
- mock_automodelforcausallm.from_pretrained.assert_called_once_with(
- "test", quantization_config=quantization_config
- )
+# Test for setting model_id
+def test_llm_set_model_id(llm_instance):
+ new_model_id = "EleutherAI/gpt-neo-2.7B"
+ llm_instance.set_model_id(new_model_id)
+ assert llm_instance.model_id == new_model_id
+
+
+# Test for setting model
+@patch(
+ "swarms.models.huggingface.AutoModelForCausalLM.from_pretrained"
+)
+def test_llm_set_model(mock_model, llm_instance):
+ mock_model.return_value = "mocked model"
+ llm_instance.set_model(mock_model)
+ assert llm_instance.model == "mocked model"
+
+
+# Test for setting tokenizer
+@patch("swarms.models.huggingface.AutoTokenizer.from_pretrained")
+def test_llm_set_tokenizer(mock_tokenizer, llm_instance):
+ mock_tokenizer.return_value = "mocked tokenizer"
+ llm_instance.set_tokenizer(mock_tokenizer)
+ assert llm_instance.tokenizer == "mocked tokenizer"
+
+
+# Test for setting logger
+def test_llm_set_logger(llm_instance):
+ new_logger = logging.getLogger("test_logger")
+ llm_instance.set_logger(new_logger)
+ assert llm_instance.logger == new_logger
+
+
+# Test for saving model
+@patch("torch.save")
+def test_llm_save_model(mock_save, llm_instance):
+ llm_instance.save_model("path/to/save")
+ mock_save.assert_called_once()
-def test_generate_text(hugging_face_llm):
- prompt_text = "test prompt"
- expected_output = "test output"
- hugging_face_llm.tokenizer.encode.return_value = torch.tensor(
- [0]
- ) # Mock tensor
- hugging_face_llm.model.generate.return_value = torch.tensor(
- [0]
- ) # Mock tensor
- hugging_face_llm.tokenizer.decode.return_value = expected_output
+# Test for print_dashboard
+@patch("builtins.print")
+def test_llm_print_dashboard(mock_print, llm_instance):
+ llm_instance.print_dashboard("test task")
+ mock_print.assert_called()
- output = hugging_face_llm.generate_text(prompt_text)
- assert output == expected_output
+# Test for __call__ method
+@patch("swarms.models.huggingface.HuggingfaceLLM.run")
+def test_llm_call(mock_run, llm_instance):
+ mock_run.return_value = "mocked output"
+ result = llm_instance("test task")
+ assert result == "mocked output"
diff --git a/tests/models/test_kosmos2.py b/tests/models/test_kosmos2.py
deleted file mode 100644
index 7e4f0e5f..00000000
--- a/tests/models/test_kosmos2.py
+++ /dev/null
@@ -1,394 +0,0 @@
-import pytest
-import os
-from PIL import Image
-from swarms.models.kosmos2 import Kosmos2, Detections
-
-
-# Fixture for a sample image
-@pytest.fixture
-def sample_image():
- image = Image.new("RGB", (224, 224))
- return image
-
-
-# Fixture for initializing Kosmos2
-@pytest.fixture
-def kosmos2():
- return Kosmos2.initialize()
-
-
-# Test Kosmos2 initialization
-def test_kosmos2_initialization(kosmos2):
- assert kosmos2 is not None
-
-
-# Test Kosmos2 with a sample image
-def test_kosmos2_with_sample_image(kosmos2, sample_image):
- detections = kosmos2(img=sample_image)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 0
- )
-
-
-# Mocked extract_entities function for testing
-def mock_extract_entities(text):
- return [
- ("entity1", (0.1, 0.2, 0.3, 0.4)),
- ("entity2", (0.5, 0.6, 0.7, 0.8)),
- ]
-
-
-# Mocked process_entities_to_detections function for testing
-def mock_process_entities_to_detections(entities, image):
- return Detections(
- xyxy=[(10, 20, 30, 40), (50, 60, 70, 80)],
- class_id=[0, 0],
- confidence=[1.0, 1.0],
- )
-
-
-# Test Kosmos2 with mocked entity extraction and detection
-def test_kosmos2_with_mocked_extraction_and_detection(
- kosmos2, sample_image, monkeypatch
-):
- monkeypatch.setattr(
- kosmos2, "extract_entities", mock_extract_entities
- )
- monkeypatch.setattr(
- kosmos2,
- "process_entities_to_detections",
- mock_process_entities_to_detections,
- )
-
- detections = kosmos2(img=sample_image)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 2
- )
-
-
-# Test Kosmos2 with empty entity extraction
-def test_kosmos2_with_empty_extraction(
- kosmos2, sample_image, monkeypatch
-):
- monkeypatch.setattr(kosmos2, "extract_entities", lambda x: [])
- detections = kosmos2(img=sample_image)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 0
- )
-
-
-# Test Kosmos2 with invalid image path
-def test_kosmos2_with_invalid_image_path(kosmos2):
- with pytest.raises(Exception):
- kosmos2(img="invalid_image_path.jpg")
-
-
-# Additional tests can be added for various scenarios and edge cases
-
-
-# Test Kosmos2 with a larger image
-def test_kosmos2_with_large_image(kosmos2):
- large_image = Image.new("RGB", (1024, 768))
- detections = kosmos2(img=large_image)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 0
- )
-
-
-# Test Kosmos2 with different image formats
-def test_kosmos2_with_different_image_formats(kosmos2, tmp_path):
- # Create a temporary directory
- temp_dir = tmp_path / "images"
- temp_dir.mkdir()
-
- # Create sample images in different formats
- image_formats = ["jpeg", "png", "gif", "bmp"]
- for format in image_formats:
- image_path = temp_dir / f"sample_image.{format}"
- Image.new("RGB", (224, 224)).save(image_path)
-
- # Test Kosmos2 with each image format
- for format in image_formats:
- image_path = temp_dir / f"sample_image.{format}"
- detections = kosmos2(img=image_path)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 0
- )
-
-
-# Test Kosmos2 with a non-existent model
-def test_kosmos2_with_non_existent_model(kosmos2):
- with pytest.raises(Exception):
- kosmos2.model = None
- kosmos2(img="sample_image.jpg")
-
-
-# Test Kosmos2 with a non-existent processor
-def test_kosmos2_with_non_existent_processor(kosmos2):
- with pytest.raises(Exception):
- kosmos2.processor = None
- kosmos2(img="sample_image.jpg")
-
-
-# Test Kosmos2 with missing image
-def test_kosmos2_with_missing_image(kosmos2):
- with pytest.raises(Exception):
- kosmos2(img="non_existent_image.jpg")
-
-
-# ... (previous tests)
-
-
-# Test Kosmos2 with a non-existent model and processor
-def test_kosmos2_with_non_existent_model_and_processor(kosmos2):
- with pytest.raises(Exception):
- kosmos2.model = None
- kosmos2.processor = None
- kosmos2(img="sample_image.jpg")
-
-
-# Test Kosmos2 with a corrupted image
-def test_kosmos2_with_corrupted_image(kosmos2, tmp_path):
- # Create a temporary directory
- temp_dir = tmp_path / "images"
- temp_dir.mkdir()
-
- # Create a corrupted image
- corrupted_image_path = temp_dir / "corrupted_image.jpg"
- with open(corrupted_image_path, "wb") as f:
- f.write(b"corrupted data")
-
- with pytest.raises(Exception):
- kosmos2(img=corrupted_image_path)
-
-
-# Test Kosmos2 with a large batch size
-def test_kosmos2_with_large_batch_size(kosmos2, sample_image):
- kosmos2.batch_size = 32
- detections = kosmos2(img=sample_image)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 0
- )
-
-
-# Test Kosmos2 with an invalid compute type
-def test_kosmos2_with_invalid_compute_type(kosmos2, sample_image):
- kosmos2.compute_type = "invalid_compute_type"
- with pytest.raises(Exception):
- kosmos2(img=sample_image)
-
-
-# Test Kosmos2 with a valid HF API key
-def test_kosmos2_with_valid_hf_api_key(kosmos2, sample_image):
- kosmos2.hf_api_key = "valid_api_key"
- detections = kosmos2(img=sample_image)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 2
- )
-
-
-# Test Kosmos2 with an invalid HF API key
-def test_kosmos2_with_invalid_hf_api_key(kosmos2, sample_image):
- kosmos2.hf_api_key = "invalid_api_key"
- with pytest.raises(Exception):
- kosmos2(img=sample_image)
-
-
-# Test Kosmos2 with a very long generated text
-def test_kosmos2_with_long_generated_text(
- kosmos2, sample_image, monkeypatch
-):
- def mock_generate_text(*args, **kwargs):
- return "A" * 10000
-
- monkeypatch.setattr(kosmos2.model, "generate", mock_generate_text)
- detections = kosmos2(img=sample_image)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 0
- )
-
-
-# Test Kosmos2 with entities containing special characters
-def test_kosmos2_with_entities_containing_special_characters(
- kosmos2, sample_image, monkeypatch
-):
- def mock_extract_entities(text):
- return [
- (
- "entity1 with special characters (ΓΌ, ΓΆ, etc.)",
- (0.1, 0.2, 0.3, 0.4),
- )
- ]
-
- monkeypatch.setattr(
- kosmos2, "extract_entities", mock_extract_entities
- )
- detections = kosmos2(img=sample_image)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 1
- )
-
-
-# Test Kosmos2 with image containing multiple objects
-def test_kosmos2_with_image_containing_multiple_objects(
- kosmos2, sample_image, monkeypatch
-):
- def mock_extract_entities(text):
- return [
- ("entity1", (0.1, 0.2, 0.3, 0.4)),
- ("entity2", (0.5, 0.6, 0.7, 0.8)),
- ]
-
- monkeypatch.setattr(
- kosmos2, "extract_entities", mock_extract_entities
- )
- detections = kosmos2(img=sample_image)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 2
- )
-
-
-# Test Kosmos2 with image containing no objects
-def test_kosmos2_with_image_containing_no_objects(
- kosmos2, sample_image, monkeypatch
-):
- def mock_extract_entities(text):
- return []
-
- monkeypatch.setattr(
- kosmos2, "extract_entities", mock_extract_entities
- )
- detections = kosmos2(img=sample_image)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 0
- )
-
-
-# Test Kosmos2 with a valid YouTube video URL
-def test_kosmos2_with_valid_youtube_video_url(kosmos2):
- youtube_video_url = "https://www.youtube.com/watch?v=VIDEO_ID"
- detections = kosmos2(video_url=youtube_video_url)
- assert isinstance(detections, Detections)
- assert (
- len(detections.xyxy)
- == len(detections.class_id)
- == len(detections.confidence)
- == 2
- )
-
-
-# Test Kosmos2 with an invalid YouTube video URL
-def test_kosmos2_with_invalid_youtube_video_url(kosmos2):
- invalid_youtube_video_url = (
- "https://www.youtube.com/invalid_video"
- )
- with pytest.raises(Exception):
- kosmos2(video_url=invalid_youtube_video_url)
-
-
-# Test Kosmos2 with no YouTube video URL provided
-def test_kosmos2_with_no_youtube_video_url(kosmos2):
- with pytest.raises(Exception):
- kosmos2(video_url=None)
-
-
-# Test Kosmos2 installation
-def test_kosmos2_installation():
- kosmos2 = Kosmos2()
- kosmos2.install()
- assert os.path.exists("video.mp4")
- assert os.path.exists("video.mp3")
- os.remove("video.mp4")
- os.remove("video.mp3")
-
-
-# Test Kosmos2 termination
-def test_kosmos2_termination(kosmos2):
- kosmos2.terminate()
- assert kosmos2.process is None
-
-
-# Test Kosmos2 start_process method
-def test_kosmos2_start_process(kosmos2):
- kosmos2.start_process()
- assert kosmos2.process is not None
-
-
-# Test Kosmos2 preprocess_code method
-def test_kosmos2_preprocess_code(kosmos2):
- code = "print('Hello, World!')"
- preprocessed_code = kosmos2.preprocess_code(code)
- assert isinstance(preprocessed_code, str)
- assert "end_of_execution" in preprocessed_code
-
-
-# Test Kosmos2 run method with debug mode
-def test_kosmos2_run_with_debug_mode(kosmos2, sample_image):
- kosmos2.debug_mode = True
- detections = kosmos2(img=sample_image)
- assert isinstance(detections, Detections)
-
-
-# Test Kosmos2 handle_stream_output method
-def test_kosmos2_handle_stream_output(kosmos2):
- stream_output = "Sample output"
- kosmos2.handle_stream_output(stream_output, is_error=False)
-
-
-# Test Kosmos2 run method with invalid image path
-def test_kosmos2_run_with_invalid_image_path(kosmos2):
- with pytest.raises(Exception):
- kosmos2.run(img="invalid_image_path.jpg")
-
-
-# Test Kosmos2 run method with invalid video URL
-def test_kosmos2_run_with_invalid_video_url(kosmos2):
- with pytest.raises(Exception):
- kosmos2.run(video_url="invalid_video_url")
-
-
-# ... (more tests)
diff --git a/tests/models/test_mixtral.py b/tests/models/test_mixtral.py
new file mode 100644
index 00000000..9eb31af0
--- /dev/null
+++ b/tests/models/test_mixtral.py
@@ -0,0 +1,53 @@
+import pytest
+from unittest.mock import patch, MagicMock
+from swarms.models.mixtral import Mixtral
+
+
+@patch("swarms.models.mixtral.AutoTokenizer")
+@patch("swarms.models.mixtral.AutoModelForCausalLM")
+def test_mixtral_init(mock_model, mock_tokenizer):
+ mixtral = Mixtral()
+ mock_tokenizer.from_pretrained.assert_called_once()
+ mock_model.from_pretrained.assert_called_once()
+ assert mixtral.model_name == "mistralai/Mixtral-8x7B-v0.1"
+ assert mixtral.max_new_tokens == 20
+
+
+@patch("swarms.models.mixtral.AutoTokenizer")
+@patch("swarms.models.mixtral.AutoModelForCausalLM")
+def test_mixtral_run(mock_model, mock_tokenizer):
+ mixtral = Mixtral()
+ mock_tokenizer_instance = MagicMock()
+ mock_model_instance = MagicMock()
+ mock_tokenizer.from_pretrained.return_value = (
+ mock_tokenizer_instance
+ )
+ mock_model.from_pretrained.return_value = mock_model_instance
+ mock_tokenizer_instance.return_tensors = "pt"
+ mock_model_instance.generate.return_value = [101, 102, 103]
+ mock_tokenizer_instance.decode.return_value = "Generated text"
+ result = mixtral.run("Test task")
+ assert result == "Generated text"
+ mock_tokenizer_instance.assert_called_once_with(
+ "Test task", return_tensors="pt"
+ )
+ mock_model_instance.generate.assert_called_once()
+ mock_tokenizer_instance.decode.assert_called_once_with(
+ [101, 102, 103], skip_special_tokens=True
+ )
+
+
+@patch("swarms.models.mixtral.AutoTokenizer")
+@patch("swarms.models.mixtral.AutoModelForCausalLM")
+def test_mixtral_run_error(mock_model, mock_tokenizer):
+ mixtral = Mixtral()
+ mock_tokenizer_instance = MagicMock()
+ mock_model_instance = MagicMock()
+ mock_tokenizer.from_pretrained.return_value = (
+ mock_tokenizer_instance
+ )
+ mock_model.from_pretrained.return_value = mock_model_instance
+ mock_tokenizer_instance.return_tensors = "pt"
+ mock_model_instance.generate.side_effect = Exception("Test error")
+ with pytest.raises(Exception, match="Test error"):
+ mixtral.run("Test task")
diff --git a/tests/models/test_multion.py b/tests/models/test_multion.py
deleted file mode 100644
index 416e6dc3..00000000
--- a/tests/models/test_multion.py
+++ /dev/null
@@ -1,54 +0,0 @@
-import pytest
-from unittest.mock import Mock, patch
-from swarms.models.multion import MultiOn
-
-
-@pytest.fixture
-def multion_instance():
- return MultiOn()
-
-
-@pytest.fixture
-def mock_multion():
- return Mock()
-
-
-def test_multion_import():
- with pytest.raises(ImportError):
- import multion
-
-
-def test_multion_init():
- multion = MultiOn()
- assert isinstance(multion, MultiOn)
-
-
-def test_multion_run_with_valid_input(multion_instance, mock_multion):
- task = "Order chicken tendies"
- url = "https://www.google.com/"
- mock_multion.new_session.return_value = (
- "Order chicken tendies. https://www.google.com/"
- )
-
- with patch("swarms.models.multion.multion", mock_multion):
- response = multion_instance.run(task, url)
-
- assert (
- response == "Order chicken tendies. https://www.google.com/"
- )
-
-
-def test_multion_run_with_invalid_input(
- multion_instance, mock_multion
-):
- task = ""
- url = "https://www.google.com/"
- mock_multion.new_session.return_value = None
-
- with patch("swarms.models.multion.multion", mock_multion):
- response = multion_instance.run(task, url)
-
- assert response is None
-
-
-# Add more test cases to cover different scenarios, edge cases, and error handling as needed.
diff --git a/tests/models/test_open_dalle.py b/tests/models/test_open_dalle.py
new file mode 100644
index 00000000..2483d705
--- /dev/null
+++ b/tests/models/test_open_dalle.py
@@ -0,0 +1,59 @@
+import pytest
+import torch
+from swarms.models.open_dalle import OpenDalle
+
+
+def test_init():
+ od = OpenDalle()
+ assert isinstance(od, OpenDalle)
+
+
+def test_init_custom_model():
+ od = OpenDalle(model_name="custom_model")
+ assert od.pipeline.model_name == "custom_model"
+
+
+def test_init_custom_dtype():
+ od = OpenDalle(torch_dtype=torch.float32)
+ assert od.pipeline.torch_dtype == torch.float32
+
+
+def test_init_custom_device():
+ od = OpenDalle(device="cpu")
+ assert od.pipeline.device == "cpu"
+
+
+def test_run():
+ od = OpenDalle()
+ result = od.run("A picture of a cat")
+ assert isinstance(result, torch.Tensor)
+
+
+def test_run_no_task():
+ od = OpenDalle()
+ with pytest.raises(ValueError, match="Task cannot be None"):
+ od.run(None)
+
+
+def test_run_non_string_task():
+ od = OpenDalle()
+ with pytest.raises(TypeError, match="Task must be a string"):
+ od.run(123)
+
+
+def test_run_empty_task():
+ od = OpenDalle()
+ with pytest.raises(ValueError, match="Task cannot be empty"):
+ od.run("")
+
+
+def test_run_custom_args():
+ od = OpenDalle()
+ result = od.run("A picture of a cat", custom_arg="custom_value")
+ assert isinstance(result, torch.Tensor)
+
+
+def test_run_error():
+ od = OpenDalle()
+ with pytest.raises(Exception):
+ od.run("A picture of a cat", raise_error=True)
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"]
diff --git a/tests/models/test_ssd_1b.py b/tests/models/test_ssd_1b.py
index 35cc4864..39e4264e 100644
--- a/tests/models/test_ssd_1b.py
+++ b/tests/models/test_ssd_1b.py
@@ -162,74 +162,3 @@ def test_ssd1b_repr_str(ssd1b_model):
image_url = ssd1b_model(task)
assert repr(ssd1b_model) == f"SSD1B(image_url={image_url})"
assert str(ssd1b_model) == f"SSD1B(image_url={image_url})"
-
-
-import pytest
-from your_module import SSD1B
-
-
-# Create fixtures if needed
-@pytest.fixture
-def ssd1b_model():
- return SSD1B()
-
-
-# Test cases for additional scenarios and behaviors
-def test_ssd1b_dashboard_printing(ssd1b_model, capsys):
- ssd1b_model.dashboard = True
- ssd1b_model.print_dashboard()
- captured = capsys.readouterr()
- assert "SSD1B Dashboard:" in captured.out
-
-
-def test_ssd1b_generate_image_name(ssd1b_model):
- task = "A painting of a dog"
- img_name = ssd1b_model._generate_image_name(task)
- assert isinstance(img_name, str)
- assert len(img_name) > 0
-
-
-def test_ssd1b_set_width_height(ssd1b_model, mocker):
- img = mocker.MagicMock()
- width, height = 800, 600
- result = ssd1b_model.set_width_height(img, width, height)
- assert result == img.resize.return_value
-
-
-def test_ssd1b_read_img(ssd1b_model, mocker):
- img = mocker.MagicMock()
- result = ssd1b_model.read_img(img)
- assert result == img.open.return_value
-
-
-def test_ssd1b_convert_to_bytesio(ssd1b_model, mocker):
- img = mocker.MagicMock()
- img_format = "PNG"
- result = ssd1b_model.convert_to_bytesio(img, img_format)
- assert isinstance(result, bytes)
-
-
-def test_ssd1b_save_image(ssd1b_model, mocker, tmp_path):
- img = mocker.MagicMock()
- img_name = "test.png"
- save_path = tmp_path / img_name
- ssd1b_model._download_image(img, img_name, save_path)
- assert save_path.exists()
-
-
-def test_ssd1b_repr_str(ssd1b_model):
- task = "A painting of a dog"
- image_url = ssd1b_model(task)
- assert repr(ssd1b_model) == f"SSD1B(image_url={image_url})"
- assert str(ssd1b_model) == f"SSD1B(image_url={image_url})"
-
-
-def test_ssd1b_rate_limited_call(ssd1b_model, mocker):
- task = "A painting of a dog"
- mocker.patch.object(
- ssd1b_model,
- "__call__",
- side_effect=Exception("Rate limit exceeded"),
- )
- with pytest.raises(Exception, match="Rate limit exceeded"):
- ssd1b_model.rate_limited_call(task)
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_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_togther.py b/tests/models/test_togther.py
index 75313a45..43a99b00 100644
--- a/tests/models/test_togther.py
+++ b/tests/models/test_togther.py
@@ -1,8 +1,7 @@
-import os
import requests
import pytest
from unittest.mock import patch, Mock
-from swarms.models.together import TogetherModel
+from swarms.models.together import TogetherLLM
import logging
@@ -12,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"
@@ -26,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",
@@ -58,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)
@@ -71,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)
@@ -79,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):
@@ -92,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
@@ -106,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)
@@ -119,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)
@@ -130,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)
@@ -139,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
diff --git a/tests/models/test_ultralytics.py b/tests/models/test_ultralytics.py
new file mode 100644
index 00000000..3e7a7b5c
--- /dev/null
+++ b/tests/models/test_ultralytics.py
@@ -0,0 +1,34 @@
+from unittest.mock import patch
+from swarms.models.ultralytics_model import UltralyticsModel
+
+
+def test_ultralytics_init():
+ with patch("swarms.models.YOLO") as mock_yolo:
+ model_name = "yolov5s"
+ 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
+
+
+def test_ultralytics_call():
+ with patch("swarms.models.YOLO") as mock_yolo:
+ model_name = "yolov5s"
+ ultralytics = UltralyticsModel(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 = 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/models/test_vllm.py b/tests/models/test_vllm.py
deleted file mode 100644
index d15a13b9..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_instance = 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/models/test_whisperx.py b/tests/models/test_whisperx.py
deleted file mode 100644
index 4b0e4120..00000000
--- a/tests/models/test_whisperx.py
+++ /dev/null
@@ -1,222 +0,0 @@
-import os
-import subprocess
-import tempfile
-from unittest.mock import patch
-
-import pytest
-import whisperx
-from pydub import AudioSegment
-from pytube import YouTube
-from swarms.models.whisperx_model import WhisperX
-
-
-# Fixture to create a temporary directory for testing
-@pytest.fixture
-def temp_dir():
- with tempfile.TemporaryDirectory() as tempdir:
- yield tempdir
-
-
-# Mock subprocess.run to prevent actual installation during tests
-@patch.object(subprocess, "run")
-def test_speech_to_text_install(mock_run):
- stt = WhisperX("https://www.youtube.com/watch?v=MJd6pr16LRM")
- stt.install()
- mock_run.assert_called_with(["pip", "install", "whisperx"])
-
-
-# Mock pytube.YouTube and pytube.Streams for download tests
-@patch("pytube.YouTube")
-@patch.object(YouTube, "streams")
-def test_speech_to_text_download_youtube_video(
- mock_streams, mock_youtube, temp_dir
-):
- # Mock YouTube and streams
- video_url = "https://www.youtube.com/watch?v=MJd6pr16LRM"
- mock_stream = mock_streams().filter().first()
- mock_stream.download.return_value = os.path.join(
- temp_dir, "video.mp4"
- )
- mock_youtube.return_value = mock_youtube
- mock_youtube.streams = mock_streams
-
- stt = WhisperX(video_url)
- audio_file = stt.download_youtube_video()
-
- assert os.path.exists(audio_file)
- assert audio_file.endswith(".mp3")
-
-
-# Mock whisperx.load_model and whisperx.load_audio for transcribe tests
-@patch("whisperx.load_model")
-@patch("whisperx.load_audio")
-@patch("whisperx.load_align_model")
-@patch("whisperx.align")
-@patch.object(whisperx.DiarizationPipeline, "__call__")
-def test_speech_to_text_transcribe_youtube_video(
- mock_diarization,
- mock_align,
- mock_align_model,
- mock_load_audio,
- mock_load_model,
- temp_dir,
-):
- # Mock whisperx functions
- mock_load_model.return_value = mock_load_model
- mock_load_model.transcribe.return_value = {
- "language": "en",
- "segments": [{"text": "Hello, World!"}],
- }
-
- mock_load_audio.return_value = "audio_path"
- mock_align_model.return_value = (mock_align_model, "metadata")
- mock_align.return_value = {
- "segments": [{"text": "Hello, World!"}]
- }
-
- # Mock diarization pipeline
- mock_diarization.return_value = None
-
- video_url = "https://www.youtube.com/watch?v=MJd6pr16LRM/video"
- stt = WhisperX(video_url)
- transcription = stt.transcribe_youtube_video()
-
- assert transcription == "Hello, World!"
-
-
-# More tests for different scenarios and edge cases can be added here.
-
-
-# Test transcribe method with provided audio file
-def test_speech_to_text_transcribe_audio_file(temp_dir):
- # Create a temporary audio file
- audio_file = os.path.join(temp_dir, "test_audio.mp3")
- AudioSegment.silent(duration=500).export(audio_file, format="mp3")
-
- stt = WhisperX("https://www.youtube.com/watch?v=MJd6pr16LRM")
- transcription = stt.transcribe(audio_file)
-
- assert transcription == ""
-
-
-# Test transcribe method when Whisperx fails
-@patch("whisperx.load_model")
-@patch("whisperx.load_audio")
-def test_speech_to_text_transcribe_whisperx_failure(
- mock_load_audio, mock_load_model, temp_dir
-):
- # Mock whisperx functions to raise an exception
- mock_load_model.side_effect = Exception("Whisperx failed")
- mock_load_audio.return_value = "audio_path"
-
- stt = WhisperX("https://www.youtube.com/watch?v=MJd6pr16LRM")
- transcription = stt.transcribe("audio_path")
-
- assert transcription == "Whisperx failed"
-
-
-# Test transcribe method with missing 'segments' key in Whisperx output
-@patch("whisperx.load_model")
-@patch("whisperx.load_audio")
-@patch("whisperx.load_align_model")
-@patch("whisperx.align")
-@patch.object(whisperx.DiarizationPipeline, "__call__")
-def test_speech_to_text_transcribe_missing_segments(
- mock_diarization,
- mock_align,
- mock_align_model,
- mock_load_audio,
- mock_load_model,
-):
- # Mock whisperx functions to return incomplete output
- mock_load_model.return_value = mock_load_model
- mock_load_model.transcribe.return_value = {"language": "en"}
-
- mock_load_audio.return_value = "audio_path"
- mock_align_model.return_value = (mock_align_model, "metadata")
- mock_align.return_value = {}
-
- # Mock diarization pipeline
- mock_diarization.return_value = None
-
- stt = WhisperX("https://www.youtube.com/watch?v=MJd6pr16LRM")
- transcription = stt.transcribe("audio_path")
-
- assert transcription == ""
-
-
-# Test transcribe method with Whisperx align failure
-@patch("whisperx.load_model")
-@patch("whisperx.load_audio")
-@patch("whisperx.load_align_model")
-@patch("whisperx.align")
-@patch.object(whisperx.DiarizationPipeline, "__call__")
-def test_speech_to_text_transcribe_align_failure(
- mock_diarization,
- mock_align,
- mock_align_model,
- mock_load_audio,
- mock_load_model,
-):
- # Mock whisperx functions to raise an exception during align
- mock_load_model.return_value = mock_load_model
- mock_load_model.transcribe.return_value = {
- "language": "en",
- "segments": [{"text": "Hello, World!"}],
- }
-
- mock_load_audio.return_value = "audio_path"
- mock_align_model.return_value = (mock_align_model, "metadata")
- mock_align.side_effect = Exception("Align failed")
-
- # Mock diarization pipeline
- mock_diarization.return_value = None
-
- stt = WhisperX("https://www.youtube.com/watch?v=MJd6pr16LRM")
- transcription = stt.transcribe("audio_path")
-
- assert transcription == "Align failed"
-
-
-# Test transcribe_youtube_video when Whisperx diarization fails
-@patch("pytube.YouTube")
-@patch.object(YouTube, "streams")
-@patch("whisperx.DiarizationPipeline")
-@patch("whisperx.load_audio")
-@patch("whisperx.load_align_model")
-@patch("whisperx.align")
-def test_speech_to_text_transcribe_diarization_failure(
- mock_align,
- mock_align_model,
- mock_load_audio,
- mock_diarization,
- mock_streams,
- mock_youtube,
- temp_dir,
-):
- # Mock YouTube and streams
- video_url = "https://www.youtube.com/watch?v=MJd6pr16LRM"
- mock_stream = mock_streams().filter().first()
- mock_stream.download.return_value = os.path.join(
- temp_dir, "video.mp4"
- )
- mock_youtube.return_value = mock_youtube
- mock_youtube.streams = mock_streams
-
- # Mock whisperx functions
- mock_load_audio.return_value = "audio_path"
- mock_align_model.return_value = (mock_align_model, "metadata")
- mock_align.return_value = {
- "segments": [{"text": "Hello, World!"}]
- }
-
- # Mock diarization pipeline to raise an exception
- mock_diarization.side_effect = Exception("Diarization failed")
-
- stt = WhisperX(video_url)
- transcription = stt.transcribe_youtube_video()
-
- assert transcription == "Diarization failed"
-
-
-# Add more tests for other scenarios and edge cases as needed.
diff --git a/tests/models/test_zeroscope.py b/tests/models/test_zeroscope.py
new file mode 100644
index 00000000..25a4c597
--- /dev/null
+++ b/tests/models/test_zeroscope.py
@@ -0,0 +1,122 @@
+from unittest.mock import MagicMock, patch
+
+import pytest
+
+from swarms.models.zeroscope import ZeroscopeTTV
+
+
+@patch("swarms.models.zeroscope.DiffusionPipeline")
+@patch("swarms.models.zeroscope.DPMSolverMultistepScheduler")
+def test_zeroscope_ttv_init(mock_scheduler, mock_pipeline):
+ zeroscope = ZeroscopeTTV()
+ mock_pipeline.from_pretrained.assert_called_once()
+ mock_scheduler.assert_called_once()
+ assert zeroscope.model_name == "cerspense/zeroscope_v2_576w"
+ assert zeroscope.chunk_size == 1
+ assert zeroscope.dim == 1
+ assert zeroscope.num_inference_steps == 40
+ assert zeroscope.height == 320
+ assert zeroscope.width == 576
+ assert zeroscope.num_frames == 36
+
+
+@patch("swarms.models.zeroscope.DiffusionPipeline")
+@patch("swarms.models.zeroscope.DPMSolverMultistepScheduler")
+def test_zeroscope_ttv_forward(mock_scheduler, mock_pipeline):
+ zeroscope = ZeroscopeTTV()
+ mock_pipeline_instance = MagicMock()
+ mock_pipeline.from_pretrained.return_value = (
+ mock_pipeline_instance
+ )
+ mock_pipeline_instance.return_value = MagicMock(
+ frames="Generated frames"
+ )
+ mock_pipeline_instance.enable_vae_slicing.assert_called_once()
+ mock_pipeline_instance.enable_forward_chunking.assert_called_once_with(
+ chunk_size=1, dim=1
+ )
+ result = zeroscope.forward("Test task")
+ assert result == "Generated frames"
+ mock_pipeline_instance.assert_called_once_with(
+ "Test task",
+ num_inference_steps=40,
+ height=320,
+ width=576,
+ num_frames=36,
+ )
+
+
+@patch("swarms.models.zeroscope.DiffusionPipeline")
+@patch("swarms.models.zeroscope.DPMSolverMultistepScheduler")
+def test_zeroscope_ttv_forward_error(mock_scheduler, mock_pipeline):
+ zeroscope = ZeroscopeTTV()
+ mock_pipeline_instance = MagicMock()
+ mock_pipeline.from_pretrained.return_value = (
+ mock_pipeline_instance
+ )
+ mock_pipeline_instance.return_value = MagicMock(
+ frames="Generated frames"
+ )
+ mock_pipeline_instance.side_effect = Exception("Test error")
+ with pytest.raises(Exception, match="Test error"):
+ zeroscope.forward("Test task")
+
+
+@patch("swarms.models.zeroscope.DiffusionPipeline")
+@patch("swarms.models.zeroscope.DPMSolverMultistepScheduler")
+def test_zeroscope_ttv_call(mock_scheduler, mock_pipeline):
+ zeroscope = ZeroscopeTTV()
+ mock_pipeline_instance = MagicMock()
+ mock_pipeline.from_pretrained.return_value = (
+ mock_pipeline_instance
+ )
+ mock_pipeline_instance.return_value = MagicMock(
+ frames="Generated frames"
+ )
+ result = zeroscope.__call__("Test task")
+ assert result == "Generated frames"
+ mock_pipeline_instance.assert_called_once_with(
+ "Test task",
+ num_inference_steps=40,
+ height=320,
+ width=576,
+ num_frames=36,
+ )
+
+
+@patch("swarms.models.zeroscope.DiffusionPipeline")
+@patch("swarms.models.zeroscope.DPMSolverMultistepScheduler")
+def test_zeroscope_ttv_call_error(mock_scheduler, mock_pipeline):
+ zeroscope = ZeroscopeTTV()
+ mock_pipeline_instance = MagicMock()
+ mock_pipeline.from_pretrained.return_value = (
+ mock_pipeline_instance
+ )
+ mock_pipeline_instance.return_value = MagicMock(
+ frames="Generated frames"
+ )
+ mock_pipeline_instance.side_effect = Exception("Test error")
+ with pytest.raises(Exception, match="Test error"):
+ zeroscope.__call__("Test task")
+
+
+@patch("swarms.models.zeroscope.DiffusionPipeline")
+@patch("swarms.models.zeroscope.DPMSolverMultistepScheduler")
+def test_zeroscope_ttv_save_video_path(mock_scheduler, mock_pipeline):
+ zeroscope = ZeroscopeTTV()
+ mock_pipeline_instance = MagicMock()
+ mock_pipeline.from_pretrained.return_value = (
+ mock_pipeline_instance
+ )
+ mock_pipeline_instance.return_value = MagicMock(
+ frames="Generated frames"
+ )
+ result = zeroscope.save_video_path("Test video path")
+ assert result == "Test video path"
+ mock_pipeline_instance.assert_called_once_with(
+ "Test video path",
+ num_inference_steps=40,
+ height=320,
+ width=576,
+ num_frames=36,
+ )
diff --git a/tests/structs/test_agent.py b/tests/structs/test_agent.py
index a8e1cf92..8e5b11be 100644
--- a/tests/structs/test_agent.py
+++ b/tests/structs/test_agent.py
@@ -347,7 +347,7 @@ def test_flow_response_filtering(flow_instance):
def test_flow_undo_last(flow_instance):
# Test the undo functionality
response1 = flow_instance.run("Task 1")
- response2 = flow_instance.run("Task 2")
+ flow_instance.run("Task 2")
previous_state, message = flow_instance.undo_last()
assert response1 == previous_state
assert "Restored to" in message
@@ -577,7 +577,7 @@ def test_flow_rollback(flow_instance):
# Test rolling back to a previous state
state1 = flow_instance.get_state()
flow_instance.change_prompt("New prompt")
- state2 = flow_instance.get_state()
+ flow_instance.get_state()
flow_instance.rollback_to_state(state1)
assert (
flow_instance.get_current_prompt() == state1["current_prompt"]
diff --git a/tests/structs/test_autoscaler.py b/tests/structs/test_autoscaler.py
index cfb9560e..ac3da51a 100644
--- a/tests/structs/test_autoscaler.py
+++ b/tests/structs/test_autoscaler.py
@@ -1,7 +1,9 @@
import os
from dotenv import load_dotenv
-from pytest import patch
+from unittest.mock import MagicMock, patch
+
+import pytest
from swarms.models import OpenAIChat
from swarms.structs import Agent
@@ -23,18 +25,18 @@ def test_autoscaler_init():
assert autoscaler.scale_up_factor == 1
assert autoscaler.idle_threshold == 0.2
assert autoscaler.busy_threshold == 0.7
- assert autoscaler.autoscale == True
+ assert autoscaler.autoscale is True
assert autoscaler.min_agents == 1
assert autoscaler.max_agents == 5
- assert autoscaler.custom_scale_strategy == None
+ assert autoscaler.custom_scale_strategy is None
assert len(autoscaler.agents_pool) == 5
- assert autoscaler.task_queue.empty() == True
+ assert autoscaler.task_queue.empty() is True
def test_autoscaler_add_task():
autoscaler = AutoScaler(initial_agents=5, agent=agent)
autoscaler.add_task("task1")
- assert autoscaler.task_queue.empty() == False
+ assert autoscaler.task_queue.empty() is False
def test_autoscaler_run():
@@ -75,7 +77,7 @@ def test_autoscaler_get_agent_by_id():
def test_autoscaler_get_agent_by_id_not_found():
autoscaler = AutoScaler(initial_agents=5, agent=agent)
agent = autoscaler.get_agent_by_id("fake_id")
- assert agent == None
+ assert agent is None
@patch("swarms.swarms.Agent.is_healthy")
@@ -138,3 +140,140 @@ def test_autoscaler_print_dashboard(mock_print):
autoscaler = AutoScaler(initial_agents=5, agent=agent)
autoscaler.print_dashboard()
mock_print.assert_called()
+
+
+@patch("swarms.structs.autoscaler.logging")
+def test_check_agent_health_all_healthy(mock_logging):
+ autoscaler = AutoScaler(initial_agents=5, agent=agent)
+ for agent in autoscaler.agents_pool:
+ agent.is_healthy = MagicMock(return_value=True)
+ autoscaler.check_agent_health()
+ mock_logging.warning.assert_not_called()
+
+
+@patch("swarms.structs.autoscaler.logging")
+def test_check_agent_health_some_unhealthy(mock_logging):
+ autoscaler = AutoScaler(initial_agents=5, agent=agent)
+ for i, agent in enumerate(autoscaler.agents_pool):
+ agent.is_healthy = MagicMock(return_value=(i % 2 == 0))
+ autoscaler.check_agent_health()
+ assert mock_logging.warning.call_count == 2
+
+
+@patch("swarms.structs.autoscaler.logging")
+def test_check_agent_health_all_unhealthy(mock_logging):
+ autoscaler = AutoScaler(initial_agents=5, agent=agent)
+ for agent in autoscaler.agents_pool:
+ agent.is_healthy = MagicMock(return_value=False)
+ autoscaler.check_agent_health()
+ assert mock_logging.warning.call_count == 5
+
+
+@patch("swarms.structs.autoscaler.Agent")
+def test_add_agent(mock_agent):
+ autoscaler = AutoScaler(initial_agents=5, agent=agent)
+ initial_count = len(autoscaler.agents_pool)
+ autoscaler.add_agent()
+ assert len(autoscaler.agents_pool) == initial_count + 1
+ mock_agent.assert_called_once()
+
+
+@patch("swarms.structs.autoscaler.Agent")
+def test_remove_agent(mock_agent):
+ autoscaler = AutoScaler(initial_agents=5, agent=agent)
+ initial_count = len(autoscaler.agents_pool)
+ autoscaler.remove_agent()
+ assert len(autoscaler.agents_pool) == initial_count - 1
+
+
+@patch("swarms.structs.autoscaler.AutoScaler.add_agent")
+@patch("swarms.structs.autoscaler.AutoScaler.remove_agent")
+def test_scale(mock_remove_agent, mock_add_agent):
+ autoscaler = AutoScaler(initial_agents=5, agent=agent)
+ autoscaler.scale(10)
+ assert mock_add_agent.call_count == 5
+ assert mock_remove_agent.call_count == 0
+
+ mock_add_agent.reset_mock()
+ mock_remove_agent.reset_mock()
+
+ autoscaler.scale(3)
+ assert mock_add_agent.call_count == 0
+ assert mock_remove_agent.call_count == 2
+
+
+def test_add_task_success():
+ autoscaler = AutoScaler(initial_agents=5)
+ initial_queue_size = autoscaler.task_queue.qsize()
+ autoscaler.add_task("test_task")
+ assert autoscaler.task_queue.qsize() == initial_queue_size + 1
+
+
+@patch("swarms.structs.autoscaler.queue.Queue.put")
+def test_add_task_exception(mock_put):
+ mock_put.side_effect = Exception("test error")
+ autoscaler = AutoScaler(initial_agents=5)
+ with pytest.raises(Exception) as e:
+ autoscaler.add_task("test_task")
+ assert str(e.value) == "test error"
+
+
+def test_autoscaler_initialization():
+ autoscaler = AutoScaler(
+ initial_agents=5,
+ scale_up_factor=2,
+ idle_threshold=0.1,
+ busy_threshold=0.8,
+ agent=agent,
+ )
+ assert isinstance(autoscaler, AutoScaler)
+ assert autoscaler.scale_up_factor == 2
+ assert autoscaler.idle_threshold == 0.1
+ assert autoscaler.busy_threshold == 0.8
+ assert len(autoscaler.agents_pool) == 5
+
+
+def test_autoscaler_add_task():
+ autoscaler = AutoScaler(agent=agent)
+ autoscaler.add_task("task1")
+ assert autoscaler.task_queue.qsize() == 1
+
+
+def test_autoscaler_scale_up():
+ autoscaler = AutoScaler(
+ initial_agents=5, scale_up_factor=2, agent=agent
+ )
+ autoscaler.scale_up()
+ assert len(autoscaler.agents_pool) == 10
+
+
+def test_autoscaler_scale_down():
+ autoscaler = AutoScaler(initial_agents=5, agent=agent)
+ autoscaler.scale_down()
+ assert len(autoscaler.agents_pool) == 4
+
+
+@patch("swarms.swarms.AutoScaler.scale_up")
+@patch("swarms.swarms.AutoScaler.scale_down")
+def test_autoscaler_monitor_and_scale(mock_scale_down, mock_scale_up):
+ autoscaler = AutoScaler(initial_agents=5, agent=agent)
+ autoscaler.add_task("task1")
+ autoscaler.monitor_and_scale()
+ mock_scale_up.assert_called_once()
+ mock_scale_down.assert_called_once()
+
+
+@patch("swarms.swarms.AutoScaler.monitor_and_scale")
+@patch("swarms.swarms.agent.run")
+def test_autoscaler_start(mock_run, mock_monitor_and_scale):
+ autoscaler = AutoScaler(initial_agents=5, agent=agent)
+ autoscaler.add_task("task1")
+ autoscaler.start()
+ mock_run.assert_called_once()
+ mock_monitor_and_scale.assert_called_once()
+
+
+def test_autoscaler_del_agent():
+ autoscaler = AutoScaler(initial_agents=5, agent=agent)
+ autoscaler.del_agent()
+ assert len(autoscaler.agents_pool) == 4
diff --git a/tests/swarms/test_base.py b/tests/structs/test_base.py
similarity index 99%
rename from tests/swarms/test_base.py
rename to tests/structs/test_base.py
index 9641ed7e..8b54dec0 100644
--- a/tests/swarms/test_base.py
+++ b/tests/structs/test_base.py
@@ -1,7 +1,7 @@
import pytest
import os
from datetime import datetime
-from swarms.swarms.base import BaseStructure
+from swarms.structs.base import BaseStructure
class TestBaseStructure:
diff --git a/tests/structs/test_base_workflow.py b/tests/structs/test_base_workflow.py
new file mode 100644
index 00000000..17be5ea8
--- /dev/null
+++ b/tests/structs/test_base_workflow.py
@@ -0,0 +1,66 @@
+import os
+import pytest
+import json
+from swarms.models import OpenAIChat
+from swarms.structs import BaseWorkflow
+
+from dotenv import load_dotenv
+
+load_dotenv()
+
+api_key = os.environ.get("OPENAI_API_KEY")
+
+
+def setup_workflow():
+ llm = OpenAIChat(openai_api_key=api_key)
+ workflow = BaseWorkflow(max_loops=1)
+ workflow.add("What's the weather in miami", llm)
+ workflow.add("Create a report on these metrics", llm)
+ workflow.save_workflow_state("workflow_state.json")
+ return workflow
+
+
+def teardown_workflow():
+ os.remove("workflow_state.json")
+
+
+def test_load_workflow_state():
+ workflow = setup_workflow()
+ workflow.load_workflow_state("workflow_state.json")
+ assert workflow.max_loops == 1
+ assert len(workflow.tasks) == 2
+ assert (
+ workflow.tasks[0].description == "What's the weather in miami"
+ )
+ assert (
+ workflow.tasks[1].description
+ == "Create a report on these metrics"
+ )
+ teardown_workflow()
+
+
+def test_load_workflow_state_with_missing_file():
+ workflow = setup_workflow()
+ with pytest.raises(FileNotFoundError):
+ workflow.load_workflow_state("non_existent_file.json")
+ teardown_workflow()
+
+
+def test_load_workflow_state_with_invalid_file():
+ workflow = setup_workflow()
+ with open("invalid_file.json", "w") as f:
+ f.write("This is not valid JSON")
+ with pytest.raises(json.JSONDecodeError):
+ workflow.load_workflow_state("invalid_file.json")
+ os.remove("invalid_file.json")
+ teardown_workflow()
+
+
+def test_load_workflow_state_with_missing_keys():
+ workflow = setup_workflow()
+ with open("missing_keys.json", "w") as f:
+ json.dump({"max_loops": 1}, f)
+ with pytest.raises(KeyError):
+ workflow.load_workflow_state("missing_keys.json")
+ os.remove("missing_keys.json")
+ teardown_workflow()
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)
diff --git a/tests/structs/test_concurrent_workflow.py b/tests/structs/test_concurrent_workflow.py
new file mode 100644
index 00000000..206e8e2a
--- /dev/null
+++ b/tests/structs/test_concurrent_workflow.py
@@ -0,0 +1,56 @@
+from unittest.mock import Mock, create_autospec, patch
+from concurrent.futures import Future
+from swarms.structs import ConcurrentWorkflow, Task, Agent
+
+
+def test_add():
+ workflow = ConcurrentWorkflow(max_workers=2)
+ task = Mock(spec=Task)
+ workflow.add(task)
+ assert task in workflow.tasks
+
+
+def test_run():
+ workflow = ConcurrentWorkflow(max_workers=2)
+ task1 = create_autospec(Task)
+ task2 = create_autospec(Task)
+ workflow.add(task1)
+ workflow.add(task2)
+
+ with patch(
+ "concurrent.futures.ThreadPoolExecutor"
+ ) as mock_executor:
+ future1 = Future()
+ future1.set_result(None)
+ future2 = Future()
+ future2.set_result(None)
+
+ mock_executor.return_value.__enter__.return_value.submit.side_effect = [
+ future1,
+ future2,
+ ]
+ mock_executor.return_value.__enter__.return_value.as_completed.return_value = [
+ future1,
+ future2,
+ ]
+
+ workflow.run()
+
+ task1.execute.assert_called_once()
+ task2.execute.assert_called_once()
+
+
+def test_execute_task():
+ workflow = ConcurrentWorkflow(max_workers=2)
+ task = create_autospec(Task)
+ workflow._execute_task(task)
+ task.execute.assert_called_once()
+
+
+def test_agent_execution():
+ workflow = ConcurrentWorkflow(max_workers=2)
+ agent = create_autospec(Agent)
+ task = Task(agent)
+ workflow.add(task)
+ workflow._execute_task(task)
+ agent.execute.assert_called_once()
diff --git a/tests/structs/test_conversation.py b/tests/structs/test_conversation.py
new file mode 100644
index 00000000..84673a42
--- /dev/null
+++ b/tests/structs/test_conversation.py
@@ -0,0 +1,241 @@
+import pytest
+from swarms.structs.conversation import Conversation
+
+
+@pytest.fixture
+def conversation():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ conv.add("assistant", "Hello, user!")
+ return conv
+
+
+def test_add_message():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ assert len(conv.conversation_history) == 1
+ assert conv.conversation_history[0]["role"] == "user"
+ assert conv.conversation_history[0]["content"] == "Hello, world!"
+
+
+def test_add_message_with_time():
+ conv = Conversation(time_enabled=True)
+ conv.add("user", "Hello, world!")
+ assert len(conv.conversation_history) == 1
+ assert conv.conversation_history[0]["role"] == "user"
+ assert conv.conversation_history[0]["content"] == "Hello, world!"
+ assert "timestamp" in conv.conversation_history[0]
+
+
+def test_delete_message():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ conv.delete(0)
+ assert len(conv.conversation_history) == 0
+
+
+def test_delete_message_out_of_bounds():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ with pytest.raises(IndexError):
+ conv.delete(1)
+
+
+def test_update_message():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ conv.update(0, "assistant", "Hello, user!")
+ assert len(conv.conversation_history) == 1
+ assert conv.conversation_history[0]["role"] == "assistant"
+ assert conv.conversation_history[0]["content"] == "Hello, user!"
+
+
+def test_update_message_out_of_bounds():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ with pytest.raises(IndexError):
+ conv.update(1, "assistant", "Hello, user!")
+
+
+def test_return_history_as_string_with_messages(conversation):
+ result = conversation.return_history_as_string()
+ assert result is not None
+
+
+def test_return_history_as_string_with_no_messages():
+ conv = Conversation()
+ result = conv.return_history_as_string()
+ assert result == ""
+
+
+@pytest.mark.parametrize(
+ "role, content",
+ [
+ ("user", "Hello, world!"),
+ ("assistant", "Hello, user!"),
+ ("system", "System message"),
+ ("function", "Function message"),
+ ],
+)
+def test_return_history_as_string_with_different_roles(role, content):
+ conv = Conversation()
+ conv.add(role, content)
+ result = conv.return_history_as_string()
+ expected = f"{role}: {content}\n\n"
+ assert result == expected
+
+
+@pytest.mark.parametrize("message_count", range(1, 11))
+def test_return_history_as_string_with_multiple_messages(
+ message_count,
+):
+ conv = Conversation()
+ for i in range(message_count):
+ conv.add("user", f"Message {i + 1}")
+ result = conv.return_history_as_string()
+ expected = "".join(
+ [f"user: Message {i + 1}\n\n" for i in range(message_count)]
+ )
+ assert result == expected
+
+
+@pytest.mark.parametrize(
+ "content",
+ [
+ "Hello, world!",
+ "This is a longer message with multiple words.",
+ "This message\nhas multiple\nlines.",
+ "This message has special characters: !@#$%^&*()",
+ "This message has unicode characters: δ½ ε₯½οΌδΈηοΌ",
+ ],
+)
+def test_return_history_as_string_with_different_contents(content):
+ conv = Conversation()
+ conv.add("user", content)
+ result = conv.return_history_as_string()
+ expected = f"user: {content}\n\n"
+ assert result == expected
+
+
+def test_return_history_as_string_with_large_message(conversation):
+ large_message = "Hello, world! " * 10000 # 10,000 repetitions
+ conversation.add("user", large_message)
+ result = conversation.return_history_as_string()
+ expected = (
+ "user: Hello, world!\n\nassistant: Hello, user!\n\nuser:"
+ f" {large_message}\n\n"
+ )
+ assert result == expected
+
+
+def test_search_keyword_in_conversation(conversation):
+ result = conversation.search_keyword_in_conversation("Hello")
+ assert len(result) == 2
+ assert result[0]["content"] == "Hello, world!"
+ assert result[1]["content"] == "Hello, user!"
+
+
+def test_export_import_conversation(conversation, tmp_path):
+ filename = tmp_path / "conversation.txt"
+ conversation.export_conversation(filename)
+ new_conversation = Conversation()
+ new_conversation.import_conversation(filename)
+ assert (
+ new_conversation.return_history_as_string()
+ == conversation.return_history_as_string()
+ )
+
+
+def test_count_messages_by_role(conversation):
+ counts = conversation.count_messages_by_role()
+ assert counts["user"] == 1
+ assert counts["assistant"] == 1
+
+
+def test_display_conversation(capsys, conversation):
+ conversation.display_conversation()
+ captured = capsys.readouterr()
+ assert "user: Hello, world!\n\n" in captured.out
+ assert "assistant: Hello, user!\n\n" in captured.out
+
+
+def test_display_conversation_detailed(capsys, conversation):
+ conversation.display_conversation(detailed=True)
+ captured = capsys.readouterr()
+ assert "user: Hello, world!\n\n" in captured.out
+ assert "assistant: Hello, user!\n\n" in captured.out
+
+
+def test_search():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ conv.add("assistant", "Hello, user!")
+ results = conv.search("Hello")
+ assert len(results) == 2
+ assert results[0]["content"] == "Hello, world!"
+ assert results[1]["content"] == "Hello, user!"
+
+
+def test_return_history_as_string():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ conv.add("assistant", "Hello, user!")
+ result = conv.return_history_as_string()
+ expected = "user: Hello, world!\n\nassistant: Hello, user!\n\n"
+ assert result == expected
+
+
+def test_search_no_results():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ conv.add("assistant", "Hello, user!")
+ results = conv.search("Goodbye")
+ assert len(results) == 0
+
+
+def test_search_case_insensitive():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ conv.add("assistant", "Hello, user!")
+ results = conv.search("hello")
+ assert len(results) == 2
+ assert results[0]["content"] == "Hello, world!"
+ assert results[1]["content"] == "Hello, user!"
+
+
+def test_search_multiple_occurrences():
+ conv = Conversation()
+ conv.add("user", "Hello, world! Hello, world!")
+ conv.add("assistant", "Hello, user!")
+ results = conv.search("Hello")
+ assert len(results) == 2
+ assert results[0]["content"] == "Hello, world! Hello, world!"
+ assert results[1]["content"] == "Hello, user!"
+
+
+def test_query_no_results():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ conv.add("assistant", "Hello, user!")
+ results = conv.query("Goodbye")
+ assert len(results) == 0
+
+
+def test_query_case_insensitive():
+ conv = Conversation()
+ conv.add("user", "Hello, world!")
+ conv.add("assistant", "Hello, user!")
+ results = conv.query("hello")
+ assert len(results) == 2
+ assert results[0]["content"] == "Hello, world!"
+ assert results[1]["content"] == "Hello, user!"
+
+
+def test_query_multiple_occurrences():
+ conv = Conversation()
+ conv.add("user", "Hello, world! Hello, world!")
+ conv.add("assistant", "Hello, user!")
+ results = conv.query("Hello")
+ assert len(results) == 2
+ assert results[0]["content"] == "Hello, world! Hello, world!"
+ assert results[1]["content"] == "Hello, user!"
diff --git a/tests/swarms/test_groupchat.py b/tests/structs/test_groupchat.py
similarity index 99%
rename from tests/swarms/test_groupchat.py
rename to tests/structs/test_groupchat.py
index ce17a4d2..e8096d9c 100644
--- a/tests/swarms/test_groupchat.py
+++ b/tests/structs/test_groupchat.py
@@ -3,7 +3,7 @@ import pytest
from swarms.models import OpenAIChat
from swarms.models.anthropic import Anthropic
from swarms.structs.agent import Agent
-from swarms.swarms.groupchat import GroupChat, GroupChatManager
+from swarms.structs.groupchat import GroupChat, GroupChatManager
llm = OpenAIChat()
llm2 = Anthropic()
diff --git a/tests/structs/test_model_parallizer.py b/tests/structs/test_model_parallizer.py
new file mode 100644
index 00000000..37ca43db
--- /dev/null
+++ b/tests/structs/test_model_parallizer.py
@@ -0,0 +1,146 @@
+import pytest
+from swarms.structs.model_parallizer import ModelParallelizer
+from swarms.models import (
+ HuggingfaceLLM,
+ Mixtral,
+ GPT4VisionAPI,
+ ZeroscopeTTV,
+)
+
+# Initialize the models
+custom_config = {
+ "quantize": True,
+ "quantization_config": {"load_in_4bit": True},
+ "verbose": True,
+}
+huggingface_llm = HuggingfaceLLM(
+ model_id="NousResearch/Nous-Hermes-2-Vision-Alpha",
+ **custom_config,
+)
+mixtral = Mixtral(load_in_4bit=True, use_flash_attention_2=True)
+gpt4_vision_api = GPT4VisionAPI(max_tokens=1000)
+zeroscope_ttv = ZeroscopeTTV()
+
+
+def test_init():
+ mp = ModelParallelizer(
+ [
+ huggingface_llm,
+ mixtral,
+ gpt4_vision_api,
+ zeroscope_ttv,
+ ]
+ )
+ assert isinstance(mp, ModelParallelizer)
+
+
+def test_run():
+ mp = ModelParallelizer([huggingface_llm])
+ result = mp.run(
+ "Create a list of known biggest risks of structural collapse"
+ " with references"
+ )
+ assert isinstance(result, str)
+
+
+def test_run_all():
+ mp = ModelParallelizer(
+ [
+ huggingface_llm,
+ mixtral,
+ gpt4_vision_api,
+ zeroscope_ttv,
+ ]
+ )
+ result = mp.run_all(
+ "Create a list of known biggest risks of structural collapse"
+ " with references"
+ )
+ assert isinstance(result, list)
+ assert len(result) == 5
+
+
+def test_add_llm():
+ mp = ModelParallelizer([huggingface_llm])
+ mp.add_llm(mixtral)
+ assert len(mp.llms) == 2
+
+
+def test_remove_llm():
+ mp = ModelParallelizer([huggingface_llm, mixtral])
+ mp.remove_llm(mixtral)
+ assert len(mp.llms) == 1
+
+
+def test_save_responses_to_file(tmp_path):
+ mp = ModelParallelizer([huggingface_llm])
+ mp.run(
+ "Create a list of known biggest risks of structural collapse"
+ " with references"
+ )
+ file = tmp_path / "responses.txt"
+ mp.save_responses_to_file(file)
+ assert file.read_text() != ""
+
+
+def test_get_task_history():
+ mp = ModelParallelizer([huggingface_llm])
+ mp.run(
+ "Create a list of known biggest risks of structural collapse"
+ " with references"
+ )
+ assert mp.get_task_history() == [
+ "Create a list of known biggest risks of structural collapse"
+ " with references"
+ ]
+
+
+def test_summary(capsys):
+ mp = ModelParallelizer([huggingface_llm])
+ mp.run(
+ "Create a list of known biggest risks of structural collapse"
+ " with references"
+ )
+ mp.summary()
+ captured = capsys.readouterr()
+ assert "Tasks History:" in captured.out
+
+
+def test_enable_load_balancing():
+ mp = ModelParallelizer([huggingface_llm])
+ mp.enable_load_balancing()
+ assert mp.load_balancing is True
+
+
+def test_disable_load_balancing():
+ mp = ModelParallelizer([huggingface_llm])
+ mp.disable_load_balancing()
+ assert mp.load_balancing is False
+
+
+def test_concurrent_run():
+ mp = ModelParallelizer([huggingface_llm, mixtral])
+ result = mp.concurrent_run(
+ "Create a list of known biggest risks of structural collapse"
+ " with references"
+ )
+ assert isinstance(result, list)
+ assert len(result) == 2
+
+
+def test_concurrent_run_no_task():
+ mp = ModelParallelizer([huggingface_llm])
+ with pytest.raises(TypeError):
+ mp.concurrent_run()
+
+
+def test_concurrent_run_non_string_task():
+ mp = ModelParallelizer([huggingface_llm])
+ with pytest.raises(TypeError):
+ mp.concurrent_run(123)
+
+
+def test_concurrent_run_empty_task():
+ mp = ModelParallelizer([huggingface_llm])
+ result = mp.concurrent_run("")
+ assert result == [""]
diff --git a/tests/swarms/test_multi_agent_collab.py b/tests/structs/test_multi_agent_collab.py
similarity index 84%
rename from tests/swarms/test_multi_agent_collab.py
rename to tests/structs/test_multi_agent_collab.py
index e30358aa..475b32b3 100644
--- a/tests/swarms/test_multi_agent_collab.py
+++ b/tests/structs/test_multi_agent_collab.py
@@ -4,10 +4,8 @@ import pytest
from unittest.mock import Mock
from swarms.structs import Agent
from swarms.models import OpenAIChat
-from swarms.swarms.multi_agent_collab import (
+from swarms.structs.multi_agent_collab import (
MultiAgentCollaboration,
- select_next_speaker_director,
- select_speaker_round_table,
)
# Sample agents for testing
@@ -26,7 +24,7 @@ def test_collaboration_initialization(collaboration):
assert callable(collaboration.select_next_speaker)
assert collaboration.max_iters == 10
assert collaboration.results == []
- assert collaboration.logging == True
+ assert collaboration.logging is True
def test_reset(collaboration):
@@ -105,13 +103,6 @@ def test_set_interaction_rules(collaboration):
assert collaboration.interaction_rules == rules
-def test_set_interaction_rules(collaboration):
- rules = {"rule1": "action1", "rule2": "action2"}
- collaboration.set_interaction_rules(rules)
- assert hasattr(collaboration, "interaction_rules")
- assert collaboration.interaction_rules == rules
-
-
def test_repr(collaboration):
repr_str = repr(collaboration)
assert isinstance(repr_str, str)
@@ -144,17 +135,6 @@ def test_save(collaboration, tmp_path):
# Add more tests here...
-
-# Example of parameterized test for different selection functions
-@pytest.mark.parametrize(
- "selection_function",
- [select_next_speaker_director, select_speaker_round_table],
-)
-def test_selection_functions(collaboration, selection_function):
- collaboration.select_next_speaker = selection_function
- assert callable(collaboration.select_next_speaker)
-
-
# Add more parameterized tests for different scenarios...
diff --git a/tests/structs/test_nonlinear_workflow.py b/tests/structs/test_nonlinear_workflow.py
new file mode 100644
index 00000000..8919fc76
--- /dev/null
+++ b/tests/structs/test_nonlinear_workflow.py
@@ -0,0 +1,49 @@
+import pytest
+from swarms.structs import NonlinearWorkflow, Task
+from swarms.models import OpenAIChat
+
+
+class TestNonlinearWorkflow:
+ def test_add_task(self):
+ llm = OpenAIChat(openai_api_key="")
+ task = Task(llm, "What's the weather in miami")
+ workflow = NonlinearWorkflow()
+ workflow.add(task)
+ assert task.name in workflow.tasks
+ assert task.name in workflow.edges
+
+ def test_run_without_tasks(self):
+ workflow = NonlinearWorkflow()
+ # No exception should be raised
+ workflow.run()
+
+ def test_run_with_single_task(self):
+ llm = OpenAIChat(openai_api_key="")
+ task = Task(llm, "What's the weather in miami")
+ workflow = NonlinearWorkflow()
+ workflow.add(task)
+ # No exception should be raised
+ workflow.run()
+
+ def test_run_with_circular_dependency(self):
+ llm = OpenAIChat(openai_api_key="")
+ task1 = Task(llm, "What's the weather in miami")
+ task2 = Task(llm, "What's the weather in new york")
+ workflow = NonlinearWorkflow()
+ workflow.add(task1, task2.name)
+ workflow.add(task2, task1.name)
+ with pytest.raises(
+ Exception, match="Circular dependency detected"
+ ):
+ workflow.run()
+
+ def test_run_with_stopping_token(self):
+ llm = OpenAIChat(openai_api_key="")
+ task1 = Task(llm, "What's the weather in miami")
+ task2 = Task(llm, "What's the weather in new york")
+ workflow = NonlinearWorkflow(stopping_token="stop")
+ workflow.add(task1)
+ workflow.add(task2)
+ # Assuming that task1's execute method returns "stop"
+ # No exception should be raised
+ workflow.run()
diff --git a/tests/structs/test_recursive_workflow.py b/tests/structs/test_recursive_workflow.py
new file mode 100644
index 00000000..171c7cad
--- /dev/null
+++ b/tests/structs/test_recursive_workflow.py
@@ -0,0 +1,72 @@
+import pytest
+from unittest.mock import Mock, create_autospec
+from swarms.models import OpenAIChat
+from swarms.structs import RecursiveWorkflow, Task
+
+
+def test_add():
+ workflow = RecursiveWorkflow(stop_token="")
+ task = Mock(spec=Task)
+ workflow.add(task)
+ assert task in workflow.tasks
+
+
+def test_run():
+ workflow = RecursiveWorkflow(stop_token="")
+ agent1 = create_autospec(OpenAIChat)
+ agent2 = create_autospec(OpenAIChat)
+ task1 = Task("What's the weather in miami", agent1)
+ task2 = Task("What's the weather in miami", agent2)
+ workflow.add(task1)
+ workflow.add(task2)
+
+ agent1.execute.return_value = "Not done"
+ agent2.execute.return_value = ""
+
+ workflow.run()
+
+ assert agent1.execute.call_count >= 1
+ assert agent2.execute.call_count == 1
+
+
+def test_run_no_tasks():
+ workflow = RecursiveWorkflow(stop_token="")
+ # No tasks are added to the workflow
+ # This should not raise any errors
+ workflow.run()
+
+
+def test_run_stop_token_not_in_result():
+ workflow = RecursiveWorkflow(stop_token="")
+ agent = create_autospec(OpenAIChat)
+ task = Task("What's the weather in miami", agent)
+ workflow.add(task)
+
+ agent.execute.return_value = "Not done"
+
+ # If the stop token is never found in the result, the workflow could run forever.
+ # To prevent this, we'll set a maximum number of iterations.
+ max_iterations = 1000
+ for _ in range(max_iterations):
+ try:
+ workflow.run()
+ except RecursionError:
+ pytest.fail(
+ "RecursiveWorkflow.run caused a RecursionError"
+ )
+
+ assert agent.execute.call_count == max_iterations
+
+
+def test_run_stop_token_in_result():
+ workflow = RecursiveWorkflow(stop_token="")
+ agent = create_autospec(OpenAIChat)
+ task = Task("What's the weather in miami", agent)
+ workflow.add(task)
+
+ agent.execute.return_value = ""
+
+ workflow.run()
+
+ # If the stop token is found in the result, the workflow should stop running the task.
+ assert agent.execute.call_count == 1
diff --git a/tests/structs/test_swarmnetwork.py b/tests/structs/test_swarmnetwork.py
new file mode 100644
index 00000000..9dc6d903
--- /dev/null
+++ b/tests/structs/test_swarmnetwork.py
@@ -0,0 +1,52 @@
+from unittest.mock import Mock, patch
+
+import pytest
+
+from swarms.structs.agent import Agent
+from swarms.structs.swarm_net import SwarmNetwork
+
+
+@pytest.fixture
+def swarm_network():
+ agents = [Agent(id=f"Agent_{i}") for i in range(5)]
+ return SwarmNetwork(agents=agents)
+
+
+def test_swarm_network_init(swarm_network):
+ assert isinstance(swarm_network.agents, list)
+ assert len(swarm_network.agents) == 5
+
+
+@patch("swarms.structs.swarm_net.SwarmNetwork.logger")
+def test_run(mock_logger, swarm_network):
+ swarm_network.run()
+ assert (
+ mock_logger.info.call_count == 10
+ ) # 2 log messages per agent
+
+
+def test_run_with_mocked_agents(mocker, swarm_network):
+ mock_agents = [Mock(spec=Agent) for _ in range(5)]
+ mocker.patch.object(swarm_network, "agents", mock_agents)
+ swarm_network.run()
+ for mock_agent in mock_agents:
+ assert mock_agent.run.called
+
+
+def test_swarm_network_with_no_agents():
+ swarm_network = SwarmNetwork(agents=[])
+ assert swarm_network.agents == []
+
+
+def test_swarm_network_add_agent(swarm_network):
+ new_agent = Agent(id="Agent_5")
+ swarm_network.add_agent(new_agent)
+ assert len(swarm_network.agents) == 6
+ assert swarm_network.agents[-1] == new_agent
+
+
+def test_swarm_network_remove_agent(swarm_network):
+ agent_to_remove = swarm_network.agents[0]
+ swarm_network.remove_agent(agent_to_remove)
+ assert len(swarm_network.agents) == 4
+ assert agent_to_remove not in swarm_network.agents
diff --git a/tests/structs/test_task.py b/tests/structs/test_task.py
index 2c116402..8a76549c 100644
--- a/tests/structs/test_task.py
+++ b/tests/structs/test_task.py
@@ -1,4 +1,3 @@
-import os
from unittest.mock import Mock
import pytest
@@ -10,6 +9,8 @@ from swarms.prompts.multi_modal_autonomous_instruction_prompt import (
)
from swarms.structs.agent import Agent
from swarms.structs.task import Task
+import datetime
+from datetime import timedelta
load_dotenv()
@@ -163,4 +164,120 @@ def test_execute():
agent = Agent()
task = Task(id="5", task="Task5", result=None, agents=[agent])
# Assuming execute method returns True on successful execution
- assert task.execute() == True
+ assert task.execute() is True
+
+
+def test_task_execute_with_agent(mocker):
+ mock_agent = mocker.Mock(spec=Agent)
+ mock_agent.run.return_value = "result"
+ task = Task(description="Test task", agent=mock_agent)
+ task.execute()
+ assert task.result == "result"
+ assert task.history == ["result"]
+
+
+def test_task_execute_with_callable(mocker):
+ mock_callable = mocker.Mock()
+ mock_callable.run.return_value = "result"
+ task = Task(description="Test task", agent=mock_callable)
+ task.execute()
+ assert task.result == "result"
+ assert task.history == ["result"]
+
+
+def test_task_execute_with_condition(mocker):
+ mock_agent = mocker.Mock(spec=Agent)
+ mock_agent.run.return_value = "result"
+ condition = mocker.Mock(return_value=True)
+ task = Task(
+ description="Test task", agent=mock_agent, condition=condition
+ )
+ task.execute()
+ assert task.result == "result"
+ assert task.history == ["result"]
+
+
+def test_task_execute_with_condition_false(mocker):
+ mock_agent = mocker.Mock(spec=Agent)
+ mock_agent.run.return_value = "result"
+ condition = mocker.Mock(return_value=False)
+ task = Task(
+ description="Test task", agent=mock_agent, condition=condition
+ )
+ task.execute()
+ assert task.result is None
+ assert task.history == []
+
+
+def test_task_execute_with_action(mocker):
+ mock_agent = mocker.Mock(spec=Agent)
+ mock_agent.run.return_value = "result"
+ action = mocker.Mock()
+ task = Task(
+ description="Test task", agent=mock_agent, action=action
+ )
+ task.execute()
+ assert task.result == "result"
+ assert task.history == ["result"]
+ action.assert_called_once()
+
+
+def test_task_handle_scheduled_task_now(mocker):
+ mock_agent = mocker.Mock(spec=Agent)
+ mock_agent.run.return_value = "result"
+ task = Task(
+ description="Test task",
+ agent=mock_agent,
+ schedule_time=datetime.now(),
+ )
+ task.handle_scheduled_task()
+ assert task.result == "result"
+ assert task.history == ["result"]
+
+
+def test_task_handle_scheduled_task_future(mocker):
+ mock_agent = mocker.Mock(spec=Agent)
+ mock_agent.run.return_value = "result"
+ task = Task(
+ description="Test task",
+ agent=mock_agent,
+ schedule_time=datetime.now() + timedelta(days=1),
+ )
+ with mocker.patch.object(
+ task.scheduler, "enter"
+ ) as mock_enter, mocker.patch.object(
+ task.scheduler, "run"
+ ) as mock_run:
+ task.handle_scheduled_task()
+ mock_enter.assert_called_once()
+ mock_run.assert_called_once()
+
+
+def test_task_set_trigger():
+ task = Task(description="Test task", agent=Agent())
+
+ def trigger():
+ return True
+
+ task.set_trigger(trigger)
+ assert task.trigger == trigger
+
+
+def test_task_set_action():
+ task = Task(description="Test task", agent=Agent())
+
+ def action():
+ return True
+
+ task.set_action(action)
+ assert task.action == action
+
+
+def test_task_set_condition():
+ task = Task(description="Test task", agent=Agent())
+
+ def condition():
+ return True
+
+ task.set_condition(condition)
+ assert task.condition == condition
diff --git a/tests/structs/test_team.py b/tests/structs/test_team.py
new file mode 100644
index 00000000..44d64e18
--- /dev/null
+++ b/tests/structs/test_team.py
@@ -0,0 +1,52 @@
+import json
+import unittest
+
+from swarms.models import OpenAIChat
+from swarms.structs import Agent, Task
+from swarms.structs.team import Team
+
+
+class TestTeam(unittest.TestCase):
+ def setUp(self):
+ self.agent = Agent(
+ llm=OpenAIChat(openai_api_key=""),
+ max_loops=1,
+ dashboard=False,
+ )
+ self.task = Task(
+ description="What's the weather in miami",
+ agent=self.agent,
+ )
+ self.team = Team(
+ tasks=[self.task],
+ agents=[self.agent],
+ architecture="sequential",
+ verbose=False,
+ )
+
+ def test_check_config(self):
+ with self.assertRaises(ValueError):
+ self.team.check_config({"config": None})
+
+ with self.assertRaises(ValueError):
+ self.team.check_config(
+ {"config": json.dumps({"agents": [], "tasks": []})}
+ )
+
+ def test_run(self):
+ self.assertEqual(self.team.run(), self.task.execute())
+
+ def test_sequential_loop(self):
+ self.assertEqual(
+ self.team._Team__sequential_loop(), self.task.execute()
+ )
+
+ def test_log(self):
+ self.assertIsNone(self.team._Team__log("Test message"))
+
+ self.team.verbose = True
+ self.assertIsNone(self.team._Team__log("Test message"))
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/tests/structs/test_tests_graph_workflow.py b/tests/structs/test_tests_graph_workflow.py
new file mode 100644
index 00000000..c4bafd35
--- /dev/null
+++ b/tests/structs/test_tests_graph_workflow.py
@@ -0,0 +1,77 @@
+import pytest
+from swarms.structs.graph_workflow import GraphWorkflow
+
+
+@pytest.fixture
+def graph_workflow():
+ return GraphWorkflow()
+
+
+def test_init(graph_workflow):
+ assert graph_workflow.graph == {}
+ assert graph_workflow.entry_point is None
+
+
+def test_add(graph_workflow):
+ graph_workflow.add("node1", "value1")
+ assert "node1" in graph_workflow.graph
+ assert graph_workflow.graph["node1"]["value"] == "value1"
+ assert graph_workflow.graph["node1"]["edges"] == {}
+
+
+def test_set_entry_point(graph_workflow):
+ graph_workflow.add("node1", "value1")
+ graph_workflow.set_entry_point("node1")
+ assert graph_workflow.entry_point == "node1"
+
+
+def test_set_entry_point_nonexistent_node(graph_workflow):
+ with pytest.raises(
+ ValueError, match="Node does not exist in graph"
+ ):
+ graph_workflow.set_entry_point("nonexistent")
+
+
+def test_add_edge(graph_workflow):
+ graph_workflow.add("node1", "value1")
+ graph_workflow.add("node2", "value2")
+ graph_workflow.add_edge("node1", "node2")
+ assert "node2" in graph_workflow.graph["node1"]["edges"]
+
+
+def test_add_edge_nonexistent_node(graph_workflow):
+ graph_workflow.add("node1", "value1")
+ with pytest.raises(
+ ValueError, match="Node does not exist in graph"
+ ):
+ graph_workflow.add_edge("node1", "nonexistent")
+
+
+def test_add_conditional_edges(graph_workflow):
+ graph_workflow.add("node1", "value1")
+ graph_workflow.add("node2", "value2")
+ graph_workflow.add_conditional_edges(
+ "node1", "condition1", {"condition_value1": "node2"}
+ )
+ assert "node2" in graph_workflow.graph["node1"]["edges"]
+
+
+def test_add_conditional_edges_nonexistent_node(graph_workflow):
+ graph_workflow.add("node1", "value1")
+ with pytest.raises(
+ ValueError, match="Node does not exist in graph"
+ ):
+ graph_workflow.add_conditional_edges(
+ "node1", "condition1", {"condition_value1": "nonexistent"}
+ )
+
+
+def test_run(graph_workflow):
+ graph_workflow.add("node1", "value1")
+ graph_workflow.set_entry_point("node1")
+ assert graph_workflow.run() == graph_workflow.graph
+
+
+def test_run_no_entry_point(graph_workflow):
+ with pytest.raises(ValueError, match="Entry point not set"):
+ graph_workflow.run()
diff --git a/tests/swarms/test_autoscaler.py b/tests/swarms/test_autoscaler.py
deleted file mode 100644
index fbf63637..00000000
--- a/tests/swarms/test_autoscaler.py
+++ /dev/null
@@ -1,73 +0,0 @@
-from unittest.mock import patch
-from swarms.structs.autoscaler import AutoScaler
-from swarms.models import OpenAIChat
-from swarms.structs import Agent
-
-llm = OpenAIChat()
-
-agent = Agent(
- llm=llm,
- max_loops=2,
- dashboard=True,
-)
-
-
-def test_autoscaler_initialization():
- autoscaler = AutoScaler(
- initial_agents=5,
- scale_up_factor=2,
- idle_threshold=0.1,
- busy_threshold=0.8,
- agent=agent,
- )
- assert isinstance(autoscaler, AutoScaler)
- assert autoscaler.scale_up_factor == 2
- assert autoscaler.idle_threshold == 0.1
- assert autoscaler.busy_threshold == 0.8
- assert len(autoscaler.agents_pool) == 5
-
-
-def test_autoscaler_add_task():
- autoscaler = AutoScaler(agent=agent)
- autoscaler.add_task("task1")
- assert autoscaler.task_queue.qsize() == 1
-
-
-def test_autoscaler_scale_up():
- autoscaler = AutoScaler(
- initial_agents=5, scale_up_factor=2, agent=agent
- )
- autoscaler.scale_up()
- assert len(autoscaler.agents_pool) == 10
-
-
-def test_autoscaler_scale_down():
- autoscaler = AutoScaler(initial_agents=5, agent=agent)
- autoscaler.scale_down()
- assert len(autoscaler.agents_pool) == 4
-
-
-@patch("swarms.swarms.AutoScaler.scale_up")
-@patch("swarms.swarms.AutoScaler.scale_down")
-def test_autoscaler_monitor_and_scale(mock_scale_down, mock_scale_up):
- autoscaler = AutoScaler(initial_agents=5, agent=agent)
- autoscaler.add_task("task1")
- autoscaler.monitor_and_scale()
- mock_scale_up.assert_called_once()
- mock_scale_down.assert_called_once()
-
-
-@patch("swarms.swarms.AutoScaler.monitor_and_scale")
-@patch("swarms.swarms.agent.run")
-def test_autoscaler_start(mock_run, mock_monitor_and_scale):
- autoscaler = AutoScaler(initial_agents=5, agent=agent)
- autoscaler.add_task("task1")
- autoscaler.start()
- mock_run.assert_called_once()
- mock_monitor_and_scale.assert_called_once()
-
-
-def test_autoscaler_del_agent():
- autoscaler = AutoScaler(initial_agents=5, agent=agent)
- autoscaler.del_agent()
- assert len(autoscaler.agents_pool) == 4
diff --git a/tests/swarms/test_godmode.py b/tests/swarms/test_godmode.py
deleted file mode 100644
index 8f528026..00000000
--- a/tests/swarms/test_godmode.py
+++ /dev/null
@@ -1,36 +0,0 @@
-from unittest.mock import patch
-from swarms.swarms.god_mode import GodMode, LLM
-
-
-def test_godmode_initialization():
- godmode = GodMode(llms=[LLM] * 5)
- assert isinstance(godmode, GodMode)
- assert len(godmode.llms) == 5
-
-
-def test_godmode_run(monkeypatch):
- def mock_llm_run(self, task):
- return "response"
-
- monkeypatch.setattr(LLM, "run", mock_llm_run)
- godmode = GodMode(llms=[LLM] * 5)
- responses = godmode.run("task1")
- assert len(responses) == 5
- assert responses == [
- "response",
- "response",
- "response",
- "response",
- "response",
- ]
-
-
-@patch("builtins.print")
-def test_godmode_print_responses(mock_print, monkeypatch):
- def mock_llm_run(self, task):
- return "response"
-
- monkeypatch.setattr(LLM, "run", mock_llm_run)
- godmode = GodMode(llms=[LLM] * 5)
- godmode.print_responses("task1")
- assert mock_print.call_count == 1
diff --git a/tests/test___init__.py b/tests/test___init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/upload_tests_to_issues.py b/tests/test_upload_tests_to_issues.py
similarity index 98%
rename from tests/upload_tests_to_issues.py
rename to tests/test_upload_tests_to_issues.py
index 864fee29..15de1245 100644
--- a/tests/upload_tests_to_issues.py
+++ b/tests/test_upload_tests_to_issues.py
@@ -1,7 +1,5 @@
import os
import subprocess
-import json
-import re
import requests
from dotenv import load_dotenv
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
diff --git a/tests/tools/test_base.py b/tests/tools/test_tools_base.py
similarity index 97%
rename from tests/tools/test_base.py
rename to tests/tools/test_tools_base.py
index 9f9c700f..9060f53f 100644
--- a/tests/tools/test_base.py
+++ b/tests/tools/test_tools_base.py
@@ -391,21 +391,6 @@ def test_structured_tool_ainvoke_with_exceptions():
tool.ainvoke({"tool_input": "input_data"})
-# Test additional functionality and edge cases
-def test_tool_with_fixture(some_fixture):
- # Test Tool with a fixture
- tool = Tool()
- result = tool.invoke(test_input)
- assert result == expected_output
-
-
-def test_structured_tool_with_fixture(some_fixture):
- # Test StructuredTool with a fixture
- tool = StructuredTool()
- result = tool.invoke(test_input)
- assert result == expected_output
-
-
def test_base_tool_verbose_logging(caplog):
# Test verbose logging in BaseTool
tool = BaseTool(verbose=True)
@@ -428,13 +413,6 @@ def test_structured_tool_async_invoke():
assert result == expected_output
-def test_tool_async_invoke_with_fixture(some_fixture):
- # Test asynchronous invoke with a fixture in Tool
- tool = Tool()
- result = tool.ainvoke(test_input)
- assert result == expected_output
-
-
# Add more tests for specific functionalities and edge cases as needed
# Import necessary libraries and modules
diff --git a/tests/utils/test_check_device.py b/tests/utils/test_check_device.py
new file mode 100644
index 00000000..d542803a
--- /dev/null
+++ b/tests/utils/test_check_device.py
@@ -0,0 +1,64 @@
+import torch
+import logging
+from swarms.utils import check_device
+
+# For the purpose of the test, we're assuming that the `memory_allocated`
+# and `memory_reserved` function behave the same as `torch.cuda.memory_allocated`
+# and `torch.cuda.memory_reserved`
+
+
+def test_check_device_no_cuda(monkeypatch):
+ # Mock torch.cuda.is_available to always return False
+ monkeypatch.setattr(torch.cuda, "is_available", lambda: False)
+
+ result = check_device(log_level=logging.DEBUG)
+ assert result.type == "cpu"
+
+
+def test_check_device_cuda_exception(monkeypatch):
+ # Mock torch.cuda.is_available to raise an exception
+ monkeypatch.setattr(
+ torch.cuda, "is_available", lambda: 1 / 0
+ ) # Raises ZeroDivisionError
+
+ result = check_device(log_level=logging.DEBUG)
+ assert result.type == "cpu"
+
+
+def test_check_device_one_cuda(monkeypatch):
+ # Mock torch.cuda.is_available to return True
+ monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
+ # Mock torch.cuda.device_count to return 1
+ monkeypatch.setattr(torch.cuda, "device_count", lambda: 1)
+ # Mock torch.cuda.memory_allocated and torch.cuda.memory_reserved to return 0
+ monkeypatch.setattr(
+ torch.cuda, "memory_allocated", lambda device: 0
+ )
+ monkeypatch.setattr(
+ torch.cuda, "memory_reserved", lambda device: 0
+ )
+
+ result = check_device(log_level=logging.DEBUG)
+ assert len(result) == 1
+ assert result[0].type == "cuda"
+ assert result[0].index == 0
+
+
+def test_check_device_multiple_cuda(monkeypatch):
+ # Mock torch.cuda.is_available to return True
+ monkeypatch.setattr(torch.cuda, "is_available", lambda: True)
+ # Mock torch.cuda.device_count to return 4
+ monkeypatch.setattr(torch.cuda, "device_count", lambda: 4)
+ # Mock torch.cuda.memory_allocated and torch.cuda.memory_reserved to return 0
+ monkeypatch.setattr(
+ torch.cuda, "memory_allocated", lambda device: 0
+ )
+ monkeypatch.setattr(
+ torch.cuda, "memory_reserved", lambda device: 0
+ )
+
+ result = check_device(log_level=logging.DEBUG)
+ assert len(result) == 4
+ for i in range(4):
+ assert result[i].type == "cuda"
+ assert result[i].index == i
diff --git a/tests/utils/test_class_args_wrapper.py b/tests/utils/test_class_args_wrapper.py
new file mode 100644
index 00000000..a222ffe9
--- /dev/null
+++ b/tests/utils/test_class_args_wrapper.py
@@ -0,0 +1,55 @@
+import pytest
+from io import StringIO
+from contextlib import redirect_stdout
+from swarms.utils.class_args_wrapper import print_class_parameters
+from swarms.structs.agent import Agent
+from fastapi import FastAPI
+from fastapi.testclient import TestClient
+
+app = FastAPI()
+
+
+def test_print_class_parameters_agent():
+ f = StringIO()
+ with redirect_stdout(f):
+ print_class_parameters(Agent)
+ output = f.getvalue().strip()
+ # Replace with the expected output for Agent class
+ expected_output = (
+ "Parameter: name, Type: \nParameter: age, Type:"
+ " "
+ )
+ assert output == expected_output
+
+
+def test_print_class_parameters_error():
+ with pytest.raises(TypeError):
+ print_class_parameters("Not a class")
+
+
+@app.get("/parameters/{class_name}")
+def get_parameters(class_name: str):
+ classes = {"Agent": Agent}
+ if class_name in classes:
+ return print_class_parameters(
+ classes[class_name], api_format=True
+ )
+ else:
+ return {"error": "Class not found"}
+
+
+client = TestClient(app)
+
+
+def test_get_parameters_agent():
+ response = client.get("/parameters/Agent")
+ assert response.status_code == 200
+ # Replace with the expected output for Agent class
+ expected_output = {"x": "", "y": ""}
+ assert response.json() == expected_output
+
+
+def test_get_parameters_not_found():
+ response = client.get("/parameters/NonexistentClass")
+ assert response.status_code == 200
+ assert response.json() == {"error": "Class not found"}
diff --git a/tests/utils/test_device.py b/tests/utils/test_device.py
new file mode 100644
index 00000000..14399de9
--- /dev/null
+++ b/tests/utils/test_device.py
@@ -0,0 +1,111 @@
+import torch
+from unittest.mock import MagicMock
+import pytest
+from swarms.utils.device_checker_cuda import check_device
+
+
+def test_cuda_not_available(mocker):
+ mocker.patch("torch.cuda.is_available", return_value=False)
+ device = check_device()
+ assert str(device) == "cpu"
+
+
+def test_single_gpu_available(mocker):
+ mocker.patch("torch.cuda.is_available", return_value=True)
+ mocker.patch("torch.cuda.device_count", return_value=1)
+ devices = check_device()
+ assert len(devices) == 1
+ assert str(devices[0]) == "cuda"
+
+
+def test_multiple_gpus_available(mocker):
+ mocker.patch("torch.cuda.is_available", return_value=True)
+ mocker.patch("torch.cuda.device_count", return_value=2)
+ devices = check_device()
+ assert len(devices) == 2
+ assert str(devices[0]) == "cuda:0"
+ assert str(devices[1]) == "cuda:1"
+
+
+def test_device_properties(mocker):
+ mocker.patch("torch.cuda.is_available", return_value=True)
+ mocker.patch("torch.cuda.device_count", return_value=1)
+ mocker.patch(
+ "torch.cuda.get_device_capability", return_value=(7, 5)
+ )
+ mocker.patch(
+ "torch.cuda.get_device_properties",
+ return_value=MagicMock(total_memory=1000),
+ )
+ mocker.patch("torch.cuda.memory_allocated", return_value=200)
+ mocker.patch("torch.cuda.memory_reserved", return_value=300)
+ mocker.patch(
+ "torch.cuda.get_device_name", return_value="Tesla K80"
+ )
+ devices = check_device()
+ assert len(devices) == 1
+ assert str(devices[0]) == "cuda"
+
+
+def test_memory_threshold(mocker):
+ mocker.patch("torch.cuda.is_available", return_value=True)
+ mocker.patch("torch.cuda.device_count", return_value=1)
+ mocker.patch(
+ "torch.cuda.get_device_capability", return_value=(7, 5)
+ )
+ mocker.patch(
+ "torch.cuda.get_device_properties",
+ return_value=MagicMock(total_memory=1000),
+ )
+ mocker.patch(
+ "torch.cuda.memory_allocated", return_value=900
+ ) # 90% of total memory
+ mocker.patch("torch.cuda.memory_reserved", return_value=300)
+ mocker.patch(
+ "torch.cuda.get_device_name", return_value="Tesla K80"
+ )
+ with pytest.warns(
+ UserWarning,
+ match=r"Memory usage for device cuda exceeds threshold",
+ ):
+ devices = check_device(
+ memory_threshold=0.8
+ ) # Set memory threshold to 80%
+ assert len(devices) == 1
+ assert str(devices[0]) == "cuda"
+
+
+def test_compute_capability_threshold(mocker):
+ mocker.patch("torch.cuda.is_available", return_value=True)
+ mocker.patch("torch.cuda.device_count", return_value=1)
+ mocker.patch(
+ "torch.cuda.get_device_capability", return_value=(3, 0)
+ ) # Compute capability 3.0
+ mocker.patch(
+ "torch.cuda.get_device_properties",
+ return_value=MagicMock(total_memory=1000),
+ )
+ mocker.patch("torch.cuda.memory_allocated", return_value=200)
+ mocker.patch("torch.cuda.memory_reserved", return_value=300)
+ mocker.patch(
+ "torch.cuda.get_device_name", return_value="Tesla K80"
+ )
+ with pytest.warns(
+ UserWarning,
+ match=(
+ r"Compute capability for device cuda is below threshold"
+ ),
+ ):
+ devices = check_device(
+ capability_threshold=3.5
+ ) # Set compute capability threshold to 3.5
+ assert len(devices) == 1
+ assert str(devices[0]) == "cuda"
+
+
+def test_return_single_device(mocker):
+ mocker.patch("torch.cuda.is_available", return_value=True)
+ mocker.patch("torch.cuda.device_count", return_value=2)
+ device = check_device(return_type="single")
+ assert isinstance(device, torch.device)
+ assert str(device) == "cuda:0"
diff --git a/tests/utils/test_display_markdown_message.py b/tests/utils/test_display_markdown_message.py
new file mode 100644
index 00000000..048038b2
--- /dev/null
+++ b/tests/utils/test_display_markdown_message.py
@@ -0,0 +1,65 @@
+# import necessary modules
+import pytest
+from swarms.utils import display_markdown_message
+from rich.console import Console
+from rich.markdown import Markdown
+from rich.rule import Rule
+from unittest import mock
+
+
+def test_basic_message():
+ # Test basic message functionality
+ with mock.patch.object(Console, "print") as mock_print:
+ display_markdown_message("This is a test")
+ mock_print.assert_called_once_with(
+ Markdown("This is a test", style="cyan")
+ )
+
+
+def test_empty_message():
+ # Test how function handles empty input
+ with mock.patch.object(Console, "print") as mock_print:
+ display_markdown_message("")
+ mock_print.assert_called_once_with("")
+
+
+@pytest.mark.parametrize("color", ["cyan", "red", "blue"])
+def test_colors(color):
+ # Test different colors
+ with mock.patch.object(Console, "print") as mock_print:
+ display_markdown_message("This is a test", color)
+ mock_print.assert_called_once_with(
+ Markdown("This is a test", style=color)
+ )
+
+
+def test_dash_line():
+ # Test how function handles "---"
+ with mock.patch.object(Console, "print") as mock_print:
+ display_markdown_message("---")
+ mock_print.assert_called_once_with(Rule(style="cyan"))
+
+
+def test_message_with_whitespace():
+ # Test how function handles message with whitespaces
+ with mock.patch.object(Console, "print") as mock_print:
+ display_markdown_message(" \n Test \n --- \n Test \n")
+ calls = [
+ mock.call(""),
+ mock.call(Markdown("Test", style="cyan")),
+ mock.call(Rule(style="cyan")),
+ mock.call(Markdown("Test", style="cyan")),
+ mock.call(""),
+ ]
+ mock_print.assert_has_calls(calls)
+
+
+def test_message_start_with_greater_than():
+ # Test how function handles message line starting with ">"
+ with mock.patch.object(Console, "print") as mock_print:
+ display_markdown_message(">This is a test")
+ calls = [
+ mock.call(Markdown(">This is a test", style="cyan")),
+ mock.call(""),
+ ]
+ mock_print.assert_has_calls(calls)
diff --git a/tests/utils/test_extract_code_from_markdown.py b/tests/utils/test_extract_code_from_markdown.py
new file mode 100644
index 00000000..9d37fc94
--- /dev/null
+++ b/tests/utils/test_extract_code_from_markdown.py
@@ -0,0 +1,47 @@
+import pytest
+from swarms.utils import extract_code_from_markdown
+
+
+@pytest.fixture
+def markdown_content_with_code():
+ return """
+ # This is a markdown document
+
+ Some intro text here.
+Some additional text.
+"""
+
+
+@pytest.fixture
+def markdown_content_without_code():
+ return """
+ # This is a markdown document
+
+ There is no code in this document.
+ """
+
+
+def test_extract_code_from_markdown_with_code(
+ markdown_content_with_code,
+):
+ extracted_code = extract_code_from_markdown(
+ markdown_content_with_code
+ )
+ assert "def my_func():" in extracted_code
+ assert 'print("This is my function.")' in extracted_code
+ assert "class MyClass:" in extracted_code
+ assert "pass" in extracted_code
+
+
+def test_extract_code_from_markdown_without_code(
+ markdown_content_without_code,
+):
+ extracted_code = extract_code_from_markdown(
+ markdown_content_without_code
+ )
+ assert extracted_code == ""
+
+
+def test_extract_code_from_markdown_exception():
+ with pytest.raises(TypeError):
+ extract_code_from_markdown(None)
diff --git a/tests/utils/test_find_image_path.py b/tests/utils/test_find_image_path.py
new file mode 100644
index 00000000..9fbc09ee
--- /dev/null
+++ b/tests/utils/test_find_image_path.py
@@ -0,0 +1,52 @@
+# Filename: test_utils.py
+
+import pytest
+from swarms.utils import find_image_path
+import os
+
+
+def test_find_image_path_no_images():
+ assert (
+ find_image_path(
+ "This is a test string without any image paths."
+ )
+ is None
+ )
+
+
+def test_find_image_path_one_image():
+ text = "This is a string with one image path: sample_image.jpg."
+ assert find_image_path(text) == "sample_image.jpg"
+
+
+def test_find_image_path_multiple_images():
+ text = "This string has two image paths: img1.png, and img2.jpg."
+ assert (
+ find_image_path(text) == "img2.jpg"
+ ) # Assuming both images exist
+
+
+def test_find_image_path_wrong_input():
+ with pytest.raises(TypeError):
+ find_image_path(123)
+
+
+@pytest.mark.parametrize(
+ "text, expected",
+ [
+ ("no image path here", None),
+ ("image: sample.png", "sample.png"),
+ ("image: sample.png, another: another.jpeg", "another.jpeg"),
+ ],
+)
+def test_find_image_path_parameterized(text, expected):
+ assert find_image_path(text) == expected
+
+
+def mock_os_path_exists(path):
+ return True
+
+
+def test_find_image_path_mocking(monkeypatch):
+ monkeypatch.setattr(os.path, "exists", mock_os_path_exists)
+ assert find_image_path("image.jpg") == "image.jpg"
diff --git a/tests/utils/test_limit_tokens_from_string.py b/tests/utils/test_limit_tokens_from_string.py
new file mode 100644
index 00000000..5b5f8efd
--- /dev/null
+++ b/tests/utils/test_limit_tokens_from_string.py
@@ -0,0 +1,45 @@
+import pytest
+from swarms.utils import limit_tokens_from_string
+
+
+def test_limit_tokens_from_string():
+ sentence = (
+ "This is a test sentence. It is used for testing the number"
+ " of tokens."
+ )
+ limited = limit_tokens_from_string(sentence, limit=5)
+ assert (
+ len(limited.split()) <= 5
+ ), "The output string has more than 5 tokens."
+
+
+def test_limit_zero_tokens():
+ sentence = "Expect empty result when limit is set to zero."
+ limited = limit_tokens_from_string(sentence, limit=0)
+ assert limited == "", "The output is not empty."
+
+
+def test_negative_token_limit():
+ sentence = (
+ "This test will raise an exception when limit is negative."
+ )
+ with pytest.raises(Exception):
+ limit_tokens_from_string(sentence, limit=-1)
+
+
+@pytest.mark.parametrize(
+ "sentence, model", [("Some sentence", "unavailable-model")]
+)
+def test_unknown_model(sentence, model):
+ with pytest.raises(Exception):
+ limit_tokens_from_string(sentence, model=model)
+
+
+def test_string_token_limit_exceeded():
+ sentence = (
+ "This is a long sentence with more than twenty tokens which"
+ " is used for testing. It checks whether the function"
+ " correctly limits the tokens to a specified amount."
+ )
+ limited = limit_tokens_from_string(sentence, limit=20)
+ assert len(limited.split()) <= 20, "The token limit is exceeded."
diff --git a/tests/utils/test_load_model_torch.py b/tests/utils/test_load_model_torch.py
new file mode 100644
index 00000000..ef2c17d4
--- /dev/null
+++ b/tests/utils/test_load_model_torch.py
@@ -0,0 +1,111 @@
+import pytest
+import torch
+from torch import nn
+from swarms.utils import load_model_torch
+
+
+class DummyModel(nn.Module):
+ def __init__(self):
+ super().__init__()
+ self.fc = nn.Linear(10, 2)
+
+ def forward(self, x):
+ return self.fc(x)
+
+
+# Test case 1: Test if model can be loaded successfully
+def test_load_model_torch_success(tmp_path):
+ model = DummyModel()
+ # Save the model to a temporary directory
+ model_path = tmp_path / "model.pt"
+ torch.save(model.state_dict(), model_path)
+
+ # Load the model
+ model_loaded = load_model_torch(model_path, model=DummyModel())
+
+ # Check if loaded model has the same architecture
+ assert isinstance(
+ model_loaded, DummyModel
+ ), "Loaded model type mismatch."
+
+
+# Test case 2: Test if function raises FileNotFoundError for non-existent file
+def test_load_model_torch_file_not_found():
+ with pytest.raises(FileNotFoundError):
+ load_model_torch("non_existent_model.pt")
+
+
+# Test case 3: Test if function catches and raises RuntimeError for invalid model file
+def test_load_model_torch_invalid_file(tmp_path):
+ file = tmp_path / "invalid_model.pt"
+ file.write_text("Invalid model file.")
+
+ with pytest.raises(RuntimeError):
+ load_model_torch(file)
+
+
+# Test case 4: Test for handling of 'strict' parameter
+def test_load_model_torch_strict_handling(tmp_path):
+ # Create a model and modify it to cause a mismatch
+ model = DummyModel()
+ model.fc = nn.Linear(10, 3)
+ model_path = tmp_path / "model.pt"
+ torch.save(model.state_dict(), model_path)
+
+ # Try to load the modified model with 'strict' parameter set to True
+ with pytest.raises(RuntimeError):
+ load_model_torch(model_path, model=DummyModel(), strict=True)
+
+
+# Test case 5: Test for 'device' parameter handling
+def test_load_model_torch_device_handling(tmp_path):
+ model = DummyModel()
+ model_path = tmp_path / "model.pt"
+ torch.save(model.state_dict(), model_path)
+
+ # Define a device other than default and load the model to the specified device
+ device = torch.device("cpu")
+ model_loaded = load_model_torch(
+ model_path, model=DummyModel(), device=device
+ )
+
+ assert (
+ model_loaded.fc.weight.device == device
+ ), "Model not loaded to specified device."
+
+
+# Test case 6: Testing for correct handling of '*args' and '**kwargs'
+def test_load_model_torch_args_kwargs_handling(monkeypatch, tmp_path):
+ model = DummyModel()
+ model_path = tmp_path / "model.pt"
+ torch.save(model.state_dict(), model_path)
+
+ def mock_torch_load(*args, **kwargs):
+ assert (
+ "pickle_module" in kwargs
+ ), "Keyword arguments not passed to 'torch.load'."
+
+ # Monkeypatch 'torch.load' to check if '*args' and '**kwargs' are passed correctly
+ monkeypatch.setattr(torch, "load", mock_torch_load)
+ load_model_torch(
+ model_path, model=DummyModel(), pickle_module="dummy_module"
+ )
+
+
+# Test case 7: Test for model loading on CPU if no GPU is available
+def test_load_model_torch_cpu(tmp_path):
+ model = DummyModel()
+ model_path = tmp_path / "model.pt"
+ torch.save(model.state_dict(), model_path)
+
+ def mock_torch_cuda_is_available():
+ return False
+
+ # Monkeypatch to simulate no GPU available
+ pytest.MonkeyPatch.setattr(
+ torch.cuda, "is_available", mock_torch_cuda_is_available
+ )
+ model_loaded = load_model_torch(model_path, model=DummyModel())
+
+ # Ensure model is loaded on CPU
+ assert next(model_loaded.parameters()).device.type == "cpu"
diff --git a/tests/utils/test_load_models_torch.py b/tests/utils/test_load_models_torch.py
new file mode 100644
index 00000000..707f1ce4
--- /dev/null
+++ b/tests/utils/test_load_models_torch.py
@@ -0,0 +1,54 @@
+import pytest
+import torch
+from unittest.mock import MagicMock
+from swarms.utils.load_model_torch import load_model_torch
+
+
+def test_load_model_torch_no_model_path():
+ with pytest.raises(FileNotFoundError):
+ load_model_torch()
+
+
+def test_load_model_torch_model_not_found(mocker):
+ mocker.patch("torch.load", side_effect=FileNotFoundError)
+ with pytest.raises(FileNotFoundError):
+ load_model_torch("non_existent_model_path")
+
+
+def test_load_model_torch_runtime_error(mocker):
+ mocker.patch("torch.load", side_effect=RuntimeError)
+ with pytest.raises(RuntimeError):
+ load_model_torch("model_path")
+
+
+def test_load_model_torch_no_device_specified(mocker):
+ mock_model = MagicMock(spec=torch.nn.Module)
+ mocker.patch("torch.load", return_value=mock_model)
+ mocker.patch("torch.cuda.is_available", return_value=False)
+ load_model_torch("model_path")
+ mock_model.to.assert_called_once_with(torch.device("cpu"))
+
+
+def test_load_model_torch_device_specified(mocker):
+ mock_model = MagicMock(spec=torch.nn.Module)
+ mocker.patch("torch.load", return_value=mock_model)
+ load_model_torch("model_path", device=torch.device("cuda"))
+ mock_model.to.assert_called_once_with(torch.device("cuda"))
+
+
+def test_load_model_torch_model_specified(mocker):
+ mock_model = MagicMock(spec=torch.nn.Module)
+ mocker.patch("torch.load", return_value={"key": "value"})
+ load_model_torch("model_path", model=mock_model)
+ mock_model.load_state_dict.assert_called_once_with(
+ {"key": "value"}, strict=True
+ )
+
+
+def test_load_model_torch_model_specified_strict_false(mocker):
+ mock_model = MagicMock(spec=torch.nn.Module)
+ mocker.patch("torch.load", return_value={"key": "value"})
+ load_model_torch("model_path", model=mock_model, strict=False)
+ mock_model.load_state_dict.assert_called_once_with(
+ {"key": "value"}, strict=False
+ )
diff --git a/tests/utils/test_math_eval.py b/tests/utils/test_math_eval.py
new file mode 100644
index 00000000..ae7ee04c
--- /dev/null
+++ b/tests/utils/test_math_eval.py
@@ -0,0 +1,41 @@
+from swarms.utils import math_eval
+
+
+def func1_no_exception(x):
+ return x + 2
+
+
+def func2_no_exception(x):
+ return x + 2
+
+
+def func1_with_exception(x):
+ raise ValueError()
+
+
+def func2_with_exception(x):
+ raise ValueError()
+
+
+def test_same_results_no_exception(caplog):
+ @math_eval(func1_no_exception, func2_no_exception)
+ def test_func(x):
+ return x
+
+ result1, result2 = test_func(5)
+ assert result1 == result2 == 7
+ assert "Outputs do not match" not in caplog.text
+
+
+def test_func1_exception(caplog):
+ @math_eval(func1_with_exception, func2_no_exception)
+ def test_func(x):
+ return x
+
+ result1, result2 = test_func(5)
+ assert result1 is None
+ assert result2 == 7
+ assert "Error in func1:" in caplog.text
+
+
+# similar tests for func2_with_exception and when func1 and func2 return different results
diff --git a/tests/utils/test_metrics_decorator.py b/tests/utils/test_metrics_decorator.py
new file mode 100644
index 00000000..7a676657
--- /dev/null
+++ b/tests/utils/test_metrics_decorator.py
@@ -0,0 +1,84 @@
+# pytest imports
+import pytest
+from unittest.mock import Mock
+
+# Imports from your project
+from swarms.utils import metrics_decorator
+import time
+
+
+# Basic successful test
+def test_metrics_decorator_success():
+ @metrics_decorator
+ def decorated_func():
+ time.sleep(0.1)
+ return [1, 2, 3, 4, 5]
+
+ metrics = decorated_func()
+ assert "Time to First Token" in metrics
+ assert "Generation Latency" in metrics
+ assert "Throughput:" in metrics
+
+
+@pytest.mark.parametrize(
+ "wait_time, return_val",
+ [
+ (0, []),
+ (0.1, [1, 2, 3]),
+ (0.5, list(range(50))),
+ ],
+)
+def test_metrics_decorator_with_various_wait_times_and_return_vals(
+ wait_time, return_val
+):
+ @metrics_decorator
+ def decorated_func():
+ time.sleep(wait_time)
+ return return_val
+
+ metrics = decorated_func()
+ assert "Time to First Token" in metrics
+ assert "Generation Latency" in metrics
+ assert "Throughput:" in metrics
+
+
+# Test to ensure that mocked time function was called and throughputs are calculated as expected
+def test_metrics_decorator_with_mocked_time(mocker):
+ mocked_time = Mock()
+ mocker.patch("time.time", mocked_time)
+
+ mocked_time.side_effect = [0, 5, 10, 20]
+
+ @metrics_decorator
+ def decorated_func():
+ return ["tok_1", "tok_2"]
+
+ metrics = decorated_func()
+ assert metrics == """
+ Time to First Token: 5
+ Generation Latency: 20
+ Throughput: 0.1
+ """
+ mocked_time.assert_any_call()
+
+
+# Test to ensure that exceptions in the decorated function are propagated
+def test_metrics_decorator_raises_exception():
+ @metrics_decorator
+ def decorated_func():
+ raise ValueError("Oops!")
+
+ with pytest.raises(ValueError, match="Oops!"):
+ decorated_func()
+
+
+# Test to ensure proper handling when decorated function returns non-list value
+def test_metrics_decorator_with_non_list_return_val():
+ @metrics_decorator
+ def decorated_func():
+ return "Hello, world!"
+
+ metrics = decorated_func()
+ assert "Time to First Token" in metrics
+ assert "Generation Latency" in metrics
+ assert "Throughput:" in metrics
diff --git a/tests/utils/test_pdf_to_text.py b/tests/utils/test_pdf_to_text.py
new file mode 100644
index 00000000..57e3b33f
--- /dev/null
+++ b/tests/utils/test_pdf_to_text.py
@@ -0,0 +1,40 @@
+import pytest
+import PyPDF2
+from swarms.utils import pdf_to_text
+
+
+@pytest.fixture
+def pdf_file(tmpdir):
+ pdf_writer = PyPDF2.PdfWriter()
+ pdf_page = PyPDF2.pdf.PageObject.createBlankPage(None, 200, 200)
+ pdf_writer.add_page(pdf_page)
+ pdf_file = tmpdir.join("temp.pdf")
+ with open(pdf_file, "wb") as output:
+ pdf_writer.write(output)
+ return str(pdf_file)
+
+
+def test_valid_pdf_to_text(pdf_file):
+ result = pdf_to_text(pdf_file)
+ assert isinstance(result, str)
+
+
+def test_non_existing_file():
+ with pytest.raises(FileNotFoundError):
+ pdf_to_text("non_existing_file.pdf")
+
+
+def test_passing_non_pdf_file(tmpdir):
+ file = tmpdir.join("temp.txt")
+ file.write("This is a test")
+ with pytest.raises(
+ Exception,
+ match=r"An error occurred while reading the PDF file",
+ ):
+ pdf_to_text(str(file))
+
+
+@pytest.mark.parametrize("invalid_pdf_file", [None, 123, {}, []])
+def test_invalid_pdf_to_text(invalid_pdf_file):
+ with pytest.raises(Exception):
+ pdf_to_text(invalid_pdf_file)
diff --git a/tests/utils/test_phoenix_handler.py b/tests/utils/test_phoenix_handler.py
deleted file mode 100644
index 3b6915b9..00000000
--- a/tests/utils/test_phoenix_handler.py
+++ /dev/null
@@ -1,152 +0,0 @@
-# Import necessary modules and functions for testing
-import functools
-import subprocess
-import sys
-import traceback
-
-import pytest
-
-# Try importing phoenix and handle exceptions
-try:
- import phoenix as px
-except Exception as error:
- print(f"Error importing phoenix: {error}")
- print("Please install phoenix: pip install phoenix")
- subprocess.run(
- [sys.executable, "-m", "pip", "install", "arize-mlflow"]
- )
-
-# Import the code to be tested
-from swarms.utils.phoenix_handler import phoenix_trace_decorator
-
-
-# Define a fixture for Phoenix session
-@pytest.fixture(scope="function")
-def phoenix_session():
- session = px.active_session() or px.launch_app()
- yield session
- session.stop()
-
-
-# Define test cases for the phoenix_trace_decorator function
-def test_phoenix_trace_decorator_documentation():
- """Test if phoenix_trace_decorator has a docstring."""
- assert phoenix_trace_decorator.__doc__ is not None
-
-
-def test_phoenix_trace_decorator_functionality(
- capsys, phoenix_session
-):
- """Test the functionality of phoenix_trace_decorator."""
-
- # Define a function to be decorated
- @phoenix_trace_decorator("This is a test function.")
- def test_function():
- print("Hello, Phoenix!")
-
- # Execute the decorated function
- test_function()
-
- # Capture the printed output
- captured = capsys.readouterr()
- assert captured.out == "Hello, Phoenix!\n"
-
-
-def test_phoenix_trace_decorator_exception_handling(phoenix_session):
- """Test if phoenix_trace_decorator handles exceptions correctly."""
-
- # Define a function that raises an exception
- @phoenix_trace_decorator("This function raises an exception.")
- def exception_function():
- raise ValueError("An error occurred.")
-
- # Execute the decorated function
- with pytest.raises(ValueError):
- exception_function()
-
- # Check if the exception was traced by Phoenix
- traces = phoenix_session.get_traces()
- assert len(traces) == 1
- assert traces[0].get("error") is not None
- assert traces[0].get("error_info") is not None
-
-
-# Define test cases for phoenix_trace_decorator
-def test_phoenix_trace_decorator_docstring():
- """Test if phoenix_trace_decorator's inner function has a docstring."""
-
- @phoenix_trace_decorator("This is a test function.")
- def test_function():
- """Test function docstring."""
- pass
-
- assert test_function.__doc__ is not None
-
-
-def test_phoenix_trace_decorator_functionality_with_params(
- capsys, phoenix_session
-):
- """Test the functionality of phoenix_trace_decorator with parameters."""
-
- # Define a function with parameters to be decorated
- @phoenix_trace_decorator("This function takes parameters.")
- def param_function(a, b):
- result = a + b
- print(f"Result: {result}")
-
- # Execute the decorated function with parameters
- param_function(2, 3)
-
- # Capture the printed output
- captured = capsys.readouterr()
- assert captured.out == "Result: 5\n"
-
-
-def test_phoenix_trace_decorator_nested_calls(
- capsys, phoenix_session
-):
- """Test nested calls of phoenix_trace_decorator."""
-
- # Define a nested function with decorators
- @phoenix_trace_decorator("Outer function")
- def outer_function():
- print("Outer function")
-
- @phoenix_trace_decorator("Inner function")
- def inner_function():
- print("Inner function")
-
- inner_function()
-
- # Execute the decorated functions
- outer_function()
-
- # Capture the printed output
- captured = capsys.readouterr()
- assert "Outer function" in captured.out
- assert "Inner function" in captured.out
-
-
-def test_phoenix_trace_decorator_nested_exception_handling(
- phoenix_session,
-):
- """Test exception handling with nested phoenix_trace_decorators."""
-
- # Define a function with nested decorators and an exception
- @phoenix_trace_decorator("Outer function")
- def outer_function():
- @phoenix_trace_decorator("Inner function")
- def inner_function():
- raise ValueError("Inner error")
-
- inner_function()
-
- # Execute the decorated functions
- with pytest.raises(ValueError):
- outer_function()
-
- # Check if both exceptions were traced by Phoenix
- traces = phoenix_session.get_traces()
- assert len(traces) == 2
- assert "Outer function" in traces[0].get("error_info")
- assert "Inner function" in traces[1].get("error_info")
diff --git a/tests/utils/test_prep_torch_inference.py b/tests/utils/test_prep_torch_inference.py
new file mode 100644
index 00000000..8ee33fbc
--- /dev/null
+++ b/tests/utils/test_prep_torch_inference.py
@@ -0,0 +1,49 @@
+import unittest
+import pytest
+import torch
+from unittest.mock import Mock
+from swarms.utils import prep_torch_inference
+
+
+def test_prep_torch_inference():
+ model_path = "model_path"
+ device = torch.device(
+ "cuda" if torch.cuda.is_available() else "cpu"
+ )
+ model_mock = Mock()
+ model_mock.eval = Mock()
+
+ # Mocking the load_model_torch function to return our mock model.
+ with unittest.mock.patch(
+ "swarms.utils.load_model_torch", return_value=model_mock
+ ) as _:
+ model = prep_torch_inference(model_path, device)
+
+ # Check if model was properly loaded and eval function was called
+ assert model == model_mock
+ model_mock.eval.assert_called_once()
+
+
+@pytest.mark.parametrize(
+ "model_path, device",
+ [
+ (
+ "invalid_path",
+ torch.device("cuda"),
+ ), # Invalid file path, valid device
+ (None, torch.device("cuda")), # None file path, valid device
+ ("model_path", None), # Valid file path, None device
+ (None, None), # None file path, None device
+ ],
+)
+def test_prep_torch_inference_exceptions(model_path, device):
+ with pytest.raises(Exception):
+ prep_torch_inference(model_path, device)
+
+
+def test_prep_torch_inference_return_none():
+ model_path = "invalid_path" # Invalid file path
+ device = torch.device("cuda") # Valid device
+
+ # Since load_model_torch function will raise an exception, prep_torch_inference should return None
+ assert prep_torch_inference(model_path, device) is None
diff --git a/tests/utils/test_prep_torch_model_inference.py b/tests/utils/test_prep_torch_model_inference.py
new file mode 100644
index 00000000..4a13bee1
--- /dev/null
+++ b/tests/utils/test_prep_torch_model_inference.py
@@ -0,0 +1,48 @@
+import torch
+from unittest.mock import MagicMock
+from swarms.utils.prep_torch_model_inference import (
+ prep_torch_inference,
+)
+
+
+def test_prep_torch_inference_no_model_path():
+ result = prep_torch_inference()
+ assert result is None
+
+
+def test_prep_torch_inference_model_not_found(mocker):
+ mocker.patch(
+ "swarms.utils.prep_torch_model_inference.load_model_torch",
+ side_effect=FileNotFoundError,
+ )
+ result = prep_torch_inference("non_existent_model_path")
+ assert result is None
+
+
+def test_prep_torch_inference_runtime_error(mocker):
+ mocker.patch(
+ "swarms.utils.prep_torch_model_inference.load_model_torch",
+ side_effect=RuntimeError,
+ )
+ result = prep_torch_inference("model_path")
+ assert result is None
+
+
+def test_prep_torch_inference_no_device_specified(mocker):
+ mock_model = MagicMock(spec=torch.nn.Module)
+ mocker.patch(
+ "swarms.utils.prep_torch_model_inference.load_model_torch",
+ return_value=mock_model,
+ )
+ prep_torch_inference("model_path")
+ mock_model.eval.assert_called_once()
+
+
+def test_prep_torch_inference_device_specified(mocker):
+ mock_model = MagicMock(spec=torch.nn.Module)
+ mocker.patch(
+ "swarms.utils.prep_torch_model_inference.load_model_torch",
+ return_value=mock_model,
+ )
+ prep_torch_inference("model_path", device=torch.device("cuda"))
+ mock_model.eval.assert_called_once()
diff --git a/tests/utils/test_print_class_parameters.py b/tests/utils/test_print_class_parameters.py
new file mode 100644
index 00000000..ae824170
--- /dev/null
+++ b/tests/utils/test_print_class_parameters.py
@@ -0,0 +1,119 @@
+import pytest
+from swarms.utils import print_class_parameters
+
+
+class TestObject:
+ def __init__(self, value1, value2: int):
+ pass
+
+
+class TestObject2:
+ def __init__(self: "TestObject2", value1, value2: int = 5):
+ pass
+
+
+def test_class_with_complex_parameters():
+ class ComplexArgs:
+ def __init__(self, value1: list, value2: dict = {}):
+ pass
+
+ output = {"value1": "", "value2": ""}
+ assert (
+ print_class_parameters(ComplexArgs, api_format=True) == output
+ )
+
+
+def test_empty_class():
+ class Empty:
+ pass
+
+ with pytest.raises(Exception):
+ print_class_parameters(Empty)
+
+
+def test_class_with_no_annotations():
+ class NoAnnotations:
+ def __init__(self, value1, value2):
+ pass
+
+ output = {
+ "value1": "",
+ "value2": "",
+ }
+ assert (
+ print_class_parameters(NoAnnotations, api_format=True)
+ == output
+ )
+
+
+def test_class_with_partial_annotations():
+ class PartialAnnotations:
+ def __init__(self, value1, value2: int):
+ pass
+
+ output = {
+ "value1": "",
+ "value2": "",
+ }
+ assert (
+ print_class_parameters(PartialAnnotations, api_format=True)
+ == output
+ )
+
+
+@pytest.mark.parametrize(
+ "obj, expected",
+ [
+ (
+ TestObject,
+ {
+ "value1": "",
+ "value2": "",
+ },
+ ),
+ (
+ TestObject2,
+ {
+ "value1": "",
+ "value2": "",
+ },
+ ),
+ ],
+)
+def test_parametrized_class_parameters(obj, expected):
+ assert print_class_parameters(obj, api_format=True) == expected
+
+
+@pytest.mark.parametrize(
+ "value",
+ [
+ int,
+ float,
+ str,
+ list,
+ set,
+ dict,
+ bool,
+ tuple,
+ complex,
+ bytes,
+ bytearray,
+ memoryview,
+ range,
+ frozenset,
+ slice,
+ object,
+ ],
+)
+def test_not_class_exception(value):
+ with pytest.raises(Exception):
+ print_class_parameters(value)
+
+
+def test_api_format_flag():
+ assert print_class_parameters(TestObject2, api_format=True) == {
+ "value1": "",
+ "value2": "",
+ }
+ print_class_parameters(TestObject)
+ # TODO: Capture printed output and assert correctness.
diff --git a/tests/utils/test_subprocess_code_interpreter.py b/tests/utils/test_subprocess_code_interpreter.py
index 2c7f7e47..3ce54530 100644
--- a/tests/utils/test_subprocess_code_interpreter.py
+++ b/tests/utils/test_subprocess_code_interpreter.py
@@ -1,307 +1,77 @@
+import pytest
import subprocess
import threading
-import time
-
-import pytest
-
+import queue
from swarms.utils.code_interpreter import (
- BaseCodeInterpreter,
SubprocessCodeInterpreter,
-)
+) # Adjust the import according to your project structure
+# Fixture for the SubprocessCodeInterpreter instance
@pytest.fixture
-def subprocess_code_interpreter():
- interpreter = SubprocessCodeInterpreter()
- interpreter.start_cmd = "python -c"
- yield interpreter
- interpreter.terminate()
-
-
-def test_base_code_interpreter_init():
- interpreter = BaseCodeInterpreter()
- assert isinstance(interpreter, BaseCodeInterpreter)
-
-
-def test_base_code_interpreter_run_not_implemented():
- interpreter = BaseCodeInterpreter()
- with pytest.raises(NotImplementedError):
- interpreter.run("code")
-
-
-def test_base_code_interpreter_terminate_not_implemented():
- interpreter = BaseCodeInterpreter()
- with pytest.raises(NotImplementedError):
- interpreter.terminate()
-
-
-def test_subprocess_code_interpreter_init(
- subprocess_code_interpreter,
-):
- assert isinstance(
- subprocess_code_interpreter, SubprocessCodeInterpreter
- )
-
-
-def test_subprocess_code_interpreter_start_process(
- subprocess_code_interpreter,
-):
- subprocess_code_interpreter.start_process()
- assert subprocess_code_interpreter.process is not None
-
-
-def test_subprocess_code_interpreter_terminate(
- subprocess_code_interpreter,
-):
- subprocess_code_interpreter.start_process()
- subprocess_code_interpreter.terminate()
- assert subprocess_code_interpreter.process.poll() is not None
-
-
-def test_subprocess_code_interpreter_run_success(
- subprocess_code_interpreter,
-):
- code = 'print("Hello, World!")'
- result = list(subprocess_code_interpreter.run(code))
- assert any(
- "Hello, World!" in output.get("output", "")
- for output in result
- )
-
-
-def test_subprocess_code_interpreter_run_with_error(
- subprocess_code_interpreter,
-):
- code = 'print("Hello, World")\nraise ValueError("Error!")'
- result = list(subprocess_code_interpreter.run(code))
- assert any(
- "Error!" in output.get("output", "") for output in result
- )
-
-
-def test_subprocess_code_interpreter_run_with_keyboard_interrupt(
- subprocess_code_interpreter,
-):
- code = (
- 'import time\ntime.sleep(2)\nprint("Hello, World")\nraise'
- " KeyboardInterrupt"
- )
- result = list(subprocess_code_interpreter.run(code))
- assert any(
- "KeyboardInterrupt" in output.get("output", "")
- for output in result
- )
-
-
-def test_subprocess_code_interpreter_run_max_retries(
- subprocess_code_interpreter, monkeypatch
-):
- def mock_subprocess_popen(*args, **kwargs):
- raise subprocess.CalledProcessError(1, "mocked_cmd")
-
- monkeypatch.setattr(subprocess, "Popen", mock_subprocess_popen)
+def interpreter():
+ return SubprocessCodeInterpreter()
- code = 'print("Hello, World!")'
- result = list(subprocess_code_interpreter.run(code))
- assert any(
- "Maximum retries reached. Could not execute code."
- in output.get("output", "")
- for output in result
- )
+# Test for correct initialization
+def test_initialization(interpreter):
+ assert interpreter.start_cmd == ""
+ assert interpreter.process is None
+ assert not interpreter.debug_mode
+ assert isinstance(interpreter.output_queue, queue.Queue)
+ assert isinstance(interpreter.done, threading.Event)
-def test_subprocess_code_interpreter_run_retry_on_error(
- subprocess_code_interpreter, monkeypatch
-):
- def mock_subprocess_popen(*args, **kwargs):
- nonlocal popen_count
- if popen_count == 0:
- popen_count += 1
- raise subprocess.CalledProcessError(1, "mocked_cmd")
- else:
- return subprocess.Popen(
- "echo 'Hello, World!'",
- shell=True,
- stdout=subprocess.PIPE,
- stderr=subprocess.PIPE,
- )
-
- monkeypatch.setattr(subprocess, "Popen", mock_subprocess_popen)
- popen_count = 0
-
- code = 'print("Hello, World!")'
- result = list(subprocess_code_interpreter.run(code))
- assert any(
- "Hello, World!" in output.get("output", "")
- for output in result
- )
+# Test for starting and terminating process
+def test_start_and_terminate_process(interpreter):
+ interpreter.start_cmd = "echo Hello"
+ interpreter.start_process()
+ assert isinstance(interpreter.process, subprocess.Popen)
+ interpreter.terminate()
+ assert (
+ interpreter.process.poll() is not None
+ ) # Process should be terminated
-# Add more tests to cover other aspects of the code and edge cases as needed
-# Import statements and fixtures from the previous code block
+# Test preprocess_code method
+def test_preprocess_code(interpreter):
+ code = "print('Hello, World!')"
+ processed_code = interpreter.preprocess_code(code)
+ # Add assertions based on expected behavior of preprocess_code
+ assert processed_code == code # Example assertion
-def test_subprocess_code_interpreter_line_postprocessor(
- subprocess_code_interpreter,
-):
- line = "This is a test line"
- processed_line = subprocess_code_interpreter.line_postprocessor(
- line
- )
+# Test detect_active_line method
+def test_detect_active_line(interpreter):
+ line = "Some line of code"
assert (
- processed_line == line
- ) # No processing, should remain the same
+ interpreter.detect_active_line(line) is None
+ ) # Adjust assertion based on implementation
-def test_subprocess_code_interpreter_preprocess_code(
- subprocess_code_interpreter,
-):
- code = 'print("Hello, World!")'
- preprocessed_code = subprocess_code_interpreter.preprocess_code(
- code
- )
+# Test detect_end_of_execution method
+def test_detect_end_of_execution(interpreter):
+ line = "End of execution line"
assert (
- preprocessed_code == code
- ) # No preprocessing, should remain the same
-
-
-def test_subprocess_code_interpreter_detect_active_line(
- subprocess_code_interpreter,
-):
- line = "Active line: 5"
- active_line = subprocess_code_interpreter.detect_active_line(line)
- assert active_line == 5
-
-
-def test_subprocess_code_interpreter_detect_end_of_execution(
- subprocess_code_interpreter,
-):
- line = "Execution completed."
- end_of_execution = (
- subprocess_code_interpreter.detect_end_of_execution(line)
- )
- assert end_of_execution is True
-
-
-def test_subprocess_code_interpreter_run_debug_mode(
- subprocess_code_interpreter, capsys
-):
- subprocess_code_interpreter.debug_mode = True
- code = 'print("Hello, World!")'
- list(subprocess_code_interpreter.run(code))
- captured = capsys.readouterr()
- assert "Running code:\n" in captured.out
- assert "Received output line:\n" in captured.out
-
-
-def test_subprocess_code_interpreter_run_no_debug_mode(
- subprocess_code_interpreter, capsys
-):
- subprocess_code_interpreter.debug_mode = False
- code = 'print("Hello, World!")'
- list(subprocess_code_interpreter.run(code))
- captured = capsys.readouterr()
- assert "Running code:\n" not in captured.out
- assert "Received output line:\n" not in captured.out
-
+ interpreter.detect_end_of_execution(line) is None
+ ) # Adjust assertion based on implementation
-def test_subprocess_code_interpreter_run_empty_output_queue(
- subprocess_code_interpreter,
-):
- code = 'print("Hello, World!")'
- result = list(subprocess_code_interpreter.run(code))
- assert not any("active_line" in output for output in result)
-
-
-def test_subprocess_code_interpreter_handle_stream_output_stdout(
- subprocess_code_interpreter,
-):
- line = "This is a test line"
- subprocess_code_interpreter.handle_stream_output(
- threading.current_thread(), False
- )
- subprocess_code_interpreter.process.stdout.write(line + "\n")
- subprocess_code_interpreter.process.stdout.flush()
- time.sleep(0.1)
- output = subprocess_code_interpreter.output_queue.get()
- assert output["output"] == line
-
-
-def test_subprocess_code_interpreter_handle_stream_output_stderr(
- subprocess_code_interpreter,
-):
- line = "This is an error line"
- subprocess_code_interpreter.handle_stream_output(
- threading.current_thread(), True
- )
- subprocess_code_interpreter.process.stderr.write(line + "\n")
- subprocess_code_interpreter.process.stderr.flush()
- time.sleep(0.1)
- output = subprocess_code_interpreter.output_queue.get()
- assert output["output"] == line
-
-
-def test_subprocess_code_interpreter_run_with_preprocess_code(
- subprocess_code_interpreter, capsys
-):
- code = 'print("Hello, World!")'
- subprocess_code_interpreter.preprocess_code = (
- lambda x: x.upper()
- ) # Modify code in preprocess_code
- result = list(subprocess_code_interpreter.run(code))
- assert any(
- "Hello, World!" in output.get("output", "")
- for output in result
- )
-
-
-def test_subprocess_code_interpreter_run_with_exception(
- subprocess_code_interpreter, capsys
-):
- code = 'print("Hello, World!")'
- subprocess_code_interpreter.start_cmd = ( # Force an exception during subprocess creation
- "nonexistent_command"
- )
- result = list(subprocess_code_interpreter.run(code))
- assert any(
- "Maximum retries reached" in output.get("output", "")
- for output in result
- )
-
-
-def test_subprocess_code_interpreter_run_with_active_line(
- subprocess_code_interpreter, capsys
-):
- code = "a = 5\nprint(a)" # Contains an active line
- result = list(subprocess_code_interpreter.run(code))
- assert any(output.get("active_line") == 5 for output in result)
-
-
-def test_subprocess_code_interpreter_run_with_end_of_execution(
- subprocess_code_interpreter, capsys
-):
- code = ( # Simple code without active line marker
- 'print("Hello, World!")'
- )
- result = list(subprocess_code_interpreter.run(code))
- assert any(output.get("active_line") is None for output in result)
+# Test line_postprocessor method
+def test_line_postprocessor(interpreter):
+ line = "Some output line"
+ assert (
+ interpreter.line_postprocessor(line) == line
+ ) # Adjust assertion based on implementation
-def test_subprocess_code_interpreter_run_with_multiple_lines(
- subprocess_code_interpreter, capsys
-):
- code = "a = 5\nb = 10\nprint(a + b)"
- result = list(subprocess_code_interpreter.run(code))
- assert any("15" in output.get("output", "") for output in result)
+# Test handle_stream_output method
+def test_handle_stream_output(interpreter, monkeypatch):
+ # This requires more complex setup, including monkeypatching and simulating stream output
+ # Example setup
+ def mock_readline():
+ yield "output line"
+ yield ""
-def test_subprocess_code_interpreter_run_with_unicode_characters(
- subprocess_code_interpreter, capsys
-):
- code = 'print("γγγ«γ‘γ―γδΈη")' # Contains unicode characters
- result = list(subprocess_code_interpreter.run(code))
- assert any(
- "γγγ«γ‘γ―γδΈη" in output.get("output", "")
- for output in result
- )
+ monkeypatch.setattr("sys.stdout", mock_readline())
+ # More test code needed here to simulate and assert the behavior of handle_stream_output
diff --git a/tests/utils/test_try_except_wrapper.py b/tests/utils/test_try_except_wrapper.py
new file mode 100644
index 00000000..26b509fb
--- /dev/null
+++ b/tests/utils/test_try_except_wrapper.py
@@ -0,0 +1,45 @@
+from swarms.utils.try_except_wrapper import try_except_wrapper
+
+
+def test_try_except_wrapper_with_no_exception():
+ @try_except_wrapper
+ def add(x, y):
+ return x + y
+
+ result = add(1, 2)
+ assert (
+ result == 3
+ ), "The function should return the sum of the arguments"
+
+
+def test_try_except_wrapper_with_exception():
+ @try_except_wrapper
+ def divide(x, y):
+ return x / y
+
+ result = divide(1, 0)
+ assert (
+ result is None
+ ), "The function should return None when an exception is raised"
+
+
+def test_try_except_wrapper_with_multiple_arguments():
+ @try_except_wrapper
+ def concatenate(*args):
+ return "".join(args)
+
+ result = concatenate("Hello", " ", "world")
+ assert (
+ result == "Hello world"
+ ), "The function should concatenate the arguments"
+
+
+def test_try_except_wrapper_with_keyword_arguments():
+ @try_except_wrapper
+ def greet(name="world"):
+ return f"Hello, {name}"
+
+ result = greet(name="Alice")
+ assert (
+ result == "Hello, Alice"
+ ), "The function should use the keyword arguments"