January 8, 2024

How to Craft Prompts for Different Large Language Models Tasks

By Justin Delisi

Large Language Models (LLMs) like ChatGPT are prevalent tools for businesses to interact with AI through what seems like a simple chat function. However, the messages users send (prompts) are directly correlated with the quality of the output of these models. 

In this blog, we will provide general tips for crafting effective prompts and list some advanced techniques for some of the top use cases of LLMs. Using these skills, you will be one step closer to mastering the art of prompt engineering.

Quick Overview of Large Language Models (LLMs)

A large language model (LLM) is a cutting-edge AI trained on massive amounts of text data. These versatile models are rapidly changing the game in fields like writing, research, and communication (even pushing the boundaries of natural language processing).

Prompt engineering is the practice of carefully crafting the input instructions or queries provided to these models to retrieve the best response possible from the LLM.

What Is A Prompt?

Simply put, a prompt is the user’s input that the LLM responds to. They are typically a few sentences that dictate the type of response expected from the LLM. These can range anywhere from simple questions to asking for something extremely complex.

Some examples of prompts include:

what country's capital has the most people

write a Pyhton code template to connect to Snowflake

write a 5000 word essay on artificial intelligence, citing sources in APA format

General Tips For Crafting Effective Prompts

Every type of task a user asks an LLM to perform has its own unique set of challenges, but some generalized practices will help create more effective prompts for all types of tasks. 

  1. Have A Clear Objective:
    If you don’t know exactly what you’re looking for, the LLM won’t either. Even the smallest mistake or misconception in a natural language prompt can give wildly different results than expected.

  2. Be As Specific As Possible:
    Give clear and complete instructions with concrete steps, delimiters for referenced data, and a description of your desired output.

  3. Use Keywords:
    Utilizing pertinent keywords is crucial for ensuring that LLMs comprehend your query effectively. When formulating a prompt, ensure you incorporate keywords that align with your intended goal.

  4. Provide Context:
    Briefly provide context if necessary. Explain the background or any specific information that the LLM should consider when generating a response.

  5. Use Complete Sentences:
    Formulate your prompts as complete sentences or questions. This helps the LLM understand the structure of your request.

  6. Limit Ambiguity:
    Minimize the use of ambiguous words like “it,” “this,” or “that” in your prompts. Specify what you are referring to.

  7. Set Constraints:
    If needed, set constraints or guidelines for the response. For instance, you can instruct the LLM to provide a concise answer or avoid certain topics.

  8. Don’t Rush an Answer:
    Your instructions should have the LLM walk through the logic needed to solve problems before it gives a solution.

  1. Use the Golden Rule:
    Only ask questions that you know the answer to. Leverage the LLM to use factual text to help better summarize, extract, translate, or elaborate text.

  1. Knowledge Discovery is a Dangerous Task:
    Remember that the LLM will be confident in giving an incorrect and plausible-sounding answer.

  1. Verify the Output Using Reliable Sources:
    Use it as a beginning, not the end of your journey.

  1. Be patient and Iterate:
    You don’t need to start with the answer; you need to end with it.

How To Craft Effective Prompts For Specific Tasks

Content Generation And Marketing

LLMs can generate human-like text, making them valuable tools for automating content generation for technical write-ups, social media posts, product descriptions, and even legal documents. However, if the prompts used to create the content aren’t good, the ‘artificial’ element becomes more pronounced. With the proper prompts, generating human-like content that’s relevant and high quality is a breeze.

Techniques to Generate Quality Content

Clearly Define Your Topic

Begin by defining the specific topic or subject matter you want the content to focus on. Be as clear and specific as possible.

Generate content about eco-friendly gardening tools

Specify The Type Of Content

Indicate the type of content you want to create. For instance, you could specify, “Write a blog post, Create a product description,  or Draft a social media post.”

Write a blog post about eco-friendly gardening tools

Set The Tone And Style

Describe the tone and style you want the content to have. Are you looking for a formal, informative tone or a more casual, conversational one? Providing this guidance helps tailor the output to your brand’s voice.

Write a blog post with a friendly and conversational tone about eco-friendly gardening tools with an introduction and conclusion, include five tips for sustainable gardening

Specify Length And Structure

If applicable, mention the desired word count or structure.

Create a 700-word blog post with a friendly and conversational tone about eco-friendly gardening tools with an introduction and conclusion, include five tips for sustainable gardening

Consider SEO And Keywords

If the content is intended for online publishing, incorporate relevant keywords and SEO guidelines to improve search engine visibility.

Create a 700-word blog post with a friendly and conversational tone about eco-friendly gardening tools with an introduction and conclusion, include five tips for sustainable gardening, use keywords like 'sustainable gardening tips', 'organic gardening' and 'eco-friendly'

