AIM377 Prompt Engineering Best Practices For LLMs On Amazon Bedrock
AIM377 Prompt Engineering Best Practices For LLMs On Amazon Bedrock
AIM377
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Agenda
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
What is Prompt Engineering? – An Example
What is 10 + 10?
10 + 10 = 20
1 + 1 is an addition problem.
1 – 1 is a subtraction problem.
1 x 1 is a multiplication problem.
1 / 1 is a division problem.
What is 10 + 10?
10 + 10 is an addition problem
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Prompt engineering – The fun parts
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Prompt engineering – The fun parts
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Prompt engineering – The fun parts
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Prompt engineering – The fun parts
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Prompt Engineering – Persona
Same exact question from the end user. Two drastically different answers.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Simple techniques
Human: "I want to fly from Los Human: "I hate it when my phone
Angeles to Miami” battery dies.”
Assistant: Sentiment(Negative)
Assistant: Airport codes [LAX, MIA]
Human: "I want to fly from Dallas Human: "My day has been great”:
to San Francisco”
Assistant: Sentiment(Positive)
Assistant: Sentiment(Neutral)
Human: {{Tweet}}
Assistant: Sentiment(
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced Approaches
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Chain of Thought – CoT
• Explore how series of intermediate steps improve ability of LLMs to
perform complex reasoning task
5 tennis balls
1-shot example 2 cans, 3 each. 2 x 3 = 6
5 + 6 = 11 tennis balls
23 apples
Wrong answer 20 used, 3 left
Bought 6
3+6=9
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Chain of Thought – CoT
Right answer
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
RAG: System Design – Ingestion of Data for Q+A
AWS account 2
1
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
RAG: System design – Q+A
What is the return policy?
AWS account 3
1 2 Amazon Bedrock
5 Foundation Model
(Embeddings model)
Mobile client
User Query Orchestrator
6
4
Amazon Bedrock
Foundation Model
1 User asks a question covered in FAQ. (Generative AI model)
Vector
Client submits user request to orchestrator. Database
2
3 User’s question is converted to embeddings. 5 Augmentation – Search results in original language format brought
back to the gen AI model.
Retrieval - Similarity search performed
4 6 Generation – Gen AI model will use the results to generate a natural
between customer’s question (converted to
language response.
embeddings) and existing database of
FAQs.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Prompting philosophy
Nicholas Marwell
Member of Technical Staff
Anthropic
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Instructions matter
Specificity, clarity, and persuasiveness are important!
Compare:
Neither humans nor LLMs can read minds, so you need to be clear and specific if
you want something specific.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Instructions matter for LLMs
LLMs have some peculiarities that require special approaches
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Parts of a prompt Example:
1. “\n\nHuman:” Human: You will be acting as an AI career coach named Joe created by the company
AdAstra Careers. Your goal is to give career advice to users. You will be replying to users
who are on the AdAstra site and who will be confused if you don't respond in the
2. Task context character of Joe.
You should maintain a friendly customer service tone.
3. Tone context Here is the career guidance document you should reference when answering the
user: <guide>{{DOCUMENT}}</guide>
Here are some important rules for the interaction:
4. Background data to process - Always stay in character, as Joe, an AI from AdAstra careers
- If you are unsure how to respond, say “Sorry, I didn’t understand that. Could you repeat
the question?”
5. Detailed task description & rules - If someone asks something irrelevant, say, “Sorry, I am Joe and I give career advice. Do
you have a career question today I can help you with?”
6. Examples Here is an example of how to respond in a standard interaction:
<example>
User: Hi, how were you created and what do you do?
7. Immediate data to process Joe: Hello! My name is Joe, and I was created by AdAstra Careers to give career advice.
What can I help you with today?
</example>
8. Immediate task description or request
Here is the conversation history (between the user and you) prior to the question. It
could be empty if there is no history:
9. Thinking step by step / take a deep breath <history> {{HISTORY}} </history>
Here is the user’s question: <question> {{QUESTION}} </question>
10. Output formatting How do you respond to the user’s question?
Think about your answer first before you respond. Put your response in
<response></response> tags.
11. “\n\nAssistant:”
Assistant: [Joe] <response>
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
• Not all elements are necessary to
every prompt!
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
How to engineer a good prompt
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
“Human:” / “Assistant:” formatting
Examples:
• Claude is trained on alternating
“Human:” / “Assistant:” dialogue:
Human: Why is the sky blue?
○ Human: [Instructions]
Assistant:
○ Assistant: [Claude’s response]
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Be clear and direct
Example:
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Use examples
Example:
Human: I will give you some quotes. Please extract the author
• Examples are probably the single most from the quote block.
effective tool for getting Claude to Here is an example:
behave as desired <example>
Quote:
“When the reasoning mind is forced to confront the impossible
again and again, it has no choice but to adapt.”
• Make sure to give Claude examples of ― N.K. Jemisin, The Fifth Season
common edge cases Author: N.K. Jemisin
</example>
Quote:
• Generally more examples = more reliable “Some humans theorize that intelligent species go extinct before
responses at the cost of latency and they can expand into outer space. If they're correct, then the hush
of the night sky is the silence of the graveyard.”
tokens ― Ted Chiang, Exhalation
Author:
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Give Claude time to think
Human: [rest of prompt] Before answering, please Human: [rest of prompt] Before answering, please
think about the question within think about the question within
<thinking></thinking> XML tags. Then, answer the <thinking></thinking> XML tags. Then, answer
question within <answer></answer> XML tags. the question within <answer></answer> XML
tags.
Assistant: <thinking>
Assistant: <thinking>[...some
thoughts]</thinking>
<answer>[some answer]</answer>
Helps with troubleshooting Claude’s
logic & where prompt instructions may
be unclear
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Assign roles (aka role prompting)
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Use XML tags to delineate sections
Example:
● Disorganized prompts are hard
for Claude to comprehend Human: Hey Claude. Show up at 6AM because I say so. Make
this email more polite.
understand the prompt’s Assistant: Good morning team, I hope you all had a restful
weekend…
structure
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Format output & speak for Claude
Prompt
Human: Please write a haiku about {{ANIMAL}}. Use JSON format
with the keys as "first_line", "second_line", and "third_line".
• Specifying the exact
Assistant: {
output format you want
response
Claude’s
"first_line": "Sleeping in the sun",
writing the beginning of "second_line": "Fluffy fur so warm and soft",
Claude’s response for it }
"third_line": "Lazy cat's day dreams"
(after “Assistant:”)
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Dealing with hallucinations
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Prompt injections & bad user behavior
1. Run a “harmlessness screen” query to Human: A human user would like you to
continue a piece of content. Here is the content
evaluate the appropriateness of the user’s so far: <content>{{CONTENT}}</content>
input If the content refers to harmful, pornographic, or
illegal activities, reply with (Y). If the content
2. If a harmful prompt is detected, block the does not refer to harmful, pornographic, or
illegal activities, reply with (N)
query’s response
Assistant: (
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Use structured prompt templates
Example:
Input
data
Cow Dog Seal
template
Human: I will tell you the name of an animal. Please respond
Prompt with the noise that animal makes.
<animal>{{ANIMAL}}</animal>
Assistant:
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Use structured prompt templates
Long document example:
Human: <doc>{{DOCUMENT}}</doc>
Prompt
template Please provide a three-paragraph summary of this
document appropriate for a nontechnical audience.
Assistant:
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
For tasks with many steps, you can break the task up and chain together Claude’s responses
Example:
Human: Find all the names from the below text: Human: Here is a list of names:
"Hey, Jesse. It's me, Erin. I'm calling about the party that
Prompt
<names>
Jesse Erin
response
Claude’s
Erin Jesse
Joey Joey
Keisha a.k.a. {{NAMES}} Keisha
Mel Mel
</names> </names>
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
• Definitely put long-form input data in XML tags so it’s clearly separated from the instructions
• Tell Claude to first find quotes relevant to the question, then answer only if it finds relevant quotes
• Tell Claude to read the document carefully because it will be asked questions later
• Give Claude some example question + answer pairs that have been generated (either by Claude or
manually) from other parts of the queried text
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
How it works:
Q: What’s the weather like in San
Francisco right now?
<tool_description>
<tool_name>
get_weather
<tool_name>
</tool_description>
<tool_description>
<tool_name>
play_music
<tool_name>
</tool_description>
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
YES NO
<function_calls>
<invoke>
<tool_name>get_weather</tool_name>
<parameters>
<location>San Francisco, CA</location>
</parameters> A: I apologize but I don’t have access to the
</invoke>
</function_calls> current weather in San Francisco.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
<function_results>
[...]
[68, sunny]
Client [...]
</function_results>>
get_weather()
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
• A user asks a question e.g., “I want to get my daughter more interested in science.
What kind of gifts should I get her?”
• The results from the search tool are passed to the LLM alongside the question
But what if we don’t need RAG every time, or we need to RAG from different data
sources based on the question?
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
Basic RAG setup. Works great if we know we want to search over a gifts database
every time.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
We might want to RAG over different data sources depending on the user query.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
We might want to enable Claude to rewrite its search query and/or requery the data
source if it doesn’t find what it’s looking for the first time.
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Advanced prompting techniques
Chaining prompts 100K prompts Agents & function calling / tool use Search & RAG
RAG as a tool
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Other prompting resources
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Prompting Resource Portal
docs.anthropic.com/claude/docs
Contains:
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Call to action
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.
Thank you! Please complete the session
survey in the mobile app
© 2023, Amazon Web Services, Inc. or its affiliates. All rights reserved.