LLM Prompt Engineering for Beginners: What It Is and How to Get Started

Sahin Ahmed, Data Scientist
The Deep Hub
Published in
12 min readApr 14, 2024
Source: Image Generated by author using Dall E

In the realm of artificial intelligence, Large Language Models (LLMs) have emerged as powerhouses of language processing. These sophisticated AI models have been trained on vast volumes of text data, enabling them to communicate and generate human-like text in response to a wide range of prompts and questions. Their capabilities extend to tasks like writing different kinds of creative content, translating languages, and providing informative answers.

Prompt engineering has arisen as the key to unlocking the full potential of LLMs. It can be described as the art of carefully crafting instructions and inputs to guide these models towards a desired outcome. By skillfully tailoring prompts, users can direct LLMs to perform specific actions without the complexity of traditional coding.

What exactly is a prompt?

In essence, a prompt is a piece of text or a set of instructions that you provide to a Large Language Model (LLM) to trigger a specific response or action.

Here’s how it works:

  • Input: The prompt is your way of communicating with the LLM. It’s the starting point that tells the model what you want it to do.
  • Processing: The LLM analyzes your prompt, drawing on its vast knowledge base and understanding of language patterns.
  • Output: The LLM generates a response based on your prompt. This response could be Text completion, A translation, A summary of information, A creative text piece (like a poem or script)Even code generation in some cases

Think of a prompt like…

  • A search query: Only instead of searching the web, you’re searching the knowledge and abilities of the LLM.
  • Giving directions: You’re providing detailed instructions to the LLM on how you’d like it to proceed with a task.
  • Setting a scene for a story: The prompt becomes the seed for the LLM to build on and expand a narrative.

Prompt engineering

Prompt engineering is the process of designing and crafting effective prompts to guide Large Language Models (LLMs) to generate the responses you want.

Here’s a breakdown of what it involves:

Key Aspects of Prompt Engineering:

  • Understanding LLMs: Successful prompt engineering starts with a grasp of how LLMs work, their capabilities, and their limitations. This knowledge helps you form prompts the model is likely to understand and respond to well.
  • Goal-Oriented: Prompt engineering has a specific target in mind. Whether you want a translation, a summary, or a creative poem, the prompt must direct the LLM towards that goal.
  • Specificity and Clarity: The more precise your prompt, the better the LLM can understand your intent. Avoid ambiguity and focus on providing clear instructions and context.
  • Iterative Process: Prompt engineering is often about experimentation. Be prepared to refine your prompts, try different wordings, and adjust the level of detail to get the best results.
  • Tools and Techniques: As the field matures, various tools and frameworks are emerging to simplify prompt engineering. These can help you structure prompts, manage examples, and optimize your results.

Analogy: Think of prompt engineering like the following:

  • Giving a chef a recipe: A detailed recipe (prompt) guides the chef (LLM) to create the desired dish (output).
  • Programming with natural language: Instead of complex code, you use carefully crafted language to instruct the LLM.
  • The art of asking the right questions: Prompt engineering is about finding the most effective way to communicate your needs to the LLM to get the outcome you desire.

Main types of prompts used in prompt engineering

Here’s a breakdown of the main types of prompts used in prompt engineering, along with illustrative examples:

1. Direct Instruction Prompts

  • Purpose: Clearly state the exact task you want the LLM to execute.
  • Example: “Translate this paragraph from English to Spanish.”

2. Task Completion Prompts

  • Purpose: Provide a scenario or problem and ask the LLM to complete a task related to it.
  • Example: “You are a customer service representative. A customer is upset their package is late. Write a reply email that apologizes and offers a solution.”

3. Few-Shot Learning Prompts

  • Purpose: Give a few examples of the desired input-output format to guide the LLM towards a new task.
  • Example:
  • “Input: Apple, Output: Fruit”
  • “Input: Paris, Output: Country”
  • “Input: Microwave. Output: Appliance”
  • “Input: Bark, Output: ?” (The LLM should respond with “Dog”)

4. Story Continuation Prompts

  • Purpose: Set the stage for a narrative and let the LLM continue the story.
  • Example: “Once upon a time, in a land far away, a brave knight named Sir Thomas set out on a quest to find a hidden treasure…”

5. Question-Answering Prompts

  • Purpose: Ask the LLM a question and have it provide an answer based on its knowledge.
  • Example: “What is the capital of France?”

