[FEAT][Agent prompt cleanup]

pull/243/head
Kye 1 year ago
parent 0555a2bacd
commit 8d0b751ec2

@ -16,6 +16,7 @@ vision_api = GPT4VisionAPI(api_key=openai_api_key)
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 @@ class Idea2Image(Agent):
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

@ -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 <kye@apac.ai>"]

@ -145,6 +145,8 @@ class StableDiffusion:
# 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

@ -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 @@ 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.
"""
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

@ -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."
)

@ -15,8 +15,6 @@ from swarms.prompts.multi_modal_autonomous_instruction_prompt import (
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 @@ from swarms.utils.parse_code import (
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 @@ class Agent:
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 @@ class Agent:
# 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 @@ class Agent:
{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 @@ class Agent:
SYSTEM_PROMPT: {system_prompt}
History: {history}
################ CHAT HISTORY ####################
{history}
"""
return agent_history_prompt

Loading…
Cancel
Save