高级提示策略

Amazon Bedrock 入门

Nikhil Rangarajan

Data Scientist

提示架构

  • 系统提示设定风格与方法
  • 链式思维提示提供解题框架
  • 少样本提示提供示例
  • 逐层构建理解,提升可靠性

一个包含四个相连齿轮的图表,展示提示架构顺序:系统提示、链式思维步骤、少样本示例和内容请求。

Amazon Bedrock 入门

系统提示与风格控制

prompt_template = {
    "system_prompt": f"""System: Write in a professional but approachable tone.""",

"content_type": "blog_post",
"examples": [ { "title": "Cloud Migration Basics", "content": "Moving to the cloud doesn't have to be complex..." } ],
"task": f"Write a blog post about {topic}"
}
Amazon Bedrock 入门

链式思维提示

def generate_message_content(text_data):
    steps = [
        "1. Understand the target audience",
        "2. Outline key points",
        "3. Draft a promotional email"
    ]

text_data["text"] += f"\nSteps:{' '.join(steps)}"
return text_data
  • 💡 引导模型的思维过程
Amazon Bedrock 入门

内容适配与参数

parameters = {

"blog_post": {"temperature": 0.7, "top_p": 0.9}, "social_media": {"temperature": 0.8, "top_p": 0.95},
"technical": {"temperature": 0.4, "top_p": 0.8}
}
content_type = "blog_post" prompt_data["parameters"] = parameters[content_type]
  • 按内容类型调整参数
  • 预设参数保证一致性
Amazon Bedrock 入门

使用模板生成提示

# Write the prompt
topic = "Cloud Security"
content_type = "blog_post"
prompt = f"Write a {topic} blog post. Use this example: {prompt_template["examples"][0]}"


# Define the parameters temperature = parameters[content_type]["temperature"] top_p = parameters[content_type]["top_p"]
# Call the model response = bedrock.invoke_model(modelId=model_id, body=json.dumps({"messages": [{"role": "user","content": [{"type": "text", "text": prompt}]}], "temperature": temperature, "top_p": top_p}))
Amazon Bedrock 入门

Passons à la pratique !

Amazon Bedrock 入门

Preparing Video For Download...