Creating prompts for LLM models, such as ChatGPT, can be considered a new art. From structuring prompts to using context, it is possible to improve the conversational experience and extract maximum value from interactions with LLMs. Through practical examples, we will see how to best structure prompts to generate engaging and meaningful conversations with AI based on the set task.

Share

Reading time: 7 minutes

In Article Prompt engineering, we introduced the figure of the prompt engineer. This figure is responsible for studying large language models (LLMs) in order to define the best structure of input data to achieve the best results. As we have seen, there are some suggestions of how best to structure the prompts, although nevertheless only with practice and experience will the expected results be achieved. To encourage the proper use of LLMs, we will look below at some prompts related to real use cases by introducing some key concepts along the way. Indeed, often the best way to learn concepts is through examples. Let’s get started then!

Generations of resumes

One of the standard tasks in natural language generation is summary generation, also called summarization. Over time, several algorithms have been developed to generate a summary from a collection of texts. The advent of LLMs has far surpassed what has been achieved so far.

The summary can include many different aspects and domains. Indeed, one of the most promising applications of language models is the ability to summarize articles and concepts into quick and easy-to-read summaries. Let us try a basic summary task using prompts.

Suppose we are interested in learning about antibiotics, we could try a prompt like this:

				
					Explain the antibiotics
R:

				
			

Output:

				
					Antibiotics are medicines used to treat bacterial infections in the body. They work by either killing the bacteria or stopping their growth. It's important to note that antibiotics don't work against viral infections like the common cold or flu. Using antibiotics correctly and only when necessary helps prevent the development of antibiotic resistance, which can make some bacteria harder to treat in the future.

				
			

The answer obtained is inherent in the question. But what is the purpose of “R:” at the end of the prompt? This is an explicit prompt format that is used in answers to questions. Here we used it to tell the model that there is an additional wait. In this example, it is not obvious how it is useful versus not using it, but consider that it might affect the result depending on the LLM used. Suppose this information is too much and we want to summarize it further. In fact, we can instruct the model to summarize in a sentence like this:

				
					Antibiotics are a type of drug used to treat bacterial infections. They work by killing bacteria or preventing them from reproducing, allowing the body's immune system to fight the infection. Antibiotics are usually taken orally in the form of pills, capsules or liquid solutions, or sometimes given intravenously. They are not effective against viral infections, and their inappropriate use can lead to antibiotic resistance.
Explain the above in one sentence:


				
			

Output:

				
					Antibiotics are drugs that combat bacterial infections by either destroying the bacteria or inhibiting their reproduction, enabling the body's immune system to tackle the infection, typically administered orally or intravenously, but they are ineffective against viral infections and misuse can lead to antibiotic resistance.


				
			

Without paying too much attention to the accuracy of the output above, the model tried to summarize the paragraph in one sentence.

Information extraction

Although language models are trained to perform natural language generation and related tasks, they are also capable of performing natural language processing (NLP) techniques such as classification and named entity recognition.

We can, for example, ask to extract the object described in a paragraph!

				
					Author contribution statements and acknowledgements in research papers should clearly and specifically indicate whether, and to what extent, the authors used AI technologies such as ChatGPT in the preparation of the manuscript and analysis. They should also indicate which LLMs were used. This will enable editors and reviewers to examine manuscripts more closely for potential biases, inaccuracies, and improper crediting of sources. Similarly, scientific journals should be transparent about the use of LLMs, such as in the selection of submitted manuscripts.
Cite the large language model-based product mentioned in the previous paragraph:



				
			

Output:

				
					The large language model-based product mentioned in the previous paragraph is "ChatGPT."



				
			

There are many ways to improve on the results we have just seen, but these examples already show us the potential of LLMs.

By now it should be obvious that you can ask the model to perform different tasks simply by giving it instructions on what to do. This is a powerful capability that developers of artificial intelligence products are already using to create impressive products and experiences.

Answering questions

One of the best ways to get the model to respond to specific answers is to improve the format of the prompt. A prompt can combine instructions, context, input and output indicators to achieve better results (see article XXX). Although these components are not mandatory, it is good practice to include them because the more specific the instructions, the better the results. The following is an example of what a more structured prompt might look like.

				
					Answer the question based on the underlying context. Answer briefly and concisely. Answer "Uncertain about answer" if you are unsure of the answer.
