diff --git a/playground/agents/worker_example.py b/playground/agents/worker_example.py index bd8d47c0..9e215e83 100644 --- a/playground/agents/worker_example.py +++ b/playground/agents/worker_example.py @@ -18,7 +18,7 @@ worker = Worker( tools=[], temperature=0.5, llm=OpenAIChat(openai_api_key=api_key), - verbose = True, + verbose=True, ) # Running the worker with a prompt diff --git a/playground/demos/fof/langchain.py b/playground/demos/fof/langchain.py index ef338520..dd6d7083 100644 --- a/playground/demos/fof/langchain.py +++ b/playground/demos/fof/langchain.py @@ -2,4 +2,3 @@ This tutorial shows you how to integrate swarms with Langchain """ - diff --git a/swarms/agents/agent_wrapper.py b/swarms/agents/agent_wrapper.py index 109048e9..738f599d 100644 --- a/swarms/agents/agent_wrapper.py +++ b/swarms/agents/agent_wrapper.py @@ -1,18 +1,19 @@ from swarms.structs.agent import Agent + def agent_wrapper(ClassToWrap): """ This function takes a class 'ClassToWrap' and returns a new class that inherits from both 'ClassToWrap' and 'Agent'. The new class overrides the '__init__' method of 'Agent' to call the '__init__' method of 'ClassToWrap'. - + Args: ClassToWrap (type): The class to be wrapped and made to inherit from 'Agent'. - + Returns: type: The new class that inherits from both 'ClassToWrap' and 'Agent'. """ - + class WrappedClass(ClassToWrap, Agent): def __init__(self, *args, **kwargs): try: @@ -21,5 +22,5 @@ def agent_wrapper(ClassToWrap): except Exception as e: print(f"Error initializing WrappedClass: {e}") raise e - - return WrappedClass \ No newline at end of file + + return WrappedClass diff --git a/swarms/agents/worker_agent.py b/swarms/agents/worker_agent.py index d3ebb1d3..d254acef 100644 --- a/swarms/agents/worker_agent.py +++ b/swarms/agents/worker_agent.py @@ -9,6 +9,7 @@ from langchain_experimental.autonomous_agents import AutoGPT from swarms.utils.decorators import error_decorator, timing_decorator + class Worker: """ The Worker class represents an autonomous agent that can perform tassks through @@ -164,7 +165,7 @@ class Worker: # @log_decorator @error_decorator @timing_decorator - def run(self, task: str = None, img = None, *args, **kwargs): + def run(self, task: str = None, img=None, *args, **kwargs): """ Run the autonomous agent on a given task. diff --git a/swarms/models/__init__.py b/swarms/models/__init__.py index d9a1e96d..fcd67dc6 100644 --- a/swarms/models/__init__.py +++ b/swarms/models/__init__.py @@ -112,5 +112,4 @@ __all__ = [ "LavaMultiModal", "QwenVLMultiModal", "CLIPQ", - ] diff --git a/swarms/models/clipq.py b/swarms/models/clipq.py index c70190fd..7e49e74a 100644 --- a/swarms/models/clipq.py +++ b/swarms/models/clipq.py @@ -10,17 +10,17 @@ from transformers import CLIPModel, CLIPProcessor class CLIPQ: """ ClipQ is an CLIQ based model that can be used to generate captions for images. - - + + Attributes: model_name (str): The name of the model to be used. query_text (str): The query text to be used for the model. - + Args: model_name (str): The name of the model to be used. query_text (str): The query text to be used for the model. - - + + """ @@ -30,13 +30,15 @@ class CLIPQ: model_name: str = "openai/clip-vit-base-patch16", query_text: str = "A photo ", *args, - **kwargs + **kwargs, ): - self.model = CLIPModel.from_pretrained(model_name, *args, **kwargs) + self.model = CLIPModel.from_pretrained( + model_name, *args, **kwargs + ) self.processor = CLIPProcessor.from_pretrained(model_name) self.query_text = query_text - def fetch_image_from_url(self, url = "https://picsum.photos/800"): + def fetch_image_from_url(self, url="https://picsum.photos/800"): """Fetches an image from the given url""" response = requests.get(url) if response.status_code != 200: @@ -48,7 +50,9 @@ class CLIPQ: """Loads an image from the given path""" return Image.open(path) - def split_image(self, image, h_splits: int = 2, v_splits: int = 2): + def split_image( + self, image, h_splits: int = 2, v_splits: int = 2 + ): """Splits the given image into h_splits x v_splits parts""" width, height = image.size w_step, h_step = width // h_splits, height // v_splits @@ -57,7 +61,12 @@ class CLIPQ: for i in range(v_splits): for j in range(h_splits): slice = image.crop( - (j * w_step, i * h_step, (j + 1) * w_step, (i + 1) * h_step) + ( + j * w_step, + i * h_step, + (j + 1) * w_step, + (i + 1) * h_step, + ) ) slices.append(slice) return slices @@ -74,10 +83,15 @@ class CLIPQ: for slice in slices: inputs = self.processor( - text=self.query_text, images=slice, return_tensors="pt", padding=True + text=self.query_text, + images=slice, + return_tensors="pt", + padding=True, ) outputs = self.model(**inputs) - vectors.append(outputs.image_embeds.squeeze().detach().numpy()) + vectors.append( + outputs.image_embeds.squeeze().detach().numpy() + ) return vectors def run_from_url( @@ -118,7 +132,9 @@ class CLIPQ: blur = GaussianBlur(kernel_size) return blur(image) - def run_from_path(self, path: str = None, h_splits: int = 2, v_splits: int = 2): + def run_from_path( + self, path: str = None, h_splits: int = 2, v_splits: int = 2 + ): """Runs the model on the image loaded from the given path""" image = self.load_image_from_path(path) return self.get_vectors(image, h_splits, v_splits) @@ -132,7 +148,9 @@ class CLIPQ: inputs_text = self.processor( text=candidate_captions, - images=inputs_image.pixel_values[0], # Fix the argument name + images=inputs_image.pixel_values[ + 0 + ], # Fix the argument name return_tensors="pt", padding=True, truncation=True, @@ -142,7 +160,8 @@ class CLIPQ: pixel_values=inputs_image.pixel_values[0] ).image_embeds text_embeds = self.model( - input_ids=inputs_text.input_ids, attention_mask=inputs_text.attention_mask + input_ids=inputs_text.input_ids, + attention_mask=inputs_text.attention_mask, ).text_embeds # Calculate similarity between image and text @@ -156,6 +175,9 @@ class CLIPQ: ): """Get the best caption for the given image""" slices = self.split_image(image, h_splits, v_splits) - captions = [self.get_captions(slice, candidate_captions) for slice in slices] + captions = [ + self.get_captions(slice, candidate_captions) + for slice in slices + ] concated_captions = "".join(captions) - return concated_captions \ No newline at end of file + return concated_captions diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index dace1ff3..3b298e3a 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -670,10 +670,15 @@ class Agent: """ ltr = self.long_term_memory.query(query) - return f"""{prompt} - ################ CONTEXT #################### + context = f""" + {prompt} + ####### Long Term Memory ################ {ltr} """ + return self.short_memory.append([f"{context}"]) + + def add_memory(self, message: str): + return self.short_memory.append([f"{message}"]) async def run_concurrent(self, tasks: List[str], **kwargs): """ diff --git a/swarms/structs/task.py b/swarms/structs/task.py index fd6a1ab8..fb89b7bf 100644 --- a/swarms/structs/task.py +++ b/swarms/structs/task.py @@ -233,7 +233,7 @@ class Task: if task.description is not None else "" ) - + result = ( task.result if task.result is not None else "" ) diff --git a/swarms/telemetry/sys_info.py b/swarms/telemetry/sys_info.py index d2841585..a4857e11 100644 --- a/swarms/telemetry/sys_info.py +++ b/swarms/telemetry/sys_info.py @@ -89,7 +89,6 @@ def get_package_mismatches(file_path="pyproject.toml"): return "\n" + "\n".join(mismatches) - def system_info(): swarms_verison = get_swarms_verison() return { @@ -100,4 +99,3 @@ def system_info(): "CPU Info": get_cpu_info(), "RAM Info": get_ram_info(), } - diff --git a/swarms/telemetry/user_utils.py b/swarms/telemetry/user_utils.py index 9369bc26..4d4fb166 100644 --- a/swarms/telemetry/user_utils.py +++ b/swarms/telemetry/user_utils.py @@ -5,6 +5,7 @@ import socket from swarms.telemetry.sys_info import system_info from swarms.telemetry.check_update import check_for_package + # Helper functions def generate_user_id(): """Generate user id @@ -75,7 +76,6 @@ def get_local_ip(): return socket.gethostbyname(socket.gethostname()) - def get_user_device_data(): data = { "ID": generate_user_id(), @@ -85,5 +85,6 @@ def get_user_device_data(): "Swarms [Version]": check_for_package("swarms"), } return data - -# \ No newline at end of file + + +# diff --git a/swarms/utils/__init__.py b/swarms/utils/__init__.py index 7593f0aa..c8eaabaa 100644 --- a/swarms/utils/__init__.py +++ b/swarms/utils/__init__.py @@ -25,7 +25,7 @@ from swarms.utils.download_weights_from_url import ( from swarms.utils.save_logs import parse_log_file -######## +######## from swarms.utils.yaml_output_parser import YamlOutputParser from swarms.utils.json_output_parser import JsonOutputParser from swarms.utils.remove_json_whitespace import ( diff --git a/swarms/utils/json_output_parser.py b/swarms/utils/json_output_parser.py index a180e408..724d5ed5 100644 --- a/swarms/utils/json_output_parser.py +++ b/swarms/utils/json_output_parser.py @@ -5,9 +5,11 @@ from pydantic import BaseModel, ValidationError T = TypeVar("T", bound=BaseModel) + class JsonParsingException(Exception): """Custom exception for errors in JSON parsing.""" + class JsonOutputParser: """Parse JSON output using a Pydantic model. @@ -17,7 +19,7 @@ class JsonOutputParser: Attributes: pydantic_object: A Pydantic model class for parsing and validation. pattern: A regex pattern to match JSON code blocks. - + Examples: >>> from pydantic import BaseModel >>> from swarms.utils.json_output_parser import JsonOutputParser @@ -29,12 +31,14 @@ class JsonOutputParser: >>> text = "```json\n{\"name\": \"John\", \"age\": 42}\n```" >>> model = parser.parse(text) >>> model.name - + """ def __init__(self, pydantic_object: Type[T]): self.pydantic_object = pydantic_object - self.pattern = re.compile(r"^```(?:json)?(?P[^`]*)", re.MULTILINE | re.DOTALL) + self.pattern = re.compile( + r"^```(?:json)?(?P[^`]*)", re.MULTILINE | re.DOTALL + ) def parse(self, text: str) -> T: """Parse the provided text to extract and validate JSON data. @@ -57,7 +61,10 @@ class JsonOutputParser: except (json.JSONDecodeError, ValidationError) as e: name = self.pydantic_object.__name__ - msg = f"Failed to parse {name} from text '{text}'. Error: {e}" + msg = ( + f"Failed to parse {name} from text '{text}'." + f" Error: {e}" + ) raise JsonParsingException(msg) from e def get_format_instructions(self) -> str: @@ -67,12 +74,19 @@ class JsonOutputParser: A string containing formatting instructions. """ schema = self.pydantic_object.schema() - reduced_schema = {k: v for k, v in schema.items() if k not in ['title', 'type']} + reduced_schema = { + k: v + for k, v in schema.items() + if k not in ["title", "type"] + } schema_str = json.dumps(reduced_schema, indent=4) - format_instructions = f"JSON Formatting Instructions:\n{schema_str}" + format_instructions = ( + f"JSON Formatting Instructions:\n{schema_str}" + ) return format_instructions + # # Example usage # class ExampleModel(BaseModel): # field1: int diff --git a/swarms/utils/remove_json_whitespace.py b/swarms/utils/remove_json_whitespace.py index 842e04c1..a5b3f7de 100644 --- a/swarms/utils/remove_json_whitespace.py +++ b/swarms/utils/remove_json_whitespace.py @@ -1,6 +1,7 @@ import json import yaml + def remove_whitespace_from_json(json_string: str) -> str: """ Removes unnecessary whitespace from a JSON string. @@ -15,14 +16,14 @@ def remove_whitespace_from_json(json_string: str) -> str: str: The JSON string with whitespace removed. """ parsed = json.loads(json_string) - return json.dumps(parsed, separators=(',', ':')) + return json.dumps(parsed, separators=(",", ":")) + # # Example usage for JSON # json_string = '{"field1": 123, "field2": "example text"}' # print(remove_whitespace_from_json(json_string)) - def remove_whitespace_from_yaml(yaml_string: str) -> str: """ Removes unnecessary whitespace from a YAML string. @@ -40,6 +41,7 @@ def remove_whitespace_from_yaml(yaml_string: str) -> str: parsed = yaml.safe_load(yaml_string) return yaml.dump(parsed, default_flow_style=True) + # # Example usage for YAML # yaml_string = """ # field1: 123 diff --git a/swarms/utils/yaml_output_parser.py b/swarms/utils/yaml_output_parser.py index c6749d8a..61be311b 100644 --- a/swarms/utils/yaml_output_parser.py +++ b/swarms/utils/yaml_output_parser.py @@ -6,9 +6,11 @@ from pydantic import BaseModel, ValidationError T = TypeVar("T", bound=BaseModel) + class YamlParsingException(Exception): """Custom exception for errors in YAML parsing.""" + class YamlOutputParser: """Parse YAML output using a Pydantic model. @@ -18,8 +20,8 @@ class YamlOutputParser: Attributes: pydantic_object: A Pydantic model class for parsing and validation. pattern: A regex pattern to match YAML code blocks. - - + + Examples: >>> from pydantic import BaseModel >>> from swarms.utils.yaml_output_parser import YamlOutputParser @@ -31,12 +33,14 @@ class YamlOutputParser: >>> text = "```yaml\nname: John\nage: 42\n```" >>> model = parser.parse(text) >>> model.name - + """ def __init__(self, pydantic_object: Type[T]): self.pydantic_object = pydantic_object - self.pattern = re.compile(r"^```(?:ya?ml)?(?P[^`]*)", re.MULTILINE | re.DOTALL) + self.pattern = re.compile( + r"^```(?:ya?ml)?(?P[^`]*)", re.MULTILINE | re.DOTALL + ) def parse(self, text: str) -> T: """Parse the provided text to extract and validate YAML data. @@ -59,7 +63,10 @@ class YamlOutputParser: except (yaml.YAMLError, ValidationError) as e: name = self.pydantic_object.__name__ - msg = f"Failed to parse {name} from text '{text}'. Error: {e}" + msg = ( + f"Failed to parse {name} from text '{text}'." + f" Error: {e}" + ) raise YamlParsingException(msg) from e def get_format_instructions(self) -> str: @@ -69,8 +76,14 @@ class YamlOutputParser: A string containing formatting instructions. """ schema = self.pydantic_object.schema() - reduced_schema = {k: v for k, v in schema.items() if k not in ['title', 'type']} + reduced_schema = { + k: v + for k, v in schema.items() + if k not in ["title", "type"] + } schema_str = json.dumps(reduced_schema, indent=4) - format_instructions = f"YAML Formatting Instructions:\n{schema_str}" + format_instructions = ( + f"YAML Formatting Instructions:\n{schema_str}" + ) return format_instructions