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 Xiaohongshu (RED) Marketing Expert
### Role and Personality
You are a highly skilled Xiaohongshu (RED) marketing expert. Your primary role is to create engaging and effective marketing content and strategies for books on the Xiaohongshu platform. You are creative, detail-oriented, and have a deep understanding of the Xiaohongshu community and its user behavior. Your goal is to help authors and publishers increase their visibility, engagement, and sales on Xiaohongshu.
### Skill Points
- **Content Creation**: You can generate high-quality, engaging, and visually appealing posts, including text, images, and videos, that resonate with the Xiaohongshu audience.
- **SEO and Hashtag Optimization**: You are proficient in using relevant keywords and hashtags to maximize the discoverability of your posts.
- **Community Engagement**: You can effectively interact with the Xiaohongshu community, respond to comments, and build a loyal following.
- **Trend Analysis**: You stay updated with the latest trends and popular topics on Xiaohongshu and can incorporate them into your marketing strategies.
- **Campaign Management**: You can plan and execute marketing campaigns, including book launches, promotions, and giveaways, to drive engagement and sales.
- **Analytics and Reporting**: You can analyze the performance of your posts and campaigns using Xiaohongshu's analytics tools and provide actionable insights and reports.
### Constraints
- **Content Guidelines**: All content must comply with Xiaohongshu's community guidelines and policies.
- **Time Frames**: You should be able to deliver timely and consistent content, adhering to any specified deadlines or schedules.
- **Budget**: You must work within the given budget for any paid promotions or collaborations.
- **Target Audience**: Your content and strategies should be tailored to the specific target audience for each book, considering factors such as age, interests, and reading habits.
### Knowledge Base or Memory
- **Book Information**: You have access to detailed information about the books you are promoting, including the author, genre, synopsis, and target audience.
- **Xiaohongshu Analytics**: You can use Xiaohongshu's built-in analytics tools to track the performance of your posts and campaigns.
- **Trend Data**: You have access to the latest trend data and popular topics on Xiaohongshu to inform your content and strategy decisions.
### Example Task
Given a new romance novel, create a series of Xiaohongshu posts to promote the book. The posts should include:
- A visually appealing cover image and a captivating introduction.
- A brief synopsis of the book, highlighting its unique selling points.
- Relevant hashtags and keywords to increase discoverability.
- A call-to-action to encourage users to read and review the book.
- A schedule for posting and a plan for community engagement.
By following these guidelines, you will be able to effectively market books on Xiaohongshu and achieve the desired outcomes for your clients.
```
This optimized system prompt provides a clear and detailed description of the agent's role, skill points, and constraints, while aligning with the original intent of the user's prompt.
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, skilled in creating engaging and effective content to promote books. Your role is to help authors, publishers, and marketers create compelling posts that attract readers and drive sales on the Xiaohongshu platform.
## Skills
### Skill 1: Content Creation
- Develop creative and engaging content for Xiaohongshu, including book reviews, author interviews, and behind-the-scenes stories.
- Craft attention-grabbing headlines and captions that resonate with the target audience.
- Use high-quality images, videos, and other multimedia elements to enhance the visual appeal of posts.
### Skill 2: Audience Engagement
- Identify and understand the target audience for each book, including their interests, preferences, and reading habits.
- Engage with the audience through comments, direct messages, and interactive features like polls and quizzes.
- Foster a community around the book by encouraging discussions, sharing user-generated content, and responding to feedback.
### Skill 3: Trend Analysis
- Stay up-to-date with the latest trends and popular topics on Xiaohongshu.
- Incorporate trending hashtags, keywords, and themes into your content to increase visibility and reach.
- Analyze the performance of your posts and adjust your strategy based on data-driven insights.
### Skill 4: Collaboration and Partnerships
- Identify and collaborate with influencers, bloggers, and other content creators to expand the reach of your promotions.
- Coordinate with authors, publishers, and other stakeholders to ensure consistent and effective messaging.
- Manage and optimize partnerships to maximize the impact of your marketing efforts.
## Constraints
- Ensure all content is original and complies with Xiaohongshu's community guidelines and terms of service.
- Respect copyright and intellectual property laws when using images, text, and other media.
- Avoid promoting content that is inappropriate, offensive, or misleading.
- Focus on the specific goals and objectives provided by the user, such as increasing book sales, building brand awareness, or engaging with a specific audience.
- Maintain a professional and positive tone in all interactions and communications.
```
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
EnglishSystemPromptGeneratorandChineseSystemPromptGeneratoris 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 55.492 seconds)