Context: Teplizumab has its roots in a New Jersey pharmaceutical company, Ortho Pharmaceutical. Here scientists generated an early version of the antibody, called OKT3. Originally obtained from mice, the molecule was able to bind to the surface of T cells and limit their killing potential. In 1986 it was approved to help prevent organ rejection after kidney transplantation, making it the first therapeutic antibody approved for human use.
Question: From what was OKT3 originally derived?
Answer:




				
			

Output:

				
					OKT3 was originally derived from mice.




				
			

Text classification

So far we have used simple instructions to perform a task. As prompt engineers, you will have to get better at providing more precise instructions. But that is not all! You will also find that, for more difficult use cases, providing only instructions will not be enough. You must, in fact, think more about context and the different elements that can be used in a prompt. Other elements that can be provided are input data or examples.

Let’s see how we can use a prompt to classify text, without necessarily building an ad hoc dataset and model to do so.

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





				
			

Output:

				
					Sentiment: Positive




				
			

We gave the instruction to classify the text and the model responded with “Sentiment: Positive,” which is correct. There is nothing wrong with that, but let’s say that what we need is for the model to provide the label in the exact format we want. So instead of the whole sentence, we want it to return only one of the labels defined at the beginning. How can we achieve this? There are several ways to do this. Since we care about specificity, the more information we provide to the prompt the better the results will be. We can try to provide examples to specify the correct behavior. Let’s try again:

				
					Classify the text into neutral, negative or positive. 
Text: I think the vacation is going well.
Sentiment: neutral
Text: I think the food was good. 
Sentiment:



				
			

Output:

				
					positive



				
			

Attention

Start a new chat each time if not the memory of previous answers may affect the result obtained!

Conversation

Perhaps one of the most interesting things you can achieve with prompt engineering is to instruct the LLM system on how to behave, its intentions, and its identity. This is especially useful when building conversational systems such as customer service chatbots.

For example, we create a conversational system that can generate more technical and scientific answers to questions. Notice how we are explicitly telling it how to behave through instructions. This is sometimes referred to as role prompting.

				
					The following is a conversation with an artificial intelligence (AI) research assistant. The assistant's tone is technical and scientific.
Person: Hello, who are you?
AI: Hello! I am an AI research assistant. How can I help you today?
Person: Can you tell me about the creation of black holes?
AI:



				
			

Output:

				
					Certainly. Black holes are formed through a process known as gravitational collapse. This occurs when a massive star undergoes a catastrophic event, such as a supernova explosion, at the end of its life cycle. 

During a supernova, the outer layers of the star are expelled into space, while the core collapses under its own gravitational pull. If the core's mass is above a critical threshold (approximately 2.5 to 3 solar masses), it will continue to collapse indefinitely, forming a region of infinite density and curvature known as a singularity at its center. This singularity is surrounded by an event horizon, which is the boundary beyond which no information or matter can escape, including light.

The size of the event horizon is determined solely by the mass of the black hole and is proportional to it. For instance, a black hole with ten times the mass of our Sun will have an event horizon roughly ten times larger in diameter.

It's important to note that black holes can also form from other processes, such as the collapse of massive clouds of gas without the need for a precursor star. These are known as primordial black holes.

Is there a specific aspect of black holes you would like to explore further?


				
			

Our AI search assistant seems a little too technical, doesn’t it? Okay, let’s change this behavior and instruct the system to give more accessible answers.

				
					The following is a conversation with an AI research assistant. The assistant's answers should be easy for even elementary school students to understand.
Person: Hello, who are you?
AI: Hello! I'm an AI research assistant. How can I help you today?
Person: Can you tell me about the creation of black holes?
AI:
				
			

Output:

				
					Sure! Black holes are like really special objects in space. They happen when a big star runs out of energy and collapses. This makes a super strong force that sucks in everything around it, even light! That's why it's called a "black" hole. It's like a place where things disappear. They're really interesting, but we can't see them directly because they don't give off light. We learn about them by studying how they affect things around them. Isn't space cool? 😊
				
			

The result is greatly improved. An emoji was used to make the assistant more “friendly”. Excellent result! Obviously this is a starting point, and with practice we can achieve more and more detailed results in line with the application context we have in mind.