Important Notes:

  • Complexity: Prompts can range from simple one-line instructions to elaborate, multi-part scenarios.
  • Specificity: The level of detail in your prompt greatly influences the quality of the LLM’s output.
  • Prompt Chains: Complex tasks can sometimes be broken down into a series of smaller, connected prompts.

Advanced Techniques in Prompt Engineering:

Chain of Thought (CoT):

Chain of Thought (CoT) prompting is an advanced technique that pushes Large Language Models (LLMs) beyond simply generating an answer. It aims to get them to explain their reasoning process, making their responses more transparent and potentially more reliable.

Here’s a deeper look at CoT prompting:

The Core Idea:

  • Standard prompts typically ask for a direct answer.
  • CoT prompts, however, ask the LLM to not only provide an answer but also to show the steps it took to arrive at that answer.
Image Source: https://ar5iv.labs.arxiv.org/html/2401.14423v3

Example of scenario where Chain of Thought (CoT) prompting would be beneficial:

Scenario: Analyzing a news article for bias

Problem: You have a news article about a controversial political event and want to determine if the article presents a biased viewpoint.

Standard Prompt:

  • “Is this news article biased?”
  • (Possible LLM Output: “Yes, the article seems biased towards one side of the issue.”)

Complex CoT Prompt:

  • “Analyze this news article for potential bias. Provide the following:
  • Identify any language that uses strong emotional appeals, exaggerations, or omits important details. Provide specific examples.
  • Does the article present multiple perspectives on the issue, or does it heavily favor one side? Explain your reasoning.
  • Based on your analysis, would you classify this article as biased? Justify your answer.”

Zero-shot CoT vs Manual CoT

  • Zero-shot CoT relies on a remarkably simple modification to the original prompt. Instead of crafting specific examples, you primarily append the phrase “Let’s think step by step” to encourage the LLM to break down its reasoning.
  • Manual CoT involves you carefully crafting prompts with instructions to think through a problem step-by-step. This also includes hand-picking examples to demonstrate the desired reasoning pattern for the LLM.

Benefits of CoT Prompting:

  • Improved Accuracy: By revealing the thought process, CoT prompts can help identify and address any logical flaws in the LLM’s reasoning, potentially leading to more accurate answers.
  • Debugging and Understanding: Seeing the chain of thought allows you to diagnose errors in the LLM’s reasoning and pinpoint areas for improvement.
  • Explainability: CoT prompts make LLM outputs more interpretable, providing valuable insights into how they arrived at their conclusions.

Implementation Approaches:

There are several ways to implement CoT prompting:

  • Explicit Instructions: You can directly instruct the LLM to explain its reasoning steps within the prompt itself.
  • Templates: Templates can be pre-designed to guide the LLM in structuring its thought process explanation.
  • Examples: Providing worked-out examples with clear explanations can train the LLM to follow a similar reasoning style for new problems.

Advanced CoT Techniques:

  • Self-Consistency: Generate multiple chains of thought for the same problem and choose the most consistent answer.
  • Auto-CoT: Automatically construct demonstrations with questions and reasoning chains for training LLMs.

Keep in mind:

  • CoT prompting is a complex technique still under development.
  • It may not be suitable for all tasks, especially very simple ones.

Example of chain of thought prompt for coding task:

Problem Statement: Create a function to find the shortest path between two points in a maze. The maze is represented by a 2D grid where:

  • ‘0’ represents an open path
  • ‘1’ represents a wall
  • ‘S’ represents the starting point
  • ‘E’ represents the ending point

Assumptions:

  • The maze only allows up, down, left, and right movement (no diagonals).
  • There is always at least one guaranteed path between ‘S’ and ‘E’.

Chain of Thought

  1. Algorithm Choice: Breadth-First Search (BFS) is well-suited for finding shortest paths in graphs (our maze is essentially a grid graph).
  2. Data Structures:
  • maze: The input 2D grid.
  • queue: To hold nodes (coordinates) during BFS traversal.
  • visited: A set to keep track of visited nodes to avoid cycles.
def shortest_path(maze):
start = find_start(maze) # Helper function (see below)
queue = [(start, 0)] # (Coordinate, distance) pairs
visited = set()
# ... rest of the logic
#helper functions
def find_start(maze):
# Code to find the coordinates of 'S'

