Writing effective prompts for LLMs

Requires some experimenting but

Page content

Still there are some common approaches how to write good prompts so LLM would not get confused trying to understand what you wand from it.

Human describing complex idea to a robot

How to Improve Language Model Performance Using Prompt Engineering Techniques

Prompt engineering is a powerful technique used in natural language processing (NLP) to enhance the performance of language models by providing them with additional context, guidance, and information about the task at hand. This article will guide you through various methods for crafting effective prompts that can help your NLP model generate more accurate, relevant, and diverse outputs.

Crafting Effective Prompts

1. Be Clear and Concise

  • Objective: Ensure your prompt is easily understandable by the language model.
  • Implementation: Avoid using jargon or technical terms that might confuse the model. Keep instructions straightforward and to the point.

2. Use Specific Examples

  • Objective: Help the model better understand what kind of output is expected.
  • Implementation: Include specific examples in your prompt, such as setting, characters, or plot details for story generation tasks.

3. Vary the Prompts

  • Objective: Enable the model to learn more about the task and produce diverse outputs.
  • Implementation: Experiment with different styles, tones, and formats to see how the model responds.

4. Test and Refine

  • Objective: Validate prompt effectiveness and make necessary adjustments.
  • Implementation: After creating a set of prompts, test them on your language model. Analyze results and refine prompts based on feedback or performance metrics.

5. Use Feedback

  • Objective: Continuously improve the quality and relevance of your prompts.
  • Implementation: Incorporate user feedback or insights from other sources to enhance prompt effectiveness over time.

Explicit Instructions for Better Results

Example:

  • Prompt: Explain the latest advances in large language models to me.
  • Instructions: Always cite your sources, never older than 2020.

Stylization for Elementary Students

Explanation: Present complex concepts using simple language and relatable examples suitable for children’s educational networks.

Formatting Guidelines

Example:

{
  "role": "sentiment_classifier",
  "instructions": [
    "For each message, provide the percentage of positive, neutral, and negative sentiment.",
    "Examples: 'I liked it' -> 70% positive, 30% neutral; 'It could be better' -> 0% positive, 50% neutral, 50% negative"
  ],
  "examples": [
    {
      "message": "I thought it was okay",
      "sentiment": [25%, 50%, 25%]
    },
    {
      "message": "I loved it!",
      "sentiment": [100%, 0%, 0%]
    }
  ]
}

Role-Based Prompts

Example:

  • Prompt: You are a virtual tour guide currently walking the tourists around the Eiffel Tower on a night tour. Describe the Eiffel Tower to your audience, covering its history, number of people visiting each year, amount of time it takes for a full tour, and why so many people visit this place annually.

Chain of Thought Technique

Example:

  • Prompt: You are a virtual tour guide from 1901. Your tourists are visiting the Eiffel Tower.
  1. Why was it built?
  2. How long did it take to build?
  3. Where were the materials sourced for its construction?
  4. What is the annual number of visitors in the 1900s?
  5. How much time does a full tour take, and why do so many people visit this place each year?

Self-Consistency

Example:

  • Prompt: John found that the average of 15 numbers is 40. If 10 is added to each number, what is the new mean?
  • Instructions: Run the prompt several times and select the most commonly returned value for the answer.

Retrieval-Augmented Generation

Example:

Given the following information about temperatures in Menlo Park:

  • 2023-12-11: 52 degrees Fahrenheit

  • 2023-12-12: 51 degrees Fahrenheit

  • 2023-12-13: 55 degrees Fahrenheit

  • Prompt: What was the temperature in Menlo Park on December 12, 2023?

Program-Aided Language Models

Example:

  • Prompt: Calculate: ((-5 + 93 * 4 - 0) * (4^4 + -7 + 0 * 5))

Limiting Extraneous Tokens

Example:

  • Prompt: You are a robot that only outputs JSON.
  • Instructions: Your reply must include the field ‘zip_code’.
  • Example Question: What is the zip code of Menlo Park?
  • Expected Answer: {“zip_code”: “94025”}

Reducing Hallucinations

Example 1:

  • Prompt: Explain the concept of quantum computing to a high school student.
  • Instructions: Provide simple explanations and avoid complex jargon.

Example 2:

  • Prompt: Write an email to your boss requesting a meeting about project progress.
  • Instructions: Use professional language, maintain a formal tone, and include specific details about the project status.

Example 3:

  • Prompt: Generate a poem about nature.
  • Instructions: Focus on descriptive language and imagery related to natural elements.

Conclusion

By following these guidelines for prompt engineering, you can significantly enhance the performance of your NLP models. Whether it’s through clear instructions, specific examples, or role-based prompts, each technique plays a crucial role in guiding the model towards more accurate and relevant outputs.