diff --git a/multi_modal_auto_agent.py b/multi_modal_auto_agent.py index 411e54ae..f5462847 100644 --- a/multi_modal_auto_agent.py +++ b/multi_modal_auto_agent.py @@ -10,7 +10,7 @@ img = "images/swarms.jpeg" ## Initialize the workflow flow = Flow( llm=llm, - max_loops='auto', + max_loops="auto", dashboard=True, ) diff --git a/playground/demos/positive_med/positive_med.py b/playground/demos/positive_med/positive_med.py index c14dee4b..14d522c1 100644 --- a/playground/demos/positive_med/positive_med.py +++ b/playground/demos/positive_med/positive_med.py @@ -64,8 +64,8 @@ topic_selection_task = ( " practices" ) topics = llm( - f"Your System Instructions: {TOPIC_GENERATOR_SYSTEM_PROMPT}, Your current task:" - f" {topic_selection_task}" + f"Your System Instructions: {TOPIC_GENERATOR_SYSTEM_PROMPT}, Your current" + f" task: {topic_selection_task}" ) dashboard = print( diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index 70c32d59..3525d26d 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -55,5 +55,4 @@ __all__ = [ # "Dalle3", # "DistilWhisperModel", "GPT4VisionAPI", - ] diff --git a/swarms/models/anthropic.py b/swarms/models/anthropic.py index 91073ed1..679a46d4 100644 --- a/swarms/models/anthropic.py +++ b/swarms/models/anthropic.py @@ -200,11 +200,9 @@ def build_extra_kwargs( if field_name in extra_kwargs: raise ValueError(f"Found {field_name} supplied twice.") if field_name not in all_required_field_names: - warnings.warn( - f"""WARNING! {field_name} is not default parameter. + warnings.warn(f"""WARNING! {field_name} is not default parameter. {field_name} was transferred to model_kwargs. - Please confirm that {field_name} is what you intended.""" - ) + Please confirm that {field_name} is what you intended.""") extra_kwargs[field_name] = values.pop(field_name) invalid_model_kwargs = all_required_field_names.intersection( diff --git a/swarms/models/dalle3.py b/swarms/models/dalle3.py index 49ae2468..f15f8f31 100644 --- a/swarms/models/dalle3.py +++ b/swarms/models/dalle3.py @@ -170,9 +170,12 @@ class Dalle3: # Handling exceptions and printing the errors details print( colored( + ( f"Error running Dalle3: {error} try optimizing your api" " key and or try again" ), + "red", + ) ) raise error @@ -320,8 +323,10 @@ class Dalle3: except Exception as error: print( colored( + ( f"Error running Dalle3: {error} try optimizing" " your api key and or try again" + ), "red", ) ) diff --git a/swarms/models/gpt4_vision_api.py b/swarms/models/gpt4_vision_api.py index 05736261..87c76200 100644 --- a/swarms/models/gpt4_vision_api.py +++ b/swarms/models/gpt4_vision_api.py @@ -7,6 +7,7 @@ from dotenv import load_dotenv load_dotenv() openai_api_key = os.getenv("OPENAI_API_KEY") + class GPT4VisionAPI: """ GPT-4 Vision API @@ -34,13 +35,11 @@ class GPT4VisionAPI: >>> task = "What is the color of the object?" >>> img = "https://i.imgur.com/2M2ZGwC.jpeg" >>> llm.run(task, img) - - + + """ - def __init__( - self, - openai_api_key: str = openai_api_key - ): + + def __init__(self, openai_api_key: str = openai_api_key): super().__init__() self.openai_api_key = openai_api_key @@ -52,7 +51,7 @@ class GPT4VisionAPI: # Function to handle vision tasks def run(self, task: str, img: str): """Run the model.""" - try: + try: base64_image = self.encode_image(img) headers = { "Content-Type": "application/json", @@ -68,7 +67,9 @@ class GPT4VisionAPI: { "type": "image_url", "image_url": { - "url": f"data:image/jpeg;base64,{base64_image}" + "url": ( + f"data:image/jpeg;base64,{base64_image}" + ) }, }, ], @@ -92,7 +93,7 @@ class GPT4VisionAPI: def __call__(self, task: str, img: str): """Run the model.""" - try: + try: base64_image = self.encode_image(img) headers = { "Content-Type": "application/json", @@ -108,7 +109,9 @@ class GPT4VisionAPI: { "type": "image_url", "image_url": { - "url": f"data:image/jpeg;base64,{base64_image}" + "url": ( + f"data:image/jpeg;base64,{base64_image}" + ) }, }, ], diff --git a/swarms/models/huggingface.py b/swarms/models/huggingface.py index 5c01d0b2..e5934f60 100644 --- a/swarms/models/huggingface.py +++ b/swarms/models/huggingface.py @@ -307,9 +307,12 @@ class HuggingfaceLLM: except Exception as e: print( colored( + ( "HuggingfaceLLM could not generate text because of" f" error: {e}, try optimizing your arguments" ), + "red", + ) ) raise diff --git a/swarms/models/ssd_1b.py b/swarms/models/ssd_1b.py index 9cf53c8e..06087d9a 100644 --- a/swarms/models/ssd_1b.py +++ b/swarms/models/ssd_1b.py @@ -144,8 +144,10 @@ class SSD1B: # Handling exceptions and printing the errors details print( colored( + ( f"Error running SSD1B: {error} try optimizing your api" " key and or try again" + ), "red", ) ); diff --git a/swarms/prompts/autobloggen.py b/swarms/prompts/autobloggen.py index bd71fc46..cffa9ca2 100644 --- a/swarms/prompts/autobloggen.py +++ b/swarms/prompts/autobloggen.py @@ -274,5 +274,3 @@ Check Accuracy: - Flag any bold claims that lack credible evidence for fact-checker review. """ - - diff --git a/swarms/structs/flow.py b/swarms/structs/flow.py index 2287273c..47740f73 100644 --- a/swarms/structs/flow.py +++ b/swarms/structs/flow.py @@ -489,8 +489,10 @@ class Flow: except Exception as error: print( colored( - "Error activating autonomous agent. Try optimizing your" - " parameters...", + ( + "Error activating autonomous agent. Try optimizing your" + " parameters..." + ), "red", ) ) @@ -657,7 +659,7 @@ class Flow: while attempt < self.retry_attempts: try: response = self.llm( - task ** kwargs, + task**kwargs, ) if self.interactive: print(f"AI: {response}") diff --git a/swarms/structs/sequential_workflow.py b/swarms/structs/sequential_workflow.py index 0b368d65..5facfc96 100644 --- a/swarms/structs/sequential_workflow.py +++ b/swarms/structs/sequential_workflow.py @@ -533,9 +533,11 @@ class SequentialWorkflow: except Exception as e: print( colored( - f"Error initializing the Sequential workflow: {e} try" - " optimizing your inputs like the flow class and task" - " description", + ( + f"Error initializing the Sequential workflow: {e} try" + " optimizing your inputs like the flow class and task" + " description" + ), "red", attrs=["bold", "underline"], ) diff --git a/tests/models/test_gpt4_vision_api.py b/tests/models/test_gpt4_vision_api.py index dfd03e27..a40d6f63 100644 --- a/tests/models/test_gpt4_vision_api.py +++ b/tests/models/test_gpt4_vision_api.py @@ -1,12 +1,9 @@ -import asyncio -import os -from unittest.mock import AsyncMock, Mock, mock_open, patch -from aiohttp import ClientResponseError import pytest -from dotenv import load_dotenv +from unittest.mock import mock_open, patch, Mock from requests.exceptions import RequestException - from swarms.models.gpt4_vision_api import GPT4VisionAPI +import os +from dotenv import load_dotenv load_dotenv() @@ -26,21 +23,18 @@ def test_init(vision_api): def test_encode_image(vision_api): with patch( - "builtins.open", - mock_open(read_data=b"test_image_data"), - create=True, + "builtins.open", mock_open(read_data=b"test_image_data"), create=True ): - encoded_image = vision_api.encode_image(img) + encoded_image = vision_api.encode_image("test_image.jpg") assert encoded_image == "dGVzdF9pbWFnZV9kYXRh" def test_run_success(vision_api): - expected_response = {"This is the model's response."} + expected_response = {"choices": [{"text": "This is the model's response."}]} with patch( - "requests.post", - return_value=Mock(json=lambda: expected_response), + "requests.post", return_value=Mock(json=lambda: expected_response) ) as mock_post: - result = vision_api.run("What is this?", img) + result = vision_api.run("What is this?", "test_image.jpg") mock_post.assert_called_once() assert result == "This is the model's response." @@ -50,28 +44,24 @@ def test_run_request_error(vision_api): "requests.post", side_effect=RequestException("Request Error") ) as mock_post: with pytest.raises(RequestException): - vision_api.run("What is this?", img) + vision_api.run("What is this?", "test_image.jpg") def test_run_response_error(vision_api): expected_response = {"error": "Model Error"} with patch( - "requests.post", - return_value=Mock(json=lambda: expected_response), + "requests.post", return_value=Mock(json=lambda: expected_response) ) as mock_post: with pytest.raises(RuntimeError): - vision_api.run("What is this?", img) + vision_api.run("What is this?", "test_image.jpg") def test_call(vision_api): - expected_response = { - "choices": [{"text": "This is the model's response."}] - } + expected_response = {"choices": [{"text": "This is the model's response."}]} with patch( - "requests.post", - return_value=Mock(json=lambda: expected_response), + "requests.post", return_value=Mock(json=lambda: expected_response) ) as mock_post: - result = vision_api("What is this?", img) + result = vision_api("What is this?", "test_image.jpg") mock_post.assert_called_once() assert result == "This is the model's response." @@ -92,12 +82,22 @@ def test_initialization_with_custom_key(): assert api.openai_api_key == custom_key +def test_run_successful_response(gpt_api): + task = "What is in the image?" + img_url = img + response_json = {"choices": [{"text": "Answer from GPT-4 Vision"}]} + mock_response = Mock() + mock_response.json.return_value = response_json + with patch("requests.post", return_value=mock_response) as mock_post: + result = gpt_api.run(task, img_url) + mock_post.assert_called_once() + assert result == response_json["choices"][0]["text"] + + def test_run_with_exception(gpt_api): task = "What is in the image?" img_url = img - with patch( - "requests.post", side_effect=Exception("Test Exception") - ): + with patch("requests.post", side_effect=Exception("Test Exception")): with pytest.raises(Exception): gpt_api.run(task, img_url) @@ -105,14 +105,10 @@ def test_run_with_exception(gpt_api): def test_call_method_successful_response(gpt_api): task = "What is in the image?" img_url = img - response_json = { - "choices": [{"text": "Answer from GPT-4 Vision"}] - } + response_json = {"choices": [{"text": "Answer from GPT-4 Vision"}]} mock_response = Mock() mock_response.json.return_value = response_json - with patch( - "requests.post", return_value=mock_response - ) as mock_post: + with patch("requests.post", return_value=mock_response) as mock_post: result = gpt_api(task, img_url) mock_post.assert_called_once() assert result == response_json @@ -121,132 +117,6 @@ def test_call_method_successful_response(gpt_api): def test_call_method_with_exception(gpt_api): task = "What is in the image?" img_url = img - with patch( - "requests.post", side_effect=Exception("Test Exception") - ): + 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)