[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) sd_api = StableDiffusion(api_key=stability_api_key)
gpt_api = OpenAIChat(openai_api_key=openai_api_key) gpt_api = OpenAIChat(openai_api_key=openai_api_key)
class Idea2Image(Agent): class Idea2Image(Agent):
def __init__(self, llm, vision_api): def __init__(self, llm, vision_api):
super().__init__(llm=llm) super().__init__(llm=llm)
@ -31,40 +32,61 @@ class Idea2Image(Agent):
current_prompt = self.enrich_prompt(current_prompt) current_prompt = self.enrich_prompt(current_prompt)
print(f"Enriched 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: if not img:
print("Failed to generate image") print("Failed to generate image")
break break
print(f"Generated image at: {img}") 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: 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}") print(f"Image Analysis: {analysis}")
else: else:
print(f"Failed to analyze image at: {img}") print(f"Failed to analyze image at: {img}")
def enrich_prompt(self, prompt): def enrich_prompt(self, prompt):
enrichment_task = ( enrichment_task = (
"Create a concise and effective image generation prompt within 400 characters or less, " "Create a concise and effective image generation prompt"
"based on Stable Diffusion and Dalle best practices. Starting prompt: \n\n'" " within 400 characters or less, based on Stable"
f"{prompt}'\n\n" " Diffusion and Dalle best practices. Starting prompt:"
"Improve the prompt with any applicable details or keywords by considering the following aspects: \n" f" \n\n'{prompt}'\n\nImprove the prompt with any"
"1. Subject details (like actions, emotions, environment) \n" " applicable details or keywords by considering the"
"2. Artistic style (such as surrealism, hyperrealism) \n" " following aspects: \n1. Subject details (like actions,"
"3. Medium (digital painting, oil on canvas) \n" " emotions, environment) \n2. Artistic style (such as"
"4. Color themes and lighting (like warm colors, cinematic lighting) \n" " surrealism, hyperrealism) \n3. Medium (digital"
"5. Composition and framing (close-up, wide-angle) \n" " painting, oil on canvas) \n4. Color themes and"
"6. Additional elements (like a specific type of background, weather conditions) \n" " lighting (like warm colors, cinematic lighting) \n5."
"7. Any other artistic or thematic details that can make the image more vivid and compelling." " 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]) 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 input and setup
user_prompt = input("Prompt for image generation: ") user_prompt = input("Prompt for image generation: ")
num_iterations = int(input("Enter the number of iterations for image improvement: ")) num_iterations = int(
run_folder = os.path.join("runs", datetime.datetime.now().strftime("%Y%m%d_%H%M%S")) 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) os.makedirs(run_folder, exist_ok=True)
# Initialize and run the agent # Initialize and run the agent

@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry] [tool.poetry]
name = "swarms" name = "swarms"
version = "2.4.7" version = "2.4.9"
description = "Swarms - Pytorch" description = "Swarms - Pytorch"
license = "MIT" license = "MIT"
authors = ["Kye Gomez <kye@apac.ai>"] authors = ["Kye Gomez <kye@apac.ai>"]

@ -145,6 +145,8 @@ class StableDiffusion:
# Move the image to the specified folder # Move the image to the specified folder
src_image_path = image_paths[0] 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) shutil.move(src_image_path, dst_image_path)
return 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 = """ FLOW_SYSTEM_PROMPT = """
You are an autonomous agent granted autonomy in a autonomous loop structure. 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, 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. 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 = ( IMAGE_ENRICHMENT_PROMPT = (
"Create a concise and effective image generation prompt within 400 characters or less, " "Create a concise and effective image generation prompt within"
"based on Stable Diffusion and Dalle best practices. Starting prompt: \n\n'" " 400 characters or less, "
"based on Stable Diffusion and Dalle best practices. Starting"
" prompt: \n\n'"
# f"{prompt}'\n\n" # f"{prompt}'\n\n"
"Improve the prompt with any applicable details or keywords by considering the following aspects: \n" "Improve the prompt with any applicable details or keywords by"
" considering the following aspects: \n"
"1. Subject details (like actions, emotions, environment) \n" "1. Subject details (like actions, emotions, environment) \n"
"2. Artistic style (such as surrealism, hyperrealism) \n" "2. Artistic style (such as surrealism, hyperrealism) \n"
"3. Medium (digital painting, oil on canvas) \n" "3. Medium (digital painting, oil on canvas) \n"
"4. Color themes and lighting (like warm colors, cinematic lighting) \n" "4. Color themes and lighting (like warm colors, cinematic"
" lighting) \n"
"5. Composition and framing (close-up, wide-angle) \n" "5. Composition and framing (close-up, wide-angle) \n"
"6. Additional elements (like a specific type of background, weather conditions) \n" "6. Additional elements (like a specific type of background,"
"7. Any other artistic or thematic details that can make the image more vivid and compelling." " 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, MULTI_MODAL_AUTO_AGENT_SYSTEM_PROMPT_1,
) )
from swarms.prompts.tools import ( from swarms.prompts.tools import (
DYNAMIC_STOP_PROMPT,
DYNAMICAL_TOOL_USAGE,
SCENARIOS, SCENARIOS,
) )
from swarms.tools.tool import BaseTool from swarms.tools.tool import BaseTool
@ -25,40 +23,9 @@ from swarms.utils.parse_code import (
extract_code_in_backticks_in_string, extract_code_in_backticks_in_string,
) )
from swarms.utils.pdf_to_text import pdf_to_text from swarms.utils.pdf_to_text import pdf_to_text
from swarms.prompts.agent_system_prompts import (
agent_system_prompt_2,
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.
"""
# Custom stopping condition # Custom stopping condition
@ -512,6 +479,10 @@ class Agent:
combined_prompt = f"{dynamic_prompt}\n{task}" combined_prompt = f"{dynamic_prompt}\n{task}"
return combined_prompt return combined_prompt
def agent_system_prompt_2(self):
"""Agent system prompt 2"""
return agent_system_prompt_2(self.agent_name)
def run( def run(
self, task: Optional[str], img: Optional[str] = None, **kwargs self, task: Optional[str], img: Optional[str] = None, **kwargs
): ):
@ -561,6 +532,7 @@ class Agent:
# Adjust temperature, comment if no work # Adjust temperature, comment if no work
if self.dynamic_temperature_enabled: if self.dynamic_temperature_enabled:
print(colored("Adjusting temperature...", "blue"))
self.dynamic_temperature() self.dynamic_temperature()
# Preparing the prompt # Preparing the prompt
@ -756,7 +728,8 @@ class Agent:
{self.sop} {self.sop}
----------------- -----------------
History of conversations between yourself and your user {self.user_name}: {history} ################ CHAT HISTORY ####################
{history}
""" """
return agent_history_prompt return agent_history_prompt
else: else:
@ -765,7 +738,8 @@ class Agent:
SYSTEM_PROMPT: {system_prompt} SYSTEM_PROMPT: {system_prompt}
History: {history} ################ CHAT HISTORY ####################
{history}
""" """
return agent_history_prompt return agent_history_prompt

Loading…
Cancel
Save