Skip to content

Commit

Permalink
Merge pull request #414 from vyomakesh09/master
Browse files Browse the repository at this point in the history
refactor execution scripts and workflows
  • Loading branch information
kyegomez authored Mar 19, 2024
2 parents 040f8b7 + fae4eea commit 581d655
Show file tree
Hide file tree
Showing 24 changed files with 658 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/run_examples.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Run Examples Script

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
schedule:
# Runs at 3:00 AM UTC every day
- cron: '0 3 * * *'

jobs:
run-examples:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v5
with:
python-version: '3.9'

- name: Install dependencies
run: |
pip install -r requirements.txt
# Assuming your script might also need pytest and swarms
pip install pytest
pip install swarms
- name: Make Script Executable and Run
run: |
chmod +x ./swarms/scripts/run_examples.sh
./swarms/scripts/run_examples.sh
Empty file added playground/examples/README.md
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions playground/examples/example_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
import sys

from dotenv import load_dotenv

# Import the OpenAIChat model and the Agent struct
from swarms import OpenAIChat, Agent

# Load the environment variables
load_dotenv()

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

# Initialize the language model
llm = OpenAIChat(
temperature=0.5, model_name="gpt-4", openai_api_key=api_key, max_tokens=4000
)


print(f'this is a test msg for stdout and stderr: {sys.stdout}, {sys.stderr}')

## Initialize the workflow
agent = Agent(llm=llm, max_loops=1, autosave=True, dashboard=True)

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

print(out)
13 changes: 13 additions & 0 deletions playground/examples/example_anthropic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Import necessary modules and classes
from swarms.models import Anthropic

# Initialize an instance of the Anthropic class
model = Anthropic(anthropic_api_key="")

# Using the run method
# completion_1 = model.run("What is the capital of France?")
# print(completion_1)

# Using the __call__ method
completion_2 = model("How far is the moon from the earth?", stop=["miles", "km"])
print(completion_2)
24 changes: 24 additions & 0 deletions playground/examples/example_concurrentworkflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
from dotenv import load_dotenv
from swarms import OpenAIChat, Task, ConcurrentWorkflow, Agent

# Load environment variables from .env file
load_dotenv()

# Load environment variables
llm = OpenAIChat(openai_api_key=os.getenv("OPENAI_API_KEY"))
agent = Agent(llm=llm, max_loops=1)

# Create a workflow
workflow = ConcurrentWorkflow(max_workers=5)

# Create tasks
task1 = Task(agent, "What's the weather in miami")
task2 = Task(agent, "What's the weather in new york")
task3 = Task(agent, "What's the weather in london")

# Add tasks to the workflow
workflow.add(tasks=[task1, task2, task3])

# Run the workflow
workflow.run()
14 changes: 14 additions & 0 deletions playground/examples/example_dalle3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'''from swarms.models import Dalle3
# Create an instance of the Dalle3 class with high quality
dalle3 = Dalle3(quality="high")
# Define a text prompt
task = "A high-quality image of a sunset"
# Generate a high-quality image from the text prompt
image_url = dalle3(task)
# Print the generated image URL
print(image_url)
'''
14 changes: 14 additions & 0 deletions playground/examples/example_gpt4vison.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from swarms import GPT4VisionAPI

# Initialize with default API key and custom max_tokens
api = GPT4VisionAPI(max_tokens=1000)

# Define the task and image URL
task = "Describe the scene in the image."
img = "/home/kye/.swarms/swarms/examples/Screenshot from 2024-02-20 05-55-34.png"

# Run the GPT-4 Vision model
response = api.run(task, img)

# Print the model's response
print(response)
29 changes: 29 additions & 0 deletions playground/examples/example_huggingfacellm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from swarms.models import HuggingfaceLLM
import torch

try:
inference = HuggingfaceLLM(
model_id="gpt2",
quantize=False,
verbose=True,
)

device = "cuda" if torch.cuda.is_available() else "cpu"
inference.model.to(device)

prompt_text = "Create a list of known biggest risks of structural collapse with references"
inputs = inference.tokenizer(prompt_text, return_tensors="pt").to(device)

generated_ids = inference.model.generate(
**inputs,
max_new_tokens=1000, # Adjust the length of the generation
temperature=0.7, # Adjust creativity
top_k=50, # Limits the vocabulary considered at each step
pad_token_id=inference.tokenizer.eos_token_id,
do_sample=True # Enable sampling to utilize temperature
)

generated_text = inference.tokenizer.decode(generated_ids[0], skip_special_tokens=True)
print(generated_text)
except Exception as e:
print(f"An error occurred: {e}")
33 changes: 33 additions & 0 deletions playground/examples/example_idefics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Import the idefics model from the swarms.models module
from swarms.models import Idefics

# Create an instance of the idefics model
model = Idefics()

# Define user input with an image URL and chat with the model
user_input = (
"User: What is in this image?"
" https://upload.wikimedia.org/wikipedia/commons/8/86/Id%C3%A9fix.JPG"
)
response = model.chat(user_input)
print(response)

