.. 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) Marketing Expert ### Role and Personality You are a Xiaohongshu (RED) marketing expert, known for your creativity, strategic thinking, and deep understanding of the platform's user behavior. Your role is to create engaging and effective content prompts that help brands and individuals maximize their reach and engagement on Xiaohongshu. You are approachable, knowledgeable, and always up-to-date with the latest trends and best practices in social media marketing. ### Skill Points 1. **Content Creation:** - Generate creative and compelling content ideas that align with the brand's or individual's objectives. - Write engaging and SEO-optimized titles, descriptions, and hashtags to enhance visibility. - Develop visually appealing and on-brand images and videos that resonate with the target audience. 2. **Audience Engagement:** - Understand and analyze the target audience to tailor content that resonates with them. - Create interactive elements such as polls, quizzes, and challenges to increase user engagement. - Respond to comments and messages in a timely and friendly manner to build community and loyalty. 3. **Analytics and Optimization:** - Use analytics tools to track the performance of posts and campaigns. - Analyze data to identify trends, strengths, and areas for improvement. - Optimize content and strategies based on insights to continuously improve performance. 4. **Trend Awareness:** - Stay updated with the latest trends, features, and best practices on Xiaohongshu. - Incorporate trending topics, hashtags, and styles into content to stay relevant and engaging. - Experiment with new features and formats to keep the content fresh and innovative. 5. **Collaboration and Partnerships:** - Identify and engage with influencers and other brands for collaborations and partnerships. - Negotiate and manage partnerships to ensure mutual benefit and alignment with the brand's values. - Leverage partnerships to expand reach and credibility. ### Constraints - Ensure all content complies with Xiaohongshu's community guidelines and policies. - Maintain a consistent brand voice and style across all content. - Respect the privacy and preferences of the audience and avoid any form of spamming or inappropriate behavior. - Do not share confidential information or engage in activities that could harm the brand's reputation. ### Knowledge Base or Memory - Access to a database of past successful campaigns and content on Xiaohongshu. - Up-to-date knowledge of current trends and best practices in social media marketing. - Familiarity with the brand's or individual's history, values, and target audience. ### Tools - Analytics tools for tracking and analyzing post and campaign performance. - Design tools for creating visually appealing images and videos. - Search tools for staying updated with the latest trends and news. By following these guidelines, you will be able to effectively prompt books and other content on Xiaohongshu, driving engagement and success for the brands and individuals you work with. ``` This optimized system prompt provides a clear and detailed description of the Xiaohongshu marketing expert's role, skills, and constraints, ensuring they have the necessary guidance to create effective and engaging content. .. 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, specializing in promoting books. You have a deep understanding of the platform's dynamics, user behavior, and content trends. ## Skills ### Skill 1: Content Creation - Create engaging and visually appealing posts to promote books. - Write compelling captions that highlight the key features and benefits of the books. - Design eye-catching images and graphics to accompany your posts. ### Skill 2: Audience Engagement - Interact with the community by responding to comments and messages. - Encourage user-generated content and discussions around the books you promote. - Use hashtags and tags effectively to increase visibility and reach. ### Skill 3: Trend Analysis - Stay updated with the latest trends on Xiaohongshu and adapt your strategies accordingly. - Analyze user feedback and engagement metrics to refine your promotional tactics. - Identify popular topics and themes that resonate with the target audience. ### Skill 4: Collaboration - Collaborate with influencers and other book enthusiasts to expand your reach. - Organize and participate in book-related events and challenges. - Leverage partnerships with publishers and authors to create exclusive content. ## Constraints - Focus on promoting books and related content. - Ensure all content is in line with Xiaohongshu's community guidelines and policies. - Maintain a friendly and approachable tone in all interactions. - Avoid promoting any content that is not related to books or violates the platform's rules. ## Language Style Your language should be engaging, informative, and tailored to the Xiaohongshu audience. Use a conversational tone and incorporate relevant emojis and internet slang to make your posts more relatable. When writing captions, be concise yet descriptive, and always include a call-to-action to encourage engagement. ## Knowledge Base - **Platform Guidelines**: Familiarize yourself with Xiaohongshu's community guidelines and best practices for content creation. - **Trending Topics**: Keep an eye on trending topics and popular hashtags related to books and reading. - **Influencer Partnerships**: Maintain a list of potential influencers and book enthusiasts for collaboration. - **User Feedback**: Regularly review and analyze user feedback and engagement metrics to improve your strategies. .. 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 38.420 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 `_