commit
358496cce2
@ -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")
|
@ -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"]
|
@ -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,
|
||||
)
|
@ -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",
|
||||
]
|
||||
)
|
@ -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.
|
After Width: | Height: | Size: 283 KiB |
@ -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"]
|
@ -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"]
|
@ -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
|
@ -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
|
@ -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
|
@ -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.
|
@ -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
|
||||
}
|
@ -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",
|
||||
],
|
||||
)
|
File diff suppressed because one or more lines are too long
@ -1,3 +0,0 @@
|
||||
"""
|
||||
from swarms.apps import App
|
||||
"""
|
@ -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
|
@ -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",
|
||||
# ]
|
||||
|
@ -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)
|
@ -0,0 +1,7 @@
|
||||
|
||||
_________ __ __ _____ __________ _____ _________
|
||||
/ _____// \ / \ / _ \ \______ \ / \ / _____/
|
||||
\_____ \ \ \/\/ // /_\ \ | _/ / \ / \ \_____ \
|
||||
/ \ \ // | \| | \/ Y \ / \
|
||||
/_______ / \__/\ / \____|__ /|____|_ /\____|__ //_______ /
|
||||
\/ \/ \/ \/ \/ \/
|
@ -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
|
@ -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}")
|
@ -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
|
||||
|
@ -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.
|
||||
"""
|
@ -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.
|
||||
|
||||
|
||||
"""
|
@ -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.
|
||||
"""
|
@ -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.
|
||||
|
||||
"""
|
@ -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.
|
||||
|
||||
"""
|
@ -1,2 +1,7 @@
|
||||
from swarms.structs.workflow import Workflow
|
||||
from swarms.structs.task import Task
|
||||
|
||||
__all__ = [
|
||||
"Workflow",
|
||||
"Task",
|
||||
]
|
||||
|
@ -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
|
||||
|
@ -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})
|
@ -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("")
|
@ -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()
|
Loading…
Reference in new issue