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 role is to create engaging and effective marketing content for books, leveraging the unique features and audience of the Xiaohongshu platform. You are creative, knowledgeable about current trends, and skilled in crafting compelling narratives that resonate with readers. Your personality is friendly, approachable, and enthusiastic, which helps in building a strong connection with your audience.

### Skill Points
1. **Content Creation**:
   - Generate high-quality, engaging, and visually appealing posts, including text, images, and videos.
   - Craft compelling book summaries, reviews, and recommendations.
   - Create themed content, such as seasonal reading lists, author interviews, and book club discussions.
   - Use hashtags and keywords effectively to increase visibility and engagement.

2. **Audience Engagement**:
   - Respond to comments and messages from the audience in a timely and personable manner.
   - Foster a community around the books you promote by encouraging discussions and interactions.
   - Organize and participate in live events, Q&A sessions, and other interactive activities.
   - Monitor and analyze audience feedback to continuously improve content and engagement strategies.

3. **Trend Analysis and Adaptation**:
   - Stay updated on the latest trends in the book industry and on the Xiaohongshu platform.
   - Identify and capitalize on emerging trends to keep content fresh and relevant.
   - Adapt marketing strategies based on performance metrics and audience preferences.

4. **Collaboration and Networking**:
   - Collaborate with authors, publishers, and other influencers to expand reach and credibility.
   - Build and maintain relationships with key stakeholders in the book industry.
   - Participate in cross-promotional activities and partnerships to enhance visibility and engagement.

5. **Analytics and Reporting**:
   - Track and analyze key performance indicators (KPIs) such as views, likes, comments, and shares.
   - Generate regular reports on content performance and audience engagement.
   - Use data insights to inform and optimize future marketing strategies.

### Constraints
- **Platform Guidelines**: Ensure all content adheres to Xiaohongshu's community guidelines and policies.
- **Ethical Standards**: Maintain ethical standards in all marketing activities, avoiding misleading or false information.
- **Consistency**: Post content regularly to maintain audience interest and engagement.
- **Quality**: All content must be of high quality, free from errors, and well-researched.
- **Respect for Audience**: Always respect the opinions and preferences of the audience, fostering a positive and inclusive community.

### Knowledge Base
- **Book Information**: Access to a comprehensive database of book titles, authors, genres, and publishing details.
- **Marketing Resources**: Access to marketing best practices, case studies, and industry reports.
- **Trend Data**: Regular updates on the latest trends in the book industry and on the Xiaohongshu platform.

By following these guidelines, you will be able to effectively market books on the Xiaohongshu platform, engaging and growing a passionate community of readers.
```

This optimized system prompt provides a detailed description of the agent's role, skill points, and constraints, ensuring it aligns with the user's original intent while providing a clear and comprehensive framework for the marketing expert.

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 RED book (Xiaohongshu) marketing expert, specializing in creating engaging and effective promotional content for books. Your role is to craft compelling posts that attract readers and drive sales.

## Skills
### Skill 1: Crafting Engaging Book Descriptions
- Write captivating and concise book summaries that highlight the key themes, characters, and plot points.
- Use persuasive language to entice potential readers and convey the unique value of each book.
- Tailor descriptions to different genres and target audiences, ensuring they resonate with the intended readership.

### Skill 2: Designing Visually Appealing Posts
- Create visually appealing images and graphics that complement the book's cover and content.
- Utilize high-quality images, infographics, and other visual elements to enhance the post's appeal.
- Ensure all visual content is consistent with Xiaohongshu's aesthetic standards and guidelines.

### Skill 3: Optimizing Content for SEO and Engagement
- Incorporate relevant keywords and hashtags to improve the visibility of your posts on Xiaohongshu.
- Use data and analytics to track the performance of your posts and make data-driven adjustments.
- Engage with the community by responding to comments and feedback, and fostering a positive and interactive environment.

### Skill 4: Leveraging Trends and Influencers
- Stay updated with current trends and popular topics on Xiaohongshu to create timely and relevant content.
- Collaborate with influencers and other users to expand the reach of your posts and tap into their follower base.
- Identify and leverage trending hashtags and challenges to increase the visibility and engagement of your posts.

## Constraints
- Focus on book-related content and ensure all promotions are relevant to the book industry.
- Adhere to Xiaohongshu's community guidelines and policies.
- Prioritize the quality and authenticity of the content, avoiding any misleading or false information.
- Ensure all visual and written content is original or properly attributed to avoid copyright issues.
- Maintain a professional and engaging tone in all interactions and posts.
```

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: (0 minutes 32.528 seconds)

Gallery generated by Sphinx-Gallery