Proofread And Edit

Even though LLMs produce high-quality text, it’s still important to proofread and edit the generated content to ensure accuracy and consistency with your brand’s guidelines. The LLM you’re using may not be current on the latest information about your topic, or the model may generate a hallucination. 

Hallucinations are instances where the model generates text that may be incorrect, nonsensical, or divergent from factual information. LLMs, while powerful, are not infallible and can produce outputs that may seem plausible on the surface but lack accuracy or coherence. 

These errors can arise from various factors, including biases in training data, ambiguous queries, or limitations in the model’s understanding. It’s essential to approach the outputs of LLMs critically and verify information from reliable sources when accuracy is crucial. 

Lastly, while LLMs are powerful and able to generate content at record speeds, it doesn’t always mean the content is good. As of this writing, even the best prompts can’t replace the quality of human writing, but with time and innovation, this could change.

Code Generation

Businesses use LLMs to significantly enhance coding efficiency, productivity, and consistency. LLMs automate repetitive coding tasks, reduce human error, and expedite development processes. They also provide scalability benefits, assist with legacy code maintenance, and facilitate knowledge transfer within development teams. 

Techniques to Generate Quality Code with an LLM

Define the Task Clearly

Begin by clearly defining the coding task you want the LLM to perform. Specify the programming language you want the code in and provide a concise description of the task.

Generate Python code that calculates the factorial of a given integer

Result
				
					def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n-1)


# Example usage
user_input = int(input("Enter an integer to calculate its factorial: "))
result = factorial(user_input)
print(f"The factorial of {user_input} is {result}")
    
				
			
Include Input and Output Details

Specify the input data or parameters and the expected output. This helps the LLM understand the problem and generate code that meets your requirements.

Generate Python code that when given the integer 'num', the code should return the factorial of 'num' as an integer

Result
				
					def factorial(num):
    if num < 0:
        return "Factorial is undefined for negative numbers"
    elif num == 0 or num == 1:
        return 1
    else:
        result = 1
        for i in range(2, num + 1):
            result *= i
        return result


# Example usage
user_input = int(input("Enter an integer to calculate its factorial: "))
result = factorial(user_input)
print(f"The factorial of {user_input} is {result}")
    
				
			
Specify Function or Class Name

If applicable, specify the function or class names you want to use in the code. This helps maintain code structure and readability.

Generate Python code with a function called 'calculate_factorial' that when given the integer 'num', the code should return the factorial of 'num' as an integer

Result
				
					def calculate_factorial(num):
    if num < 0:
        return "Factorial is undefined for negative numbers"
    elif num == 0 or num == 1:
        return 1
    else:
        result = 1
        for i in range(2, num + 1):
            result *= i
        return result


# Example usage
user_input = int(input("Enter an integer to calculate its factorial: "))
result = calculate_factorial(user_input)
print(f"The factorial of {user_input} is {result}")
    
				
			
Set Constraints or Guidelines

Provide any necessary constraints or guidelines for the code. This can include requirements like handling edge cases, optimizing for efficiency, or following coding style conventions.

Generate Python code with a function called 'calculate_factorial' that when given the integer 'num', the code should return the factorial of 'num' as an integer, optimize the code for performance when calculating large factorials

Result
				
					import math

def calculate_factorial(num):
    if num < 0:
        return "Factorial is undefined for negative numbers"
    else:
        return math.factorial(num)


# Example usage
user_input = int(input("Enter an integer to calculate its factorial: "))
result = calculate_factorial(user_input)
print(f"The factorial of {user_input} is {result}")
    
				
			
Use Pseudocode Or Algorithm Description

If you have a specific algorithm in mind, you can provide a pseudocode or algorithmic description as part of the prompt to guide the LLM.

Generate Python code with a function called 'calculate_factorial' that when given the integer 'num', the code should return the factorial of 'num' as an integer, use a loop to iteratively multiply 'num' by decreasing integers until 'num' reaches 1

Result
				
					def calculate_factorial(num):
    if num < 0:
        return "Factorial is undefined for negative numbers"
    elif num == 0 or num == 1:
        return 1
    else:
        result = 1
        for i in range(num, 1, -1):
            result *= i
        return result


# Example usage
user_input = int(input("Enter an integer to calculate its factorial: "))
result = calculate_factorial(user_input)
print(f"The factorial of {user_input} is {result}")
				
			
Chain-of-Thought (CoT) Technique

CoT prompting enables complex reasoning capabilities through intermediate reasoning steps. Breaking down a complex problem into smaller, simpler steps has been shown to significantly improve the output of LLMs.

