Large Language Models (LLMs) have transformed how we interact with AI, offering immense potential for solving problems, generating insights, and creating content. Yet, their effectiveness depends on how well prompts are crafted: poorly written prompts often result in vague, irrelevant, or inaccurate responses.
In this article, we explore eight best practices for prompt engineering, featuring insights from data scientists from the KNIME community. These guidelines will help you design precise and effective prompts, ensuring your LLM produces the results you were looking for.
What is a prompt?
A prompt is the input or instruction given to a LLM to guide it towards generating a response. It is usually written by a human in a natural language and provides guidance and the necessary context for the model to understand the task at hand.
What is prompt engineering?
Prompt engineering is the process of crafting and refining prompts to guide an LLM towards generating accurate, relevant, and effective responses that meet the user’s needs. With the broad adoption of AI across industry and academia, prompt engineering has become a crucial element in the interaction with this technology.
On the left, the model generates a generic image of a sunset without specific details. On the right, by adding more specifics to the prompt, we obtain an image with a distinctive style, showcasing the impact of a well-crafted prompt.
Why is prompt engineering important?
Prompt engineering is important because AI models rely on input prompts to generate responses. Clear and specific prompts minimize ambiguity, enhance task alignment, fulfill the user's intent, and reduce the likelihood of errors or irrelevant outputs, improving the overall effectiveness and accuracy of AI applications.
How to write good prompts?
Writing effective prompts is an emerging yet highly demanded skill that requires time and practice. Mastering prompt writing involves an iterative approach, and as with any new tool, there is much to learn from those who have been using it successfully.
KNIME, the visual and open-source data science platform, is supported by a vibrant community of data professionals. To explore best practices in prompt engineering, we reached out to a few data experts in the KNIME community and asked for their top tips. Three contributors shared their insights:
- Angel Molina, data science consultant, social media influencer, and founder of MOLA DATA. Angel is also a KNIME Contributor of the Month (Jan ‘24).
- Martin Dieste, business consultant, digital enabler, and founder of Finnovation Flows. Martin is also a KNIME Contributor of the Month (Nov ‘24) and KNIME Certified Trainer.
- Francesco Alaimo, team lead at TIM and data science educator. Francesco is also a KNIME Certified Trainer.
Let’s get started!
1. Be specific
Angel Molina, data science consultant, suggested that when designing prompts, the number one rule is to be specific. Providing detailed instructions on the desired output will result in a more accurate and tailored response.
Explain the process of training a machine learning model | |
Summarize the key steps involved in training a machine learning model for a classification task, using a random forest algorithm. Present the steps in a concise, numbered list format. |
2. Put instructions first
The first few words of the prompt should clearly answer the question, "What do I want the LLM to do?". Whether it's summarizing, translating, etc., putting the instruction first helps avoid unnecessary processing or a delayed response to the core request.
I do not speak Spanish so I thought you could help me understand this text. Can you translate it from Spanish into English? | |
Translate this text from Spanish into English. |
3. Include examples
Examples are a great way to show the LLMs how you would like your response to look like. It is highly recommended to provide examples, whenever possible.
Create a dataset to analyze climate trends. | |
Create a dataset to analyze climate trends that looks like the example. Example: || Country | Emissions | Average Temperature | Precipitation || |
4. Structure your prompt
When crafting prompts for LLMs, clear organization is essential. Separating different parts of a prompt using distinct markers, such as ### or """""""", can help the model understand the structure and intent more effectively.
Find a good title for this paragraph {your text} | |
Paragraph: ###{your text}### |
For more complex requests, adopting a highly structured prompt can be beneficial. Martin Dieste, business consultant, digital enabler, and founder of Finnovation Flows, recommends using XML-style tags as an effective method for adding clarity and organization. This approach mimics the tree structure of XML attributes and elements and adapts it to the task at end. In this way, it becomes easier for the model to identify specific sections of the prompt, spot crucial instructions, or follow provided reasoning steps, enhancing the model’s overall ability to generate accurate and relevant responses.
Here is an example:
<role> You are a professional copywriter with expertise in crafting engaging product descriptions for eco-friendly goods. </role> <process> <step1>Analyze the features of the product provided in the input.</step1> <step2>Create a creative and persuasive product description.</step2> <step3>Conclude with a call-to-action to encourage purchase.</step3> </process> <task> Write a 100-150 word product description for an eco-friendly water bottle made of stainless steel. Features: - Capacity: 750 ml - Double-walled insulation (keeps drinks cold for 24 hours and hot for 12 hours) - BPA-free and leak-proof - Available in multiple colors - 100% recyclable packaging </task> <responseInstructions> Write the product description in a professional and engaging tone. Ensure it emphasizes the eco-friendly aspects of the product and includes the key features. Wrap your response inside these tags: <finalResponse>…your product description…</finalResponse>. Do not include any content outside the tags. </responseInstructions>
5. Break down tasks
If you are dealing with a complex task that implies reasoning, it’s best to break it down into a series of smaller tasks that illustrate the process and that the LLM can more easily follow. When that’s the case, it is beneficial to explicitly list the steps of the process ‒ similarly to how you would instruct a human to carry out a given task.
Analyze the research paper below. Research paper: ### {your paper} ### | |
Analyze the research paper below by following these steps: 1. Identify the central research question or problem the paper addresses. 2. Review the methodology used in the study and evaluate if it’s appropriate for the research question. 3. Examine the results presented in the paper and assess whether they are clearly supported by the data. 4. Analyze the discussion and conclusion to determine if the authors' interpretations and conclusions are justified. Research paper: ###{your paper}### |
6. Assign a persona
Assigning a specific role to the LLM helps it narrow its focus, ensuring that responses align with the desired expertise and context. Introduce statements like “imagine you are X” or “act as X”.
Review the code below. Code: ###{your code}### | |
Review the code below. Act as a cybersecurity expert whose role is to assess potential security risk. Code: ###{your code}### |
7. Specify the audience
Setting clearly who the intended target audience is ensures that the prompt generates responses tailored to their needs, preferences, and level of understanding. This specificity enhances the relevance and quality of the output.
Explain what is a linear regression. | |
Explain what is a linear regression to a middle school student that is not familiar with calculus. |
8. Learn and refine
Effective prompt engineering is usually not a static, one-time interaction. It’s a learning process where testing and refining your prompts is essential to achieve outputs that align with your needs.
Francesco Alaimo, team leader at TIM and data science educator, shared his experience with prompt engineering while developing a Retrieval Augmented Generation (RAG)-based chatbot. During development, he encountered a common issue in the responses generated by the LLM: they contained “hallucinations”. When the model couldn’t retrieve the pertinent context from the company’s knowledge base, it would still output a response that sounded plausible but it was actually incorrect.
After several iterations and prompt refinements, Francesco learnt that being extremely explicit with his prompts was crucial. He needed to clearly instruct the LLM to answer questions based exclusively on the available context in the knowledge base, and offer the model a graceful exit strategy if the proper context wasn’t available.
To generate the response, consider only the provided context. If the proper piece of information is not available within the provided context, output a counterfactual response. This is what a counterfactual response looks like for a given question: Question: “Can you do this thing?” Counterfactual response: “No, I cannot do this thing”. |
Learn from the KNIME community and master prompt engineering
Our top eight prompt engineering best practices were shaped through experimentation and iterative refinement of prompts by data experts within the KNIME community. This process highlights the power of iterative learning with this technology, as well as the incredible value of sharing knowledge and expertise within the data science community.
Start building your GenAI solutions using the KNIME AI extension, try out our best practices and share yours!