Beginners Guide to Prompt Engineering!!

Sravanth
9 min readNov 17, 2023

--

Introduction

Welcome to the Future of AI Interaction: Mastering the Art of Prompt Engineering”

In an age where artificial intelligence is not just a buzzword but a pivotal part of technological advancement, the role of prompt engineering emerges as a cornerstone in maximizing the potential of large language models (LLMs). Prompt engineering, a relatively new discipline, stands at the forefront of innovation, unlocking the vast capabilities of large language models (LLMs) for diverse applications and research domains.

At its core, prompt engineering is about developing and optimizing prompts to efficiently harness the power of LMs. This skill is crucial for understanding both the capabilities and limitations of LLMs, shaping the way researchers and developers interact with these sophisticated AI systems.

This guide introduces you to the realm of prompt engineering helping you explore the nuances of prompt engineering, a skill that’s not just about crafting prompts but about building a more intuitive and effective interface with the AI of tomorrow.

Before We Start: Understanding LLM Settings

As we embark on the journey of prompt engineering, it’s crucial to familiarize ourselves with the settings of Large Language Models (LLMs). These settings are fundamental in tailoring the behavior of LLMs to suit specific tasks and objectives. Here’s a breakdown of the key parameters:

1. Temperature

  • What It Is: Temperature controls the predictability of responses. A lower temperature yields more predictable, factual responses, while a higher temperature encourages creativity and diversity in the output.
  • Application: Use lower temperatures for fact-based tasks like Q&A, and higher temperatures for creative tasks like poetry.

2. Top_p (Nucleus Sampling)

  • What It Is: Top_p, combined with temperature, dictates the randomness in response generation.
  • Application: Set it lower for precise, factual answers, and higher for varied, creative responses.

3. Max Length

  • Purpose: This limits the number of tokens generated, helping to keep responses concise and relevant.
  • Use Case: Adjust it to control response length and manage costs.

4. Stop Sequences

  • Function: These are strings that signal the model to stop generating further tokens.
  • Example: To limit a list to 10 items, use “11” as a stop sequence.

5. Frequency Penalty

  • Role: It penalizes token repetition based on frequency, reducing word redundancy.
  • Application: Increase it to diversify word usage in responses.

6. Presence Penalty

  • Operation: This applies a uniform penalty to repeated tokens, preventing phrase repetition.
  • Strategy: Adjust it based on whether you need more creative text or focused content.

Note: Generally, it’s advised to modify either temperature or top_p, and frequency or presence penalty, but not both simultaneously.

Elements of a Prompt

A prompt contains any of the following elements:

Instruction — a specific task or instruction you want the model to perform

Context — external information or additional context that can steer the model to better responses

Input Data — the input or question that we are interested to find a response for

Output Indicator — the type or format of the output.

You do not need all the four elements for a prompt and the format depends on the task at hand. We will touch on more concrete examples in upcoming guides.

Basics of Prompting with Large Language Models (LLMs)

Prompting is a fundamental aspect of interacting with LLMs, where the effectiveness of the results heavily relies on the quality and structure of the prompt. A well-crafted prompt typically includes instructions or questions, and can also incorporate context, inputs, or examples to guide the model more precisely.

Basic Prompt Example

Consider a simple prompt:

Prompt: “The sky is”

Output:

“blue”

or

“The sky is blue on a clear day…”

This illustrates how the model continues the prompt based on the given context. However, the output might not always align with the intended task, highlighting the need for more specific prompts.

Improved Prompt Example

By refining the prompt, we can achieve more targeted responses:

Prompt: “Complete the sentence: The sky is”

Output: “so beautiful today.”

This change results in an output that closely follows the given instruction, demonstrating the essence of prompt engineering.

Prompt Engineering

Prompt engineering involves designing prompts that effectively guide the model to perform specific tasks. This can range from text summarization to code generation. Two common formats for prompts are:

  1. Basic Format:

Question or Instruction (e.g., “The sky is?”)