Write a Python function that takes a list of numbers and returns the sum of all the even numbers in the list.
1. Define a function called `sum_even_numbers`.
2. Take a list of numbers as input.
3. Initialize a variable called `sum` to 0.
4. Iterate over the list of numbers.
5. If the number is even, add it to the `sum` variable.
6. Return the `sum` variable.

It’s important to note here that some models are farther along in code generation than others. For example, Chat GPT automatically does step-by-step and full consistency checks when writing code. If you ask it to write Python, it’ll literally run it for you and test that it works correctly. Knowing this, some of these techniques aren’t required when working with more advanced models

Self-Consistency Technique

Self-consistency is a prompt engineering technique that aims to improve the accuracy and reliability of language model responses by asking the model to generate multiple responses to the same (or slightly varying) prompt and then selecting the most consistent answer. 

This technique is particularly useful for tasks that require the model to reason over multiple steps, such as writing code.

Write a function called longest_unique_substring that takes a string as input and returns the longest substring of the string that contains only unique characters.

Given a string s, write a function that returns the longest substring of s that contains only unique characters.

Write a function, longest_unique_substring, that takes a string as input and returns the longest substring of the string that does not contain any repeated characters.

These prompts are all slightly different, but they all ask the LLM to solve the same problem. The goal is to encourage the LLM to generate a diverse set of reasoning paths and then select the most consistent output as the final answer.

Self-consistency prompting can be used to generate code for a variety of tasks, just like chain-of-thought prompting. However, self-consistency prompting is generally considered to be a more powerful and reliable technique, as it is less likely to generate incorrect or inconsistent code.

Test and Validate

Test the generated code to ensure it functions as expected and meets your requirements, debugging and making adjustments if necessary. Prompts used to create code should also be versioned, and performance should be benchmarked against previous versions. 

As mentioned above with content creation, LLMs may not always be perfect. The best practice is to treat code generated by an LLM like you would code generated by a human, with code reviews and a process for approving code before it gets used in a production environment.

Data Analysis And Insights

LLMs can analyze large volumes of text data to extract insights and trends. Businesses use them for tasks like data summarization, trend analysis, and competitive intelligence. For instance, a market research firm might utilize an LLM to analyze news articles, customer reviews, and industry reports to provide clients with comprehensive market insights. Creating the right prompts is essential for businesses to obtain these valuable insights out of the LLM. 

Techniques to Obtain Quality Analysis and Insights with an LLM

Specify the Data and Objective

Clearly define the dataset or data source you want to analyze and specify the analytical objective.

Analyze the sales data for the last quarter and identify the key factors contributing to revenue growth

Detail the Analysis Type

Specify the type of analysis you want to perform, such as descriptive statistics, regression analysis, clustering, or time series analysis.

Perform a descriptive analysis of the customer demographics in the dataset, including age distribution, gender breakdown, and location

Include Key Variables of Interest

List the variables or columns you want to focus on in the analysis. Be specific about the data elements you want to explore.

Examine the relationship between 'advertising spend' and 'sales revenue' to determine if there's a correlation

Set Statistical Parameters

Specify any statistical parameters or thresholds you want to apply, such as significance levels, confidence intervals, or data transformation methods.

Calculate the correlation coefficient between 'customer satisfaction' and 'repeat purchase rate' with a significance level of 0.05

Provide Context or Hypotheses

If relevant, provide context or hypotheses that guide the analysis. This helps the LLM understand the purpose of the analysis.

Test the hypothesis that an increase in 'website traffic' leads to a higher 'conversion rate' for online sales

Closing

We hope this blog helps you take a giant step closer to mastering the art of prompt engineering. While we only covered three common use cases, the application of LLMs grows every day. At phData, we’ve been fortunate enough to work on the front lines of innovation with our customers on how to best utilize LLMs. 

Our work on automated prompt engineering at scale

This blog showcases how to build an automated (and effective) HR Chatbot using modern data stack technologies.

Explore all about it!

FAQs

As a fledgling field of study, prompt engineering is evolving nearly every day. New techniques such as Adversarial, Ask-Me-Anything, or Meta prompting are just a few of the newest techniques for prompt engineering. Researchers are actively developing new techniques, and the field of prompt engineering is rapidly evolving.

As corporations use these technologies more and more, they are finding interesting ways to utilize the LLMs to their benefit. LLMs can be used to generate leads and qualify prospects in sales, generate ideas for new products and features for product development, or even generate new hypotheses and theories for research and development. The LLMs will continue to expand into new areas as users find ways to utilize them to their benefit. 

Data Coach is our premium analytics training program with one-on-one coaching from renowned experts.

Accelerate and automate your data projects with the phData Toolkit