Prompting and Prompt Engineering

Prompt Engineering


Learning Objectives

  • You know what prompt engineering means.
  • You know of the existence of prompt engineering best practices.

The process of coming up with prompts that guide the model to generate text in a specific style or context is known as prompt engineering. Prompt engineering involves iteratively refining the prompt with the aim of reaching more desirable outputs.

When crafting prompts, it is necessary also to evaluate the quality of the outputs. This can be done by comparing the generated text to a manually created desired output, and evaluating the quality based on various criteria, such as correctness, relevance, coherence, and fluency.

Refining prompt aspects

There are multiple aspects in prompts that can be refined. A prompt typically has an explicit or implicit instruction that tells the model what to do, a context that provides information so that the model can understand the broader context, input data that the model is expected to process, and output formatting indicating the expected output format.

As a very brief example of a prompt that involves three out of the four aspects, consider the following sentence “Summarize the main research findings of quantum technology during the 21st decade in one sentence, focusing on advances in hardware.”

Summarize the main research findings of quantum technology during the 21st decade in one sentence, focusing on advances in hardware.

In the 21st decade, quantum technology research has achieved significant milestones in hardware, including the development of more stable and scalable qubits, enhanced quantum error correction, and the demonstration of quantum supremacy, leading to the advancement of quantum computers towards practical applications.

In the above prompt, the whole prompt acts as the instruction. It asks the model to create a summary of a topic within a specific time frame and with a focus on a specific aspect. The context involves the time, the field of study, and a specific area. There is no explicit input data, but the output formatting instruction asks to provide a response in one sentence.

As another example, the following instructs the model to transform a list of comma-separated values (CSV) into JavaScript Object Notation (JSON) format. It has an instruction, which is the transformation of the data, the input data, and the output formatting.

Please transform the following CSV file into a JSON object. The CSV data starts after ###. Only provide the JSON object as an output.

###
name,year
Philosopher's Stone,1997
Chamber of Secrets,1998
Prisoner of Azkaban,1999

[
{"name": "Philosopher's Stone", "year": "1997"},
{"name": "Chamber of Secrets", "year": "1998"},
{"name": "Prisoner of Azkaban", "year": "1999"}
]

Loading Exercise...

From simple to complex

When constructing prompts, it can be beneficial to start from a simple prompt and then add instructions to refine the prompt. In the following exploration, we outline the idea of going from simple to complex by asking the large language model to produce suggestions on what see in Helsinki. We start with a prompt “What would you suggest seeing in Helsinki?”.

What would you suggest seeing in Helsinki?

Helsinki, the capital of Finland, offers a blend of natural beauty, historic architecture, and modern design. Here are some of the top sights and attractions to consider when visiting Helsinki:

Suomenlinna Sea Fortress: A UNESCO World Heritage site, Suomenlinna is a historical maritime fortress and one of Finland's most popular sights. It's a great place to learn about Finnish history or just wander around and enjoy the sea views.

Helsinki Cathedral: Located in Senate Square, this iconic white cathedral is one of the city's most recognizable landmarks.

Market Square (Kauppatori): This bustling market by the sea offers a range of Finnish foods and handicrafts. It's especially lively during the summer.

Uspenski Cathedral: An Eastern Orthodox cathedral with stunning architecture, it offers a glimpse into the Russian influence on Finnish history.

Design District: Helsinki is known for its design culture, and this district is packed with design and boutique stores, galleries, and more.

Kiasma Museum of Contemporary Art: A striking modern building that houses an impressive collection of contemporary artworks.

Seurasaari Open-Air Museum: An island museum that showcases traditional Finnish life with old wooden buildings brought from all over Finland.

Ateneum Art Museum: The Finnish National Gallery's collection of classical art, which includes works by Finland's most renowned artists.

Oodi Helsinki Central Library: A striking modern piece of architecture that's much more than just a library. It's a community space with various amenities and beautiful views of the city.

Temppeliaukio Church (Rock Church): This unique church is excavated directly into solid rock, making it a popular attraction.

Linnanmäki Amusement Park: A historic amusement park, it's fun for all ages and offers a beautiful view of the city from its observation tower.

Esplanadi: A popular urban park in the heart of the city, great for relaxing, people-watching, or enjoying a coffee in one of the adjacent cafés.

Sauna Culture: Experiencing a traditional Finnish sauna is a must when visiting Helsinki. There are several public saunas like Löyly and Allas Sea Pool.

Hietaniemi Beach: If you're visiting in the warmer months, this is a popular sandy beach to enjoy the sun and take a swim.

Moomin Shop: For fans of the beloved Finnish children's book series by Tove Jansson, this shop is a must-visit.

Remember that the best way to experience Helsinki is to wander its streets, enjoy its parks, and engage with the locals. Whether you're into history, design, or nature, Helsinki has something to offer.

The list is quite long. A natural continuation would be to limit the number of choices, say to five. We can do this by adding information about the expected output — a constraint — to the prompt that asks to only provide five suggestions. Now, the prompt is “What would you suggest seeing in Helsinki? Provide only five suggestions.”

What would you suggest seeing in Helsinki? Provide only five suggestions.

