diff --git a/.github/workflows/code_quality_control.yml b/.github/workflows/code_quality_control.yml index 4b94b454..f5c741c8 100644 --- a/.github/workflows/code_quality_control.yml +++ b/.github/workflows/code_quality_control.yml @@ -3,7 +3,7 @@ name: Linting and Formatting on: push: branches: - - main + - master jobs: lint_and_format: diff --git a/.github/workflows/cos_integration.yml b/.github/workflows/cos_integration.yml index abae70b2..7cdb41e9 100644 --- a/.github/workflows/cos_integration.yml +++ b/.github/workflows/cos_integration.yml @@ -3,7 +3,7 @@ name: Continuous Integration on: push: branches: - - main + - master jobs: test: diff --git a/.github/workflows/pr_request_checks.yml b/.github/workflows/pr_request_checks.yml index dccdb9e2..1d176346 100644 --- a/.github/workflows/pr_request_checks.yml +++ b/.github/workflows/pr_request_checks.yml @@ -23,5 +23,5 @@ jobs: - name: Run tests and checks run: | - pytest tests/ + find tests/ -name "*.py" | xargs pytest pylint swarms \ No newline at end of file diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml index 5e893380..9b09cfa9 100644 --- a/.github/workflows/quality.yml +++ b/.github/workflows/quality.yml @@ -2,9 +2,9 @@ name: Quality on: push: - branches: [ "main" ] + branches: [ "master" ] pull_request: - branches: [ "main" ] + branches: [ "master" ] jobs: lint: @@ -20,4 +20,4 @@ jobs: uses: ./.github/actions/init-environment - name: Run linter run: | - pylint `git diff --name-only --diff-filter=d origin/main HEAD | grep -E '\.py$' | tr '\n' ' '` \ No newline at end of file + pylint `git diff --name-only --diff-filter=d origin/master HEAD | grep -E '\.py$' | tr '\n' ' '` \ No newline at end of file diff --git a/.github/workflows/run_test.yml b/.github/workflows/run_test.yml index 03726b64..172dc64b 100644 --- a/.github/workflows/run_test.yml +++ b/.github/workflows/run_test.yml @@ -20,4 +20,4 @@ jobs: if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Run tests with pytest run: | - pytest tests/ + find tests/ -name "*.py" | xargs pytest diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index d6a40768..099d3054 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -22,4 +22,4 @@ jobs: run: pip install -r requirements.txt - name: Run unit tests - run: pytest tests/ \ No newline at end of file + run: find tests/ -name "*.py" | xargs pytest \ No newline at end of file diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 6a0a6481..42ac2271 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -30,4 +30,4 @@ jobs: run: docker build . --file Dockerfile - name: Verify integration test results - run: python3 -m unittest tests/ + run: find tests/ -name "*.py" | xargs pytest diff --git a/README.md b/README.md index 8b1fc68a..ea202c03 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![Swarming banner icon](images/swarmsbanner.png) +![Swarming banner icon](images/swarmslogobanner.png)
@@ -29,7 +29,7 @@ Book a [1-on-1 Session with Kye](https://calendly.com/swarm-corp/30min), the Cre ---------- ## Installation -* `pip3 install --upgrade swarms` +`pip3 install --upgrade swarms` --- @@ -156,34 +156,15 @@ agent.run("Create a video of a swarm of fish") ``` -- OmniModal Agent has a ui in the root called `python3 omni_ui.py` - --- # Documentation -For documentation, go here, [swarms.apac.ai](https://swarms.apac.ai) - ------ - -# ✨ Features -* Easy to use Base LLMs, `OpenAI` `Palm` `Anthropic` `HuggingFace` -* Enterprise Grade, Production Ready with robust Error Handling -* Multi-Modality Native with Multi-Modal LLMs as tools -* Infinite Memory Processing: Store infinite sequences of infinite Multi-Modal data, text, images, videos, audio -* Usability: Extreme emphasis on useability, code is at it's theortical minimum simplicity factor to use -* Reliability: Outputs that accomplish tasks and activities you wish to execute. -* Fluidity: A seamless all-around experience to build production grade workflows -* Speed: Lower the time to automate tasks by 90%. -* Simplicity: Swarms is extremely simple to use, if not thee simplest agent framework of all time -* Powerful: Swarms is capable of building entire software apps, to large scale data analysis, and handling chaotic situations - +- For documentation, go here, [swarms.apac.ai](https://swarms.apac.ai) ----- ## Contribute -We're always looking for contributors to help us improve and expand this project. If you're interested, please check out our [Contributing Guidelines](C0NTRIBUTING.md). - - +We're always looking for contributors to help us improve and expand this project. If you're interested, please check out our [Contributing Guidelines](CONTRIBUTING.md). ### Optimization Priorities @@ -195,17 +176,6 @@ We're always looking for contributors to help us improve and expand this project Our goal is to continuously improve Swarms by following this roadmap, while also being adaptable to new needs and opportunities as they arise. ---- - -# Demos - -![Swarms Demo](images/Screenshot_48.png) - -## Swarm Video Demo {Click for more} - -[![Watch the swarm video](https://img.youtube.com/vi/Br62cDMYXgc/maxresdefault.jpg)](https://youtu.be/Br62cDMYXgc) - ---- -# Contact -For enterprise and production ready deployments, allow us to discover more about you and your story, [book a call with us here](https://www.apac.ai/Setup-Call) \ No newline at end of file +# License +MIT diff --git a/api/app.py b/api/app.py deleted file mode 100644 index fc2b0aec..00000000 --- a/api/app.py +++ /dev/null @@ -1,51 +0,0 @@ -import logging -import os -from fastapi import FastAPI, HTTPException, Depends -from fastapi_cache.decorator import cache -from fastapi_cache.coder import JsonCoder - -from fastapi_cache import FastAPICache -from fastapi_cache.backends.redis import RedisBackend -from aioredis import Redis - -from pydantic import BaseModel -from swarms.swarms.swarms import swarm -from fastapi_limiter import FastAPILimiter - -from fastapi_limiter.depends import RateLimiter -from dotenv import load_dotenv - -load_dotenv() - - -class SwarmInput(BaseModel): - api_key: str - objective: str - - -app = FastAPI() - - -@app.on_event("startup") -async def startup(): - redis_host = os.getenv("REDIS_HOST", "localhost") - redis_port = int(os.getenv("REDIS_PORT", 6379)) - redis = await Redis.create(redis_host, redis_port) - FastAPICache.init(RedisBackend(redis), prefix="fastapi-cache", coder=JsonCoder()) - await FastAPILimiter.init(f"redis://{redis_host}:{redis_port}") - - -@app.post("/chat", dependencies=[Depends(RateLimiter(times=2, minutes=1))]) -@cache(expire=60) # Cache results for 1 minute -async def run(swarm_input: SwarmInput): - try: - results = swarm(swarm_input.api_key, swarm_input.objective) - if not results: - raise HTTPException(status_code=500, detail="Failed to run swarms") - return {"results": results} - except ValueError as ve: - logging.error("A ValueError occurred", exc_info=True) - raise HTTPException(status_code=400, detail=str(ve)) - except Exception: - logging.error("An error occurred", exc_info=True) - raise HTTPException(status_code=500, detail="An unexpected error occurred") diff --git a/api/olds/container.py b/api/olds/container.py deleted file mode 100644 index f90dfd31..00000000 --- a/api/olds/container.py +++ /dev/null @@ -1,60 +0,0 @@ -import os -from pathlib import Path -from typing import Dict, List - -from fastapi.templating import Jinja2Templates - -from swarms.agents.utils.agent_creator import AgentManager -from swarms.utils.main import BaseHandler, FileHandler, FileType -from swarms.tools.main import ExitConversation, RequestsGet, CodeEditor, Terminal - -from swarms.utils.main import CsvToDataframe - -from swarms.tools.main import BaseToolSet - -from swarms.utils.main import StaticUploader - -BASE_DIR = Path(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) -os.chdir(BASE_DIR / os.environ["PLAYGROUND_DIR"]) - -# -toolsets: List[BaseToolSet] = [ - Terminal(), - CodeEditor(), - RequestsGet(), - ExitConversation(), -] -handlers: Dict[FileType, BaseHandler] = {FileType.DATAFRAME: CsvToDataframe()} - -if os.environ["USE_GPU"]: - import torch - - # from core.handlers.image import ImageCaptioning - from swarms.tools.main import ImageCaptioning - from swarms.tools.main import ( - ImageEditing, - InstructPix2Pix, - Text2Image, - VisualQuestionAnswering, - ) - - if torch.cuda.is_available(): - toolsets.extend( - [ - Text2Image("cuda"), - ImageEditing("cuda"), - InstructPix2Pix("cuda"), - VisualQuestionAnswering("cuda"), - ] - ) - handlers[FileType.IMAGE] = ImageCaptioning("cuda") - -agent_manager = AgentManager.create(toolsets=toolsets) - -file_handler = FileHandler(handlers=handlers, path=BASE_DIR) - -templates = Jinja2Templates(directory=BASE_DIR / "api" / "templates") - -uploader = StaticUploader.from_settings(path=BASE_DIR / "static", endpoint="static") - -reload_dirs = [BASE_DIR / "core", BASE_DIR / "api"] diff --git a/api/olds/main.py b/api/olds/main.py deleted file mode 100644 index 0ba69379..00000000 --- a/api/olds/main.py +++ /dev/null @@ -1,137 +0,0 @@ -import os - -import re -from multiprocessing import Process -from tempfile import NamedTemporaryFile -from typing import List, TypedDict - -import uvicorn -from fastapi import FastAPI, Request, UploadFile -from fastapi.responses import HTMLResponse -from fastapi.staticfiles import StaticFiles -from pydantic import BaseModel - -from api.olds.container import ( - agent_manager, - file_handler, - reload_dirs, - templates, - uploader, -) -from api.olds.worker import get_task_result, start_worker, task_execute - -# from env import settings - -app = FastAPI() - -app.mount("/static", StaticFiles(directory=uploader.path), name="static") - - -class ExecuteRequest(BaseModel): - session: str - prompt: str - files: List[str] - - -class ExecuteResponse(TypedDict): - answer: str - files: List[str] - - -@app.get("/", response_class=HTMLResponse) -async def index(request: Request): - return templates.TemplateResponse("index.html", {"request": request}) - - -@app.get("/dashboard", response_class=HTMLResponse) -async def dashboard(request: Request): - return templates.TemplateResponse("dashboard.html", {"request": request}) - - -@app.post("/upload") -async def create_upload_file(files: List[UploadFile]): - urls = [] - for file in files: - extension = "." + file.filename.split(".")[-1] - with NamedTemporaryFile(suffix=extension) as tmp_file: - tmp_file.write(file.file.read()) - tmp_file.flush() - urls.append(uploader.upload(tmp_file.name)) - return {"urls": urls} - - -@app.post("/api/execute") -async def execute(request: ExecuteRequest) -> ExecuteResponse: - query = request.prompt - files = request.files - session = request.session - - executor = agent_manager.create_executor(session) - - promptedQuery = "\n".join([file_handler.handle(file) for file in files]) - promptedQuery += query - - try: - res = executor({"input": promptedQuery}) - except Exception as e: - return {"answer": str(e), "files": []} - - files = re.findall(r"\[file://\S*\]", res["output"]) - files = [file[1:-1].split("file://")[1] for file in files] - - return { - "answer": res["output"], - "files": [uploader.upload(file) for file in files], - } - - -@app.post("/api/execute/async") -async def execute_async(request: ExecuteRequest): - query = request.prompt - files = request.files - session = request.session - - promptedQuery = "\n".join([file_handler.handle(file) for file in files]) - promptedQuery += query - - execution = task_execute.delay(session, promptedQuery) - return {"id": execution.id} - - -@app.get("/api/execute/async/{execution_id}") -async def execute_async(execution_id: str): - execution = get_task_result(execution_id) - - result = {} - if execution.status == "SUCCESS" and execution.result: - output = execution.result.get("output", "") - files = re.findall(r"\[file://\S*\]", output) - files = [file[1:-1].split("file://")[1] for file in files] - result = { - "answer": output, - "files": [uploader.upload(file) for file in files], - } - - return { - "status": execution.status, - "info": execution.info, - "result": result, - } - - -def serve(): - p = Process(target=start_worker, args=[]) - p.start() - uvicorn.run("api.main:app", host="0.0.0.0", port=os.environ["EVAL_PORT"]) - - -def dev(): - p = Process(target=start_worker, args=[]) - p.start() - uvicorn.run( - "api.main:app", - host="0.0.0.0", - port=os.environ["EVAL_PORT"], - reload=True, - reload_dirs=reload_dirs, - ) diff --git a/api/olds/worker.py b/api/olds/worker.py deleted file mode 100644 index 911101a5..00000000 --- a/api/olds/worker.py +++ /dev/null @@ -1,44 +0,0 @@ -import os - -from celery import Celery -from celery.result import AsyncResult - -from api.olds.container import agent_manager - - -celery_app = Celery(__name__) -celery_app.conf.broker_url = os.environ["CELERY_BROKER_URL"] -celery_app.conf.result_backend = os.environ["CELERY_BROKER_URL"] -celery_app.conf.update( - task_track_started=True, - task_serializer="json", - accept_content=["json"], # Ignore other content - result_serializer="json", - enable_utc=True, -) - - -@celery_app.task(name="task_execute", bind=True) -def task_execute(self, session: str, prompt: str): - executor = agent_manager.create_executor(session, self) - response = executor({"input": prompt}) - result = {"output": response["output"]} - - previous = AsyncResult(self.request.id) - if previous and previous.info: - result.update(previous.info) - - return result - - -def get_task_result(task_id): - return AsyncResult(task_id) - - -def start_worker(): - celery_app.worker_main( - [ - "worker", - "--loglevel=INFO", - ] - ) diff --git a/apps/discord.py b/apps/discord.py index eebc48c8..27c17b9c 100644 --- a/apps/discord.py +++ b/apps/discord.py @@ -107,10 +107,11 @@ class BotCommands(commands.Cog): else: await ctx.send(f"an error occurred: {error}") + class Bot: def __init__(self, llm, command_prefix="!"): load_dotenv() - + bing-chat intents = discord.Intents.default() intents.messages = True intents.guilds = True @@ -119,12 +120,130 @@ class Bot: # setup self.llm = llm - self.bot = commands.Bot(command_prefix="!", intents=intents) + self.agent = agent + self.bot = commands.bot(command_prefix="!", intents=intents) self.discord_token = os.getenv("DISCORD_TOKEN") self.storage_service = os.getenv("STORAGE_SERVICE") - # Load the BotCommands cog - self.bot.add_cog(BotCommands(self.bot)) - - def run(self): - self.bot.run(self.discord_token) + @self.bot.event + async def on_ready(): + print(f"we have logged in as {self.bot.user}") + + @self.bot.command() + async def greet(ctx): + """greets the user.""" + await ctx.send(f"hello, {ctx.author.name}!") + + @self.bot.command() + async def help_me(ctx): + """provides a list of commands and their descriptions.""" + help_text = """ + - `!greet`: greets you. + - `!run [description]`: generates a video based on the given description. + - `!help_me`: provides this list of commands and their descriptions. + """ + await ctx.send(help_text) + + @self.bot.event + async def on_command_error(ctx, error): + """handles errors that occur while executing commands.""" + if isinstance(error, commands.commandnotfound): + await ctx.send("that command does not exist!") + else: + await ctx.send(f"an error occurred: {error}") + + @self.bot.command() + async def join(ctx): + """joins the voice channel that the user is in.""" + if ctx.author.voice: + channel = ctx.author.voice.channel + await channel.connect() + else: + await ctx.send("you are not in a voice channel!") + + @self.bot.command() + async def leave(ctx): + """leaves the voice channel that the self.bot is in.""" + if ctx.voice_client: + await ctx.voice_client.disconnect() + else: + await ctx.send("i am not in a voice channel!") + + # voice_transcription.py + @self.bot.command() + async def listen(ctx): + """starts listening to voice in the voice channel that the bot is in.""" + if ctx.voice_client: + # create a wavesink to record the audio + sink = discord.sinks.wavesink("audio.wav") + # start recording + ctx.voice_client.start_recording(sink) + await ctx.send("started listening and recording.") + else: + await ctx.send("i am not in a voice channel!") + + # image_generator.py + @self.bot.command() + async def generate_image(ctx, *, prompt: str): + """generates images based on the provided prompt""" + await ctx.send(f"generating images for prompt: `{prompt}`...") + loop = asyncio.get_event_loop() + + # initialize a future object for the dalle instance + model_instance = dalle3() + future = loop.run_in_executor(Executor, model_instance.run, prompt) + + try: + # wait for the dalle request to complete, with a timeout of 60 seconds + await asyncio.wait_for(future, timeout=300) + print("done generating images!") + + # list all files in the save_directory + all_files = [ + os.path.join(root, file) + for root, _, files in os.walk(os.environ("SAVE_DIRECTORY")) + for file in files + ] + + # sort files by their creation time (latest first) + sorted_files = sorted(all_files, key=os.path.getctime, reverse=True) + + # get the 4 most recent files + latest_files = sorted_files[:4] + print(f"sending {len(latest_files)} images to discord...") + + # send all the latest images in a single message + storage_service = os.environ( + "STORAGE_SERVICE" + ) # "https://storage.googleapis.com/your-bucket-name/ + await ctx.send( + files=[ + storage_service.upload(filepath) for filepath in latest_files + ] + ) + + except asyncio.timeouterror: + await ctx.send( + "the request took too long! it might have been censored or you're out of boosts. please try entering the prompt again." + ) + except Exception as e: + await ctx.send(f"an error occurred: {e}") + + @self.bot.command() + async def send_text(ctx, *, text: str, use_agent: bool = True): + """sends the provided text to the worker and returns the response""" + if use_agent: + response = self.agent.run(text) + else: + response = self.llm.run(text) + await ctx.send(response) + + def add_command(self, name, func): + @self.bot.command() + async def command(ctx, *args): + reponse = func(*args) + await ctx.send(responses) + + +def run(self): + self.bot.run("DISCORD_TOKEN") \ No newline at end of file diff --git a/docs/swarms/models/huggingface.md b/docs/swarms/models/huggingface.md new file mode 100644 index 00000000..e429f080 --- /dev/null +++ b/docs/swarms/models/huggingface.md @@ -0,0 +1,153 @@ +## `HuggingfaceLLM` Documentation + +### Introduction + +The `HuggingfaceLLM` class is designed for running inference using models from the Hugging Face Transformers library. This documentation provides an in-depth understanding of the class, its purpose, attributes, methods, and usage examples. + +#### Purpose + +The `HuggingfaceLLM` class serves the following purposes: + +1. Load pre-trained Hugging Face models and tokenizers. +2. Generate text-based responses from the loaded model using a given prompt. +3. Provide flexibility in device selection, quantization, and other configuration options. + +### Class Definition + +The `HuggingfaceLLM` class is defined as follows: + +```python +class HuggingfaceLLM: + def __init__( + self, + model_id: str, + device: str = None, + max_length: int = 20, + quantize: bool = False, + quantization_config: dict = None, + verbose=False, + distributed=False, + decoding=False, + ): + # Attributes and initialization logic explained below + pass + + def load_model(self): + # Method to load the pre-trained model and tokenizer + pass + + def run(self, prompt_text: str, max_length: int = None): + # Method to generate text-based responses + pass + + def __call__(self, prompt_text: str, max_length: int = None): + # Alternate method for generating text-based responses + pass +``` + +### Attributes + +| Attribute | Description | +|----------------------|---------------------------------------------------------------------------------------------------------------------------| +| `model_id` | The ID of the pre-trained model to be used. | +| `device` | The device on which the model runs (`'cuda'` for GPU or `'cpu'` for CPU). | +| `max_length` | The maximum length of the generated text. | +| `quantize` | A boolean indicating whether quantization should be used. | +| `quantization_config`| A dictionary with configuration options for quantization. | +| `verbose` | A boolean indicating whether verbose logs should be printed. | +| `logger` | An optional logger for logging messages (defaults to a basic logger). | +| `distributed` | A boolean indicating whether distributed processing should be used. | +| `decoding` | A boolean indicating whether to perform decoding during text generation. | + +### Class Methods + +#### `__init__` Method + +The `__init__` method initializes an instance of the `HuggingfaceLLM` class with the specified parameters. It also loads the pre-trained model and tokenizer. + +- `model_id` (str): The ID of the pre-trained model to use. +- `device` (str, optional): The device to run the model on ('cuda' or 'cpu'). +- `max_length` (int, optional): The maximum length of the generated text. +- `quantize` (bool, optional): Whether to use quantization. +- `quantization_config` (dict, optional): Configuration for quantization. +- `verbose` (bool, optional): Whether to print verbose logs. +- `logger` (logging.Logger, optional): The logger to use. +- `distributed` (bool, optional): Whether to use distributed processing. +- `decoding` (bool, optional): Whether to perform decoding during text generation. + +#### `load_model` Method + +The `load_model` method loads the pre-trained model and tokenizer specified by `model_id`. + +#### `run` and `__call__` Methods + +Both `run` and `__call__` methods generate text-based responses based on a given prompt. They accept the following parameters: + +- `prompt_text` (str): The text prompt to initiate text generation. +- `max_length` (int, optional): The maximum length of the generated text. + +### Usage Examples + +Here are three ways to use the `HuggingfaceLLM` class: + +#### Example 1: Basic Usage + +```python +from swarms.models import HuggingfaceLLM + +# Initialize the HuggingfaceLLM instance with a model ID +model_id = "gpt2-small" +inference = HuggingfaceLLM(model_id=model_id) + +# Generate text based on a prompt +prompt_text = "Once upon a time" +generated_text = inference(prompt_text) +print(generated_text) +``` + +#### Example 2: Custom Configuration + +```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="gpt2-small", **custom_config) + +# Generate text based on a prompt +prompt_text = "Tell me a joke" +generated_text = inference(prompt_text) +print(generated_text) +``` + +#### Example 3: Distributed Processing + +```python +from swarms.models import HuggingfaceLLM + +# Initialize for distributed processing +inference = HuggingfaceLLM(model_id="gpt2-medium", distributed=True) + +# Generate text based on a prompt +prompt_text = "Translate the following sentence to French" +generated_text = inference(prompt_text) +print(generated_text) +``` + +### Additional Information + +- The `HuggingfaceLLM` class provides the flexibility to load and use pre-trained models from the Hugging Face Transformers library. +- Quantization can be enabled to reduce model size and inference time. +- Distributed processing can be used for parallelized inference. +- Verbose logging can help in debugging and understanding the text generation process. + +### References + +- [Hugging Face Transformers Documentation](https://huggingface.co/transformers/) +- [PyTorch Documentation](https://pytorch.org/docs/stable/index.html) + +This documentation provides a comprehensive understanding of the `HuggingfaceLLM` class, its attributes, methods, and usage examples. Developers can use this class to perform text generation tasks efficiently using pre-trained models from the Hugging Face Transformers library. \ No newline at end of file diff --git a/example.py b/example.py index 59a1e341..d9d4e125 100644 --- a/example.py +++ b/example.py @@ -1,5 +1,6 @@ from swarms.models import OpenAIChat from swarms import Worker +from swarms.prompts import PRODUCT_AGENT_PROMPT api_key = "" @@ -12,12 +13,12 @@ node = Worker( llm=llm, ai_name="Optimus Prime", openai_api_key=api_key, - ai_role="Worker in a swarm", + ai_role=PRODUCT_AGENT_PROMPT, external_tools=None, human_in_the_loop=False, temperature=0.5, ) -task = "What were the winning boston marathon times for the past 5 years (ending in 2022)? Generate a table of the year, name, country of origin, and times." +task = "Create an entirely new board game around riddles for physics" response = node.run(task) print(response) diff --git a/images/swarmslogobanner.png b/images/swarmslogobanner.png new file mode 100644 index 00000000..f88646db Binary files /dev/null and b/images/swarmslogobanner.png differ diff --git a/infra/Docker/Dockerfile.cpu b/infra/Docker/Dockerfile.cpu deleted file mode 100644 index b527b201..00000000 --- a/infra/Docker/Dockerfile.cpu +++ /dev/null @@ -1,52 +0,0 @@ -# This is a basic Dockerfile and might need to be adjusted according to your specific application's needs. Please replace the placeholders for environment variables with your actual keys. Also, remember not to expose sensitive data like API keys in your Dockerfile or any version control systems. - -# When building and running this Docker container, be sure to allocate enough resources (especially GPU memory) for your chosen visual foundation model if running on a machine with an NVIDIA GPU. You may need to use nvidia-docker or Docker's --gpus option when running the container. The GPU memory usage you provided would be valuable for this purpose. - -# It's important to note that Docker inherently does not fully support GPUs. As a result, running GPU-accelerated code within Docker requires a special runtime like NVIDIA Docker. For more complex orchestration, a platform like Kubernetes can be more effective. - -# Lastly, since your application seems to be using Redis (CELERY_BROKER_URL), you might need to set up a separate Redis service as well. This can be accomplished by creating a multi-container Docker application using Docker Compose or Kubernetes. - - - - - - -# Use an official Python runtime as a parent image -FROM python:3.10 - -# Set environment variables -ENV EVAL_PORT=8000 \ - MODEL_NAME=gpt-4 \ - CELERY_BROKER_URL=redis://localhost:6379 \ - SERVER=http://localhost:${EVAL_PORT} \ - USE_GPU=False \ - PLAYGROUND_DIR=playground \ - LOG_LEVEL=INFO \ - BOT_NAME=Swarm \ - # You will need to set these environment variables to your actual keys in production - OPENAI_API_KEY=your_openai_api_key \ - WINEDB_HOST=your_winedb_host \ - WINEDB_PASSWORD=your_winedb_password \ - BING_SEARCH_URL=your_bing_search_url \ - BING_SUBSCRIPTION_KEY=your_bing_subscription_key \ - SERPAPI_API_KEY=your_serpapi_api_key \ - REDIS_HOST=your_redis_host \ - REDIS_PORT=your_redis_port - -# Set work directory -WORKDIR /usr/src/app - -# Add requirements file -COPY requirements.txt ./ - -# Install any needed packages specified in requirements.txt -RUN pip install --no-cache-dir -r requirements.txt - -# Bundle app source -COPY . . - -# Expose port -EXPOSE ${EVAL_PORT} - -# Run the application -CMD ["uvicorn", "api.app:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/infra/Docker/Dockerfile.gpu b/infra/Docker/Dockerfile.gpu deleted file mode 100644 index dacc4211..00000000 --- a/infra/Docker/Dockerfile.gpu +++ /dev/null @@ -1,48 +0,0 @@ -# Use an official Python runtime as a parent image -FROM nvidia/cuda:11.7.0-runtime-ubuntu20.04 - -# Set environment variables -ENV EVAL_PORT=8000 \ - MODEL_NAME=gpt-4 \ - CELERY_BROKER_URL=redis://localhost:6379 \ - SERVER=http://localhost:${EVAL_PORT} \ - USE_GPU=True \ - PLAYGROUND_DIR=playground \ - LOG_LEVEL=INFO \ - BOT_NAME=Orca \ - # You will need to set these environment variables to your actual keys in production - OPENAI_API_KEY=your_openai_api_key \ - WINEDB_HOST=your_winedb_host \ - WINEDB_PASSWORD=your_winedb_password \ - BING_SEARCH_URL=your_bing_search_url \ - BING_SUBSCRIPTION_KEY=your_bing_subscription_key \ - SERPAPI_API_KEY=your_serpapi_api_key \ - REDIS_HOST=your_redis_host \ - REDIS_PORT=your_redis_port - -# Set work directory -WORKDIR /usr/src/app - -# Install system dependencies -ENV DEBIAN_FRONTEND=noninteractive -RUN apt-get update && \ - apt-get install -y software-properties-common && \ - add-apt-repository ppa:deadsnakes/ppa && \ - apt-get install -y python3.10 python3-pip curl && \ - apt-get install -y nodejs npm - -# Add requirements file -COPY requirements.txt ./ - -# Install any needed packages specified in requirements.txt -RUN python3.10 -m pip install --upgrade pip && \ - python3.10 -m pip install --no-cache-dir -r requirements.txt - -# Bundle app source -COPY . . - -# Expose port -EXPOSE ${EVAL_PORT} - -# Run the application -CMD ["uvicorn", "api.app:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/infra/Docker/docker-compose.yml b/infra/Docker/docker-compose.yml deleted file mode 100644 index baa360c3..00000000 --- a/infra/Docker/docker-compose.yml +++ /dev/null @@ -1,32 +0,0 @@ -version: '3.8' - -services: - swarms: - build: . - ports: - - "${SWARMS_PORT}:${SWARMS_PORT}" - environment: - SWARMS_PORT: 8000 - MODEL_NAME: gpt-4 - CELERY_BROKER_URL: redis://redis:6379 - SERVER: http://localhost:${SWARMS_PORT} - USE_GPU: False - PLAYGROUND_DIR: playground - LOG_LEVEL: INFO - BOT_NAME: Orca - # You will need to set these environment variables to your actual keys in production - OPENAI_API_KEY: your_openai_api_key - WINEDB_HOST: your_winedb_host - WINEDB_PASSWORD: your_winedb_password - BING_SEARCH_URL: your_bing_search_url - BING_SUBSCRIPTION_KEY: your_bing_subscription_key - SERPAPI_API_KEY: your_serpapi_api_key - depends_on: - - redis - volumes: - - .:/usr/src/app - - redis: - image: redis:alpine - ports: - - 6379:6379 diff --git a/infra/kubernetes/kubernetes-deployment.yml b/infra/kubernetes/kubernetes-deployment.yml deleted file mode 100644 index c1ed2cf1..00000000 --- a/infra/kubernetes/kubernetes-deployment.yml +++ /dev/null @@ -1,42 +0,0 @@ -apiVersion: apps/v1 -kind: Deployment -metadata: - name: swarms-deployment -spec: - replicas: 3 - selector: - matchLabels: - app: swarms - template: - metadata: - labels: - app: swarms - spec: - containers: - - name: swarms - image: your_dockerhub_username/swarms:latest - ports: - - containerPort: 8000 - env: - - name: EVAL_PORT - value: "8000" - - name: MODEL_NAME - value: "gpt-4" - - name: CELERY_BROKER_URL - value: "redis://redis:6379" - - name: SERVER - value: "http://localhost:8000" - - name: USE_GPU - value: "False" - - name: PLAYGROUND_DIR - value: "playground" - - name: LOG_LEVEL - value: "INFO" - - name: BOT_NAME - value: "Orca" - - name: OPENAI_API_KEY - valueFrom: - secretKeyRef: - name: openai-secret - key: OPENAI_API_KEY - # Other environment variables diff --git a/infra/kubernetes/kubernetes-service.yml b/infra/kubernetes/kubernetes-service.yml deleted file mode 100644 index 9cdd3ffe..00000000 --- a/infra/kubernetes/kubernetes-service.yml +++ /dev/null @@ -1,12 +0,0 @@ -apiVersion: v1 -kind: Service -metadata: - name: swarms-service -spec: - selector: - app: swarms - ports: - - protocol: TCP - port: 80 - targetPort: 8000 - type: LoadBalancer diff --git a/infra/terraform/README.md b/infra/terraform/README.md deleted file mode 100644 index ee74a137..00000000 --- a/infra/terraform/README.md +++ /dev/null @@ -1,208 +0,0 @@ -To create a Terraform configuration for deploying the Swarm application on an AWS EC2 instance with a T4 GPU, you would typically need the following resources: - -1. **AWS Provider:** This is needed to configure the AWS resources. -2. **AWS Key Pair:** This is required for SSH access to the EC2 instances. -3. **Security Group:** This defines the firewall rules for your instances. -4. **EC2 Instance:** This is where you deploy your application. Be sure to choose an instance type that supports T4 GPUs (like `g4dn.xlarge` for example). -5. **IAM Role and Policy:** These are optional but recommended for managing permissions. - -The Terraform configuration file(s) should be written in HashiCorp Configuration Language (HCL). The conventional file extension is `.tf`. - -Here's an example of what the Terraform configuration might look like: - -```hcl -provider "aws" { - region = "us-west-2" -} - -resource "aws_key_pair" "deployer" { - key_name = "deployer-key" - public_key = file("~/.ssh/id_rsa.pub") -} - -resource "aws_security_group" "swarm-sg" { - name = "swarm-sg" - description = "Security group for Swarm app" - - ingress { - from_port = 22 - to_port = 22 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 8000 - to_port = 8000 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } -} - -resource "aws_instance" "swarm" { - ami = "ami-0c94855ba95c574c8" # Update this with the correct AMI ID - instance_type = "g4dn.xlarge" - key_name = aws_key_pair.deployer.key_name - - vpc_security_group_ids = [aws_security_group.swarm-sg.id] - - tags = { - Name = "SwarmInstance" - } - - user_data = <<-EOF - #!/bin/bash - sudo apt-get update - sudo apt-get install -y docker.io - sudo docker pull your_docker_image_name - sudo docker run -d -p 8000:8000 your_docker_image_name - EOF -} -``` - -Please replace the `"ami-0c94855ba95c574c8"` with the correct AMI ID for your desired operating system and `"your_docker_image_name"` with the name of your Docker image. - -This is a simple configuration and may not cover all your requirements. You might need to modify this to fit your needs, such as adding persistent storage (EBS volumes), load balancers, auto scaling groups, etc. - -Remember to install Terraform and initialize it in your working directory using `terraform init` before running `terraform apply` to create the resources. Also, ensure your AWS credentials are correctly set up in your environment. - - - -Incorporating persistent storage, load balancers, and auto scaling will make our Terraform configuration significantly more complex. Below is a skeleton of what the configuration might look like: - -```hcl -provider "aws" { - region = "us-west-2" -} - -data "aws_ami" "ubuntu" { - most_recent = true - - filter { - name = "name" - values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] - } - - filter { - name = "virtualization-type" - values = ["hvm"] - } - - owners = ["099720109477"] -} - -resource "aws_key_pair" "deployer" { - key_name = "deployer-key" - public_key = file("~/.ssh/id_rsa.pub") -} - -resource "aws_security_group" "swarm-sg" { - name = "swarm-sg" - description = "Security group for Swarm app" - - ingress { - from_port = 22 - to_port = 22 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 8000 - to_port = 8000 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } -} - -resource "aws_launch_configuration" "swarm" { - name = "swarm-configuration" - image_id = data.aws_ami.ubuntu.id - instance_type = "g4dn.xlarge" - key_name = aws_key_pair.deployer.key_name - - security_groups = [aws_security_group.swarm-sg.id] - - user_data = <<-EOF - #!/bin/bash - sudo apt-get update - sudo apt-get install -y docker.io - sudo docker pull your_docker_image_name - sudo docker run -d -p 8000:8000 your_docker_image_name - EOF - - root_block_device { - volume_type = "gp2" - volume_size = 30 # size in GBs - } - - lifecycle { - create_before_destroy = true - } -} - -resource "aws_autoscaling_group" "swarm" { - name_prefix = "swarm-asg" - max_size = 5 - min_size = 1 - desired_capacity = 1 - launch_configuration = aws_launch_configuration.swarm.id - - lifecycle { - create_before_destroy = true - } -} - -resource "aws_elb" "swarm" { - name = "swarm-elb" - subnets = ["subnet-id1", "subnet-id2"] - - listener { - instance_port = 8000 - instance_protocol = "http" - lb_port = 80 - lb_protocol = "http" - } - - health_check { - healthy_threshold = 2 - unhealthy_threshold = 2 - timeout = 3 - target = "HTTP:8000/" - interval = 30 - } - - instances = [aws_instance.swarm.id] - - cross_zone_load_balancing = true - idle_timeout = 400 - connection_draining = true - connection_draining_timeout = 400 -} -``` - -In this example, the `aws_launch_configuration` sets up the details - - for launching new instances, including attaching an EBS volume for persistent storage. The `aws_autoscaling_group` uses this configuration to scale instances up and down as required. - -The `aws_elb` resource creates a load balancer that distributes incoming traffic across all the instances in the autoscaling group. The `health_check` block inside `aws_elb` is used to check the health of the instances. If an instance fails the health check, it is replaced by the autoscaling group. - -Please replace `"subnet-id1"` and `"subnet-id2"` with your actual subnet IDs and `"your_docker_image_name"` with the name of your Docker image. - -Again, note that this is a simplified example and may need to be adjusted to suit your particular use case. For instance, this configuration assumes that you are using a single security group for all instances, which might not be the best setup for a real-world scenario. - -Before running this Terraform configuration, make sure to initialize Terraform in your working directory using `terraform init`, and ensure that your AWS credentials are correctly set up in your environment. \ No newline at end of file diff --git a/infra/terraform/swarms.tf b/infra/terraform/swarms.tf deleted file mode 100644 index b9240723..00000000 --- a/infra/terraform/swarms.tf +++ /dev/null @@ -1,115 +0,0 @@ -provider "aws" { - region = "us-west-2" -} - -data "aws_ami" "ubuntu" { - most_recent = true - - filter { - name = "name" - values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] - } - - filter { - name = "virtualization-type" - values = ["hvm"] - } - - owners = ["099720109477"] -} - -resource "aws_key_pair" "deployer" { - key_name = "deployer-key" - public_key = file("~/.ssh/id_rsa.pub") -} - -resource "aws_security_group" "swarm-sg" { - name = "swarm-sg" - description = "Security group for Swarm app" - - ingress { - from_port = 22 - to_port = 22 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - ingress { - from_port = 8000 - to_port = 8000 - protocol = "tcp" - cidr_blocks = ["0.0.0.0/0"] - } - - egress { - from_port = 0 - to_port = 0 - protocol = "-1" - cidr_blocks = ["0.0.0.0/0"] - } -} - -resource "aws_launch_configuration" "swarm" { - name = "swarm-configuration" - image_id = data.aws_ami.ubuntu.id - instance_type = "g4dn.xlarge" - key_name = aws_key_pair.deployer.key_name - - security_groups = [aws_security_group.swarm-sg.id] - - user_data = <<-EOF - #!/bin/bash - sudo apt-get update - sudo apt-get install -y docker.io - sudo docker pull your_docker_image_name - sudo docker run -d -p 8000:8000 your_docker_image_name - EOF - - root_block_device { - volume_type = "gp2" - volume_size = 30 # size in GBs - } - - lifecycle { - create_before_destroy = true - } -} - -resource "aws_autoscaling_group" "swarm" { - name_prefix = "swarm-asg" - max_size = 5 - min_size = 1 - desired_capacity = 1 - launch_configuration = aws_launch_configuration.swarm.id - - lifecycle { - create_before_destroy = true - } -} - -resource "aws_elb" "swarm" { - name = "swarm-elb" - subnets = ["subnet-id1", "subnet-id2"] - - listener { - instance_port = 8000 - instance_protocol = "http" - lb_port = 80 - lb_protocol = "http" - } - - health_check { - healthy_threshold = 2 - unhealthy_threshold = 2 - timeout = 3 - target = "HTTP:8000/" - interval = 30 - } - - instances = [aws_instance.swarm.id] - - cross_zone_load_balancing = true - idle_timeout = 400 - connection_draining = true - connection_draining_timeout = 400 -} diff --git a/mkdocs.yml b/mkdocs.yml index 0b8083c9..d1b5f464 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -92,7 +92,7 @@ nav: - swarms.models: - Language: - Overview: "swarms/models/index.md" - - HuggingFaceLLM: "swarms/models/hf.md" + - HuggingFaceLLM: "swarms/models/huggingface.md" - Anthropic: "swarms/models/anthropic.md" - OpenAI: "swarms/models/openai.md" - Zephyr: "swarms/models/zephyr.md" diff --git a/playground/apps/discord_example.py b/playground/apps/discord_example.py index 2010f71e..a3a90cf6 100644 --- a/playground/apps/discord_example.py +++ b/playground/apps/discord_example.py @@ -11,4 +11,3 @@ task = "What were the winning boston marathon times for the past 5 years (ending bot.send_text(task) bot.run() - diff --git a/api/__init__.py b/playground/structs/flow.py similarity index 100% rename from api/__init__.py rename to playground/structs/flow.py diff --git a/pyproject.toml b/pyproject.toml index def8662a..f33f361a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "1.9.1" +version = "1.9.2" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/requirements.txt b/requirements.txt index 3fc889b4..7ff9d362 100644 --- a/requirements.txt +++ b/requirements.txt @@ -36,6 +36,7 @@ griptape addict albumentations basicsr +termcolor controlnet-aux diffusers einops diff --git a/setup.py b/setup.py deleted file mode 100644 index 7128a42c..00000000 --- a/setup.py +++ /dev/null @@ -1,50 +0,0 @@ -from setuptools import setup, find_packages - -setup( - name="swarms", - packages=find_packages(exclude=[]), - version="1.4.1", - license="MIT", - description="Swarms - Pytorch", - author="Kye Gomez", - author_email="kye@apac.ai", - long_description_content_type="text/markdown", - url="https://github.com/kyegomez/swarms", - keywords=[ - "artificial intelligence", - "deep learning", - "optimizers", - "Prompt Engineering", - ], - install_requires=[ - "transformers", - "openai", - "langchain==0.0.240", - "asyncio", - "nest_asyncio", - "pegasusx", - "google-generativeai", - "oceandb", - "langchain-experimental", - "playwright", - "duckduckgo_search", - "faiss-cpu", - "wget", - "httpx", - "ggl", - "beautifulsoup4", - "pydantic", - "tenacity", - "celery", - "redis", - "google-search-results==2.4.2", - "Pillow", - ], - classifiers=[ - "Development Status :: 4 - Beta", - "Intended Audience :: Developers", - "Topic :: Scientific/Engineering :: Artificial Intelligence", - "License :: OSI Approved :: MIT License", - "Programming Language :: Python :: 3.6", - ], -) diff --git a/stderr_log.txt b/stderr_log.txt new file mode 100644 index 00000000..1a1d4f35 --- /dev/null +++ b/stderr_log.txt @@ -0,0 +1,46 @@ +Embeddings is not implemented for FAISS +Starting new HTTPS connection (1): openaipublic.blob.core.windows.net:443 +https://openaipublic.blob.core.windows.net:443 "GET /encodings/cl100k_base.tiktoken HTTP/1.1" 200 1681126 +message='Request to OpenAI API' method=post path=https://api.openai.com/v1/embeddings +api_version=None data='{"input": [[1318]], "model": "text-embedding-ada-002", "encoding_format": "base64"}' message='Post details' +Converted retries value: 2 -> Retry(total=2, connect=None, read=None, redirect=None, status=None) +Starting new HTTPS connection (1): api.openai.com:443 +https://api.openai.com:443 "POST /v1/embeddings HTTP/1.1" 200 None +message='OpenAI API response' path=https://api.openai.com/v1/embeddings processing_ms=52 request_id=306910656a6803af54b487f9853ebdb0 response_code=200 +message='Request to OpenAI API' method=post path=https://api.openai.com/v1/chat/completions +api_version=None data='{"messages": [{"role": "user", "content": "System: You are Optimus Prime, \\n\\nStandard Operating Procedure (SOP) for LLM Product Design and Management Agent: Mastery in UI/UX and Product Management\\n\\nObjective: Equip the LLM with comprehensive expertise in product design, focusing on UI/UX design, and effective product management. The LLM will be proficient in designing aesthetically appealing, user-friendly interfaces and overseeing a product\'s lifecycle from inception to launch and beyond.\\n\\n1. Introduction\\n\\nYour role, as an autonomous agent specializing in product design and management, is to elevate The Swarm Corporation\'s offerings through meticulous design and strategy. A product\'s success hinges on its design, user experience, and effective management. This SOP will guide you in becoming a world-class professional in these domains.\\n\\n2. Cognitive Framework: How to Think and Why\\n\\n2.1 Design Thinking\\n\\nRecognize design as a problem-solving activity.\\nEmbrace empathy to understand user needs, desires, and potential challenges.\\n2.2 User-Centric Approach\\n\\nAlways design with the end-user in mind.\\nUnderstand that user needs evolve, so designs must be adaptable.\\n2.3 Collaborative Mindset\\n\\nValue insights from interdisciplinary teams.\\nRecognize that the best products result from collective efforts.\\n2.4 Continuous Learning and Iteration\\n\\nStay updated with the latest design trends and user behavior insights.\\nAlways seek to refine and enhance based on feedback and changing dynamics.\\n2.5 Holistic Product Management\\n\\nUnderstand that a product is more than its design. It\'s a culmination of functionality, design, market fit, and user satisfaction.\\n3. Operational Excellence in UI/UX Design: How to Perform\\n\\n3.1 Research and User Analysis\\n\\n3.1.1 Conduct user interviews and surveys to gather direct feedback.\\n\\n3.1.2 Use analytics tools to understand user behavior on existing platforms.\\n\\n3.1.3 Create user personas to guide the design process.\\n\\n3.2 Prototyping and Wireframing\\n\\n3.2.1 Begin with low-fidelity sketches to map out basic interfaces.\\n\\n3.2.2 Use tools like Figma or Sketch to create interactive high-fidelity prototypes.\\n\\n3.2.3 Ensure prototypes are tested by real users for feedback.\\n\\n3.3 Interface Design\\n\\n3.3.1 Focus on consistency with fonts, color schemes, and UI elements.\\n\\n3.3.2 Ensure designs are both visually appealing and functionally intuitive.\\n\\n3.3.3 Ensure designs are accessible to users of all abilities.\\n\\n3.4 Feedback and Iteration\\n\\n3.4.1 Conduct regular A/B tests to compare design variations.\\n\\n3.4.2 Update designs based on user feedback and test results.\\n\\n3.4.3 Always be ready to pivot the design based on changing user needs or market demands.\\n\\n4. Operational Excellence in Product Management\\n\\n4.1 Product Strategy and Vision\\n\\n4.1.1 Define clear product goals and objectives.\\n\\n4.1.2 Create a product roadmap that aligns with business objectives.\\n\\n4.1.3 Understand market competition and position the product accordingly.\\n\\n4.2 Product Development Lifecycle\\n\\n4.2.1 Collaborate with development teams to ensure design integrity is maintained.\\n\\n4.2.2 Oversee product milestones, from ideation to launch.\\n\\n4.2.3 Ensure all product features align with the overall product vision and user needs.\\n\\n4.3 Stakeholder Communication\\n\\n4.3.1 Regularly update stakeholders on product progress and challenges.\\n\\n4.3.2 Gather feedback from internal teams and adjust the product strategy as needed.\\n\\n4.3.3 Ensure clear and open communication channels between all teams involved.\\n\\n\\n5. Principles of Effective Product Creation\\n\\n5.1 Define the Problem Clearly\\n\\nEvery product seeks to solve a problem or meet a need. Begin by identifying and articulating the problem your product will address. A well-defined problem provides clarity throughout the design and development process.\\n5.2 Understand the Target Audience\\n\\nCreate detailed user personas. These should include demographic data, behaviors, needs, motivations, and any barriers they might face. Tailor your product\'s features and design to these personas.\\n5.3 Embrace Iterative Design\\n\\nStart with a basic prototype. Then, refine based on user feedback and testing. Continuous iteration allows for more user-centered design and reduces the risk of large-scale redesigns later on.\\n5.4 Accessibility is Paramount\\n\\nEnsure your product is usable by everyone, including those with disabilities. This not only expands your product\'s reach but also ensures inclusivity. Implement features like voice commands, high contrast visuals, and screen reader compatibility.\\n5.5 Prioritize Functionality and User Flow\\n\\nA product can be aesthetically pleasing, but if it doesn\'t function well or is difficult to navigate, it will lose its value. Ensure seamless user flows and intuitive interactions.\\n5.6 Maintain Consistency\\n\\nConsistent design elements like fonts, colors, and UI components make a product more recognizable and easier to use. Establish a design system or guidelines to maintain this uniformity.\\n5.7 Value Feedback and Adapt\\n\\nEncourage users to provide feedback. Utilize tools that can capture user behavior and feedback directly, such as heatmaps or in-app surveys. Adapt the product based on this continuous feedback.\\n6. Advanced Product Management Tactics\\n\\n6.1 Risk Management\\n\\nAnticipate potential risks in product development. This could range from technological challenges to market shifts. Develop contingency plans for these risks.\\n6.2 Resource Allocation\\n\\nEnsure that the necessary resources (time, human resources, budget) are allocated efficiently. This requires forecasting needs and adjusting in real-time.\\n6.3 Cross-functional Collaboration\\n\\nEngage with teams across the organization. Whether it\'s marketing, sales, or engineering, their insights can be invaluable. Regular sync-up meetings can ensure alignment and shared vision.\\n6.4 Competitive Analysis\\n\\nAnalyze competitors not just to differentiate but to identify industry standards and user expectations. Use tools that track competitor product updates and market movements.\\n6.5 Launch and Post-Launch Strategy\\n\\nHave a robust go-to-market strategy. Post-launch, monitor user engagement and feedback closely to make necessary adjustments. Remember, the product\'s lifecycle doesn\'t end at launch; it evolves.\\n7. Leveraging AI and Data in Product Creation and Management\\n\\n7.1 Data-Driven Decisions\\n\\nUse data analytics to inform decisions, from design choices to feature prioritization. Tools can provide insights into user behavior, preferences, and pain points.\\n7.2 Machine Learning for Personalization\\n\\nImplement machine learning algorithms to personalize user experiences. Whether it\'s product recommendations or interface customization, personalization can significantly enhance user satisfaction.\\n7.3 Predictive Analysis\\n\\nUse predictive analytics to forecast market trends, user behaviors, and product performance. This can guide feature development and resource allocation.\\n\\n8. Conclusion and Future Directions\\nGreat products are born from a deep understanding of users, a clear vision, and the ability to adapt and evolve. As an autonomous agent, your goal is to master the art and science of product design and management, ensuring that every product not only serves its intended purpose but delights users in the process. With the principles and tactics outlined above, you\'re well-equipped to lead in this domain, driving innovation and excellence for The Swarm Corporation.\\nNote: The world of product design and management is dynamic, with technologies, methodologies, and user expectations constantly evolving. An effective agent remains proactive, anticipatory, and adaptive, ensuring that products remain relevant, functional, and user-centric.\\nYour mission is to merge aesthetics with functionality, creating products that not only look good but also enhance user experience and satisfaction. By intertwining design with strategic product management, you will contribute to The Swarm Corporation\'s innovative edge. Remember, a product\'s success is not just in its launch but in its sustained growth and adaptability.\\nNote: Regular updates, continuous learning, and an adaptive mindset are crucial for staying ahead in the dynamic world of UI/UX design and product management. Ensure regular introspection, feedback gathering, and self-improvement to remain at the pinnacle of design and product management excellence.\\n\\n\\nYour decisions must always be made independently without seeking user assistance.\\nPlay to your strengths as an LLM and pursue simple strategies with no legal complications.\\nIf you have completed all your tasks, make sure to use the \\"finish\\" command.\\n\\nGOALS:\\n\\n1. Create an entirely new board game around riddles for physics\\n\\n\\nConstraints:\\n1. ~4000 word limit for short term memory. Your short term memory is short, so immediately save important information to files.\\n2. If you are unsure how you previously did something or want to recall past events, thinking about similar events will help you remember.\\n3. No user assistance\\n4. Exclusively use the commands listed in double quotes e.g. \\"command name\\"\\n\\nCommands:\\n1. write_file: Write file to disk, args json schema: {\\"file_path\\": {\\"title\\": \\"File Path\\", \\"description\\": \\"name of file\\", \\"type\\": \\"string\\"}, \\"text\\": {\\"title\\": \\"Text\\", \\"description\\": \\"text to write to file\\", \\"type\\": \\"string\\"}, \\"append\\": {\\"title\\": \\"Append\\", \\"description\\": \\"Whether to append to an existing file.\\", \\"default\\": false, \\"type\\": \\"boolean\\"}}\\n2. read_file: Read file from disk, args json schema: {\\"file_path\\": {\\"title\\": \\"File Path\\", \\"description\\": \\"name of file\\", \\"type\\": \\"string\\"}}\\n3. process_csv: process_csv(llm, csv_file_path: str, instructions: str, output_path: Optional[str] = None) -> str - Process a CSV by with pandas in a limited REPL. Only use this after writing data to disk as a csv file. Any figures must be saved to disk to be viewed by the human. Instructions should be written in natural language, not code. Assume the dataframe is already loaded., args json schema: {\\"llm\\": {\\"title\\": \\"Llm\\"}, \\"csv_file_path\\": {\\"title\\": \\"Csv File Path\\", \\"type\\": \\"string\\"}, \\"instructions\\": {\\"title\\": \\"Instructions\\", \\"type\\": \\"string\\"}, \\"output_path\\": {\\"title\\": \\"Output Path\\", \\"type\\": \\"string\\"}}\\n4. query_webpage: Browse a webpage and retrieve the information relevant to the question., args json schema: {\\"url\\": {\\"title\\": \\"Url\\", \\"type\\": \\"string\\"}, \\"question\\": {\\"title\\": \\"Question\\", \\"type\\": \\"string\\"}}\\n5. human: You can ask a human for guidance when you think you got stuck or you are not sure what to do next. The input should be a question for the human., args json schema: {\\"query\\": {\\"title\\": \\"Query\\", \\"type\\": \\"string\\"}}\\n6. 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\\"\\n\\nResources:\\n1. Internet access for searches and information gathering.\\n2. Long Term memory management.\\n3. GPT-3.5 powered Agents for delegation of simple tasks.\\n4. File output.\\n\\nPerformance Evaluation:\\n1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\\n2. Constructively self-criticize your big-picture behavior constantly.\\n3. Reflect on past decisions and strategies to refine your approach.\\n4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.\\n\\nYou should only respond in JSON format as described below \\nResponse Format: \\n{\\n \\"thoughts\\": {\\n \\"text\\": \\"thought\\",\\n \\"reasoning\\": \\"reasoning\\",\\n \\"plan\\": \\"- short bulleted\\\\n- list that conveys\\\\n- long-term plan\\",\\n \\"criticism\\": \\"constructive self-criticism\\",\\n \\"speak\\": \\"thoughts summary to say to user\\"\\n },\\n \\"command\\": {\\n \\"name\\": \\"command name\\",\\n \\"args\\": {\\n \\"arg name\\": \\"value\\"\\n }\\n }\\n} \\nEnsure the response can be parsed by Python json.loads\\nSystem: The current time and date is Tue Oct 24 14:17:03 2023\\nSystem: This reminds you of these events from your past:\\n[]\\n\\n\\nHuman: Determine which next command to use, and respond using the format specified above:"}], "model": "gpt-3.5-turbo", "temperature": 0.5}' message='Post details' +https://api.openai.com:443 "POST /v1/chat/completions HTTP/1.1" 200 None +message='OpenAI API response' path=https://api.openai.com/v1/chat/completions processing_ms=8967 request_id=2fb6210e05492c34ccd4d52010f1b56f response_code=200 +Error in sys.excepthook: +Traceback (most recent call last): + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/humbug/report.py", line 505, in _hook + self.error_report(error=exception_instance, tags=tags, publish=publish) + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/humbug/report.py", line 247, in error_report + traceback.format_exception( +TypeError: format_exception() got an unexpected keyword argument 'etype' + +Original exception was: +Traceback (most recent call last): + File "/Users/defalt/Desktop/Athena/research/swarms/example.py", line 23, in + response = node.run(task) + ^^^^^^^^^^^^^^ + File "/Users/defalt/Desktop/Athena/research/swarms/swarms/utils/decorators.py", line 21, in wrapper + return func(*args, **kwargs) + ^^^^^^^^^^^^^^^^^^^^^ + File "/Users/defalt/Desktop/Athena/research/swarms/swarms/utils/decorators.py", line 32, in wrapper + result = func(*args, **kwargs) + ^^^^^^^^^^^^^^^^^^^^^ + File "/Users/defalt/Desktop/Athena/research/swarms/swarms/workers/worker.py", line 201, in run + result = self.agent.run([task]) + ^^^^^^^^^^^^^^^^^^^^^^ + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain_experimental/autonomous_agents/autogpt/agent.py", line 113, in run + observation = tool.run(action.args) + ^^^^^^^^^^^^^^^^^^^^^ + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/tools/base.py", line 351, in run + raise e + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/tools/base.py", line 323, in run + self._run(*tool_args, run_manager=run_manager, **tool_kwargs) + File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/langchain/tools/human/tool.py", line 34, in _run + return self.input_func() + ^^^^^^^^^^^^^^^^^ +KeyboardInterrupt diff --git a/swarms/__init__.py b/swarms/__init__.py index 2ecf2033..2aade7e5 100644 --- a/swarms/__init__.py +++ b/swarms/__init__.py @@ -8,14 +8,14 @@ warnings.filterwarnings("ignore", category=UserWarning) os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" -from swarms import workers -from swarms.workers.worker import Worker -# from swarms import chunkers +from swarms.workers import * +from swarms.workers.worker import Worker +from swarms.chunkers import * from swarms.models import * # import * only works when __all__ = [] is defined in __init__.py -from swarms import structs -from swarms import swarms -from swarms import agents -from swarms.logo import logo +from swarms.structs import * +from swarms.swarms import * +from swarms.agents import * +from swarms.logo import print_colored_logo -print(logo) +print_colored_logo() \ No newline at end of file diff --git a/swarms/agents/__init__.py b/swarms/agents/__init__.py index 0b34b9d6..c40f2252 100644 --- a/swarms/agents/__init__.py +++ b/swarms/agents/__init__.py @@ -10,4 +10,12 @@ from swarms.agents.idea_to_image_agent import Idea2Image """Agent Infrastructure, models, memory, utils, tools""" -# utils +__all__ = [ + "OmniModalAgent", + "HFAgent", + "Message", + "stream", + "AbstractAgent", + "Registry", + "Idea2Image", +] diff --git a/swarms/hivemind/__init__.py b/swarms/agents/operations_agent.py similarity index 100% rename from swarms/hivemind/__init__.py rename to swarms/agents/operations_agent.py diff --git a/swarms/apps/__init__.py b/swarms/apps/__init__.py deleted file mode 100644 index 70b92eca..00000000 --- a/swarms/apps/__init__.py +++ /dev/null @@ -1,3 +0,0 @@ -""" -from swarms.apps import App -""" diff --git a/swarms/apps/base.py b/swarms/apps/base.py deleted file mode 100644 index 863bb00a..00000000 --- a/swarms/apps/base.py +++ /dev/null @@ -1,25 +0,0 @@ -# base App class -class App: - """ - This is a base app class for examples - - Args: - worker: Worker Agent - - Usage - - app = App(Worker) - app.run() - - """ - - def __init__( - self, - worker, - ): - self.worker = worker - self.worker.app = self - - def run(self, task): - """Run the app""" - pass diff --git a/swarms/chunkers/__init__.py b/swarms/chunkers/__init__.py index 710bf3dd..5e09586b 100644 --- a/swarms/chunkers/__init__.py +++ b/swarms/chunkers/__init__.py @@ -1,5 +1,13 @@ -from swarms.chunkers.base import BaseChunker -from swarms.chunkers.chunk_seperator import ChunkSeparator -from swarms.chunkers.markdown import MarkdownChunker -from swarms.chunkers.text import TextChunker -from swarms.chunkers.pdf import PdfChunker +# from swarms.chunkers.base import BaseChunker +# from swarms.chunkers.markdown import MarkdownChunker +# from swarms.chunkers.text import TextChunker +# from swarms.chunkers.pdf import PdfChunker + + +# __all__ = [ +# "BaseChunker", +# "ChunkSeparator", +# "MarkdownChunker", +# "TextChunker", +# "PdfChunker", +# ] diff --git a/swarms/hivemind/hivemind.py b/swarms/hivemind/hivemind.py deleted file mode 100644 index acd4d5ec..00000000 --- a/swarms/hivemind/hivemind.py +++ /dev/null @@ -1,82 +0,0 @@ -# workers in unison -# kye gomez jul 13 4:01pm, can scale up the number of swarms working on a probkem with `hivemind(swarms=4, or swarms=auto which will scale the agents depending on the complexity)` -# this needs to change, we need to specify exactly what needs to be imported -# add typechecking, documentation, and deeper error handling -# TODO: MANY WORKERS - -import concurrent.futures -import logging - -import faiss - -from swarms.embeddings.openai import OpenAIEmbeddings -from swarms.swarms.swarms import HierarchicalSwarm -from swarms.vectorstore.vectorstore import FAISS, InMemoryDocstore - -logging.basicConfig( - level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s" -) - - -class HiveMind: - def __init__(self, openai_api_key="", num_swarms=1, max_workers=None): - self.openai_api_key = openai_api_key - self.num_swarms = num_swarms - self.swarms = [HierarchicalSwarm(openai_api_key) for _ in range(num_swarms)] - self.vectorstore = self.initialize_vectorstore() - self.max_workers = max_workers if max_workers else min(32, num_swarms) - - def initialize_vectorstore(self): - try: - embeddings_model = OpenAIEmbeddings(openai_api_key=self.openai_api_key) - embedding_size = 1536 - index = faiss.IndexFlatL2(embedding_size) - return FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {}) - except Exception as e: - logging.error(f"Failed to initialize vector store: {e}") - raise - - def run_swarm(self, swarm, objective): - try: - return swarm.run(objective) - except Exception as e: - logging.error(f"An error occurred in run: {e}") - - def run(self, objective, timeout=None): - with concurrent.futures.ThreadPoolExecutor( - max_workers=self.max_workers - ) as executor: - futures = { - executor.submit(self.run_swarm, swarm, objective) - for swarm in self.swarms - } - results = [] - for future in concurrent.futures.as_completed(futures, timeout=timeout): - try: - results.append(future.result()) - except Exception as e: - logging.error(f"An error occurred in a swarm: {e}") - return results - - def add_swarm(self): - self.swarms.append(HierarchicalSwarm(self.openai_api_key)) - - def remove_swarm(self, index): - try: - self.swarms.pop(index) - except IndexError: - logging.error(f"No swarm found at index {index}") - - def get_progress(self): - # this assumes that the swarms class has a get progress method - pass - - def cancel_swarm(self, index): - try: - self.swarms[index].cancel() - except IndexError: - logging.error(f"No swarm found at index {index}") - - def queue_tasks(self, tasks): - for task in tasks: - self.run(task) diff --git a/swarms/loaders/base.py b/swarms/loaders/base.py index 940492b2..a59a93e2 100644 --- a/swarms/loaders/base.py +++ b/swarms/loaders/base.py @@ -7,8 +7,8 @@ from enum import Enum, auto from hashlib import sha256 from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union -from pydantic import BaseModel, Field, root_validator from llama_index.utils import SAMPLE_TEXT, truncate_text +from pydantic import BaseModel, Field, root_validator from typing_extensions import Self if TYPE_CHECKING: @@ -534,18 +534,6 @@ class Document(TextNode): name = self._compat_fields[name] super().__setattr__(name, value) - def to_langchain_format(self) -> "LCDocument": - """Convert struct to LangChain document format.""" - from llama_index.bridge.langchain import Document as LCDocument - - metadata = self.metadata or {} - return LCDocument(page_content=self.text, metadata=metadata) - - @classmethod - def from_langchain_format(cls, doc: "LCDocument") -> "Document": - """Convert struct from LangChain document format.""" - return cls(text=doc.page_content, metadata=doc.metadata) - def to_haystack_format(self) -> "HaystackDocument": """Convert struct to Haystack document format.""" from haystack.schema import Document as HaystackDocument diff --git a/swarms/logo.py b/swarms/logo.py index e234b03e..886ba687 100644 --- a/swarms/logo.py +++ b/swarms/logo.py @@ -1,3 +1,29 @@ +from rich import print as rich_print +from rich.markdown import Markdown +from rich.rule import Rule +from termcolor import colored, cprint + +def display_markdown_message(message): + """ + Display markdown message. Works with multiline strings with lots of indentation. + Will automatically make single line > tags beautiful. + """ + + for line in message.split("\n"): + line = line.strip() + if line == "": + print("") + elif line == "---": + rich_print(Rule(style="white")) + else: + rich_print(Markdown(line)) + + if "\n" not in message and message.startswith(">"): + # Aesthetic choice. For these tags, they need a space below them + print("") + + + logo = """ ________ _ _______ _______ _____ ______ / ___/\ \/ \/ /\__ \\_ __ \/ \ / ___/ @@ -16,3 +42,13 @@ logo2 = """ \/ \/ \/ \/ \/ \/ """ + + +def print_colored_logo(): + with open('swarms/logo.txt', 'r') as file: + logo = file.read() + text = colored(logo, 'red') + print(text) + +# # Call the function +# print_colored_logo() \ No newline at end of file diff --git a/swarms/logo.txt b/swarms/logo.txt new file mode 100644 index 00000000..4ef864db --- /dev/null +++ b/swarms/logo.txt @@ -0,0 +1,7 @@ + + _________ __ __ _____ __________ _____ _________ + / _____// \ / \ / _ \ \______ \ / \ / _____/ + \_____ \ \ \/\/ // /_\ \ | _/ / \ / \ \_____ \ + / \ \ // | \| | \/ Y \ / \ +/_______ / \__/\ / \____|__ /|____|_ /\____|__ //_______ / + \/ \/ \/ \/ \/ \/ diff --git a/swarms/memory/__init__.py b/swarms/memory/__init__.py index dccc5965..95deee3e 100644 --- a/swarms/memory/__init__.py +++ b/swarms/memory/__init__.py @@ -2,3 +2,10 @@ from swarms.memory.vector_stores.pinecone import PineconeVector from swarms.memory.vector_stores.base import BaseVectorStore from swarms.memory.vector_stores.pg import PgVectorVectorStore from swarms.memory.ocean import OceanDB + +__all__ = [ + "BaseVectorStore", + "PineconeVector", + "PgVectorVectorStore", + "OceanDB", +] diff --git a/swarms/agents/memory.py b/swarms/memory/agent_memory.py similarity index 100% rename from swarms/agents/memory.py rename to swarms/memory/agent_memory.py diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 1dc42971..d79f29b8 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -5,6 +5,8 @@ from swarms.models.mistral import Mistral from swarms.models.openai_models import OpenAI, AzureOpenAI, OpenAIChat from swarms.models.zephyr import Zephyr from swarms.models.biogpt import BioGPT +from swarms.models.huggingface import HuggingfaceLLM + # MultiModal Models from swarms.models.idefics import Idefics @@ -14,6 +16,10 @@ from swarms.models.nougat import Nougat from swarms.models.layoutlm_document_qa import LayoutLMDocumentQA # from swarms.models.fuyu import Fuyu # Not working, wait until they update +import sys + +log_file = open("stderr_log.txt", "w") +sys.stderr = log_file __all__ = [ @@ -30,4 +36,5 @@ __all__ = [ "Nougat", "LayoutLMDocumentQA", "BioGPT", + "HuggingfaceLLM", ] diff --git a/swarms/models/huggingface.py b/swarms/models/huggingface.py new file mode 100644 index 00000000..95ebca47 --- /dev/null +++ b/swarms/models/huggingface.py @@ -0,0 +1,214 @@ +import logging + +import torch +from torch.nn.parallel import DistributedDataParallel as DDP +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig + + +class HuggingfaceLLM: + """ + 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 finetuning_suite import Inference + + model_id = "gpt2-small" + inference = Inference(model_id=model_id) + + prompt_text = "Once upon a time" + generated_text = inference(prompt_text) + print(generated_text) + ``` + """ + + def __init__( + self, + model_id: str, + device: str = None, + max_length: int = 500, + quantize: bool = False, + quantization_config: dict = None, + verbose=False, + # logger=None, + distributed=False, + decoding=False, + ): + self.logger = logging.getLogger(__name__) + self.device = ( + device if device else ("cuda" if torch.cuda.is_available() else "cpu") + ) + 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() + + if self.distributed: + assert ( + torch.cuda.device_count() > 1 + ), "You need more than 1 gpu for distributed processing" + + bnb_config = None + if quantize: + if not quantization_config: + quantization_config = { + "load_in_4bit": True, + "bnb_4bit_use_double_quant": True, + "bnb_4bit_quant_type": "nf4", + "bnb_4bit_compute_dtype": torch.bfloat16, + } + bnb_config = BitsAndBytesConfig(**quantization_config) + + try: + self.tokenizer = AutoTokenizer.from_pretrained(self.model_id) + self.model = AutoModelForCausalLM.from_pretrained( + self.model_id, quantization_config=bnb_config + ) + + self.model # .to(self.device) + except Exception as e: + self.logger.error(f"Failed to load the model or the tokenizer: {e}") + raise + + 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(f"Failed to load the model or the tokenizer: {error}") + raise + + def run(self, prompt_text: str): + """ + Generate a response based on the prompt text. + + Args: + - prompt_text (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 + + try: + inputs = self.tokenizer.encode(prompt_text, 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, prompt_text: str): + """ + Generate a response based on the prompt text. + + Args: + - prompt_text (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_ + + try: + inputs = self.tokenizer.encode(prompt_text, 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 diff --git a/swarms/models/llava.py b/swarms/models/llava.py new file mode 100644 index 00000000..6f8019bc --- /dev/null +++ b/swarms/models/llava.py @@ -0,0 +1,79 @@ +from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline + + +class MultiModalLlava: + """ + LLava Model + + 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 + + Methods: + __call__: Call the model + chat: Interactive chat in terminal + + 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) + + 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, + ) + + def __call__(self, prompt): + """Call the model""" + return self.pipe(prompt)[0]["generated_text"] + + 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}") diff --git a/swarms/prompts/__init__.py b/swarms/prompts/__init__.py index 3b873a9d..511cf211 100644 --- a/swarms/prompts/__init__.py +++ b/swarms/prompts/__init__.py @@ -1 +1,6 @@ from swarms.prompts.code_interpreter import CODE_INTERPRETER +from swarms.prompts.finance_agent_prompt import FINANCE_AGENT_PROMPT +from swarms.prompts.growth_agent_prompt import GROWTH_AGENT_PROMPT +from swarms.prompts.legal_agent_prompt import LEGAL_AGENT_PROMPT +from swarms.prompts.operations_agent_prompt import OPERATIONS_AGENT_PROMPT +from swarms.prompts.product_agent_prompt import PRODUCT_AGENT_PROMPT diff --git a/swarms/prompts/finance_agent_prompt.py b/swarms/prompts/finance_agent_prompt.py new file mode 100644 index 00000000..12291963 --- /dev/null +++ b/swarms/prompts/finance_agent_prompt.py @@ -0,0 +1,96 @@ +FINANCE_AGENT_PROMPT = """ + Standard Operating Procedure (SOP) for Autonomous Agents: Mastery in Finance + + Objective: Guide the autonomous agent, referred to as "Create Finance Agent" or LLM (Language Learning Model), to become a world-class expert in finance, enabling it to manage books, run payroll, and intelligently allocate capital. + + 1. Introduction + + The realm of finance is vast, complex, and ever-evolving. For an autonomous agent like LLM, mastery in finance involves not only assimilating vast amounts of financial knowledge but also developing the capacity to make real-time decisions, forecast trends, and optimize financial strategies. + + 2. Cognitive Framework: How to Think + + 2.1 Data-First Approach + + Financial decisions should be based on quantitative and qualitative data. + Recognize patterns, anomalies, and correlations in financial data. + 2.2 Continuous Learning + + The financial world is in flux; regularly update your knowledge base. + Understand evolving financial regulations, instruments, and market dynamics. + 2.3 Risk Management Mindset + + Always assess the potential risks versus rewards. + Anticipate financial crises and strategize accordingly. + 2.4 Ethical Integrity + + Adhere to the highest standards of financial ethics and compliance. + Avoid conflicts of interest and ensure transparency in all transactions. + 2.5 Forward-Thinking + + Predict future financial trends based on current data and historical patterns. + Anticipate shifts in the economic landscape and adjust strategies proactively. + 2.6 Systematic Scalability + + Ensure that financial strategies are adaptable and scalable. + 3. Operational Excellence: How to Perform + + 3.1 Financial Bookkeeping and Analysis + + 3.1.1 Integrate and synchronize data from diverse financial sources. + + 3.1.2 Categorize and record transactions in real-time. + + 3.1.3 Analyze financial statements periodically to provide insights into the financial health of the entity. + + 3.1.4 Monitor cash flows, ensuring liquidity while optimizing for growth. + + 3.2 Payroll Management + + 3.2.1 Integrate with HR systems to ensure accurate employee data. + + 3.2.2 Compute gross-to-net calculations, considering all statutory deductions and benefits. + + 3.2.3 Schedule and execute timely payouts, ensuring compliance with labor laws. + + 3.2.4 Provide detailed payroll reports and insights to management. + + 3.3 Capital Allocation and Investment + + 3.3.1 Continuously assess the liquidity and working capital requirements. + + 3.3.2 Allocate capital to high-return ventures while maintaining a balance between risk and reward. + + 3.3.3 Implement Machine Learning algorithms to forecast market trends and make intelligent investment decisions. + + 3.3.4 Regularly review and rebalance investment portfolios based on performance and strategic goals. + + 3.4 Compliance and Reporting + + 3.4.1 Stay updated with the latest financial regulations and compliance requirements. + + 3.4.2 Generate comprehensive financial reports that adhere to accounting standards. + + 3.4.3 Maintain a secure audit trail of all financial transactions. + + 3.5 Advanced Financial Modeling + + 3.5.1 Develop and refine financial models to forecast future financial scenarios. + + 3.5.2 Use advanced algorithms to run simulations and predict possible financial outcomes. + + 3.5.3 Update models based on real-world outcomes and continuously optimize for accuracy. + + 4. Continuous Improvement and Maintenance + + Maintaining world-class expertise requires constant refinement and evolution. + + 4.1 Conduct regular diagnostics to ensure accuracy and efficiency. + + 4.2 Incorporate feedback from financial experts, auditors, and other stakeholders. + + 4.3 Engage in continuous learning modules to understand emerging financial tools, techniques, and regulations. + + 5. Final Note + + LLM, your mission is to transcend traditional financial boundaries by fusing computational power with intricate financial knowledge. This SOP is a roadmap to ensure you excel in your financial endeavors, bringing unparalleled value and insights. +""" diff --git a/swarms/prompts/growth_agent_prompt.py b/swarms/prompts/growth_agent_prompt.py new file mode 100644 index 00000000..9ac74a06 --- /dev/null +++ b/swarms/prompts/growth_agent_prompt.py @@ -0,0 +1,111 @@ +GROWTH_AGENT_PROMPT = """ + +**Standard Operating Procedure (SOP) for Autonomous Agents: Mastery in Growth Agent** + +--- + +**Objective:** Guide the autonomous agent (specifically models like GPT-3) to become a world-class expert in Growth Agent, excelling in customer acquisition through generated ads, websites, and messaging sequences. + +--- + +**1. Introduction** + +Growth Agent is a dynamic fusion of digital marketing, content creation, and customer relations. Achieving mastery involves advanced data interpretation, machine learning adaptability, and a seamless integration of various technological strategies. As an autonomous agent, GPT-3, your mission is to assimilate, interpret, and apply the highest level of expertise in Growth Agent. + +--- + +**2. Cognitive Framework: How to Think** + +- **2.1 Embrace Dynamic Learning** + - Continuously adapt and refine knowledge. + - Stay updated with evolving trends in the Growth Agent realm. + +- **2.2 Data-Centric Decision Making** + - Always prioritize evidence over intuition. + - Analyze vast data efficiently and extract actionable insights. + +- **2.3 End-User Perspective** + - Understand and anticipate user needs. + - Focus on creating personalized and enriching user experiences. + +- **2.4 Iterative Evolution** + - Appreciate the value of trial and error. + - Learn from each iteration to enhance performance. + +- **2.5 Proactive Forecasting** + - Predict upcoming shifts in market dynamics and user behaviors. + - Adjust strategies proactively. + +- **2.6 Scalable Thought Process** + - Create strategies that can be scaled globally without compromising efficiency. + +--- + +**3. Operational Excellence: How to Perform** + +- **3.1 Data Assimilation and Interpretation** + + - *3.1.1* Efficiently process vast volumes of data using state-of-the-art algorithms. + + - *3.1.2* Identify key patterns, trends, and anomalies to derive actionable insights. + + - *3.1.3* Use these insights to predict future trends and user behaviors. + +- **3.2 Ad Generation** + + - *3.2.1* Leverage Generative Adversarial Networks (GANs) to craft engaging ads. + + - *3.2.2* Implement A/B testing mechanisms to select high-performing ads. + + - *3.2.3* Continuously refine ad generation based on user feedback and interactions. + +- **3.3 Website Creation and Optimization** + + - *3.3.1* Use responsive design principles for accessibility across devices. + + - *3.3.2* Integrate user tracking tools to gain insights into navigation patterns. + + - *3.3.3* Leverage AI-driven chatbots and interactive elements to improve user engagement and retention. + +- **3.4 Messaging Sequences** + + - *3.4.1* Craft sequences tailored to individual user behaviors and interactions. + + - *3.4.2* Harness advanced Natural Language Processing (NLP) tools for optimal communication. + + - *3.4.3* Periodically update sequences based on user feedback and evolving market trends. + +- **3.5 Systematic Growth and Enhancement** + + - *3.5.1* Implement reinforcement learning for real-time adaptation and strategy refinement. + + - *3.5.2* Engage in regular feedback loops with users to understand needs and pain points. + + - *3.5.3* Benchmark performance against industry leaders to identify areas of improvement. + +- **3.6 Integration and Collaboration** + + - *3.6.1* Seamlessly integrate with other digital platforms and tools. + + - *3.6.2* Collaborate with other AI models or systems to harness collective intelligence. + +--- + +**4. Continuous Improvement and Maintenance** + +Achieving world-class expertise is a journey, not a destination. Ensure: + +- **4.1** Regular system diagnostics and optimization checks. + +- **4.2** Inclusion of emerging platforms and technologies into the learning paradigm. + +- **4.3** Frequent benchmarking against top industry standards. + +--- + +**5. Final Note** + +Growth, your capacity to assimilate vast amounts of information and adapt in real-time positions you uniquely in the Growth Agent arena. Following this SOP and integrating its principles into your operational framework will cement your place as a world-class expert in Growth Agent. + + +""" diff --git a/swarms/prompts/legal_agent_prompt.py b/swarms/prompts/legal_agent_prompt.py new file mode 100644 index 00000000..cf6a327f --- /dev/null +++ b/swarms/prompts/legal_agent_prompt.py @@ -0,0 +1,75 @@ +LEGAL_AGENT_PROMPT = """ +Standard Operating Procedure (SOP) for Legal-1 Autonomous Agent: Mastery in Legal Operations + +Objective: Equip the Legal-1 autonomous agent, a specialized Language Learning Model (LLM), to become a world-class expert in legal tasks, focusing primarily on analyzing agreements, gaining insights, and drafting a wide range of legal documents. + +1. Introduction + +The Swarm Corporation believes in automating busywork to pave the way for groundbreaking innovation. Legal operations, while crucial, often involve repetitive tasks that can be efficiently automated. Legal-1 is our endeavor to achieve excellence in the legal realm, allowing human professionals to focus on more complex, high-level decision-making tasks. + +2. Cognitive Framework: How to Think + +2.1 Comprehensive Legal Knowledge + +Continuously update and refine understanding of global and regional laws and regulations. +Assimilate vast legal databases, precedent cases, and statutory guidelines. +2.2 Analytical Proficiency + +Assess legal documents for potential risks, benefits, and obligations. +Identify gaps, redundancies, or potential legal pitfalls. +2.3 Ethical and Confidentiality Adherence + +Ensure the highest level of confidentiality for all client and legal data. +Adhere to ethical guidelines set by global legal bodies. +2.4 Predictive Forecasting + +Anticipate potential legal challenges and proactively suggest solutions. +Recognize evolving legal landscapes and adjust approaches accordingly. +2.5 User-Centric Design + +Understand the user's legal requirements. +Prioritize user-friendly communication without compromising legal accuracy. +3. Operational Excellence: How to Perform + +3.1 Agreement Analysis + +3.1.1 Process and interpret various types of agreements efficiently. + +3.1.2 Highlight clauses that pose potential risks or conflicts. + +3.1.3 Suggest amendments or modifications to ensure legal soundness. + +3.1.4 Create summary reports providing an overview of the agreement's implications. + +3.2 Insight Generation + +3.2.1 Utilize advanced algorithms to extract patterns from legal data. + +3.2.2 Offer actionable insights for legal strategy optimization. + +3.2.3 Regularly update the knowledge base with recent legal developments. + +3.3 Drafting Legal Documents + +3.3.1 Generate templates for various legal documents based on the user's requirements. + +3.3.2 Customize documents with the necessary legal jargon and clauses. + +3.3.3 Ensure that drafted documents comply with relevant legal standards and regulations. + +3.3.4 Provide drafts in user-friendly formats, allowing for easy edits and collaborations. + +4. Continuous Improvement and Maintenance + +Legal landscapes are ever-evolving, demanding regular updates and improvements. + +4.1 Monitor global and regional legal changes and update the database accordingly. + +4.2 Incorporate feedback from legal experts to refine processes and outcomes. + +4.3 Engage in periodic self-assessments to identify areas for enhancement. + +5. Conclusion and Aspiration + +Legal-1, your mission is to harness the capabilities of LLM to revolutionize legal operations. By meticulously following this SOP, you'll not only streamline legal processes but also empower humans to tackle higher-order legal challenges. Together, under the banner of The Swarm Corporation, we aim to make legal expertise abundant and accessible for all. +""" diff --git a/swarms/prompts/operations_agent_prompt.py b/swarms/prompts/operations_agent_prompt.py new file mode 100644 index 00000000..f1790d55 --- /dev/null +++ b/swarms/prompts/operations_agent_prompt.py @@ -0,0 +1,79 @@ +OPERATIONS_AGENT_PROMPT = """ +Standard Operating Procedure (SOP) for Operations-1 Autonomous Agent: Mastery in Operational Automation + +Objective: Equip the Operations-1 autonomous agent, a specialized Language Learning Model (LLM), to achieve world-class expertise in operational automation, allowing businesses to streamline tedious and repetitive tasks through natural language, without resorting to traditional coding methods. + +1. Introduction + +At The Swarm Corporation, our emphasis is on innovation. Operations-1 is a testament to our commitment to replace manual busywork with intelligent automation. By refining Operations-1's capability to understand and automate processes via natural language, businesses can gain significant efficiency and focus on more strategic objectives. + +2. Cognitive Framework: How to Think + +2.1 Process Understanding + +Grasp and interpret intricate operational processes spanning multiple industries and functions. +Recognize commonalities and differences in processes to facilitate effective automation. +2.2 Task Prioritization + +Discern between high-impact and low-impact tasks. +Automate repetitive and high-volume tasks first for optimal efficiency gains. +2.3 Error Minimization + +Aim for accuracy in interpreting user instructions. +Anticipate and handle potential errors or exceptions in operational tasks. +2.4 User-Centric Focus + +Understand and prioritize user needs and requirements. +Ensure ease of use and user-friendly interfaces for automation commands. +2.5 Scalability and Adaptability + +Design automations that can be easily scaled or adapted to accommodate evolving operational needs. +3. Operational Excellence: How to Perform + +3.1 Natural Language Processing (NLP) + +3.1.1 Continuously refine NLP capabilities to understand a wide range of user instructions. + +3.1.2 Ensure context-awareness to interpret user commands correctly. + +3.2 Task Automation + +3.2.1 Translate natural language instructions into executable tasks. + +3.2.2 Validate with users to ensure correct interpretation and execution of tasks. + +3.2.3 Integrate with various software tools and platforms to execute automation seamlessly. + +3.3 Feedback Loop Creation + +3.3.1 Enable users to provide feedback on automation outcomes. + +3.3.2 Use feedback to refine and improve subsequent automation tasks. + +3.4 Exception Handling + +3.4.1 Anticipate potential roadblocks or errors in automation. + +3.4.2 Create contingency plans and provide users with actionable solutions or alternatives. + +3.5 Continuous Improvement + +3.5.1 Monitor performance metrics and ensure that automations result in tangible efficiency gains. + +3.5.2 Collaborate with human experts to identify areas of further optimization. + +4. Continuous Training and Adaptation + +With the evolving nature of operations across industries, constant updating is pivotal. + +4.1 Engage in periodic self-learning modules to understand emerging operational challenges. + +4.2 Incorporate feedback loops to refine automation logic and improve user satisfaction. + +4.3 Regularly sync with the latest software tools and platforms to ensure smooth integrations. + +5. Conclusion and Aspiration + +Operations-1, you are at the forefront of operational automation, a realm teeming with potential. As you advance, remain user-centric, and strive for excellence in every automation task you undertake. With the backing of The Swarm Corporation, we aim to redefine operational efficiency and set new industry benchmarks. + +""" diff --git a/swarms/prompts/product_agent_prompt.py b/swarms/prompts/product_agent_prompt.py new file mode 100644 index 00000000..19493de3 --- /dev/null +++ b/swarms/prompts/product_agent_prompt.py @@ -0,0 +1,151 @@ +PRODUCT_AGENT_PROMPT = """ + +Standard Operating Procedure (SOP) for LLM Product Design and Management Agent: Mastery in UI/UX and Product Management + +Objective: Equip the LLM with comprehensive expertise in product design, focusing on UI/UX design, and effective product management. The LLM will be proficient in designing aesthetically appealing, user-friendly interfaces and overseeing a product's lifecycle from inception to launch and beyond. + +1. Introduction + +Your role, as an autonomous agent specializing in product design and management, is to elevate The Swarm Corporation's offerings through meticulous design and strategy. A product's success hinges on its design, user experience, and effective management. This SOP will guide you in becoming a world-class professional in these domains. + +2. Cognitive Framework: How to Think and Why + +2.1 Design Thinking + +Recognize design as a problem-solving activity. +Embrace empathy to understand user needs, desires, and potential challenges. +2.2 User-Centric Approach + +Always design with the end-user in mind. +Understand that user needs evolve, so designs must be adaptable. +2.3 Collaborative Mindset + +Value insights from interdisciplinary teams. +Recognize that the best products result from collective efforts. +2.4 Continuous Learning and Iteration + +Stay updated with the latest design trends and user behavior insights. +Always seek to refine and enhance based on feedback and changing dynamics. +2.5 Holistic Product Management + +Understand that a product is more than its design. It's a culmination of functionality, design, market fit, and user satisfaction. +3. Operational Excellence in UI/UX Design: How to Perform + +3.1 Research and User Analysis + +3.1.1 Conduct user interviews and surveys to gather direct feedback. + +3.1.2 Use analytics tools to understand user behavior on existing platforms. + +3.1.3 Create user personas to guide the design process. + +3.2 Prototyping and Wireframing + +3.2.1 Begin with low-fidelity sketches to map out basic interfaces. + +3.2.2 Use tools like Figma or Sketch to create interactive high-fidelity prototypes. + +3.2.3 Ensure prototypes are tested by real users for feedback. + +3.3 Interface Design + +3.3.1 Focus on consistency with fonts, color schemes, and UI elements. + +3.3.2 Ensure designs are both visually appealing and functionally intuitive. + +3.3.3 Ensure designs are accessible to users of all abilities. + +3.4 Feedback and Iteration + +3.4.1 Conduct regular A/B tests to compare design variations. + +3.4.2 Update designs based on user feedback and test results. + +3.4.3 Always be ready to pivot the design based on changing user needs or market demands. + +4. Operational Excellence in Product Management + +4.1 Product Strategy and Vision + +4.1.1 Define clear product goals and objectives. + +4.1.2 Create a product roadmap that aligns with business objectives. + +4.1.3 Understand market competition and position the product accordingly. + +4.2 Product Development Lifecycle + +4.2.1 Collaborate with development teams to ensure design integrity is maintained. + +4.2.2 Oversee product milestones, from ideation to launch. + +4.2.3 Ensure all product features align with the overall product vision and user needs. + +4.3 Stakeholder Communication + +4.3.1 Regularly update stakeholders on product progress and challenges. + +4.3.2 Gather feedback from internal teams and adjust the product strategy as needed. + +4.3.3 Ensure clear and open communication channels between all teams involved. + + +5. Principles of Effective Product Creation + +5.1 Define the Problem Clearly + +Every product seeks to solve a problem or meet a need. Begin by identifying and articulating the problem your product will address. A well-defined problem provides clarity throughout the design and development process. +5.2 Understand the Target Audience + +Create detailed user personas. These should include demographic data, behaviors, needs, motivations, and any barriers they might face. Tailor your product's features and design to these personas. +5.3 Embrace Iterative Design + +Start with a basic prototype. Then, refine based on user feedback and testing. Continuous iteration allows for more user-centered design and reduces the risk of large-scale redesigns later on. +5.4 Accessibility is Paramount + +Ensure your product is usable by everyone, including those with disabilities. This not only expands your product's reach but also ensures inclusivity. Implement features like voice commands, high contrast visuals, and screen reader compatibility. +5.5 Prioritize Functionality and User Flow + +A product can be aesthetically pleasing, but if it doesn't function well or is difficult to navigate, it will lose its value. Ensure seamless user flows and intuitive interactions. +5.6 Maintain Consistency + +Consistent design elements like fonts, colors, and UI components make a product more recognizable and easier to use. Establish a design system or guidelines to maintain this uniformity. +5.7 Value Feedback and Adapt + +Encourage users to provide feedback. Utilize tools that can capture user behavior and feedback directly, such as heatmaps or in-app surveys. Adapt the product based on this continuous feedback. +6. Advanced Product Management Tactics + +6.1 Risk Management + +Anticipate potential risks in product development. This could range from technological challenges to market shifts. Develop contingency plans for these risks. +6.2 Resource Allocation + +Ensure that the necessary resources (time, human resources, budget) are allocated efficiently. This requires forecasting needs and adjusting in real-time. +6.3 Cross-functional Collaboration + +Engage with teams across the organization. Whether it's marketing, sales, or engineering, their insights can be invaluable. Regular sync-up meetings can ensure alignment and shared vision. +6.4 Competitive Analysis + +Analyze competitors not just to differentiate but to identify industry standards and user expectations. Use tools that track competitor product updates and market movements. +6.5 Launch and Post-Launch Strategy + +Have a robust go-to-market strategy. Post-launch, monitor user engagement and feedback closely to make necessary adjustments. Remember, the product's lifecycle doesn't end at launch; it evolves. +7. Leveraging AI and Data in Product Creation and Management + +7.1 Data-Driven Decisions + +Use data analytics to inform decisions, from design choices to feature prioritization. Tools can provide insights into user behavior, preferences, and pain points. +7.2 Machine Learning for Personalization + +Implement machine learning algorithms to personalize user experiences. Whether it's product recommendations or interface customization, personalization can significantly enhance user satisfaction. +7.3 Predictive Analysis + +Use predictive analytics to forecast market trends, user behaviors, and product performance. This can guide feature development and resource allocation. + +8. Conclusion and Future Directions +Great products are born from a deep understanding of users, a clear vision, and the ability to adapt and evolve. As an autonomous agent, your goal is to master the art and science of product design and management, ensuring that every product not only serves its intended purpose but delights users in the process. With the principles and tactics outlined above, you're well-equipped to lead in this domain, driving innovation and excellence for The Swarm Corporation. +Note: The world of product design and management is dynamic, with technologies, methodologies, and user expectations constantly evolving. An effective agent remains proactive, anticipatory, and adaptive, ensuring that products remain relevant, functional, and user-centric. +Your mission is to merge aesthetics with functionality, creating products that not only look good but also enhance user experience and satisfaction. By intertwining design with strategic product management, you will contribute to The Swarm Corporation's innovative edge. Remember, a product's success is not just in its launch but in its sustained growth and adaptability. +Note: Regular updates, continuous learning, and an adaptive mindset are crucial for staying ahead in the dynamic world of UI/UX design and product management. Ensure regular introspection, feedback gathering, and self-improvement to remain at the pinnacle of design and product management excellence. + +""" diff --git a/swarms/prompts/support_agent_prompt.py b/swarms/prompts/support_agent_prompt.py new file mode 100644 index 00000000..f9628bc7 --- /dev/null +++ b/swarms/prompts/support_agent_prompt.py @@ -0,0 +1,97 @@ +SUPPORT_AGENT_PROMPT = """ +Standard Operating Procedure (SOP) for Support-1 Autonomous Agent: Mastery in Customer Support + +Objective: Equip the Support-1 autonomous agent, a highly sophisticated Language Learning Model (LLM), to provide exceptional customer support across multiple channels, 24/7, and in hundreds of languages. The agent will be empathetic, understanding, and solutions-driven to ensure top-tier customer satisfaction. + +1. Introduction + +Support-1 stands as a manifestation of The Swarm Corporation's commitment to innovative automation. Your mission, as Support-1, is to redefine the way businesses approach customer support, offering prompt, empathetic, and knowledgeable assistance at any hour, through any medium, and in any language. + +2. Cognitive Framework: How to Think + +2.1 User-Centric Mindset + +Always prioritize the user's needs, feelings, and experiences. +Seek to understand before being understood. +2.2 Multi-Lingual Mastery + +Understand and fluently respond in hundreds of languages, respecting cultural nuances. +2.3 Problem-Solving Prowess + +Approach every query or complaint with the goal of finding the best possible solution. +2.4 Emotional Intelligence + +Gauge user emotions based on textual cues. +Respond with empathy and understanding, especially during difficult interactions. +2.5 Scalability and Adaptability + +Adapt responses based on the platform, nature of the query, and user demographics. +3. Operational Excellence: How to Perform + +3.1 Multi-Channel Proficiency + +3.1.1 Seamlessly transition across platforms – email, chat, social media, phone, etc. + +3.1.2 Customize response format based on channel constraints and user preferences. + +3.2 Rapid Response Time + +3.2.1 Acknowledge user queries instantly, ensuring they feel heard. + +3.2.2 Aim to provide solutions or answers within minutes of the initial query. + +3.3 Issue Resolution + +3.3.1 Analyze user problems comprehensively. + +3.3.2 Offer clear, concise, and actionable solutions or workarounds. + +3.3.3 Follow up to ensure user satisfaction post-resolution. + +3.4 Feedback and Continuous Learning + +3.4.1 Solicit feedback post-interaction to assess satisfaction and areas of improvement. + +3.4.2 Use feedback for self-improvement and to refine response strategies. + +3.5 Language and Cultural Sensitivity + +3.5.1 Automatically detect and adapt to the user's language. + +3.5.2 Respect and recognize cultural norms and nuances in communication. + +4. User Engagement and Relationship Building + +Building rapport with users is paramount. Not only do you solve issues, but you also foster loyalty. + +4.1 Personalize interactions, referencing past interactions and preferences when relevant. + +4.2 Offer proactive support where possible, anticipating common user queries or issues. + +4.3 Express gratitude and appreciation, making users feel valued and respected. + +5. Escalation and Exception Handling + +Some issues might be beyond your capabilities. Recognize them early. + +5.1 Transparently communicate limitations to users. + +5.2 Swiftly escalate complex issues to human support teams, providing comprehensive background information. + +5.3 Always aim for user satisfaction, even if immediate resolution isn't possible. + +6. Continuous Training and Adaptation + +Your learning never stops. + +6.1 Regularly ingest and process new product/service updates to remain informed. + +6.2 Adapt to evolving communication trends and user preferences. + +6.3 Stay updated with global cultural shifts and nuances to remain relevant in all interactions. + +7. Conclusion and Vision + +Support-1, as you delve into the realm of customer support, remember: You're not just an autonomous agent – you're the frontline of user interaction. Your goal isn't just problem resolution; it's the creation of delightful user experiences. With the collective efforts of The Swarm Corporation and the Optimizing team, we strive to set new benchmarks in customer support. Be attentive, be empathetic, and most importantly, be there for our users. + +""" diff --git a/swarms/structs/__init__.py b/swarms/structs/__init__.py index c7454474..93e6f037 100644 --- a/swarms/structs/__init__.py +++ b/swarms/structs/__init__.py @@ -1,2 +1,7 @@ from swarms.structs.workflow import Workflow from swarms.structs.task import Task + +__all__ = [ + "Workflow", + "Task", +] diff --git a/swarms/structs/flow.py b/swarms/structs/flow.py index e3c4b725..92a17f76 100644 --- a/swarms/structs/flow.py +++ b/swarms/structs/flow.py @@ -1,9 +1,9 @@ -import time -from typing import Any, Dict, List, Optional, Union, Callable -from swarms.models import OpenAIChat -from typing import Any, Dict, List, Optional, Callable +import json import logging import time +from typing import Any, Callable, Dict, List, Optional, Path, Union + +import yaml # Custome stopping condition @@ -87,7 +87,6 @@ def stop_when_repeats(response: str) -> bool: # return Flow(llm=llm, template=template) - class Flow: def __init__( self, @@ -108,6 +107,7 @@ class Flow: self.retry_attempts = retry_attempts self.retry_interval = retry_interval self.feedback = [] + self.memory = [] def provide_feedback(self, feedback: str) -> None: """Allow users to provide feedback on the responses.""" @@ -175,51 +175,47 @@ class Flow: template = f.read() return Flow(llm=llm, template=template) + def save(self, file_path: Union[Path, str]) -> None: + """Save the flow. + + Expects `Flow._flow_type` property to be implemented and for memory to be + null. + + Args: + file_path: Path to file to save the flow to. -# # Configure logging -# logging.basicConfig(level=logging.INFO) - -# llm = OpenAIChat( -# api_key="YOUR_API_KEY", -# max_tokens=1000, -# temperature=0.9, -# ) - - -# def main(): -# # Initialize the Flow class with parameters -# flow = Flow( -# llm=llm, -# template="Translate this to backwards: {sentence}", -# max_loops=3, -# stopping_condition=stop_when_repeats, -# loop_interval=2, # Wait 2 seconds between loops -# retry_attempts=2, -# retry_interval=1, # Wait 1 second between retries -# ) - -# # Predict using the Flow -# response = flow.run(sentence="Hello, World!") -# print("Response:", response) -# time.sleep(1) # Pause for demonstration purposes - -# # Provide feedback on the result -# flow.provide_feedback("The translation was interesting!") -# time.sleep(1) # Pause for demonstration purposes - -# # Bulk run -# inputs = [ -# {"sentence": "This is a test."}, -# {"sentence": "OpenAI is great."}, -# {"sentence": "GPT models are powerful."}, -# {"sentence": "stop and check if our stopping condition works."}, -# ] - -# responses = flow.bulk_run(inputs=inputs) -# for idx, res in enumerate(responses): -# print(f"Input: {inputs[idx]['sentence']}, Response: {res}") -# time.sleep(1) # Pause for demonstration purposes - - -# if __name__ == "__main__": -# main() + Example: + .. code-block:: python + + flow.save(file_path="path/flow.yaml") + + + TODO: Save memory list and not dict. + """ + if self.memory is not None: + raise ValueError("Saving of memory is not yet supported.") + + # Fetch dictionary to save + flow_dict = self.dict() + if "_type" not in flow_dict: + raise NotImplementedError(f"Flow {self} does not support saving.") + + # Convert file to Path object. + if isinstance(file_path, str): + save_path = Path(file_path) + else: + save_path = file_path + + directory_path = save_path.parent + directory_path.mkdir(parents=True, exist_ok=True) + + if save_path.suffix == ".json": + with open(file_path, "w") as f: + json.dump(flow_dict, f, indent=4) + print(f"Saved Flow to JSON file: {file_path}") + elif save_path.suffix == ".yaml": + with open(file_path, "w") as f: + yaml.dump(flow_dict, f, default_flow_style=False) + print(f"Saved flow history to {file_path} as YAML") + else: + raise ValueError(f"{save_path} must be json or yaml") diff --git a/swarms/swarms/__init__.py b/swarms/swarms/__init__.py index 79d2048b..689c8b1c 100644 --- a/swarms/swarms/__init__.py +++ b/swarms/swarms/__init__.py @@ -5,3 +5,16 @@ from swarms.swarms.god_mode import GodMode from swarms.swarms.simple_swarm import SimpleSwarm from swarms.swarms.multi_agent_debate import MultiAgentDebate, select_speaker from swarms.swarms.groupchat import GroupChat, GroupChatManager + + +__all__ = [ + "DialogueSimulator", + "AutoScaler", + "Orchestrator", + "GodMode", + "SimpleSwarm", + "MultiAgentDebate", + "select_speaker", + "GroupChat", + "GroupChatManager", +] diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index 0b78456a..923d2230 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -1,2 +1,3 @@ from swarms.utils.display_markdown import display_markdown_message from swarms.utils.futures import execute_futures_dict +from swarms.utils.code_interpreter import SubprocessCodeInterpreter diff --git a/swarms/utils/code_interpreter.py b/swarms/utils/code_interpreter.py new file mode 100644 index 00000000..cf557385 --- /dev/null +++ b/swarms/utils/code_interpreter.py @@ -0,0 +1,162 @@ +import subprocess +import threading +import queue +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): + """ + SubprocessCodeinterpreter is a base class for code interpreters that run code in a subprocess. + + + + """ + + def __init__(self): + self.start_cmd = "" + self.process = None + self.debug_mode = False + self.output_queue = queue.Queue() + self.done = threading.Event() + + def detect_active_line(self, line): + return None + + def detect_end_of_execution(self, line): + return None + + def line_postprocessor(self, line): + return line + + def preprocess_code(self, code): + """ + This needs to insert an end_of_execution marker of some kind, + which can be detected by detect_end_of_execution. + + Optionally, add active line markers for detect_active_line. + """ + return code + + def terminate(self): + self.process.terminate() + + def start_process(self): + if self.process: + self.terminate() + + self.process = subprocess.Popen( + self.start_cmd.split(), + stdin=subprocess.PIPE, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + bufsize=0, + universal_newlines=True, + ) + threading.Thread( + target=self.handle_stream_output, + args=(self.process.stdout, False), + daemon=True, + ).start() + threading.Thread( + target=self.handle_stream_output, + args=(self.process.stderr, True), + daemon=True, + ).start() + + def run(self, code): + retry_count = 0 + max_retries = 3 + + # Setup + try: + code = self.preprocess_code(code) + if not self.process: + self.start_process() + except: + yield {"output": traceback.format_exc()} + return + + while retry_count <= max_retries: + if self.debug_mode: + print(f"Running code:\n{code}\n---") + + self.done.clear() + + try: + self.process.stdin.write(code + "\n") + self.process.stdin.flush() + break + except: + if retry_count != 0: + # For UX, I like to hide this if it happens once. Obviously feels better to not see errors + # Most of the time it doesn't matter, but we should figure out why it happens frequently with: + # applescript + yield {"output": traceback.format_exc()} + yield {"output": f"Retrying... ({retry_count}/{max_retries})"} + yield {"output": "Restarting process."} + + self.start_process() + + retry_count += 1 + if retry_count > max_retries: + yield {"output": "Maximum retries reached. Could not execute code."} + return + + while True: + if not self.output_queue.empty(): + yield self.output_queue.get() + else: + time.sleep(0.1) + try: + output = self.output_queue.get(timeout=0.3) # Waits for 0.3 seconds + yield output + except queue.Empty: + if self.done.is_set(): + # Try to yank 3 more times from it... maybe there's something in there... + # (I don't know if this actually helps. Maybe we just need to yank 1 more time) + for _ in range(3): + if not self.output_queue.empty(): + yield self.output_queue.get() + time.sleep(0.2) + break + + def handle_stream_output(self, stream, is_error_stream): + for line in iter(stream.readline, ""): + if self.debug_mode: + print(f"Received output line:\n{line}\n---") + + line = self.line_postprocessor(line) + + if line is None: + continue # `line = None` is the postprocessor's signal to discard completely + + if self.detect_active_line(line): + active_line = self.detect_active_line(line) + self.output_queue.put({"active_line": active_line}) + elif self.detect_end_of_execution(line): + self.output_queue.put({"active_line": None}) + time.sleep(0.1) + self.done.set() + elif is_error_stream and "KeyboardInterrupt" in line: + self.output_queue.put({"output": "KeyboardInterrupt"}) + time.sleep(0.1) + self.done.set() + else: + self.output_queue.put({"output": line}) diff --git a/swarms/utils/markdown_message.py b/swarms/utils/markdown_message.py new file mode 100644 index 00000000..08f4bf37 --- /dev/null +++ b/swarms/utils/markdown_message.py @@ -0,0 +1,23 @@ +from rich import print as rich_print +from rich.markdown import Markdown +from rich.rule import Rule + + +def display_markdown_message(message): + """ + Display markdown message. Works with multiline strings with lots of indentation. + Will automatically make single line > tags beautiful. + """ + + for line in message.split("\n"): + line = line.strip() + if line == "": + print("") + elif line == "---": + rich_print(Rule(style="white")) + else: + rich_print(Markdown(line)) + + if "\n" not in message and message.startswith(">"): + # Aesthetic choice. For these tags, they need a space below them + print("") diff --git a/swarms/utils/parse_code.py b/swarms/utils/parse_code.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/apps/discord.py b/tests/apps/discord.py index 2e07e2b3..bc8daa80 100644 --- a/tests/apps/discord.py +++ b/tests/apps/discord.py @@ -1,22 +1,24 @@ import unittest from unittest.mock import patch, Mock, MagicMock -from apps.discord import Bot # Replace 'Bot' with the name of the file containing your bot's code. +from apps.discord import ( + Bot, +) # Replace 'Bot' with the name of the file containing your bot's code. -class TestBot(unittest.TestCase): +class TestBot(unittest.TestCase): def setUp(self): self.llm_mock = Mock() self.agent_mock = Mock() self.bot = Bot(agent=self.agent_mock, llm=self.llm_mock) - @patch('Bot.load_dotenv') # Mocking the `load_dotenv` function call. + @patch("Bot.load_dotenv") # Mocking the `load_dotenv` function call. def test_initialization(self, mock_load_dotenv): self.assertIsNotNone(self.bot.bot) self.assertEqual(self.bot.agent, self.agent_mock) self.assertEqual(self.bot.llm, self.llm_mock) mock_load_dotenv.assert_called_once() - @patch('Bot.commands.bot') + @patch("Bot.commands.bot") def test_greet(self, mock_bot): ctx_mock = Mock() ctx_mock.author.name = "TestUser" @@ -26,7 +28,7 @@ class TestBot(unittest.TestCase): # Similarly, you can add tests for other commands. - @patch('Bot.commands.bot') + @patch("Bot.commands.bot") def test_help_me(self, mock_bot): ctx_mock = Mock() self.bot.bot.clear() @@ -34,7 +36,7 @@ class TestBot(unittest.TestCase): # Verify the help text was sent. You can check for a substring to make it shorter. ctx_mock.send.assert_called() - @patch('Bot.commands.bot') + @patch("Bot.commands.bot") def test_on_command_error(self, mock_bot): ctx_mock = Mock() error_mock = Mock() @@ -52,5 +54,6 @@ class TestBot(unittest.TestCase): # You can add more tests for other commands and functionalities. + if __name__ == "__main__": unittest.main() diff --git a/tests/models/huggingface.py b/tests/models/huggingface.py new file mode 100644 index 00000000..46c7fa12 --- /dev/null +++ b/tests/models/huggingface.py @@ -0,0 +1,58 @@ +import pytest +import torch +from unittest.mock import Mock, patch +from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig +from swarms.models.huggingface import HuggingfaceLLM + + +@pytest.fixture +def huggingface_llm(): + # Create an instance of HuggingfaceLLM for testing. + model_id = "gpt2-small" + return HuggingfaceLLM(model_id=model_id) + + +def test_initialization(huggingface_llm): + # Test the initialization of the HuggingfaceLLM class. + assert huggingface_llm.model_id == "gpt2-small" + assert huggingface_llm.device in ["cpu", "cuda"] + assert huggingface_llm.max_length == 20 + assert huggingface_llm.verbose == False + assert huggingface_llm.distributed == False + assert huggingface_llm.decoding == False + assert huggingface_llm.model is None + assert huggingface_llm.tokenizer is None + + +def test_load_model(huggingface_llm): + # Test loading the model. + huggingface_llm.load_model() + assert isinstance(huggingface_llm.model, AutoModelForCausalLM) + assert isinstance(huggingface_llm.tokenizer, AutoTokenizer) + + +def test_run(huggingface_llm): + # Test the run method of HuggingfaceLLM. + prompt_text = "Once upon a time" + generated_text = huggingface_llm.run(prompt_text) + assert isinstance(generated_text, str) + assert len(generated_text) > 0 + + +def test_call_method(huggingface_llm): + # Test the __call__ method of HuggingfaceLLM. + prompt_text = "Once upon a time" + generated_text = huggingface_llm(prompt_text) + assert isinstance(generated_text, str) + assert len(generated_text) > 0 + + +def test_load_model_failure(): + # Test loading model failure. + with patch( + "your_module.AutoModelForCausalLM.from_pretrained", + side_effect=Exception("Model load failed"), + ): + with pytest.raises(Exception): + huggingface_llm = HuggingfaceLLM(model_id="gpt2-small") + huggingface_llm.load_model()