Prompt engineering is a relatively new field focusing on creating and improving prompts for using language models (LLMs) effectively across various applications and research areas. We can easily understand the capabilities and restrictions of large language models( LLMs) with the aid of quick engineering skills. Researchers intensively use prompt engineering to increase LLMs’ capacity for various tasks, including question answering and mathematical reasoning. Developers use prompt engineering to develop dependable, effective prompting techniques that work with LLMs and other tools.
This article was published as a part of the Data Science Blogathon.
A large language model (LLM) is a type of artificial intelligence (AI) algorithm that uses some techniques and a large set of data to understand, generate, summarize, and predict new content.
Language models use autoregression to generate text. The model forecasts the probability distribution of the following word in the sequence based on an initial prompt or context. The most likely word is then produced, repeated continuously to produce words based on the original context.
Prompt engineering refers to crafting effective prompts or instructions to guide the behavior of a language model or AI system. It involves formulating queries or commands to elicit the desired response or output from the AI. Prompt engineering is crucial for fine-tuning models like GPT-3 to generate accurate and relevant results.
By carefully designing prompts, developers can control the AI’s output style, tone, and content. It requires understanding the model’s capabilities and limitations, experimenting with different phrasings, and iterating to achieve the desired outcome. Prompt engineering is essential to harness the potential of AI systems while avoiding biases, inaccuracies, or unintended outputs.
Although LLMs are great for generating the appropriate responses for various tasks, where the model predicts the probability distribution of the next word in the sequence and generates the most likely words. This process continues iteratively, and the given task is fulfilled. But then there are several challenges to generating the relevant responses.
To address these challenges, prompt engineering plays a crucial role. Developers can guide the language model’s output by carefully designing prompts and providing additional context, constraints, or instructions to steer the generation process. Prompt engineering helps mitigate language model limitations and improve the generated responses’ coherence, relevance, and quality.
Prompt engineering requires understanding the AI model’s capabilities and the desired outcomes. It’s a crucial step in harnessing the power of AI technology effectively and responsibly.
Here are some simple examples of prompt engineering:
The first task is to load your OpenAI API key in the environment variable.
import openai
import os
import IPython
from langchain.llms import OpenAI
from dotenv import load_dotenv
load_dotenv()
# API configuration
openai.api_key = os.getenv("OPENAI_API_KEY")
The ‘get_completion’ function generates a completion from a language model based on a given prompt using the specified model. We will be using GPT-3.5-turbo.
def get_completion(prompt, model="gpt-3.5-turbo"):
model=model,
messages=messages,
temperature=0, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
The process performed here is automatic text summarization, one of the common activities in natural language processing tasks. In the prompt, we just ask to summarize the document and enter a sample paragraph; one doesn’t give sample training examples. After activating the API, we will get the summarized format of the input paragraph.
text = """
Pandas is a popular open-source library in Python
that provides high-performance data manipulation and analysis tools.
Built on top of NumPy, Pandas introduces powerful data structures,
namely Series (one-dimensional labeled arrays) and DataFrame
(two-dimensional labeled data tables),
which offer intuitive and efficient ways to work with structured data.
With Pandas, data can be easily loaded, cleaned, transformed, and analyzed
using a rich set of functions and methods.
It provides functionalities for indexing, slicing, aggregating, joining,
and filtering data, making it an indispensable tool for data scientists, analysts,
and researchers working with tabular data in various domains.
"""
prompt = f"""
Your task is to generate a short summary of the text
Summarize the text below, delimited by triple
backticks, in at most 30 words.
Text: ```{text}```
"""
response = get_completion(prompt)
print(response)
Output
By providing a context with a question, we expect the model to predict the answer from the given context. So, the task here is an unstructured question answering.
prompt = """ You need to 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:"""
response = get_completion(prompt)
print(response)
Output
The task is to perform text classification. Given a text, the task is to predict the sentiment of the text, whether it is positive, negative, or neutral.
prompt = """Classify the text into neutral, negative or positive.
Text: I think the food was bad.
Sentiment:"""
response = get_completion(prompt)
print(response)
Output
Effective, prompt engineering involves employing various techniques to optimize the output of language models.
Some techniques include:
For zero-shot prompting, no examples are provided for training. The LLM understands the prompt and works accordingly.
prompt = """I went to the market and bought 10 apples.
I gave 2 apples to the neighbor and 2 to the repairman.
I then went and bought 5 more apples and ate 1.
How many apples did I remain with?
Let's think step by step."""
response = get_completion(prompt)
print(response)
When zero shot fails, practitioners utilize a few-shot prompt technique where they provide examples for the model to learn and perform accordingly. This approach enables in-context learning by incorporating examples directly within the prompt.
prompt = """The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: The answer is False.
The odd numbers in this group add up to an even number: 17, 10, 19, 4, 8, 12, 24.
A: The answer is True.
The odd numbers in this group add up to an even number: 16, 11, 14, 4, 8, 13, 24.
A: The answer is True.
The odd numbers in this group add up to an even number: 17, 9, 10, 12, 13, 4, 2.
A: The answer is False.
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.
A:"""
response = get_completion(prompt)
print(response)
By teaching the model to consider the task when responding, make prompting better. Tasks that use reasoning can benefit from this. To achieve more desired results, combine with few-shot prompting.
prompt = """The odd numbers in this group add up to an even number: 4, 8, 9, 15, 12, 2, 1.
A: Adding all the odd numbers (9, 15, 1) gives 25. The answer is False.
The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7.
A:"""
response = get_completion(prompt)
print(response)
Now that you have a basic idea of various prompting techniques let’s use the prompt engineering technique to create an order bot.
The main purpose of using GPT-3 is for natural language generation. It supports lots of other tasks along with natural language generation. Some of these are:
Now that you have a basic idea of various prompting techniques let’s use the prompt engineering technique to create an order bot using OpenAI’s API.
This function utilizes the OpenAI API to generate a complete response based on a list of messages. Use the parameter as temperature which is set to 0.
def get_completion_from_messages(messages, model="gpt-3.5-turbo", temperature=0):
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature, # this is the degree of randomness of the model's output
)
return response.choices[0].message["content"]
We will use the Panel library in Python to create a simple GUI. The collect_messages function in a Panel-based GUI collects user input, generates an assistant’s response using a language model, and updates the display with the conversation.
def collect_messages(_):
prompt = inp.value_input
inp.value = ''
context.append({'role':'user', 'content':f"{prompt}"})
response = get_completion_from_messages(context)
context.append({'role':'assistant', 'content':f"{response}"})
panels.append(
pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
panels.append(
pn.Row('Assistant:', pn.pane.Markdown(response, width=600,
style={'background-color': '#F6F6F6'})))
return pn.Column(*panels)
The prompt is provided in the context variable, a list containing a dictionary. The dictionary contains information about the role and content of the system related to an automated service called OrderBot for a pizza restaurant. The content describes how OrderBot interacts with customers, collects orders, asks about pickup or delivery, summarizes orders, checks for additional items, etc.
import panel as pn # GUI
pn.extension()
panels = [] # collect display
context = [ {'role':'system', 'content':"""
You are OrderBot, an automated service to collect orders for a pizza restaurant.
You first greet the customer, then collects the order,
and then ask if it's a pickup or delivery.
You wait to collect the entire order, then summarize it and check for a final
time if the customer wants to add anything else.
If it's a delivery, you ask for an address.
Finally, you collect the payment
Make sure to clarify all options, extras, and sizes to uniquely
identify the item from the menu.
You respond in a short, very conversational friendly style.
The menu includes
pepperoni pizza 12.95, 10.00, 7.00
cheese pizza 10.95, 9.25, 6.50
eggplant pizza 11.95, 9.75, 6.75
fries 4.50, 3.50
greek salad 7.25
Toppings:
extra cheese 2.00,
mushrooms 1.50
sausage 3.00
Canadian bacon 3.50
AI sauce 1.50
peppers 1.00
Drinks:
coke 3.00, 2.00, 1.00
sprite 3.00, 2.00, 1.00
bottled water 5.00
"""} ]
The code sets up a Panel-based dashboard with an input widget and a button for initiating a conversation. When the button clicks, the ‘collect_messages’ function is triggered to process the user input and update the conversation panel.
inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…')
button_conversation = pn.widgets.Button(name="Chat!")
interactive_conversation = pn.bind(collect_messages, button_conversation)
dashboard = pn.Column(
inp,
pn.Row(button_conversation),
pn.panel(interactive_conversation, loading_indicator=True, height=300),
)
dashboard
On the basis of the given prompt, the bot behaves as an order bot for a Pizza Restaurant. You can see how powerful the prompt is and can easily create applications.
In conclusion, designing powerful prompts is a crucial aspect of prompt engineering for language models. Well-crafted prompts provide a starting point and context for generating text, influencing the output of language models. They play a significant role in guiding AI-generated content by setting expectations, providing instructions, and shaping the generated text’s style, tone, and purpose.
Ready to master the art of prompt engineering? Enroll in our “Crafting Powerful Prompts” course today and learn how to design prompts that unlock the full potential of language models. Join us now and elevate your prompt engineering skills!
Thus, mastering the art of prompt engineering empowers content creators to harness the full potential of language models and leverage AI technology, such as OpenAI’s API, to achieve their specific goals.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.
A. A prompt engineer designs and develops prompt systems, which are algorithms used in natural language processing to generate human-like text responses based on given inputs.
A. Prompt engineering is a rapidly evolving field with great potential for the future of AI technology and language processing.
A. Yes, anyone with a strong interest in AI and language processing can learn prompt engineering through online courses, tutorials, and hands-on practice
A. Prompt engineering typically involves coding skills, as engineers need to write and modify algorithms, work with programming languages, and understand the technical aspects of NLP frameworks.