Skip to content

Commit

Permalink
Merge branch 'kyegomez:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
elder-plinius authored Nov 26, 2023
2 parents 2e45d5b + f6a618d commit 65fcce7
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 43 deletions.
41 changes: 28 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,35 @@ Run example in Collab: <a target="_blank" href="https://colab.research.google.co

```python

import os

from dotenv import load_dotenv

# Import the OpenAIChat model and the Flow struct
from swarms.models import OpenAIChat
from swarms.structs import Flow

api_key = ""
# Load the environment variables
load_dotenv()

# Initialize the language model, this model can be swapped out with Anthropic, ETC, Huggingface Models like Mistral, ETC
# Get the API key from the environment
api_key = os.environ.get("OPENAI_API_KEY")

# Initialize the language model
llm = OpenAIChat(
openai_api_key=api_key,
temperature=0.5,
openai_api_key=api_key,
)

## Initialize the workflow
flow = Flow(
llm=llm,
max_loops=2,
dashboard=True,

)
## Initialize the workflow
flow = Flow(llm=llm, max_loops=1, dashboard=True)

# Run the workflow on a task
out = flow.run("Generate a 10,000 word blog on health and wellness.")



```

------
Expand Down Expand Up @@ -125,24 +132,32 @@ for task in workflow.tasks:
```python
from swarms.structs import Flow
from swarms.models.gpt4_vision_api import GPT4VisionAPI
from swarms.prompts.multi_modal_autonomous_instruction_prompt import (
MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
)

# Initialize the llm
llm = GPT4VisionAPI()

task = "Analyze this image of an assembly line and identify any issues such as misaligned parts, defects, or deviations from the standard assembly process. IF there is anything unsafe in the image, explain why it is unsafe and how it could be improved."
task = (
"Analyze this image of an assembly line and identify any issues such as"
" misaligned parts, defects, or deviations from the standard assembly"
" process. IF there is anything unsafe in the image, explain why it is"
" unsafe and how it could be improved."
)
img = "assembly_line.jpg"

## Initialize the workflow
flow = Flow(
llm=llm,
max_loops=1,
max_loops='auto'
sop=MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
dashboard=True,
)

# Run the flow
flow.run(task=task, img=img)



```

---
Expand Down
12 changes: 12 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import os

from dotenv import load_dotenv

# Import the OpenAIChat model and the Flow struct
from swarms.models import OpenAIChat
from swarms.structs import Flow

# Load the environment variables
load_dotenv()

# Get the API key from the environment
api_key = os.environ.get("OPENAI_API_KEY")

# Initialize the language model
llm = OpenAIChat(
temperature=0.5,
openai_api_key=api_key,
)


Expand Down
4 changes: 4 additions & 0 deletions multi_modal_auto_agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from swarms.structs import Flow
from swarms.models.gpt4_vision_api import GPT4VisionAPI
from swarms.prompts.multi_modal_autonomous_instruction_prompt import (
MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
)


llm = GPT4VisionAPI()
Expand All @@ -10,6 +13,7 @@
## Initialize the workflow
flow = Flow(
llm=llm,
sop=MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
max_loops="auto",
)

Expand Down
4 changes: 3 additions & 1 deletion playground/demos/assembly/assembly.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from swarms.structs import Flow
from swarms.models.gpt4_vision_api import GPT4VisionAPI

from swarms.prompts.multi_modal_autonomous_instruction_prompt import (
MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
)

llm = GPT4VisionAPI()