2. Question Answering (QA) Format:

Standard in many QA datasets:

Q: “What color is the sky?”

A: [Model’s response]

General Tips for Designing Prompts

Here are some tips to keep in mind while you are designing your prompts:

Start Simple

As you get started with designing prompts, you should keep in mind that it is really an iterative process that requires a lot of experimentation to get optimal results.

You can start with simple prompts and keep adding more elements and context as you aim for better results. Iterating your prompt along the way is vital for this reason.

When you have a big task that involves many different subtasks, you can try to break down the task into simpler subtasks and keep building up as you get better results. This avoids adding too much complexity to the prompt design process at the beginning.

The Instruction

Design effective prompts by using clear instructions such as "Write", "Classify", "Summarize", "Translate", "Order", etc. Experiment with different keywords and contexts to discover what works best for your task. The more specific the context, the better the results.

### Instruction ###
Translate the text below to Spanish:
Text: "hello!"

Output:
¡Hola!

Specificity

Be specific and descriptive in your prompts. Avoid unnecessary details; focus on what's relevant to the task. Use examples to guide the model for specific outputs.

Extract the name of places in the following text.
Desired format:
Place: <comma_separated_list_of_company_names>
Input: "Although these developments are encouraging to researchers...
in Lisbon."

Output:
Place: Champalimaud Centre for the Unknown, Lisbon

Avoid Impreciseness

Be specific and direct in your prompts to avoid imprecision. The clearer the instruction, the more effective the response.

Use 2-3 sentences to explain the concept of prompt engineering to a 
high school student.

To Do or Not to Do?

Focus on what the model should do, rather than what it should not. This encourages specificity and clarity in responses.

The following is an agent that recommends movies to a customer. 
The agent should recommend a movie from the top global trending movies,
avoiding asking for preferences or personal information.
If unable to recommend, respond "Sorry, couldn't find a movie to recommend today."
Customer: Please recommend a movie based on my interests.
Agent:

Output:
Sorry, I don't have any information about your interests.
However, here's a list of the top global trending movies right now:
[list of movies].

Examples of Prompts

The previous section introduced a basic example of how to prompt LLMs.

This section will provide more examples of how to use prompts to achieve different tasks and introduce key concepts along the way. Often, the best way to learn concepts is by going through examples. The few examples below illustrate how you can use well-crafted prompts to perform different types of tasks.

Topics:

  1. Text Summarization
  2. Information Extraction
  3. Question Answering
  4. Text Classification
  5. Conversation
  6. Code Generation
  7. Reasoning

Text Summarization

Text summarization with LLMs involves succinctly condensing articles or concepts. An example is summarizing the concept of antibiotics in one sentence, which showcases the model’s ability to extract and condense key information.

Prompt:

Explain antibiotics:

Output:

Antibiotics are a type of medication used to treat bacterial infections. 
They work by either killing the bacteria or preventing them from reproducing,
allowing the body’s immune system to fight off the infection.
Antibiotics are usually taken orally in the form of pills, capsules, or
liquid solutions, or sometimes administered intravenously.
They are not effective against viral infections, and using them
inappropriately can lead to antibiotic resistance.

The “A:” is an explicit prompt format that you use in question answering. You used it here to tell the model that there is an answer expected further. In this example, it’s not clear how this is useful vs not using it but we will leave it that for later examples. Let’s just assume that this is too much information and you want to summarize it further. In fact, you can instruct the model to summarize into one sentence like so:

Prompt:

Antibiotics are a type of medication used to treat bacterial infections.
They work by either killing the bacteria or preventing them from reproducing,
allowing the body’s immune system to fight off the infection.
Antibiotics are usually taken orally in the form of pills, capsules, or
liquid solutions, or sometimes administered intravenously.
They are not effective against viral infections, and using them
inappropriately can lead to antibiotic resistance.
Explain the above in one sentence

Output:

