diff --git a/README.md b/README.md index 2b104caf..2e3adbd4 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,32 @@ workflow.run() for task in workflow.tasks: print(f"Task: {task.description}, Result: {task.result}") +``` + +## `Multi Modal Autonomous Agents` +- Run the flow with multiple modalities useful for various real-world tasks in manufacturing, logistics, and health. + +```python +from swarms.structs import Flow +from swarms.models.gpt4_vision_api import GPT4VisionAPI + +# 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." +img = "assembly_line.jpg" + +## Initialize the workflow +flow = Flow( + llm=llm, + max_loops=1, + dashboard=True, +) + +# Run the flow +flow.run(task=task, img=img) + + ``` --- diff --git a/multi_modal_auto_agent.py b/multi_modal_auto_agent.py index 1500349c..0ed2f42f 100644 --- a/multi_modal_auto_agent.py +++ b/multi_modal_auto_agent.py @@ -11,7 +11,6 @@ img = "images/swarms.jpeg" flow = Flow( llm=llm, max_loops="auto", - ) flow.run(task=task, img=img) diff --git a/playground/demos/assembly/assembly.py b/playground/demos/assembly/assembly.py index b0f728e9..d3ebf39a 100644 --- a/playground/demos/assembly/assembly.py +++ b/playground/demos/assembly/assembly.py @@ -4,7 +4,12 @@ from swarms.models.gpt4_vision_api import GPT4VisionAPI 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 diff --git a/swarms/models/fast_vit_classes.json b/swarms/models/fast_vit_classes.json deleted file mode 100644 index 57434253..00000000 --- a/swarms/models/fast_vit_classes.json +++ /dev/null @@ -1,1000 +0,0 @@ -["tench", -"goldfish", -"great white shark", -"tiger shark", -"hammerhead shark", -"electric ray", -"stingray", -"cock", -"hen", -"ostrich", -"brambling", -"goldfinch", -"house finch", -"junco", -"indigo bunting", -"American robin", -"bulbul", -"jay", -"magpie", -"chickadee", -"American dipper", -"kite", -"bald eagle", -"vulture", -"great grey owl", -"fire salamander", -"smooth newt", -"newt", -"spotted salamander", -"axolotl", -"American bullfrog", -"tree frog", -"tailed frog", -"loggerhead sea turtle", -"leatherback sea turtle", -"mud turtle", -"terrapin", -"box turtle", -"banded gecko", -"green iguana", -"Carolina anole", -"desert grassland whiptail lizard", -"agama", -"frilled-necked lizard", -"alligator lizard", -"Gila monster", -"European green lizard", -"chameleon", -"Komodo dragon", -"Nile crocodile", -"American alligator", -"triceratops", -"worm snake", -"ring-necked snake", -"eastern hog-nosed snake", -"smooth green snake", -"kingsnake", -"garter snake", -"water snake", -"vine snake", -"night snake", -"boa constrictor", -"African rock python", -"Indian cobra", -"green mamba", -"sea snake", -"Saharan horned viper", -"eastern diamondback rattlesnake", -"sidewinder", -"trilobite", -"harvestman", -"scorpion", -"yellow garden spider", -"barn spider", -"European garden spider", -"southern black widow", -"tarantula", -"wolf spider", -"tick", -"centipede", -"black grouse", -"ptarmigan", -"ruffed grouse", -"prairie grouse", -"peacock", -"quail", -"partridge", -"grey parrot", -"macaw", -"sulphur-crested cockatoo", -"lorikeet", -"coucal", -"bee eater", -"hornbill", -"hummingbird", -"jacamar", -"toucan", -"duck", -"red-breasted merganser", -"goose", -"black swan", -"tusker", -"echidna", -"platypus", -"wallaby", -"koala", -"wombat", -"jellyfish", -"sea anemone", -"brain coral", -"flatworm", -"nematode", -"conch", -"snail", -"slug", -"sea slug", -"chiton", -"chambered nautilus", -"Dungeness crab", -"rock crab", -"fiddler crab", -"red king crab", -"American lobster", -"spiny lobster", -"crayfish", -"hermit crab", -"isopod", -"white stork", -"black stork", -"spoonbill", -"flamingo", -"little blue heron", -"great egret", -"bittern", -"crane (bird)", -"limpkin", -"common gallinule", -"American coot", -"bustard", -"ruddy turnstone", -"dunlin", -"common redshank", -"dowitcher", -"oystercatcher", -"pelican", -"king penguin", -"albatross", -"grey whale", -"killer whale", -"dugong", -"sea lion", -"Chihuahua", -"Japanese Chin", -"Maltese", -"Pekingese", -"Shih Tzu", -"King Charles Spaniel", -"Papillon", -"toy terrier", -"Rhodesian Ridgeback", -"Afghan Hound", -"Basset Hound", -"Beagle", -"Bloodhound", -"Bluetick Coonhound", -"Black and Tan Coonhound", -"Treeing Walker Coonhound", -"English foxhound", -"Redbone Coonhound", -"borzoi", -"Irish Wolfhound", -"Italian Greyhound", -"Whippet", -"Ibizan Hound", -"Norwegian Elkhound", -"Otterhound", -"Saluki", -"Scottish Deerhound", -"Weimaraner", -"Staffordshire Bull Terrier", -"American Staffordshire Terrier", -"Bedlington Terrier", -"Border Terrier", -"Kerry Blue Terrier", -"Irish Terrier", -"Norfolk Terrier", -"Norwich Terrier", -"Yorkshire Terrier", -"Wire Fox Terrier", -"Lakeland Terrier", -"Sealyham Terrier", -"Airedale Terrier", -"Cairn Terrier", -"Australian Terrier", -"Dandie Dinmont Terrier", -"Boston Terrier", -"Miniature Schnauzer", -"Giant Schnauzer", -"Standard Schnauzer", -"Scottish Terrier", -"Tibetan Terrier", -"Australian Silky Terrier", -"Soft-coated Wheaten Terrier", -"West Highland White Terrier", -"Lhasa Apso", -"Flat-Coated Retriever", -"Curly-coated Retriever", -"Golden Retriever", -"Labrador Retriever", -"Chesapeake Bay Retriever", -"German Shorthaired Pointer", -"Vizsla", -"English Setter", -"Irish Setter", -"Gordon Setter", -"Brittany Spaniel", -"Clumber Spaniel", -"English Springer Spaniel", -"Welsh Springer Spaniel", -"Cocker Spaniels", -"Sussex Spaniel", -"Irish Water Spaniel", -"Kuvasz", -"Schipperke", -"Groenendael", -"Malinois", -"Briard", -"Australian Kelpie", -"Komondor", -"Old English Sheepdog", -"Shetland Sheepdog", -"collie", -"Border Collie", -"Bouvier des Flandres", -"Rottweiler", -"German Shepherd Dog", -"Dobermann", -"Miniature Pinscher", -"Greater Swiss Mountain Dog", -"Bernese Mountain Dog", -"Appenzeller Sennenhund", -"Entlebucher Sennenhund", -"Boxer", -"Bullmastiff", -"Tibetan Mastiff", -"French Bulldog", -"Great Dane", -"St. Bernard", -"husky", -"Alaskan Malamute", -"Siberian Husky", -"Dalmatian", -"Affenpinscher", -"Basenji", -"pug", -"Leonberger", -"Newfoundland", -"Pyrenean Mountain Dog", -"Samoyed", -"Pomeranian", -"Chow Chow", -"Keeshond", -"Griffon Bruxellois", -"Pembroke Welsh Corgi", -"Cardigan Welsh Corgi", -"Toy Poodle", -"Miniature Poodle", -"Standard Poodle", -"Mexican hairless dog", -"grey wolf", -"Alaskan tundra wolf", -"red wolf", -"coyote", -"dingo", -"dhole", -"African wild dog", -"hyena", -"red fox", -"kit fox", -"Arctic fox", -"grey fox", -"tabby cat", -"tiger cat", -"Persian cat", -"Siamese cat", -"Egyptian Mau", -"cougar", -"lynx", -"leopard", -"snow leopard", -"jaguar", -"lion", -"tiger", -"cheetah", -"brown bear", -"American black bear", -"polar bear", -"sloth bear", -"mongoose", -"meerkat", -"tiger beetle", -"ladybug", -"ground beetle", -"longhorn beetle", -"leaf beetle", -"dung beetle", -"rhinoceros beetle", -"weevil", -"fly", -"bee", -"ant", -"grasshopper", -"cricket", -"stick insect", -"cockroach", -"mantis", -"cicada", -"leafhopper", -"lacewing", -"dragonfly", -"damselfly", -"red admiral", -"ringlet", -"monarch butterfly", -"small white", -"sulphur butterfly", -"gossamer-winged butterfly", -"starfish", -"sea urchin", -"sea cucumber", -"cottontail rabbit", -"hare", -"Angora rabbit", -"hamster", -"porcupine", -"fox squirrel", -"marmot", -"beaver", -"guinea pig", -"common sorrel", -"zebra", -"pig", -"wild boar", -"warthog", -"hippopotamus", -"ox", -"water buffalo", -"bison", -"ram", -"bighorn sheep", -"Alpine ibex", -"hartebeest", -"impala", -"gazelle", -"dromedary", -"llama", -"weasel", -"mink", -"European polecat", -"black-footed ferret", -"otter", -"skunk", -"badger", -"armadillo", -"three-toed sloth", -"orangutan", -"gorilla", -"chimpanzee", -"gibbon", -"siamang", -"guenon", -"patas monkey", -"baboon", -"macaque", -"langur", -"black-and-white colobus", -"proboscis monkey", -"marmoset", -"white-headed capuchin", -"howler monkey", -"titi", -"Geoffroy's spider monkey", -"common squirrel monkey", -"ring-tailed lemur", -"indri", -"Asian elephant", -"African bush elephant", -"red panda", -"giant panda", -"snoek", -"eel", -"coho salmon", -"rock beauty", -"clownfish", -"sturgeon", -"garfish", -"lionfish", -"pufferfish", -"abacus", -"abaya", -"academic gown", -"accordion", -"acoustic guitar", -"aircraft carrier", -"airliner", -"airship", -"altar", -"ambulance", -"amphibious vehicle", -"analog clock", -"apiary", -"apron", -"waste container", -"assault rifle", -"backpack", -"bakery", -"balance beam", -"balloon", -"ballpoint pen", -"Band-Aid", -"banjo", -"baluster", -"barbell", -"barber chair", -"barbershop", -"barn", -"barometer", -"barrel", -"wheelbarrow", -"baseball", -"basketball", -"bassinet", -"bassoon", -"swimming cap", -"bath towel", -"bathtub", -"station wagon", -"lighthouse", -"beaker", -"military cap", -"beer bottle", -"beer glass", -"bell-cot", -"bib", -"tandem bicycle", -"bikini", -"ring binder", -"binoculars", -"birdhouse", -"boathouse", -"bobsleigh", -"bolo tie", -"poke bonnet", -"bookcase", -"bookstore", -"bottle cap", -"bow", -"bow tie", -"brass", -"bra", -"breakwater", -"breastplate", -"broom", -"bucket", -"buckle", -"bulletproof vest", -"high-speed train", -"butcher shop", -"taxicab", -"cauldron", -"candle", -"cannon", -"canoe", -"can opener", -"cardigan", -"car mirror", -"carousel", -"tool kit", -"carton", -"car wheel", -"automated teller machine", -"cassette", -"cassette player", -"castle", -"catamaran", -"CD player", -"cello", -"mobile phone", -"chain", -"chain-link fence", -"chain mail", -"chainsaw", -"chest", -"chiffonier", -"chime", -"china cabinet", -"Christmas stocking", -"church", -"movie theater", -"cleaver", -"cliff dwelling", -"cloak", -"clogs", -"cocktail shaker", -"coffee mug", -"coffeemaker", -"coil", -"combination lock", -"computer keyboard", -"confectionery store", -"container ship", -"convertible", -"corkscrew", -"cornet", -"cowboy boot", -"cowboy hat", -"cradle", -"crane (machine)", -"crash helmet", -"crate", -"infant bed", -"Crock Pot", -"croquet ball", -"crutch", -"cuirass", -"dam", -"desk", -"desktop computer", -"rotary dial telephone", -"diaper", -"digital clock", -"digital watch", -"dining table", -"dishcloth", -"dishwasher", -"disc brake", -"dock", -"dog sled", -"dome", -"doormat", -"drilling rig", -"drum", -"drumstick", -"dumbbell", -"Dutch oven", -"electric fan", -"electric guitar", -"electric locomotive", -"entertainment center", -"envelope", -"espresso machine", -"face powder", -"feather boa", -"filing cabinet", -"fireboat", -"fire engine", -"fire screen sheet", -"flagpole", -"flute", -"folding chair", -"football helmet", -"forklift", -"fountain", -"fountain pen", -"four-poster bed", -"freight car", -"French horn", -"frying pan", -"fur coat", -"garbage truck", -"gas mask", -"gas pump", -"goblet", -"go-kart", -"golf ball", -"golf cart", -"gondola", -"gong", -"gown", -"grand piano", -"greenhouse", -"grille", -"grocery store", -"guillotine", -"barrette", -"hair spray", -"half-track", -"hammer", -"hamper", -"hair dryer", -"hand-held computer", -"handkerchief", -"hard disk drive", -"harmonica", -"harp", -"harvester", -"hatchet", -"holster", -"home theater", -"honeycomb", -"hook", -"hoop skirt", -"horizontal bar", -"horse-drawn vehicle", -"hourglass", -"iPod", -"clothes iron", -"jack-o'-lantern", -"jeans", -"jeep", -"T-shirt", -"jigsaw puzzle", -"pulled rickshaw", -"joystick", -"kimono", -"knee pad", -"knot", -"lab coat", -"ladle", -"lampshade", -"laptop computer", -"lawn mower", -"lens cap", -"paper knife", -"library", -"lifeboat", -"lighter", -"limousine", -"ocean liner", -"lipstick", -"slip-on shoe", -"lotion", -"speaker", -"loupe", -"sawmill", -"magnetic compass", -"mail bag", -"mailbox", -"tights", -"tank suit", -"manhole cover", -"maraca", -"marimba", -"mask", -"match", -"maypole", -"maze", -"measuring cup", -"medicine chest", -"megalith", -"microphone", -"microwave oven", -"military uniform", -"milk can", -"minibus", -"miniskirt", -"minivan", -"missile", -"mitten", -"mixing bowl", -"mobile home", -"Model T", -"modem", -"monastery", -"monitor", -"moped", -"mortar", -"square academic cap", -"mosque", -"mosquito net", -"scooter", -"mountain bike", -"tent", -"computer mouse", -"mousetrap", -"moving van", -"muzzle", -"nail", -"neck brace", -"necklace", -"nipple", -"notebook computer", -"obelisk", -"oboe", -"ocarina", -"odometer", -"oil filter", -"organ", -"oscilloscope", -"overskirt", -"bullock cart", -"oxygen mask", -"packet", -"paddle", -"paddle wheel", -"padlock", -"paintbrush", -"pajamas", -"palace", -"pan flute", -"paper towel", -"parachute", -"parallel bars", -"park bench", -"parking meter", -"passenger car", -"patio", -"payphone", -"pedestal", -"pencil case", -"pencil sharpener", -"perfume", -"Petri dish", -"photocopier", -"plectrum", -"Pickelhaube", -"picket fence", -"pickup truck", -"pier", -"piggy bank", -"pill bottle", -"pillow", -"ping-pong ball", -"pinwheel", -"pirate ship", -"pitcher", -"hand plane", -"planetarium", -"plastic bag", -"plate rack", -"plow", -"plunger", -"Polaroid camera", -"pole", -"police van", -"poncho", -"billiard table", -"soda bottle", -"pot", -"potter's wheel", -"power drill", -"prayer rug", -"printer", -"prison", -"projectile", -"projector", -"hockey puck", -"punching bag", -"purse", -"quill", -"quilt", -"race car", -"racket", -"radiator", -"radio", -"radio telescope", -"rain barrel", -"recreational vehicle", -"reel", -"reflex camera", -"refrigerator", -"remote control", -"restaurant", -"revolver", -"rifle", -"rocking chair", -"rotisserie", -"eraser", -"rugby ball", -"ruler", -"running shoe", -"safe", -"safety pin", -"salt shaker", -"sandal", -"sarong", -"saxophone", -"scabbard", -"weighing scale", -"school bus", -"schooner", -"scoreboard", -"CRT screen", -"screw", -"screwdriver", -"seat belt", -"sewing machine", -"shield", -"shoe store", -"shoji", -"shopping basket", -"shopping cart", -"shovel", -"shower cap", -"shower curtain", -"ski", -"ski mask", -"sleeping bag", -"slide rule", -"sliding door", -"slot machine", -"snorkel", -"snowmobile", -"snowplow", -"soap dispenser", -"soccer ball", -"sock", -"solar thermal collector", -"sombrero", -"soup bowl", -"space bar", -"space heater", -"space shuttle", -"spatula", -"motorboat", -"spider web", -"spindle", -"sports car", -"spotlight", -"stage", -"steam locomotive", -"through arch bridge", -"steel drum", -"stethoscope", -"scarf", -"stone wall", -"stopwatch", -"stove", -"strainer", -"tram", -"stretcher", -"couch", -"stupa", -"submarine", -"suit", -"sundial", -"sunglass", -"sunglasses", -"sunscreen", -"suspension bridge", -"mop", -"sweatshirt", -"swimsuit", -"swing", -"switch", -"syringe", -"table lamp", -"tank", -"tape player", -"teapot", -"teddy bear", -"television", -"tennis ball", -"thatched roof", -"front curtain", -"thimble", -"threshing machine", -"throne", -"tile roof", -"toaster", -"tobacco shop", -"toilet seat", -"torch", -"totem pole", -"tow truck", -"toy store", -"tractor", -"semi-trailer truck", -"tray", -"trench coat", -"tricycle", -"trimaran", -"tripod", -"triumphal arch", -"trolleybus", -"trombone", -"tub", -"turnstile", -"typewriter keyboard", -"umbrella", -"unicycle", -"upright piano", -"vacuum cleaner", -"vase", -"vault", -"velvet", -"vending machine", -"vestment", -"viaduct", -"violin", -"volleyball", -"waffle iron", -"wall clock", -"wallet", -"wardrobe", -"military aircraft", -"sink", -"washing machine", -"water bottle", -"water jug", -"water tower", -"whiskey jug", -"whistle", -"wig", -"window screen", -"window shade", -"Windsor tie", -"wine bottle", -"wing", -"wok", -"wooden spoon", -"wool", -"split-rail fence", -"shipwreck", -"yawl", -"yurt", -"website", -"comic book", -"crossword", -"traffic sign", -"traffic light", -"dust jacket", -"menu", -"plate", -"guacamole", -"consomme", -"hot pot", -"trifle", -"ice cream", -"ice pop", -"baguette", -"bagel", -"pretzel", -"cheeseburger", -"hot dog", -"mashed potato", -"cabbage", -"broccoli", -"cauliflower", -"zucchini", -"spaghetti squash", -"acorn squash", -"butternut squash", -"cucumber", -"artichoke", -"bell pepper", -"cardoon", -"mushroom", -"Granny Smith", -"strawberry", -"orange", -"lemon", -"fig", -"pineapple", -"banana", -"jackfruit", -"custard apple", -"pomegranate", -"hay", -"carbonara", -"chocolate syrup", -"dough", -"meatloaf", -"pizza", -"pot pie", -"burrito", -"red wine", -"espresso", -"cup", -"eggnog", -"alp", -"bubble", -"cliff", -"coral reef", -"geyser", -"lakeshore", -"promontory", -"shoal", -"seashore", -"valley", -"volcano", -"baseball player", -"bridegroom", -"scuba diver", -"rapeseed", -"daisy", -"yellow lady's slipper", -"corn", -"acorn", -"rose hip", -"horse chestnut seed", -"coral fungus", -"agaric", -"gyromitra", -"stinkhorn mushroom", -"earth star", -"hen-of-the-woods", -"bolete", -"ear of corn", -"toilet paper"] \ No newline at end of file diff --git a/swarms/models/gpt4_vision_api.py b/swarms/models/gpt4_vision_api.py index 835384ec..2a242670 100644 --- a/swarms/models/gpt4_vision_api.py +++ b/swarms/models/gpt4_vision_api.py @@ -1,8 +1,15 @@ +import asyncio import base64 +import concurrent.futures +from termcolor import colored +import json import os +from concurrent.futures import ThreadPoolExecutor +from typing import List, Tuple + +import aiohttp import requests from dotenv import load_dotenv -import concurrent.futures # Load environment variables load_dotenv() @@ -22,7 +29,7 @@ class GPT4VisionAPI: max_tokens : int The maximum number of tokens to generate. Defaults to 300. - + Methods ------- encode_image(img: str) @@ -43,16 +50,29 @@ class GPT4VisionAPI: """ - def __init__(self, openai_api_key: str = openai_api_key, max_tokens: str = 300): + def __init__( + self, + openai_api_key: str = openai_api_key, + model_name: str = "gpt-4-vision-preview", + max_workers: int = 10, + max_tokens: str = 300, + openai_proxy: str = "https://api.openai.com/v1/chat/completions", + ): super().__init__() self.openai_api_key = openai_api_key + self.model_name = model_name + self.max_workers = max_workers self.max_tokens = max_tokens + self.openai_proxy = openai_proxy def encode_image(self, img: str): """Encode image to base64.""" with open(img, "rb") as image_file: return base64.b64encode(image_file.read()).decode("utf-8") + def download_img_then_encode(self, img: str): + """Download image from URL then encode image to base64 using requests""" + # Function to handle vision tasks def run(self, task: str, img: str): """Run the model.""" @@ -63,7 +83,7 @@ class GPT4VisionAPI: "Authorization": f"Bearer {openai_api_key}", } payload = { - "model": "gpt-4-vision-preview", + "model": self.model_name, "messages": [ { "role": "user", @@ -125,7 +145,7 @@ class GPT4VisionAPI: "max_tokens": self.max_tokens, } response = requests.post( - "https://api.openai.com/v1/chat/completions", + self.openai_proxy, headers=headers, json=payload, ) @@ -138,3 +158,135 @@ class GPT4VisionAPI: raise error # Function to handle vision tasks + def run_many( + self, + tasks: List[str], + imgs: List[str], + ): + """ + Run the model on multiple tasks and images all at once using concurrent + + """ + # Instantiate the thread pool executor + with ThreadPoolExecutor(max_workers=self.max_workers) as executor: + results = executor.map(self.run, tasks, imgs) + + # Print the results for debugging + for result in results: + print(result) + + return list(results) + + async def arun( + self, + task: str, + img: str, + ): + """ + Asynchronously run the model + + Overview: + --------- + This method is used to asynchronously run the model. It is used to run the model + on a single task and image. + + Parameters: + ---------- + task : str + The task to run the model on. + img : str + The image to run the task on + + """ + try: + base64_image = self.encode_image(img) + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {openai_api_key}", + } + payload = { + "model": "gpt-4-vision-preview", + "messages": [ + { + "role": "user", + "content": [ + {"type": "text", "text": task}, + { + "type": "image_url", + "image_url": { + "url": ( + f"data:image/jpeg;base64,{base64_image}" + ) + }, + }, + ], + } + ], + "max_tokens": self.max_tokens, + } + async with aiohttp.ClientSession() as session: + async with session.post( + self.openai_proxy, headers=headers, data=json.dumps(payload) + ) as response: + out = await response.json() + content = out["choices"][0]["message"]["content"] + print(content) + except Exception as error: + print(f"Error with the request {error}") + raise error + + 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: + futures = [ + executor.submit(self.run, task, img) + for task, img in tasks_images + ] + results = [future.result() for future in futures] + return results + + async def run_batch_async( + self, tasks_images: List[Tuple[str, str]] + ) -> List[str]: + """Process a batch of tasks and images asynchronously""" + loop = asyncio.get_event_loop() + futures = [ + loop.run_in_executor(None, self.run, task, img) + for task, img in tasks_images + ] + return await asyncio.gather(*futures) + + async def run_batch_async_with_retries( + self, tasks_images: List[Tuple[str, str]] + ) -> List[str]: + """Process a batch of tasks and images asynchronously with retries""" + loop = asyncio.get_event_loop() + futures = [ + loop.run_in_executor(None, self.run_with_retries, task, img) + for task, img in tasks_images + ] + return await asyncio.gather(*futures) + + def health_check(self): + """Health check for the GPT4Vision model""" + try: + response = requests.get("https://api.openai.com/v1/engines") + return response.status_code == 200 + except requests.RequestException as error: + print(f"Health check failed: {error}") + return False + + def print_dashboard(self): + dashboard = print( + colored( + f""" + GPT4Vision Dashboard + ------------------- + Model: {self.model_name} + Max Workers: {self.max_workers} + OpenAIProxy: {self.openai_proxy} + """, + "green", + ) + ) + return dashboard diff --git a/tests/models/test_gpt4_vision_api.py b/tests/models/test_gpt4_vision_api.py index df2379a8..bca3b5f6 100644 --- a/tests/models/test_gpt4_vision_api.py +++ b/tests/models/test_gpt4_vision_api.py @@ -1,9 +1,12 @@ +import asyncio +import os +from unittest.mock import AsyncMock, Mock, mock_open, patch +from aiohttp import ClientResponseError import pytest -from unittest.mock import mock_open, patch, Mock +from dotenv import load_dotenv from requests.exceptions import RequestException + from swarms.models.gpt4_vision_api import GPT4VisionAPI -import os -from dotenv import load_dotenv load_dotenv() @@ -120,3 +123,116 @@ def test_call_method_with_exception(gpt_api): with patch("requests.post", side_effect=Exception("Test Exception")): with pytest.raises(Exception): gpt_api(task, img_url) + + +@pytest.mark.asyncio +async def test_arun_success(vision_api): + expected_response = { + "choices": [{"message": {"content": "This is the model's response."}}] + } + with patch( + "aiohttp.ClientSession.post", + new_callable=AsyncMock, + return_value=AsyncMock(json=AsyncMock(return_value=expected_response)), + ) as mock_post: + result = await vision_api.arun("What is this?", img) + mock_post.assert_called_once() + assert result == "This is the model's response." + + +@pytest.mark.asyncio +async def test_arun_request_error(vision_api): + with patch( + "aiohttp.ClientSession.post", + new_callable=AsyncMock, + side_effect=Exception("Request Error"), + ) as mock_post: + with pytest.raises(Exception): + await vision_api.arun("What is this?", img) + + +def test_run_many_success(vision_api): + expected_response = { + "choices": [{"message": {"content": "This is the model's response."}}] + } + with patch( + "requests.post", return_value=Mock(json=lambda: expected_response) + ) as mock_post: + tasks = ["What is this?", "What is that?"] + imgs = [img, img] + results = vision_api.run_many(tasks, imgs) + assert mock_post.call_count == 2 + assert results == [ + "This is the model's response.", + "This is the model's response.", + ] + + +def test_run_many_request_error(vision_api): + with patch( + "requests.post", side_effect=RequestException("Request Error") + ) as mock_post: + tasks = ["What is this?", "What is that?"] + imgs = [img, img] + with pytest.raises(RequestException): + vision_api.run_many(tasks, imgs) + + +@pytest.mark.asyncio +async def test_arun_json_decode_error(vision_api): + with patch( + "aiohttp.ClientSession.post", + new_callable=AsyncMock, + return_value=AsyncMock(json=AsyncMock(side_effect=ValueError)), + ) as mock_post: + with pytest.raises(ValueError): + await vision_api.arun("What is this?", img) + + +@pytest.mark.asyncio +async def test_arun_api_error(vision_api): + error_response = {"error": {"message": "API Error"}} + with patch( + "aiohttp.ClientSession.post", + new_callable=AsyncMock, + return_value=AsyncMock(json=AsyncMock(return_value=error_response)), + ) as mock_post: + with pytest.raises(Exception, match="API Error"): + await vision_api.arun("What is this?", img) + + +@pytest.mark.asyncio +async def test_arun_unexpected_response(vision_api): + unexpected_response = {"unexpected": "response"} + with patch( + "aiohttp.ClientSession.post", + new_callable=AsyncMock, + return_value=AsyncMock( + json=AsyncMock(return_value=unexpected_response) + ), + ) as mock_post: + with pytest.raises(Exception, match="Unexpected response"): + await vision_api.arun("What is this?", img) + + +@pytest.mark.asyncio +async def test_arun_retries(vision_api): + with patch( + "aiohttp.ClientSession.post", + new_callable=AsyncMock, + side_effect=ClientResponseError(None, None), + ) as mock_post: + with pytest.raises(ClientResponseError): + await vision_api.arun("What is this?", img) + assert mock_post.call_count == vision_api.retries + 1 + + +@pytest.mark.asyncio +async def test_arun_timeout(vision_api): + with patch( + "aiohttp.ClientSession.post", + new_callable=AsyncMock, + side_effect=asyncio.TimeoutError, + ) as mock_post: + with pytest.raises(asyncio.TimeoutError): + await vision_api.arun("What is this?", img) diff --git a/tests/models/test_revgptv4.py b/tests/models/test_revgptv4.py deleted file mode 100644 index 7a40ab30..00000000 --- a/tests/models/test_revgptv4.py +++ /dev/null @@ -1,93 +0,0 @@ -import unittest -from unittest.mock import patch -from RevChatGPTModelv4 import RevChatGPTModelv4 - - -class TestRevChatGPT(unittest.TestCase): - def setUp(self): - self.access_token = "123" - self.model = RevChatGPTModelv4(access_token=self.access_token) - - def test_run(self): - prompt = "What is the capital of France?" - self.model.start_time = 10 - self.model.end_time = 20 - response = self.model.run(prompt) - self.assertEqual(response, "The capital of France is Paris.") - self.assertEqual(self.model.start_time, 10) - self.assertEqual(self.model.end_time, 20) - - def test_generate_summary(self): - text = "Hello world. This is some text. It has multiple sentences." - summary = self.model.generate_summary(text) - self.assertEqual(summary, "") - - @patch("RevChatGPTModelv4.Chatbot.install_plugin") - def test_enable_plugin(self, mock_install_plugin): - plugin_id = "plugin123" - self.model.enable_plugin(plugin_id) - mock_install_plugin.assert_called_with(plugin_id=plugin_id) - - @patch("RevChatGPTModelv4.Chatbot.get_plugins") - def test_list_plugins(self, mock_get_plugins): - mock_get_plugins.return_value = [{"id": "123", "name": "Test Plugin"}] - plugins = self.model.list_plugins() - self.assertEqual(len(plugins), 1) - self.assertEqual(plugins[0]["id"], "123") - self.assertEqual(plugins[0]["name"], "Test Plugin") - - @patch("RevChatGPTModelv4.Chatbot.get_conversations") - def test_get_conversations(self, mock_get_conversations): - self.model.chatbot.get_conversations() - mock_get_conversations.assert_called() - - @patch("RevChatGPTModelv4.Chatbot.get_msg_history") - def test_get_msg_history(self, mock_get_msg_history): - convo_id = "123" - self.model.chatbot.get_msg_history(convo_id) - mock_get_msg_history.assert_called_with(convo_id) - - @patch("RevChatGPTModelv4.Chatbot.share_conversation") - def test_share_conversation(self, mock_share_conversation): - self.model.chatbot.share_conversation() - mock_share_conversation.assert_called() - - @patch("RevChatGPTModelv4.Chatbot.gen_title") - def test_gen_title(self, mock_gen_title): - convo_id = "123" - message_id = "456" - self.model.chatbot.gen_title(convo_id, message_id) - mock_gen_title.assert_called_with(convo_id, message_id) - - @patch("RevChatGPTModelv4.Chatbot.change_title") - def test_change_title(self, mock_change_title): - convo_id = "123" - title = "New Title" - self.model.chatbot.change_title(convo_id, title) - mock_change_title.assert_called_with(convo_id, title) - - @patch("RevChatGPTModelv4.Chatbot.delete_conversation") - def test_delete_conversation(self, mock_delete_conversation): - convo_id = "123" - self.model.chatbot.delete_conversation(convo_id) - mock_delete_conversation.assert_called_with(convo_id) - - @patch("RevChatGPTModelv4.Chatbot.clear_conversations") - def test_clear_conversations(self, mock_clear_conversations): - self.model.chatbot.clear_conversations() - mock_clear_conversations.assert_called() - - @patch("RevChatGPTModelv4.Chatbot.rollback_conversation") - def test_rollback_conversation(self, mock_rollback_conversation): - num = 2 - self.model.chatbot.rollback_conversation(num) - mock_rollback_conversation.assert_called_with(num) - - @patch("RevChatGPTModelv4.Chatbot.reset_chat") - def test_reset_chat(self, mock_reset_chat): - self.model.chatbot.reset_chat() - mock_reset_chat.assert_called() - - -if __name__ == "__main__": - unittest.main()