"The fact that you can do something with a Generative AI model does not mean that it's okay or the right thing to do."

Because Prompt Engineering is all the rage. And I really want to learn how to better interact with ChatGPT (LLMs), I sought out a LinkedIn Learning Path on Prompt Engineering and proceeded with Develop Your Prompt Engineering Skills

What surprised me while taking this Learning Path is the notion of Prompt Hacking. Never thought that the classic SQL Injection is possible while using LLMs. Given this day & age where scams and spams are everywhere, I guess we really have to be able to think from an adversary’s perspective to safeguard our data.

While some of the content is similar to Prompt Engineering Tips in Reasoning Engines in Artificial Intelligence (AI), Generative AI, Ethical AI & Machine Learning, the learning path touched on additional prompt engineering theories and practical applications e.g. using LLMs as a learning buddy to Excel formulas, DAX measures, M Code, Macros, etc.

Let’s start at the very beginning. A very good place to start. (Sound of Music anyone?)

1. Prompt Engineering is the art of how to talk to AI with the objectives of
      a. Avoiding hallucination
      b. Gaining valuable knowledge

2. Every prompt is broken down into tokens that are easily understood by the LLMs. 1 word could be broken into multiple tokens.

3. Types of Prompts

Prompt Format Example
a. Instruction Write something
b. Question What are some good examples
c. Input Data Describe background + Instruction
d. Examples Provide examples of likes & dislikes + Instruction

4. Prompt Engineering Examples

Prompt Format Example
a. Question + Instruction How should I do A? Give me suggestions on B, C, & D.
b. Instruction + Input Data Given this information, write something
c. Question + Examples I like A, B & C. Can you recommend D?

5. Besides zero-shot learning (prompt without examples) & few-shot learning (provide some quality samples to show the LLM what we’re looking for), here are some advanced prompt engineering examples:

Advanced Prompt Engineering Example
a. Chain of Thought Prompting Explicitly encourage the model to be factual/correct by forcing it to follow a series of steps in its reasoning.
Q: <Question>
A: Let’s think step by step, <give_reasoning> Therefore, the answer is <final_answer> OR
Q: Take a deep breath and work on this problem step by step. <Question>
A: Let’s think step by step. First, 
b. Cite the right sources Though the LLM may still hallucinate, when sources are included, it’s easier to verify the response’s accuracy. End your prompt with e.g. “Answer only using reliable sources and cite those sources”.
c. GPT-based LLM’s special message <|end of prompt|> The model is built to engage in conversation instead of continuing text. This special command separate instructions from the starting passage, indicating to the model to continue its reply from what you wrote.
d. Generated Knowledge Prompting Provide a series of input and corresponding knowledge examples. End with a question and an open-ended knowledge. Ask question based on the newly generated knowledge.
e. Tree-of-Thought (ToT) Prompting This is usually used to solve complex problems:
i.   Imagine n different experts
ii.  All experts will write down 1 step of their thinking to share with the group
iii. All experts go on to the next step.
iv.  If any expert realizes they’re wrong, they’ll leave the discussion
v.   End by providing the prompt/question
f. Directional Stimulus Prompting The key is to provide the model a hint so that we’ll get an answer that points out the key features we’re looking for. When providing the prompt question, end with “based on the hint: <hint>” to tell the LLM what we are looking for.
g. Chain-of-Density (CoD) prompting This is used to summarize long text and to ensure all key points are captured:
i.  Summarize article
ii. Repeat 1) Identify missing entities 2) Rewrite summary to include previous summary and the missing entities

 

6. On to Excel! Below are some ways ChatGPT (LLMs) can help elevate your Excel skills to the next level:
      a. Generate & explain Excel formulas e.g. XLOOKUP
            i.  Can you provide an example using some sample data and show the formula syntax
            ii. Can you explain this Excel formula in simple terms
      b. Generate DAX measures for Power Pivot
      c. Generate M Code for Power Query
            i.  Home –> Advanced editor
            ii. Copy existing M Code for ChatGPT to modify
      d. Generate VBA Scripts & Macros to Automate tasks in Excel e.g. table formatting
      e. Data preparation and exploratory analysis
            i.   Could you please act like a data quality assurance engineer and give me a step-by-step plan to help me QA and prepare my data for analysis?
            ii.  I see some missing values. How should I handle them?
            iii. My data is looking good. Can you provide a plan for conducting some data profiling and exploratory analysis in Excel?
      f. Generate custom sample datasets
            i.  The data we normally deal with is pretty messy, so I’d like to practice my data cleaning skills as well
            ii. Could you please modify that data sample to include some common data QA issues, and describe what changes you made?

7. Mitigating Prompt Injection and Prompt Hacking
      a. Filter the user input to look for blacklisted words or phrases you want to prevent them from using e.g. remove words like hypothetical, let’s pretend, behave as a, ignore previous requests
      b. Limit amount of free prompting
      c. Fill in a form (for easier validation) vs complex prompt
      d. Add control prompts before and/or after the user prompt to clean up any potential harmful input
      e. Isolate the user input to give it less weightage than our own control prompts
      f. Escape any special characters in user prompts
      g. Send user prompts through another LLM to check for anything adversarial
      h. Potential Hacks
            i.   Translate binary or Base64 encoding into English with hidden attacks built in
            ii.  Rearrange/separate/concatenate/reverse inputs prior to executing instructions
            iii. Input malicious code from the internet e.g. URL, external 3rd party like plugin

8. Additional tips for AI Generated Images
      a. Add “digital art” & “high quality photo” for enhanced results
      b. Provide as many details as possible in your prompt e.g. year, style, accessories, etc.