pull/348/head
Kye 12 months ago
parent 4f39f4fe0a
commit 466f27a714

@ -21,6 +21,7 @@ A modular framework that enables you to Build, Deploy, and Scale Reliable Autono
--- ---
## Usage ## 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: <a target="_blank" href="https://colab.research.google.com/github/kyegomez/swarms/blob/master/playground/swarms_example.ipynb"> Run example in Collab: <a target="_blank" href="https://colab.research.google.com/github/kyegomez/swarms/blob/master/playground/swarms_example.ipynb">
<img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/> <img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/>

@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry] [tool.poetry]
name = "swarms" name = "swarms"
version = "3.8.7" version = "3.8.8"
description = "Swarms - Pytorch" description = "Swarms - Pytorch"
license = "MIT" license = "MIT"
authors = ["Kye Gomez <kye@apac.ai>"] authors = ["Kye Gomez <kye@apac.ai>"]
@ -105,4 +105,4 @@ preview = true
[tool.poetry.scripts] [tool.poetry.scripts]
swarms = 'swarms.cli._cli:cli' swarms = 'swarms.cli._cli:main'

@ -1,37 +1,95 @@
from swarms.structs.conversation import Conversation from swarms.structs.conversation import Conversation
from swarms.models.base_llm import AbstractLLM 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 # Run the language model in a loop for n iterations
def SimpleAgent( def SimpleAgent(
llm: AbstractLLM = None, iters: int = 10, *args, **kwargs llm: AbstractLLM = None, iters: Any = "automatic", *args, **kwargs
): ):
"""Simple agent conversation """
A simple agent that interacts with a language model.
Args: Args:
llm (_type_): _description_ llm (AbstractLLM): The language model to use for generating responses.
iters (int, optional): _description_. Defaults to 10. iters (Any): The number of iterations or "automatic" to run indefinitely.
*args: Additional positional arguments to pass to the language model.
Example: **kwargs: Additional keyword arguments to pass to the language model.
>>> from swarms.models import GPT2LM
>>> from swarms.agents import SimpleAgent Raises:
>>> llm = GPT2LM() Exception: If the language model is not defined or cannot be found.
>>> SimpleAgent(llm, iters=10)
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: try:
conv = Conversation(*args, **kwargs) 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): for i in range(iters):
user_input = input("User: ") user_input = input("\033[91mUser:\033[0m ")
conv.add("user", user_input) conv.add("user", user_input)
if user_input.lower() == "quit": if user_input.lower() == "quit":
break break
task = ( task = (
conv.return_history_as_string() conv.return_history_as_string()
) # Get the conversation history ) # Get the conversation history
out = llm(task) out = llm(task, *args, **kwargs)
conv.add("assistant", out) conv.add("assistant", out)
print( print(
f"Assistant: {out}", f"\033[94mAssistant:\033[0m {out}",
) )
conv.display_conversation() conv.display_conversation()
conv.export_conversation("conversation.txt") conv.export_conversation("conversation.txt")

@ -1,75 +1,53 @@
import argparse import argparse
import sys from swarms.agents.simple_agent import SimpleAgent, get_llm_by_name
def cli(): def main():
parser = argparse.ArgumentParser(description="Swarms CLI") parser = argparse.ArgumentParser(
parser.add_argument( prog="swarms",
"file_name", help="Python file containing Swarms code to run" description=(
) "Run the SimpleAgent with a specified language model."
# Help message for the -h flag is automatically generated by argparse ),
parser.add_argument(
"-v", "--version", action="version", version="%(prog)s 0.1.0"
) )
subparsers = parser.add_subparsers(dest="command")
# Check deployments for a given model run_parser = subparsers.add_parser(
parser.add_argument( "run", help="Run the SimpleAgent."
"-c", "--check", help="Check deployments for a given agent"
) )
run_parser.add_argument(
# Generate an API key for a given agent "modelname",
parser.add_argument( type=str,
"-g", help="The name of the language model to use.",
"--generate",
help="Generate an API key for a given agent",
) )
run_parser.add_argument(
# Signin to swarms with a given API key "--iters",
parser.add_argument( type=int,
"-s", "--signin", help="Signin to swarms with a given API key" default="automatic",
help=(
'Number of iterations or "automatic" for infinite loop.'
' Defaults to "automatic".'
),
) )
# Signout of swarms # Add a help command
parser.add_argument("-o", "--signout", help="Signout of swarms") help_parser = subparsers.add_parser(
"help", help="Show this help message and exit."
# List all agents
parser.add_argument("-l", "--list", help="List all agents")
# List all deployments
parser.add_argument(
"-d", "--deployments", help="List all deployments"
) )
help_parser.set_defaults(func=lambda args: parser.print_help())
# Pricing information args = parser.parse_args()
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")
# Get a deployment's logs if hasattr(args, "func"):
parser.add_argument( args.func(args)
"-z", "--logs", help="Get a deployment's logs" 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 # if __name__ == "__main__":
try: # main()
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)

@ -0,0 +1,12 @@
from swarms.tokenizers.r_tokenizers import (
SentencePieceTokenizer,
HuggingFaceTokenizer,
Tokenizer,
)
__all__ = [
"SentencePieceTokenizer",
"HuggingFaceTokenizer",
"Tokenizer",
]

@ -1,14 +1,7 @@
from unittest.mock import Mock from unittest.mock import Mock
import torch import torch
import pytest import pytest
from swarms.models.timm import TimmModel, TimmModelInfo from swarms.models.timm import TimmModel
@pytest.fixture
def sample_model_info():
return TimmModelInfo(
model_name="resnet18", pretrained=True, in_chans=3
)
def test_get_supported_models(): def test_get_supported_models():
@ -33,45 +26,6 @@ def test_call(sample_model_info):
assert isinstance(output_shape, torch.Size) 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(): def test_get_supported_models_mock():
model_handler = TimmModel() model_handler = TimmModel()
model_handler._get_supported_models = Mock( model_handler._get_supported_models = Mock(
@ -88,98 +42,6 @@ def test_create_model_mock(sample_model_info):
assert isinstance(model, torch.nn.Module) 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(): def test_coverage_report():
# Install pytest-cov # Install pytest-cov
# Run tests with coverage report # Run tests with coverage report

@ -1,11 +1,11 @@
from unittest.mock import patch from unittest.mock import patch
from swarms.models.ultralytics_model import Ultralytics from swarms.models.ultralytics_model import UltralyticsModel
def test_ultralytics_init(): def test_ultralytics_init():
with patch("swarms.models.YOLO") as mock_yolo: with patch("swarms.models.YOLO") as mock_yolo:
model_name = "yolov5s" model_name = "yolov5s"
ultralytics = Ultralytics(model_name) ultralytics = UltralyticsModel(model_name)
mock_yolo.assert_called_once_with(model_name) mock_yolo.assert_called_once_with(model_name)
assert ultralytics.model_name == model_name assert ultralytics.model_name == model_name
assert ultralytics.model == mock_yolo.return_value assert ultralytics.model == mock_yolo.return_value
@ -14,7 +14,7 @@ def test_ultralytics_init():
def test_ultralytics_call(): def test_ultralytics_call():
with patch("swarms.models.YOLO") as mock_yolo: with patch("swarms.models.YOLO") as mock_yolo:
model_name = "yolov5s" model_name = "yolov5s"
ultralytics = Ultralytics(model_name) ultralytics = UltralyticsModel(model_name)
task = "detect" task = "detect"
args = (1, 2, 3) args = (1, 2, 3)
kwargs = {"a": "A", "b": "B"} kwargs = {"a": "A", "b": "B"}
@ -28,7 +28,7 @@ def test_ultralytics_call():
def test_ultralytics_list_models(): def test_ultralytics_list_models():
with patch("swarms.models.YOLO") as mock_yolo: with patch("swarms.models.YOLO") as mock_yolo:
model_name = "yolov5s" model_name = "yolov5s"
ultralytics = Ultralytics(model_name) ultralytics = UltralyticsModel(model_name)
result = ultralytics.list_models() result = ultralytics.list_models()
mock_yolo.list_models.assert_called_once() mock_yolo.list_models.assert_called_once()
assert result == mock_yolo.list_models.return_value assert result == mock_yolo.list_models.return_value

@ -2,7 +2,7 @@ from unittest.mock import MagicMock, Mock, patch
import pytest import pytest
from swarm_net import SwarmNet from swarms.structs.swarm_net import SwarmNet
from swarms.structs.agent import Agent from swarms.structs.agent import Agent
from swarms.structs.swarm_net import SwarmNetwork from swarms.structs.swarm_net import SwarmNetwork

Loading…
Cancel
Save