👋 Hi, this is Sagar with this week’s edition of the Mindful Matrix newsletter. This is my third edition on GenAI/LLM learning series. Check out my previous editions on this series here.
In this new age of LLMs, prompts are king. Thus in this edition, I’ll be discussing about Prompt Engineering, which is a new discipline for developing and optimizing prompts to efficiently use LLMs.
Before we begin, let’s take a look at what you're about to get from this article -
What is Prompt and Prompt engineering?
Elements of Prompt.
General tips for designing prompt.
Type of prompting techniques.
LLMs are not inherently capable of performing complex tasks such as math problems or commonsense reasoning. They require guidance and optimization to extend their capabilities and broaden the range of tasks they can perform effectively, which can be achieved through the use of prompts.
What is Prompt and Prompt engineering?
Prompts are a specific set of inputs provided by you, the user, that guide LLMs to generate an appropriate response or output for a given task or instruction. The output will be based on the existing knowledge the LLMs has.
Prompt engineering refers to the practice of crafting and optimizing input prompts to effectively use LLMs for a wide variety of applications. The quality of prompts that you provide to LLMs can impact the quality of their responses.
In other words, prompt engineering is the art of communicating with an LLM. It's a bit like learning how to ask the right questions to get the best answers.
The thing about prompt engineering is that it's pretty straightforward. You don't need to be a tech expert to do it, which is great for most people.Â
Elements of Prompt
A prompt may contain the following:
Instruction: Task or instruction for the model (eg: classify, summarize, …)
Input: Input statement or question for the model to generate a response.
Context: Additional relevant information to guide the model’s response (eg: examples to help the model better understand the task.)
Output Format: Specific type or format of the output generated by the model.
Not all four elements are necessary for a prompt, and the format depends on the specific task being performed.
Sample prompt:
Another Sample prompt :
Instruction : Answer the question based on the context below. Context: LLMs are the latest models used in NLP. Their superior performance over smaller models has made them incredibly useful for developers building NLP enabled applications. These models can be accessed via Hugging Face's `transformers` library, via OpenAI using the `openai` library, or via AWS Bedrock using `AWS SDKs`. Question: Which libraries and model providers offer LLMs?
Prompt templates
Frameworks like langchain provides prompt template classes to make constructing prompts with dynamic inputs easier. Ex -
from langchain import PromptTemplate template = """ Question: {query} """ prompt_template = PromptTemplate( input_variables=["query"], template=template ) print( prompt_template.format(query="What are top 5 applications of LLM?") ) >> Output : Question: What are top 5 applications of LLM?
We can fed the output of this directly into an LLM :
bedrock_llm(prompt_template.format(query="What are top 5 applications of LLM?" ))
General tips for designing prompt
Be clear and concise - Avoid ambigous language.
Ex - "Explain the key concepts of LLM and it’s applications in software " is better prompt than "Tell me about LLM"
Format prompts properly : Separate the input, instruction, context, and output directions. Prompts with the format tends to achieve better results than prompts with random formatting.
Ex - Using some clear separator like "#" to separate the instruction and context. Sample prompt :
### Instruction ### Translate the text below to Spanish: Text: "hello!"
Similarly context can be provided manually or through RAG pipeline in the prompt which I covered in my previous artile on desigining RAG application.
Focus on what the model needs to do - Try not to focus on what model should not do. Positive instructions are more effective.
Ex - "Explain the concept using simple language" is better prompt than "Do not use financial jargon in the response "
Provide examples to articulate the desired output format
Sample Prompt :
Extract the company and people names mentioned in the text below. Text: Amazon was started by Jeff Bezos. Microsoft was started by Bill gates and Paul Allen. Desired format: Company names : <comma_separated_list_of_company_name> People names : <comma_separated_list_of_people_name>
Above is better prompt than following -
Extract the company and people names mentioned in the text below. Text: Amazon was started by Jeff Bezos. Microsoft was started by Bill gates and Paul Allen.
Control the length of output - Explicitly requesting the output length in the prompt helps in faster inference and cost effective as most services charges per token generated.
Ex - "Write an article on Climate change in less than 150 words" is better prompt than "Write an article on Climate change" or "What is Climate change"
Keep in mind that crafting effective prompts is really an iterative process that requires a lot of experimentation to get optimal results. Using a simple playground from OpenAI or other LLM models is a good starting point.
You can start with simple prompts and keep adding more elements and context as you aim for better results.
Type of Prompting Techniques
Zero-shot Prompting
Zero-shot prompting refers to the ability of an AI model to generate meaningful responses or complete tasks without any prior training on specific prompts. The larger and more capable the LLM, the better results zero-shot prompting will yield.
Classify the text into neutral, negative or positive. Text: I think the product was okay. >> Output : neutral
Note that in the prompt above we didn't provide the model with any examples of text alongside their classifications, the LLM already understands "sentiment" -- that's the zero-shot capabilities at work.
When zero-shot doesn't work, it's recommended to provide demonstrations or examples in the prompt which leads to few-shot prompting.
Few-shot Prompting
In contrast to zero-shot prompting, few-shot prompting involves training an AI model with only a small amount of data or examples (also called "shots")
This technique allows the model to quickly adapt and generate responses based on limited examples, plus the instructions provided by the user.
Determine the sentiment of the query based on the examples and their sentiment. Example : Great product, 10/10: Sentiment : positive Example : Didn't work very well: Sentiment : negative Example : Works fine Sentiment : neutral query : Working as expected Sentiment: Output >> neutral
Examples can guide the model, but do not always enhance its performance. Sometimes a well crafted zero short prompt can be more effective than providing multiple examples.
Chain-of-Thought Prompting
Chain-of-Thought (CoT) prompting is technique that breaks down complex tasks through intermediate reasoning steps. This allows LLMs to overcome difficulties with some reasoning tasks that require logical thinking and multiple steps to solve, such as arithmetic or commonsense reasoning questions.
This encourages model to explain it’s reasoning process by decomposing the solution into a series of steps, mimicking human-like conversation flow. This behaviour can be facilitated through various strategies (Zero shot, few short, etc).
This is a basis for other prompting techniques such as ReAct which separates out the task’s decomposition and it’s solving.
ReAct Prompting
CoT aims to imitate how humans think about problems, but it lack access to the external world or inability to update its knowledge which lead to issues like fact hallucination and error propagation, which is where ReAct Prompting is useful.
ReAct allows language models to generate verbal reasoning traces and text actions concurrently.
Actions receive feedback from the external environment ("Env"), while reasoning traces update the model's internal state by reasoning over the context and incorporating useful information for future reasoning and action.
As shown below, there are various types of useful reasoning traces, e.g., decomposing task goals to create action plans, injecting commonsense knowledge relevant to task solving, and so on.
Therefore, in order to implement ReAct, you need:
An environment where a text action is chosen from various options based on the environment's internal state and then generates a text observation in response.
An output parser framework that stops text generation after producing a valid action, executes it in the environment, and returns the observation by appending it to the generated text so far and prompting the LLM.
Human-generated examples of intermixed thoughts, actions, and observations in the environment to use for few-shot learning.
Langchain provides a mechanism to implement ReAct through framework like Agents and Tools. We will cover these in details in upcoming editions where I’ll demonstrate these concepts in action through developing LLM powered applications.
Although there are other prompting techniques like TreeOfThought (ToT), Self-consistency, and Automatic Prompt Engineer (APE), the above method lays a foundational ground for building more complex LLM applications.
Final Thought
As this field continues to evolve with new methodologies emerging, it's essential to grasp these fundamentals of prompt engineering, and constructing good prompts is a crucial skill for those building with LLMs.
Interesting reads you don’t want to miss
My 3 strategies to manage my workload (before reaching burnout in Big Tech) by
Done is better than Perfection by
Exactly what to say in code reviews by
How I Stopped Overthinking Last Week by
Cross-team drama draining your team? by
In case you missed my previous articles on this series…
If you found this useful, please share it with your network and consider subscribing for more such insights.
If you haven’t subscribed, or followed me on LinkedIn, I’d love to connect with you. Please share your thoughts, feedback, and ideas, or even just to say hello!
Love this, Sagar! Full of great tips. Bookmarked for my next prompt ;)