def is_valid(maze, x, y):
# Check if within grid bounds and if it's an open path

# The output should be Shortest path length and the shortest path itself

Answer given by Gemini Advanced LLM from Google:

def shortest_path(maze):
def find_start(maze):
for row_idx, row in enumerate(maze):
for col_idx, cell in enumerate(row):
if cell == 'S':
return row_idx, col_idx

def is_valid(maze, row, col):
return (0 <= row < len(maze) and 0 <= col < len(maze[0]) and
maze[row][col] != '1')

def get_neighbors(row, col):
deltas = [(0, 1), (0, -1), (1, 0), (-1, 0)] # Right, Left, Down, Up
neighbors = []
for dr, dc in deltas:
new_row, new_col = row + dr, col + dc
if is_valid(maze, new_row, new_col):
neighbors.append((new_row, new_col))
return neighbors

start = find_start(maze)
end = find_end(maze) # Similar find_start implementation for ending point
queue = [(start, 0)] # (Coordinate, distance) pairs
visited = {start}
parents = {} # To track the path

while queue:
(row, col), distance = queue.pop(0)

if (row, col) == end:
return distance, reconstruct_path(parents, end)

for neighbor_row, neighbor_col in get_neighbors(row, col):
if (neighbor_row, neighbor_col) not in visited:
queue.append(((neighbor_row, neighbor_col), distance + 1))
visited.add((neighbor_row, neighbor_col))
parents[(neighbor_row, neighbor_col)] = (row, col) # Store parent

return -1, None # No path found

def reconstruct_path(parents, end):
path = []
current = end
while current in parents:
path.append(current)
current = parents[current]
path.reverse() # From start to end
return path

# Example usage
maze = [
['0', '0', '1', 'S'],
['1', '0', '0', '0'],
['0', '0', '0', 'E']
]

distance, path = shortest_path(maze)
if path:
print("Shortest distance:", distance)
print("Path:", path)
else:
print("No path found between the start and end.")

This code ran successfully.

N.B: The code provided by LLMs may not always be efficient. It may need proper checks and tests to judge its robustness.

Tree of Thought Prompting

Tree of thought prompting guides an AI model to explore several possible lines of reasoning simultaneously from a single question or problem statement. Unlike the linear progression of traditional COT prompting, the tree of thought approach branches out, allowing the exploration of multiple perspectives or solutions at various decision points. This method can effectively map out a more comprehensive range of possibilities and contingencies in the model’s reasoning process.

Image Source:https://arxiv.org/abs/2305.10601

How It Works

  1. User Input: The user introduces the problem they want to solve.
  2. Prompter Agent: Crafts prompts that encourage the LLM to not solve the entire problem at once, but focus on manageable, intermediate solutions.
  3. LLM: The LLM processes the prompt and generates an intermediate solution.
  4. Checker Module: This module acts as a crucial filter, verifying the validity of the LLM’s proposed intermediate solution.
  5. Memory Module: If the intermediate solution is valid, it is stored in the memory module. This memory serves as a knowledge base for the LLM to build upon in future steps.
  6. ToT Controller: This component orchestrates the whole process:
  • Guidance: It provides feedback or hints to the LLM if the intermediate solution fails the check.
  • Backtracking: If an intermediate solution, while valid, doesn’t seem promising, the ToT controller can trigger backtracking, prompting the LLM to reconsider and try alternative solution branches.
Image Source: https://arxiv.org/pdf/2305.08291.pdf

Key Advantages of the Train of thought(ToT) System:

  • Divide and Conquer: The ToT decomposes complex problems into smaller, more manageable steps, well-suited to LLM capabilities.
  • Error Correction: The checker module helps catch potential mistakes made by the LLM, improving solution accuracy.
  • Exploration: The ability to backtrack expands the search space for solutions, fostering creative and diverse problem-solving strategies.
  • ‘Short-Range’ vs. ‘Long-Range’ Reasoning: ToT leverages the LLM’s strength in short-range reasoning and augments it with system-level logic for long-range strategic problem-solving.
  • Increased Computational Capacity: The multi-step ToT process pushes computational boundaries compared to a single interaction with the LLM.

An example of a “tree of thought” prompt

Task: Develop a function to play a simplified version of Tic-Tac-Toe where the AI opponent makes intelligent decisions to block the human player’s moves or attempt to win.

