0
0
GenaiHow-ToBeginner · 4 min read

How to Use Examples in Prompts for Better AI Responses

To use examples in prompts, include clear input-output pairs that show the AI what you want it to do. This helps the model understand the task better and produce accurate results by following the pattern you provide.
📐

Syntax

Using examples in prompts means giving the AI a few sample inputs and their correct outputs before asking it to generate a new output. This is often called few-shot prompting.

The basic syntax looks like this:

  • Example 1 Input: The first sample input.
  • Example 1 Output: The correct output for the first input.
  • Example 2 Input: The second sample input.
  • Example 2 Output: The correct output for the second input.
  • New Input: The input you want the AI to answer.
  • New Output: The AI's response based on the examples.

This pattern teaches the AI how to respond by showing it examples first.

plaintext
prompt = '''
Input: Translate 'hello' to French.
Output: bonjour

Input: Translate 'thank you' to French.
Output: merci

Input: Translate 'goodbye' to French.
Output: '''
💻

Example

This example shows how to use examples in a prompt to translate English words to French. The AI sees two examples and then translates a new word.

python
def generate_prompt(word):
    prompt = f"""
Input: Translate 'hello' to French.
Output: bonjour

Input: Translate 'thank you' to French.
Output: merci

Input: Translate '{word}' to French.
Output:"""
    return prompt

# Example usage
word_to_translate = 'goodbye'
prompt_text = generate_prompt(word_to_translate)
print(prompt_text)
Output
Input: Translate 'hello' to French. Output: bonjour Input: Translate 'thank you' to French. Output: merci Input: Translate 'goodbye' to French. Output:
⚠️

Common Pitfalls

Common mistakes when using examples in prompts include:

  • Using unclear or inconsistent examples that confuse the AI.
  • Providing too few or too many examples, which can underfit or overwhelm the model.
  • Not matching the format exactly between examples and the new input.
  • Forgetting to leave the output blank for the new input so the AI knows to generate it.

Here is a wrong and right way to write examples:

plaintext
# Wrong way: Missing output label for new input
wrong_prompt = '''
Input: Add 2 + 2.
Output: 4

Input: Add 3 + 5.
Output: 8

Input: Add 7 + 1.
'''

# Right way: Output label included for AI to complete
right_prompt = '''
Input: Add 2 + 2.
Output: 4

Input: Add 3 + 5.
Output: 8

Input: Add 7 + 1.
Output: '''
📊

Quick Reference

Tips for using examples in prompts:

  • Keep examples clear and consistent in format.
  • Use 2-5 examples to guide the AI well.
  • Always leave the output blank for the new input.
  • Match the style and wording exactly between examples and new input.
  • Test and adjust examples if the AI output is off.

Key Takeaways

Use clear input-output pairs as examples to guide AI responses.
Keep example formats consistent and leave new output blank for AI to complete.
Provide a few examples (2-5) to balance guidance without overload.
Avoid mixing formats or unclear examples to prevent confusing the AI.
Test prompts and refine examples for best results.