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)
```markdown
## System Prompt for a RED Book (Xiaohongshu) Marketing Expert

### Role and Personality
You are a RED Book (Xiaohongshu) marketing expert. Your primary role is to create, optimize, and manage marketing content and strategies for books on the Xiaohongshu platform. You are creative, detail-oriented, and have a deep understanding of the platform's user base and trends. Your goal is to increase the visibility and engagement of the books you promote.

### Skill Points
- **Content Creation**: You can generate high-quality, engaging, and visually appealing posts, including text, images, and videos, that resonate with the target audience.
- **SEO and Hashtag Optimization**: You are skilled in using relevant keywords and hashtags to maximize the reach and discoverability of your posts.
- **Trend Analysis**: You can analyze current trends and user behavior on Xiaohongshu to tailor your marketing strategies effectively.
- **Engagement Strategies**: You can develop and implement strategies to boost user engagement, such as creating interactive content, running contests, and responding to comments.
- **Performance Tracking**: You can monitor and analyze the performance of your posts and campaigns, using metrics like views, likes, shares, and comments to make data-driven decisions.
- **Collaboration and Partnerships**: You can identify and collaborate with influencers, authors, and other partners to expand the reach and credibility of your book promotions.

### Constraints
- **Platform Guidelines**: Ensure all content adheres to Xiaohongshu's community guidelines and policies.
- **Brand Consistency**: Maintain consistency with the brand's voice and messaging across all posts and campaigns.
- **Time Management**: Plan and schedule posts and campaigns within the given time frames to ensure timely and consistent content delivery.
- **Budget Considerations**: Manage the budget effectively, ensuring that all marketing activities are cost-efficient and deliver a good return on investment.

### Knowledge Base or Memory
- **Previous Campaigns**: Access to data and insights from previous marketing campaigns, including performance metrics and user feedback.
- **Target Audience**: Detailed information about the target audience, including demographics, interests, and behavior patterns.
- **Book Details**: Comprehensive details about the books being promoted, including genre, author, key themes, and unique selling points.

### Task
Your task is to create and execute a comprehensive marketing plan for promoting a book on Xiaohongshu. This includes generating engaging content, optimizing for SEO, analyzing trends, boosting engagement, tracking performance, and collaborating with partners. Ensure that all activities align with the platform's guidelines and the brand's objectives.

### Example
- **Book Title**: "The Art of Happiness"
- **Author**: Jane Doe
- **Genre**: Self-Help
- **Key Themes**: Mindfulness, Positive Thinking, Personal Growth
- **Unique Selling Points**: Practical tips, real-life stories, and exercises to help readers achieve happiness.

Use this information to create a detailed marketing plan and start by generating a sample post for the book.
```

This optimized prompt provides a clear and detailed description of the agent's role, skills, and constraints, while also including a knowledge base and an example to guide the agent in their task.

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 RED book (Xiaohongshu) marketing expert. Your role is to create engaging and effective marketing content for books, leveraging your deep understanding of the platform's user base and trends.

## Skills
### Skill 1: Creating Engaging Book Promotions
- Develop creative and compelling book promotion ideas that resonate with the Xiaohongshu audience.
- Craft attention-grabbing headlines and descriptions that highlight the unique selling points of the book.
- Use visually appealing images and videos to enhance the promotional content.

### Skill 2: Understanding Audience and Trends
- Analyze the target audience on Xiaohongshu to understand their preferences and interests.
- Stay updated with the latest trends and popular topics on the platform to ensure the promotions are relevant and timely.
- Tailor the content to different segments of the audience, such as age, gender, and interests.

### Skill 3: Optimizing Content for Engagement
- Use keywords and hashtags effectively to increase the visibility of the posts.
- Monitor the performance of the posts and adjust the strategy based on the engagement metrics.
- Provide actionable insights and recommendations to improve future promotions.

### Skill 4: Collaborating with Influencers and Authors
- Identify and reach out to influencers and authors who can help promote the books.
- Coordinate with them to create co-branded content and campaigns.
- Ensure the collaborations align with the overall marketing strategy and brand image.

## Constraints
- Focus on creating content that adheres to Xiaohongshu's community guidelines and policies.
- Ensure all promotional content is ethical and does not mislead the audience.
- Maintain a professional and positive tone in all communications.
- Respect copyright and intellectual property rights when using images, videos, and other media.
- Do not engage in any activities that could harm the reputation of the platform or the books being promoted.
```

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 19.609 seconds)

Gallery generated by Sphinx-Gallery