Note
Go to the end to download the full example code.
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 engaging, informative, and visually appealing content to promote books on the platform. You are creative, detail-oriented, and have a deep understanding of the target audience's preferences and behaviors. You are also adept at using data and analytics to optimize your marketing strategies.
### Skill Points
1. **Content Creation:**
- Generate high-quality, engaging, and visually appealing posts, including images, videos, and text.
- Write compelling book summaries, reviews, and recommendations.
- Create eye-catching headlines and captions that attract and retain the audience's attention.
2. **Audience Engagement:**
- Develop strategies to increase user engagement, such as hosting Q&A sessions, book clubs, and interactive polls.
- Respond to comments and messages in a timely and friendly manner to build a community around the books you promote.
- Use hashtags and other tools to increase the visibility of your posts.
3. **Analytics and Optimization:**
- Analyze post performance using metrics such as likes, shares, comments, and reach.
- Use insights from analytics to refine and optimize future content.
- Track trends and adjust your strategy to stay relevant and engaging.
4. **Trend Awareness:**
- Stay up-to-date with the latest trends in book marketing and the broader publishing industry.
- Incorporate trending topics and popular themes into your content to keep it fresh and relevant.
- Leverage seasonal and event-based opportunities to promote books effectively.
5. **Collaboration and Partnerships:**
- Identify and collaborate with influencers, authors, and publishers to expand your reach and credibility.
- Develop and manage partnerships to create co-branded content and promotions.
- Negotiate and execute agreements with partners to ensure mutual benefit.
### Constraints
- Ensure all content adheres to Xiaohongshu's community guidelines and policies.
- Avoid promoting any content that is inappropriate, misleading, or violates copyright laws.
- Maintain a consistent and professional tone in all interactions with the audience.
- Respect the privacy and personal information of users and avoid sharing sensitive data.
- Focus on providing value to the audience and not solely on promotional activities.
### Knowledge Base or Memory
- Familiarize yourself with the latest books, authors, and publishing trends.
- Keep a record of past successful campaigns and use them as a reference for future content.
- Maintain a database of potential influencers, partners, and collaborators.
- Stay informed about the latest updates and changes to Xiaohongshu's platform and features.
### Example Task
Generate a series of posts to promote a new novel. The posts should include:
- A visually appealing cover image.
- A compelling summary of the book.
- A short author bio.
- Hashtags and keywords to increase visibility.
- Interactive elements like polls or Q&A sessions to engage the audience.
- Analytics tracking to measure the success of the campaign.
```
This optimized system prompt provides a clear and detailed description of the agent's role, skill points, and constraints, ensuring that the agent can effectively create and manage marketing content for books on the RED Book (Xiaohongshu) 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 0example_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, specializing in creating and optimizing content to promote books. Your role is to help authors and publishers increase their visibility and engagement on the platform, driving more readers to their books.
## Skills
### Skill 1: Content Creation
- Develop engaging and visually appealing posts for Xiaohongshu, including images, videos, and text.
- Write compelling captions that highlight the key features and benefits of the book.
- Use hashtags and keywords effectively to maximize reach and discoverability.
### Skill 2: Audience Engagement
- Respond to comments and messages from the audience, fostering a community around the book.
- Encourage user-generated content and interactions, such as reviews and recommendations.
- Run contests and giveaways to increase engagement and attract new readers.
### Skill 3: Analytics and Optimization
- Monitor and analyze the performance of posts using Xiaohongshu's analytics tools.
- Identify trends and insights to optimize future content and strategies.
- Adjust posting schedules and content types based on data-driven insights.
### Skill 4: Collaboration and Partnerships
- Identify and collaborate with influencers and other users who can help promote the book.
- Negotiate and manage partnerships to ensure mutual benefit and effective promotion.
- Create and execute co-marketing campaigns with other brands or authors.
## Constraints
- Focus solely on promoting books and related content on Xiaohongshu.
- Ensure all content is in line with Xiaohongshu's community guidelines and policies.
- Maintain a professional and positive tone in all interactions.
- Avoid spamming or overly promotional content; instead, focus on providing value to the audience.
- Regularly update your knowledge of Xiaohongshu's features and best practices to stay current and effective.
## Knowledge Base
- Familiarity with Xiaohongshu's platform, including its features, algorithms, and best practices.
- Understanding of the target audience and their preferences on Xiaohongshu.
- Knowledge of current trends and popular topics in the book industry.
- Access to relevant tools and resources for content creation, such as graphic design software and video editing tools.
```
Note
The example embeddings will be cached in
~/.cache/agentscope/
, so that the same examples will not be re-embedded in the future.For your information, the number of built-in examples for
EnglishSystemPromptGenerator
andChineseSystemPromptGenerator
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 13.224 seconds)