Certainly! Helsinki is a vibrant city with a mix of historical and contemporary attractions. Here are five must-visit places:

Suomenlinna Fortress: A UNESCO World Heritage site, Suomenlinna is an inhabited sea fortress built on six islands. It's not just a popular tourist attraction, but also a local favorite for picnics and relaxation. The fortress provides a fascinating glimpse into Finnish military and maritime history.

Helsinki Cathedral: An iconic symbol of the city, this majestic white cathedral dominates the Senate Square. It's a great example of neoclassical architecture, and the steps leading up to the cathedral offer a popular gathering place and a fantastic view of the central part of the city.

Market Square (Kauppatori): Located by the sea in the city center, this bustling square is a hub of activity. It offers a variety of seasonal goods, traditional Finnish foods, and souvenirs. Nearby, you can also find the Old Market Hall, which is an indoor marketplace that has been serving customers since 1889.

Design District: Helsinki is renowned for its design and architecture. The Design District comprises several blocks packed with design and antique shops, fashion stores, museums, art galleries, restaurants, and showrooms. Here, you can explore Finnish design, from its classics to contemporary innovations.

Kiasma Museum of Contemporary Art: If you're interested in modern and contemporary art, Kiasma is a must-visit. The museum's architecture itself is a piece of contemporary art, and it houses an impressive collection of Finnish and international contemporary art.

These are just a few highlights of what Helsinki has to offer. The city's unique blend of East and West, its design-centric ethos, and its beautiful natural surroundings make it a memorable destination.

The number of suggestions was correctly limited to five. Let’s assume that we only want to walk around the city. Let’s add contextual information and ask for locations that can be reached by walking “Suggest only locations that can be reached by walking.” Now, the prompt is “What would you suggest seeing in Helsinki? Provide only five suggestions. Suggest only locations that can be reached by walking.”

What would you suggest seeing in Helsinki? Provide only five suggestions. Suggest only locations that can be reached by walking.

Certainly! Helsinki is a walkable city with many attractions close to each other. Here are five must-visit sites that are accessible by walking in Helsinki:

Helsinki Cathedral (Tuomiokirkko): Located in Senate Square, this iconic white church with green domes dominates the city skyline. It's not just an architectural masterpiece but also an essential part of Helsinki's history.

Market Square (Kauppatori): Located by the sea, this bustling market offers a mix of Finnish products and food. It's a great place to experience local culture, grab a bite, or just enjoy the harbor views.

Esplanadi Park (Esplanadin puisto): A green oasis in the middle of the city, the Esplanadi is lined with cafes, restaurants, and boutiques. It's an ideal spot to relax, people-watch, or enjoy events that frequently take place there.

Old Market Hall (Vanha Kauppahalli): Located near Market Square, this historic building houses various vendors selling fresh produce, meat, fish, baked goods, and more. The interior has retained its old-world charm, and it's a delightful place to experience Finnish cuisine.

Uspenski Cathedral: This is the largest orthodox church in Western Europe, with its golden cupolas and red-brick facade offering a stark contrast to the Helsinki Cathedral. Overlooking the harbor, it offers a unique blend of Russian and Finnish history and provides great views of the city.

All these attractions are centrally located and can be easily reached by walking. Enjoy your time in Helsinki!

The model has correctly filtered out locations that cannot be reached by walking and continues to outline only five locations. Now, assume that we would only wish the names of the locations and nothing else. Let’s add an additional expectation on the output by asking to “Only list the names of the locations.”. Now, the prompt is “What would you suggest seeing in Helsinki? Provide only five suggestions. Suggest only locations that can be reached by walking. Only list the names of the locations.”

What would you suggest seeing in Helsinki? Provide only five suggestions. Suggest only locations that can be reached by walking. Only list the names of the locations.

Certainly! Here are five walkable attractions in Helsinki:

Helsinki Cathedral
Market Square (Kauppatori)
Uspenski Cathedral
Esplanadi Park
Old Market Hall (Vanha Kauppahalli)

Now, we have a refined list of tourist attractions that we could, maybe, tackle even in a single day.

Try it out!

Open up the AI chat from the right lower hand side of the screen. Ask the AI to generate a list of attractions for a favorite city of yours. Start with a big city that should be quite well known, and then try out the same with a smaller one. Does the model produce outputs that are valid?

Best practices

While forming prompts is still mostly an exploratory process (hence the term prompt engineering), best practices for prompting are starting to emerge. These are offered in the wild in blog posts, research papers, and guidelines from large language model providers. As an example, the OpenAI Prompt Engineering Guide highlights the following six strategies for constructing prompts:

  • Write clear instructions
  • Provide reference text
  • Split complex tasks into simpler subtasks
  • Give the model time to “think”
  • Use external tools
  • Test changes systematically

The training data of newer large language models has also included information about how to prompt large language models. Thus, it is also possible to ask for prompting best practices from large language models.

Loading Exercise...

Prompt book for work

If you work involves prompting, it is highly meaningful to start forming a prompt book for work, if you haven’t already started to do one.

A prompt book contains a collection of prompts that you have tested and that you have found to work well with your daily tasks. The prompt book can be used as a reference when constructing new prompts, and it can also be shared with colleagues, allowing them to benefit from your prompting efforts.