- AI with Armand
- Posts
- The Basics of Prompt Engineering
The Basics of Prompt Engineering
Talking to AI
Welcome to the 173 new members this week! This newsletter now has 48,179 subscribers.
The Basics of Prompt Engineering
If you're here, you've likely heard of ChatGPT and GPT-4, and might be familiar with the term prompt engineering. In today’s issue, I'll introduce you to the basics of talking to AI, focusing on good prompt engineering practices.
Today, I’ll cover the following:
Understanding How Generative AI Works
What is a Prompt?
Designing a Prompt
Basic Prompt Examples
Advanced Prompt Examples
What is Prompt Engineering?
Prompting Tips and Tricks
Let’s dive in 🤿
Understanding How Generative AI Works
Before we focus more deeply into prompt engineering, let's discuss generative AI more broadly and define some key concepts. AI has been around for quite some time. Companies and other institutions in various fields have used it with different degrees of success for years. For example, AI has been used to recommend movies and shows on platforms like Netflix. In recent years, AI models have achieved new feats, such as beating professional chess or Go players. But why is AI, all of a sudden, on the cover of major publications? A few innovations have come into play and are categorized under the new label of generative AI.
Generative AI not only classifies inputs but also generates new content in response to natural language, making prompt design crucial.
Generative AI models, such as large language models (e.g., GPT-4, Llama 3), predict the next token and can engage in realistic conversations.
Text-to-image models (e.g., DALL-E , Stable Diffusion) and other applications like text-to-music and action transformers translate text into various forms of media and actions.
These models are based on transformer architecture and incorporate techniques like reinforcement learning with human feedback and diffusion models, exhibiting zero-shot learning capability.
What is a Prompt?
Now that we've introduced some key concepts for generative AI, let's focus on the one essential for this post: the prompt.
What is a prompt? Users interact with generative AI models through textual input in natural language. You tell the model what to do through a text interface, and the model tries to accomplish the task by giving you a response.
We define the prompt as the natural language input to a generative AI model where the user tells the model what to do.
The prompt for image generation AI models is mainly the description of the image you want to generate.
For example, "a fat crocodile with a gold crown on his head, wearing a three-piece suit, 4K, professional photography, studio lighting, LinkedIn profile picture, photorealistic."
Result of the Prompt Above
In the case of large language models (LLMs) like GPT-4 or Llama 3, the prompt can range from a simple question, such as "Who is the president of the US?" to a complicated problem with various data inputs. A prompt can also be a vague statement like, "Tell me a joke, I'm feeling down today." In generative task-oriented models, the prompt can be extremely high-level, such as "I need to organize a one-week trip to Greece."
Designing a Prompt
If a prompt is just natural language text, why learn about it? AI models have quirks despite their intelligence since they only predict the next word and might struggle with complex topics. Understanding their limitations and past experiences is crucial; a poorly designed prompt can cause hallucinations, while a well-designed one can provide valuable insights. Let's explore the basic elements of a prompt, which can include:
Instructions: For example, "Write a 3-paragraph long love letter."
Questions: For example, "What are some good examples of things to say in a love letter?"
Input Data: For example, "John is a 24-year-old accountant from California who is in love with Mary, a 24-year-old computer programmer from Arkansas. Write a 3-paragraph love letter from John to Mary."
Examples: For example, "My boyfriend really likes 'La La Land,' 'Her,' and 'Crazy, Stupid, Love.' He doesn't like 'Ghost' and 'Notting Hill.' Write a love letter for him."
Thinking about how to structure your prompt will lead to better results.
Basic Prompt Examples
A prompt can include instructions, questions, input data, and examples as a reminder. To obtain a result, either instructions or questions must be present. Let's look at a few examples using GPT-4:
Question + Instructions:
"How should I write my college admission essay? Give me suggestions about the different sections I should include, what tone I should use, and what expressions I should avoid."
Instructions + Input Data:
"Given the following information about me, write a four-paragraph college essay. I'm originally from Barcelona, Spain. While my childhood had different traumatic events such as the death of my father when I was only six, I still think I had a quite happy childhood."
Question + Examples:
"Here are some examples of music I really like: Radiohead, Lana del Rey, Rosalia, Bon Iver, and Andrew Bird. I do not like Coldplay, Taylor Swift, or Bruno Mars. What other music would you recommend?"
These examples illustrate how a general-purpose large language model can be tailored for specific tasks by providing detailed and structured prompts.
What is Prompt Engineering?
Prompt engineering is a rapidly growing discipline focused on designing optimal prompts for generative models, potentially replacing other machine learning aspects like feature or architecture engineering. It requires understanding both the goal and the AI model, as different models respond differently to the same prompts. Scaling prompts involve creating templates that can be programmatically modified based on a dataset or context. Like any engineering field, prompt engineering is iterative, requiring exploration and processes similar to software engineering, such as version control and QA. Tools to support this iterative process are also emerging.
Advanced Prompt Examples
Now that we have set the foundation for creating prompts with basic components let's get more creative and explore some advanced techniques and problems with generative AI and how to mitigate them.
One important technique is chain-of-thought prompting, which explicitly encourages the model to be factual or correct by following a series of steps in its reasoning. For example, the prompt "What European soccer team won the Champions League the year Barcelona hosted the Olympic games?" followed by "Q: Repeat question. A: Let's think step by step. Give reasoning; therefore, the answer is final answer" helps guide the model to a correct response.
Another advanced technique is prompting the model to cite reliable sources. For example, "What are the top three most important discoveries that the Hubble Space Telescope has enabled? Answer only using reliable sources and cite those sources." This approach helps verify the factual accuracy of the response.
It's also important to note that models like GPT-4 do not have access to the current web and have been trained with data that can be over two years old. Methodologies such as RAG, which combine LLMs with access to knowledge bases or the web, are more reliable for current information.
Prompt Engineering Tips and Tricks
Here are a few tips and tricks for creating effective prompts:
Order of Examples: Large language models read forward, completing texts. It helps to provide instructions before the examples and experiment with different orders of prompts and examples.
Affordances: Define functions in the prompt that the model is explicitly instructed to use when responding. For example, instruct the model to use a "calc" function for mathematical expressions.
Different Languages: LLMs can understand and speak multiple languages. You can interact with the model in your native language or use it for programming tasks.
Use of Caps and Exclamation Marks: Models sometimes respond better to forceful language. Using all caps and exclamation marks can ensure the model follows instructions more strictly.
As prompt engineering grows and expands, stay updated by reading articles, watching videos, and experimenting. Soon, you'll discover new tips and tricks and share them with others.
Wrapping up!
Prompt engineering is an essential skill for interacting effectively with generative AI models. By understanding the basics of prompt design, incorporating advanced techniques, and following practical tips and tricks, you can enhance the performance and reliability of AI responses. As AI technology evolves, prompt engineering will play a critical role in unlocking the full potential of generative AI models.
Enjoy the weekend, folks! Summer is here! ☀️
Armand
Whenever you're ready, learn AI with me:
The 15-day Generative AI course: Join my 15-day Generative AI email course, and learn with just 5 minutes a day. You'll receive concise daily lessons focused on practical business applications. It is perfect for quickly learning and applying core AI concepts. 15,000+ Business Professionals are already learning with it.
Learn the basics of Prompt Engineering in this course:
Reply