Code generation

One application in which LLMs are very effective is code generation. A large number of code generation operations can be performed with intelligent hints. Let’s look at some examples below.

First, let’s try a simple program that greets the user.

				
					/*
Ask the user his name and to "Hello."
*/
				
			

Output:

				
					Certainly! Here is a simple code snippet in C++ that asks the user for their name and then greets them:

```cpp
#include <iostream>
#include <string>

int main() {
    std::string name;

    std::cout << "What is your name? ";
    std::getline(std::cin, name);

    std::cout << "Hello, " << name << "!" << std::endl;

    return 0;
}
```

This program prompts the user to enter their name, reads it, and then prints out a greeting using their name.

				
			

It can be seen that it was not necessary to specify the language to be used. The system automatically decided which language to use. In some cases it may ask for it to be more precise in the answer.

Let’s change the level a little bit. I want to show you how powerful LLMs can be with a little more effort in prompt design.

				
					"""
Table departments, columns = [DepartmentId, DepartmentName]
Student table, columns = [DepartmentId, StudentId, StudentName]
Create a MySQL query for all students in the Computer Science department.
"""
				
			

Output:

				
					To retrieve all students in the Computer Science department from the given tables, you can use a simple SQL query with a `JOIN` statement. Assuming that the DepartmentId for Computer Science is known (let's assume it's 1 for this example), the query would look like this:

```sql
SELECT Student.StudentId, Student.StudentName
FROM Student
JOIN Departments ON Student.DepartmentId = Departments.DepartmentId
WHERE Departments.DepartmentName = 'Computer Science';
```

This query joins the `Student` table with the `Departments` table based on the `DepartmentId` column. Then, it selects the `StudentId` and `StudentName` columns from the `Student` table for rows where the `DepartmentName` in the `Departments` table is 'Computer Science'.

				
			

This is very impressive. In this case, we provided data about the database schema and asked it to generate a valid MySQL query. In addition to the SQL code, we also had an explanation of how the query is structured.

Reasoning

Perhaps one of the most difficult tasks for an LLM today is one that requires some form of reasoning. Reasoning is one of the areas that excites me the most, because of the kinds of complex applications that can emerge from LLMs.

There have been some improvements in tasks that require mathematical skills. That said, it is important to note that current LLMs struggle with reasoning tasks, so this requires even more advanced prompt engineering techniques. Let us look at some basic examples to show arithmetic skills.

				
					What is 9,000 * 9,000?
				
			

Output:

				
					The product of 9,000 multiplied by 9,000 is 81,000,000.
				
			

Let’s try something more difficult.

				
					The sum of the odd numbers in this group generates an even number: 15, 32, 5, 13, 82, 7, 1. 
R:

				
			

Output:

				
					Let's find the sum of the odd numbers in the group:

Odd numbers in the group: 15, 5, 13, 7, 1

Sum of odd numbers = 15 + 5 + 13 + 7 + 1 = 41

Since 41 is an odd number, the statement "The sum of the odd numbers in this group generates an even number" is not true for this particular group of numbers.

				
			

The comprehension skills are truly remarkable in that he gave not only the answer but also the process to reach it.

More To Explore

Python language

Pandas: data analysis with Python [part 1].

Data scientists continually need to read, manipulate, and analyze data. In many cases they use specific tools, but sometimes they need to develop their own code. To do this, the Pandas library comes to our aid. Let’s learn about its data structures, how we can read data from different sources and manipulate it for our purposes.

Artificial intelligence

Gradio: web applications in Python for AI [Part 3]

With Gradio, it is possible to create web applications for our machine learning and AI models in just a few lines of code. Through some examples, we will see the advanced features available, such as authentication, caching, and input file processing. We will also build a chatbot and an image classifier from pre-trained models. Finally we will discuss how to deploy our project in a few simple steps.

Leave a Reply

Your email address will not be published. Required fields are marked *

Progetta con MongoDB!!!

Acquista il nuovo libro che ti aiuterà a usare correttamente MongoDB per le tue applicazioni. Disponibile ora su Amazon!

Design with MongoDB

Design with MongoDB!!!

Buy the new book that will help you to use MongoDB correctly for your applications. Available now on Amazon!