Prompt: Imagine you have three different coding experts working on this Tic-Tac-Toe AI. Each expert will provide one step of their solution, and then the next expert will evaluate and build upon it.

Expert 1:

  • Thought: I’ll start by representing the game board as a simple list for easier manipulation. I’ll need functions to check if a space is empty, place a mark (‘X’ for the player, ‘O’ for the AI), and check for basic winning conditions (rows, columns, diagonals).

Expert 2:

  • Thought: Expert 1’s approach is solid. However, for a more strategic AI, I’ll introduce a function to evaluate potential moves. This function would assign scores: higher scores for moves that lead to AI victory, lower scores for moves that could lead to the player winning.

Expert 3:

  • Thought: Building on the previous ideas, I think a Minimax algorithm would be great here. It would allow the AI to look ahead and simulate potential game outcomes, helping it choose the most advantageous move based on the scoring from Expert 2.

Ans given by GPT4 LLM from Open AI:

Expert 1: Game Board Representation and Basic Functions

class TicTacToe:
def __init__(self):
# The board is a list of 9 elements representing 3x3 grid
self.board = [" " for _ in range(9)]

def print_board(self):
# Visual representation of the board
for i in range(3):
print('|' + '|'.join(self.board[i*3:(i+1)*3]) + '|')

def is_space_free(self, index):
# Check if the board space is free
return self.board[index] == " "

def place_mark(self, index, mark):
# Place a mark ('X' or 'O') on the board at the specified index
if self.is_space_free(index):
self.board[index] = mark
return True
return False
def check_winner(self, mark):
# Check all winning conditions for the given mark
win_conditions = [
[0, 1, 2], [3, 4, 5], [6, 7, 8], # rows
[0, 3, 6], [1, 4, 7], [2, 5, 8], # columns
[0, 4, 8], [2, 4, 6] # diagonals
]
for condition in win_conditions:
if all(self.board[i] == mark for i in condition):
return True
return False

Expert 2: Evaluating Potential Moves

def evaluate_move(self):
# Evaluate potential moves to find the best move for the AI
best_score = -float('inf')
best_move = None
for i in range(9):
if self.is_space_free(i):
# Simulate placing 'O' for AI
self.board[i] = 'O'
if self.check_winner('O'):
score = 1
elif self.check_winner('X'):
score = -1
else:
score = 0
# Undo the move
self.board[i] = ' '
# Choose the move with the highest score
if score > best_score:
best_score = score
best_move = i
return best_move

Expert 3: Minimax Algorithm

def minimax(self, is_ai):
# Minimax algorithm to choose the optimal move
if self.check_winner('O'): # AI wins
return 1
if self.check_winner('X'): # Player wins
return -1

# Possible moves and scores
moves = []
for i in range(9):
if self.is_space_free(i):
self.board[i] = 'O' if is_ai else 'X'
score = self.minimax(not is_ai)
moves.append((score, i))
self.board[i] = ' ' # Undo move

if not moves:
return 0 # Tie game

if is_ai:
# Maximize score for AI
return max(moves)[1] # Return index of best move
else:
# Minimize score for opponent
return min(moves)[1] # Return index of best move

To run the game, instantiate the TicTacToe class and use the methods to play the game, alternating between player and AI moves. The AI uses the minimax function to decide its moves strategically, considering both offensive and defensive plays.

This implementation effectively integrates the ideas from all experts, resulting in a strategic AI opponent that is challenging for human players.

N.B: Code provided by LLMs needs to be checked thoroughly although it runs successfully. There might be hidden bugs or the code may fail in edge cases. To do these you can prompt the LLM further .

Stay tuned for the upcoming articles on more advanced and state of the art prompt engineering techniques.

References and further readings:

1.Large Language Model Guided Tree-of- Thought, Jieyi Long

2. Tree of Thoughts: Deliberate Problem Solving with Large Language Models

4. Prompt Design and Engineering: Introduction and Advanced Methods,Xavier Amatriain

5. OpenAI’s Prompt Engineering Guide

6. Prompt Engineering Guide on PromptingGuide.ai

7. A Systematic Survey of Prompt Engineering in Large Language Models: Techniques and Applications

--

--

Sahin Ahmed, Data Scientist
The Deep Hub

Data Scientist | MSc Data science|Lifelong Learner | Making an Impact through Data Science | Machine Learning| Deep Learning |NLP| Statistical Modeling