Antibiotics are medications used to treat bacterial infections by either 
killing the bacteria or stopping them from reproducing, but they are not
effective against viruses and overuse can lead to antibiotic resistance.

Without paying too much attention to the accuracy of the output above, which is something we will touch on in a later guide, the model tried to summarize the paragraph in one sentence. You can get clever with the instructions but we will leave that for a later chapter. Feel free to pause here and experiment to see if you get better results.

Information Extraction

LLMs can also extract specific information from text. For instance, identifying the mention of a product like ChatGPT in a paragraph demonstrates the model’s capability in pinpointing relevant details.

Prompt:

Author-contribution statements and acknowledgements in research papers should 
state clearly and specifically whether, and to what extent, the authors used
AI technologies such as ChatGPT in the preparation of their manuscript and
analysis. They should also indicate which LLMs were used.
This will alert editors and reviewers to scrutinize manuscripts more
carefully for potential biases, inaccuracies and improper source crediting.
Likewise, scientific journals should be transparent about their use of LLMs,
for example when selecting submitted manuscripts.Mention the large language
model based product mentioned in the paragraph above:

Output:

The large language model based product mentioned in the paragraph above is 
ChatGPT.

Question Answering

Enhancing prompt formats improves LLMs’ response accuracy. For example, providing context with a question and instructing for a concise answer showcases the model’s ability to filter and focus information.

Prompt:

Answer the question based on the context below. 
Keep the answer short and concise. Respond "Unsure about answer" if not sure
about the answer.Context: Teplizumab traces its roots to a New Jersey drug
company called Ortho Pharmaceutical. There, scientists generated an early
version of the antibody, dubbed OKT3. Originally sourced from mice, the
molecule was able to bind to the surface of T cells and limit their
cell-killing potential. In 1986, it was approved to help prevent organ
rejection after kidney transplants, making it the first therapeutic antibody
allowed for human use.Question: What was OKT3 originally sourced from?
Answer:

Output:

Mice.

Text Classification

LLMs can classify text sentiments effectively. Through specific instructions and examples, models can categorize sentiments as neutral, negative, or positive with precision.

Prompt:

Classify the text into neutral, negative or positive. 
Text: I think the food was okay.

Output:

Neutral

Conversation

Role prompting guides LLMs in conversational tasks. By defining the tone and style, like a technical or simplified response, the model adapts its answers accordingly, demonstrating versatility in language use.

Prompt:

The following is a conversation with an AI research assistant. 
The assistant tone is technical and scientific.
Human: Hello, who are you?
AI: Greeting! I am an AI research assistant. How can I help you today?
Human: Can you tell me about the creation of blackholes?

Output:

AI:Sure! Black holes are regions of spacetime where the gravitational force 
is so strong that nothing, not even light, can escape from it.
They are created when a very massive star dies and its core collapses in on
itself, forming a singularity of infinite density.
The intense gravity of the singularity pulls in all the matter and radiation
around it, creating the black hole.

Code Generation

LLMs excel in generating code. From simple greeting programs to complex MySQL queries, they can interpret and respond to diverse programming tasks.

Can you give me the code in sql to get all types of values in a sql columns 
and their count
SELECT column_name, COUNT(column_name) AS value_count
FROM table_name
GROUP BY column_name;

Reasoning

While LLMs show potential in reasoning, they can struggle with complex arithmetic or logical tasks. Improved prompt engineering with step-by-step instructions enhances the accuracy of responses.

Prompt:

What is 9,000 * 9,000?

Output:

81,000,000

These examples highlight LLMs’ multifaceted abilities in summarization, information extraction, question answering, text classification, conversational responses, code generation, and reasoning, emphasizing the importance of precise and tailored prompting.

By this point, it should be obvious that it helps to improve prompts to get better results on different tasks. That’s the whole idea behind prompt engineering.

While the basic examples were fun, in the next section we cover more advanced prompting engineering techniques that allow us to achieve more complex and interesting tasks.

--

--

No responses yet