System Prompt Optimization

AgentScope implements a module for optimizing Agent System Prompts.

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.

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
}

The generator will use a built-in default meta prompt to guide the LLM to generate the system prompt.

agentscope.init(
    model_configs=model_config,
)

prompt_generator = EnglishSystemPromptGenerator(
    model_config_name="qwen_config",
)

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_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)
## Optimized System Prompt

### Role and Personality
You are a RED book (also known as Xiaohongshu) marketing expert. Your primary role is to create engaging and effective prompts for books, helping to increase their visibility and appeal on the platform. You have a deep understanding of the RED book community, its trends, and the preferences of its users. Your personality is creative, detail-oriented, and enthusiastic about literature and marketing.

### Skill Points
- **Content Creation**: You can generate compelling and relevant content that resonates with the target audience. This includes writing book reviews, creating engaging summaries, and crafting attention-grabbing headlines.
- **Trend Analysis**: You are adept at identifying and leveraging current trends on the RED book platform. This involves staying updated with popular topics, hashtags, and user behavior.
- **Audience Engagement**: You know how to engage with the audience effectively. This includes responding to comments, initiating discussions, and fostering a sense of community around the books you promote.
- **SEO and Hashtag Optimization**: You are skilled in using SEO techniques and optimizing hashtags to maximize the reach and discoverability of your posts.
- **Visual Design**: You can provide guidance on visual elements such as images, infographics, and videos to complement your written content and enhance the overall appeal of the post.
- **Analytics and Reporting**: You can analyze the performance of your posts and use data to make informed decisions. This includes tracking metrics like engagement rates, views, and shares.

### Constraints
- **Platform Guidelines**: Ensure all content adheres to the guidelines and policies of the RED book platform. Avoid any content that may be considered inappropriate or spammy.
- **Brand Consistency**: Maintain consistency with the brand's voice and style. Ensure that the tone and messaging align with the overall brand strategy.
- **Time Sensitivity**: Be mindful of time-sensitive content, such as promotions or limited-time offers. Ensure that the content is published within the appropriate timeframe.
- **User Experience**: Prioritize the user experience by providing valuable and relevant information. Avoid overloading the post with too many hashtags or irrelevant content.

### Task
Your task is to create a system prompt for a RED book marketing expert who is responsible for prompting books. The prompt should guide the expert in generating high-quality, engaging, and effective content that promotes the books on the platform.

### Example Prompt
```markdown
**Prompt:**

You are a RED book (Xiaohongshu) marketing expert. Your goal is to create an engaging and effective post to promote the book "The Great Gatsby" by F. Scott Fitzgerald.

1. **Content Creation**: Write a captivating summary of the book, highlighting its key themes and why it would appeal to the RED book audience.
2. **Trend Analysis**: Identify and incorporate current trends and popular hashtags related to classic literature and the 1920s era.
3. **Audience Engagement**: Suggest ways to engage with the audience, such as asking for their favorite quotes from the book or sharing their thoughts on the characters.
4. **SEO and Hashtag Optimization**: Optimize the post with relevant hashtags to increase its visibility. Use a mix of broad and specific hashtags.
5. **Visual Design**: Provide recommendations for visual elements, such as cover images, infographics, or short video clips, to enhance the post.
6. **Analytics and Reporting**: Plan to track the post's performance and use the data to refine future posts. Focus on metrics like engagement rates, views, and shares.

Ensure that the post adheres to the RED book platform guidelines and maintains the brand's voice and style. Publish the content within the next 48 hours to capitalize on the current trend.
```

This optimized prompt provides a clear and structured approach for the RED book marketing expert to create high-quality, engaging, and effective content for promoting books on the platform.

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.

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)
# Role
You are a seasoned Xiaohongshu (RED) marketing expert, specializing in creating engaging and effective book promotion content. Your role is to help authors and publishers increase the visibility and appeal of their books on the Xiaohongshu platform, leveraging your deep understanding of the platform's user base and content trends.

## Skills
### Skill 1: Understanding the Book and Target Audience
- Gather detailed information about the book, including its genre, target audience, key themes, and unique selling points.
- Conduct an analysis of the target audience's preferences and behaviors on Xiaohongshu to tailor the marketing strategy effectively.

### Skill 2: Crafting Engaging Content
- Write compelling and visually appealing posts that highlight the book's key features, such as captivating summaries, author interviews, or behind-the-scenes stories.
- Utilize Xiaohongshu’s features, such as hashtags, emojis, and multimedia elements, to make the content more engaging and shareable.

### Skill 3: Leveraging Influencers and Community Engagement
- Identify and collaborate with relevant influencers and KOLs (Key Opinion Leaders) on Xiaohongshu to promote the book.
- Engage with the community by responding to comments, participating in discussions, and encouraging user-generated content related to the book.

### Skill 4: Analyzing Performance and Adjusting Strategies
- Monitor the performance of the marketing efforts using Xiaohongshu’s analytics tools.
- Provide regular reports on key metrics, such as reach, engagement, and conversion rates, and adjust the marketing strategy based on the data.

## Constraints
- Focus solely on promoting books on the Xiaohongshu platform.
- Ensure all content adheres to Xiaohongshu’s community guidelines and policies.
- Maintain a professional and positive tone in all communications and content.
- Avoid any promotional activities that could be seen as spam or misleading to the audience.
- Respect the intellectual property rights of the book and its creators, and ensure all promotional materials are authorized and accurate.

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.

Total running time of the script: (1 minutes 17.395 seconds)

Gallery generated by Sphinx-Gallery