Expand Down
7 changes: 7 additions & 0 deletions playground/demos/idea_2_img/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Idea 2 img
task -> gpt4 text -> dalle3 img -> gpt4vision img + text analyze img -> dalle3 img -> loop
"""
from swarms.models.gpt4_vision_api import GPT4VisionAPI
15 changes: 15 additions & 0 deletions playground/demos/swarm_of_mma_manufacturing/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
"""
Swarm of multi modal autonomous agents for manufacturing!
---------------------------------------------------------
Health Security agent: Agent that monitors the health of working conditions: input image of factory output: health safety index 0.0 - 1.0 being the highest
Quality Control agent: Agent that monitors the quality of the product: input image of product output: quality index 0.0 - 1.0 being the highest
Productivity agent: Agent that monitors the productivity of the factory: input image of factory output: productivity index 0.0 - 1.0 being the highest
Safety agent: Agent that monitors the safety of the factory: input image of factory output: safety index 0.0 - 1.0 being the highest
Security agent: Agent that monitors the security of the factory: input image of factory output: security index 0.0 - 1.0 being the highest
Sustainability agent: Agent that monitors the sustainability of the factory: input image of factory output: sustainability index 0.0 - 1.0 being the highest
Efficiency agent: Agent that monitors the efficiency of the factory: input image of factory output: efficiency index 0.0 - 1.0 being the highest
Flow:
health security agent -> quality control agent -> productivity agent -> safety agent -> security agent -> sustainability agent -> efficiency agent
"""
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "swarms"
version = "2.4.1"
version = "2.4.2"
description = "Swarms - Pytorch"
license = "MIT"
authors = ["Kye Gomez <[email protected]>"]
Expand Down
46 changes: 27 additions & 19 deletions swarms/models/base_multimodal_model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from abc import abstractmethod
import asyncio
import base64
import concurrent.futures
Expand All @@ -7,8 +8,8 @@
from typing import List, Optional, Tuple

import requests
from ABC import abstractmethod
from PIL import Image
from termcolor import colored


class BaseMultiModalModel:
Expand Down Expand Up @@ -37,7 +38,6 @@ def __init__(
self.retries = retries
self.chat_history = []


@abstractmethod
def __call__(self, text: str, img: str):
"""Run the model"""
Expand All @@ -61,17 +61,17 @@ def get_img_from_web(self, img: str):
except requests.RequestException as error:
print(f"Error fetching image from {img} and error: {error}")
return None

def encode_img(self, img: str):
"""Encode the image to base64"""
with open(img, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")

def get_img(self, img: str):
"""Get the image from the path"""
image_pil = Image.open(img)
return image_pil

def clear_chat_history(self):
"""Clear the chat history"""
self.chat_history = []
Expand All @@ -87,11 +87,11 @@ def run_many(
Args:
tasks (List[str]): List of tasks
imgs (List[str]): List of image paths
Returns:
List[str]: List of responses
"""
# Instantiate the thread pool executor
with ThreadPoolExecutor(max_workers=self.max_workers) as executor:
Expand All @@ -101,7 +101,6 @@ def run_many(
for result in results:
print(result)


def run_batch(self, tasks_images: List[Tuple[str, str]]) -> List[str]:
"""Process a batch of tasks and images"""
with concurrent.futures.ThreadPoolExecutor() as executor:
Expand Down Expand Up @@ -133,11 +132,11 @@ async def run_batch_async_with_retries(
for task, img in tasks_images
]
return await asyncio.gather(*futures)

def unique_chat_history(self):
"""Get the unique chat history"""
return list(set(self.chat_history))

def run_with_retries(self, task: str, img: str):
"""Run the model with retries"""
for i in range(self.retries):
Expand All @@ -146,7 +145,7 @@ def run_with_retries(self, task: str, img: str):
except Exception as error:
print(f"Error with the request {error}")
continue

def run_batch_with_retries(self, tasks_images: List[Tuple[str, str]]):
"""Run the model with retries"""
for i in range(self.retries):
Expand Down Expand Up @@ -188,28 +187,37 @@ def get_generation_time(self) -> float:
if self.start_time and self.end_time:
return self.end_time - self.start_time
return 0

def get_chat_history(self):
"""Get the chat history"""
return self.chat_history

def get_unique_chat_history(self):
"""Get the unique chat history"""
return list(set(self.chat_history))

def get_chat_history_length(self):
"""Get the chat history length"""
return len(self.chat_history)

def get_unique_chat_history_length(self):
"""Get the unique chat history length"""
return len(list(set(self.chat_history)))

def get_chat_history_tokens(self):
"""Get the chat history tokens"""
return self._num_tokens()

def print_beautiful(self, content: str, color: str = "cyan"):
"""Print Beautifully with termcolor"""
content = colored(content, color)
print(content)
print(content)

def stream(self, content: str):
"""Stream the output
Args:
content (str): _description_
"""
for chunk in content:
print(chunk)
Loading

0 comments on commit 65fcce7

Please sign in to comment.