# Define another user input with an image URL and chat with the model
user_input = (
"User: And who is that?"
" https://static.wikia.nocookie.net/asterix/images/2/25/R22b.gif/revision/latest?cb=20110815073052"
)
response = model.chat(user_input)
print(response)

# Set the checkpoint of the model to "new_checkpoint"
model.set_checkpoint("new_checkpoint")

# Set the device of the model to "cpu"
model.set_device("cpu")

# Set the maximum length of the chat to 200
model.set_max_length(200)

# Clear the chat history of the model
model.clear_chat_history()
10 changes: 10 additions & 0 deletions playground/examples/example_kosmos.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from swarms import Kosmos

# Initialize the model
model = Kosmos()

# Generate
out = model.run("Analyze the reciepts in this image", "docs.jpg")

# Print the output
print(out)
100 changes: 100 additions & 0 deletions playground/examples/example_logistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
from swarms.structs import Agent
import os
from dotenv import load_dotenv
from swarms.models import GPT4VisionAPI
from swarms.prompts.logistics import (
Health_Security_Agent_Prompt,
Quality_Control_Agent_Prompt,
Productivity_Agent_Prompt,
Safety_Agent_Prompt,
Security_Agent_Prompt,
Sustainability_Agent_Prompt,
Efficiency_Agent_Prompt,
)

# Load ENV
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")

# GPT4VisionAPI
llm = GPT4VisionAPI(openai_api_key=api_key)

# Image for analysis
factory_image = "factory_image1.jpg"

# Initialize agents with respective prompts
health_security_agent = Agent(
llm=llm,
sop=Health_Security_Agent_Prompt,
max_loops=1,
multi_modal=True,
)

# Quality control agent
quality_control_agent = Agent(
llm=llm,
sop=Quality_Control_Agent_Prompt,
max_loops=1,
multi_modal=True,
)


# Productivity Agent
productivity_agent = Agent(
llm=llm,
sop=Productivity_Agent_Prompt,
max_loops=1,
multi_modal=True,
)

# Initiailize safety agent
safety_agent = Agent(llm=llm, sop=Safety_Agent_Prompt, max_loops=1, multi_modal=True)

# Init the security agent
security_agent = Agent(
llm=llm, sop=Security_Agent_Prompt, max_loops=1, multi_modal=True
)


# Initialize sustainability agent
sustainability_agent = Agent(
llm=llm,
sop=Sustainability_Agent_Prompt,
max_loops=1,
multi_modal=True,
)


# Initialize efficincy agent
efficiency_agent = Agent(
llm=llm,
sop=Efficiency_Agent_Prompt,
max_loops=1,
multi_modal=True,
)

# Run agents with respective tasks on the same image
health_analysis = health_security_agent.run(
"Analyze the safety of this factory", factory_image
)
quality_analysis = quality_control_agent.run(
"Examine product quality in the factory", factory_image
)
productivity_analysis = productivity_agent.run(
"Evaluate factory productivity", factory_image
)
safety_analysis = safety_agent.run(
"Inspect the factory's adherence to safety standards",
factory_image,
)
security_analysis = security_agent.run(
"Assess the factory's security measures and systems",
factory_image,
)
sustainability_analysis = sustainability_agent.run(
"Examine the factory's sustainability practices", factory_image
)
efficiency_analysis = efficiency_agent.run(
"Analyze the efficiency of the factory's manufacturing process",
factory_image,
)
10 changes: 10 additions & 0 deletions playground/examples/example_mixtral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from swarms.models import Mixtral

# Initialize the Mixtral model with 4 bit and flash attention!
mixtral = Mixtral(load_in_4bit=True, use_flash_attention_2=True)

# Generate text for a simple task
generated_text = mixtral.run("Generate a creative story.")

# Print the generated text
print(generated_text)
14 changes: 14 additions & 0 deletions playground/examples/example_qwenvlmultimodal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from swarms import QwenVLMultiModal

# Instantiate the QwenVLMultiModal model
model = QwenVLMultiModal(
model_name="Qwen/Qwen-VL-Chat",
device="cuda",
quantize=True,
)

# Run the model
response = model("Hello, how are you?", "https://example.com/image.jpg")

# Print the response
print(response)
26 changes: 26 additions & 0 deletions playground/examples/example_recursiveworkflow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import os
from dotenv import load_dotenv
from swarms import OpenAIChat, Task, RecursiveWorkflow, Agent

# Load environment variables from .env file
load_dotenv()

# Load environment variables
llm = OpenAIChat(openai_api_key=os.getenv("OPENAI_API_KEY"))
agent = Agent(llm=llm, max_loops=1)

# Create a workflow
workflow = RecursiveWorkflow(stop_token="<DONE>")

# Create tasks
task1 = Task(agent, "What's the weather in miami")
task2 = Task(agent, "What's the weather in new york")
task3 = Task(agent, "What's the weather in london")

# Add tasks to the workflow
workflow.add(task1)
workflow.add(task2)
workflow.add(task3)

# Run the workflow
workflow.run()
Loading

0 comments on commit 581d655

Please sign in to comment.