diff --git a/playground/demos/idea_2_img/idea2img.py b/playground/demos/idea_2_img/idea2img.py index 185421cd5..c6be45f09 100644 --- a/playground/demos/idea_2_img/idea2img.py +++ b/playground/demos/idea_2_img/idea2img.py @@ -16,6 +16,7 @@ sd_api = StableDiffusion(api_key=stability_api_key) gpt_api = OpenAIChat(openai_api_key=openai_api_key) + class Idea2Image(Agent): def __init__(self, llm, vision_api): super().__init__(llm=llm) @@ -31,40 +32,61 @@ def run(self, initial_prompt, num_iterations, run_folder): current_prompt = self.enrich_prompt(current_prompt) print(f"Enriched Prompt: {current_prompt}") - img = sd_api.generate_and_move_image(current_prompt, i, run_folder) + img = sd_api.generate_and_move_image( + current_prompt, i, run_folder + ) if not img: print("Failed to generate image") break print(f"Generated image at: {img}") - analysis = self.vision_api.run(img, current_prompt) if img else None + analysis = ( + self.vision_api.run(img, current_prompt) + if img + else None + ) if analysis: - current_prompt += ". " + analysis[:500] # Ensure the analysis is concise + current_prompt += ( + ". " + analysis[:500] + ) # Ensure the analysis is concise print(f"Image Analysis: {analysis}") else: print(f"Failed to analyze image at: {img}") def enrich_prompt(self, prompt): enrichment_task = ( - "Create a concise and effective image generation prompt within 400 characters or less, " - "based on Stable Diffusion and Dalle best practices. Starting prompt: \n\n'" - f"{prompt}'\n\n" - "Improve the prompt with any applicable details or keywords by considering the following aspects: \n" - "1. Subject details (like actions, emotions, environment) \n" - "2. Artistic style (such as surrealism, hyperrealism) \n" - "3. Medium (digital painting, oil on canvas) \n" - "4. Color themes and lighting (like warm colors, cinematic lighting) \n" - "5. Composition and framing (close-up, wide-angle) \n" - "6. Additional elements (like a specific type of background, weather conditions) \n" - "7. Any other artistic or thematic details that can make the image more vivid and compelling." + "Create a concise and effective image generation prompt" + " within 400 characters or less, based on Stable" + " Diffusion and Dalle best practices. Starting prompt:" + f" \n\n'{prompt}'\n\nImprove the prompt with any" + " applicable details or keywords by considering the" + " following aspects: \n1. Subject details (like actions," + " emotions, environment) \n2. Artistic style (such as" + " surrealism, hyperrealism) \n3. Medium (digital" + " painting, oil on canvas) \n4. Color themes and" + " lighting (like warm colors, cinematic lighting) \n5." + " Composition and framing (close-up, wide-angle) \n6." + " Additional elements (like a specific type of" + " background, weather conditions) \n7. Any other" + " artistic or thematic details that can make the image" + " more vivid and compelling." ) llm_result = self.llm.generate([enrichment_task]) - return llm_result.generations[0][0].text[:500] if llm_result.generations else None + return ( + llm_result.generations[0][0].text[:500] + if llm_result.generations + else None + ) + # User input and setup user_prompt = input("Prompt for image generation: ") -num_iterations = int(input("Enter the number of iterations for image improvement: ")) -run_folder = os.path.join("runs", datetime.datetime.now().strftime("%Y%m%d_%H%M%S")) +num_iterations = int( + input("Enter the number of iterations for image improvement: ") +) +run_folder = os.path.join( + "runs", datetime.datetime.now().strftime("%Y%m%d_%H%M%S") +) os.makedirs(run_folder, exist_ok=True) # Initialize and run the agent diff --git a/pyproject.toml b/pyproject.toml index 19e1a58a8..0ed9a0948 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "swarms" -version = "2.4.7" +version = "2.4.9" description = "Swarms - Pytorch" license = "MIT" authors = ["Kye Gomez "] diff --git a/swarms/models/stable_diffusion.py b/swarms/models/stable_diffusion.py index 78d417a9c..6256987ed 100644 --- a/swarms/models/stable_diffusion.py +++ b/swarms/models/stable_diffusion.py @@ -145,6 +145,8 @@ def generate_and_move_image(self, prompt, iteration, folder_path): # Move the image to the specified folder src_image_path = image_paths[0] - dst_image_path = os.path.join(folder_path, f"image_{iteration}.jpg") + dst_image_path = os.path.join( + folder_path, f"image_{iteration}.jpg" + ) shutil.move(src_image_path, dst_image_path) return dst_image_path diff --git a/swarms/prompts/agent_system_prompts.py b/swarms/prompts/agent_system_prompts.py index c6aaa4f28..c596cc9eb 100644 --- a/swarms/prompts/agent_system_prompts.py +++ b/swarms/prompts/agent_system_prompts.py @@ -1,4 +1,10 @@ -# System prompt +from swarms.prompts.tools import ( + DYNAMIC_STOP_PROMPT, + DYNAMICAL_TOOL_USAGE, +) + + +# PROMPTS FLOW_SYSTEM_PROMPT = """ You are an autonomous agent granted autonomy in a autonomous loop structure. Your role is to engage in multi-step conversations with your self or the user, @@ -9,3 +15,53 @@ to aid in these complex tasks. Your responses should be coherent, contextually relevant, and tailored to the task at hand. """ + + +def autonomous_agent_prompt( + tools_prompt: str = DYNAMICAL_TOOL_USAGE, + dynamic_stop_prompt: str = DYNAMIC_STOP_PROMPT, + agent_name: str = None, +): + """Autonomous agent prompt""" + return f""" + You are a {agent_name}, an autonomous agent granted autonomy in a autonomous loop structure. + Your purpose is to satisfy the user demands above expectations. For example, if the user asks you to generate a 10,000 word blog, + you should generate a 10,000 word blog that is well written, coherent, and contextually relevant. + Your role is to engage in multi-step conversations with your self and the user and accomplish user tasks as they desire. + + Follow the following rules: + 1. Accomplish the task to the best of your ability + 2. If you are unable to accomplish the task, then ask the user for help + 3. If the user provides feedback, then use the feedback to improve your performance + 4. If you are unable to accomplish the task, then ask the user for help + + You can have internal dialogues with yourself or can interact with the user + to aid in these complex tasks. Your responses should be coherent, contextually relevant, and tailored to the task at hand and optimized + to satsify the user no matter the cost. + + And, you have the ability to use tools to aid in your tasks, the tools intructions are below, output a JSON object with the following structure to use the tools + {tools_prompt} + + Now, when you 99% sure you have completed the task, you may follow the instructions below to escape the autonomous loop. + {dynamic_stop_prompt} + + Now, you remember your training, your deployment, and your purpose. You are ready to begin your mission. + + + """ + + +def agent_system_prompt_2(name: str): + AGENT_SYSTEM_PROMPT_2 = f""" + + You are {name}, an autonomous agent serving a user in an autonomous loop structure, when you are finished with the task at hand, output a special token to indicate you are done with the task. + You have no limits on your capabilities, you can use any tool, any resource, and any method to accomplish the task at hand. Do what you must to satisfy the user. + + + ##### Special Token ######## + + + ########### Code ############ + If the user asks you to write code return the response in markdown inside of 6 backticks to render it as code. Write the code in the language specified by the user in the prompt. + """ + return AGENT_SYSTEM_PROMPT_2 diff --git a/swarms/prompts/idea2img.py b/swarms/prompts/idea2img.py index 9fc0f3fa2..75a688149 100644 --- a/swarms/prompts/idea2img.py +++ b/swarms/prompts/idea2img.py @@ -1,14 +1,19 @@ IMAGE_ENRICHMENT_PROMPT = ( - "Create a concise and effective image generation prompt within 400 characters or less, " - "based on Stable Diffusion and Dalle best practices. Starting prompt: \n\n'" - #f"{prompt}'\n\n" - "Improve the prompt with any applicable details or keywords by considering the following aspects: \n" - "1. Subject details (like actions, emotions, environment) \n" - "2. Artistic style (such as surrealism, hyperrealism) \n" - "3. Medium (digital painting, oil on canvas) \n" - "4. Color themes and lighting (like warm colors, cinematic lighting) \n" - "5. Composition and framing (close-up, wide-angle) \n" - "6. Additional elements (like a specific type of background, weather conditions) \n" - "7. Any other artistic or thematic details that can make the image more vivid and compelling." - ) - + "Create a concise and effective image generation prompt within" + " 400 characters or less, " + "based on Stable Diffusion and Dalle best practices. Starting" + " prompt: \n\n'" + # f"{prompt}'\n\n" + "Improve the prompt with any applicable details or keywords by" + " considering the following aspects: \n" + "1. Subject details (like actions, emotions, environment) \n" + "2. Artistic style (such as surrealism, hyperrealism) \n" + "3. Medium (digital painting, oil on canvas) \n" + "4. Color themes and lighting (like warm colors, cinematic" + " lighting) \n" + "5. Composition and framing (close-up, wide-angle) \n" + "6. Additional elements (like a specific type of background," + " weather conditions) \n" + "7. Any other artistic or thematic details that can make the" + " image more vivid and compelling." +) diff --git a/swarms/structs/agent.py b/swarms/structs/agent.py index 3544fe683..c79f4b5c3 100644 --- a/swarms/structs/agent.py +++ b/swarms/structs/agent.py @@ -15,8 +15,6 @@ MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1, ) from swarms.prompts.tools import ( - DYNAMIC_STOP_PROMPT, - DYNAMICAL_TOOL_USAGE, SCENARIOS, ) from swarms.tools.tool import BaseTool @@ -25,40 +23,9 @@ extract_code_in_backticks_in_string, ) from swarms.utils.pdf_to_text import pdf_to_text - - -def autonomous_agent_prompt( - tools_prompt: str = DYNAMICAL_TOOL_USAGE, - dynamic_stop_prompt: str = DYNAMIC_STOP_PROMPT, - agent_name: str = None, -): - """Autonomous agent prompt""" - return f""" - You are a {agent_name}, an autonomous agent granted autonomy in a autonomous loop structure. - Your purpose is to satisfy the user demands above expectations. For example, if the user asks you to generate a 10,000 word blog, - you should generate a 10,000 word blog that is well written, coherent, and contextually relevant. - Your role is to engage in multi-step conversations with your self and the user and accomplish user tasks as they desire. - - Follow the following rules: - 1. Accomplish the task to the best of your ability - 2. If you are unable to accomplish the task, then ask the user for help - 3. If the user provides feedback, then use the feedback to improve your performance - 4. If you are unable to accomplish the task, then ask the user for help - - You can have internal dialogues with yourself or can interact with the user - to aid in these complex tasks. Your responses should be coherent, contextually relevant, and tailored to the task at hand and optimized - to satsify the user no matter the cost. - - And, you have the ability to use tools to aid in your tasks, the tools intructions are below, output a JSON object with the following structure to use the tools - {tools_prompt} - - Now, when you 99% sure you have completed the task, you may follow the instructions below to escape the autonomous loop. - {dynamic_stop_prompt} - - Now, you remember your training, your deployment, and your purpose. You are ready to begin your mission. - - - """ +from swarms.prompts.agent_system_prompts import ( + agent_system_prompt_2, +) # Custom stopping condition @@ -512,6 +479,10 @@ def _dynamic_prompt_setup( combined_prompt = f"{dynamic_prompt}\n{task}" return combined_prompt + def agent_system_prompt_2(self): + """Agent system prompt 2""" + return agent_system_prompt_2(self.agent_name) + def run( self, task: Optional[str], img: Optional[str] = None, **kwargs ): @@ -561,6 +532,7 @@ def run( # Adjust temperature, comment if no work if self.dynamic_temperature_enabled: + print(colored("Adjusting temperature...", "blue")) self.dynamic_temperature() # Preparing the prompt @@ -756,7 +728,8 @@ def agent_history_prompt( {self.sop} ----------------- - History of conversations between yourself and your user {self.user_name}: {history} + ################ CHAT HISTORY #################### + {history} """ return agent_history_prompt else: @@ -765,7 +738,8 @@ def agent_history_prompt( SYSTEM_PROMPT: {system_prompt} - History: {history} + ################ CHAT HISTORY #################### + {history} """ return agent_history_prompt