Skip to content

Commit

Permalink
[FEAT][Agent prompt cleanup]
Browse files Browse the repository at this point in the history
  • Loading branch information
Kye committed Dec 1, 2023
1 parent 0555a2b commit 8d0b751
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 71 deletions.
56 changes: 39 additions & 17 deletions playground/demos/idea_2_img/idea2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
Expand Down
4 changes: 3 additions & 1 deletion swarms/models/stable_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
58 changes: 57 additions & 1 deletion swarms/prompts/agent_system_prompts.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 ########
<DONE>
########### 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
31 changes: 18 additions & 13 deletions swarms/prompts/idea2img.py
Original file line number Diff line number Diff line change
@@ -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."
)
50 changes: 12 additions & 38 deletions swarms/structs/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -765,7 +738,8 @@ def agent_history_prompt(
SYSTEM_PROMPT: {system_prompt}
History: {history}
################ CHAT HISTORY ####################
{history}
"""
return agent_history_prompt

Expand Down

0 comments on commit 8d0b751

Please sign in to comment.