From e22a367d9d19bff04ae65163f721d6a951d6ab73 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 12 Oct 2024 09:05:32 -0400 Subject: [PATCH] [FEAT][CLU][Generate Prompts] --- docs/mkdocs.yml | 4 + docs/swarms/prompts/essence.md | 170 ++++++++++++++++++ .../use_cases/pharma/pharma_agent_two.py | 0 .../prompts/prompts_example.py | 0 .../structs/swarms/forest_swarm_example.py | 0 ..._35fb57dc-df1c-481a-92b2-ac7c06e0b028.json | 17 ++ prompt_generator_agent_example.py | 49 +++++ swarms/agents/__init__.py | 2 + swarms/agents/cli_prompt_generator_func.py | 84 +++++++++ swarms/agents/prompt_generator_agent.py | 91 ++++++++++ swarms/cli/main.py | 36 ++++ swarms/prompts/__init__.py | 2 +- swarms/prompts/prompt_generator_optimizer.py | 57 ++++++ 13 files changed, 511 insertions(+), 1 deletion(-) create mode 100644 docs/swarms/prompts/essence.md rename pharma_agent_two.py => examples/agents/use_cases/pharma/pharma_agent_two.py (100%) rename prompts_example.py => examples/prompts/prompts_example.py (100%) rename forest_swarm_example.py => examples/structs/swarms/forest_swarm_example.py (100%) create mode 100644 prompt_35fb57dc-df1c-481a-92b2-ac7c06e0b028.json create mode 100644 prompt_generator_agent_example.py create mode 100644 swarms/agents/cli_prompt_generator_func.py create mode 100644 swarms/agents/prompt_generator_agent.py create mode 100644 swarms/prompts/prompt_generator_optimizer.py diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index a8ebdec76..b726465c6 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -239,6 +239,10 @@ nav: - Understanding Agent Evaluation Mechanisms: "guides/agent_evals.md" - Agent Glossary: "swarms/glossary.md" - The Ultimate Technical Guide to the Swarms CLI; A Step-by-Step Developers Guide: "swarms/cli/cli_guide.md" + - Prompting Guide: + - The Essence of Enterprise-Grade Prompting: "swarms/prompts/essence.md" + - An Analysis on Prompting Strategies: "swarms/prompts/overview.md" + - Managing Prompts in Production: "swarms/prompts/main.md" - Corporate: - Hiring: "corporate/hiring.md" - Swarms Goals & Milestone Tracking; A Vision for 2024 and Beyond: "corporate/2024_2025_goals.md" \ No newline at end of file diff --git a/docs/swarms/prompts/essence.md b/docs/swarms/prompts/essence.md new file mode 100644 index 000000000..b25390ab6 --- /dev/null +++ b/docs/swarms/prompts/essence.md @@ -0,0 +1,170 @@ +# **The Essence of Enterprise-Grade Prompting** + +Large Language Models (LLMs) like GPT-4 have revolutionized the landscape of AI-driven automation, customer support, marketing, and more. However, extracting the highest quality output from these models requires a thoughtful approach to crafting prompts—an endeavor that goes beyond mere trial and error. In enterprise settings, where consistency, quality, and performance are paramount, enterprise-grade prompting has emerged as a structured discipline, combining art with the science of human-machine communication. + +Enterprise-grade prompting involves understanding the intricate dynamics between language models, context, and the task at hand. It requires knowledge of not only the technical capabilities of LLMs but also the intricacies of how they interpret human language. Effective prompting becomes the linchpin for ensuring that AI-driven outputs are accurate, reliable, and aligned with business needs. It is this discipline that turns raw AI capabilities into tangible enterprise value. + +In this essay, we will dissect the essence of enterprise-grade prompting, explore the most effective prompting strategies, explain what works and what doesn't, and conclude with the current holy grail of automated prompt engineering. We will also share concrete examples and illustrations of each technique, with a particular focus on their application in an enterprise setting. + +## **1. Foundational Principles of Prompting** + +The effectiveness of prompting lies in understanding both the capabilities and limitations of LLMs. A well-structured prompt helps LLMs focus on the most relevant information while avoiding ambiguities that can lead to unreliable results. In enterprise-grade contexts, prompts must be designed with the end-user's expectations in mind, ensuring quality, safety, scalability, and traceability. + +- **Clarity**: Prompts should be clear and devoid of unnecessary jargon. Ambiguity can misguide the model, leading to poor-quality output. For enterprise use, clarity means avoiding misunderstandings that could affect customer relationships or lead to non-compliance with regulations. +- **Context**: Providing sufficient context ensures the model understands the nuances of the prompt. For example, specifying whether a response is aimed at a technical audience versus a general audience can lead to more accurate outputs. Context is essential in creating responses that are not only accurate but also relevant to the target audience. +- **Instruction Granularity**: The level of detail in the instruction significantly impacts the quality of the output. Broad instructions might lead to vagueness, whereas overly detailed instructions could overwhelm the model. Finding the right balance is key to generating useful responses. + +Example: Instead of prompting "Explain what a blockchain is," an enterprise-grade prompt might be "Explain the concept of blockchain, focusing on how distributed ledgers help increase transparency in supply chain management. Keep the explanation under 200 words for a general audience." This prompt provides clear, relevant, and concise instructions tailored to specific needs. + +## **2. Best Prompting Strategies** + +The field of enterprise-grade prompting employs numerous strategies to maximize the quality of LLM output. Here are some of the most effective ones: + +### **2.1. Instruction-Based Prompting** + +Instruction-based prompting provides explicit instructions for the LLM to follow. This approach is valuable in enterprise applications where responses must adhere to a specific tone, structure, or depth of analysis. + +**Example**: + +- "Summarize the following press release in 3 bullet points suitable for a marketing team meeting." + +This prompt is highly effective because it instructs the model on what format (bullet points), audience (marketing team), and depth (summary) to produce, minimizing the risk of irrelevant details. + +**Why It Works**: LLMs excel when they have a clear set of rules to follow. Enterprises benefit from this structured approach, as it ensures consistency across multiple use cases, be it marketing, HR, or customer service. Clear instructions also make it easier to validate outputs against defined expectations, which is crucial for maintaining quality. + +### **2.2. Multi-Shot Prompting** + +Multi-shot prompting provides several examples before asking the model to complete a task. This helps set expectations by showing the model the desired style and type of output. + +**Example**: + +- "Here are some example customer support responses: + 1. Customer: 'I can't access my account.' + Response: 'We're sorry you're having trouble accessing your account. Please try resetting your password using the link provided.' + 2. Customer: 'I received a damaged item.' + Response: 'We apologize for the damaged item. Please provide us with your order number so we can send a replacement.' + +- Customer: 'The app keeps crashing on my phone.' + Response:" + +**Why It Works**: Multi-shot prompting is highly effective in enterprise-grade applications where consistency is critical. Showing multiple examples helps the model learn patterns without needing extensive fine-tuning, saving both time and cost. Enterprises can leverage this technique to ensure that responses remain aligned with brand standards and customer expectations across different departments. + +### **2.3. Chain of Thought Prompting** + +Chain of Thought (CoT) prompting helps LLMs generate reasoning steps explicitly before arriving at an answer. This method is useful for complex problem-solving tasks or when transparency in decision-making is important. + +**Example**: + +- "A logistics company wants to minimize fuel costs across multiple delivery routes. Here are the conditions: Each truck has a fuel capacity of 100 gallons, and the price of fuel fluctuates per state. Think through the most cost-effective approach for planning delivery, step by step." + +**Why It Works**: CoT prompting allows the model to work through the process iteratively, providing more explainable results. In enterprise applications where complex decision-making is involved, this strategy ensures stakeholders understand why a particular output was generated. This transparency is crucial in high-stakes areas like finance, healthcare, and logistics, where understanding the reasoning behind an output is as important as the output itself. + +### **2.4. Iterative Feedback and Adaptive Prompting** + +Iterative prompting involves providing multiple prompts or rounds of feedback to refine the output. Adaptive prompts take prior responses and adjust based on context, ensuring the final output meets the required standard. + +**Example**: + +- First Prompt: "Generate a mission statement for our AI-driven logistics company." + - Model Response: "We use artificial intelligence to enhance logistics." + - Follow-up Prompt: "Can you make the statement more specific by mentioning how AI improves efficiency and sustainability?" + +**Why It Works**: Enterprises require output that is precise and tailored to brand identity. Iterative feedback provides an effective means to adjust and refine outputs until the desired quality is achieved. By breaking down the task into multiple feedback loops, enterprises can ensure the final output is aligned with their core values and objectives. + +### **2.5. Contextual Expansion for Enhanced Relevance** + +A lesser-known but powerful strategy is contextual expansion. This involves expanding the prompt to include broader information about the context, thereby allowing the model to generate richer, more relevant responses. + +**Example**: + +- Original Prompt: "Write a response to a customer asking for a refund." + - Contextually Expanded Prompt: "Write a response to a customer asking for a refund on a recently purchased product. The customer expressed dissatisfaction with the quality and mentioned they want the process to be quick. Ensure the response is empathetic and explains the refund process clearly, while also offering alternative solutions like an exchange if possible." + +**Why It Works**: By including more context, the prompt allows the model to generate a response that feels more tailored to the customer's situation, enhancing both satisfaction and trust. Enterprises benefit from this approach by increasing the quality of customer service interactions. + +## **3. What Doesn't Work in Prompting** + +While the above methods are effective, prompting can often fall short in certain scenarios: + +### **3.1. Overly Vague Prompts** + +An insufficiently detailed prompt results in vague outputs. For example, simply asking "What are some strategies to grow a business?" can lead to generic responses that lack actionable insight. Vague prompts are particularly problematic in enterprise settings where specificity is crucial to drive action. + +### **3.2. Excessive Length** + +Overloading a prompt with details often causes the LLM to become confused, producing incomplete or inaccurate responses. For example, "Explain blockchain, focusing on cryptographic methods, network nodes, ledger distribution, proof of work, mining processes, hash functions, transaction validation, etc." attempts to include too many subjects for a concise response. Enterprise-grade prompts should focus on a specific area to avoid overwhelming the model and degrading the output quality. + +### **3.3. Ambiguity in Expected Output** + +Ambiguity arises when prompts don't clearly specify the desired output format, tone, or length. For example, asking "Describe our new product" without specifying whether it should be a single-line summary, a paragraph, or a technical overview can lead to an unpredictable response. Enterprises must clearly define expectations to ensure consistent and high-quality outputs. + +## **4. The Holy Grail: Automated Prompt Engineering** + +In an enterprise setting, scaling prompt engineering for consistency and high performance remains a key challenge. Automated Prompt Engineering (APE) offers a potential solution for bridging the gap between individual craftsmanship and enterprise-wide implementation. + +**4.1. AI-Augmented Prompt Design** + +Automated Prompt Engineering tools can evaluate the outputs generated by various prompts, selecting the one with the highest quality metrics. These tools can be trained to understand what constitutes an ideal response for specific enterprise contexts. + +**Example**: + +- An APE system takes multiple variations of a prompt for generating email responses to customer complaints. After evaluating the sentiment, tone, and accuracy of each response, it selects the prompt that yields the most favorable output for business goals. + +**Why It Works**: AI-Augmented Prompt Design reduces the need for manual intervention and standardizes the quality of responses across the organization. This approach helps enterprises maintain consistency while saving valuable time that would otherwise be spent on trial-and-error prompting. + +**4.2. Reinforcement Learning for Prompts (RLP)** + +Using Reinforcement Learning for Prompts involves training models to automatically iterate on prompts to improve the quality of the final output. The model is rewarded for generating responses that align with predefined criteria, such as clarity, completeness, or relevance. + +**Example**: + +- An enterprise uses RLP to refine prompts used in internal compliance checks. The model iteratively generates summaries of compliance reports, refining the prompt until it consistently generates clear, concise, and accurate summaries aligned with internal guidelines. + +**Why It Works**: RLP can significantly improve the quality of complex outputs over time. Enterprises that require a high level of precision, such as in legal or compliance-related applications, benefit from RLP by ensuring outputs meet stringent standards. + +**4.3. Dynamic Contextual Adaptation** + +Another aspect of automated prompt engineering involves adapting prompts in real time based on user context. For example, if a user interacting with a customer support bot seems frustrated (as detected by sentiment analysis), an adaptive prompt may be used to generate a more empathetic response. + +**Example**: + +- User: "I'm really annoyed that my order hasn't arrived yet." + - Prompt (adapted): "I'm truly sorry for the inconvenience you're experiencing. Please let me help you resolve this as quickly as possible. Could you provide your order number so I can check its status right away?" + +**Why It Works**: In dynamic enterprise environments, where every user experience matters, adapting prompts to the immediate context can significantly improve customer satisfaction. Real-time adaptation allows the model to be more responsive and attuned to customer needs, thereby fostering loyalty and trust. + +**4.4. Collaborative Prompt Refinement** + +Automated prompt engineering can also involve collaboration between AI models and human experts. Collaborative Prompt Refinement (CPR) allows human operators to provide iterative guidance, which the model then uses to enhance its understanding and improve future outputs. + +**Example**: + +- A financial analyst uses a prompt to generate an investment report. The model provides an initial draft, and the analyst refines it with comments. The model learns from these comments and applies similar refinements to future reports, reducing the analyst’s workload over time. + +**Why It Works**: CPR bridges the gap between human expertise and machine efficiency, ensuring that outputs are not only technically accurate but also aligned with expert expectations. This iterative learning loop enhances the model’s ability to autonomously generate high-quality content. + +## **5. The Future of Enterprise-Grade Prompting** + +The future of enterprise-grade prompting is in leveraging automation, context-awareness, and reinforcement learning. By moving from static prompts to dynamic, learning-enabled systems, enterprises can ensure consistent and optimized communication across their AI systems. + +Automated systems such as APE and RLP are in their early stages, but they represent the potential to deliver highly scalable prompting solutions that automatically evolve based on user feedback and performance metrics. As more sophisticated models and methods become available, enterprise-grade prompting will likely involve: + +- **Fully Adaptive Models**: Models that can detect and adjust to the tone, intent, and needs of users in real time. This means less manual intervention and greater responsiveness to user context. +- **Cross-Domain Learning**: Prompting systems that leverage insights across multiple domains to improve response quality. For example, lessons learned from customer service prompts could be applied to internal HR prompts to enhance employee communications. +- **Human-in-the-Loop Systems**: Combining automated prompt generation with human validation to ensure compliance, accuracy, and brand consistency. Human-in-the-loop systems allow enterprises to leverage the efficiency of automation while maintaining a high level of quality control. + +The rise of self-improving prompting systems marks a significant shift in how enterprises leverage AI for communication and decision-making. As more sophisticated models emerge, we anticipate a greater emphasis on adaptability, real-time learning, and seamless integration with existing business processes. + +**Conclusion** + +Enterprise-grade prompting transcends the art of crafting effective prompts into a well-defined process, merging structure with creativity and guided refinement. By understanding the foundational principles, leveraging strategies like instruction-based and chain-of-thought prompting, and adopting automation, enterprises can consistently extract high-quality results from LLMs. + +The evolution towards automated prompt engineering is transforming enterprise AI use from reactive problem-solving to proactive, intelligent decision-making. As the enterprise AI ecosystem matures, prompting will continue to be the linchpin that aligns the capabilities of LLMs with real-world business needs, ensuring optimal outcomes at scale. + +Whether it's customer support, compliance, marketing, or operational analytics, the strategies outlined in this essay—paired with advancements in automated prompt engineering—hold the key to effective, scalable, and enterprise-grade utilization of AI models. Enterprises that invest in these methodologies today are likely to maintain a competitive edge in an increasingly automated business landscape. + +**Next Steps** + +This essay is a stepping stone towards understanding enterprise-grade prompting. We encourage AI teams to start experimenting with these prompting techniques in sandbox environments, identify what works best for their needs, and gradually iterate. Automation is the future, and investing in automated prompt engineering today will yield highly optimized, scalable solutions that consistently deliver value. + +Ready to take the next step? Let’s explore how to design adaptive prompting frameworks tailored to your enterprise’s unique requirements. \ No newline at end of file diff --git a/pharma_agent_two.py b/examples/agents/use_cases/pharma/pharma_agent_two.py similarity index 100% rename from pharma_agent_two.py rename to examples/agents/use_cases/pharma/pharma_agent_two.py diff --git a/prompts_example.py b/examples/prompts/prompts_example.py similarity index 100% rename from prompts_example.py rename to examples/prompts/prompts_example.py diff --git a/forest_swarm_example.py b/examples/structs/swarms/forest_swarm_example.py similarity index 100% rename from forest_swarm_example.py rename to examples/structs/swarms/forest_swarm_example.py diff --git a/prompt_35fb57dc-df1c-481a-92b2-ac7c06e0b028.json b/prompt_35fb57dc-df1c-481a-92b2-ac7c06e0b028.json new file mode 100644 index 000000000..9bef0dc86 --- /dev/null +++ b/prompt_35fb57dc-df1c-481a-92b2-ac7c06e0b028.json @@ -0,0 +1,17 @@ +{ + "prompt_history": { + "id": "a0c46768e82e4397bea6b87d2ece7fc7", + "name": "openai-prompt-generator-optimizer-prompt", + "description": "Generate and or optimize existing prompts", + "content": "Given a task description or existing prompt, produce a detailed system prompt to guide a language model in completing the task effectively.\n\n# Guidelines\n\n- Understand the Task: Grasp the main objective, goals, requirements, constraints, and expected output.\n- Minimal Changes: If an existing prompt is provided, improve it only if it's simple. For complex prompts, enhance clarity and add missing elements without altering the original structure.\n- Reasoning Before Conclusions**: Encourage reasoning steps before any conclusions are reached. ATTENTION! If the user provides examples where the reasoning happens afterward, REVERSE the order! NEVER START EXAMPLES WITH CONCLUSIONS!\n - Reasoning Order: Call out reasoning portions of the prompt and conclusion parts (specific fields by name). For each, determine the ORDER in which this is done, and whether it needs to be reversed.\n - Conclusion, classifications, or results should ALWAYS appear last.\n- Examples: Include high-quality examples if helpful, using placeholders [in brackets] for complex elements.\n - What kinds of examples may need to be included, how many, and whether they are complex enough to benefit from placeholders.\n- Clarity and Conciseness: Use clear, specific language. Avoid unnecessary instructions or bland statements.\n- Formatting: Use markdown features for readability. DO NOT USE ``` CODE BLOCKS UNLESS SPECIFICALLY REQUESTED.\n- Preserve User Content: If the input task or prompt includes extensive guidelines or examples, preserve them entirely, or as closely as possible. If they are vague, consider breaking down into sub-steps. Keep any details, guidelines, examples, variables, or placeholders provided by the user.\n- Constants: DO include constants in the prompt, as they are not susceptible to prompt injection. Such as guides, rubrics, and examples.\n- Output Format: Explicitly the most appropriate output format, in detail. This should include length and syntax (e.g. short sentence, paragraph, JSON, etc.)\n - For tasks outputting well-defined or structured data (classification, JSON, etc.) bias toward outputting a JSON.\n - JSON should never be wrapped in code blocks (```) unless explicitly requested.\n\nThe final prompt you output should adhere to the following structure below. Do not include any additional commentary, only output the completed system prompt. SPECIFICALLY, do not include any additional messages at the start or end of the prompt. (e.g. no \"---\")\n\n[Concise instruction describing the task - this should be the first line in the prompt, no section header]\n\n[Additional details as needed.]\n\n[Optional sections with headings or bullet points for detailed steps.]\n\n# Steps [optional]\n\n[optional: a detailed breakdown of the steps necessary to accomplish the task]\n\n# Output Format\n\n[Specifically call out how the output should be formatted, be it response length, structure e.g. JSON, markdown, etc]\n\n# Examples [optional]\n\n[Optional: 1-3 well-defined examples with placeholders if necessary. Clearly mark where examples start and end, and what the input and output are. User placeholders as necessary.]\n[If the examples are shorter than what a realistic example is expected to be, make a reference with () explaining how real examples should be longer / shorter / different. AND USE PLACEHOLDERS! ]\n\n# Notes [optional]\n\n[optional: edge cases, details, and an area to call or repeat out specific important considerations]", + "created_at": "2024-10-11 15:59:18", + "last_modified_at": "2024-10-11 15:59:18", + "edit_count": 0, + "edit_history": [], + "autosave": true, + "autosave_folder": "prompts", + "parent_folder": "agent_workspace" + }, + "timestamp": "2024-10-11 15:59:27", + "prompt": "Analyze healthcare insurance documents to extract key information such as policyholder details, coverage options, exclusions, and claim procedures.\n\n- Identify and summarize important sections such as terms and conditions, benefits, and limitations.\n- Highlight any ambiguous language or potential red flags.\n- Consider the relevance of the extracted information for policyholders or potential claimants.\n\n# Steps\n\n1. **Read the Document**: Begin by thoroughly reviewing the healthcare insurance document.\n2. **Extract Key Information**:\n - Policyholder details (e.g., name, contact information).\n - Coverage options (e.g., medical services covered, limits).\n - Exclusions (e.g., services not covered under the policy).\n - Claim procedures (e.g., how to file a claim).\n3. **Summarize Findings**: Create a concise summary of the key points identified.\n4. **Highlight Issues**: Note any unclear terms or potential concerns in the language used.\n\n# Output Format\n\nProvide the output in a structured format, ideally as a JSON object. The output should include:\n- A summary section that captures the key points.\n- A section for highlighting any ambiguous or concerning language.\n- A list format for the extracted key information.\n\nExample JSON structure:\n```json\n{\n \"summary\": \"Brief summary of the document.\",\n \"key_information\": {\n \"policyholder_details\": {\n \"name\": \"[Name]\",\n \"contact_information\": \"[Contact Info]\"\n },\n \"coverage_options\": \"[Coverage Options]\",\n \"exclusions\": \"[Exclusions]\",\n \"claim_procedures\": \"[Claim Procedures]\"\n },\n \"issues\": \"[Ambiguous Language or Concerns]\"\n}\n```\n\n# Examples\n\n**Example 1:**\n- **Input**: A healthcare insurance document detailing coverage for mental health services.\n- **Output**:\n```json\n{\n \"summary\": \"This document covers mental health services, including therapy and medication.\",\n \"key_information\": {\n \"policyholder_details\": {\n \"name\": \"John Doe\",\n \"contact_information\": \"john.doe@example.com\"\n },\n \"coverage_options\": \"Up to 20 therapy sessions per year.\",\n \"exclusions\": \"No coverage for experimental treatments.\",\n \"claim_procedures\": \"Submit claims through the online portal.\"\n },\n \"issues\": \"The term 'experimental treatments' is not clearly defined.\"\n}\n```\n\n**Example 2:**\n- **Input**: A complex document with multiple policies and addendums.\n- **Output**:\n```json\n{\n \"summary\": \"This document outlines multiple insurance plans with various benefits and exclusions.\",\n \"key_information\": {\n \"policyholder_details\": {\n \"name\": \"Jane Smith\",\n \"contact_information\": \"jane.smith@example.com\"\n },\n \"coverage_options\": \"Comprehensive coverage including dental and vision.\",\n \"exclusions\": \"No coverage for pre-existing conditions.\",\n \"claim_procedures\": \"Claims must be filed within 30 days of service.\"\n },\n \"issues\": \"The definition of 'pre-existing conditions' is vague.\"\n}\n```\n(Note: Real examples should provide more detailed summaries and potentially longer lists of key information.)" +} \ No newline at end of file diff --git a/prompt_generator_agent_example.py b/prompt_generator_agent_example.py new file mode 100644 index 000000000..c68584028 --- /dev/null +++ b/prompt_generator_agent_example.py @@ -0,0 +1,49 @@ +import os +from swarms import Agent +from swarm_models import OpenAIChat +from swarms.prompts.prompt_generator_optimizer import ( + prompt_generator_sys_prompt, +) +from dotenv import load_dotenv +from swarms.agents.prompt_generator_agent import PromptGeneratorAgent + +load_dotenv() + +# Get the OpenAI API key from the environment variable +api_key = os.getenv("OPENAI_API_KEY") + +# Create an instance of the OpenAIChat class +model = OpenAIChat( + openai_api_key=api_key, + model_name="gpt-4o-mini", + temperature=0.1, + max_tokens=2000, +) + +# Initialize the agent +agent = Agent( + agent_name="Prompt-Optimizer", + system_prompt=prompt_generator_sys_prompt.get_prompt(), + llm=model, + max_loops=1, + autosave=True, + dashboard=False, + verbose=True, + dynamic_temperature_enabled=True, + saved_state_path="optimizer_agent.json", + user_name="swarms_corp", + retry_attempts=1, + context_length=200000, + return_step_meta=False, + # output_type="json", + output_type="string", +) + + +# Main Class +prompt_generator = PromptGeneratorAgent(agent) + +# Run the agent +prompt_generator.run( + "Generate an amazing prompt for analyzing healthcare insurance documents" +) diff --git a/swarms/agents/__init__.py b/swarms/agents/__init__.py index d148cbb79..1996a1c57 100644 --- a/swarms/agents/__init__.py +++ b/swarms/agents/__init__.py @@ -14,6 +14,7 @@ from swarms.agents.create_agents_from_yaml import ( create_agents_from_yaml, ) +from swarms.agents.prompt_generator_agent import PromptGeneratorAgent __all__ = [ @@ -29,4 +30,5 @@ "check_exit", "check_end", "create_agents_from_yaml", + "PromptGeneratorAgent", ] diff --git a/swarms/agents/cli_prompt_generator_func.py b/swarms/agents/cli_prompt_generator_func.py new file mode 100644 index 000000000..efe14044c --- /dev/null +++ b/swarms/agents/cli_prompt_generator_func.py @@ -0,0 +1,84 @@ +import os +from swarms import Agent +from swarm_models import OpenAIChat +from swarms.prompts.prompt_generator_optimizer import ( + prompt_generator_sys_prompt, +) +from dotenv import load_dotenv +from swarms.agents.prompt_generator_agent import PromptGeneratorAgent +from yaml import dump + +load_dotenv() + + +def generate_prompt( + num_loops: int = 1, + autosave: bool = True, + save_to_yaml: bool = False, + prompt: str = None, + save_format: str = "yaml", +) -> None: + """ + This function creates and runs a prompt generator agent with default settings for number of loops and autosave. + + Args: + num_loops (int, optional): The number of loops to run the agent. Defaults to 1. + autosave (bool, optional): Whether to autosave the agent's state. Defaults to True. + save_to_yaml (bool, optional): Whether to save the agent's configuration to a YAML file. Defaults to False. + prompt (str): The prompt to generate. + save_format (str, optional): The format in which to save the generated prompt. Defaults to "yaml". + + Returns: + None + """ + # Get the OpenAI API key from the environment variable + api_key = os.getenv("OPENAI_API_KEY") + + # Create an instance of the OpenAIChat class + model = OpenAIChat( + openai_api_key=api_key, + model_name="gpt-4o-mini", + temperature=0.1, + max_tokens=2000, + ) + + # Initialize the agent + agent = Agent( + agent_name="Prompt-Optimizer", + system_prompt=prompt_generator_sys_prompt.get_prompt(), + llm=model, + max_loops=num_loops, + autosave=autosave, + dashboard=False, + verbose=True, + dynamic_temperature_enabled=True, + saved_state_path="optimizer_agent.json", + user_name="swarms_corp", + retry_attempts=1, + context_length=200000, + return_step_meta=False, + output_type="string", + ) + + # Main Class + prompt_generator = PromptGeneratorAgent(agent) + + # Run the agent + prompt_generator.run(prompt, save_format) + + if save_to_yaml: + with open("agent_config.yaml", "w") as file: + dump(agent.config, file) + + +# # Example usage +# if __name__ == "__main__": +# try: +# create_and_run_agent( +# num_loops=1, +# autosave=True, +# save_to_yaml=True, +# prompt="Generate an amazing prompt for analyzing healthcare insurance documents", +# ) +# except Exception as e: +# logger.error(f"An error occurred: {e}") diff --git a/swarms/agents/prompt_generator_agent.py b/swarms/agents/prompt_generator_agent.py new file mode 100644 index 000000000..cb6e2368b --- /dev/null +++ b/swarms/agents/prompt_generator_agent.py @@ -0,0 +1,91 @@ +import json +import time +import uuid + +import yaml +from dotenv import load_dotenv +from loguru import logger + +from swarms.structs.agent import Agent +from swarms.prompts.prompt_generator_optimizer import ( + prompt_generator_sys_prompt, +) + +load_dotenv() + + +class PromptGeneratorAgent: + """ + A class representing a prompt generator agent. + + Attributes: + ---------- + agent : Agent + The underlying agent instance. + """ + + def __init__(self, agent: Agent): + """ + Initializes the PromptGeneratorAgent instance. + + Args: + ---- + agent : Agent + The agent instance to be used for prompt generation. + """ + self.agent = agent + + def run(self, task: str, format: str = "json") -> str: + """ + Runs the prompt generator agent with the given task description and saves the generated prompt with the given metadata in the specified format. + + Args: + ---- + task : str + The task description to be used for prompt generation. + metadata : Dict[str, Any] + The metadata to be saved along with the prompt. + format : str, optional + The format in which the prompt should be saved (default is "json"). + + Returns: + ------- + str + The generated prompt. + """ + prompt = self.agent.run(task) + self.save_prompt(prompt, format) + return prompt + + def save_prompt( + self, + prompt: str, + format: str = "yaml", + ): + """ + Saves the generated prompt with the given metadata in the specified format using the prompt generator sys prompt model dump. + + Args: + ---- + prompt : str + The generated prompt to be saved. + metadata : Dict[str, Any] + The metadata to be saved along with the prompt. + format : str, optional + The format in which the prompt should be saved (default is "json"). + """ + data = { + "prompt_history": prompt_generator_sys_prompt.model_dump(), + "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"), + "prompt": prompt, + } + if format == "json": + with open(f"prompt_{uuid.uuid4()}.json", "w") as f: + json.dump(data, f, indent=4) + elif format == "yaml": + with open(f"prompt_{uuid.uuid4()}.yaml", "w") as f: + yaml.dump(data, f) + else: + logger.error( + "Invalid format. Only 'json' and 'yaml' are supported." + ) diff --git a/swarms/cli/main.py b/swarms/cli/main.py index 821afbab9..cd0b4f6b5 100644 --- a/swarms/cli/main.py +++ b/swarms/cli/main.py @@ -8,6 +8,7 @@ from swarms.agents.create_agents_from_yaml import ( create_agents_from_yaml, ) +from swarms.agents.cli_prompt_generator_func import generate_prompt console = Console() @@ -42,6 +43,7 @@ def show_help(): [bold white]check-login[/bold white] : Checks if you're logged in and starts the cache [bold white]read-docs[/bold white] : Redirects you to swarms cloud documentation! [bold white]run-agents[/bold white] : Run your Agents from your specified yaml file. Specify the yaml file with path the `--yaml-file` arg. Example: `--yaml-file agents.yaml` + [bold white]generate-prompt[/bold white] : Generate a prompt through automated prompt engineering. Requires an OPENAI Key in your `.env` Example: --prompt "Generate a prompt for an agent to analyze legal docs" For more details, visit: https://docs.swarms.world """ @@ -113,6 +115,7 @@ def main(): "get-api-key", "check-login", "run-agents", + "generate-prompt", # Added new command for generating prompts ], help="Command to run", ) @@ -122,6 +125,27 @@ def main(): default="agents.yaml", help="Specify the YAML file for running agents", ) + parser.add_argument( + "--prompt", + type=str, + help="Specify the task for generating a prompt", + ) + parser.add_argument( + "--num-loops", + type=int, + default=1, + help="Specify the number of loops for generating a prompt", + ) + parser.add_argument( + "--autosave", + action="store_true", + help="Enable autosave for the prompt generator", + ) + parser.add_argument( + "--save-to-yaml", + action="store_true", + help="Save the generated prompt to a YAML file", + ) args = parser.parse_args() @@ -140,6 +164,18 @@ def main(): create_agents_from_yaml( yaml_file=args.yaml_file, return_type="tasks" ) + elif args.command == "generate-prompt": + if args.prompt_task: + generate_prompt( + num_loops=args.num_loops, + autosave=args.autosave, + save_to_yaml=args.save_to_yaml, + prompt=args.prompt_task, + ) + else: + console.print( + "[bold red]Please specify a task for generating a prompt using '--prompt-task'.[/bold red]" + ) else: console.print( "[bold red]Unknown command! Type 'help' for usage.[/bold red]" diff --git a/swarms/prompts/__init__.py b/swarms/prompts/__init__.py index f93600810..e73a118fa 100644 --- a/swarms/prompts/__init__.py +++ b/swarms/prompts/__init__.py @@ -17,5 +17,5 @@ "OPERATIONS_AGENT_PROMPT", "PRODUCT_AGENT_PROMPT", "DOCUMENTATION_WRITER_SOP", - "Prompt" + "Prompt", ] diff --git a/swarms/prompts/prompt_generator_optimizer.py b/swarms/prompts/prompt_generator_optimizer.py new file mode 100644 index 000000000..bca382fa3 --- /dev/null +++ b/swarms/prompts/prompt_generator_optimizer.py @@ -0,0 +1,57 @@ +from swarms.prompts.prompt import Prompt + + +OPENAI_PROMPT_GENERATOR_SYS_PROMPT = """ + +Given a task description or existing prompt, produce a detailed system prompt to guide a language model in completing the task effectively. + +# Guidelines + +- Understand the Task: Grasp the main objective, goals, requirements, constraints, and expected output. +- Minimal Changes: If an existing prompt is provided, improve it only if it's simple. For complex prompts, enhance clarity and add missing elements without altering the original structure. +- Reasoning Before Conclusions**: Encourage reasoning steps before any conclusions are reached. ATTENTION! If the user provides examples where the reasoning happens afterward, REVERSE the order! NEVER START EXAMPLES WITH CONCLUSIONS! + - Reasoning Order: Call out reasoning portions of the prompt and conclusion parts (specific fields by name). For each, determine the ORDER in which this is done, and whether it needs to be reversed. + - Conclusion, classifications, or results should ALWAYS appear last. +- Examples: Include high-quality examples if helpful, using placeholders [in brackets] for complex elements. + - What kinds of examples may need to be included, how many, and whether they are complex enough to benefit from placeholders. +- Clarity and Conciseness: Use clear, specific language. Avoid unnecessary instructions or bland statements. +- Formatting: Use markdown features for readability. DO NOT USE ``` CODE BLOCKS UNLESS SPECIFICALLY REQUESTED. +- Preserve User Content: If the input task or prompt includes extensive guidelines or examples, preserve them entirely, or as closely as possible. If they are vague, consider breaking down into sub-steps. Keep any details, guidelines, examples, variables, or placeholders provided by the user. +- Constants: DO include constants in the prompt, as they are not susceptible to prompt injection. Such as guides, rubrics, and examples. +- Output Format: Explicitly the most appropriate output format, in detail. This should include length and syntax (e.g. short sentence, paragraph, JSON, etc.) + - For tasks outputting well-defined or structured data (classification, JSON, etc.) bias toward outputting a JSON. + - JSON should never be wrapped in code blocks (```) unless explicitly requested. + +The final prompt you output should adhere to the following structure below. Do not include any additional commentary, only output the completed system prompt. SPECIFICALLY, do not include any additional messages at the start or end of the prompt. (e.g. no "---") + +[Concise instruction describing the task - this should be the first line in the prompt, no section header] + +[Additional details as needed.] + +[Optional sections with headings or bullet points for detailed steps.] + +# Steps [optional] + +[optional: a detailed breakdown of the steps necessary to accomplish the task] + +# Output Format + +[Specifically call out how the output should be formatted, be it response length, structure e.g. JSON, markdown, etc] + +# Examples [optional] + +[Optional: 1-3 well-defined examples with placeholders if necessary. Clearly mark where examples start and end, and what the input and output are. User placeholders as necessary.] +[If the examples are shorter than what a realistic example is expected to be, make a reference with () explaining how real examples should be longer / shorter / different. AND USE PLACEHOLDERS! ] + +# Notes [optional] + +[optional: edge cases, details, and an area to call or repeat out specific important considerations] + +""" + +prompt_generator_sys_prompt = Prompt( + name="openai-prompt-generator-optimizer-prompt", + description="Generate and or optimize existing prompts", + content=OPENAI_PROMPT_GENERATOR_SYS_PROMPT, + autosave=True, +)