Prompt Engineering: From Art to Science – II

Digital Core Transformation

June 8, 2023

In the previous part of this article, I covered the need for prompt engineering and some basic techniques of prompt engineering, viz., simplicity, clarity, conciseness, prompt format, and context. Let’s continue from where we left.

Draw clearly distinguishing input boundaries and describe them:

A prompt may often have several sections. For example, if you want to summarize a paragraph, your prompt will include both the input paragraph and the instructions to summarize it. There could also be multiple sections in a prompt. For example, if you want to find the answer to a question within a given paragraph, your prompt has 3 sections, namely, the paragraph, the question, and instructions to find the answer. In this case, you need to clearly distinguish between the sections. Using delimiters is a good technique to draw this boundary. Here is an example:

In the first version of the prompt, I’m using headers as the boundary and referencing them in the instructions. The upper case is maintained in both inputs as well as instructions for reference clarity. In the second version, I’m using unique delimiters like tilde (~) and carrot (^) and refer to the same in the instruction. You can also use multiple characters (like, ~~~) to maintain uniqueness in such a way that the delimiter is not accidentally present in the input.

Drawing clear boundaries is very important. In many cases where the input data has multiple sections, AI may mix up data across sections and produce undesirable results. Conversely, when you see weird results, one important check is to see if there is a data leak from one section to the other. In such cases, you can also try the header and footer for each section that indicates the beginning and end of each section to plug the data leak.

Structure the output:

Provide the output structure expected out of AI as part of the prompt. The purpose is not only to format the output but even more importantly, it helps AI to understand what it must do.

The above prompt clearly instructs what is needed by defining the output format. It also limits AI from talking too much while also standardizing the output. The same technique can be used in the context of coding by asking for the output in formats like HTML, XML, JSON, etc.

Define the AI Persona:

It will be of immense value to define the persona of the AI. I’ve seen AI believe the persona you assert, and this greatly influences its response. For example, for the previous prompt, you can add that,

The above definition sets the expectation of how AI should behave. It is also a very good place to set AI’s tone. For example, “You are polite/sarcastic/…”.

Persona can be defined at the beginning of the instruction section in the prompt, in the case of models like Davinci. However, in models like turbo, role is a key part of the API payload, and the system role can be used to define the AI’s persona.

Punctuations, New Lines, & Whitespaces matter!

It really matters! For example, the same input can produce a completely different/undesired output if there are unwanted spaces or new lines. Also, apt punctuation adds clarity to the prompt and helps AI better understand it. However, not always is the world logical… so don’t expect it always! OK, let me explain. Say, you have given a prompt with instructions as bullet points. Now, the result may vary dramatically if you have or have not ended the bullets with a period(.) So it’s a good idea to try out these alternatives if your AI counterpart is driving you crazy (at times, this one can drive you crazy!). Also, on a side note, be aware that each punctuation is treated as a token – so, punctuations are costly characters and bloat your context window in some cases like the one involving lengthy JSON prompts/responses.

I believe you have enough to practice until we meet again. As always, keep prompting. ?

About the Author

Ramkumar NJ

Ramkumar NJ

Ramkumar is a hands-on Technology Leader, Solutions Architect, Teacher & a Student. He has over 2 decades of experience across ERP, Cloud, Conversational AI and ML. Currently, he is architecting AI solutions for enhancing employee productivity in Tech industry and for multiple corporate initiatives involving AI/ML. As a passionate mentor, he is also an adjunct faculty in BITS, Pilani. His varied interests include the intersection of technology & law and is a student at National Laws School studying multiple Lawtech subjects.

Read more Read more image

Related Blogs

Every outcome starts with a conversation

Ready to Pursue Opportunity?

Connect Now

right arrow

ready_to_pursue
Ready to Pursue Opportunity?

Every outcome starts with a conversation