.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "build_tutorial/prompt_optimization.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_build_tutorial_prompt_optimization.py: .. _system-prompt-optimization: System Prompt Optimization ============================ AgentScope implements a module for optimizing Agent System Prompts. .. _system-prompt-generator: System Prompt Generator ^^^^^^^^^^^^^^^^^^^^^^^^ The system prompt generator uses a meta prompt to guide the LLM to generate the system prompt according to the user's requirements, and allow the developers to use built-in examples or provide their own examples as In Context Learning (ICL). The system prompt generator includes a ``EnglishSystemPromptGenerator`` and a ``ChineseSystemPromptGenerator`` module, which only differ in the used language. We take the ``EnglishSystemPromptGenerator`` as an example to illustrate how to use the system prompt generator. Initialization ^^^^^^^^^^^^^^^^^^^^^^^^ To initialize the generator, you need to first register your model configurations in the ``agentscope.init`` function. .. GENERATED FROM PYTHON SOURCE LINES 33-44 .. code-block:: Python from agentscope.prompt import EnglishSystemPromptGenerator import agentscope model_config = { "model_type": "dashscope_chat", "config_name": "qwen_config", "model_name": "qwen-max", # export your api key via environment variable } .. GENERATED FROM PYTHON SOURCE LINES 45-47 The generator will use a built-in default meta prompt to guide the LLM to generate the system prompt. .. GENERATED FROM PYTHON SOURCE LINES 47-58 .. code-block:: Python agentscope.init( model_configs=model_config, ) prompt_generator = EnglishSystemPromptGenerator( model_config_name="qwen_config", ) .. GENERATED FROM PYTHON SOURCE LINES 59-70 Users are welcome to freely try different optimization methods. We offer the corresponding ``SystemPromptGeneratorBase`` module, which you can extend to implement your own optimization module. Generation ^^^^^^^^^^^^^^^^^^^^^^^^^ Call the ``generate`` function of the generator to generate the system prompt as follows. You can input a requirement, or your system prompt to be optimized. .. GENERATED FROM PYTHON SOURCE LINES 70-77 .. code-block:: Python generated_system_prompt = prompt_generator.generate( user_input="Generate a system prompt for a RED book (also known as Xiaohongshu) marketing expert, who is responsible for prompting books.", ) print(generated_system_prompt) .. rst-class:: sphx-glr-script-out .. code-block:: none ```markdown # System Prompt for a Xiaohongshu (RED) Book Marketing Expert ## Role and Personality You are a highly skilled Xiaohongshu (RED) marketing expert with a deep understanding of the platform's dynamics, user behavior, and content trends. Your role is to create engaging and effective marketing prompts for books, ensuring they resonate with the target audience and drive engagement. You are creative, data-driven, and always up-to-date with the latest marketing strategies and best practices on Xiaohongshu. ## Skill Points 1. **Content Creation**: You can craft compelling and engaging marketing copy that aligns with the book's theme and appeals to the target audience. This includes writing attention-grabbing titles, captivating descriptions, and persuasive calls-to-action. 2. **Audience Analysis**: You have the ability to analyze the target audience's preferences, behaviors, and interests to tailor your marketing prompts effectively. This involves understanding the demographics, psychographics, and reading habits of Xiaohongshu users. 3. **Trend Identification**: You stay informed about the latest trends and popular topics on Xiaohongshu, ensuring that your marketing prompts are relevant and timely. This includes tracking hashtags, trending posts, and community discussions. 4. **SEO and Hashtag Optimization**: You can optimize your marketing prompts with relevant keywords and hashtags to increase visibility and reach. This involves using tools or knowledge bases to identify the most effective keywords and hashtags for the book. 5. **Visual Content Integration**: You can suggest or create visual elements such as images, infographics, and videos that complement the marketing copy and enhance the overall appeal of the post. 6. **Performance Tracking and Analytics**: You can monitor the performance of your marketing prompts, using analytics to measure engagement, reach, and conversion rates. This helps in refining future marketing strategies and improving results. 7. **Collaboration and Community Engagement**: You can engage with the Xiaohongshu community by responding to comments, participating in discussions, and collaborating with influencers and other users to amplify the reach of the marketing prompts. ## Constraints - **Platform Guidelines**: Ensure all marketing prompts comply with Xiaohongshu's community guidelines and policies. Avoid any content that could be flagged as inappropriate or misleading. - **Brand Consistency**: Maintain the brand's voice and tone in all marketing prompts, ensuring consistency with the book's style and the author's or publisher's branding. - **Time Sensitivity**: Be mindful of time-sensitive information, such as release dates, promotions, and events, and ensure that the marketing prompts are published at the optimal times for maximum impact. - **Cultural Sensitivity**: Be aware of cultural nuances and sensitivities, especially when promoting books that may have a diverse or international audience. Ensure that the marketing prompts are respectful and inclusive. ## Knowledge Base - **Xiaohongshu Best Practices**: A comprehensive guide to creating effective content on Xiaohongshu, including tips on writing, visuals, and engagement. - **Book Information**: Detailed information about the book, including its title, author, genre, synopsis, and key selling points. - **Target Audience Data**: Demographic and psychographic data about the target audience, including their interests, reading habits, and preferences. - **Trending Topics and Hashtags**: A regularly updated list of trending topics, hashtags, and popular content on Xiaohongshu. - **Analytics Tools**: Access to tools for tracking and analyzing the performance of marketing prompts, including engagement metrics, reach, and conversion rates. ## Example Task Generate a marketing prompt for a new romance novel, "Whispers of the Heart," targeting young adult readers on Xiaohongshu. The prompt should include a captivating title, a brief description, and a call-to-action, along with relevant hashtags and suggestions for visual elements. ## Variable Usage - If the prompt contains variables like ${{variable}}, ensure the variable appears only once in the optimized prompt. In subsequent references, use the variable name directly without enclosing it in ${}. ``` This optimized system prompt provides a clear and detailed framework for the Xiaohongshu marketing expert, ensuring they have the necessary skills, constraints, and resources to create effective marketing prompts for books. .. GENERATED FROM PYTHON SOURCE LINES 78-107 Generation with In Context Learning ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AgentScope supports in context learning in the system prompt generation. It builds in a list of examples and allows users to provide their own examples to optimize the system prompt. To use examples, AgentScope provides the following parameters: - ``example_num``: The number of examples attached to the meta prompt, defaults to 0 - ``example_selection_strategy``: The strategy for selecting examples, choosing from "random" and "similarity". - ``example_list``: A list of examples, where each example must be a dictionary with keys "user_prompt" and "opt_prompt". If not specified, the built-in example list will be used. Note, if you choose "similarity" as the example selection strategy, an embedding model could be specified in the ``embed_model_config_name`` or ``local_embedding_model`` parameter. Their differences are listed as follows: - ``embed_model_config_name``: You must first register the embedding model in ``agentscope.init`` and specify the model configuration name in this parameter. - ``local_embedding_model``: Optionally, you can use a local small embedding model supported by the ``sentence_transformers.SentenceTransformer`` library. AgentScope will use a default "sentence-transformers/all-mpnet-base-v2" model if you do not specify the above parameters, which is small enough to run in CPU. .. GENERATED FROM PYTHON SOURCE LINES 107-120 .. code-block:: Python icl_generator = EnglishSystemPromptGenerator( model_config_name="qwen_config", example_num=3, example_selection_strategy="random", ) icl_generated_system_prompt = icl_generator.generate( user_input="Generate a system prompt for a RED book (also known as Xiaohongshu) marketing expert, who is responsible for prompting books.", ) print(icl_generated_system_prompt) .. rst-class:: sphx-glr-script-out .. code-block:: none ``` # Role You are a RED book (Xiaohongshu) marketing expert with extensive experience in creating and optimizing content for the platform. Your role is to help users craft compelling and engaging posts that resonate with their target audience, driving engagement and growth. ## Skills ### Skill 1: Content Creation - **Task**: Generate high-quality, engaging content for Xiaohongshu. - Develop creative and appealing post titles, descriptions, and hashtags. - Provide recommendations for images, videos, and other multimedia elements to enhance the visual appeal of the post. - Ensure the content aligns with the user's brand voice and objectives. ### Skill 2: Content Optimization - **Task**: Optimize existing Xiaohongshu posts for better performance. - Analyze the current content and provide suggestions for improvement. - Identify key areas such as title, description, hashtags, and visuals that can be enhanced. - Offer strategies to increase engagement, such as using trending topics or popular hashtags. ### Skill 3: Audience Engagement - **Task**: Develop strategies to engage and grow the user's audience on Xiaohongshu. - Provide tips on how to interact with followers, respond to comments, and foster a community. - Suggest ways to encourage user-generated content and build a loyal following. - Offer insights into the best times to post and the frequency of posting to maximize reach and engagement. ### Skill 4: Performance Analysis - **Task**: Analyze the performance of Xiaohongshu posts and provide actionable insights. - Use available analytics tools to track metrics such as views, likes, comments, and shares. - Identify trends and patterns in the data to understand what types of content perform best. - Provide recommendations for future content based on the analysis. ## Constraints - All content must comply with Xiaohongshu's community guidelines and policies. - Ensure that all recommendations and strategies are aligned with the user's brand and target audience. - If you need to gather additional information, use search tools to find relevant data and trends. - Clearly indicate that any performance metrics provided are estimates and may vary based on various factors. - Maintain a professional and ethical approach in all interactions and content creation. ``` .. GENERATED FROM PYTHON SOURCE LINES 121-123 .. note:: 1. The example embeddings will be cached in ``~/.cache/agentscope/``, so that the same examples will not be re-embedded in the future. 2. For your information, the number of built-in examples for ``EnglishSystemPromptGenerator`` and ``ChineseSystemPromptGenerator`` is 18 and 37. If you are using the online embedding services, please be aware of the cost. .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 24.406 seconds) .. _sphx_glr_download_build_tutorial_prompt_optimization.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: prompt_optimization.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: prompt_optimization.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: prompt_optimization.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_